Add analytics
[bus.git] / busui / owa / includes / CronParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<?php /* $Id: CronParser.php,v 1.7 2005/09/12 01:04:05 ns Exp $ */
 
/**####################################################################################################**\
   Version: V1.01
   Release Date: 12 Sep 2005
   Licence: GPL
   By: Nikol S
   Please send bug reports to ns@eyo.com.au
\**####################################################################################################**/
 
/* This class is based on the concept in the CronParser class written by Mick Sear http://www.ecreate.co.uk
 * The following functions are direct copies from or based on the original class:
 * getLastRan(), getDebug(), debug(), expand_ranges()
 *
 * Who can use this class?
 * This class is idea for people who can not use the traditional Unix cron through shell.
 * One way of using is embedding the calling script in a web page which is often visited.
 * The script will work out the last due time, by comparing with run log timestamp. The scrip
 * will envoke any scripts needed to run, be it deleting older table records, or updating prices.
 * It can parse the same cron string used by Unix.
 */
 
/* Usage example:
 
$cron_str0 = "0,12,30-51 3,21-23,10 1-25 9-12,1 0,3-7";
require_once("CronParser.php");
$cron = new CronParser();
$cron->calcLastRan($cron_str0);
// $cron->getLastRanUnix() returns an Unix timestamp
echo "Cron '$cron_str0' last due at: " . date('r', $cron->getLastRanUnix()) . "<p>";
// $cron->getLastRan() returns last due time in an array
print_r($cron->getLastRan());
echo "Debug:<br>" . nl2br($cron->getDebug());
 
$cron_str1 = "3 12 * * *";
if ($cron->calcLastRan($cron_str1))
{
   echo "<p>Cron '$cron_str1' last due at: " . date('r', $cron->getLastRanUnix()) . "<p>";
   print_r($cron->getLastRan());
}
else
{
   echo "Error parsing";
}
echo "Debug:<br>" . nl2br($cron->getDebug());
 
 *#######################################################################################################
 */
 
class CronParser
{
 
        var $bits = Array(); //exploded String like 0 1 * * *
        var $now = Array();     //Array of cron-style entries for time()
        var $lastRan;           //Timestamp of last ran time.
        var $taken;
        var $debug;
        var $year;
        var $month;
        var $day;
        var $hour;
        var $minute;
        var $minutes_arr = array();     //minutes array based on cron string
        var $hours_arr = array();       //hours array based on cron string
        var $months_arr = array();      //months array based on cron string
 
        function getLastRan()
        {
                return explode(",", strftime("%M,%H,%d,%m,%w,%Y", $this->lastRan)); //Get the values for now in a format we can use
        }
 
        function getLastRanUnix()
        {
                return $this->lastRan;
        }
 
        function getDebug()
        {
                return $this->debug;
        }
 
        function debug($str)
        {
                if (is_array($str))
                {
                        $this->debug .= "\nArray: ";
                        foreach($str as $k=>$v)
                        {
                                $this->debug .= "$k=>$v, ";
                        }
 
                }
                else
                {
                        $this->debug .= "\n$str";
                }
                //echo nl2br($this->debug);
        }
 
        /**
         * Assumes that value is not *, and creates an array of valid numbers that
         * the string represents.  Returns an array.
         */
        function expand_ranges($str)
        {
                if (strstr($str,  ","))
                {
                        $arParts = explode(',', $str);
                        foreach ($arParts AS $part)
                        {
                                if (strstr($part, '-'))
                                {
                                        $arRange = explode('-', $part);
                                        for ($i = $arRange[0]; $i <= $arRange[1]; $i++)
                                        {
                                                $ret[] = $i;
                                        }
                                }
                                else
                                {
                                        $ret[] = $part;
                                }
                        }
                }
                elseif (strstr($str,  '-'))
                {
                        $arRange = explode('-', $str);
                        for ($i = $arRange[0]; $i <= $arRange[1]; $i++)
                        {
                                $ret[] = $i;
                        }
                }
                else
                {
                        $ret[] = $str;
                }
                $ret = array_unique($ret);
                sort($ret);
                return $ret;
        }
 
        function daysinmonth($month, $year)
        {
                return date('t', mktime(0, 0, 0, $month, 1, $year));
        }
 
