Addon.php 12 KB

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