SystemEvent.php 4.8 KB

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