Addon.php 15 KB

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