Service.php
17.9 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
<?php
namespace think\addons;
use fast\Http;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use think\Db;
use think\Exception;
use ZipArchive;
/**
* 插件服务
* @package think\addons
*/
class Service
{
/**
* 远程下载插件
*
* @param string $name 插件名称
* @param array $extend 扩展参数
* @return string
* @throws AddonException
* @throws Exception
*/
public static function download($name, $extend = [])
{
$addonTmpDir = RUNTIME_PATH . 'addons' . DS;
if (!is_dir($addonTmpDir)) {
@mkdir($addonTmpDir, 0755, true);
}
$tmpFile = $addonTmpDir . $name . ".zip";
$options = [
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => [
'X-REQUESTED-WITH: XMLHttpRequest'
]
];
$ret = Http::sendRequest(self::getServerUrl() . '/addon/download', array_merge(['name' => $name], $extend), 'GET', $options);
if ($ret['ret']) {
if (substr($ret['msg'], 0, 1) == '{') {
$json = (array)json_decode($ret['msg'], true);
//如果传回的是一个下载链接,则再次下载
if ($json['data'] && isset($json['data']['url'])) {
array_pop($options);
$ret = Http::sendRequest($json['data']['url'], [], 'GET', $options);
if (!$ret['ret']) {
//下载返回错误,抛出异常
throw new AddonException($json['msg'], $json['code'], $json['data']);
}
} else {
//下载返回错误,抛出异常
throw new AddonException($json['msg'], $json['code'], $json['data']);
}
}
if ($write = fopen($tmpFile, 'w')) {
fwrite($write, $ret['msg']);
fclose($write);
return $tmpFile;
}
throw new Exception("没有权限写入临时文件");
}
throw new Exception("无法下载远程文件");
}
/**
* 解压插件
*
* @param string $name 插件名称
* @return string
* @throws Exception
*/
public static function unzip($name)
{
$file = RUNTIME_PATH . 'addons' . DS . $name . '.zip';
$dir = ADDON_PATH . $name . DS;
if (class_exists('ZipArchive')) {
$zip = new ZipArchive;
if ($zip->open($file) !== TRUE) {
throw new Exception('Unable to open the zip file');
}
if (!$zip->extractTo($dir)) {
$zip->close();
throw new Exception('Unable to extract the file');
}
$zip->close();
return $dir;
}
throw new Exception("无法执行解压操作,请确保ZipArchive安装正确");
}
/**
* 备份插件
* @param string $name 插件名称
* @return bool
* @throws Exception
*/
public static function backup($name)
{
$file = RUNTIME_PATH . 'addons' . DS . $name . '-backup-' . date("YmdHis") . '.zip';
$dir = ADDON_PATH . $name . DS;
if (class_exists('ZipArchive')) {
$zip = new ZipArchive;
$zip->open($file, ZipArchive::CREATE);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
$filePath = $fileinfo->getPathName();
$localName = str_replace($dir, '', $filePath);
if ($fileinfo->isFile()) {
$zip->addFile($filePath, $localName);
} elseif ($fileinfo->isDir()) {
$zip->addEmptyDir($localName);
}
}
$zip->close();
return true;
}
throw new Exception("无法执行压缩操作,请确保ZipArchive安装正确");
}
/**
* 检测插件是否完整
*
* @param string $name 插件名称
* @return boolean
* @throws Exception
*/
public static function check($name)
{
if (!$name || !is_dir(ADDON_PATH . $name)) {
throw new Exception('Addon not exists');
}
$addonClass = get_addon_class($name);
if (!$addonClass) {
throw new Exception("插件主启动程序不存在");
}
$addon = new $addonClass();
if (!$addon->checkInfo()) {
throw new Exception("配置文件不完整");
}
return true;
}
/**
* 是否有冲突
*
* @param string $name 插件名称
* @return boolean
* @throws AddonException
*/
public static function noconflict($name)
{
// 检测冲突文件
$list = self::getGlobalFiles($name, true);
if ($list) {
//发现冲突文件,抛出异常
throw new AddonException("发现冲突文件", -3, ['conflictlist' => $list]);
}
return true;
}
/**
* 导入SQL
*
* @param string $name 插件名称
* @return boolean
*/
public static function importsql($name)
{
$sqlFile = ADDON_PATH . $name . DS . 'install.sql';
if (is_file($sqlFile)) {
$lines = file($sqlFile);
$templine = '';
foreach ($lines as $line) {
if (substr($line, 0, 2) == '--' || $line == '' || substr($line, 0, 2) == '/*')
continue;
$templine .= $line;
if (substr(trim($line), -1, 1) == ';') {
$templine = str_ireplace('__PREFIX__', config('database.prefix'), $templine);
$templine = str_ireplace('INSERT INTO ', 'INSERT IGNORE INTO ', $templine);
try {
Db::getPdo()->exec($templine);
} catch (\PDOException $e) {
//$e->getMessage();
}
$templine = '';
}
}
}
return true;
}
/**
* 刷新插件缓存文件
*
* @return boolean
* @throws Exception
*/
public static function refresh()
{
//刷新addons.js
$addons = get_addon_list();
$bootstrapArr = [];
foreach ($addons as $name => $addon) {
$bootstrapFile = ADDON_PATH . $name . DS . 'bootstrap.js';
if ($addon['state'] && is_file($bootstrapFile)) {
$bootstrapArr[] = file_get_contents($bootstrapFile);
}
}
$addonsFile = ROOT_PATH . str_replace("/", DS, "public/assets/js/addons.js");
if ($handle = fopen($addonsFile, 'w')) {
$tpl = <<<EOD
define([], function () {
{__JS__}
});
EOD;
fwrite($handle, str_replace("{__JS__}", implode("\n", $bootstrapArr), $tpl));
fclose($handle);
} else {
throw new Exception("addons.js文件没有写入权限");
}
$file = APP_PATH . 'extra' . DS . 'addons.php';
$config = get_addon_autoload_config(true);
if ($config['autoload'])
return;
if (!is_really_writable($file)) {
throw new Exception("addons.php文件没有写入权限");
}
if ($handle = fopen($file, 'w')) {
fwrite($handle, "<?php\n\n" . "return " . var_export($config, TRUE) . ";");
fclose($handle);
} else {
throw new Exception("文件没有写入权限");
}
return true;
}
/**
* 安装插件
*
* @param string $name 插件名称
* @param boolean $force 是否覆盖
* @param array $extend 扩展参数
* @return boolean
* @throws Exception
* @throws AddonException
*/
public static function install($name, $force = false, $extend = [])
{
if (!$name || (is_dir(ADDON_PATH . $name) && !$force)) {
throw new Exception('Addon already exists');
}
// 远程下载插件
$tmpFile = Service::download($name, $extend);
// 解压插件
$addonDir = Service::unzip($name);
// 移除临时文件
@unlink($tmpFile);
try {
// 检查插件是否完整
Service::check($name);
if (!$force) {
Service::noconflict($name);
}
} catch (AddonException $e) {
@rmdirs($addonDir);
throw new AddonException($e->getMessage(), $e->getCode(), $e->getData());
} catch (Exception $e) {
@rmdirs($addonDir);
throw new Exception($e->getMessage());
}
// 复制文件
$sourceAssetsDir = self::getSourceAssetsDir($name);
$destAssetsDir = self::getDestAssetsDir($name);
if (is_dir($sourceAssetsDir)) {
copydirs($sourceAssetsDir, $destAssetsDir);
}
foreach (self::getCheckDirs() as $k => $dir) {
if (is_dir($addonDir . $dir)) {
copydirs($addonDir . $dir, ROOT_PATH . $dir);
}
}
try {
// 默认启用该插件
$info = get_addon_info($name);
if (!$info['state']) {
$info['state'] = 1;
set_addon_info($name, $info);
}
// 执行安装脚本
$class = get_addon_class($name);
if (class_exists($class)) {
$addon = new $class();
$addon->install();
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
// 导入
Service::importsql($name);
// 刷新
Service::refresh();
return true;
}
/**
* 卸载插件
*
* @param string $name
* @param boolean $force 是否强制卸载
* @return boolean
* @throws Exception
*/
public static function uninstall($name, $force = false)
{
if (!$name || !is_dir(ADDON_PATH . $name)) {
throw new Exception('Addon not exists');
}
if (!$force) {
Service::noconflict($name);
}
// 移除插件基础资源目录
$destAssetsDir = self::getDestAssetsDir($name);
if (is_dir($destAssetsDir)) {
rmdirs($destAssetsDir);
}
// 移除插件全局资源文件
if ($force) {
$list = Service::getGlobalFiles($name);
foreach ($list as $k => $v) {
@unlink(ROOT_PATH . $v);
}
}
// 执行卸载脚本
try {
$class = get_addon_class($name);
if (class_exists($class)) {
$addon = new $class();
$addon->uninstall();
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
// 移除插件目录
rmdirs(ADDON_PATH . $name);
// 刷新
Service::refresh();
return true;
}
/**
* 启用
* @param string $name 插件名称
* @param boolean $force 是否强制覆盖
* @return boolean
*/
public static function enable($name, $force = false)
{
if (!$name || !is_dir(ADDON_PATH . $name)) {
throw new Exception('Addon not exists');
}
if (!$force) {
Service::noconflict($name);
}
$addonDir = ADDON_PATH . $name . DS;
// 复制文件
$sourceAssetsDir = self::getSourceAssetsDir($name);
$destAssetsDir = self::getDestAssetsDir($name);
if (is_dir($sourceAssetsDir)) {
copydirs($sourceAssetsDir, $destAssetsDir);
}
foreach (self::getCheckDirs() as $k => $dir) {
if (is_dir($addonDir . $dir)) {
copydirs($addonDir . $dir, ROOT_PATH . $dir);
}
}
//执行启用脚本
try {
$class = get_addon_class($name);
if (class_exists($class)) {
$addon = new $class();
if (method_exists($class, "enable")) {
$addon->enable();
}
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
$info = get_addon_info($name);
$info['state'] = 1;
unset($info['url']);
set_addon_info($name, $info);
// 刷新
Service::refresh();
return true;
}
/**
* 禁用
*
* @param string $name 插件名称
* @param boolean $force 是否强制禁用
* @return boolean
* @throws Exception
*/
public static function disable($name, $force = false)
{
if (!$name || !is_dir(ADDON_PATH . $name)) {
throw new Exception('Addon not exists');
}
if (!$force) {
Service::noconflict($name);
}
// 移除插件基础资源目录
$destAssetsDir = self::getDestAssetsDir($name);
if (is_dir($destAssetsDir)) {
rmdirs($destAssetsDir);
}
// 移除插件全局资源文件
$list = Service::getGlobalFiles($name);
foreach ($list as $k => $v) {
@unlink(ROOT_PATH . $v);
}
$info = get_addon_info($name);
$info['state'] = 0;
unset($info['url']);
set_addon_info($name, $info);
// 执行禁用脚本
try {
$class = get_addon_class($name);
if (class_exists($class)) {
$addon = new $class();
if (method_exists($class, "disable")) {
$addon->disable();
}
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
// 刷新
Service::refresh();
return true;
}
/**
* 升级插件
*
* @param string $name 插件名称
* @param array $extend 扩展参数
*/
public static function upgrade($name, $extend = [])
{
$info = get_addon_info($name);
if ($info['state']) {
throw new Exception(__('Please disable addon first'));
}
$config = get_addon_config($name);
if ($config) {
//备份配置
}
// 备份插件文件
Service::backup($name);
// 远程下载插件
$tmpFile = Service::download($name, $extend);
// 解压插件
$addonDir = Service::unzip($name);
// 移除临时文件
@unlink($tmpFile);
if ($config) {
// 还原配置
set_addon_config($name, $config);
}
// 导入
Service::importsql($name);
// 执行升级脚本
try {
$class = get_addon_class($name);
if (class_exists($class)) {
$addon = new $class();
if (method_exists($class, "upgrade")) {
$addon->upgrade();
}
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
// 刷新
Service::refresh();
return true;
}
/**
* 获取插件在全局的文件
*
* @param string $name 插件名称
* @return array
*/
public static function getGlobalFiles($name, $onlyconflict = false)
{
$list = [];
$addonDir = ADDON_PATH . $name . DS;
// 扫描插件目录是否有覆盖的文件
foreach (self::getCheckDirs() as $k => $dir) {
$checkDir = ROOT_PATH . DS . $dir . DS;
if (!is_dir($checkDir))
continue;
//检测到存在插件外目录
if (is_dir($addonDir . $dir)) {
//匹配出所有的文件
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($addonDir . $dir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
if ($fileinfo->isFile()) {
$filePath = $fileinfo->getPathName();
$path = str_replace($addonDir, '', $filePath);
if ($onlyconflict) {
$destPath = ROOT_PATH . $path;
if (is_file($destPath)) {
if (filesize($filePath) != filesize($destPath) || md5_file($filePath) != md5_file($destPath)) {
$list[] = $path;
}
}
} else {
$list[] = $path;
}
}
}
}
}
return $list;
}
/**
* 获取插件源资源文件夹
* @param string $name 插件名称
* @return string
*/
protected static function getSourceAssetsDir($name)
{
return ADDON_PATH . $name . DS . 'assets' . DS;
}
/**
* 获取插件目标资源文件夹
* @param string $name 插件名称
* @return string
*/
protected static function getDestAssetsDir($name)
{
$assetsDir = ROOT_PATH . str_replace("/", DS, "public/assets/addons/{$name}/");
if (!is_dir($assetsDir)) {
mkdir($assetsDir, 0755, true);
}
return $assetsDir;
}
/**
* 获取远程服务器
* @return string
*/
protected static function getServerUrl()
{
return config('fastadmin.api_url');
}
/**
* 获取检测的全局文件夹目录
* @return array
*/
protected static function getCheckDirs()
{
return [
'application',
'public'
];
}
}