Addon.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. $this->success("安装成功", null, ['addon' => get_addon_info($name)]);
  116. }
  117. catch (AddonException $e)
  118. {
  119. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  120. }
  121. catch (Exception $e)
  122. {
  123. $this->error($e->getMessage(), $e->getCode());
  124. }
  125. }
  126. /**
  127. * 卸载
  128. */
  129. public function uninstall()
  130. {
  131. $name = $this->request->post("name");
  132. $force = (int) $this->request->post("force");
  133. if (!$name)
  134. {
  135. $this->error(__('Parameter %s can not be empty', 'name'));
  136. }
  137. try
  138. {
  139. Service::uninstall($name, $force);
  140. $this->success("卸载成功");
  141. }
  142. catch (AddonException $e)
  143. {
  144. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  145. }
  146. catch (Exception $e)
  147. {
  148. $this->error($e->getMessage());
  149. }
  150. }
  151. /**
  152. * 禁用启用
  153. */
  154. public function state()
  155. {
  156. $name = $this->request->post("name");
  157. $action = $this->request->post("action");
  158. $force = (int) $this->request->post("force");
  159. if (!$name)
  160. {
  161. $this->error(__('Parameter %s can not be empty', 'name'));
  162. }
  163. try
  164. {
  165. $action = $action == 'enable' ? $action : 'disable';
  166. //调用启用、禁用的方法
  167. Service::$action($name, $force);
  168. $this->success("操作成功");
  169. }
  170. catch (AddonException $e)
  171. {
  172. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  173. }
  174. catch (Exception $e)
  175. {
  176. $this->error($e->getMessage());
  177. }
  178. }
  179. /**
  180. * 本地上传
  181. */
  182. public function local()
  183. {
  184. $file = $this->request->file('file');
  185. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  186. if (!is_dir($addonTmpDir))
  187. {
  188. @mkdir($addonTmpDir, 0755, true);
  189. }
  190. $info = $file->rule('uniqid')->validate(['size' => 10240000, 'ext' => 'zip'])->move($addonTmpDir);
  191. if ($info)
  192. {
  193. $tmpName = substr($info->getFilename(), 0, stripos($info->getFilename(), '.'));
  194. $tmpAddonDir = ADDON_PATH . $tmpName . DS;
  195. $tmpFile = $addonTmpDir . $info->getSaveName();
  196. try
  197. {
  198. Service::unzip($tmpName);
  199. @unlink($tmpFile);
  200. $infoFile = $tmpAddonDir . 'info.ini';
  201. if (!is_file($infoFile))
  202. {
  203. throw new Exception("插件配置文件未找到");
  204. }
  205. $config = Config::parse($infoFile, '', $tmpName);
  206. $name = isset($config['name']) ? $config['name'] : '';
  207. if (!$name)
  208. {
  209. throw new Exception("插件配置信息不正确");
  210. }
  211. $newAddonDir = ADDON_PATH . $name . DS;
  212. if (is_dir($newAddonDir))
  213. {
  214. throw new Exception("上传的插件已经存在");
  215. }
  216. //重命名插件文件夹
  217. rename($tmpAddonDir, $newAddonDir);
  218. try
  219. {
  220. //默认禁用该插件
  221. $info = get_addon_info($name);
  222. if ($info['state'])
  223. {
  224. $info['state'] = 0;
  225. set_addon_info($name, $info);
  226. }
  227. //执行插件的安装方法
  228. $class = get_addon_class($name);
  229. if (class_exists($class))
  230. {
  231. $addon = new $class();
  232. $addon->install();
  233. }
  234. //导入SQL
  235. Service::importsql($name);
  236. $this->success("插件安装成功,你需要手动启用该插件,使之生效", null, ['addon' => $info]);
  237. }
  238. catch (Exception $e)
  239. {
  240. @rmdirs($newAddonDir);
  241. throw new Exception($e->getMessage());
  242. }
  243. }
  244. catch (Exception $e)
  245. {
  246. @unlink($tmpFile);
  247. @rmdirs($tmpAddonDir);
  248. $this->error($e->getMessage());
  249. }
  250. }
  251. else
  252. {
  253. // 上传失败获取错误信息
  254. $this->error($file->getError());
  255. }
  256. }
  257. /**
  258. * 刷新缓存
  259. */
  260. public function refresh()
  261. {
  262. try
  263. {
  264. Service::refresh();
  265. $this->success("操作成功");
  266. }
  267. catch (Exception $e)
  268. {
  269. $this->error($e->getMessage());
  270. }
  271. }
  272. }