PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/base/cron.php

https://gitlab.com/nexxuz/phpBMS
PHP | 377 lines | 298 code | 58 blank | 21 comment | 76 complexity | c670d7102c5799ff4dff6bffae98f3e0 MD5 | raw file
  1. <?php
  2. $loginNoKick=true;
  3. $loginNoDisplayError=true;
  4. include("../../include/session.php");
  5. /**
  6. * tables.php for push records
  7. */
  8. include_once("../../include/tables.php");
  9. //testing cron access
  10. if(isset($_GET["t"]) && !APP_DEBUG){
  11. echo "ztz";
  12. exit;
  13. }//endif
  14. $now = gmdate('Y-m-d H:i', strtotime('now'));
  15. $querystatement = "
  16. SELECT
  17. `scheduler`.id,
  18. `scheduler`.name,
  19. `scheduler`.crontab,
  20. IF(`scheduler`.`pushrecordid` != '', 'pushrecord', 'job') AS `type`,
  21. `scheduler`.job,
  22. `scheduler`.`pushrecordid`,
  23. `scheduler`.startdatetime,
  24. `scheduler`.enddatetime
  25. ";
  26. if(moduleExists("mod:b2d42220-443b-fe74-dbdb-ed2c0968c38c", $phpbms->modules)){
  27. $querystatement .= "
  28. ,
  29. `tabledefs`.`maintable`,
  30. `modules`.`name` AS `modulename`
  31. FROM
  32. ";
  33. $querystatement .= "
  34. (((scheduler LEFT JOIN `pushrecords` ON `scheduler`.`pushrecordid` = `pushrecords`.`uuid`) LEFT JOIN `tabledefs` ON `pushrecords`.`originuuid` = `tabledefs`.`uuid`) LEFT JOIN `modules` ON `tabledefs`.`moduleid` = `modules`.`uuid`)
  35. ";
  36. }else{
  37. $querystatement .= "
  38. FROM
  39. ";
  40. $querystatement .= "
  41. `scheduler`
  42. ";
  43. }//end if
  44. $querystatement .= " WHERE
  45. `scheduler`.inactive = '0'
  46. AND `scheduler`.startdatetime < NOW()
  47. AND (`scheduler`.enddatetime > NOW() OR `scheduler`.enddatetime IS NULL)
  48. ";
  49. $queryresult=$db->query($querystatement);
  50. while($schedule_record=$db->fetchArray($queryresult)){
  51. $datetimearray=explode(" ",$schedule_record["startdatetime"]);
  52. $schedule_record["startdate"]=stringToDate($datetimearray[0],"SQL");
  53. $schedule_record["starttime"]=stringToTime($datetimearray[1],"24 Hour");
  54. if($schedule_record["enddatetime"]){
  55. $datetimearray=explode(" ",$schedule_record["enddatetime"]);
  56. $schedule_record["enddate"]=stringToDate($datetimearray[0],"SQL");
  57. $schedule_record["endtime"]=stringToTime($datetimearray[1],"24 Hour");
  58. }//endif enddateiem
  59. $validTimes = getTimes($schedule_record);
  60. if(is_array($validTimes) && in_array($now, $validTimes)){
  61. switch($schedule_record["type"]){
  62. case "job":
  63. $success = @ include($schedule_record["job"]);
  64. break;
  65. case "pushrecord":
  66. include_once("../../modules/api/include/push.php");
  67. //try to include table specific functions
  68. $tableFile = "../../modules/".$schedule_record["modulename"]."/include/".$schedule_record["maintable"].".php";
  69. if(file_exists($tableFile))
  70. include_once($tableFile);
  71. $push = new push($db, $schedule_record["pushrecordid"]);
  72. $success = $push->process();
  73. break;
  74. }//end switch
  75. if($success){
  76. $updatestatement = "UPDATE scheduler SET lastrun=NOW() WHERE id=".$schedule_record["id"];
  77. $db->query($updatestatement);
  78. $log = new phpbmsLog("Scheduled Job ".$schedule_record["name"]." (".$schedule_record["id"].") completed","SCHEDULER","usr:42e0cc76-3c31-d9b6-ff12-fe4adfd15e75");
  79. } else {
  80. $log = new phpbmsLog("Scheduled Job ".$schedule_record["name"]." (".$schedule_record["id"].") returned errors","SCHEDULER","usr:42e0cc76-3c31-d9b6-ff12-fe4adfd15e75");
  81. }//endif success
  82. }//endif is_array();
  83. }//endwhile
  84. function getTimes($recordarray){
  85. $dayInt = array('*',1,2,3,4,5,6,7);
  86. $dayLabel = array('*',"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
  87. $monthsInt = array(0,1,2,3,4,5,6,7,8,9,10,11,12);
  88. $monthsLabel = array(0,"January","February","March","April","May","June","July","August","September","October","November","December");
  89. $metricsVar = array("*", "/", "-", ",");
  90. $metricsVal = array(' every ','',' thru ',' and ');
  91. $dateTimes = array();
  92. $ints = explode('::', str_replace(' ','',$recordarray["crontab"]));
  93. $days = $ints[4];
  94. $mons = $ints[3];
  95. $dates = $ints[2];
  96. $hrs = $ints[1];
  97. $mins = $ints[0];
  98. $today = getdate(gmmktime());
  99. // derive day part
  100. if($days == '*') {
  101. //do nothing
  102. } elseif(strstr($days, '*/')) {
  103. $theDay = str_replace('*/','',$days);
  104. $dayName[] = str_replace($dayInt, $dayLabel, $theDay);
  105. } elseif($days != '*') {
  106. if(strstr($days, ',')) {
  107. $exDays = explode(',',$days);
  108. foreach($exDays as $k1 => $dayGroup) {
  109. if(strstr($dayGroup,'-')) {
  110. $exDayGroup = explode('-', $dayGroup);
  111. for($i=$exDayGroup[0];$i<=$exDayGroup[1];$i++) {
  112. $dayName[] = str_replace($dayInt, $dayLabel, $i);
  113. }
  114. } else { // individuals
  115. $dayName[] = str_replace($dayInt, $dayLabel, $dayGroup);
  116. }
  117. }
  118. } elseif(strstr($days, '-')) {
  119. $exDayGroup = explode('-', $days);
  120. for($i=$exDayGroup[0];$i<=$exDayGroup[1];$i++) {
  121. $dayName[] = str_replace($dayInt, $dayLabel, $i);
  122. }
  123. } else {
  124. $dayName[] = str_replace($dayInt, $dayLabel, $days);
  125. }
  126. // check the day to be in scope:
  127. if(!in_array($today['weekday'], $dayName)) {
  128. return false;
  129. }
  130. } else {
  131. return false;
  132. }
  133. // derive months part
  134. if($mons == '*') {
  135. // do nothing
  136. } elseif(strstr($mons, '*/')) {
  137. $mult = str_replace('*/','',$mons);
  138. $startMon = date(strtotime('m',$recordarray["startdate"]));
  139. $startFrom = ($startMon % $mult);
  140. for($i=$startFrom;$i<=12;$i+$mult) {
  141. $compMons[] = $i+$mult;
  142. $i += $mult;
  143. }
  144. // this month is not in one of the multiplier months
  145. if(!in_array($today['mon'],$compMons)) {
  146. return false;
  147. }
  148. } elseif($mons != '*') {
  149. if(strstr($mons,',')) { // we have particular (groups) of months
  150. $exMons = explode(',',$mons);
  151. foreach($exMons as $k1 => $monGroup) {
  152. if(strstr($monGroup, '-')) { // we have a range of months
  153. $exMonGroup = explode('-',$monGroup);
  154. for($i=$exMonGroup[0];$i<=$exMonGroup[1];$i++) {
  155. $monName[] = $i;
  156. }
  157. } else {
  158. $monName[] = $monGroup;
  159. }
  160. }
  161. } elseif(strstr($mons, '-')) {
  162. $exMonGroup = explode('-', $mons);
  163. for($i=$exMonGroup[0];$i<=$exMonGroup[1];$i++) {
  164. $monName[] = $i;
  165. }
  166. } else { // one particular month
  167. $monName[] = $mons;
  168. }
  169. // check that particular months are in scope
  170. if(!in_array($today['mon'], $monName)) {
  171. return false;
  172. }
  173. }
  174. // derive dates part
  175. if($dates == '*') {
  176. //do nothing
  177. } elseif(strstr($dates, '*/')) {
  178. $mult = str_replace('*/','',$dates);
  179. $startDate = date('d', strtotime($recordarray["startdate"]));
  180. $startFrom = ($startDate % $mult);
  181. for($i=$startFrom; $i<=31; $i+$mult) {
  182. $dateName[] = str_pad(($i+$mult),2,'0',STR_PAD_LEFT);
  183. $i += $mult;
  184. }
  185. if(!in_array($today['mday'], $dateName)) {
  186. return false;
  187. }
  188. } elseif($dates != '*') {
  189. if(strstr($dates, ',')) {
  190. $exDates = explode(',', $dates);
  191. foreach($exDates as $k1 => $dateGroup) {
  192. if(strstr($dateGroup, '-')) {
  193. $exDateGroup = explode('-', $dateGroup);
  194. for($i=$exDateGroup[0];$i<=$exDateGroup[1];$i++) {
  195. $dateName[] = $i;
  196. }
  197. } else {
  198. $dateName[] = $dateGroup;
  199. }
  200. }
  201. } elseif(strstr($dates, '-')) {
  202. $exDateGroup = explode('-', $dates);
  203. for($i=$exDateGroup[0];$i<=$exDateGroup[1];$i++) {
  204. $dateName[] = $i;
  205. }
  206. } else {
  207. $dateName[] = $dates;
  208. }
  209. // check that dates are in scope
  210. if(!in_array($today['mday'], $dateName)) {
  211. return false;
  212. }
  213. }
  214. // derive hours part
  215. $currentHour = date('G', strtotime('00:00'));
  216. if($hrs == '*') {
  217. for($i=0;$i<24; $i++) {
  218. $hrName[]=$i;
  219. }
  220. } elseif(strstr($hrs, '*/')) {
  221. $mult = str_replace('*/','',$hrs);
  222. for($i=0; $i<24; $i) { // weird, i know
  223. $hrName[]=$i;
  224. $i += $mult;
  225. }
  226. } elseif($hrs != '*') {
  227. if(strstr($hrs, ',')) {
  228. $exHrs = explode(',',$hrs);
  229. foreach($exHrs as $k1 => $hrGroup) {
  230. if(strstr($hrGroup, '-')) {
  231. $exHrGroup = explode('-', $hrGroup);
  232. for($i=$exHrGroup[0];$i<=$exHrGroup[1];$i++) {
  233. $hrName[] = $i;
  234. }
  235. } else {
  236. $hrName[] = $hrGroup;
  237. }
  238. }
  239. } elseif(strstr($hrs, '-')) {
  240. $exHrs = explode('-', $hrs);
  241. for($i=$exHrs[0];$i<=$exHrs[1];$i++) {
  242. $hrName[] = $i;
  243. }
  244. } else {
  245. $hrName[] = $hrs;
  246. }
  247. }
  248. // derive minutes
  249. $currentMin = date('i', strtotime($recordarray["starttime"]));
  250. if(substr($currentMin, 0, 1) == '0') {
  251. $currentMin = substr($currentMin, 1, 1);
  252. }
  253. if($mins == '*') {
  254. for($i=0; $i<60; $i++) {
  255. if(($currentMin + $i) > 59) {
  256. $minName[] = ($i + $currentMin - 60);
  257. } else {
  258. $minName[] = ($i+$currentMin);
  259. }
  260. }
  261. } elseif(strstr($mins,'*/')) {
  262. $mult = str_replace('*/','',$mins);
  263. $startMin = date('i',strtotime($recordarray["starttime"]));
  264. $startFrom = ($startMin % $mult);
  265. for($i=$startFrom; $i<=59; $i) {
  266. if(($currentMin + $i) > 59) {
  267. $minName[] = ($i + $currentMin - 60);
  268. } else {
  269. $minName[] = ($i+$currentMin);
  270. }
  271. $i += $mult;
  272. }
  273. } elseif($mins != '*') {
  274. if(strstr($mins, ',')) {
  275. $exMins = explode(',',$mins);
  276. foreach($exMins as $k1 => $minGroup) {
  277. if(strstr($minGroup, '-')) {
  278. $exMinGroup = explode('-', $minGroup);
  279. for($i=$exMinGroup[0]; $i<=$exMinGroup[1]; $i++) {
  280. $minName[] = $i;
  281. }
  282. } else {
  283. $minName[] = $minGroup;
  284. }
  285. }
  286. } elseif(strstr($mins, '-')) {
  287. $exMinGroup = explode('-', $mins);
  288. for($i=$exMinGroup[0]; $i<=$exMinGroup[1]; $i++) {
  289. $minName[] = $i;
  290. }
  291. } else {
  292. $minName[] = $mins;
  293. }
  294. }
  295. // prep some boundaries - these are not in GMT b/c gmt is a 24hour period, possibly bridging 2 local days
  296. $timeFromTs = 0;
  297. $timeToTs = strtotime('+1 da');
  298. $timeToTs++;
  299. $lastRunTs = 0;
  300. /**
  301. * initialize return array
  302. */
  303. $validJobTime = array();
  304. $hourSeen = 0;
  305. foreach($hrName as $kHr=>$hr) {
  306. $hourSeen++;
  307. foreach($minName as $kMin=>$min) {
  308. if($hr < $currentHour || $hourSeen == 25)
  309. $theDate = date('Y-m-d', strtotime('+1 day'));
  310. else
  311. $theDate = date('Y-m-d');
  312. $tsGmt = strtotime($theDate.' '.str_pad($hr,2,'0',STR_PAD_LEFT).":".str_pad($min,2,'0',STR_PAD_LEFT).":00"); // this is LOCAL
  313. $validJobTime[] = gmdate('Y-m-d H:i', $tsGmt);
  314. }
  315. }
  316. sort($validJobTime);
  317. return $validJobTime;
  318. }//end function
  319. ?>