Addon.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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'])) {
  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')]);
  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. if (!is_dir(ADDON_PATH . $name)) {
  55. $this->error(__('Directory not found'));
  56. }
  57. $info = get_addon_info($name);
  58. $config = get_addon_fullconfig($name);
  59. if (!$info) {
  60. $this->error(__('No Results were found'));
  61. }
  62. if ($this->request->isPost()) {
  63. $params = $this->request->post("row/a", [], 'trim');
  64. if ($params) {
  65. foreach ($config as $k => &$v) {
  66. if (isset($params[$v['name']])) {
  67. if ($v['type'] == 'array') {
  68. $params[$v['name']] = is_array($params[$v['name']]) ? $params[$v['name']] : (array)json_decode($params[$v['name']], true);
  69. $value = $params[$v['name']];
  70. } else {
  71. $value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
  72. }
  73. $v['value'] = $value;
  74. }
  75. }
  76. try {
  77. //更新配置文件
  78. set_addon_fullconfig($name, $config);
  79. Service::refresh();
  80. $this->success();
  81. } catch (Exception $e) {
  82. $this->error(__($e->getMessage()));
  83. }
  84. }
  85. $this->error(__('Parameter %s can not be empty', ''));
  86. }
  87. $tips = [];
  88. foreach ($config as $index => &$item) {
  89. if ($item['name'] == '__tips__') {
  90. $tips = $item;
  91. unset($config[$index]);
  92. }
  93. }
  94. $this->view->assign("addon", ['info' => $info, 'config' => $config, 'tips' => $tips]);
  95. $configFile = ADDON_PATH . $name . DS . 'config.html';
  96. $viewFile = is_file($configFile) ? $configFile : '';
  97. return $this->view->fetch($viewFile);
  98. }
  99. /**
  100. * 安装
  101. */
  102. public function install()
  103. {
  104. $name = $this->request->post("name");
  105. $force = (int)$this->request->post("force");
  106. if (!$name) {
  107. $this->error(__('Parameter %s can not be empty', 'name'));
  108. }
  109. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  110. $this->error(__('Addon name incorrect'));
  111. }
  112. $info = [];
  113. try {
  114. $uid = $this->request->post("uid");
  115. $token = $this->request->post("token");
  116. $version = $this->request->post("version");
  117. $faversion = $this->request->post("faversion");
  118. $extend = [
  119. 'uid' => $uid,
  120. 'token' => $token,
  121. 'version' => $version,
  122. 'faversion' => $faversion
  123. ];
  124. $info = Service::install($name, $force, $extend);
  125. } catch (AddonException $e) {
  126. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  127. } catch (Exception $e) {
  128. $this->error(__($e->getMessage()), $e->getCode());
  129. }
  130. $this->success(__('Install successful'), '', ['addon' => $info]);
  131. }
  132. /**
  133. * 卸载
  134. */
  135. public function uninstall()
  136. {
  137. $name = $this->request->post("name");
  138. $force = (int)$this->request->post("force");
  139. $droptables = (int)$this->request->post("droptables");
  140. if (!$name) {
  141. $this->error(__('Parameter %s can not be empty', 'name'));
  142. }
  143. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  144. $this->error(__('Addon name incorrect'));
  145. }
  146. //只有开启调试且为超级管理员才允许删除相关数据库
  147. $tables = [];
  148. if ($droptables && Config::get("app_debug") && $this->auth->isSuperAdmin()) {
  149. $tables = get_addon_tables($name);
  150. }
  151. try {
  152. Service::uninstall($name, $force);
  153. if ($tables) {
  154. $prefix = Config::get('database.prefix');
  155. //删除插件关联表
  156. foreach ($tables as $index => $table) {
  157. //忽略非插件标识的表名
  158. if (!preg_match("/^{$prefix}{$name}/", $table)) {
  159. continue;
  160. }
  161. Db::execute("DROP TABLE IF EXISTS `{$table}`");
  162. }
  163. }
  164. } catch (AddonException $e) {
  165. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  166. } catch (Exception $e) {
  167. $this->error(__($e->getMessage()));
  168. }
  169. $this->success(__('Uninstall successful'));
  170. }
  171. /**
  172. * 禁用启用
  173. */
  174. public function state()
  175. {
  176. $name = $this->request->post("name");
  177. $action = $this->request->post("action");
  178. $force = (int)$this->request->post("force");
  179. if (!$name) {
  180. $this->error(__('Parameter %s can not be empty', 'name'));
  181. }
  182. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  183. $this->error(__('Addon name incorrect'));
  184. }
  185. try {
  186. $action = $action == 'enable' ? $action : 'disable';
  187. //调用启用、禁用的方法
  188. Service::$action($name, $force);
  189. Cache::rm('__menu__');
  190. } catch (AddonException $e) {
  191. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  192. } catch (Exception $e) {
  193. $this->error(__($e->getMessage()));
  194. }
  195. $this->success(__('Operate successful'));
  196. }
  197. /**
  198. * 本地上传
  199. */
  200. public function local()
  201. {
  202. Config::set('default_return_type', 'json');
  203. $info = [];
  204. $file = $this->request->file('file');
  205. try {
  206. $uid = $this->request->post("uid");
  207. $token = $this->request->post("token");
  208. $faversion = $this->request->post("faversion");
  209. if (!$uid || !$token) {
  210. throw new Exception(__('Please login and try to install'));
  211. }
  212. $extend = [
  213. 'uid' => $uid,
  214. 'token' => $token,
  215. 'faversion' => $faversion
  216. ];
  217. $info = Service::local($file, $extend);
  218. } catch (AddonException $e) {
  219. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  220. } catch (Exception $e) {
  221. $this->error(__($e->getMessage()));
  222. }
  223. $this->success(__('Offline installed tips'), '', ['addon' => $info]);
  224. }
  225. /**
  226. * 更新插件
  227. */
  228. public function upgrade()
  229. {
  230. $name = $this->request->post("name");
  231. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  232. if (!$name) {
  233. $this->error(__('Parameter %s can not be empty', 'name'));
  234. }
  235. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  236. $this->error(__('Addon name incorrect'));
  237. }
  238. if (!is_dir($addonTmpDir)) {
  239. @mkdir($addonTmpDir, 0755, true);
  240. }
  241. $info = [];
  242. try {
  243. $uid = $this->request->post("uid");
  244. $token = $this->request->post("token");
  245. $version = $this->request->post("version");
  246. $faversion = $this->request->post("faversion");
  247. $extend = [
  248. 'uid' => $uid,
  249. 'token' => $token,
  250. 'version' => $version,
  251. 'faversion' => $faversion
  252. ];
  253. //调用更新的方法
  254. $info = Service::upgrade($name, $extend);
  255. Cache::rm('__menu__');
  256. } catch (AddonException $e) {
  257. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  258. } catch (Exception $e) {
  259. $this->error(__($e->getMessage()));
  260. }
  261. $this->success(__('Operate successful'), '', ['addon' => $info]);
  262. }
  263. /**
  264. * 已装插件
  265. */
  266. public function downloaded()
  267. {
  268. $offset = (int)$this->request->get("offset");
  269. $limit = (int)$this->request->get("limit");
  270. $filter = $this->request->get("filter");
  271. $search = $this->request->get("search");
  272. $search = htmlspecialchars(strip_tags($search));
  273. $onlineaddons = Cache::get("onlineaddons");
  274. if (!is_array($onlineaddons) && config('fastadmin.api_url')) {
  275. $onlineaddons = [];
  276. $result = Http::sendRequest(config('fastadmin.api_url') . '/addon/index', [], 'GET', [
  277. CURLOPT_HTTPHEADER => ['Accept-Encoding:gzip'],
  278. CURLOPT_ENCODING => "gzip"
  279. ]);
  280. if ($result['ret']) {
  281. $json = (array)json_decode($result['msg'], true);
  282. $rows = isset($json['rows']) ? $json['rows'] : [];
  283. foreach ($rows as $index => $row) {
  284. $onlineaddons[$row['name']] = $row;
  285. }
  286. }
  287. Cache::set("onlineaddons", $onlineaddons, 600);
  288. }
  289. $filter = (array)json_decode($filter, true);
  290. $addons = get_addon_list();
  291. $list = [];
  292. foreach ($addons as $k => $v) {
  293. if ($search && stripos($v['name'], $search) === false && stripos($v['title'], $search) === false && stripos($v['intro'], $search) === false) {
  294. continue;
  295. }
  296. if (isset($onlineaddons[$v['name']])) {
  297. $v = array_merge($v, $onlineaddons[$v['name']]);
  298. } else {
  299. $v['category_id'] = 0;
  300. $v['flag'] = '';
  301. $v['banner'] = '';
  302. $v['image'] = '';
  303. $v['donateimage'] = '';
  304. $v['demourl'] = '';
  305. $v['price'] = __('None');
  306. $v['screenshots'] = [];
  307. $v['releaselist'] = [];
  308. }
  309. $v['url'] = addon_url($v['name']);
  310. $v['url'] = str_replace($this->request->server('SCRIPT_NAME'), '', $v['url']);
  311. $v['createtime'] = filemtime(ADDON_PATH . $v['name']);
  312. if ($filter && isset($filter['category_id']) && is_numeric($filter['category_id']) && $filter['category_id'] != $v['category_id']) {
  313. continue;
  314. }
  315. $list[] = $v;
  316. }
  317. $total = count($list);
  318. if ($limit) {
  319. $list = array_slice($list, $offset, $limit);
  320. }
  321. $result = array("total" => $total, "rows" => $list);
  322. $callback = $this->request->get('callback') ? "jsonp" : "json";
  323. return $callback($result);
  324. }
  325. /**
  326. * 获取插件相关表
  327. */
  328. public function get_table_list()
  329. {
  330. $name = $this->request->post("name");
  331. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  332. $this->error(__('Addon name incorrect'));
  333. }
  334. $tables = get_addon_tables($name);
  335. $prefix = Config::get('database.prefix');
  336. foreach ($tables as $index => $table) {
  337. //忽略非插件标识的表名
  338. if (!preg_match("/^{$prefix}{$name}/", $table)) {
  339. unset($tables[$index]);
  340. }
  341. }
  342. $tables = array_values($tables);
  343. $this->success('', null, ['tables' => $tables]);
  344. }
  345. }