国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
4.1KB

  1. <?php
  2. /**
  3. *
  4. * 计划任务
  5. *
  6. * 计划任务程序使用说明:
  7. * 本程序本身并不会执行任务任务,职责是跳转到要执行的任务网址(JS调用形式),或直接返回要执行的任务网址(客户端形式)
  8. * ·为了确保任务能执行完全,建议使用Dede的客户端工具,否则只能通过JS触发,但JS触发有很多不确定因素会导致任务不能完成;
  9. * ·JS触发方式:在所有文档页面中用JS调用/plus/task.php?client=js(必须禁用计划任务的密码,系统配置参数->其它选项);
  10. * ·自行定制客户端:直接访问“http://网址/plus/task.php?clientpwd=管理密码”,会返回其中一个可执行任务的网址(没有可用任务则返回串:notask),然后客户端运行这个网址即可。
  11. *
  12. * @version $Id: task.php$
  13. * @package DedeBIZ.Site
  14. * @copyright Copyright (c) 2020, DedeBIZ.COM
  15. * @license https://www.dedebiz.com/license
  16. * @link https://www.dedebiz.com
  17. */
  18. require_once(dirname(__FILE__) . '/../include/common.inc.php');
  19. require_once(DEDEINC . '/dedetag.class.php');
  20. if (empty($client)) $client = 'dede';
  21. if (empty($clientpwd)) $clientpwd = '';
  22. $cfg_task_pwd = trim($cfg_task_pwd);
  23. //验证客户端工具密码
  24. if (!empty($cfg_task_pwd) && $clientpwd != $cfg_task_pwd) {
  25. echo ($client == 'js' ? '' : 'notask');
  26. exit();
  27. }
  28. //取得当时间的各个值
  29. $ntime = time();
  30. $nformattime = GetDateTimeMk($ntime);
  31. list($ndate, $ntime) = explode(' ', $nformattime);
  32. list($y, $m, $d) = explode('-', $ndate);
  33. list($hh, $mm, $ss) = explode(':', $ntime);
  34. $daylimit = 24 * 3600;
  35. $dsql->Execute('me', 'SELECT * FROM `#@__sys_task` WHERE islock=0 ORDER BY id ASC ');
  36. while ($arr = $dsql->GetArray()) {
  37. $starttime = $arr['starttime'];
  38. $endtime = $arr['endtime'];
  39. $ntime = strtotime("now");
  40. //跳过一次性运行,并且已经运行的任务
  41. if ($arr['lastrun'] > $starttime && $arr['runtype'] == 1) continue;
  42. //超过了设定的任务结束时间
  43. if ($endtime != 0 && $endtime < $ntime) continue;
  44. //未达到任务开始时间的任务
  45. if ($starttime != 0 && $ntime < $starttime) continue;
  46. $dotime = GetMkTime($ndate . ' ' . $arr['runtime'] . ':00');
  47. $limittime = $daylimit * $arr['freq'];
  48. $isplay = false;
  49. //判断符合执行条件的任务
  50. if ($arr['freq'] > 1 && $ntime - $arr['lastrun'] > $limittime) {
  51. $isplay = true;
  52. } else {
  53. $ndateInt = intval(str_replace('-', '', $ndate));
  54. $rdateInt = intval(str_replace('-', '', GetDateMk($arr['lastrun'])));
  55. list($th, $tm) = explode(':', $arr['runtime']);
  56. if (
  57. $ndateInt > $rdateInt
  58. && ($hh > $th || ($hh == $th && $mm >= $tm))
  59. ) {
  60. $isplay = true;
  61. }
  62. }
  63. //符合需执行条件的任务
  64. if ($isplay) {
  65. $dourl = trim($arr['dourl']);
  66. if (!file_exists("task/$dourl")) {
  67. echo ($client == 'js' ? '' : 'notask');
  68. exit();
  69. } else {
  70. $getConfigStr = trim($arr['parameter']);
  71. $getString = '';
  72. if (preg_match('#t:#', $getConfigStr)) {
  73. $getStrings = array();
  74. $dtp = new DedeTagParse();
  75. $dtp->SetNameSpace('t', '<', '>');
  76. $dtp->LoadString($getConfigStr);
  77. if (is_array($dtp->CTags)) {
  78. foreach ($dtp->CTags as $k => $ctag) {
  79. $getString .= ($getString == '' ? '' : '&') . $ctag->GetAtt('key') . '=' . urlencode($ctag->GetAtt('value'));
  80. }
  81. }
  82. }
  83. $dsql->ExecuteNoneQuery("Update `#@__sys_task` set lastrun='" . time() . "', sta='运行' where id='{$arr['id']}' ");
  84. if ($getString != '') $dourl .= '?' . $getString;
  85. if ($client == 'js') header("location:{$cfg_phpurl}/task/{$dourl}");
  86. else echo "{$cfg_phpurl}/task/{$dourl}";
  87. exit();
  88. }
  89. }
  90. }
  91. echo ($client == 'js' ? '' : 'notask');
  92. exit();