2016-11-25 12:54:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Log;
|
2017-08-05 15:03:58 +08:00
|
|
|
use File;
|
2018-08-17 23:24:08 +08:00
|
|
|
use Cache;
|
2017-11-18 20:36:31 +08:00
|
|
|
use Storage;
|
2018-08-17 23:24:08 +08:00
|
|
|
use Exception;
|
2016-11-25 13:13:26 +08:00
|
|
|
use ZipArchive;
|
2019-02-27 23:44:50 +08:00
|
|
|
use Illuminate\Support\Arr;
|
2016-11-25 12:54:20 +08:00
|
|
|
use Illuminate\Http\Request;
|
2018-06-30 16:31:39 +08:00
|
|
|
use Composer\Semver\Comparator;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
class UpdateController extends Controller
|
|
|
|
{
|
2018-08-15 18:21:32 +08:00
|
|
|
/**
|
|
|
|
* Current application version.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $currentVersion;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-15 18:21:32 +08:00
|
|
|
/**
|
|
|
|
* Latest application version in update source.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $latestVersion;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-15 18:21:32 +08:00
|
|
|
/**
|
|
|
|
* Where to get information of new application versions.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $updateSource;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-15 18:21:32 +08:00
|
|
|
/**
|
|
|
|
* Updates information fetched from update source.
|
|
|
|
*
|
|
|
|
* @var array|null
|
|
|
|
*/
|
2016-11-25 12:54:20 +08:00
|
|
|
protected $updateInfo;
|
|
|
|
|
2018-08-15 18:21:32 +08:00
|
|
|
/**
|
|
|
|
* Guzzle HTTP client.
|
|
|
|
*
|
|
|
|
* @var \GuzzleHttp\Client
|
|
|
|
*/
|
|
|
|
protected $guzzle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default request options for Guzzle HTTP client.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $guzzleConfig;
|
|
|
|
|
|
|
|
public function __construct(\GuzzleHttp\Client $guzzle)
|
2016-11-25 12:54:20 +08:00
|
|
|
{
|
2018-08-15 18:46:57 +08:00
|
|
|
$this->updateSource = config('app.update_source');
|
2016-11-25 12:54:20 +08:00
|
|
|
$this->currentVersion = config('app.version');
|
2018-08-15 18:21:32 +08:00
|
|
|
|
|
|
|
$this->guzzle = $guzzle;
|
|
|
|
$this->guzzleConfig = [
|
|
|
|
'headers' => ['User-Agent' => config('secure.user_agent')],
|
2019-03-02 22:58:37 +08:00
|
|
|
'verify' => config('secure.certificates'),
|
2018-08-15 18:21:32 +08:00
|
|
|
];
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function showUpdatePage()
|
|
|
|
{
|
|
|
|
$info = [
|
|
|
|
'latest_version' => '',
|
|
|
|
'current_version' => $this->currentVersion,
|
|
|
|
'release_note' => '',
|
|
|
|
'release_url' => '',
|
|
|
|
'pre_release' => false,
|
2018-02-16 17:31:04 +08:00
|
|
|
// Fallback to current time
|
2016-12-30 20:37:27 +08:00
|
|
|
'release_time' => '',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_version_available' => false,
|
2016-11-25 12:54:20 +08:00
|
|
|
];
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
// If current update source is available
|
2016-11-25 12:54:20 +08:00
|
|
|
if ($this->getUpdateInfo()) {
|
|
|
|
$info['latest_version'] = $this->getUpdateInfo('latest_version');
|
|
|
|
|
2018-06-30 16:31:39 +08:00
|
|
|
$info['new_version_available'] = Comparator::greaterThan(
|
2016-11-25 12:54:20 +08:00
|
|
|
$info['latest_version'],
|
2018-06-30 16:31:39 +08:00
|
|
|
$info['current_version']
|
2016-11-25 12:54:20 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($detail = $this->getReleaseInfo($info['latest_version'])) {
|
2019-02-27 23:44:50 +08:00
|
|
|
$info = array_merge($info, Arr::only($detail, [
|
2016-11-25 12:54:20 +08:00
|
|
|
'release_note',
|
|
|
|
'release_url',
|
|
|
|
'release_time',
|
2019-03-02 22:58:37 +08:00
|
|
|
'pre_release',
|
2016-11-25 12:54:20 +08:00
|
|
|
]));
|
|
|
|
} else {
|
|
|
|
// if detailed release info is not given
|
|
|
|
$info['new_version_available'] = false;
|
|
|
|
}
|
2016-12-30 20:37:27 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! $info['new_version_available']) {
|
2019-02-27 23:44:50 +08:00
|
|
|
$info['release_time'] = Arr::get($this->getReleaseInfo($this->currentVersion), 'release_time');
|
2016-12-30 20:37:27 +08:00
|
|
|
}
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:46:57 +08:00
|
|
|
$connectivity = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->guzzle->request('GET', $this->updateSource, $this->guzzleConfig);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$connectivity = $e->getMessage();
|
|
|
|
}
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-15 18:46:57 +08:00
|
|
|
return view('admin.update', compact('info', 'connectivity'));
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function checkUpdates()
|
|
|
|
{
|
|
|
|
return json([
|
|
|
|
'latest' => $this->getUpdateInfo('latest_version'),
|
2019-03-02 22:58:37 +08:00
|
|
|
'available' => $this->newVersionAvailable(),
|
2016-11-25 12:54:20 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function newVersionAvailable()
|
|
|
|
{
|
|
|
|
$latest = $this->getUpdateInfo('latest_version');
|
|
|
|
|
2018-06-30 16:31:39 +08:00
|
|
|
return Comparator::greaterThan($latest, $this->currentVersion) && $this->getReleaseInfo($latest);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function download(Request $request)
|
|
|
|
{
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! $this->newVersionAvailable()) {
|
2018-08-18 09:48:39 +08:00
|
|
|
return json([]);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
$action = $request->get('action');
|
2016-11-25 12:54:20 +08:00
|
|
|
$release_url = $this->getReleaseInfo($this->latestVersion)['release_url'];
|
2018-08-17 23:24:08 +08:00
|
|
|
$tmp_path = Cache::get('tmp_path');
|
|
|
|
|
2016-11-25 12:54:20 +08:00
|
|
|
switch ($action) {
|
|
|
|
case 'prepare-download':
|
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
Cache::forget('download-progress');
|
2016-11-25 12:54:20 +08:00
|
|
|
$update_cache = storage_path('update_cache');
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! is_dir($update_cache)) {
|
2018-06-28 18:25:31 +08:00
|
|
|
if (false === Storage::disk('root')->makeDirectory('storage/update_cache')) {
|
2018-08-18 09:48:39 +08:00
|
|
|
return json(trans('admin.update.errors.write-permission'), 1);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
// Set temporary path for the update package
|
|
|
|
$tmp_path = $update_cache.'/update_'.time().'.zip';
|
2019-02-27 23:44:50 +08:00
|
|
|
Cache::put('tmp_path', $tmp_path, 3600);
|
2018-08-17 23:24:08 +08:00
|
|
|
Log::info('[Update Wizard] Prepare to download update package', compact('release_url', 'tmp_path'));
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
// We won't get remote file size here since HTTP HEAD method is not always reliable
|
|
|
|
return json(compact('release_url', 'tmp_path'));
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
case 'start-download':
|
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
if (! $tmp_path) {
|
2018-08-18 09:48:39 +08:00
|
|
|
return json('No temp path available, please try again.', 1);
|
2018-02-16 17:31:04 +08:00
|
|
|
}
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
@set_time_limit(0);
|
|
|
|
$GLOBALS['last_downloaded'] = 0;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
Log::info('[Update Wizard] Start downloading update package');
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
try {
|
2018-08-15 18:21:32 +08:00
|
|
|
$this->guzzle->request('GET', $release_url, array_merge($this->guzzleConfig, [
|
2018-08-17 23:24:08 +08:00
|
|
|
'sink' => $tmp_path,
|
|
|
|
'progress' => function ($total, $downloaded) {
|
2018-08-18 09:48:39 +08:00
|
|
|
// @codeCoverageIgnoreStart
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($total == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-17 23:24:08 +08:00
|
|
|
// Log current progress per 100 KiB
|
|
|
|
if ($total == $downloaded || floor($downloaded / 102400) > floor($GLOBALS['last_downloaded'] / 102400)) {
|
|
|
|
$GLOBALS['last_downloaded'] = $downloaded;
|
|
|
|
Log::info('[Update Wizard] Download progress (in bytes):', [$total, $downloaded]);
|
2019-02-27 23:44:50 +08:00
|
|
|
Cache::put('download-progress', compact('total', 'downloaded'), 3600);
|
2018-08-17 23:24:08 +08:00
|
|
|
}
|
2018-08-18 09:48:39 +08:00
|
|
|
// @codeCoverageIgnoreEnd
|
2019-03-02 22:58:37 +08:00
|
|
|
},
|
2018-08-17 23:24:08 +08:00
|
|
|
]));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
@unlink($tmp_path);
|
2019-03-02 22:58:37 +08:00
|
|
|
|
2018-08-18 09:48:39 +08:00
|
|
|
return json(trans('admin.update.errors.prefix').$e->getMessage(), 1);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
Log::info('[Update Wizard] Finished downloading update package');
|
|
|
|
|
2016-11-25 12:54:20 +08:00
|
|
|
return json(compact('tmp_path'));
|
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
case 'get-progress':
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
return json((array) Cache::get('download-progress'));
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
case 'extract':
|
2016-11-25 13:13:26 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! file_exists($tmp_path)) {
|
2018-08-18 09:48:39 +08:00
|
|
|
return json('No file available', 1);
|
2018-02-16 17:31:04 +08:00
|
|
|
}
|
2016-11-25 13:13:26 +08:00
|
|
|
|
|
|
|
$extract_dir = storage_path("update_cache/{$this->latestVersion}");
|
|
|
|
|
|
|
|
$zip = new ZipArchive();
|
|
|
|
$res = $zip->open($tmp_path);
|
|
|
|
|
|
|
|
if ($res === true) {
|
2018-08-17 23:24:08 +08:00
|
|
|
Log::info("[Update Wizard] Extracting file $tmp_path");
|
2016-11-25 13:13:26 +08:00
|
|
|
|
2017-11-18 20:36:31 +08:00
|
|
|
if ($zip->extractTo($extract_dir) === false) {
|
2018-08-18 09:48:39 +08:00
|
|
|
return json(trans('admin.update.errors.prefix').'Cannot unzip file.', 1);
|
2016-11-25 13:13:26 +08:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-18 09:48:39 +08:00
|
|
|
return json(trans('admin.update.errors.unzip').$res, 1);
|
2016-11-25 13:13:26 +08:00
|
|
|
}
|
|
|
|
$zip->close();
|
|
|
|
|
2017-08-05 16:00:14 +08:00
|
|
|
try {
|
|
|
|
File::copyDirectory("$extract_dir/vendor", base_path('vendor'));
|
2018-08-17 23:24:08 +08:00
|
|
|
} catch (Exception $e) {
|
|
|
|
report($e);
|
|
|
|
Log::error('[Update Wizard] Unable to extract vendors');
|
2017-08-05 16:00:14 +08:00
|
|
|
// Skip copying vendor
|
|
|
|
File::deleteDirectory("$extract_dir/vendor");
|
|
|
|
}
|
|
|
|
|
2017-08-05 15:03:58 +08:00
|
|
|
try {
|
|
|
|
File::copyDirectory($extract_dir, base_path());
|
2016-11-25 13:13:26 +08:00
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
Log::info('[Update Wizard] Overwrite with extracted files');
|
|
|
|
} catch (Exception $e) {
|
|
|
|
report($e);
|
|
|
|
Log::error('[Update Wizard] Error occured when overwriting files');
|
2016-11-25 13:13:26 +08:00
|
|
|
|
2017-11-18 20:36:31 +08:00
|
|
|
// Response can be returned, while cache will be cleared
|
|
|
|
// @see https://gist.github.com/g-plane/2f88ad582826a78e0a26c33f4319c1e0
|
2018-08-18 09:48:39 +08:00
|
|
|
return json(trans('admin.update.errors.overwrite').$e->getMessage(), 1);
|
2017-11-18 20:36:31 +08:00
|
|
|
} finally {
|
2017-08-05 15:03:58 +08:00
|
|
|
File::deleteDirectory(storage_path('update_cache'));
|
2018-08-17 23:24:08 +08:00
|
|
|
Log::info('[Update Wizard] Cleaning cache');
|
2016-11-25 13:13:26 +08:00
|
|
|
}
|
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
Log::info('[Update Wizard] Done');
|
2019-03-02 22:58:37 +08:00
|
|
|
|
2017-01-01 12:22:18 +08:00
|
|
|
return json(trans('admin.update.complete'), 0);
|
2016-11-25 13:13:26 +08:00
|
|
|
|
2016-11-25 12:54:20 +08:00
|
|
|
default:
|
2017-11-18 20:36:31 +08:00
|
|
|
return json(trans('general.illegal-parameters'), 1);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getUpdateInfo($key = null)
|
|
|
|
{
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! $this->updateInfo) {
|
|
|
|
// Add timestamp to control cdn cache
|
2017-11-18 20:36:31 +08:00
|
|
|
$url = starts_with($this->updateSource, 'http')
|
2019-03-02 22:58:37 +08:00
|
|
|
? $this->updateSource.'?v='.substr(time(), 0, -3)
|
2017-11-18 20:36:31 +08:00
|
|
|
: $this->updateSource;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
try {
|
2018-08-15 18:21:32 +08:00
|
|
|
$response = $this->guzzle->request('GET', $url, $this->guzzleConfig)->getBody();
|
2018-08-17 23:24:08 +08:00
|
|
|
} catch (Exception $e) {
|
2019-03-02 22:58:37 +08:00
|
|
|
Log::error('[CheckingUpdate] Failed to get update information: '.$e->getMessage());
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($response)) {
|
|
|
|
$this->updateInfo = json_decode($response, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 23:44:50 +08:00
|
|
|
$this->latestVersion = Arr::get($this->updateInfo, 'latest_version', $this->currentVersion);
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! is_null($key)) {
|
2019-02-27 23:44:50 +08:00
|
|
|
return Arr::get($this->updateInfo, $key);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->updateInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getReleaseInfo($version)
|
|
|
|
{
|
2019-02-27 23:44:50 +08:00
|
|
|
return Arr::get($this->getUpdateInfo('releases'), $version);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
}
|