-
Notifications
You must be signed in to change notification settings - Fork 594
/
Action.php
110 lines (89 loc) · 3.32 KB
/
Action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* Typecho update assistant.
*
* @package UpdateAssistant
* @author mrgeneral
* @version 1.0.1
* @link https://www.chengxiaobai.cn
*/
class UpdateAssistant_Action extends Typecho_Widget implements Widget_Interface_Do
{
private $pluginRootPath;
private $isDevelop;
/**
* @var array realPath => realRootPath
*/
private $updateList = [
// admin
__TYPECHO_ROOT_DIR__ . __TYPECHO_ADMIN_DIR__ => __TYPECHO_ROOT_DIR__,
// var
__TYPECHO_ROOT_DIR__ . DIRECTORY_SEPARATOR . 'var/' => __TYPECHO_ROOT_DIR__,
//index.php
__TYPECHO_ROOT_DIR__ . DIRECTORY_SEPARATOR . 'index.php' => __TYPECHO_ROOT_DIR__,
];
/**
* @var array realPath => rawName
*/
private $updateNameMap = [
__TYPECHO_ROOT_DIR__ . __TYPECHO_ADMIN_DIR__ => 'admin',
__TYPECHO_ROOT_DIR__ . DIRECTORY_SEPARATOR . 'var/' => 'var',
__TYPECHO_ROOT_DIR__ . DIRECTORY_SEPARATOR . 'index.php' => 'index.php',
];
public function __construct($request, $response, $params = null)
{
parent::__construct($request, $response, $params);
if (!Typecho_Widget::widget('Widget_User')->pass('administrator', true)) {
throw new Typecho_Exception(_t('Forbidden'), 403);
}
$this->pluginRootPath = dirname(realpath(__FILE__));
$this->isDevelop = (bool)Helper::options()->plugin('UpdateAssistant')->isDevelop;
$this->autoLoad();
}
public function action()
{
if (Version::compare(Typecho_Common::VERSION, Version::getVersion($this->isDevelop), '=')) {
$this->response('Already up-to-date!');
}
// download
$archiveName = ($this->isDevelop ? 'develop_' : 'release_') . Version::toString(Version::getVersion($this->isDevelop));
Downloader::down($this->isDevelop, $archiveName, $this->pluginRootPath);
// backup
Archive::compress(sprintf('local_%s', Version::toString(Typecho_Common::VERSION)), $this->updateList, $this->pluginRootPath);
// decompression
$resultPath = Archive::decompression($archiveName, $this->pluginRootPath);
// update
foreach ($this->updateList as $realPath => $realRootPath) {
Archive::clearPath($realPath);
rename($resultPath . $this->updateNameMap[$realPath], $realPath);
}
// finish
$this->response('success');
}
public function getVersion()
{
$this->response(Version::getVersion($this->isDevelop));
}
protected function response($data, $code = 0)
{
headers_sent() || header('Content-Type: application/json; charset=utf-8;');
echo json_encode(
[
'code' => $code,
'data' => $data,
], JSON_UNESCAPED_UNICODE
);
exit(0);
}
protected function autoLoad()
{
$dependencePath = $this->pluginRootPath . DIRECTORY_SEPARATOR . 'library';
// First load base class
include_once $dependencePath . DIRECTORY_SEPARATOR . 'Base.php';
foreach (scandir($dependencePath) as $item) {
if (strpos($item, '.php') !== false && strpos($item, 'Base.php') === false) {
include_once $dependencePath . DIRECTORY_SEPARATOR . $item;
}
}
}
}