Addon.php 12 KB

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