Addon.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 插件管理
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use app\admin\model\Adminlog;
  16. use app\admin\model\general\Lang as LangModel;
  17. use app\common\controller\Backend;
  18. use think\addons\AddonException;
  19. use think\addons\Service;
  20. use think\Exception;
  21. use think\facade\Cache;
  22. use think\facade\Config;
  23. use think\facade\Db;
  24. use think\facade\Env;
  25. class Addon extends Backend
  26. {
  27. protected function getCmsConfigLangFields()
  28. {
  29. return ['site_name', 'site_title', 'site_keyword', 'site_description', 'site_logo_light', 'site_logo_dark'];
  30. }
  31. protected function getCmsConfigData($lang)
  32. {
  33. return Db::name('cms_config_data')->where('lang', $lang)->find();
  34. }
  35. protected function saveCmsConfigData($lang, array $params)
  36. {
  37. $data = ['lang' => $lang, 'update_time' => time()];
  38. foreach ($this->getCmsConfigLangFields() as $field) {
  39. $data[$field] = $params[$field] ?? '';
  40. }
  41. $exists = Db::name('cms_config_data')->where('lang', $lang)->find();
  42. if ($exists) {
  43. Db::name('cms_config_data')->where('lang', $lang)->update($data);
  44. } else {
  45. $data['create_time'] = time();
  46. Db::name('cms_config_data')->insert($data);
  47. }
  48. }
  49. //初始化
  50. protected function initialize()
  51. {
  52. parent::initialize();
  53. if (!$this->auth->isAdministrator() && in_array($this->request->action(), ['install', 'uninstall', 'local', 'upgrade', 'testdata'])) {
  54. $this->error('非超级管理员禁止操作!');
  55. }
  56. }
  57. //显示插件列表
  58. public function index()
  59. {
  60. $type = $this->request->param("type", 'online');
  61. $limit = $this->request->param("limit/d");
  62. $page = $this->request->param("page/d", 1);
  63. $search = $this->request->param("search", '', 'strip_tags,htmlspecialchars');
  64. if ($this->request->isAjax()) {
  65. if ($type == 'local') {
  66. $addons = get_addon_list();
  67. if ($search) {
  68. $addons = array_filter($addons, function ($v) use ($search) {
  69. return stripos($v['name'], $search) !== false ||
  70. stripos($v['title'], $search) !== false ||
  71. stripos($v['description'], $search) !== false;
  72. });
  73. }
  74. $list = [];
  75. foreach ($addons as $k => $v) {
  76. $config = get_addon_config($v['name']);
  77. $v['config'] = $config ? 1 : 0;
  78. $addons[$k]['addon'] = $v;
  79. }
  80. $count = count($addons);
  81. if ($limit) {
  82. $addons = array_slice($addons, ($page - 1) * $limit, $limit);
  83. }
  84. $result = ["code" => 0, "data" => $addons, 'count' => $count];
  85. } else {
  86. //在线插件
  87. $list = $this->getAddonList($search, $page, $limit);
  88. $onlineaddons = $list['list'] ?? [];
  89. $category = $list['category'] ?? [];
  90. $count = $list['count'] ?? -1;
  91. //本地插件
  92. $addons = get_addon_list();
  93. foreach ($addons as &$v) {
  94. $config = get_addon_config($v['name']);
  95. $v['config'] = $config ? 1 : 0;
  96. }
  97. foreach ($onlineaddons as &$item) {
  98. $item['addon'] = $addons[$item['name']] ?? '';
  99. }
  100. $result = ["code" => 0, "data" => $onlineaddons, "category" => $category, 'count' => $count];
  101. }
  102. return json($result);
  103. }
  104. $this->assign(['type' => $type]);
  105. $this->assignconfig('addon', ['type' => $type, 'api_url' => config::get('yzn.api_url')]);
  106. return $this->fetch();
  107. }
  108. /**
  109. * 设置插件页面
  110. */
  111. public function config($name = null)
  112. {
  113. $name = $name ?: $this->request->get("name");
  114. if (!$name) {
  115. $this->error('参数不得为空!');
  116. }
  117. if (!preg_match('/^[a-zA-Z0-9]+$/', $name)) {
  118. $this->error('插件名称不正确!');
  119. }
  120. if (!is_dir(ADDON_PATH . $name)) {
  121. $this->error('目录不存在!');
  122. }
  123. $info = get_addon_info($name);
  124. $config = get_addon_fullconfig($name);
  125. if (!$info) {
  126. $this->error('配置不存在!');
  127. }
  128. $defaultLang = LangModel::getDefaultMark();
  129. $currentLang = $this->request->param('lang/s', $defaultLang);
  130. if ($name === 'cms' && $currentLang !== $defaultLang) {
  131. $translate = $this->getCmsConfigData($currentLang) ?: [];
  132. foreach ($config as &$item) {
  133. if (in_array($item['name'], $this->getCmsConfigLangFields(), true) && isset($translate[$item['name']])) {
  134. $item['value'] = $translate[$item['name']];
  135. }
  136. }
  137. unset($item);
  138. }
  139. if ($this->request->isPost()) {
  140. $params = $this->request->post("row/a", [], 'trim');
  141. if ($params) {
  142. try {
  143. if ($name === 'cms' && $currentLang !== $defaultLang) {
  144. $this->saveCmsConfigData($currentLang, $params);
  145. } else {
  146. foreach ($config as $k => &$v) {
  147. if (isset($params[$v['name']])) {
  148. if ($v['type'] == 'array') {
  149. $params[$v['name']] = is_array($params[$v['name']]) ? $params[$v['name']] : (array) json_decode($params[$v['name']], true);
  150. $value = $params[$v['name']];
  151. } else {
  152. $value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
  153. }
  154. $v['value'] = $value;
  155. } elseif ($v['type'] == 'checkbox' && !isset($params[$v['name']])) {
  156. //单独处理多选框为空不传参
  157. $v['value'] = '';
  158. }
  159. }
  160. $addon = get_addon_instance($name);
  161. //插件自定义配置实现逻辑
  162. if (method_exists($addon, 'config')) {
  163. $addon->config($config);
  164. } else {
  165. //更新配置文件
  166. set_addon_fullconfig($name, $config);
  167. Service::refresh();
  168. }
  169. }
  170. } catch (\Exception $e) {
  171. $this->error($e->getMessage());
  172. }
  173. }
  174. $this->success('插件配置成功!');
  175. }
  176. $tips = [];
  177. $groupList = [];
  178. $ungroupList = [];
  179. foreach ($config as $index => &$item) {
  180. //如果有设置分组
  181. if (isset($item['group']) && $item['group']) {
  182. if (!in_array($item['group'], $groupList)) {
  183. $groupList["custom" . (count($groupList) + 1)] = $item['group'];
  184. }
  185. } elseif ($item['name'] != '__tips__') {
  186. $ungroupList[] = $item['name'];
  187. }
  188. $item['extend'] = $item['extend'] ?? '';
  189. if ($item['name'] == '__tips__') {
  190. $tips = $item;
  191. unset($config[$index]);
  192. }
  193. }
  194. if ($ungroupList) {
  195. $groupList['other'] = '其它';
  196. }
  197. $this->assign("groupList", $groupList);
  198. $this->assign('data', ['info' => $info, 'config' => $config, 'tips' => $tips]);
  199. $this->assign('configLangs', $name === 'cms' ? LangModel::getEnabledList() : []);
  200. $this->assign('currentConfigLang', $currentLang);
  201. $this->assign('defaultConfigLang', $defaultLang);
  202. $configFile = ADDON_PATH . $name . DS . 'config.html';
  203. $viewFile = is_file($configFile) ? $configFile : '';
  204. return $this->fetch($viewFile);
  205. }
  206. /**
  207. * 禁用启用.
  208. */
  209. public function state()
  210. {
  211. $name = $this->request->param('name');
  212. $action = $this->request->param('action');
  213. $force = $this->request->post("force/d");
  214. if (!$name) {
  215. $this->error('参数不得为空!');
  216. }
  217. if (!preg_match('/^[a-zA-Z0-9]+$/', $name)) {
  218. $this->error('插件名称不正确');
  219. }
  220. try {
  221. $action = $action == 'enable' ? $action : 'disable';
  222. //调用启用、禁用的方法
  223. Service::$action($name, $force);
  224. Cache::delete('__menu__');
  225. } catch (AddonException $e) {
  226. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  227. } catch (\Exception $e) {
  228. $this->error($e->getMessage());
  229. }
  230. $this->success('操作成功');
  231. }
  232. /**
  233. * 安装插件
  234. */
  235. public function install()
  236. {
  237. Adminlog::setTitle('插件安装');
  238. $name = $this->request->param('name');
  239. $force = $this->request->param("force/d");
  240. if (empty($name)) {
  241. $this->error('请选择需要安装的插件!');
  242. }
  243. if (!preg_match('/^[a-zA-Z0-9]+$/', $name)) {
  244. $this->error('插件标识错误!');
  245. }
  246. $info = [];
  247. try {
  248. $uid = $this->request->post("uid");
  249. $token = $this->request->post("token");
  250. $version = $this->request->post("version");
  251. $extend = [
  252. 'uid' => $uid,
  253. 'token' => $token,
  254. 'version' => $version,
  255. 'yzncms_version' => Config::get('version.yzncms_version'),
  256. ];
  257. $info = Service::install($name, $force, $extend);
  258. Cache::delete('__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('插件安装成功!清除浏览器缓存和框架缓存后生效!', '', ['addon' => $info]);
  265. }
  266. /**
  267. * 卸载插件
  268. */
  269. public function uninstall()
  270. {
  271. Adminlog::setTitle('插件卸载');
  272. $name = $this->request->param('name');
  273. $force = $this->request->param("force/d");
  274. $droptables = $this->request->param("droptables/d");
  275. if (empty($name)) {
  276. $this->error('请选择需要安装的插件!');
  277. }
  278. if (!preg_match('/^[a-zA-Z0-9]+$/', $name)) {
  279. $this->error('插件标识错误!');
  280. }
  281. //只有开启调试且为超级管理员才允许删除相关数据库
  282. $tables = [];
  283. if ($droptables && Env::get('APP_DEBUG') && $this->auth->isAdministrator()) {
  284. $tables = get_addon_tables($name);
  285. }
  286. try {
  287. Service::uninstall($name, $force);
  288. if ($tables) {
  289. //$prefix = Config::get('database.prefix');
  290. //删除插件关联表
  291. foreach ($tables as $table) {
  292. Db::execute("DROP TABLE IF EXISTS `{$table}`");
  293. }
  294. }
  295. } catch (AddonException $e) {
  296. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  297. } catch (\Exception $e) {
  298. $this->error($e->getMessage());
  299. }
  300. $this->success('插件卸载成功!清除浏览器缓存和框架缓存后生效!');
  301. }
  302. /**
  303. * 本地上传
  304. */
  305. public function local()
  306. {
  307. Adminlog::setTitle('插件上传安装');
  308. if (!Env::get('APP_DEBUG')) {
  309. $this->error('本地上传安装需要开启调试模式!');
  310. }
  311. $file = $this->request->file('file');
  312. try {
  313. $force = $this->request->post("force");
  314. $extend = [];
  315. $info = Service::local($file, $extend, $force);
  316. } catch (AddonException $e) {
  317. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  318. } catch (\Exception $e) {
  319. $this->error($e->getMessage());
  320. }
  321. $this->success('插件安装成功!清除浏览器缓存和框架缓存后生效!', '', ['addon' => $info]);
  322. }
  323. /**
  324. * 更新插件
  325. */
  326. public function upgrade()
  327. {
  328. $name = $this->request->param('name');
  329. $addonTmpDir = app()->getRuntimePath() . 'addons' . DS;
  330. if (empty($name)) {
  331. $this->error('请选择需要安装的插件!');
  332. }
  333. if (!preg_match('/^[a-zA-Z0-9]+$/', $name)) {
  334. $this->error('插件标识错误!');
  335. }
  336. if (!is_dir($addonTmpDir)) {
  337. @mkdir($addonTmpDir, 0755, true);
  338. }
  339. $info = [];
  340. try {
  341. $info = get_addon_info($name);
  342. $uid = $this->request->post("uid");
  343. $token = $this->request->post("token");
  344. $version = $this->request->post("version");
  345. $extend = [
  346. 'uid' => $uid,
  347. 'token' => $token,
  348. 'version' => $version,
  349. 'oldversion' => $info['version'] ?? '',
  350. 'yzncms_version' => Config::get('version.yzncms_version'),
  351. ];
  352. //调用更新的方法
  353. $info = Service::upgrade($name, $extend);
  354. Cache::delete('__menu__');
  355. } catch (AddonException $e) {
  356. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  357. } catch (Exception $e) {
  358. $this->error($e->getMessage());
  359. }
  360. $this->success('升级成功', '', ['addon' => $info]);
  361. }
  362. /**
  363. * 测试数据
  364. */
  365. public function testdata()
  366. {
  367. Adminlog::setTitle('插件导入测试数据');
  368. $name = $this->request->post("name");
  369. if (empty($name)) {
  370. $this->error('请选择需要安装的插件!');
  371. }
  372. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  373. $this->error('插件标识错误!');
  374. }
  375. try {
  376. Service::runSQL($name, 'testdata');
  377. } catch (AddonException $e) {
  378. $this->result($e->getData(), $e->getCode(), $e->getMessage());
  379. } catch (Exception $e) {
  380. $this->error($e->getMessage(), $e->getCode());
  381. }
  382. $this->success('导入成功');
  383. }
  384. /**
  385. * 检测
  386. */
  387. public function isbuy()
  388. {
  389. $name = $this->request->post("name");
  390. $uid = $this->request->post("uid");
  391. $token = $this->request->post("token");
  392. $version = $this->request->post("version");
  393. $extend = [
  394. 'uid' => $uid,
  395. 'token' => $token,
  396. 'version' => $version,
  397. 'yzncms_version' => Config::get('version.yzncms_version'),
  398. ];
  399. try {
  400. $result = Service::isBuy($name, $extend);
  401. } catch (Exception $e) {
  402. $this->error($e->getMessage());
  403. }
  404. return json($result);
  405. }
  406. /**
  407. * 获取插件相关表
  408. */
  409. public function get_table_list()
  410. {
  411. $name = $this->request->post("name");
  412. if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
  413. $this->error('插件标识错误!');
  414. }
  415. $tables = get_addon_tables($name);
  416. //$prefix = Config::get('database.prefix');
  417. $tables = array_values($tables);
  418. $this->success('', null, ['tables' => $tables]);
  419. }
  420. protected function getAddonList($search, $page, $limit)
  421. {
  422. $params = [
  423. 'uid' => $this->request->param('uid/d'),
  424. 'token' => $this->request->param('token'),
  425. 'category_id' => $this->request->param('category_id/d'),
  426. 'version' => Config::get('version.yzncms_version'),
  427. 'page' => $page,
  428. 'limit' => $limit,
  429. 'search' => $search,
  430. ];
  431. $json = [];
  432. try {
  433. $json = Service::addons($params);
  434. } catch (\Exception $e) {
  435. }
  436. return $json;
  437. }
  438. }