SystemCrontab.php 4.2 KB

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