SystemCrontab.php 4.9 KB

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