task.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. define('EmpireCMSAdmin','1');
  3. require("../class/connect.php");
  4. require("../class/db_sql.php");
  5. require("../class/functions.php");
  6. require LoadLang("pub/fun.php");
  7. require("../class/delpath.php");
  8. require("../class/copypath.php");
  9. require("../class/t_functions.php");
  10. require("../data/dbcache/class.php");
  11. require("../data/dbcache/MemberLevel.php");
  12. $link=db_connect();
  13. $empire=new mysqlquery();
  14. //验证用户
  15. $lur=is_login();
  16. $logininid=$lur['userid'];
  17. $loginin=$lur['username'];
  18. $loginrnd=$lur['rnd'];
  19. $loginlevel=$lur['groupid'];
  20. $loginadminstyleid=$lur['adminstyleid'];
  21. //ehash
  22. $ecms_hashur=hReturnEcmsHashStrAll();
  23. @set_time_limit(0);
  24. //加载
  25. $incftp=0;
  26. if($public_r['phpmode'])
  27. {
  28. include("../class/ftp.php");
  29. $incftp=1;
  30. }
  31. //防采集
  32. if($public_r['opennotcj'])
  33. {
  34. @include("../data/dbcache/notcj.php");
  35. }
  36. //验证是否执行
  37. function CheckDoTask($r){
  38. global $logininid;
  39. if($r['userid']&&$r['userid']!=$logininid)//执行用户
  40. {
  41. return false;
  42. }
  43. $time=time();
  44. $date=date("j,w,G,i,Y,m");
  45. $dr=explode(',',$date);
  46. if(','.$r['doday'].','!=',*,'&&$r['doday']!=$dr[0])//日期
  47. {
  48. return false;
  49. }
  50. if(','.$r['doweek'].','!=',*,'&&$r['doweek']!=$dr[1])//星期
  51. {
  52. return false;
  53. }
  54. if(','.$r['dohour'].','!=',*,'&&$r['dohour']!=$dr[2])//小时
  55. {
  56. return false;
  57. }
  58. $min=(int)$dr[3];
  59. if($r['dominute']!=''&&$r['dominute']!=','&&!strstr($r['dominute'],','.$min.','))//分钟
  60. {
  61. return false;
  62. }
  63. if($r['lastdo'])
  64. {
  65. if(!TogTaskTime($r,$dr))
  66. {
  67. return false;
  68. }
  69. }
  70. return $time;
  71. }
  72. //验证时间
  73. function TogTaskTime($r,$dr){
  74. $re['chdate']=$dr[4].'-'.$dr[5].'-';
  75. $re['date']='Y-m-';
  76. if($r['doday'])
  77. {
  78. $re['chdate'].='-'.$dr[0];
  79. $re['date'].='-j';
  80. }
  81. if($r['doweek'])
  82. {
  83. $re['chdate'].='-'.$dr[1];
  84. $re['date'].='-w';
  85. }
  86. if($r['dohour'])
  87. {
  88. $re['chdate'].='-'.$dr[2];
  89. $re['date'].='-G';
  90. }
  91. if($r['dominute'])
  92. {
  93. $re['chdate'].='-'.$dr[3];
  94. $re['date'].='-i';
  95. }
  96. if(date($re['date'],$r['lastdo'])==$re['chdate'])
  97. {
  98. return false;
  99. }
  100. return true;
  101. }
  102. $retasktime=30;
  103. $tasksql="select id,filename,lastdo,doweek,doday,dohour,dominute,userid,taskname from {$dbtbpre}enewstask where isopen=1 and (userid=0 or (userid<>0 and userid='$logininid'))";
  104. $ecms=RepPostStr($_GET['ecms'],1);
  105. echo"<title>执行计划任务</title><link href='adminstyle/".$loginadminstyleid."/adminstyle.css' rel='stylesheet' type='text/css'>";
  106. //执行
  107. if($ecms=='dotask')
  108. {
  109. $id=(int)$_GET['id'];
  110. if(empty($id))
  111. {
  112. exit();
  113. }
  114. $r=$empire->fetch1("select id,filename,lastdo,doweek,doday,dohour,dominute,userid,taskname from {$dbtbpre}enewstask where id=$id and isopen=1 limit 1");
  115. $file='../tasks/'.$r['filename'];
  116. if(empty($r['id'])||empty($r['filename'])||!file_exists($file))
  117. {
  118. exit();
  119. }
  120. $lasttime=CheckDoTask($r);
  121. if($lasttime)
  122. {
  123. echo"<script>parent.WriteTaskLog('任务 <".$r['taskname']."> 开始执行......');</script>";
  124. include_once($file);
  125. $usql=$empire->query("update {$dbtbpre}enewstask set lastdo='$lasttime' where id=$id");
  126. echo"<script>parent.WriteTaskLog('任务 <".$r['taskname']."> 执行完毕,最后执行时间:".date("Y-m-d H:i:s",$lasttime)."');</script>";
  127. }
  128. }
  129. //更新
  130. elseif($ecms=='retask')
  131. {
  132. echo"<meta http-equiv=\"refresh\" content=\"".$retasktime.";url=task.php?ecms=retask".$ecms_hashur['href']."\">";
  133. ?>
  134. <script>
  135. function AddTaskDiv(divid,taskid){
  136. if(parent.document.getElementById(divid)!=null)
  137. {
  138. parent.document.getElementById('page'+divid).src='task.php?<?=$ecms_hashur['href']?>&ecms=dotask&id='+taskid;
  139. }
  140. else
  141. {
  142. parent.document.getElementById("alltaskdiv").innerHTML+="<div id='"+divid+"'><iframe frameborder='0' id='page"+divid+"' name='page"+divid+"' scrolling='no' src='task.php?<?=$ecms_hashur['href']?>&ecms=dotask&id="+taskid+"' style='HEIGHT:0;VISIBILITY:inherit;WIDTH:0;Z-INDEX:1'></iframe></div>";
  143. }
  144. }
  145. </script>
  146. <script>
  147. <?php
  148. $sql=$empire->query($tasksql);
  149. while($r=$empire->fetch($sql))
  150. {
  151. if(CheckDoTask($r))
  152. {
  153. echo"AddTaskDiv('taskdiv".$r['id']."','".$r['id']."');";
  154. }
  155. }
  156. ?>
  157. </script>
  158. <?php
  159. }
  160. //运行
  161. elseif($ecms=='TodoTask')
  162. {
  163. $id=(int)$_GET['id'];
  164. if(empty($id))
  165. {
  166. exit();
  167. }
  168. //验证权限
  169. CheckLevel($logininid,$loginin,$classid,"task");
  170. $r=$empire->fetch1("select id,filename,taskname from {$dbtbpre}enewstask where id=$id limit 1");
  171. $file='../tasks/'.$r['filename'];
  172. if($r['id']&&$r['filename']&&file_exists($file))
  173. {
  174. include_once($file);
  175. $enews=$ecms;
  176. //操作日志
  177. insert_dolog("id=$id&taskname=$r[taskname]&filename=$r[filename]");
  178. }
  179. printerror('TodoTaskSuccess','');
  180. }
  181. else
  182. {
  183. ?>
  184. <script>
  185. function WriteTaskLog(str){
  186. document.tasklogform.tasklog.value=" "+str+"\r\n"+document.tasklogform.tasklog.value;
  187. }
  188. function ClearTaskLog(){
  189. document.tasklogform.tasklog.value="";
  190. }
  191. </script>
  192. <IFRAME frameBorder="0" id="retaskfile" name="retaskfile" scrolling="no" src="task.php?ecms=retask<?=$ecms_hashur['href']?>" style="HEIGHT:0;VISIBILITY:inherit;WIDTH:0;Z-INDEX:1"></IFRAME>
  193. <table width='100%' border=0 align=center cellpadding=3 cellspacing=1 class='tableborder'>
  194. <form name="tasklogform" method="post">
  195. <tr class=header><td>任务日志&nbsp;&nbsp;&nbsp;[<a href="#ecms" onclick="ClearTaskLog();">清除日志内容</a>]</td></tr>
  196. <tr><td bgcolor='#ffffff'>
  197. <textarea name="tasklog" cols="80" rows="30" id="tasklog" style="width:100%"> 计划任务开始运行时间:<?=date("Y-m-d H:i:s")?></textarea>
  198. </td></tr>
  199. </form>
  200. </table>
  201. <div id="alltaskdiv">
  202. </div>
  203. <?php
  204. }
  205. db_close();
  206. $empire=null;
  207. ?>