Addon.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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\Db;
  10. use think\Exception;
  11. /**
  12. * 插件管理
  13. *
  14. * @icon fa fa-cube
  15. * @remark 可在线安装、卸载、禁用、启用插件,同时支持添加本地插件。FastAdmin已上线插件商店 ,你可以发布你的免费或付费插件:<a href="https://www.fastadmin.net/store.html" target="_blank">https://www.fastadmin.net/store.html</a>
  16. */
  17. class Addon extends Backend
  18. {
  19. protected $model = null;
  20. protected $noNeedRight = ['get_table_list'];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. if (!$this->auth->isSuperAdmin() && in_array($this->request->action(), ['install', 'uninstall', 'local', 'upgrade'])) {
  25. $this->error(__('Access is allowed only to the super management group'));
  26. }
  27. }
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. $addons = get_addon_list();
  34. foreach ($addons as $k => &$v) {
  35. $config = get_addon_config($v['name']);
  36. $v['config'] = $config ? 1 : 0;
  37. $v['url'] = str_replace($this->request->server('SCRIPT_NAME'), '', $v['url']);
  38. }
  39. $this->assignconfig(['addons' => $addons]);
  40. // $addons = get_addon_list();
  41. // foreach ($addons as $k => &$v) {
  42. // $config = get_addon_config($v['name']);
  43. // $v['config'] = $config ? 1 : 0;
  44. // $v['url'] = str_replace($this->request->server('SCRIPT_NAME'), '', $v['url']);
  45. // }
  46. // $this->assignconfig(['addons' => $addons, 'api_url' => config('fastadmin.api_url'), 'faversion' => config('fastadmin.version')]);
  47. return $this->view->fetch();
  48. }
  49. /**
  50. * 配置
  51. */
  52. public function config($name = null)
  53. {
  54. $name = $name ? $name : $this->request->get("name");
  55. if (!$name) {
  56. $this->error(__('Parameter %s can not be empty', 'name'));
  57. }
  58. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  59. $this->error(__('Addon name incorrect'));
  60. }
  61. if (!is_dir(ADDON_PATH . $name)) {
  62. $this->error(__('Directory not found'));
  63. }
  64. $info = get_addon_info($name);
  65. $config = get_addon_fullconfig($name);
  66. if (!$info) {
  67. $this->error(__('No Results were found'));
  68. }
  69. if ($this->request->isPost()) {
  70. $params = $this->request->post("row/a", [], 'trim');
  71. if ($params) {
  72. foreach ($config as $k => &$v) {
  73. if (isset($params[$v['name']])) {
  74. if ($v['type'] == 'array') {
  75. $params[$v['name']] = is_array($params[$v['name']]) ? $params[$v['name']] : (array)json_decode($params[$v['name']], true);
  76. $value = $params[$v['name']];
  77. } else {
  78. $value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
  79. }
  80. $v['value'] = $value;
  81. }
  82. }
  83. try {
  84. //更新配置文件
  85. set_addon_fullconfig($name, $config);
  86. Service::refresh();
  87. $this->success();
  88. } catch (Exception $e) {
  89. $this->error(__($e->getMessage()));
  90. }
  91. }
  92. $this->error(__('Parameter %s can not be empty', ''));
  93. }
  94. $tips = [];
  95. foreach ($config as $index => &$item) {
  96. if ($item['name'] == '__tips__') {
  97. $tips = $item;
  98. unset($config[$index]);
  99. }
  100. }
  101. $this->view->assign("addon", ['info' => $info, 'config' => $config, 'tips' => $tips]);
  102. $configFile = ADDON_PATH . $name . DS . 'config.html';
  103. $viewFile = is_file($configFile) ? $configFile : '';
  104. return $this->view->fetch($viewFile);
  105. }
  106. /**
  107. * 安装
  108. */
  109. public function install()
  110. {
  111. $name = $this->request->post("name");
  112. $force = (int)$this->request->post("force");
  113. if (!$name) {
  114. $this->error(__('Parameter %s can not be empty', 'name'));
  115. }
  116. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  117. $this->error(__('Addon name incorrect'));
  118. }
  119. $info = [];
  120. try {
  121. $uid = $this->request->post("uid");
  122. $token = $this->request->post("token");
  123. $version = $this->request->post("version");
  124. $faversion = $this->request->post("faversion");
  125. $extend = [
  126. 'uid' => $uid,
  127. 'token' => $token,
  128. 'version' => $version,
  129. 'faversion' => $faversion
  130. ];
  131. $info = Service::install($name, $force, $extend);
  132. } catch (AddonException $e) {
  133. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  134. } catch (Exception $e) {
  135. $this->error(__($e->getMessage()), $e->getCode());
  136. }
  137. $this->success(__('Install successful'), '', ['addon' => $info]);
  138. }
  139. /**
  140. * 卸载
  141. */
  142. public function uninstall()
  143. {
  144. $name = $this->request->post("name");
  145. $force = (int)$this->request->post("force");
  146. $droptables = (int)$this->request->post("droptables");
  147. if (!$name) {
  148. $this->error(__('Parameter %s can not be empty', 'name'));
  149. }
  150. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  151. $this->error(__('Addon name incorrect'));
  152. }
  153. //只有开启调试且为超级管理员才允许删除相关数据库
  154. $tables = [];
  155. if ($droptables && Config::get("app_debug") && $this->auth->isSuperAdmin()) {
  156. $tables = get_addon_tables($name);
  157. }
  158. try {
  159. Service::uninstall($name, $force);
  160. if ($tables) {
  161. $prefix = Config::get('database.prefix');
  162. //删除插件关联表
  163. foreach ($tables as $index => $table) {
  164. //忽略非插件标识的表名
  165. if (!preg_match("/^{$prefix}{$name}/", $table)) {
  166. continue;
  167. }
  168. Db::execute("DROP TABLE IF EXISTS `{$table}`");
  169. }
  170. }
  171. } catch (AddonException $e) {
  172. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  173. } catch (Exception $e) {
  174. $this->error(__($e->getMessage()));
  175. }
  176. $this->success(__('Uninstall successful'));
  177. }
  178. /**
  179. * 禁用启用
  180. */
  181. public function state()
  182. {
  183. $name = $this->request->post("name");
  184. $action = $this->request->post("action");
  185. $force = (int)$this->request->post("force");
  186. if (!$name) {
  187. $this->error(__('Parameter %s can not be empty', 'name'));
  188. }
  189. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  190. $this->error(__('Addon name incorrect'));
  191. }
  192. try {
  193. $action = $action == 'enable' ? $action : 'disable';
  194. //调用启用、禁用的方法
  195. Service::$action($name, $force);
  196. Cache::rm('__menu__');
  197. } catch (AddonException $e) {
  198. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  199. } catch (Exception $e) {
  200. $this->error(__($e->getMessage()));
  201. }
  202. $this->success(__('Operate successful'));
  203. }
  204. /**
  205. * 本地上传
  206. */
  207. public function local()
  208. {
  209. Config::set('default_return_type', 'json');
  210. $info = [];
  211. $file = $this->request->file('file');
  212. try {
  213. $uid = $this->request->post("uid");
  214. $token = $this->request->post("token");
  215. $faversion = $this->request->post("faversion");
  216. if (!$uid || !$token) {
  217. throw new Exception(__('Please login and try to install'));
  218. }
  219. $extend = [
  220. 'uid' => $uid,
  221. 'token' => $token,
  222. 'faversion' => $faversion
  223. ];
  224. $info = Service::local($file, $extend);
  225. } catch (AddonException $e) {
  226. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  227. } catch (Exception $e) {
  228. $this->error(__($e->getMessage()));
  229. }
  230. $this->success(__('Offline installed tips'), '', ['addon' => $info]);
  231. }
  232. /**
  233. * 更新插件
  234. */
  235. public function upgrade()
  236. {
  237. $name = $this->request->post("name");
  238. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  239. if (!$name) {
  240. $this->error(__('Parameter %s can not be empty', 'name'));
  241. }
  242. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  243. $this->error(__('Addon name incorrect'));
  244. }
  245. if (!is_dir($addonTmpDir)) {
  246. @mkdir($addonTmpDir, 0755, true);
  247. }
  248. $info = [];
  249. try {
  250. $uid = $this->request->post("uid");
  251. $token = $this->request->post("token");
  252. $version = $this->request->post("version");
  253. $faversion = $this->request->post("faversion");
  254. $extend = [
  255. 'uid' => $uid,
  256. 'token' => $token,
  257. 'version' => $version,
  258. 'faversion' => $faversion
  259. ];
  260. //调用更新的方法
  261. $info = Service::upgrade($name, $extend);
  262. Cache::rm('__menu__');
  263. } catch (AddonException $e) {
  264. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  265. } catch (Exception $e) {
  266. $this->error(__($e->getMessage()));
  267. }
  268. $this->success(__('Operate successful'), '', ['addon' => $info]);
  269. }
  270. /**
  271. * 已装插件
  272. */
  273. public function downloaded()
  274. {
  275. $offset = (int)$this->request->get("offset");
  276. $limit = (int)$this->request->get("limit");
  277. $filter = $this->request->get("filter");
  278. $search = $this->request->get("search");
  279. $search = htmlspecialchars(strip_tags($search));
  280. $onlineaddons = Cache::get("onlineaddons");
  281. if (!is_array($onlineaddons) && config('fastadmin.api_url')) {
  282. $onlineaddons = [];
  283. $result = Http::sendRequest(config('fastadmin.api_url') . '/addon/index', [], 'GET', [
  284. CURLOPT_HTTPHEADER => ['Accept-Encoding:gzip'],
  285. CURLOPT_ENCODING => "gzip"
  286. ]);
  287. if ($result['ret']) {
  288. $json = (array)json_decode($result['msg'], true);
  289. $rows = isset($json['rows']) ? $json['rows'] : [];
  290. foreach ($rows as $index => $row) {
  291. $onlineaddons[$row['name']] = $row;
  292. }
  293. }
  294. Cache::set("onlineaddons", $onlineaddons, 600);
  295. }
  296. $filter = (array)json_decode($filter, true);
  297. $addons = get_addon_list();
  298. $list = [];
  299. foreach ($addons as $k => $v) {
  300. if ($search && stripos($v['name'], $search) === false && stripos($v['title'], $search) === false && stripos($v['intro'], $search) === false) {
  301. continue;
  302. }
  303. if (isset($onlineaddons[$v['name']])) {
  304. $v = array_merge($v, $onlineaddons[$v['name']]);
  305. } else {
  306. $v['category_id'] = 0;
  307. $v['flag'] = '';
  308. $v['banner'] = '';
  309. $v['image'] = '';
  310. $v['donateimage'] = '';
  311. $v['demourl'] = '';
  312. $v['price'] = __('None');
  313. $v['screenshots'] = [];
  314. $v['releaselist'] = [];
  315. }
  316. $v['url'] = addon_url($v['name']);
  317. $v['url'] = str_replace($this->request->server('SCRIPT_NAME'), '', $v['url']);
  318. $v['createtime'] = filemtime(ADDON_PATH . $v['name']);
  319. if ($filter && isset($filter['category_id']) && is_numeric($filter['category_id']) && $filter['category_id'] != $v['category_id']) {
  320. continue;
  321. }
  322. $list[] = $v;
  323. }
  324. $total = count($list);
  325. if ($limit) {
  326. $list = array_slice($list, $offset, $limit);
  327. }
  328. $result = array("total" => $total, "rows" => $list);
  329. $callback = $this->request->get('callback') ? "jsonp" : "json";
  330. return $callback($result);
  331. }
  332. /**
  333. * 获取插件相关表
  334. */
  335. public function get_table_list()
  336. {
  337. $name = $this->request->post("name");
  338. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  339. $this->error(__('Addon name incorrect'));
  340. }
  341. $tables = get_addon_tables($name);
  342. $prefix = Config::get('database.prefix');
  343. foreach ($tables as $index => $table) {
  344. //忽略非插件标识的表名
  345. if (!preg_match("/^{$prefix}{$name}/", $table)) {
  346. unset($tables[$index]);
  347. }
  348. }
  349. $tables = array_values($tables);
  350. $this->success('', null, ['tables' => $tables]);
  351. }
  352. }