Addon.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\addons\AddonException;
  5. use think\addons\Service;
  6. use think\Cache;
  7. use think\Config;
  8. use think\Exception;
  9. /**
  10. * 插件管理
  11. *
  12. * @icon fa fa-circle-o
  13. * @remark 可在线安装、卸载、禁用、启用插件,同时支持添加本地插件。FastAdmin已上线插件商店 ,你可以发布你的免费或付费插件:<a href="http://www.fastadmin.net/store.html" target="_blank">http://www.fastadmin.net/store.html</a>
  14. */
  15. class Addon extends Backend
  16. {
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. }
  22. /**
  23. * 查看
  24. */
  25. public function index()
  26. {
  27. $addons = get_addon_list();
  28. foreach ($addons as $k => &$v)
  29. {
  30. $config = get_addon_config($v['name']);
  31. $v['config'] = $config ? 1 : 0;
  32. }
  33. $this->assignconfig(['addons' => $addons]);
  34. return $this->view->fetch();
  35. }
  36. /**
  37. * 配置
  38. */
  39. public function config($ids = NULL)
  40. {
  41. $name = $this->request->get("name");
  42. if (!$name)
  43. {
  44. $this->error(__('Parameter %s can not be empty', $ids ? 'id' : 'name'));
  45. }
  46. if (!is_dir(ADDON_PATH . $name))
  47. {
  48. $this->error(__('Directory not found'));
  49. }
  50. $info = get_addon_info($name);
  51. $config = get_addon_fullconfig($name);
  52. if (!$info)
  53. $this->error(__('No Results were found'));
  54. if ($this->request->isPost())
  55. {
  56. $params = $this->request->post("row/a");
  57. if ($params)
  58. {
  59. foreach ($config as $k => &$v)
  60. {
  61. if (isset($params[$v['name']]))
  62. {
  63. if ($v['type'] == 'array')
  64. {
  65. $fieldarr = $valuearr = [];
  66. $field = $params[$v['name']]['field'];
  67. $value = $params[$v['name']]['value'];
  68. foreach ($field as $m => $n)
  69. {
  70. if ($n != '')
  71. {
  72. $fieldarr[] = $field[$m];
  73. $valuearr[] = $value[$m];
  74. }
  75. }
  76. $params[$v['name']] = array_combine($fieldarr, $valuearr);
  77. $value = $params[$v['name']];
  78. }
  79. else
  80. {
  81. $value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
  82. }
  83. $v['value'] = $value;
  84. }
  85. }
  86. try
  87. {
  88. //更新配置文件
  89. set_addon_fullconfig($name, $config);
  90. $this->success();
  91. }
  92. catch (Exception $e)
  93. {
  94. $this->error($e->getMessage());
  95. }
  96. }
  97. $this->error(__('Parameter %s can not be empty', ''));
  98. }
  99. $this->view->assign("addon", ['info' => $info, 'config' => $config]);
  100. return $this->view->fetch();
  101. }
  102. /**
  103. * 安装
  104. */
  105. public function install()
  106. {
  107. $name = $this->request->post("name");
  108. $force = (int) $this->request->post("force");
  109. if (!$name)
  110. {
  111. $this->error(__('Parameter %s can not be empty', 'name'));
  112. }
  113. try
  114. {
  115. $uid = $this->request->post("uid");
  116. $token = $this->request->post("token");
  117. $version = $this->request->post("version");
  118. $faversion = $this->request->post("faversion");
  119. $extend = [
  120. 'uid' => $uid,
  121. 'token' => $token,
  122. 'version' => $version,
  123. 'faversion' => $faversion
  124. ];
  125. Service::install($name, $force, $extend);
  126. $info = get_addon_info($name);
  127. $info['config'] = get_addon_config($name) ? 1 : 0;
  128. $info['state'] = 1;
  129. $this->success(__('Install successful'), null, ['addon' => $info]);
  130. }
  131. catch (AddonException $e)
  132. {
  133. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  134. }
  135. catch (Exception $e)
  136. {
  137. $this->error($e->getMessage(), $e->getCode());
  138. }
  139. }
  140. /**
  141. * 卸载
  142. */
  143. public function uninstall()
  144. {
  145. $name = $this->request->post("name");
  146. $force = (int) $this->request->post("force");
  147. if (!$name)
  148. {
  149. $this->error(__('Parameter %s can not be empty', 'name'));
  150. }
  151. try
  152. {
  153. Service::uninstall($name, $force);
  154. $this->success(__('Uninstall successful'));
  155. }
  156. catch (AddonException $e)
  157. {
  158. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  159. }
  160. catch (Exception $e)
  161. {
  162. $this->error($e->getMessage());
  163. }
  164. }
  165. /**
  166. * 禁用启用
  167. */
  168. public function state()
  169. {
  170. $name = $this->request->post("name");
  171. $action = $this->request->post("action");
  172. $force = (int) $this->request->post("force");
  173. if (!$name)
  174. {
  175. $this->error(__('Parameter %s can not be empty', 'name'));
  176. }
  177. try
  178. {
  179. $action = $action == 'enable' ? $action : 'disable';
  180. //调用启用、禁用的方法
  181. Service::$action($name, $force);
  182. Cache::rm('__menu__');
  183. $this->success(__('Operate successful'));
  184. }
  185. catch (AddonException $e)
  186. {
  187. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  188. }
  189. catch (Exception $e)
  190. {
  191. $this->error($e->getMessage());
  192. }
  193. }
  194. /**
  195. * 本地上传
  196. */
  197. public function local()
  198. {
  199. Config::set('default_return_type', 'json');
  200. $file = $this->request->file('file');
  201. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  202. if (!is_dir($addonTmpDir))
  203. {
  204. @mkdir($addonTmpDir, 0755, true);
  205. }
  206. $info = $file->rule('uniqid')->validate(['size' => 10240000, 'ext' => 'zip'])->move($addonTmpDir);
  207. if ($info)
  208. {
  209. $tmpName = substr($info->getFilename(), 0, stripos($info->getFilename(), '.'));
  210. $tmpAddonDir = ADDON_PATH . $tmpName . DS;
  211. $tmpFile = $addonTmpDir . $info->getSaveName();
  212. try
  213. {
  214. Service::unzip($tmpName);
  215. @unlink($tmpFile);
  216. $infoFile = $tmpAddonDir . 'info.ini';
  217. if (!is_file($infoFile))
  218. {
  219. throw new Exception(__('Addon info file was not found'));
  220. }
  221. $config = Config::parse($infoFile, '', $tmpName);
  222. $name = isset($config['name']) ? $config['name'] : '';
  223. if (!$name)
  224. {
  225. throw new Exception(__('Addon info file data incorrect'));
  226. }
  227. $newAddonDir = ADDON_PATH . $name . DS;
  228. if (is_dir($newAddonDir))
  229. {
  230. throw new Exception(__('Addon already exists'));
  231. }
  232. //重命名插件文件夹
  233. rename($tmpAddonDir, $newAddonDir);
  234. try
  235. {
  236. //默认禁用该插件
  237. $info = get_addon_info($name);
  238. if ($info['state'])
  239. {
  240. $info['state'] = 0;
  241. set_addon_info($name, $info);
  242. }
  243. //执行插件的安装方法
  244. $class = get_addon_class($name);
  245. if (class_exists($class))
  246. {
  247. $addon = new $class();
  248. $addon->install();
  249. }
  250. //导入SQL
  251. Service::importsql($name);
  252. $info['config'] = get_addon_config($name) ? 1 : 0;
  253. $this->success(__('Offline installed tips'), null, ['addon' => $info]);
  254. }
  255. catch (Exception $e)
  256. {
  257. @rmdirs($newAddonDir);
  258. throw new Exception($e->getMessage());
  259. }
  260. }
  261. catch (Exception $e)
  262. {
  263. @unlink($tmpFile);
  264. @rmdirs($tmpAddonDir);
  265. $this->error($e->getMessage());
  266. }
  267. }
  268. else
  269. {
  270. // 上传失败获取错误信息
  271. $this->error($file->getError());
  272. }
  273. }
  274. /**
  275. * 更新插件
  276. */
  277. public function upgrade()
  278. {
  279. $name = $this->request->post("name");
  280. if (!$name)
  281. {
  282. $this->error(__('Parameter %s can not be empty', 'name'));
  283. }
  284. try
  285. {
  286. $uid = $this->request->post("uid");
  287. $token = $this->request->post("token");
  288. $version = $this->request->post("version");
  289. $faversion = $this->request->post("faversion");
  290. $extend = [
  291. 'uid' => $uid,
  292. 'token' => $token,
  293. 'version' => $version,
  294. 'faversion' => $faversion
  295. ];
  296. //调用更新的方法
  297. Service::upgrade($name, $extend);
  298. Cache::rm('__menu__');
  299. $this->success(__('Operate successful'));
  300. }
  301. catch (AddonException $e)
  302. {
  303. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  304. }
  305. catch (Exception $e)
  306. {
  307. $this->error($e->getMessage());
  308. }
  309. }
  310. /**
  311. * 刷新缓存
  312. */
  313. public function refresh()
  314. {
  315. try
  316. {
  317. Service::refresh();
  318. $this->success(__('Operate successful'));
  319. }
  320. catch (Exception $e)
  321. {
  322. $this->error($e->getMessage());
  323. }
  324. }
  325. /**
  326. * 已装插件
  327. */
  328. public function downloaded()
  329. {
  330. $offset = (int) $this->request->get("offset");
  331. $limit = (int) $this->request->get("limit");
  332. $search = $this->request->get("search");
  333. $search = htmlspecialchars(strip_tags($search));
  334. $addons = get_addon_list();
  335. $list = [];
  336. foreach ($addons as $k => $v)
  337. {
  338. if ($search && stripos($v['name'], $search) === FALSE && stripos($v['intro'], $search) === FALSE)
  339. continue;
  340. $v['flag'] = '';
  341. $v['banner'] = '';
  342. $v['image'] = '';
  343. $v['donateimage'] = '';
  344. $v['demourl'] = '';
  345. $v['price'] = '0.00';
  346. $v['url'] = addon_url($v['name']);
  347. $v['createtime'] = filemtime(ADDON_PATH . $v['name']);
  348. $list[] = $v;
  349. }
  350. $total = count($list);
  351. if ($limit)
  352. {
  353. $list = array_slice($list, $offset, $limit);
  354. }
  355. $result = array("total" => $total, "rows" => $list);
  356. $callback = $this->request->get('callback') ? "jsonp" : "json";
  357. return $callback($result);
  358. }
  359. }