diff --git a/app/Controllers/AdminController.php b/app/Controllers/AdminController.php index a94aaaa8..07e4d4b5 100644 --- a/app/Controllers/AdminController.php +++ b/app/Controllers/AdminController.php @@ -46,13 +46,17 @@ class AdminController extends BaseController 'new_version_available' => true, 'latest_version' => $updater->latest_version ]); + } else { + View::json([ + 'new_version_available' => false, + 'latest_version' => $updater->current_version + ]); } } elseif ($action == "download") { View::show('admin.download'); } else { View::show('admin.update'); } - } public function users() diff --git a/app/Core/Storage.php b/app/Core/Storage.php index 99c8c858..83e8a6bf 100644 --- a/app/Core/Storage.php +++ b/app/Core/Storage.php @@ -19,6 +19,11 @@ class Storage return $result; } + public static function put($filename, $data) + { + return file_put_contents($filename, $data); + } + public static function exists($filename) { return file_exists($filename); diff --git a/app/Services/Updater.php b/app/Services/Updater.php index 4ec44755..3cfe8755 100644 --- a/app/Services/Updater.php +++ b/app/Services/Updater.php @@ -2,16 +2,50 @@ namespace App\Services; +use \Blessing\Storage; + class Updater { + /** + * Current version + * + * @var string + */ public $current_version = ""; + + /** + * Latest version + * + * @var string + */ public $latest_version = ""; + /** + * Latest update time in Y-m-d H:i:s format + * + * @var string + */ public $update_time = ""; + /** + * See /config/update.php + * + * @var array + */ private $update_sources = null; + + /** + * Current selected update source + * + * @var array + */ private $current_source = null; + /** + * Details of updates + * + * @var array + */ private $update_info = null; public function __construct($current_version) @@ -21,6 +55,11 @@ class Updater $this->current_source = $this->update_sources[\Option::get('update_source')]; } + /** + * Get update info from selected json source + * + * @return array Decoded json + */ public function getUpdateInfo() { $ch = curl_init(); @@ -30,34 +69,58 @@ class Updater curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); curl_close($ch); + $this->update_info = json_decode($result, true); return $this->update_info; } + /** + * Check for updates + * + * @return void + */ 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']); } + /** + * Download update files + * + * @param bool $silent Don't print messages + * @return void + */ 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.. - $file = fopen($release_url, 'r'); - if (!$silent) echo "下载完成。
"; + + if (!$silent) + echo "正在下载更新包:$release_url
"; + + // I don't know why curl can't get full file content here.. + $file = file_get_contents($release_url); + + if (!$silent) + echo "下载完成。
"; $update_cache = BASE_DIR."/setup/update_cache/"; - if (!is_dir($update_cache)) mkdir($update_cache); + + if (!is_dir($update_cache)) { + if (false === mkdir($update_cache)) { + exit('创建下载缓存文件夹失败,请检查目录权限。
'); + } + } $zip_path = $update_cache."update_".time().".zip"; - if (file_put_contents($zip_path, $file) === false) { - \Storage::removeDir(BASE_DIR.'/setup/update_cache/'); + if (Storage::put($zip_path, $file) === false) { + Storage::removeDir(BASE_DIR.'/setup/update_cache/'); return false; } + return $zip_path; } @@ -86,7 +149,7 @@ class Updater */ private function compareVersion($v1, $v2) { - if (strnatcasecmp($v1, $v2) > 0) { + if (version_compare($v1, $v2) > 0) { // v1 > v2 return true; } else { diff --git a/config/update.php b/config/update.php index 39e6e8e5..b9c6bbd1 100644 --- a/config/update.php +++ b/config/update.php @@ -9,15 +9,10 @@ */ return array( - 'github' => [ - 'name' => 'GitHub', - 'update_url' => 'https://work.prinzeugen.net/update.json', - 'description' => '从 作者主机 上取更新信息,GitHub Releases 上取更新包。国内主机可能会奇慢无比,请注意。' - ], 'nyavm' => [ 'name' => 'NyaVM', - 'update_url' => 'http://anycast.cdn.nyavm.net/', - 'description' => '啦啦啦' + 'update_url' => 'http://work.prinzeugen.net/update.json', + 'description' => '感谢 NyaVM 提供的 Anycast CDN,国内外主机都可获得不错的速度。NyaVM 主机,百兆 CN2 只需 $3.99 月付,稳定快速大带宽。' ], 'little_service' => [ 'name' => 'LittleService-COS',