blessing-skin-server/app/Services/Updater.php

168 lines
3.7 KiB
PHP
Raw Normal View History

2016-03-27 16:34:38 +08:00
<?php
2016-07-21 22:01:57 +08:00
namespace App\Services;
2016-03-27 16:34:38 +08:00
2016-08-27 18:11:34 +08:00
use \Blessing\Storage;
2016-03-27 16:34:38 +08:00
class Updater
{
2016-08-27 18:11:34 +08:00
/**
* Current version
*
* @var string
*/
2016-03-27 16:34:38 +08:00
public $current_version = "";
2016-08-27 18:11:34 +08:00
/**
* Latest version
*
* @var string
*/
2016-08-09 13:18:27 +08:00
public $latest_version = "";
2016-03-27 16:34:38 +08:00
2016-08-27 18:11:34 +08:00
/**
* Latest update time in Y-m-d H:i:s format
*
* @var string
*/
2016-08-09 13:18:27 +08:00
public $update_time = "";
2016-03-27 16:34:38 +08:00
2016-08-27 18:11:34 +08:00
/**
* See /config/update.php
*
* @var array
*/
2016-08-09 13:18:27 +08:00
private $update_sources = null;
2016-08-27 18:11:34 +08:00
/**
* Current selected update source
*
* @var array
*/
2016-08-09 13:18:27 +08:00
private $current_source = null;
2016-04-03 21:49:19 +08:00
2016-08-27 18:11:34 +08:00
/**
* Details of updates
*
* @var array
*/
2016-08-09 13:18:27 +08:00
private $update_info = null;
2016-03-27 16:34:38 +08:00
2016-08-09 13:18:27 +08:00
public function __construct($current_version)
{
2016-03-27 16:34:38 +08:00
$this->current_version = $current_version;
2016-08-26 22:42:49 +08:00
$this->update_sources = require BASE_DIR."/config/update.php";
2016-08-28 21:48:15 +08:00
$source = \Option::get('update_source');
if (!isset($this->update_sources[$source])) {
Option::set('update_source', config('options.update_source'));
}
2016-08-26 22:42:49 +08:00
$this->current_source = $this->update_sources[\Option::get('update_source')];
2016-03-27 16:34:38 +08:00
}
2016-08-27 18:11:34 +08:00
/**
* Get update info from selected json source
*
* @return array Decoded json
*/
2016-08-09 13:18:27 +08:00
public function getUpdateInfo()
{
2016-03-27 16:34:38 +08:00
$ch = curl_init();
2016-08-09 13:18:27 +08:00
curl_setopt($ch, CURLOPT_URL, $this->current_source['update_url']);
2016-03-27 16:34:38 +08:00
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// quick fix for accessing https resources
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
2016-08-27 18:11:34 +08:00
2016-04-03 21:49:19 +08:00
$this->update_info = json_decode($result, true);
return $this->update_info;
2016-03-27 16:34:38 +08:00
}
2016-08-27 18:11:34 +08:00
/**
* Check for updates
*
* @return void
*/
2016-08-09 13:18:27 +08:00
public function checkUpdate()
{
2016-03-27 16:34:38 +08:00
$info = $this->getUpdateInfo();
2016-08-27 18:11:34 +08:00
2016-03-27 16:34:38 +08:00
$this->latest_version = $info['latest_version'];
2016-08-26 22:42:49 +08:00
$this->update_time = date('Y-m-d H:i:s', $info['update_time']);
2016-03-27 16:34:38 +08:00
}
2016-08-27 18:11:34 +08:00
/**
* Download update files
*
* @param bool $silent Don't print messages
* @return void
*/
2016-08-09 13:18:27 +08:00
public function downloadUpdate($silent = true)
{
2016-04-03 21:49:19 +08:00
$release_url = $this->update_info['releases'][$this->latest_version]['release_url'];
2016-08-27 18:11:34 +08:00
if (!$silent)
echo "<p>正在下载更新包:$release_url </p>";
// I don't know why curl can't get full file content here..
$file = file_get_contents($release_url);
if (!$silent)
echo "<p>下载完成。</p>";
2016-04-03 21:49:19 +08:00
$update_cache = BASE_DIR."/setup/update_cache/";
2016-08-27 18:11:34 +08:00
if (!is_dir($update_cache)) {
if (false === mkdir($update_cache)) {
exit('<p>创建下载缓存文件夹失败,请检查目录权限。</p>');
}
}
2016-04-03 21:49:19 +08:00
$zip_path = $update_cache."update_".time().".zip";
2016-08-27 18:11:34 +08:00
if (Storage::put($zip_path, $file) === false) {
Storage::removeDir(BASE_DIR.'/setup/update_cache/');
2016-04-03 21:49:19 +08:00
return false;
}
2016-08-27 18:11:34 +08:00
2016-04-03 21:49:19 +08:00
return $zip_path;
}
2016-03-27 16:34:38 +08:00
/**
* Check if a new version is available
*
* @return bool
*/
2016-08-09 13:18:27 +08:00
public function newVersionAvailable()
{
2016-03-27 16:34:38 +08:00
$this->checkUpdate();
return $this->compareVersion($this->latest_version, $this->current_version);
}
2016-08-09 13:18:27 +08:00
public function getUpdateSources()
{
return $this->update_sources;
}
2016-03-27 16:34:38 +08:00
/**
* Compare version string
*
* @param string $v1
* @param string $v2
* @return boolean
*/
2016-08-09 13:18:27 +08:00
private function compareVersion($v1, $v2)
{
2016-08-27 18:11:34 +08:00
if (version_compare($v1, $v2) > 0) {
2016-03-27 16:34:38 +08:00
// v1 > v2
return true;
} else {
// v1 < v2 || v1 = v2
return false;
}
}
}