PluginController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <zxxjjforever@163.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\admin\logic\PluginLogic;
  13. use cmf\controller\AdminBaseController;
  14. use app\admin\model\PluginModel;
  15. use app\admin\model\HookPluginModel;
  16. use mindplay\annotations\Annotations;
  17. use think\facade\Cache;
  18. use think\Validate;
  19. /**
  20. * Class PluginController
  21. * @package app\admin\controller
  22. * @adminMenuRoot(
  23. * 'name' =>'应用中心',
  24. * 'action' =>'default',
  25. * 'parent' =>'',
  26. * 'display'=> true,
  27. * 'order' => 20,
  28. * 'icon' =>'cloud',
  29. * 'remark' =>'应用中心'
  30. * )
  31. */
  32. class PluginController extends AdminBaseController
  33. {
  34. protected $pluginModel;
  35. /**
  36. * 插件管理
  37. * @adminMenu(
  38. * 'name' => '插件管理',
  39. * 'parent' => 'admin/Plugin/default',
  40. * 'display'=> true,
  41. * 'hasView'=> true,
  42. * 'order' => 10000,
  43. * 'icon' => '',
  44. * 'remark' => '插件管理',
  45. * 'param' => ''
  46. * )
  47. */
  48. public function index()
  49. {
  50. $pluginModel = new PluginModel();
  51. $plugins = $pluginModel->getList();
  52. $this->assign("plugins", $plugins);
  53. return $this->fetch();
  54. }
  55. /**
  56. * 插件启用/禁用
  57. * @adminMenu(
  58. * 'name' => '插件启用禁用',
  59. * 'parent' => 'index',
  60. * 'display'=> false,
  61. * 'hasView'=> false,
  62. * 'order' => 10000,
  63. * 'icon' => '',
  64. * 'remark' => '插件启用禁用',
  65. * 'param' => ''
  66. * )
  67. */
  68. public function toggle()
  69. {
  70. if ($this->request->isPost()) {
  71. $id = $this->request->param('id', 0, 'intval');
  72. $pluginModel = PluginModel::find($id);
  73. if (empty($pluginModel)) {
  74. $this->error('插件不存在!');
  75. }
  76. $status = 1;
  77. $successMessage = "启用成功!";
  78. if ($this->request->param('disable')) {
  79. $status = 0;
  80. $successMessage = "禁用成功!";
  81. }
  82. $pluginModel->startTrans();
  83. try {
  84. $pluginModel->save(['status' => $status]);
  85. $hookPluginModel = new HookPluginModel();
  86. $hookPluginModel->where(['plugin' => $pluginModel->name])->update(['status' => $status]);
  87. $pluginModel->commit();
  88. } catch (\Exception $e) {
  89. $pluginModel->rollback();
  90. $this->error('操作失败!');
  91. }
  92. Cache::clear('init_hook_plugins');
  93. $this->success($successMessage);
  94. }
  95. }
  96. /**
  97. * 插件设置
  98. * @adminMenu(
  99. * 'name' => '插件设置',
  100. * 'parent' => 'index',
  101. * 'display'=> false,
  102. * 'hasView'=> true,
  103. * 'order' => 10000,
  104. * 'icon' => '',
  105. * 'remark' => '插件设置',
  106. * 'param' => ''
  107. * )
  108. */
  109. public function setting()
  110. {
  111. $id = $this->request->param('id', 0, 'intval');
  112. $pluginModel = new PluginModel();
  113. $plugin = $pluginModel->find($id);
  114. if (empty($plugin)) {
  115. $this->error('插件未安装!');
  116. }
  117. $plugin = $plugin->toArray();
  118. $pluginClass = cmf_get_plugin_class($plugin['name']);
  119. if (!class_exists($pluginClass)) {
  120. $this->error('插件不存在!');
  121. }
  122. $pluginObj = new $pluginClass;
  123. //$plugin['plugin_path'] = $pluginObj->plugin_path;
  124. //$plugin['custom_config'] = $pluginObj->custom_config;
  125. $pluginConfigInDb = $plugin['config'];
  126. $plugin['config'] = include $pluginObj->getConfigFilePath();
  127. if ($pluginConfigInDb) {
  128. $pluginConfigInDb = json_decode($pluginConfigInDb, true);
  129. foreach ($plugin['config'] as $key => $value) {
  130. if ($value['type'] != 'group') {
  131. if (isset($pluginConfigInDb[$key])) {
  132. $plugin['config'][$key]['value'] = $pluginConfigInDb[$key];
  133. }
  134. } else {
  135. foreach ($value['options'] as $group => $options) {
  136. foreach ($options['options'] as $gkey => $value) {
  137. if (isset($pluginConfigInDb[$gkey])) {
  138. $plugin['config'][$key]['options'][$group]['options'][$gkey]['value'] = $pluginConfigInDb[$gkey];
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. $this->assign('data', $plugin);
  146. // if ($plugin['custom_config']) {
  147. // $this->assign('custom_config', $this->fetch($plugin['plugin_path'] . $plugin['custom_config']));
  148. // }
  149. $this->assign('id', $id);
  150. return $this->fetch();
  151. }
  152. /**
  153. * 插件设置提交
  154. * @adminMenu(
  155. * 'name' => '插件设置提交',
  156. * 'parent' => 'index',
  157. * 'display'=> false,
  158. * 'hasView'=> false,
  159. * 'order' => 10000,
  160. * 'icon' => '',
  161. * 'remark' => '插件设置提交',
  162. * 'param' => ''
  163. * )
  164. */
  165. public function settingPost()
  166. {
  167. if ($this->request->isPost()) {
  168. $id = $this->request->param('id', 0, 'intval');
  169. $pluginModel = new PluginModel();
  170. $plugin = $pluginModel->find($id)->toArray();
  171. if (!$plugin) {
  172. $this->error('插件未安装!');
  173. }
  174. $pluginClass = cmf_get_plugin_class($plugin['name']);
  175. if (!class_exists($pluginClass)) {
  176. $this->error('插件不存在!');
  177. }
  178. $pluginObj = new $pluginClass;
  179. //$plugin['plugin_path'] = $pluginObj->plugin_path;
  180. //$plugin['custom_config'] = $pluginObj->custom_config;
  181. $pluginConfigInDb = $plugin['config'];
  182. $plugin['config'] = include $pluginObj->getConfigFilePath();
  183. $rules = [];
  184. $messages = [];
  185. foreach ($plugin['config'] as $key => $value) {
  186. if ($value['type'] != 'group') {
  187. if (isset($value['rule'])) {
  188. $rules[$key] = $this->_parseRules($value['rule']);
  189. }
  190. if (isset($value['message'])) {
  191. foreach ($value['message'] as $rule => $msg) {
  192. $messages[$key . '.' . $rule] = $msg;
  193. }
  194. }
  195. } else {
  196. foreach ($value['options'] as $group => $options) {
  197. foreach ($options['options'] as $gkey => $value) {
  198. if (isset($value['rule'])) {
  199. $rules[$gkey] = $this->_parseRules($value['rule']);
  200. }
  201. if (isset($value['message'])) {
  202. foreach ($value['message'] as $rule => $msg) {
  203. $messages[$gkey . '.' . $rule] = $msg;
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. $config = $this->request->param('config/a');
  211. $validate = new Validate($rules, $messages);
  212. $result = $validate->check($config);
  213. if ($result !== true) {
  214. $this->error($validate->getError());
  215. }
  216. $pluginModel = PluginModel::where('id', $id)->find();
  217. $pluginModel->save(['config' => json_encode($config)]);
  218. $this->success('保存成功', '');
  219. }
  220. }
  221. /**
  222. * 解析插件配置验证规则
  223. * @param $rules
  224. * @return array
  225. */
  226. private function _parseRules($rules)
  227. {
  228. $newRules = [];
  229. $simpleRules = [
  230. 'require', 'number',
  231. 'integer', 'float', 'boolean', 'email',
  232. 'array', 'accepted', 'date', 'alpha',
  233. 'alphaNum', 'alphaDash', 'activeUrl',
  234. 'url', 'ip'];
  235. foreach ($rules as $key => $rule) {
  236. if (in_array($key, $simpleRules) && $rule) {
  237. array_push($newRules, $key);
  238. }
  239. }
  240. return $newRules;
  241. }
  242. /**
  243. * 插件安装
  244. * @adminMenu(
  245. * 'name' => '插件安装',
  246. * 'parent' => 'index',
  247. * 'display'=> false,
  248. * 'hasView'=> false,
  249. * 'order' => 10000,
  250. * 'icon' => '',
  251. * 'remark' => '插件安装',
  252. * 'param' => ''
  253. * )
  254. */
  255. public function install()
  256. {
  257. if ($this->request->isPost()) {
  258. $pluginName = $this->request->param('name', '', 'trim');
  259. $result = PluginLogic::install($pluginName);
  260. if ($result !== true) {
  261. $this->error($result);
  262. }
  263. $this->success('安装成功!');
  264. }
  265. }
  266. /**
  267. * 插件更新
  268. * @adminMenu(
  269. * 'name' => '插件更新',
  270. * 'parent' => 'index',
  271. * 'display'=> false,
  272. * 'hasView'=> false,
  273. * 'order' => 10000,
  274. * 'icon' => '',
  275. * 'remark' => '插件更新',
  276. * 'param' => ''
  277. * )
  278. */
  279. public function update()
  280. {
  281. if ($this->request->isPost()) {
  282. $pluginName = $this->request->param('name', '', 'trim');
  283. $result = PluginLogic::update($pluginName);
  284. if ($result !== true) {
  285. $this->error($result);
  286. }
  287. $this->success('更新成功!');
  288. }
  289. }
  290. /**
  291. * 卸载插件
  292. * @adminMenu(
  293. * 'name' => '卸载插件',
  294. * 'parent' => 'index',
  295. * 'display'=> false,
  296. * 'hasView'=> false,
  297. * 'order' => 10000,
  298. * 'icon' => '',
  299. * 'remark' => '卸载插件',
  300. * 'param' => ''
  301. * )
  302. */
  303. public function uninstall()
  304. {
  305. if ($this->request->isPost()) {
  306. $pluginModel = new PluginModel();
  307. $id = $this->request->param('id', 0, 'intval');
  308. $result = $pluginModel->uninstall($id);
  309. if ($result !== true) {
  310. $this->error('卸载失败!');
  311. }
  312. Cache::clear('init_hook_plugins');
  313. Cache::clear('admin_menus');// 删除后台菜单缓存
  314. $this->success('卸载成功!');
  315. }
  316. }
  317. }