Addon.php 11 KB

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