Addon.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. @unlink($tmpFile);
  184. $infoFile = $tmpAddonDir . 'info.ini';
  185. if (!is_file($infoFile)) {
  186. throw new Exception(__('Addon info file was not found'));
  187. }
  188. $config = Config::parse($infoFile, '', $tmpName);
  189. $name = isset($config['name']) ? $config['name'] : '';
  190. if (!$name) {
  191. throw new Exception(__('Addon info file data incorrect'));
  192. }
  193. $newAddonDir = ADDON_PATH . $name . DS;
  194. if (is_dir($newAddonDir)) {
  195. throw new Exception(__('Addon already exists'));
  196. }
  197. //重命名插件文件夹
  198. rename($tmpAddonDir, $newAddonDir);
  199. try {
  200. //默认禁用该插件
  201. $info = get_addon_info($name);
  202. if ($info['state']) {
  203. $info['state'] = 0;
  204. set_addon_info($name, $info);
  205. }
  206. //执行插件的安装方法
  207. $class = get_addon_class($name);
  208. if (class_exists($class)) {
  209. $addon = new $class();
  210. $addon->install();
  211. }
  212. //导入SQL
  213. Service::importsql($name);
  214. $info['config'] = get_addon_config($name) ? 1 : 0;
  215. $this->success(__('Offline installed tips'), null, ['addon' => $info]);
  216. } catch (Exception $e) {
  217. @rmdirs($newAddonDir);
  218. throw new Exception(__($e->getMessage()));
  219. }
  220. } catch (Exception $e) {
  221. @unlink($tmpFile);
  222. @rmdirs($tmpAddonDir);
  223. $this->error(__($e->getMessage()));
  224. }
  225. } else {
  226. // 上传失败获取错误信息
  227. $this->error(__($file->getError()));
  228. }
  229. }
  230. /**
  231. * 更新插件
  232. */
  233. public function upgrade()
  234. {
  235. $name = $this->request->post("name");
  236. if (!$name) {
  237. $this->error(__('Parameter %s can not be empty', 'name'));
  238. }
  239. try {
  240. $uid = $this->request->post("uid");
  241. $token = $this->request->post("token");
  242. $version = $this->request->post("version");
  243. $faversion = $this->request->post("faversion");
  244. $extend = [
  245. 'uid' => $uid,
  246. 'token' => $token,
  247. 'version' => $version,
  248. 'faversion' => $faversion
  249. ];
  250. //调用更新的方法
  251. Service::upgrade($name, $extend);
  252. Cache::rm('__menu__');
  253. $this->success(__('Operate successful'));
  254. } catch (AddonException $e) {
  255. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  256. } catch (Exception $e) {
  257. $this->error(__($e->getMessage()));
  258. }
  259. }
  260. /**
  261. * 已装插件
  262. */
  263. public function downloaded()
  264. {
  265. $offset = (int)$this->request->get("offset");
  266. $limit = (int)$this->request->get("limit");
  267. $filter = $this->request->get("filter");
  268. $search = $this->request->get("search");
  269. $search = htmlspecialchars(strip_tags($search));
  270. $onlineaddons = Cache::get("onlineaddons");
  271. if (!is_array($onlineaddons)) {
  272. $onlineaddons = [];
  273. $result = Http::sendRequest(config('fastadmin.api_url') . '/addon/index');
  274. if ($result['ret']) {
  275. $json = (array)json_decode($result['msg'], true);
  276. $rows = isset($json['rows']) ? $json['rows'] : [];
  277. foreach ($rows as $index => $row) {
  278. $onlineaddons[$row['name']] = $row;
  279. }
  280. }
  281. Cache::set("onlineaddons", $onlineaddons, 600);
  282. }
  283. $filter = (array)json_decode($filter, true);
  284. $addons = get_addon_list();
  285. $list = [];
  286. foreach ($addons as $k => $v) {
  287. if ($search && stripos($v['name'], $search) === false && stripos($v['intro'], $search) === false) {
  288. continue;
  289. }
  290. if (isset($onlineaddons[$v['name']])) {
  291. $v = array_merge($v, $onlineaddons[$v['name']]);
  292. } else {
  293. $v['category_id'] = 0;
  294. $v['flag'] = '';
  295. $v['banner'] = '';
  296. $v['image'] = '';
  297. $v['donateimage'] = '';
  298. $v['demourl'] = '';
  299. $v['price'] = __('None');
  300. $v['screenshots'] = [];
  301. $v['releaselist'] = [];
  302. }
  303. $v['url'] = addon_url($v['name']);
  304. $v['url'] = str_replace($this->request->server('SCRIPT_NAME'), '', $v['url']);
  305. $v['createtime'] = filemtime(ADDON_PATH . $v['name']);
  306. if ($filter && isset($filter['category_id']) && is_numeric($filter['category_id']) && $filter['category_id'] != $v['category_id']) {
  307. continue;
  308. }
  309. $list[] = $v;
  310. }
  311. $total = count($list);
  312. if ($limit) {
  313. $list = array_slice($list, $offset, $limit);
  314. }
  315. $result = array("total" => $total, "rows" => $list);
  316. $callback = $this->request->get('callback') ? "jsonp" : "json";
  317. return $callback($result);
  318. }
  319. }