Addon.php 11 KB

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