SystemEvent.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\system;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\system\SystemEventServices;
  14. use think\facade\App;
  15. use think\facade\Env;
  16. class SystemEvent extends AuthController
  17. {
  18. public function __construct(App $app, SystemEventServices $services)
  19. {
  20. parent::__construct($app);
  21. $this->services = $services;
  22. }
  23. /**
  24. * 自定事件类型
  25. * @return \think\Response
  26. * @author wuhaotian
  27. * @email 442384644@qq.com
  28. * @date 2024/6/7
  29. */
  30. public function getMarkList()
  31. {
  32. return app('json')->success($this->services->getMarkList());
  33. }
  34. /**
  35. * 自定事件列表
  36. * @return \think\Response
  37. * @throws \ReflectionException
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @author wuhaotian
  42. * @email 442384644@qq.com
  43. * @date 2024/6/7
  44. */
  45. public function getEventList()
  46. {
  47. return app('json')->success($this->services->getEventList());
  48. }
  49. /**
  50. * 自定事件详情
  51. * @param $id
  52. * @return \think\Response
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. * @author wuhaotian
  57. * @email 442384644@qq.com
  58. * @date 2024/6/7
  59. */
  60. public function getEventInfo($id)
  61. {
  62. if (!$id) return app('json')->fail('参数错误');
  63. return app('json')->success($this->services->getEventInfo($id));
  64. }
  65. /**
  66. * 自定事件添加编辑
  67. * @return \think\Response
  68. * @author wuhaotian
  69. * @email 442384644@qq.com
  70. * @date 2024/6/7
  71. */
  72. public function saveEvent()
  73. {
  74. $data = $this->request->postMore([
  75. ['id', 0],
  76. ['name', ''],
  77. ['mark', ''],
  78. ['content', ''],
  79. ['is_open', 0],
  80. ['customCode', ''],
  81. ['password', ''],
  82. ]);
  83. if ($data['name'] == '') return app('json')->fail('请填写事件名称');
  84. if ($data['mark'] == '') return app('json')->fail('请选择事件类型');
  85. if (!Env::get('app_debug', false)) return app('json')->fail('生产环境下无法新增和修改自定义内容,如需修改请修改.env文件中app_debug项为true');
  86. if ($data['password'] === '') return app('json')->fail('密码不能为空');
  87. if (config('filesystem.password') !== $data['password']) return app('json')->fail('密码错误');
  88. $adminInfo = $this->request->adminInfo();
  89. if (!$adminInfo) return app('json')->fail('非法操作');
  90. if ($adminInfo['level'] != 0) return app('json')->fail('仅超级管理员可以操作定时任务');
  91. if (!$this->isSafePhpCode($data['customCode'])) return app('json')->fail('自定义内容存在危险代码,请检查代码');
  92. $this->services->saveEvent($data);
  93. return app('json')->success('保存成功');
  94. }
  95. /**
  96. * 检查是否包含删除表,删除表数据,删除文件,修改文件内容以及后缀,执行命令等操作的关键词
  97. * @param $code
  98. * @return bool
  99. * @author wuhaotian
  100. * @email 442384644@qq.com
  101. * @date 2024/6/7
  102. */
  103. function isSafePhpCode($code)
  104. {
  105. // 检查是否包含删除表,删除表数据,删除文件,修改文件内容以及后缀,执行命令等操作的关键词
  106. $dangerous_keywords = [
  107. 'delete',
  108. 'destroy',
  109. 'DROP TABLE',
  110. 'DELETE FROM',
  111. 'unlink(',
  112. 'fwrite(',
  113. 'shell_exec(',
  114. 'exec(',
  115. 'system(',
  116. 'passthru('
  117. ];
  118. foreach ($dangerous_keywords as $keyword) {
  119. if (strpos($code, $keyword) !== false) {
  120. return false;
  121. }
  122. }
  123. return true; // 如果通过所有安全检查,返回 true
  124. }
  125. /**
  126. * 自定事件是否开启开关
  127. * @param $id
  128. * @param $is_open
  129. * @return \think\Response
  130. * @author wuhaotian
  131. * @email 442384644@qq.com
  132. * @date 2024/6/7
  133. */
  134. public function setEventStatus($id, $is_open)
  135. {
  136. $this->services->setEventStatus($id, $is_open);
  137. return app('json')->success('设置成功');
  138. }
  139. /**
  140. * 删除自定事件
  141. * @param $id
  142. * @return \think\Response
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\DbException
  145. * @throws \think\db\exception\ModelNotFoundException
  146. * @author wuhaotian
  147. * @email 442384644@qq.com
  148. * @date 2024/6/7
  149. */
  150. public function delEvent($id)
  151. {
  152. if (!$id) return app('json')->fail('参数错误');
  153. $this->services->eventDel($id);
  154. return app('json')->success('删除成功');
  155. }
  156. }