phpBMS

root/trunk/phpbms/modules/base/cron.php

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