Addon.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. */
  13. class Addon extends Backend
  14. {
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. }
  20. /**
  21. * 查看
  22. */
  23. public function index()
  24. {
  25. $addons = get_addon_list();
  26. foreach ($addons as $k => &$v)
  27. {
  28. $config = get_addon_config($v['name']);
  29. $v['config'] = $config ? 1 : 0;
  30. }
  31. $this->assignconfig(['addons' => $addons]);
  32. return $this->view->fetch();
  33. }
  34. /**
  35. * 配置
  36. */
  37. public function config($ids = NULL)
  38. {
  39. $name = $this->request->get("name");
  40. if (!$name)
  41. {
  42. $this->error(__('Parameter %s can not be empty', $id ? 'id' : 'name'));
  43. }
  44. if (!is_dir(ADDON_PATH . $name))
  45. {
  46. $this->error(__('Directory not found'));
  47. }
  48. $info = get_addon_info($name);
  49. $config = get_addon_fullconfig($name);
  50. if (!$info)
  51. $this->error(__('No Results were found'));
  52. if ($this->request->isPost())
  53. {
  54. $params = $this->request->post("row/a");
  55. if ($params)
  56. {
  57. $configList = [];
  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. Service::install($name, $force);
  115. $info = get_addon_info($name);
  116. $info['config'] = get_addon_config($name) ? 1 : 0;
  117. $this->success("安装成功", null, ['addon' => $info]);
  118. }
  119. catch (AddonException $e)
  120. {
  121. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  122. }
  123. catch (Exception $e)
  124. {
  125. $this->error($e->getMessage(), $e->getCode());
  126. }
  127. }
  128. /**
  129. * 卸载
  130. */
  131. public function uninstall()
  132. {
  133. $name = $this->request->post("name");
  134. $force = (int) $this->request->post("force");
  135. if (!$name)
  136. {
  137. $this->error(__('Parameter %s can not be empty', 'name'));
  138. }
  139. try
  140. {
  141. Service::uninstall($name, $force);
  142. $this->success("卸载成功");
  143. }
  144. catch (AddonException $e)
  145. {
  146. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  147. }
  148. catch (Exception $e)
  149. {
  150. $this->error($e->getMessage());
  151. }
  152. }
  153. /**
  154. * 禁用启用
  155. */
  156. public function state()
  157. {
  158. $name = $this->request->post("name");
  159. $action = $this->request->post("action");
  160. $force = (int) $this->request->post("force");
  161. if (!$name)
  162. {
  163. $this->error(__('Parameter %s can not be empty', 'name'));
  164. }
  165. try
  166. {
  167. $action = $action == 'enable' ? $action : 'disable';
  168. //调用启用、禁用的方法
  169. Service::$action($name, $force);
  170. $this->success("操作成功");
  171. }
  172. catch (AddonException $e)
  173. {
  174. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  175. }
  176. catch (Exception $e)
  177. {
  178. $this->error($e->getMessage());
  179. }
  180. }
  181. /**
  182. * 本地上传
  183. */
  184. public function local()
  185. {
  186. $file = $this->request->file('file');
  187. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  188. if (!is_dir($addonTmpDir))
  189. {
  190. @mkdir($addonTmpDir, 0755, true);
  191. }
  192. $info = $file->rule('uniqid')->validate(['size' => 10240000, 'ext' => 'zip'])->move($addonTmpDir);
  193. if ($info)
  194. {
  195. $tmpName = substr($info->getFilename(), 0, stripos($info->getFilename(), '.'));
  196. $tmpAddonDir = ADDON_PATH . $tmpName . DS;
  197. $tmpFile = $addonTmpDir . $info->getSaveName();
  198. try
  199. {
  200. Service::unzip($tmpName);
  201. @unlink($tmpFile);
  202. $infoFile = $tmpAddonDir . 'info.ini';
  203. if (!is_file($infoFile))
  204. {
  205. throw new Exception("插件配置文件未找到");
  206. }
  207. $config = Config::parse($infoFile, '', $tmpName);
  208. $name = isset($config['name']) ? $config['name'] : '';
  209. if (!$name)
  210. {
  211. throw new Exception("插件配置信息不正确");
  212. }
  213. $newAddonDir = ADDON_PATH . $name . DS;
  214. if (is_dir($newAddonDir))
  215. {
  216. throw new Exception("上传的插件已经存在");
  217. }
  218. //重命名插件文件夹
  219. rename($tmpAddonDir, $newAddonDir);
  220. try
  221. {
  222. //默认禁用该插件
  223. $info = get_addon_info($name);
  224. if ($info['state'])
  225. {
  226. $info['state'] = 0;
  227. set_addon_info($name, $info);
  228. }
  229. //执行插件的安装方法
  230. $class = get_addon_class($name);
  231. if (class_exists($class))
  232. {
  233. $addon = new $class();
  234. $addon->install();
  235. }
  236. //导入SQL
  237. Service::importsql($name);
  238. $info['config'] = get_addon_config($name) ? 1 : 0;
  239. $this->success("插件安装成功,你需要手动启用该插件,使之生效", null, ['addon' => $info]);
  240. }
  241. catch (Exception $e)
  242. {
  243. @rmdirs($newAddonDir);
  244. throw new Exception($e->getMessage());
  245. }
  246. }
  247. catch (Exception $e)
  248. {
  249. @unlink($tmpFile);
  250. @rmdirs($tmpAddonDir);
  251. $this->error($e->getMessage());
  252. }
  253. }
  254. else
  255. {
  256. // 上传失败获取错误信息
  257. $this->error($file->getError());
  258. }
  259. }
  260. /**
  261. * 刷新缓存
  262. */
  263. public function refresh()
  264. {
  265. try
  266. {
  267. Service::refresh();
  268. $this->success("操作成功");
  269. }
  270. catch (Exception $e)
  271. {
  272. $this->error($e->getMessage());
  273. }
  274. }
  275. }