forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAUTO-UPDATE.php
475 lines (443 loc) · 13.5 KB
/
AUTO-UPDATE.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* Typecho-Fans/Plugins专用自动化更新插件信息与zip包脚本
* (非Typecho插件,仅供GitHub Actions功能调用,勿删改!)
* 作者:羽中
* 反馈:https://github.com/typecho-fans/plugins/issues
*/
date_default_timezone_set('Asia/Shanghai');
//创建临时文件夹
$tmpDir = realpath('../').'/TMP';
$tmpNew = $tmpDir.'/NEW';
if (!is_dir($tmpDir)) {
mkdir($tmpDir);
}
if (!is_dir($tmpNew)) {
mkdir($tmpNew);
}
//分析最新文档变化
if (!empty($argv['2']) && strpos($argv['2'],'.diff')) {
$record = @file_get_contents($argv['2'],0,
stream_context_create(array('http'=>array('header'=>array('Accept: application/vnd.github.v3.diff')))));
$diffs = explode(PHP_EOL,$record);
//确定行范围
$begin = 0;
$end = count($diffs)-1;
foreach ($diffs as $line=>$diff) {
if ($diff=='+++ b/TESTORE.md') {
$begin = $line;
}
if ($begin && $line>$begin && strpos($diff,'diff --git')===0) {
$end = $line;
break;
}
}
//提取变化行
$links = array();
$urls = array();
foreach ($diffs as $line=>$diff) {
if ($begin && $line>$begin && $line<$end && strpos($diff,'+[')===0) {
preg_match_all('/(?<=\()[^\)]+/',$diff,$links);
$urls[] = $links['0']['0'];
}
}
}
//预设循环内变量
$desciptions = array();
$links = array();
$metas = array();
$url = '';
$github = false;
$condition = false;
$all = 0;
$authorCode = '';
$separator = '';
$authors = array();
$authorName = array();
$authorNames = array();
$author = '';
$name = array();
$doc = false;
$main = false;
$sub = false;
$paths = array();
$api = '';
$detect = true;
$datas = array();
$path = '';
$pluginFile = '';
$logs = '--------------'.PHP_EOL.date('Y-m-d',time()).PHP_EOL;
$infos = array();
$match = array();
$version = '';
$update = 0;
$zip = '';
$repoZip = '';
$download = '';
$tmpName = '';
$tmpZip = '';
$tmpSub = '';
$phpZip = (object)array();
$pluginFolder = '';
$plugin = '';
$renamed = '';
$cdn = '';
$rootPath = '';
$filePath = '';
$status = 'failed';
$done = 0;
$tables = array();
//开始分割文档循环
$source = file_get_contents('TESTORE.md');
$lines = explode(PHP_EOL,$source);
$count = count($lines);
foreach ($lines as $line=>$column) {
if ($line<38) {
if ($line=='29') {
preg_match('/(?<=\()[^\)]+/',$column,$counts);
$column = str_replace($counts['0'],$count-39,$column);
}
$desciptions[] = $column;
} elseif ($column) {
preg_match_all('/(?<=\()[^\)]+/',$column,$links);
preg_match_all('/(?<=)[^\|]+/',$column,$metas);
$url = $links['0']['0'];
$github = strpos($url,'github.com');
//判断地址参数
if (empty($argv['2'])) { //默认处理GitHub源
$condition = $github;
} elseif (strpos($argv['2'],'.diff')) { //提交处理变化地址 (不限GitHub)
$condition = $urls && in_array($url,$urls);
} else { //手动处理参数指定
$condition = strpos($argv['2'],'github.com') && $argv['2']==$url;
}
if ($condition) {
++$all;
preg_match('/(?<=\[)[^\]]+/',$metas['0']['0'],$name);
if ($github) {
//取插件主文件地址
$doc = (strpos($url,'/blob/master/') || strpos($url,'/blob/main/')) && strpos($url,'.php');
//单文件情况
if ($doc) {
$detect = false;
$pluginFile = str_replace('blob','raw',$url); //直接确定地址
$paths = explode((strpos($url,'/raw/main/') ? '/raw/main/' : '/raw/master/'),$pluginFile);
$url = $paths['0']; //提取仓库路径
} else {
$main = strpos($url,'/tree/main/');
$sub = strpos($url,'/tree/master/') || $main;
//子目录情况
if ($sub) {
$paths = explode(($main ? '/tree/main/' : '/tree/master/'),$url);
$url = $paths['0']; //提取仓库路径
}
//查询仓库文件结构
if (!$main) {
$api = @file_get_contents(str_replace('github.com','api.github.com/repos',$url).'/git/trees/master?recursive=1&access_token='.$argv['1'],0,
stream_context_create(array('http'=>array('header'=>array('User-Agent: PHP')))));
}
//兼容main分支名
if (!$api || $main) {
$api = @file_get_contents(str_replace('github.com','api.github.com/repos',$url).'/git/trees/main?recursive=1&access_token='.$argv['1'],0,
stream_context_create(array('http'=>array('header'=>array('User-Agent: PHP')))));
if ($api && !$main) {
$main = true;
}
}
$detect = true;
$pluginFile = $url.($main ? '/raw/main/' : '/raw/master/').($sub ? $paths['1'].'/' : ''); //默认确定前缀
if ($api) {
$datas = json_decode($api,true);
//查找主文件路径
foreach ($datas['tree'] as $tree) {
$path = '';
if (false!==strpos($tree['path'],($sub ? $name['0'].'/Plugin.php' : 'Plugin.php'))) {
$path = $tree['path'];
break;
}
}
//找到拼接出地址
if ($path) {
$detect = false;
$pluginFile = $url.($main ? '/raw/main/' : '/raw/master/').$path;
}
}
}
//从主文件提取信息
$infos = call_user_func('parseInfo',($detect ? $pluginFile.'Plugin.php' : $pluginFile));
//单文件情况
if (!$infos['version'] && $detect) {
$infos = call_user_func('parseInfo',$pluginFile.$name['0'].'.php');
}
if ($infos['version']) {
//提取版本号
if (preg_match('/\d+(.\d+)*/',trim(strip_tags($infos['version'])),$match)) {
$infos['version'] = $match['0'];
}
$version = stripos($metas['0']['2'],'v')===0 ? trim(substr($metas['0']['2'],1)) : trim($metas['0']['2']);
} else {
$logs .= 'Error: "'.$pluginFile.'" not valid!'.PHP_EOL;
}
}
//对比版本号判断更新
if ($infos['version'] && $infos['version']>$version || !empty($argv['2'])) { //或有参数即可
++$update;
$zip = end($links['0']);
//准备作者名
$authorCode = html_entity_decode(trim($metas['0']['3']));
switch (true) {
case (strpos($authorCode,',')) :
$separator = ',';
break;
case (strpos($authorCode,', ')) :
$separator = ', ';
break;
case (strpos($authorCode,'&')) :
$separator = '&';
break;
case (strpos($authorCode,' & ')) :
$separator = ' & ';
break;
}
//多作者情况
if ($separator) {
$authors = explode($separator,$authorCode);
$authorNames = array();
foreach ($authors as $key=>$author) {
preg_match('/(?<=\[)[^\]]+/',$author,$authorName);
$authorNames[] = empty($authorName['0']) ? $author : $authorName['0'];
}
$author = implode($separator,$authorNames);
//单作者情况
} else {
$author = '';
preg_match('/(?<=\[)[^\]]+/',$authorCode,$authorName);
$author = empty($authorName['0']) ? $authorCode : $authorName['0'];
}
//命名zip包 (加速目录用)
$cdn = 'ZIP_CDN/'.$name['0'].'_'.($separator ? implode('_',$authorNames) : $author).'.zip';
//标签发布的需重新打包
if ($github && strpos($zip,'typecho-fans/plugins/releases/download')) {
$repoZip = $url.'/archive/'.($main ? 'main' : 'master').'.zip';
$download = @file_get_contents($repoZip);
if ($download) {
$tmpName = '/'.$all.'_'.$name['0'];
$tmpZip = $tmpDir.$tmpName.'_master.zip';
//下载实时zip
file_put_contents($tmpZip,$download);
//解压实时zip
$phpZip = new ZipArchive();
$phpZip->open($tmpZip);
$tmpSub = $tmpDir.$tmpName;
mkdir($tmpSub);
$phpZip->extractTo($tmpSub);
$pluginFolder = $tmpSub.'/'.basename($url).($main ? '-main/' : '-master/');
//替换作者名
$renamed = '';
if (!empty($infos['author']) && trim(strip_tags($infos['author']))!==$author) {
$plugin = $pluginFolder.($doc ? $paths['1'] : $path);
file_put_contents($plugin,str_replace($infos['author'],$author,file_get_contents($plugin)));
$renamed = '/ Rename Author ';
}
//命名zip包 (标签发布用)
$newZip = $tmpNew.'/'.$name['0'].'_'; //因不支持中文仅用下划线
for ($j=$line+1;$j<$count;++$j) {
if ($lines[$j]) {
preg_match_all('/(?<=)[^\|]+/',$lines[$j],$reMetas);
preg_match('/(?<=\[)[^\]]+/',$reMetas['0']['0'],$reName);
//重名增加下划线
if (!strcasecmp($reName['0'],$name['0'])) {
$newZip .= '_';
}
}
}
$newZip = $newZip.'.zip';
//打包至临时目录
$phpZip->open($newZip,ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($doc) {
$phpZip->addFile($pluginFolder.$paths['1'],$paths['1']);
} else {
$rootPath = $pluginFolder.($sub ? $paths['1'].'/' : '');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath)) as $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$phpZip->addFile($filePath,$name['0'].'/'.substr($filePath,strlen($rootPath)));
}
}
}
//复制至加速目录
if ($phpZip->close() && @copy($newZip,$cdn)) {
//更新文档下载地址
$column = str_replace($zip,dirname($zip).'/'.basename($newZip),$column);
$status = 'succeeded';
++$done;
}
//完成处理记录日志
$logs .= $name['0'].' - '.date('Y-m-d H:i',time()).' - RE-ZIP '.$renamed.$status.PHP_EOL;
} else {
$logs .= 'Error: "'.$repoZip.'" not found!'.PHP_EOL;
}
//其他仅下载至加速目录
} else {
$download = @file_get_contents($zip);
if ($download) {
if (@file_put_contents($cdn,$download)) {
$status = 'succeeded';
++$done;
}
//完成处理记录日志
$logs .= $name['0'].' - '.date('Y-m-d H:i',time()).' - '.$status.PHP_EOL;
} else {
$logs .= 'Error: "'.$zip.'" not found!'.PHP_EOL;
}
}
//更新文档版本号记录
if ($github && $infos['version'] && $status=='succeeded') {
$column = str_replace($version,$infos['version'],$column);
}
}
}
$tables[] = $column;
}
}
//按插件名排序
sort($tables);
//重组文档并生成日志
file_put_contents('TESTORE.md',implode(PHP_EOL,$desciptions).PHP_EOL.implode(PHP_EOL,$tables).PHP_EOL);
file_put_contents($tmpDir.'/updates.log',$logs.
'SCANED: '.$all.PHP_EOL.
'NEED UPDATE: '.$update.PHP_EOL.
'DONE: '.$done.PHP_EOL);
/**
* 获取插件文件的头信息 (Typecho)
*
* @param string $pluginFile 插件文件路径
* @return array
*/
function parseInfo($pluginFile)
{
$tokens = token_get_all(@file_get_contents($pluginFile));
$isDoc = false;
$isFunction = false;
$isClass = false;
$isInClass = false;
$isInFunction = false;
$isDefined = false;
$current = NULL;
/** 初始信息 */
$info = array(
'description' => '',
'title' => '',
'author' => '',
'homepage' => '',
'version' => '',
'dependence' => '',
'activate' => false,
'deactivate' => false,
'config' => false,
'personalConfig' => false
);
$map = array(
'package' => 'title',
'author' => 'author',
'link' => 'homepage',
'dependence'=> 'dependence',
'version' => 'version'
);
foreach ($tokens as $token) {
/** 获取doc comment */
if (!$isDoc && is_array($token) && T_DOC_COMMENT == $token[0]) {
/** 分行读取 */
$described = false;
$lines = preg_split("(\r|\n)", $token[1]);
foreach ($lines as $line) {
$line = trim($line);
if (!empty($line) && '*' == $line[0]) {
$line = trim(substr($line, 1));
if (!$described && !empty($line) && '@' == $line[0]) {
$described = true;
}
if (!$described && !empty($line)) {
$info['description'] .= $line . "\n";
} else if ($described && !empty($line) && '@' == $line[0]) {
$info['description'] = trim($info['description']);
$line = trim(substr($line, 1));
$args = explode(' ', $line);
$key = array_shift($args);
if (isset($map[$key])) {
$info[$map[$key]] = trim(implode(' ', $args));
}
}
}
}
$isDoc = true;
}
if (is_array($token)) {
switch ($token[0]) {
case T_FUNCTION:
$isFunction = true;
break;
case T_IMPLEMENTS:
$isClass = true;
break;
case T_WHITESPACE:
case T_COMMENT:
case T_DOC_COMMENT:
break;
case T_STRING:
$string = strtolower($token[1]);
switch ($string) {
case 'typecho_plugin_interface':
$isInClass = $isClass;
break;
case 'activate':
case 'deactivate':
case 'config':
case 'personalconfig':
if ($isFunction) {
$current = ('personalconfig' == $string ? 'personalConfig' : $string);
}
break;
default:
if (!empty($current) && $isInFunction && $isInClass) {
$info[$current] = true;
}
break;
}
break;
default:
if (!empty($current) && $isInFunction && $isInClass) {
$info[$current] = true;
}
break;
}
} else {
$token = strtolower($token);
switch ($token) {
case '{':
if ($isDefined) {
$isInFunction = true;
}
break;
case '(':
if ($isFunction && !$isDefined) {
$isDefined = true;
}
break;
case '}':
case ';':
$isDefined = false;
$isFunction = false;
$isInFunction = false;
$current = NULL;
break;
default:
if (!empty($current) && $isInFunction && $isInClass) {
$info[$current] = true;
}
break;
}
}
}
return $info;
}