Addon.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. //删除插件关联表
  157. foreach ($tables as $index => $table) {
  158. Db::execute("DROP TABLE IF EXISTS `{$table}`");
  159. }
  160. }
  161. $this->success(__('Uninstall successful'));
  162. } catch (AddonException $e) {
  163. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  164. } catch (Exception $e) {
  165. $this->error(__($e->getMessage()));
  166. }
  167. }
  168. /**
  169. * 禁用启用
  170. */
  171. public function state()
  172. {
  173. $name = $this->request->post("name");
  174. $action = $this->request->post("action");
  175. $force = (int)$this->request->post("force");
  176. if (!$name) {
  177. $this->error(__('Parameter %s can not be empty', 'name'));
  178. }
  179. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  180. $this->error(__('Addon name incorrect'));
  181. }
  182. try {
  183. $action = $action == 'enable' ? $action : 'disable';
  184. //调用启用、禁用的方法
  185. Service::$action($name, $force);
  186. Cache::rm('__menu__');
  187. $this->success(__('Operate successful'));
  188. } catch (AddonException $e) {
  189. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  190. } catch (Exception $e) {
  191. $this->error(__($e->getMessage()));
  192. }
  193. }
  194. /**
  195. * 本地上传
  196. */
  197. public function local()
  198. {
  199. Config::set('default_return_type', 'json');
  200. $file = $this->request->file('file');
  201. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  202. if (!is_dir($addonTmpDir)) {
  203. @mkdir($addonTmpDir, 0755, true);
  204. }
  205. $info = $file->rule('uniqid')->validate(['size' => 10240000, 'ext' => 'zip'])->move($addonTmpDir);
  206. if ($info) {
  207. $tmpName = substr($info->getFilename(), 0, stripos($info->getFilename(), '.'));
  208. $tmpAddonDir = ADDON_PATH . $tmpName . DS;
  209. $tmpFile = $addonTmpDir . $info->getSaveName();
  210. try {
  211. Service::unzip($tmpName);
  212. unset($info);
  213. @unlink($tmpFile);
  214. $infoFile = $tmpAddonDir . 'info.ini';
  215. if (!is_file($infoFile)) {
  216. throw new Exception(__('Addon info file was not found'));
  217. }
  218. $config = Config::parse($infoFile, '', $tmpName);
  219. $name = isset($config['name']) ? $config['name'] : '';
  220. if (!$name) {
  221. throw new Exception(__('Addon info file data incorrect'));
  222. }
  223. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  224. throw new Exception(__('Addon name incorrect'));
  225. }
  226. $newAddonDir = ADDON_PATH . $name . DS;
  227. if (is_dir($newAddonDir)) {
  228. throw new Exception(__('Addon already exists'));
  229. }
  230. //重命名插件文件夹
  231. rename($tmpAddonDir, $newAddonDir);
  232. try {
  233. //默认禁用该插件
  234. $info = get_addon_info($name);
  235. if ($info['state']) {
  236. $info['state'] = 0;
  237. set_addon_info($name, $info);
  238. }
  239. //执行插件的安装方法
  240. $class = get_addon_class($name);
  241. if (class_exists($class)) {
  242. $addon = new $class();
  243. $addon->install();
  244. }
  245. //导入SQL
  246. Service::importsql($name);
  247. $info['config'] = get_addon_config($name) ? 1 : 0;
  248. $this->success(__('Offline installed tips'), null, ['addon' => $info]);
  249. } catch (Exception $e) {
  250. @rmdirs($newAddonDir);
  251. throw new Exception(__($e->getMessage()));
  252. }
  253. } catch (Exception $e) {
  254. unset($info);
  255. @unlink($tmpFile);
  256. @rmdirs($tmpAddonDir);
  257. $this->error(__($e->getMessage()));
  258. }
  259. } else {
  260. // 上传失败获取错误信息
  261. $this->error(__($file->getError()));
  262. }
  263. }
  264. /**
  265. * 更新插件
  266. */
  267. public function upgrade()
  268. {
  269. $name = $this->request->post("name");
  270. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  271. if (!$name) {
  272. $this->error(__('Parameter %s can not be empty', 'name'));
  273. }
  274. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  275. $this->error(__('Addon name incorrect'));
  276. }
  277. if (!is_dir($addonTmpDir)) {
  278. @mkdir($addonTmpDir, 0755, true);
  279. }
  280. try {
  281. $uid = $this->request->post("uid");
  282. $token = $this->request->post("token");
  283. $version = $this->request->post("version");
  284. $faversion = $this->request->post("faversion");
  285. $extend = [
  286. 'uid' => $uid,
  287. 'token' => $token,
  288. 'version' => $version,
  289. 'faversion' => $faversion
  290. ];
  291. //调用更新的方法
  292. Service::upgrade($name, $extend);
  293. Cache::rm('__menu__');
  294. $this->success(__('Operate successful'));
  295. } catch (AddonException $e) {
  296. $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
  297. } catch (Exception $e) {
  298. $this->error(__($e->getMessage()));
  299. }
  300. }
  301. /**
  302. * 已装插件
  303. */
  304. public function downloaded()
  305. {
  306. $offset = (int)$this->request->get("offset");
  307. $limit = (int)$this->request->get("limit");
  308. $filter = $this->request->get("filter");
  309. $search = $this->request->get("search");
  310. $search = htmlspecialchars(strip_tags($search));
  311. $onlineaddons = Cache::get("onlineaddons");
  312. if (!is_array($onlineaddons) && config('fastadmin.api_url')) {
  313. $onlineaddons = [];
  314. $result = Http::sendRequest(config('fastadmin.api_url') . '/addon/index', [], 'GET', [
  315. CURLOPT_HTTPHEADER => ['Accept-Encoding:gzip'],
  316. CURLOPT_ENCODING => "gzip"
  317. ]);
  318. if ($result['ret']) {
  319. $json = (array)json_decode($result['msg'], true);
  320. $rows = isset($json['rows']) ? $json['rows'] : [];
  321. foreach ($rows as $index => $row) {
  322. $onlineaddons[$row['name']] = $row;
  323. }
  324. }
  325. Cache::set("onlineaddons", $onlineaddons, 600);
  326. }
  327. $filter = (array)json_decode($filter, true);
  328. $addons = get_addon_list();
  329. $list = [];
  330. foreach ($addons as $k => $v) {
  331. if ($search && stripos($v['name'], $search) === false && stripos($v['intro'], $search) === false) {
  332. continue;
  333. }
  334. if (isset($onlineaddons[$v['name']])) {
  335. $v = array_merge($v, $onlineaddons[$v['name']]);
  336. } else {
  337. $v['category_id'] = 0;
  338. $v['flag'] = '';
  339. $v['banner'] = '';
  340. $v['image'] = '';
  341. $v['donateimage'] = '';
  342. $v['demourl'] = '';
  343. $v['price'] = __('None');
  344. $v['screenshots'] = [];
  345. $v['releaselist'] = [];
  346. }
  347. $v['url'] = addon_url($v['name']);
  348. $v['url'] = str_replace($this->request->server('SCRIPT_NAME'), '', $v['url']);
  349. $v['createtime'] = filemtime(ADDON_PATH . $v['name']);
  350. if ($filter && isset($filter['category_id']) && is_numeric($filter['category_id']) && $filter['category_id'] != $v['category_id']) {
  351. continue;
  352. }
  353. $list[] = $v;
  354. }
  355. $total = count($list);
  356. if ($limit) {
  357. $list = array_slice($list, $offset, $limit);
  358. }
  359. $result = array("total" => $total, "rows" => $list);
  360. $callback = $this->request->get('callback') ? "jsonp" : "json";
  361. return $callback($result);
  362. }
  363. /**
  364. * 获取插件相关表
  365. */
  366. public function get_table_list()
  367. {
  368. $name = $this->request->post("name");
  369. $tables = get_addon_tables($name);
  370. $this->success('', null, ['tables' => $tables]);
  371. }
  372. }