From 6cef94924ec3ec677570d0547d7fa90bd6619676 Mon Sep 17 00:00:00 2001 From: printempw Date: Tue, 9 Aug 2016 13:18:27 +0800 Subject: [PATCH] add checking for updates --- app/Controllers/AdminController.php | 14 +++ app/Services/Updater.php | 37 ++++--- config/menu.php | 3 +- config/routes.php | 1 + config/services.php | 29 +++--- config/update.php | 20 ++++ resources/views/admin/master.tpl | 11 ++ resources/views/admin/options.tpl | 4 +- resources/views/admin/update.tpl | 156 ++++++++++++++++++++++++++++ setup/options.php | 6 +- 10 files changed, 250 insertions(+), 31 deletions(-) create mode 100644 config/update.php create mode 100644 resources/views/admin/update.tpl diff --git a/app/Controllers/AdminController.php b/app/Controllers/AdminController.php index c1ddbc7a..604178e2 100644 --- a/app/Controllers/AdminController.php +++ b/app/Controllers/AdminController.php @@ -35,6 +35,20 @@ class AdminController extends BaseController View::show('admin.options'); } + public function update() + { + if (\Utils::getValue('action', $_GET) == "check") { + $updater = new \Updater(\Application::getVersion()); + if ($updater->newVersionAvailable()) { + View::json([ + 'new_version_available' => true, + 'latest_version' => $updater->latest_version + ]); + } + } + View::show('admin.update'); + } + public function users() { $page = isset($_GET['page']) ? $_GET['page'] : 1; diff --git a/app/Services/Updater.php b/app/Services/Updater.php index b67cf3f7..b290e06c 100644 --- a/app/Services/Updater.php +++ b/app/Services/Updater.php @@ -5,22 +5,26 @@ namespace App\Services; class Updater { public $current_version = ""; - public $latest_version = ""; + public $latest_version = ""; - public $update_time = ""; + public $update_time = ""; - public $update_url = ""; + private $update_sources = null; + private $current_source = null; - public $update_info = null; + private $update_info = null; - function __construct($current_version) { + public function __construct($current_version) + { $this->current_version = $current_version; - $this->update_url = Option::get('update_url'); + $this->update_sources = require BASE_DIR."/config/update.php"; + $this->current_source = $this->update_sources[Option::get('update_source')]; } - public function getUpdateInfo() { + public function getUpdateInfo() + { $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $this->update_url); + curl_setopt($ch, CURLOPT_URL, $this->current_source['update_url']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // quick fix for accessing https resources curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); @@ -30,13 +34,15 @@ class Updater return $this->update_info; } - public function checkUpdate() { + public function checkUpdate() + { $info = $this->getUpdateInfo(); $this->latest_version = $info['latest_version']; $this->update_time = date('Y-m-d H:i:s', $info['update_time']); } - public function downloadUpdate($silent = true) { + public function downloadUpdate($silent = true) + { $release_url = $this->update_info['releases'][$this->latest_version]['release_url']; if (!$silent) echo "

正在下载更新包:$release_url

"; // I don't know why curl cant get full file here.. @@ -60,11 +66,17 @@ class Updater * * @return bool */ - public function newVersionAvailable() { + public function newVersionAvailable() + { $this->checkUpdate(); return $this->compareVersion($this->latest_version, $this->current_version); } + public function getUpdateSources() + { + return $this->update_sources; + } + /** * Compare version string * @@ -72,7 +84,8 @@ class Updater * @param string $v2 * @return boolean */ - private function compareVersion($v1, $v2) { + private function compareVersion($v1, $v2) + { if (strnatcasecmp($v1, $v2) > 0) { // v1 > v2 return true; diff --git a/config/menu.php b/config/menu.php index 88226415..cc56bcb0 100644 --- a/config/menu.php +++ b/config/menu.php @@ -22,7 +22,8 @@ $menu['admin'] = array( 3 => ['title' => '角色管理', 'link' => '/admin/players', 'icon' => 'fa-gamepad'], 4 => ['title' => '个性化', 'link' => '/admin/customize', 'icon' => 'fa-paint-brush'], 5 => ['title' => '积分配置', 'link' => '/admin/score', 'icon' => 'fa-credit-card'], - 6 => ['title' => '站点配置', 'link' => '/admin/options', 'icon' => 'fa-cog'] + 6 => ['title' => '站点配置', 'link' => '/admin/options', 'icon' => 'fa-cog'], + 6 => ['title' => '检查更新', 'link' => '/admin/update', 'icon' => 'fa-arrow-up'] ); return $menu; diff --git a/config/routes.php b/config/routes.php index 4b17ab5c..d7e8414b 100644 --- a/config/routes.php +++ b/config/routes.php @@ -100,6 +100,7 @@ Route::group(['middleware' => 'App\Middlewares\CheckAdminMiddl Route::all('/customize', 'AdminController@customize'); Route::all('/score', 'AdminController@score'); Route::all('/options', 'AdminController@options'); + Route::all('/update', 'AdminController@update'); Route::get('/users', 'AdminController@users'); Route::get('/players', 'AdminController@players'); diff --git a/config/services.php b/config/services.php index 2db1ec1c..74342d61 100644 --- a/config/services.php +++ b/config/services.php @@ -10,18 +10,19 @@ | */ return [ - 'View' => 'App\Services\View', - 'Database' => 'App\Services\Database', - 'Option' => 'App\Services\Option', - 'Utils' => 'App\Services\Utils', - 'Validate' => 'App\Services\Validate', - 'Http' => 'App\Services\Http', - 'Mail' => 'App\Services\Mail', - 'Storage' => 'App\Services\Storage', - 'Minecraft' => 'App\Services\Minecraft', - 'Updater' => 'App\Services\Updater', - 'Config' => 'App\Services\Config', - 'Schema' => 'App\Services\Schema', - 'Boot' => 'App\Services\Boot', - 'Migration' => 'App\Services\Migration' + 'View' => 'App\Services\View', + 'Database' => 'App\Services\Database', + 'Option' => 'App\Services\Option', + 'Utils' => 'App\Services\Utils', + 'Validate' => 'App\Services\Validate', + 'Http' => 'App\Services\Http', + 'Mail' => 'App\Services\Mail', + 'Storage' => 'App\Services\Storage', + 'Minecraft' => 'App\Services\Minecraft', + 'Updater' => 'App\Services\Updater', + 'Config' => 'App\Services\Config', + 'Schema' => 'App\Services\Schema', + 'Boot' => 'App\Services\Boot', + 'Migration' => 'App\Services\Migration', + 'Application' => 'App\Services\Application' ]; diff --git a/config/update.php b/config/update.php new file mode 100644 index 00000000..821877ad --- /dev/null +++ b/config/update.php @@ -0,0 +1,20 @@ + [ + 'name' => 'GitHub', + 'update_url' => 'https://work.prinzeugen.net/update.json', + ], + 'local' => [ + 'name' => 'LocalHost(开发用)', + 'update_url' => 'http://127.0.0.1/test/update.json' + ] +); diff --git a/resources/views/admin/master.tpl b/resources/views/admin/master.tpl index bdd4ce9c..296dd70e 100644 --- a/resources/views/admin/master.tpl +++ b/resources/views/admin/master.tpl @@ -131,6 +131,17 @@ + @if (Option::get('check_update') == '1') + + @endif + @yield('script') diff --git a/resources/views/admin/options.tpl b/resources/views/admin/options.tpl index de46d4ce..a943c789 100644 --- a/resources/views/admin/options.tpl +++ b/resources/views/admin/options.tpl @@ -100,10 +100,10 @@ 首选 JSON API - - + diff --git a/resources/views/admin/update.tpl b/resources/views/admin/update.tpl new file mode 100644 index 00000000..ceced168 --- /dev/null +++ b/resources/views/admin/update.tpl @@ -0,0 +1,156 @@ +@extends('admin.master') + +@section('title', '检查更新') + +@section('content') + + +
+ +
+

+ 检查更新 + Check Update +

+
+ + + +
+
+
+
+
+

更新信息

+
+
+ @if ($updater->newVersionAvailable()) +
有更新可用。
+ + + + + + + + + + + + + + + + + + + + + + + + +
最新版本: + v{{ $updater->latest_version }} +
当前版本: + v{{ $updater->current_version }} +
发布时间: + {{ $updater->update_time }} +
更新日志: + {{ nl2br($updater->getUpdateInfo()['releases'][$updater->latest_version]['release_note']) }} +
下载地址: + @GitHub +
+ @else +
已更新至最新版本。
+ + + + + + + + + + + +
当前版本: + v{{ $updater->current_version }} +
发布时间: + {{ date('Y-m-d H:i:s', $updater->getUpdateInfo()['releases'][$updater->current_version]['release_time']) }} +
+ @endif +
+ +
+ +
+
+

注意事项

+
+
+

下载更新需要连接 GitHub 服务器,国内主机可能会长时间无响应。

+
+
+
+ +
+
+
+

更新选项

+
+
+
+
+ $value) { + if ($key != "option" && $key != "submit") + Option::set($key, $value); + } + echo '
设置已保存。
'; + } ?> + + + + + + + + + + + + + + +
检查更新 + +
更新源 + +
+
+ +
+
+
+
+ +
+
+
+ +@endsection diff --git a/setup/options.php b/setup/options.php index b213a816..74256cf1 100644 --- a/setup/options.php +++ b/setup/options.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-07-29 11:53:11 * @Last Modified by: printempw - * @Last Modified time: 2016-08-08 21:51:58 + * @Last Modified time: 2016-08-08 22:41:23 */ return [ @@ -30,5 +30,7 @@ return [ 'score_per_player' => '100', 'sign_after_zero' => '0', 'avatar_query_string' => '0', - 'version' => '' + 'version' => '', + 'check_update' => '1', + 'update_source' => 'github' ];