Addon.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. if ($this->request->isAjax()) {
  40. $result = [];
  41. debug('begin');
  42. try {
  43. $result = Service::addons($this->request->get());
  44. } catch (\Exception $e) {
  45. $this->error($e->getMessage());
  46. }
  47. debug('end');
  48. \think\Log::record("tx:" . debug('begin', 'end', 6) . 's');
  49. return json($result);
  50. }
  51. $this->assignconfig(['addons' => $addons, 'api_url' => config('fastadmin.api_url'), 'faversion' => config('fastadmin.version')]);
  52. return $this->view->fetch();
  53. }
  54. /**
  55. * 配置
  56. */
  57. public function config($name = null)
  58. {
  59. $name = $name ? $name : $this->request->get("name");
  60. if (!$name) {
  61. $this->error(__('Parameter %s can not be empty', 'name'));
  62. }
  63. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  64. $this->error(__('Addon name incorrect'));
  65. }
  66. $info = get_addon_info($name);
  67. $config = get_addon_fullconfig($name);
  68. if (!$info) {
  69. $this->error(__('Addon not exists'));
  70. }
  71. if ($this->request->isPost()) {
  72. $params = $this->request->post("row/a", [], 'trim');
  73. if ($params) {
  74. foreach ($config as $k => &$v) {
  75. if (isset($params[$v['name']])) {
  76. if ($v['type'] == 'array') {
  77. $params[$v['name']] = is_array($params[$v['name']]) ? $params[$v['name']] : (array)json_decode($params[$v['name']], true);
  78. $value = $params[$v['name']];
  79. } else {
  80. $value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
  81. }
  82. $v['value'] = $value;
  83. }
  84. }
  85. try {
  86. $addon = get_addon_instance($name);
  87. //插件自定义配置实现逻辑
  88. if (method_exists($addon, 'config')) {
  89. $addon->config($name, $config);
  90. } else {
  91. //更新配置文件
  92. set_addon_fullconfig($name, $config);
  93. Service::refresh();
  94. }
  95. } catch (Exception $e) {
  96. $this->error(__($e->getMessage()));
  97. }
  98. $this->success();
  99. }
  100. $this->error(__('Parameter %s can not be empty', ''));
  101. }
  102. $tips = [];
  103. foreach ($config as $index => &$item) {
  104. if ($item['name'] == '__tips__') {
  105. $tips = $item;
  106. unset($config[$index]);
  107. }
  108. }
  109. $this->view->assign("addon", ['info' => $info, 'config' => $config, 'tips' => $tips]);
  110. $configFile = ADDON_PATH . $name . DS . 'config.html';
  111. $viewFile = is_file($configFile) ? $configFile : '';
  112. return $this->view->fetch($viewFile);
  113. }
  114. /**
  115. * 安装
  116. */
  117. public function install()
  118. {
  119. $name = $this->request->post("name");
  120. $force = (int)$this->request->post("force");
  121. if (!$name) {
  122. $this->error(__('Parameter %s can not be empty', 'name'));
  123. }
  124. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  125. $this->error(__('Addon name incorrect'));
  126. }
  127. $info = [];
  128. try {
  129. $uid = $this->request->post("uid");
  130. $token = $this->request->post("token");
  131. $version = $this->request->post("version");
  132. $faversion = $this->request->post("faversion");
  133. $extend = [
  134. 'uid' => $uid,
  135. 'token' => $token,
  136. 'version' => $version,
  137. 'faversion' => $faversion
  138. ];
  139. $info = Service::install($name, $force, $extend);
  140. } catch (AddonException $e) {
  141. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  142. } catch (Exception $e) {
  143. $this->error(__($e->getMessage()), $e->getCode());
  144. }
  145. $this->success(__('Install successful'), '', ['addon' => $info]);
  146. }
  147. /**
  148. * 卸载
  149. */
  150. public function uninstall()
  151. {
  152. $name = $this->request->post("name");
  153. $force = (int)$this->request->post("force");
  154. $droptables = (int)$this->request->post("droptables");
  155. if (!$name) {
  156. $this->error(__('Parameter %s can not be empty', 'name'));
  157. }
  158. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  159. $this->error(__('Addon name incorrect'));
  160. }
  161. //只有开启调试且为超级管理员才允许删除相关数据库
  162. $tables = [];
  163. if ($droptables && Config::get("app_debug") && $this->auth->isSuperAdmin()) {
  164. $tables = get_addon_tables($name);
  165. }
  166. try {
  167. Service::uninstall($name, $force);
  168. if ($tables) {
  169. $prefix = Config::get('database.prefix');
  170. //删除插件关联表
  171. foreach ($tables as $index => $table) {
  172. //忽略非插件标识的表名
  173. if (!preg_match("/^{$prefix}{$name}/", $table)) {
  174. continue;
  175. }
  176. Db::execute("DROP TABLE IF EXISTS `{$table}`");
  177. }
  178. }
  179. } catch (AddonException $e) {
  180. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  181. } catch (Exception $e) {
  182. $this->error(__($e->getMessage()));
  183. }
  184. $this->success(__('Uninstall successful'));
  185. }
  186. /**
  187. * 禁用启用
  188. */
  189. public function state()
  190. {
  191. $name = $this->request->post("name");
  192. $action = $this->request->post("action");
  193. $force = (int)$this->request->post("force");
  194. if (!$name) {
  195. $this->error(__('Parameter %s can not be empty', 'name'));
  196. }
  197. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  198. $this->error(__('Addon name incorrect'));
  199. }
  200. try {
  201. $action = $action == 'enable' ? $action : 'disable';
  202. //调用启用、禁用的方法
  203. Service::$action($name, $force);
  204. Cache::rm('__menu__');
  205. } catch (AddonException $e) {
  206. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  207. } catch (Exception $e) {
  208. $this->error(__($e->getMessage()));
  209. }
  210. $this->success(__('Operate successful'));
  211. }
  212. /**
  213. * 本地上传
  214. */
  215. public function local()
  216. {
  217. Config::set('default_return_type', 'json');
  218. if (!config('app_debug')) {
  219. $this->error(__('Only work at debug mode'));
  220. }
  221. $info = [];
  222. $file = $this->request->file('file');
  223. try {
  224. $uid = $this->request->post("uid");
  225. $token = $this->request->post("token");
  226. $faversion = $this->request->post("faversion");
  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);
  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. $uid = $this->request->post("uid");
  262. $token = $this->request->post("token");
  263. $version = $this->request->post("version");
  264. $faversion = $this->request->post("faversion");
  265. $extend = [
  266. 'uid' => $uid,
  267. 'token' => $token,
  268. 'version' => $version,
  269. 'faversion' => $faversion
  270. ];
  271. //调用更新的方法
  272. $info = Service::upgrade($name, $extend);
  273. Cache::rm('__menu__');
  274. } catch (AddonException $e) {
  275. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  276. } catch (Exception $e) {
  277. $this->error(__($e->getMessage()));
  278. }
  279. $this->success(__('Operate successful'), '', ['addon' => $info]);
  280. }
  281. /**
  282. * 测试数据
  283. */
  284. public function testdata()
  285. {
  286. $name = $this->request->post("name");
  287. if (!$name) {
  288. $this->error(__('Parameter %s can not be empty', 'name'));
  289. }
  290. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  291. $this->error(__('Addon name incorrect'));
  292. }
  293. try {
  294. Service::importsql($name, 'testdata.sql');
  295. } catch (AddonException $e) {
  296. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  297. } catch (Exception $e) {
  298. $this->error(__($e->getMessage()), $e->getCode());
  299. }
  300. $this->success(__('Import successful'), '');
  301. }
  302. /**
  303. * 已装插件
  304. */
  305. public function downloaded()
  306. {
  307. $offset = (int)$this->request->get("offset");
  308. $limit = (int)$this->request->get("limit");
  309. $filter = $this->request->get("filter");
  310. $search = $this->request->get("search");
  311. $search = htmlspecialchars(strip_tags($search));
  312. $onlineaddons = $this->getAddonList();
  313. $filter = (array)json_decode($filter, true);
  314. $addons = get_addon_list();
  315. $list = [];
  316. foreach ($addons as $k => $v) {
  317. if ($search && stripos($v['name'], $search) === false && stripos($v['title'], $search) === false && stripos($v['intro'], $search) === false) {
  318. continue;
  319. }
  320. if (isset($onlineaddons[$v['name']])) {
  321. $v = array_merge($v, $onlineaddons[$v['name']]);
  322. $v['price'] = '-';
  323. } else {
  324. $v['category_id'] = 0;
  325. $v['flag'] = '';
  326. $v['banner'] = '';
  327. $v['image'] = '';
  328. $v['donateimage'] = '';
  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 = isset($json['rows']) ? $json['rows'] : [];
  427. foreach ($rows as $index => $row) {
  428. $onlineaddons[$row['name']] = $row;
  429. }
  430. Cache::set("onlineaddons", $onlineaddons, 600);
  431. }
  432. return $onlineaddons;
  433. }
  434. }