Addon.php 11 KB

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