Addon.php 11 KB

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