Addon.php 15 KB

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