Addon.php 10 KB

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