Addon.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 可在线安装、卸载、禁用、启用插件,同时支持添加本地插件。FastAdmin已上线插件商店 ,你可以发布你的免费或付费插件:<a href="https://www.fastadmin.net/store.html" target="_blank">https://www.fastadmin.net/store.html</a>
  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. try {
  113. $uid = $this->request->post("uid");
  114. $token = $this->request->post("token");
  115. $version = $this->request->post("version");
  116. $faversion = $this->request->post("faversion");
  117. $extend = [
  118. 'uid' => $uid,
  119. 'token' => $token,
  120. 'version' => $version,
  121. 'faversion' => $faversion
  122. ];
  123. Service::install($name, $force, $extend);
  124. $info = get_addon_info($name);
  125. $info['config'] = get_addon_config($name) ? 1 : 0;
  126. $info['state'] = 1;
  127. $this->success(__('Install successful'), null, ['addon' => $info]);
  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. }
  134. /**
  135. * 卸载
  136. */
  137. public function uninstall()
  138. {
  139. $name = $this->request->post("name");
  140. $force = (int)$this->request->post("force");
  141. $droptables = (int)$this->request->post("droptables");
  142. if (!$name) {
  143. $this->error(__('Parameter %s can not be empty', 'name'));
  144. }
  145. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  146. $this->error(__('Addon name incorrect'));
  147. }
  148. //只有开启调试且为超级管理员才允许删除相关数据库
  149. $tables = [];
  150. if ($droptables && Config::get("app_debug") && $this->auth->isSuperAdmin()) {
  151. $tables = get_addon_tables($name);
  152. }
  153. try {
  154. Service::uninstall($name, $force);
  155. if ($tables) {
  156. $prefix = Config::get('database.prefix');
  157. //删除插件关联表
  158. foreach ($tables as $index => $table) {
  159. //忽略非插件标识的表名
  160. if (!preg_match("/^{$prefix}{$name}/", $table)) {
  161. continue;
  162. }
  163. Db::execute("DROP TABLE IF EXISTS `{$table}`");
  164. }
  165. }
  166. $this->success(__('Uninstall successful'));
  167. } catch (AddonException $e) {
  168. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  169. } catch (Exception $e) {
  170. $this->error(__($e->getMessage()));
  171. }
  172. }
  173. /**
  174. * 禁用启用
  175. */
  176. public function state()
  177. {
  178. $name = $this->request->post("name");
  179. $action = $this->request->post("action");
  180. $force = (int)$this->request->post("force");
  181. if (!$name) {
  182. $this->error(__('Parameter %s can not be empty', 'name'));
  183. }
  184. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  185. $this->error(__('Addon name incorrect'));
  186. }
  187. try {
  188. $action = $action == 'enable' ? $action : 'disable';
  189. //调用启用、禁用的方法
  190. Service::$action($name, $force);
  191. Cache::rm('__menu__');
  192. $this->success(__('Operate successful'));
  193. } catch (AddonException $e) {
  194. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  195. } catch (Exception $e) {
  196. $this->error(__($e->getMessage()));
  197. }
  198. }
  199. /**
  200. * 本地上传
  201. */
  202. public function local()
  203. {
  204. Config::set('default_return_type', 'json');
  205. $file = $this->request->file('file');
  206. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  207. if (!is_dir($addonTmpDir)) {
  208. @mkdir($addonTmpDir, 0755, true);
  209. }
  210. $info = $file->rule('uniqid')->validate(['size' => 10240000, 'ext' => 'zip'])->move($addonTmpDir);
  211. if ($info) {
  212. $tmpName = substr($info->getFilename(), 0, stripos($info->getFilename(), '.'));
  213. $tmpAddonDir = ADDON_PATH . $tmpName . DS;
  214. $tmpFile = $addonTmpDir . $info->getSaveName();
  215. try {
  216. Service::unzip($tmpName);
  217. unset($info);
  218. @unlink($tmpFile);
  219. $infoFile = $tmpAddonDir . 'info.ini';
  220. if (!is_file($infoFile)) {
  221. throw new Exception(__('Addon info file was not found'));
  222. }
  223. $config = Config::parse($infoFile, '', $tmpName);
  224. $name = isset($config['name']) ? $config['name'] : '';
  225. if (!$name) {
  226. throw new Exception(__('Addon info file data incorrect'));
  227. }
  228. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  229. throw new Exception(__('Addon name incorrect'));
  230. }
  231. $newAddonDir = ADDON_PATH . $name . DS;
  232. if (is_dir($newAddonDir)) {
  233. throw new Exception(__('Addon already exists'));
  234. }
  235. //重命名插件文件夹
  236. rename($tmpAddonDir, $newAddonDir);
  237. try {
  238. //默认禁用该插件
  239. $info = get_addon_info($name);
  240. if ($info['state']) {
  241. $info['state'] = 0;
  242. set_addon_info($name, $info);
  243. }
  244. //执行插件的安装方法
  245. $class = get_addon_class($name);
  246. if (class_exists($class)) {
  247. $addon = new $class();
  248. $addon->install();
  249. }
  250. //导入SQL
  251. Service::importsql($name);
  252. $info['config'] = get_addon_config($name) ? 1 : 0;
  253. $this->success(__('Offline installed tips'), null, ['addon' => $info]);
  254. } catch (Exception $e) {
  255. @rmdirs($newAddonDir);
  256. throw new Exception(__($e->getMessage()));
  257. }
  258. } catch (Exception $e) {
  259. unset($info);
  260. @unlink($tmpFile);
  261. @rmdirs($tmpAddonDir);
  262. $this->error(__($e->getMessage()));
  263. }
  264. } else {
  265. // 上传失败获取错误信息
  266. $this->error(__($file->getError()));
  267. }
  268. }
  269. /**
  270. * 更新插件
  271. */
  272. public function upgrade()
  273. {
  274. $name = $this->request->post("name");
  275. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  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. if (!is_dir($addonTmpDir)) {
  283. @mkdir($addonTmpDir, 0755, true);
  284. }
  285. try {
  286. $uid = $this->request->post("uid");
  287. $token = $this->request->post("token");
  288. $version = $this->request->post("version");
  289. $faversion = $this->request->post("faversion");
  290. $extend = [
  291. 'uid' => $uid,
  292. 'token' => $token,
  293. 'version' => $version,
  294. 'faversion' => $faversion
  295. ];
  296. //调用更新的方法
  297. Service::upgrade($name, $extend);
  298. Cache::rm('__menu__');
  299. $this->success(__('Operate successful'));
  300. } catch (AddonException $e) {
  301. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  302. } catch (Exception $e) {
  303. $this->error(__($e->getMessage()));
  304. }
  305. }
  306. /**
  307. * 已装插件
  308. */
  309. public function downloaded()
  310. {
  311. $offset = (int)$this->request->get("offset");
  312. $limit = (int)$this->request->get("limit");
  313. $filter = $this->request->get("filter");
  314. $search = $this->request->get("search");
  315. $search = htmlspecialchars(strip_tags($search));
  316. $onlineaddons = Cache::get("onlineaddons");
  317. if (!is_array($onlineaddons) && config('fastadmin.api_url')) {
  318. $onlineaddons = [];
  319. $result = Http::sendRequest(config('fastadmin.api_url') . '/addon/index', [], 'GET', [
  320. CURLOPT_HTTPHEADER => ['Accept-Encoding:gzip'],
  321. CURLOPT_ENCODING => "gzip"
  322. ]);
  323. if ($result['ret']) {
  324. $json = (array)json_decode($result['msg'], true);
  325. $rows = isset($json['rows']) ? $json['rows'] : [];
  326. foreach ($rows as $index => $row) {
  327. $onlineaddons[$row['name']] = $row;
  328. }
  329. }
  330. Cache::set("onlineaddons", $onlineaddons, 600);
  331. }
  332. $filter = (array)json_decode($filter, true);
  333. $addons = get_addon_list();
  334. $list = [];
  335. foreach ($addons as $k => $v) {
  336. if ($search && stripos($v['name'], $search) === false && stripos($v['intro'], $search) === false) {
  337. continue;
  338. }
  339. if (isset($onlineaddons[$v['name']])) {
  340. $v = array_merge($v, $onlineaddons[$v['name']]);
  341. } else {
  342. $v['category_id'] = 0;
  343. $v['flag'] = '';
  344. $v['banner'] = '';
  345. $v['image'] = '';
  346. $v['donateimage'] = '';
  347. $v['demourl'] = '';
  348. $v['price'] = __('None');
  349. $v['screenshots'] = [];
  350. $v['releaselist'] = [];
  351. }
  352. $v['url'] = addon_url($v['name']);
  353. $v['url'] = str_replace($this->request->server('SCRIPT_NAME'), '', $v['url']);
  354. $v['createtime'] = filemtime(ADDON_PATH . $v['name']);
  355. if ($filter && isset($filter['category_id']) && is_numeric($filter['category_id']) && $filter['category_id'] != $v['category_id']) {
  356. continue;
  357. }
  358. $list[] = $v;
  359. }
  360. $total = count($list);
  361. if ($limit) {
  362. $list = array_slice($list, $offset, $limit);
  363. }
  364. $result = array("total" => $total, "rows" => $list);
  365. $callback = $this->request->get('callback') ? "jsonp" : "json";
  366. return $callback($result);
  367. }
  368. /**
  369. * 获取插件相关表
  370. */
  371. public function get_table_list()
  372. {
  373. $name = $this->request->post("name");
  374. $tables = get_addon_tables($name);
  375. $prefix = Config::get('database.prefix');
  376. foreach ($tables as $index => $table) {
  377. //忽略非插件标识的表名
  378. if (!preg_match("/^{$prefix}{$name}/", $table)) {
  379. unset($tables[$index]);
  380. }
  381. }
  382. $tables = array_values($tables);
  383. $this->success('', null, ['tables' => $tables]);
  384. }
  385. }