forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAction.php
355 lines (322 loc) · 12.4 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php ! defined('__TYPECHO_ROOT_DIR__') and exit();
include_once 'pclzip.lib.php';
class TeStore_Action extends Typecho_Widget {
private $options;
private $server;
private $security;
//缓存时间(h)
private $cacheTime;
//使用Curl
private $useCurl;
private $cacheDir;
private $pluginRoot;
private $pluginInfo;
public function __construct($request, $response, $params = NULL)
{
parent::__construct($request, $response, $params);
$this->options = Helper::options();
$pluginOpts = $this->options->plugin('TeStore');
$this->source = array_filter(preg_split("/(\r|\n|\r\n)/", strip_tags($pluginOpts->source)));
$this->security = Helper::security();
$this->cacheTime = $pluginOpts->cache_time;
$this->useCurl = $pluginOpts->curl;
$this->pluginRoot = __TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__;
$this->cacheDir = $this->pluginRoot . '/TeStore/data/';
define('TYPEHO_ADMIN_PATH', __TYPECHO_ROOT_DIR__ . __TYPECHO_ADMIN_DIR__);
}
/**
* 获取已启用插件名称
*
* @access private
* @return array
*/
private function getActivePlugins()
{
$activatedPlugins = Typecho_Plugin::export();
return array_keys( $activatedPlugins['activated'] );
}
/**
* 获取已安装插件名称
*
* @access private
* @return array
*/
private function getLocalPlugins()
{
$dirs = array();
$files = scandir($this->pluginRoot);
foreach($files as $file){
if( is_dir($this->pluginRoot . '/' . $file) && !in_array($file, array('.', '..')) || strpos($file, '.php') ){
$dirs[] = str_replace('.php', '', $file);
}
}
return $dirs;
}
/**
* 获取已安装插件信息
*
* @access private
* @return string
*/
private function getLocalInfos($name)
{
$pluginDir = $this->pluginRoot . '/' . $name;
$plugin = is_dir($pluginDir) ? $pluginDir . '/Plugin.php' : $pluginDir . '.php';
$parseInfo = Typecho_Plugin::parseInfo($plugin);
return array( $parseInfo['author'], $parseInfo['version'] );
}
/**
* 获取插件源数据
*
* @access private
* @param string $name
* @return array
*/
public function getPluginData($name='')
{
$json = $this->cacheDir . 'list.json';
//读取缓存文件
if( $this->cacheTime && is_file($json) && (time() - filemtime($json)) <= $this->cacheTime * 3600 ){
$data = file_get_contents($json);
$this->pluginInfo = json_decode($data);
}else{
$html = '';
foreach( $this->source as $page ){
$page = trim($page);
if( $page ){
$html .= $this->useCurl ? $this->curlGet($page) : @file_get_contents($page);
}
}
//解析表格内容
if( $html !== '' ){
$dom = new DOMDocument('1.0', 'UTF-8');
$html = function_exists('mb_convert_encoding') ? mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8') : $html;
@$dom->loadHTML($html);
$tr = $dom->getElementsByTagName("tr");
$texts = array();
$urls = array();
foreach( $tr as $trKey => $text ){
if( $text->parentNode->tagName=='tbody' ){
//分割tr文本数据
foreach( $text->childNodes as $td ){
$tdVal = trim($td->nodeValue);
if( $tdVal ){
$texts[$trKey][] = $tdVal;
}
}
$td = $tr->item($trKey)->getElementsByTagName("td");
//获取td链接数据
foreach( $td as $tdKey => $val ){
if( $tdKey!==1 && $tdKey!==2 ) {
$a = $td->item($tdKey)->getElementsByTagName("a");
$href = $a->item(0) ? $a->item(0)->getAttribute("href") : '';
//处理多作者链接
if( $tdKey==3 ){
$href = '';
foreach( $a as $key => $val ){
$href .= ($key==0 ? '' : ', ') . $val->getAttribute('href');
}
}
$urls[] = $href;
}
}
}
}
$texts = array_values($texts);
$urls = array_chunk($urls , 3);
$datas = array();
//合并关联键名
$names = array();
foreach( $texts as $key => $val ){
$keys = array('pluginName', 'desc', 'version', 'author', 'source', 'pluginUrl', 'site', 'zipFile');
$names[] = isset($val[0]) ? $val[0] : $val[1]; //fix for PHP 7.0+
$datas[] = (object)array_combine($keys, array_merge($val, $urls[$key]));
}
array_multisort($names, SORT_ASC, $datas);
$this->pluginInfo = $datas;
}else{
$this->pluginInfo = NULL;
}
//生成缓存文件
if($this->cacheTime) {
$pluginInfo = json_encode($this->pluginInfo);
if( ! is_dir($this->cacheDir) ) @mkdir($this->cacheDir);
file_put_contents($this->cacheDir . 'list.json', $pluginInfo);
}
}
//获取单一插件数据
if( $name && $this->pluginInfo ) {
foreach ($this->pluginInfo as $plugin) {
if( $plugin->pluginName == $name )
return $plugin;
}
}
return $this->pluginInfo;
}
/**
* 输出插件列表
*
* @access private
* @return void
*/
public function market()
{
$options = $this->options;
$pluginPath = Typecho_Common::url('TeStore', $options->pluginUrl);
$security = $this->security;
$marketUrl = $options->index . __TYPECHO_ADMIN_DIR__ . 'te-store/market';
include_once 'views/market.php';
}
/**
* 执行安装插件
*
* @access private
* @return string
*/
public function install()
{
$this->security->protect();
$plugin = $this->request->get('plugin');
$ret = array(
'status' => false,
'error' => ''
);
if($plugin){
$pluginInfo = $this->getPluginData($plugin);
$activated = $this->getActivePlugins();
if( in_array($plugin, $activated) ){
$ret['error'] = _t('请先禁用该插件');
}elseif( $pluginInfo ){
$tempdir = $this->pluginRoot . '/TeStore/.tmp';
$tempFile = $tempdir. '/' . $plugin . '.zip';
if( ! is_dir($tempdir) ) @mkdir($tempdir);
$zipFile = $this->useCurl ? $this->curlGet($pluginInfo->zipFile) : @file_get_contents($pluginInfo->zipFile);
if( ! file_put_contents( $tempFile, $zipFile ) ){
$ret['error'] = _t('下载zip包出错');
}else{
$unzip = new PclZip($tempFile);
if( ! $unzip->extract(PCLZIP_OPT_PATH, $tempdir)===0 ){
$ret['error'] = $unzip->errorInfo(true);
}else{
@unlink($tempFile);
//遍历解压文件层级
foreach( new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tempdir)) as $filename ){
if( ! is_dir($filename) ){
$scans[] = $filename;
}
}
//以Plugin.php确定目录
foreach($scans as $scan){
if( !strcasecmp(basename($scan), 'Plugin.php') ){
$truedir = dirname($scan);
$parentdir = dirname($truedir);
}
}
if( isset($truedir) ){
foreach($scans as $scan){
//按插件名创建目录
$file_dir = $parentdir==$tempdir ? $tempdir : $parentdir;
$tar = str_replace(( strpos($scan, $truedir)===0 ? $truedir : $file_dir ), $this->pluginRoot. '/' . $plugin, $scan);
$tar_dir = dirname($tar);
if( ! is_dir($tar_dir) ) @mkdir($tar_dir, 0777, true);
if( ! rename($scan, $tar) ){
$error = error_get_last();
$ret['error'] = $error['message'];
}
}
$ret['status'] = true;
//处理单文件型插件
}elseif( count($scans)<=2 ){
foreach($scans as $scan){
if( pathinfo($scan, PATHINFO_EXTENSION)=='php' ){
if( ! rename($scan, $this->pluginRoot. '/' . basename($scan)) ){
$ret['error'] = _t('移动文件出错');
}else{
$ret['status'] = true;
}
}
}
}else{
$ret['error'] = _t('没有找到插件文件');
}
@$this->delTree($tempdir, true);
}
}
}else{
$ret['error'] = _t('没有找到插件信息');
}
}
echo json_encode($ret);
}
/**
* 执行卸载插件
*
* @access private
* @return string
*/
public function uninstall()
{
$this->security->protect();
$plugin = $this->request->get('plugin');
$installed = $this->getLocalPlugins();
$ret = array(
'status' => false,
'error' => ''
);
if( $plugin && in_array($plugin, $installed) ) {
$activated = $this->getActivePlugins();
//自动禁用处理
if( in_array($plugin, $activated) ){
Helper::removePlugin($plugin);
}
$plugindir = $this->pluginRoot. '/' . $plugin;
if( is_dir($plugindir) ){
if( ! $this->delTree($this->pluginRoot. '/' . $plugin) ){
$error = error_get_last();
$ret['error'] = $error['message'];
}else{
$ret['status'] = true;
}
}else{
if( ! unlink($plugindir . '.php') ){
$ret['error'] = _t('删除插件文件出错');
}else{
$ret['status'] = true;
}
}
}
echo json_encode($ret);
}
/**
* 清空指定文件夹
*
* @access private
* @return boolean
*/
private function delTree($dir, $tmp=false) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
is_dir("$dir/$file") ? $this->delTree("$dir/$file") : unlink("$dir/$file");
}
if($tmp) return;
return rmdir($dir);
}
/**
* 使用Curl方法下载
*
* @access private
* @return string
*/
private function curlGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CAINFO,'usr/plugins/TeStore/cacert.pem');
curl_setopt($curl, CURLOPT_URL, $url);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
}