Addon.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 可在线安装、卸载、禁用、启用、配置、升级插件,插件升级前请做好备份。
  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', 'authorization', 'testdata'])) {
  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, 'api_url' => config('fastadmin.api_url'), 'faversion' => config('fastadmin.version'), 'domain' => request()->host(true)]);
  40. return $this->view->fetch();
  41. }
  42. /**
  43. * 配置
  44. */
  45. public function config($name = null)
  46. {
  47. $name = $name ? $name : $this->request->get("name");
  48. if (!$name) {
  49. $this->error(__('Parameter %s can not be empty', 'name'));
  50. }
  51. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  52. $this->error(__('Addon name incorrect'));
  53. }
  54. $info = get_addon_info($name);
  55. $config = get_addon_fullconfig($name);
  56. if (!$info) {
  57. $this->error(__('Addon not exists'));
  58. }
  59. if ($this->request->isPost()) {
  60. $params = $this->request->post("row/a", [], 'trim');
  61. if ($params) {
  62. foreach ($config as $k => &$v) {
  63. if (isset($params[$v['name']])) {
  64. if ($v['type'] == 'array') {
  65. $params[$v['name']] = is_array($params[$v['name']]) ? $params[$v['name']] : (array)json_decode($params[$v['name']], true);
  66. $value = $params[$v['name']];
  67. } else {
  68. $value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
  69. }
  70. $v['value'] = $value;
  71. }
  72. }
  73. try {
  74. $addon = get_addon_instance($name);
  75. //插件自定义配置实现逻辑
  76. if (method_exists($addon, 'config')) {
  77. $addon->config($name, $config);
  78. } else {
  79. //更新配置文件
  80. set_addon_fullconfig($name, $config);
  81. Service::refresh();
  82. }
  83. } catch (Exception $e) {
  84. $this->error(__($e->getMessage()));
  85. }
  86. $this->success();
  87. }
  88. $this->error(__('Parameter %s can not be empty', ''));
  89. }
  90. $tips = [];
  91. $groupList = [];
  92. $ungroupList = [];
  93. foreach ($config as $index => &$item) {
  94. //如果有设置分组
  95. if (isset($item['group']) && $item['group']) {
  96. if (!in_array($item['group'], $groupList)) {
  97. $groupList["custom" . (count($groupList) + 1)] = $item['group'];
  98. }
  99. } elseif ($item['name'] != '__tips__') {
  100. $ungroupList[] = $item['name'];
  101. }
  102. if ($item['name'] == '__tips__') {
  103. $tips = $item;
  104. unset($config[$index]);
  105. }
  106. }
  107. if ($ungroupList) {
  108. $groupList['other'] = '其它';
  109. }
  110. $this->view->assign("groupList", $groupList);
  111. $this->view->assign("addon", ['info' => $info, 'config' => $config, 'tips' => $tips]);
  112. $configFile = ADDON_PATH . $name . DS . 'config.html';
  113. $viewFile = is_file($configFile) ? $configFile : '';
  114. return $this->view->fetch($viewFile);
  115. }
  116. /**
  117. * 安装
  118. */
  119. public function install()
  120. {
  121. $name = $this->request->post("name");
  122. $force = (int)$this->request->post("force");
  123. if (!$name) {
  124. $this->error(__('Parameter %s can not be empty', 'name'));
  125. }
  126. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  127. $this->error(__('Addon name incorrect'));
  128. }
  129. $info = [];
  130. try {
  131. $uid = $this->request->post("uid");
  132. $token = $this->request->post("token");
  133. $version = $this->request->post("version");
  134. $faversion = $this->request->post("faversion");
  135. $extend = [
  136. 'uid' => $uid,
  137. 'token' => $token,
  138. 'version' => $version,
  139. 'faversion' => $faversion
  140. ];
  141. $info = Service::install($name, $force, $extend);
  142. } catch (AddonException $e) {
  143. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  144. } catch (Exception $e) {
  145. $this->error(__($e->getMessage()), $e->getCode());
  146. }
  147. $this->success(__('Install successful'), '', ['addon' => $info]);
  148. }
  149. /**
  150. * 卸载
  151. */
  152. public function uninstall()
  153. {
  154. $name = $this->request->post("name");
  155. $force = (int)$this->request->post("force");
  156. $droptables = (int)$this->request->post("droptables");
  157. if (!$name) {
  158. $this->error(__('Parameter %s can not be empty', 'name'));
  159. }
  160. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  161. $this->error(__('Addon name incorrect'));
  162. }
  163. //只有开启调试且为超级管理员才允许删除相关数据库
  164. $tables = [];
  165. if ($droptables && Config::get("app_debug") && $this->auth->isSuperAdmin()) {
  166. $tables = get_addon_tables($name);
  167. }
  168. try {
  169. Service::uninstall($name, $force);
  170. if ($tables) {
  171. $prefix = Config::get('database.prefix');
  172. //删除插件关联表
  173. foreach ($tables as $index => $table) {
  174. //忽略非插件标识的表名
  175. if (!preg_match("/^{$prefix}{$name}/", $table)) {
  176. continue;
  177. }
  178. Db::execute("DROP TABLE IF EXISTS `{$table}`");
  179. }
  180. }
  181. } catch (AddonException $e) {
  182. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  183. } catch (Exception $e) {
  184. $this->error(__($e->getMessage()));
  185. }
  186. $this->success(__('Uninstall successful'));
  187. }
  188. /**
  189. * 禁用启用
  190. */
  191. public function state()
  192. {
  193. $name = $this->request->post("name");
  194. $action = $this->request->post("action");
  195. $force = (int)$this->request->post("force");
  196. if (!$name) {
  197. $this->error(__('Parameter %s can not be empty', 'name'));
  198. }
  199. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  200. $this->error(__('Addon name incorrect'));
  201. }
  202. try {
  203. $action = $action == 'enable' ? $action : 'disable';
  204. //调用启用、禁用的方法
  205. Service::$action($name, $force);
  206. Cache::rm('__menu__');
  207. } catch (AddonException $e) {
  208. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  209. } catch (Exception $e) {
  210. $this->error(__($e->getMessage()));
  211. }
  212. $this->success(__('Operate successful'));
  213. }
  214. /**
  215. * 本地上传
  216. */
  217. public function local()
  218. {
  219. Config::set('default_return_type', 'json');
  220. $info = [];
  221. $file = $this->request->file('file');
  222. try {
  223. $uid = $this->request->post("uid");
  224. $token = $this->request->post("token");
  225. $faversion = $this->request->post("faversion");
  226. $force = $this->request->post("force");
  227. if (!$uid || !$token) {
  228. throw new Exception(__('Please login and try to install'));
  229. }
  230. $extend = [
  231. 'uid' => $uid,
  232. 'token' => $token,
  233. 'faversion' => $faversion
  234. ];
  235. $info = Service::local($file, $extend, $force);
  236. } catch (AddonException $e) {
  237. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  238. } catch (Exception $e) {
  239. $this->error(__($e->getMessage()));
  240. }
  241. $this->success(__('Offline installed tips'), '', ['addon' => $info]);
  242. }
  243. /**
  244. * 更新插件
  245. */
  246. public function upgrade()
  247. {
  248. $name = $this->request->post("name");
  249. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  250. if (!$name) {
  251. $this->error(__('Parameter %s can not be empty', 'name'));
  252. }
  253. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  254. $this->error(__('Addon name incorrect'));
  255. }
  256. if (!is_dir($addonTmpDir)) {
  257. @mkdir($addonTmpDir, 0755, true);
  258. }
  259. $info = [];
  260. try {
  261. $info = get_addon_info($name);
  262. $uid = $this->request->post("uid");
  263. $token = $this->request->post("token");
  264. $version = $this->request->post("version");
  265. $faversion = $this->request->post("faversion");
  266. $extend = [
  267. 'uid' => $uid,
  268. 'token' => $token,
  269. 'version' => $version,
  270. 'oldversion' => $info['version'] ?? '',
  271. 'faversion' => $faversion
  272. ];
  273. //调用更新的方法
  274. $info = Service::upgrade($name, $extend);
  275. Cache::rm('__menu__');
  276. } catch (AddonException $e) {
  277. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  278. } catch (Exception $e) {
  279. $this->error(__($e->getMessage()));
  280. }
  281. $this->success(__('Operate successful'), '', ['addon' => $info]);
  282. }
  283. /**
  284. * 测试数据
  285. */
  286. public function testdata()
  287. {
  288. $name = $this->request->post("name");
  289. if (!$name) {
  290. $this->error(__('Parameter %s can not be empty', 'name'));
  291. }
  292. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  293. $this->error(__('Addon name incorrect'));
  294. }
  295. try {
  296. Service::importsql($name, 'testdata.sql');
  297. } catch (AddonException $e) {
  298. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  299. } catch (Exception $e) {
  300. $this->error(__($e->getMessage()), $e->getCode());
  301. }
  302. $this->success(__('Import successful'), '');
  303. }
  304. /**
  305. * 已装插件
  306. */
  307. public function downloaded()
  308. {
  309. $offset = (int)$this->request->get("offset");
  310. $limit = (int)$this->request->get("limit");
  311. $filter = $this->request->get("filter", '');
  312. $search = $this->request->get("search", '', 'strip_tags,htmlspecialchars');
  313. $onlineaddons = $this->getAddonList();
  314. $filter = (array)json_decode($filter, true);
  315. $addons = get_addon_list();
  316. $list = [];
  317. foreach ($addons as $k => $v) {
  318. if ($search && stripos($v['name'], $search) === false && stripos($v['title'], $search) === false && stripos($v['intro'], $search) === false) {
  319. continue;
  320. }
  321. if (isset($onlineaddons[$v['name']])) {
  322. $v = array_merge($v, $onlineaddons[$v['name']]);
  323. $v['price'] = '-';
  324. } else {
  325. $v['category_id'] = 0;
  326. $v['flag'] = '';
  327. $v['banner'] = '';
  328. $v['image'] = '';
  329. $v['demourl'] = '';
  330. $v['price'] = __('None');
  331. $v['screenshots'] = [];
  332. $v['releaselist'] = [];
  333. $v['url'] = addon_url($v['name']);
  334. $v['url'] = str_replace($this->request->server('SCRIPT_NAME'), '', $v['url']);
  335. }
  336. $v['createtime'] = filemtime(ADDON_PATH . $v['name']);
  337. if ($filter && isset($filter['category_id']) && is_numeric($filter['category_id']) && $filter['category_id'] != $v['category_id']) {
  338. continue;
  339. }
  340. $list[] = $v;
  341. }
  342. $total = count($list);
  343. if ($limit) {
  344. $list = array_slice($list, $offset, $limit);
  345. }
  346. $result = array("total" => $total, "rows" => $list);
  347. $callback = $this->request->get('callback') ? "jsonp" : "json";
  348. return $callback($result);
  349. }
  350. /**
  351. * 检测
  352. */
  353. public function isbuy()
  354. {
  355. $name = $this->request->post("name");
  356. $uid = $this->request->post("uid");
  357. $token = $this->request->post("token");
  358. $version = $this->request->post("version");
  359. $faversion = $this->request->post("faversion");
  360. $extend = [
  361. 'uid' => $uid,
  362. 'token' => $token,
  363. 'version' => $version,
  364. 'faversion' => $faversion
  365. ];
  366. try {
  367. $result = Service::isBuy($name, $extend);
  368. } catch (Exception $e) {
  369. $this->error(__($e->getMessage()));
  370. }
  371. return json($result);
  372. }
  373. /**
  374. * 刷新授权
  375. */
  376. public function authorization()
  377. {
  378. $params = [
  379. 'uid' => $this->request->post('uid'),
  380. 'token' => $this->request->post('token'),
  381. 'faversion' => $this->request->post('faversion'),
  382. ];
  383. try {
  384. Service::authorization($params);
  385. } catch (Exception $e) {
  386. $this->error(__($e->getMessage()));
  387. }
  388. $this->success(__('Operate successful'));
  389. }
  390. /**
  391. * 获取插件相关表
  392. */
  393. public function get_table_list()
  394. {
  395. $name = $this->request->post("name");
  396. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  397. $this->error(__('Addon name incorrect'));
  398. }
  399. $tables = get_addon_tables($name);
  400. $prefix = Config::get('database.prefix');
  401. foreach ($tables as $index => $table) {
  402. //忽略非插件标识的表名
  403. if (!preg_match("/^{$prefix}{$name}/", $table)) {
  404. unset($tables[$index]);
  405. }
  406. }
  407. $tables = array_values($tables);
  408. $this->success('', null, ['tables' => $tables]);
  409. }
  410. protected function getAddonList()
  411. {
  412. $onlineaddons = Cache::get("onlineaddons");
  413. if (!is_array($onlineaddons) && config('fastadmin.api_url')) {
  414. $onlineaddons = [];
  415. $params = [
  416. 'uid' => $this->request->post('uid'),
  417. 'token' => $this->request->post('token'),
  418. 'version' => config('fastadmin.version'),
  419. 'faversion' => config('fastadmin.version'),
  420. ];
  421. $json = [];
  422. try {
  423. $json = Service::addons($params);
  424. } catch (\Exception $e) {
  425. }
  426. $rows = $json['rows'] ?? [];
  427. foreach ($rows as $index => $row) {
  428. if (!isset($row['name'])) {
  429. continue;
  430. }
  431. $onlineaddons[$row['name']] = $row;
  432. }
  433. Cache::set("onlineaddons", $onlineaddons, 600);
  434. }
  435. return $onlineaddons;
  436. }
  437. }