Addon.php 15 KB

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