Addon.php 12 KB

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