Addon.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. $uid = $this->request->post("uid");
  256. $token = $this->request->post("token");
  257. $version = $this->request->post("version");
  258. $faversion = $this->request->post("faversion");
  259. $extend = [
  260. 'uid' => $uid,
  261. 'token' => $token,
  262. 'version' => $version,
  263. 'faversion' => $faversion
  264. ];
  265. //调用更新的方法
  266. $info = Service::upgrade($name, $extend);
  267. Cache::rm('__menu__');
  268. } catch (AddonException $e) {
  269. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  270. } catch (Exception $e) {
  271. $this->error(__($e->getMessage()));
  272. }
  273. $this->success(__('Operate successful'), '', ['addon' => $info]);
  274. }
  275. /**
  276. * 测试数据
  277. */
  278. public function testdata()
  279. {
  280. $name = $this->request->post("name");
  281. if (!$name) {
  282. $this->error(__('Parameter %s can not be empty', 'name'));
  283. }
  284. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  285. $this->error(__('Addon name incorrect'));
  286. }
  287. try {
  288. Service::importsql($name, 'testdata.sql');
  289. } catch (AddonException $e) {
  290. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  291. } catch (Exception $e) {
  292. $this->error(__($e->getMessage()), $e->getCode());
  293. }
  294. $this->success(__('Import successful'), '');
  295. }
  296. /**
  297. * 已装插件
  298. */
  299. public function downloaded()
  300. {
  301. $offset = (int)$this->request->get("offset");
  302. $limit = (int)$this->request->get("limit");
  303. $filter = $this->request->get("filter");
  304. $search = $this->request->get("search");
  305. $search = htmlspecialchars(strip_tags($search));
  306. $onlineaddons = $this->getAddonList();
  307. $filter = (array)json_decode($filter, true);
  308. $addons = get_addon_list();
  309. $list = [];
  310. foreach ($addons as $k => $v) {
  311. if ($search && stripos($v['name'], $search) === false && stripos($v['title'], $search) === false && stripos($v['intro'], $search) === false) {
  312. continue;
  313. }
  314. if (isset($onlineaddons[$v['name']])) {
  315. $v = array_merge($v, $onlineaddons[$v['name']]);
  316. $v['price'] = '-';
  317. } else {
  318. $v['category_id'] = 0;
  319. $v['flag'] = '';
  320. $v['banner'] = '';
  321. $v['image'] = '';
  322. $v['donateimage'] = '';
  323. $v['demourl'] = '';
  324. $v['price'] = __('None');
  325. $v['screenshots'] = [];
  326. $v['releaselist'] = [];
  327. $v['url'] = addon_url($v['name']);
  328. $v['url'] = str_replace($this->request->server('SCRIPT_NAME'), '', $v['url']);
  329. }
  330. $v['createtime'] = filemtime(ADDON_PATH . $v['name']);
  331. if ($filter && isset($filter['category_id']) && is_numeric($filter['category_id']) && $filter['category_id'] != $v['category_id']) {
  332. continue;
  333. }
  334. $list[] = $v;
  335. }
  336. $total = count($list);
  337. if ($limit) {
  338. $list = array_slice($list, $offset, $limit);
  339. }
  340. $result = array("total" => $total, "rows" => $list);
  341. $callback = $this->request->get('callback') ? "jsonp" : "json";
  342. return $callback($result);
  343. }
  344. /**
  345. * 检测
  346. */
  347. public function isbuy()
  348. {
  349. $name = $this->request->post("name");
  350. $uid = $this->request->post("uid");
  351. $token = $this->request->post("token");
  352. $version = $this->request->post("version");
  353. $faversion = $this->request->post("faversion");
  354. $extend = [
  355. 'uid' => $uid,
  356. 'token' => $token,
  357. 'version' => $version,
  358. 'faversion' => $faversion
  359. ];
  360. try {
  361. $result = Service::isBuy($name, $extend);
  362. } catch (Exception $e) {
  363. $this->error(__($e->getMessage()));
  364. }
  365. return json($result);
  366. }
  367. /**
  368. * 刷新授权
  369. */
  370. public function authorization()
  371. {
  372. $params = [
  373. 'uid' => $this->request->post('uid'),
  374. 'token' => $this->request->post('token'),
  375. 'faversion' => $this->request->post('faversion'),
  376. ];
  377. try {
  378. Service::authorization($params);
  379. } catch (Exception $e) {
  380. $this->error(__($e->getMessage()));
  381. }
  382. $this->success(__('Operate successful'));
  383. }
  384. /**
  385. * 获取插件相关表
  386. */
  387. public function get_table_list()
  388. {
  389. $name = $this->request->post("name");
  390. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  391. $this->error(__('Addon name incorrect'));
  392. }
  393. $tables = get_addon_tables($name);
  394. $prefix = Config::get('database.prefix');
  395. foreach ($tables as $index => $table) {
  396. //忽略非插件标识的表名
  397. if (!preg_match("/^{$prefix}{$name}/", $table)) {
  398. unset($tables[$index]);
  399. }
  400. }
  401. $tables = array_values($tables);
  402. $this->success('', null, ['tables' => $tables]);
  403. }
  404. protected function getAddonList()
  405. {
  406. $onlineaddons = Cache::get("onlineaddons");
  407. if (!is_array($onlineaddons) && config('fastadmin.api_url')) {
  408. $onlineaddons = [];
  409. $params = [
  410. 'uid' => $this->request->post('uid'),
  411. 'token' => $this->request->post('token'),
  412. 'version' => config('fastadmin.version'),
  413. 'faversion' => config('fastadmin.version'),
  414. ];
  415. $json = [];
  416. try {
  417. $json = Service::addons($params);
  418. } catch (\Exception $e) {
  419. }
  420. $rows = isset($json['rows']) ? $json['rows'] : [];
  421. foreach ($rows as $index => $row) {
  422. $onlineaddons[$row['name']] = $row;
  423. }
  424. Cache::set("onlineaddons", $onlineaddons, 600);
  425. }
  426. return $onlineaddons;
  427. }
  428. }