Addon.php 13 KB

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