From 967ef3e1b068bd06019dfd302c26353a8c077a18 Mon Sep 17 00:00:00 2001 From: printempw Date: Mon, 2 Jan 2017 16:23:31 +0800 Subject: [PATCH] fix comparing versions contain hyphen --- app/Http/Controllers/SetupController.php | 4 +- app/Http/Controllers/UpdateController.php | 4 +- app/Providers/BootServiceProvider.php | 2 +- app/Services/Utils.php | 38 +++++++++++++++++++ .../update_scripts/update-3.1.1-to-3.2.php | 14 ------- ...-3.2-pr8.php => update-3.1.2-to-3.2.0.php} | 8 +++- 6 files changed, 49 insertions(+), 21 deletions(-) delete mode 100644 database/update_scripts/update-3.1.1-to-3.2.php rename database/update_scripts/{update-3.2-to-3.2-pr8.php => update-3.1.2-to-3.2.0.php} (75%) diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 9d107e49..053e1c89 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -97,7 +97,7 @@ class SetupController extends Controller public function update() { - if (version_compare(config('app.version'), option('version', ''), '<=')) { + if (Utils::versionCompare(config('app.version'), option('version', ''), '<=')) { // no updates available return view('setup.locked'); } @@ -118,7 +118,7 @@ class SetupController extends Controller // skip if the file is not valid or expired if (!isset($matches[2]) || - version_compare($matches[2], config('app.version'), '<')) { + Utils::versionCompare($matches[2], config('app.version'), '<')) { continue; } diff --git a/app/Http/Controllers/UpdateController.php b/app/Http/Controllers/UpdateController.php index 0378cf1e..02d757a5 100644 --- a/app/Http/Controllers/UpdateController.php +++ b/app/Http/Controllers/UpdateController.php @@ -45,7 +45,7 @@ class UpdateController extends Controller if ($this->getUpdateInfo()) { $info['latest_version'] = $this->getUpdateInfo('latest_version'); - $info['new_version_available'] = version_compare( + $info['new_version_available'] = Utils::versionCompare( $info['latest_version'], $info['current_version'], '>' ); @@ -95,7 +95,7 @@ class UpdateController extends Controller { $latest = $this->getUpdateInfo('latest_version'); - return version_compare($latest, $this->currentVersion, '>') && $this->getReleaseInfo($latest); + return Utils::versionCompare($latest, $this->currentVersion, '>') && $this->getReleaseInfo($latest); } public function download(Request $request) diff --git a/app/Providers/BootServiceProvider.php b/app/Providers/BootServiceProvider.php index cbcb58b4..317510f1 100644 --- a/app/Providers/BootServiceProvider.php +++ b/app/Providers/BootServiceProvider.php @@ -60,7 +60,7 @@ class BootServiceProvider extends ServiceProvider throw new PrettyPageException(trans('setup.file.permission-error'), -1); } - if (version_compare(config('app.version'), option('version', ''), '>')) { + if (Utils::versionCompare(config('app.version'), option('version', ''), '>')) { return redirect('/setup/update')->send(); } diff --git a/app/Services/Utils.php b/app/Services/Utils.php index 7c3bb65f..b9dff163 100644 --- a/app/Services/Utils.php +++ b/app/Services/Utils.php @@ -3,6 +3,7 @@ namespace App\Services; use Log; +use Illuminate\Support\Str; use Storage as LaravelStorage; use App\Exceptions\PrettyPageException; @@ -26,6 +27,43 @@ class Utils return $ip; } + public static function versionCompare($version1, $version2, $operator = null) + { + $versions = [$version1, $version2]; + + // pre-processing for version contains hyphen + foreach ([0, 1] as $offset) { + if (false !== ($result = self::parseVersionWithHyphen($versions[$offset]))) { + $versions[$offset] = $result; + } else { + $versions[$offset] = ['main' => $versions[$offset], 'sub' => '']; + } + } + + if (version_compare($versions[0]['main'], $versions[1]['main'], '=')) { + // v3.2-pr < v3.2 + if ($versions[0]['sub'] != "" || $versions[1]['sub'] != "") { + return version_compare($versions[0]['sub'], $versions[1]['sub'], $operator); + } + } + + return version_compare($versions[0]['main'], $versions[1]['main'], $operator); + } + + public static function parseVersionWithHyphen($version) + { + preg_match('/(.*)-(.*)/', $version, $matches); + + if (isset($matches[2])) { + return [ + 'main' => $matches[1], + 'sub' => $matches[2] + ]; + } + + return false; + } + /** * Rename uploaded file * diff --git a/database/update_scripts/update-3.1.1-to-3.2.php b/database/update_scripts/update-3.1.1-to-3.2.php deleted file mode 100644 index 0e896c2b..00000000 --- a/database/update_scripts/update-3.1.1-to-3.2.php +++ /dev/null @@ -1,14 +0,0 @@ - $value) { } } -Option::set('version', '3.2-pr8'); +Option::set('version', '3.2.0'); + +return [ + 'v3.2 新加入了插件系统,支持的插件请去程序发布帖查看' +];