Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Addons.php 14KB

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