国内流行的内容管理系统(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.

110 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 DedeCMS.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. {
  26. echo ($client=='js' ? '' : 'notask');
  27. exit();
  28. }
  29. //取得当时间的各个值
  30. $ntime = time();
  31. $nformattime = GetDateTimeMk($ntime);
  32. list($ndate, $ntime) = explode(' ', $nformattime);
  33. list($y, $m, $d) = explode('-', $ndate);
  34. list($hh, $mm, $ss) = explode(':', $ntime);
  35. $daylimit = 24 * 3600;
  36. $dsql->Execute('me', 'SELECT * FROM `#@__sys_task` WHERE islock=0 ORDER BY id ASC ');
  37. while($arr = $dsql->GetArray())
  38. {
  39. $starttime = $arr['starttime'];
  40. $endtime = $arr['endtime'];
  41. $ntime = strtotime("now");
  42. //跳过一次性运行,并且已经运行的任务
  43. if($arr['lastrun'] > $starttime && $arr['runtype']==1) continue;
  44. //超过了设定的任务结束时间
  45. if($endtime!=0 && $endtime < $ntime) continue;
  46. //未达到任务开始时间的任务
  47. if($starttime!=0 && $ntime < $starttime) continue;
  48. $dotime = GetMkTime($ndate.' '.$arr['runtime'].':00');
  49. $limittime = $daylimit * $arr['freq'];
  50. $isplay = false;
  51. //判断符合执行条件的任务
  52. if($arr['freq'] > 1 && $ntime - $arr['lastrun'] > $limittime )
  53. {
  54. $isplay = true;
  55. }
  56. else
  57. {
  58. $ndateInt = intval( str_replace('-', '', $ndate) );
  59. $rdateInt = intval( str_replace('-', '', GetDateMk($arr['lastrun'])) );
  60. list($th, $tm) = explode(':', $arr['runtime']);
  61. if($ndateInt > $rdateInt
  62. && ($hh > $th || ($hh==$th && $mm >= $tm) ) )
  63. {
  64. $isplay = true;
  65. }
  66. }
  67. //符合需执行条件的任务
  68. if($isplay)
  69. {
  70. $dourl = trim($arr['dourl']);
  71. if(!file_exists("task/$dourl"))
  72. {
  73. echo ($client=='js' ? '' : 'notask');
  74. exit();
  75. }
  76. else
  77. {
  78. $getConfigStr = trim($arr['parameter']);
  79. $getString = '';
  80. if(preg_match('#t:#', $getConfigStr))
  81. {
  82. $getStrings = array();
  83. $dtp = new DedeTagParse();
  84. $dtp->SetNameSpace('t', '<', '>');
  85. $dtp->LoadString($getConfigStr);
  86. if(is_array($dtp->CTags))
  87. {
  88. foreach($dtp->CTags as $k=>$ctag)
  89. {
  90. $getString .= ($getString=='' ? '' : '&').$ctag->GetAtt('key').'='.urlencode($ctag->GetAtt('value'));
  91. }
  92. }
  93. }
  94. $dsql->ExecuteNoneQuery("Update `#@__sys_task` set lastrun='".time()."', sta='运行' where id='{$arr['id']}' ");
  95. if($getString !='' ) $dourl .= '?'.$getString;
  96. if($client=='js') header("location:{$cfg_phpurl}/task/{$dourl}");
  97. else echo "{$cfg_phpurl}/task/{$dourl}";
  98. exit();
  99. }
  100. }
  101. }
  102. echo ($client=='js' ? '' : 'notask');
  103. exit();