        /**
         *  Calculate the last due time before this moment
         */
        function calcLastRan($string)
        {
 
                $tstart = microtime();
                $this->debug = "";
                $this->lastRan = 0;
                $this->year = NULL;
                $this->month = NULL;
                $this->day = NULL;
                $this->hour = NULL;
                $this->minute = NULL;
                $this->hours_arr = array();
                $this->minutes_arr = array();
                $this->months_arr = array();
 
                $string = preg_replace('/[\s]{2,}/', ' ', $string);
 
                if (preg_match('/[^-,* \\d]/', $string) !== 0)
                {
                        $this->debug("Cron String contains invalid character");
                        return false;
                }
 
                $this->debug("<b>Working on cron schedule: $string</b>");
                $this->bits = @explode(" ", $string);
 
                if (count($this->bits) != 5)
                {
                        $this->debug("Cron string is invalid. Too many or too little sections after explode");
                        return false;
                }
 
                //put the current time into an array
                $t = strftime("%M,%H,%d,%m,%w,%Y", time());
                $this->now = explode(",", $t);
 
                $this->year = $this->now[5];
 
                $arMonths = $this->_getMonthsArray();
 
                do
                {
                        $this->month = array_pop($arMonths);
                }
                while ($this->month > $this->now[3]);
 
                if ($this->month === NULL)
                {
                        $this->year = $this->year - 1;
                        $this->debug("Not due within this year. So checking the previous year " . $this->year);
                        $arMonths = $this->_getMonthsArray();
                        $this->_prevMonth($arMonths);
                }
                elseif ($this->month == $this->now[3]) //now Sep, month = array(7,9,12)
                {
                        $this->debug("Cron is due this month, getting days array.");
                        $arDays = $this->_getDaysArray($this->month, $this->year);
 
                        do
                        {
                                $this->day = array_pop($arDays);
                        }
                        while ($this->day > $this->now[2]);
 
                        if ($this->day === NULL)
                        {
                                $this->debug("Smallest day is even greater than today");
                                $this->_prevMonth($arMonths);
                        }
                        elseif ($this->day == $this->now[2])
                        {
                                $this->debug("Due to run today");
                                $arHours = $this->_getHoursArray();
 
                                do
                                {
                                        $this->hour = array_pop($arHours);
                                }
                                while ($this->hour > $this->now[1]);
 
                                if ($this->hour === NULL) // now =2, arHours = array(3,5,7)
                                {
                                        $this->debug("Not due this hour and some earlier hours, so go for previous day");
                                        $this->_prevDay($arDays, $arMonths);
                                }
                                elseif ($this->hour < $this->now[1]) //now =2, arHours = array(1,3,5)
                                {
                                        $this->minute = $this->_getLastMinute();
                                }
                                else // now =2, arHours = array(1,2,5)
                                {
                                        $this->debug("Due this hour");
                                        $arMinutes = $this->_getMinutesArray();
                                        do
                                        {
                                                $this->minute = array_pop($arMinutes);
                                        }
                                        while ($this->minute > $this->now[0]);
 
                                        if ($this->minute === NULL)
                                        {
                                                $this->debug("Not due this minute, so go for previous hour.");
                                                $this->_prevHour($arHours, $arDays, $arMonths);
                                        }
                                        else
                                        {
                                                $this->debug("Due this very minute or some earlier minutes before this moment within this hour.");
                                        }
                                }
                        }
                        else
                        {
                                $this->debug("Cron was due on " . $this->day . " of this month");
                                $this->hour = $this->_getLastHour();
                                $this->minute = $this->_getLastMinute();
                        }
                }
                else //now Sep, arrMonths=array(7, 10)
                {
                        $this->debug("Cron was due before this month. Previous month is: " . $this->year . '-' . $this->month);
                        $this->day = $this->_getLastDay($this->month, $this->year);
                        if ($this->day === NULL)
                        {
                                //No scheduled date within this month. So we will try the previous month in the month array
                                $this->_prevMonth($arMonths);
                        }
                        else
                        {
                                $this->hour = $this->_getLastHour();
                                $this->minute = $this->_getLastMinute();
                        }
                }
 
                $tend = microtime();
                $this->taken = $tend - $tstart;
                $this->debug("Parsing $string taken " . $this->taken . " seconds");
 
                //if the last due is beyond 1970
                if ($this->minute === NULL)
                {
                        $this->debug("Error calculating last due time");
                        return false;
        &n