Addon.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. $configList = [];
  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. Service::install($name, $force, ['uid' => $uid, 'token' => $token]);
  118. $info = get_addon_info($name);
  119. $info['config'] = get_addon_config($name) ? 1 : 0;
  120. $this->success(__('Install successful'), null, ['addon' => $info]);
  121. }
  122. catch (AddonException $e)
  123. {
  124. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  125. }
  126. catch (Exception $e)
  127. {
  128. $this->error($e->getMessage(), $e->getCode());
  129. }
  130. }
  131. /**
  132. * 卸载
  133. */
  134. public function uninstall()
  135. {
  136. $name = $this->request->post("name");
  137. $force = (int) $this->request->post("force");
  138. if (!$name)
  139. {
  140. $this->error(__('Parameter %s can not be empty', 'name'));
  141. }
  142. try
  143. {
  144. Service::uninstall($name, $force);
  145. $this->success(__('Uninstall successful'));
  146. }
  147. catch (AddonException $e)
  148. {
  149. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  150. }
  151. catch (Exception $e)
  152. {
  153. $this->error($e->getMessage());
  154. }
  155. }
  156. /**
  157. * 禁用启用
  158. */
  159. public function state()
  160. {
  161. $name = $this->request->post("name");
  162. $action = $this->request->post("action");
  163. $force = (int) $this->request->post("force");
  164. if (!$name)
  165. {
  166. $this->error(__('Parameter %s can not be empty', 'name'));
  167. }
  168. try
  169. {
  170. $action = $action == 'enable' ? $action : 'disable';
  171. //调用启用、禁用的方法
  172. Service::$action($name, $force);
  173. $this->success(__('Operate successful'));
  174. }
  175. catch (AddonException $e)
  176. {
  177. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  178. }
  179. catch (Exception $e)
  180. {
  181. $this->error($e->getMessage());
  182. }
  183. }
  184. /**
  185. * 本地上传
  186. */
  187. public function local()
  188. {
  189. Config::set('default_return_type', 'json');
  190. $file = $this->request->file('file');
  191. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  192. if (!is_dir($addonTmpDir))
  193. {
  194. @mkdir($addonTmpDir, 0755, true);
  195. }
  196. $info = $file->rule('uniqid')->validate(['size' => 10240000, 'ext' => 'zip'])->move($addonTmpDir);
  197. if ($info)
  198. {
  199. $tmpName = substr($info->getFilename(), 0, stripos($info->getFilename(), '.'));
  200. $tmpAddonDir = ADDON_PATH . $tmpName . DS;
  201. $tmpFile = $addonTmpDir . $info->getSaveName();
  202. try
  203. {
  204. Service::unzip($tmpName);
  205. @unlink($tmpFile);
  206. $infoFile = $tmpAddonDir . 'info.ini';
  207. if (!is_file($infoFile))
  208. {
  209. throw new Exception(__('Addon info file was not found'));
  210. }
  211. $config = Config::parse($infoFile, '', $tmpName);
  212. $name = isset($config['name']) ? $config['name'] : '';
  213. if (!$name)
  214. {
  215. throw new Exception(__('Addon info file data incorrect'));
  216. }
  217. $newAddonDir = ADDON_PATH . $name . DS;
  218. if (is_dir($newAddonDir))
  219. {
  220. throw new Exception(__('Addon already exists'));
  221. }
  222. //重命名插件文件夹
  223. rename($tmpAddonDir, $newAddonDir);
  224. try
  225. {
  226. //默认禁用该插件
  227. $info = get_addon_info($name);
  228. if ($info['state'])
  229. {
  230. $info['state'] = 0;
  231. set_addon_info($name, $info);
  232. }
  233. //执行插件的安装方法
  234. $class = get_addon_class($name);
  235. if (class_exists($class))
  236. {
  237. $addon = new $class();
  238. $addon->install();
  239. }
  240. //导入SQL
  241. Service::importsql($name);
  242. $info['config'] = get_addon_config($name) ? 1 : 0;
  243. $this->success(__('Offline installed tips'), null, ['addon' => $info]);
  244. }
  245. catch (Exception $e)
  246. {
  247. @rmdirs($newAddonDir);
  248. throw new Exception($e->getMessage());
  249. }
  250. }
  251. catch (Exception $e)
  252. {
  253. @unlink($tmpFile);
  254. @rmdirs($tmpAddonDir);
  255. $this->error($e->getMessage());
  256. }
  257. }
  258. else
  259. {
  260. // 上传失败获取错误信息
  261. $this->error($file->getError());
  262. }
  263. }
  264. /**
  265. * 刷新缓存
  266. */
  267. public function refresh()
  268. {
  269. try
  270. {
  271. Service::refresh();
  272. $this->success(__('Operate successful'));
  273. }
  274. catch (Exception $e)
  275. {
  276. $this->error($e->getMessage());
  277. }
  278. }
  279. /**
  280. * 已装插件
  281. */
  282. public function downloaded()
  283. {
  284. $offset = (int) $this->request->get("offset");
  285. $limit = (int) $this->request->get("limit");
  286. $filter = $this->request->get("filter");
  287. $filter = (array) json_decode($filter, true);
  288. foreach ($filter as $k => &$v)
  289. {
  290. $v = htmlspecialchars(strip_tags($v));
  291. }
  292. unset($v);
  293. $where = ['status' => 'normal'];
  294. if (isset($filter['id']))
  295. {
  296. $where['id'] = (int) $filter['id'];
  297. }
  298. if (isset($filter['name']))
  299. {
  300. $where['name'] = ['like', "%{$filter['name']}%"];
  301. }
  302. if (isset($filter['title']))
  303. {
  304. $where['title'] = ['like', "%{$filter['title']}%"];
  305. }
  306. $addons = get_addon_list();
  307. $list = [];
  308. foreach ($addons as $k => $v)
  309. {
  310. $v['flag'] = '';
  311. $v['banner'] = '';
  312. $v['image'] = '';
  313. $v['donateimage'] = '';
  314. $v['demourl'] = '';
  315. $v['price'] = '0.00';
  316. $v['url'] = '/addons/' . $v['name'];
  317. $v['createtime'] = 0;
  318. $list[] = $v;
  319. }
  320. $list = array_slice($list, $offset, $limit);
  321. $result = array("total" => count($addons), "rows" => $list);
  322. $callback = $this->request->get('callback') ? "jsonp" : "json";
  323. return $callback($result);
  324. }
  325. }