-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron_parser.php
executable file
·2361 lines (1486 loc) · 71 KB
/
cron_parser.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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class CronSchedule
{
// The actual minutes, hours, daysOfMonth, months, daysOfWeek and years selected by the provided cron specification.
private $_minutes = array();
private $_hours = array();
private $_daysOfMonth = array();
private $_months = array();
private $_daysOfWeek = array();
private $_years = array();
// The original cron specification in compiled form.
private $_cronMinutes = array();
private $_cronHours = array();
private $_cronDaysOfMonth = array();
private $_cronMonths = array();
private $_cronDaysOfWeek = array();
private $_cronYears = array();
// The language table
private $_lang = FALSE;
/**
* Minimum and maximum years to cope with the Year 2038 problem in UNIX. We run PHP which most likely runs on a UNIX environment so we
* must assume vulnerability.
*/
protected $RANGE_YEARS_MIN = 1970; // Must match date range supported by date(). See also: http://en.wikipedia.org/wiki/Year_2038_problem
protected $RANGE_YEARS_MAX = 2037; // Must match date range supported by date(). See also: http://en.wikipedia.org/wiki/Year_2038_problem
/**
* Function: __construct
*
* Description: Performs only base initialization, including language initialization.
*
* Parameters: $language The languagecode of the chosen language.
*/
public function __construct($language = 'en')
{
$this->initLang($language);
}
//
// Function: fromCronString
//
// Description: Creates a new Schedule object based on a Cron specification.
//
// Parameters: $cronSpec A string containing a cron specification.
// $language The language to use to create a natural language representation of the string
//
// Result: A new Schedule object. An \Exception is thrown if the specification is invalid.
//
final public static function fromCronString($cronSpec = '* * * * * *', $language = 'en')
{
// Split input liberal. Single or multiple Spaces, Tabs and Newlines are all allowed as separators.
if(count($elements = preg_split('/\s+/', $cronSpec)) < 5)
throw new Exception('Invalid specification.');
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Named ranges in cron entries
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$arrMonths = array('JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6, 'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' => 10, 'NOV' => 11, 'DEC' => 12);
$arrDaysOfWeek = array('SUN' => 0, 'MON' => 1, 'TUE' => 2, 'WED' => 3, 'THU' => 4, 'FRI' => 5, 'SAT' => 6);
// Translate the cron specification into arrays that hold specifications of the actual dates
$newCron = new CronSchedule($language);
$newCron->_cronMinutes = $newCron->cronInterpret($elements[0], 0, 59, array(), 'minutes');
$newCron->_cronHours = $newCron->cronInterpret($elements[1], 0, 23, array(), 'hours');
$newCron->_cronDaysOfMonth = $newCron->cronInterpret($elements[2], 1, 31, array(), 'daysOfMonth');
$newCron->_cronMonths = $newCron->cronInterpret($elements[3], 1, 12, $arrMonths, 'months');
$newCron->_cronDaysOfWeek = $newCron->cronInterpret($elements[4], 0, 6, $arrDaysOfWeek, 'daysOfWeek');
$newCron->_minutes = $newCron->cronCreateItems($newCron->_cronMinutes);
$newCron->_hours = $newCron->cronCreateItems($newCron->_cronHours);
$newCron->_daysOfMonth = $newCron->cronCreateItems($newCron->_cronDaysOfMonth);
$newCron->_months = $newCron->cronCreateItems($newCron->_cronMonths);
$newCron->_daysOfWeek = $newCron->cronCreateItems($newCron->_cronDaysOfWeek);
if (isset($elements[5])) {
$newCron->_cronYears = $newCron->cronInterpret($elements[5], $newCron->RANGE_YEARS_MIN, $newCron->RANGE_YEARS_MAX, array(), 'years');
$newCron->_years = $newCron->cronCreateItems($newCron->_cronYears);
}
return $newCron;
}
/*
* Function: cronInterpret
*
* Description: Interprets a single field from a cron specification. Throws an \Exception if the specification is in some way invalid.
*
* Parameters: $specification The actual text from the spefication, such as 12-38/3
* $rangeMin The lowest value for specification.
* $rangeMax The highest value for specification
* $namesItems A key/value pair where value is a value between $rangeMin and $rangeMax and key is the name for that value.
* $errorName The name of the category to use in case of an error.
*
* Result: An array with entries, each of which is an array with the following fields:
* 'number1' The first number of the range or the number specified
* 'number2' The second number of the range if a range is specified
* 'hasInterval' TRUE if a range is specified. FALSE otherwise
* 'interval' The interval if a range is specified.
*/
final private function cronInterpret($specification, $rangeMin, $rangeMax, $namedItems, $errorName)
{
if((!is_string($specification)) && (!(is_int($specification))))
throw new Exception('Invalid specification.');
// Multiple values, separated by comma
$specs = array();
$specs['rangeMin'] = $rangeMin;
$specs['rangeMax'] = $rangeMax;
$specs['elements'] = array();
$arrSegments = explode(',', $specification);
foreach($arrSegments as $segment)
{
$hasRange = (($posRange = strpos($segment, '-')) !== FALSE);
$hasInterval = (($posIncrement = strpos($segment, '/')) !== FALSE);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check: Increment without range is invalid
//if(!$hasRange && $hasInterval) throw new \Exception("Invalid Range ($errorName).");
// Check: Increment must be final specification
if($hasRange && $hasInterval)
if($posIncrement < $posRange) throw new \Exception("Invalid order ($errorName).");
// GetSegments
$segmentNumber1 = $segment;
$segmentNumber2 = '';
$segmentIncrement = '';
$intIncrement = 1;
if($hasInterval)
{
$segmentNumber1 = substr($segment, 0, $posIncrement);
$segmentIncrement = substr($segment, $posIncrement + 1);
}
if($hasRange)
{
$segmentNumber2 = substr($segmentNumber1, $posRange + 1);
$segmentNumber1 = substr($segmentNumber1, 0, $posRange);
}
// Get and validate first value in range
if($segmentNumber1 == '*')
{
$intNumber1 = $rangeMin;
$intNumber2 = $rangeMax;
$hasRange = TRUE;
}
else
{
if(array_key_exists(strtoupper($segmentNumber1), $namedItems)) $segmentNumber1 = $namedItems[strtoupper($segmentNumber1)];
if(((string) ($intNumber1 = (int) $segmentNumber1)) != $segmentNumber1) throw new \Exception("Invalid symbol ($errorName).");
if(($intNumber1 < $rangeMin) || ($intNumber1 > $rangeMax)) throw new \Exception("Out of bounds ($errorName).");
// Get and validate second value in range
if($hasRange)
{
if(array_key_exists(strtoupper($segmentNumber2), $namedItems)) $segmentNumber2 = $namedItems[strtoupper($segmentNumber2)];
if(((string) ($intNumber2 = (int) $segmentNumber2)) != $segmentNumber2) throw new \Exception("Invalid symbol ($errorName).");
if(($intNumber2 < $rangeMin) || ($intNumber2 > $rangeMax)) throw new \Exception("Out of bounds ($errorName).");
if($intNumber1 > $intNumber2) throw new \Exception("Invalid range ($errorName).");
}
}
// Get and validate increment
if($hasInterval)
{
if(($intIncrement = (int) $segmentIncrement) != $segmentIncrement) throw new \Exception("Invalid symbol ($errorName).");
if($intIncrement < 1) throw new \Exception("Out of bounds ($errorName).");
}
// Apply range and increment
$elem = array();
$elem['number1'] = $intNumber1;
$elem['hasInterval'] = $hasRange;
if($hasRange)
{
$elem['number2'] = $intNumber2;
$elem['interval'] = $intIncrement;
}
$specs['elements'][] = $elem;
}
return $specs;
}
//
// Function: cronCreateItems
//
// Description: Uses the interpreted cron specification of a single item from a cron specification to create an array with keys that match the
// selected items.
//
// Parameters: $cronInterpreted The interpreted specification
//
// Result: An array where each key identifies a matching entry. E.g. the cron specification */10 for minutes will yield an array
// [0] => 1
// [10] => 1
// [20] => 1
// [30] => 1
// [40] => 1
// [50] => 1
//
final private function cronCreateItems($cronInterpreted)
{
$items = array();
foreach($cronInterpreted['elements'] as $elem)
{
if(!$elem['hasInterval'])
$items[$elem['number1']] = TRUE;
else
for($number = $elem['number1']; $number <= $elem['number2']; $number += $elem['interval'])
$items[$number] = TRUE;
}
ksort($items);
return $items;
}
//
// Function: dtFromParameters
//
// Description: Transforms a flexible parameter passing of a datetime specification into an internally used array.
//
// Parameters: $time If a string interpreted as a datetime string in the YYYY-MM-DD HH:II format and other parameters ignored.
// If an array $minute, $hour, $day, $month and $year are passed as keys 0-4 and other parameters ignored.
// If a string, interpreted as unix time.
// If omitted or specified FALSE, defaults to the current time.
//
// Result: An array with indices 0-4 holding the actual interpreted values for $minute, $hour, $day, $month and $year.
//
final private function dtFromParameters($time = FALSE)
{
if($time === FALSE)
{
$arrTime = getDate();
return array($arrTime['minutes'], $arrTime['hours'], $arrTime['mday'], $arrTime['mon'], $arrTime['year']);
}
elseif(is_array($time))
return $time;
elseif(is_string($time))
{
$arrTime = getDate(strtotime($time));
return array($arrTime['minutes'], $arrTime['hours'], $arrTime['mday'], $arrTime['mon'], $arrTime['year']);
}elseif(is_int($time))
{
$arrTime = getDate($time);
return array($arrTime['minutes'], $arrTime['hours'], $arrTime['mday'], $arrTime['mon'], $arrTime['year']);
}
}
final private function dtAsString($arrDt)
{
if($arrDt === FALSE)
return FALSE;
return $arrDt[4].'-'.(strlen($arrDt[3]) == 1 ? '0' : '').$arrDt[3].'-'.(strlen($arrDt[2]) == 1 ? '0' : '').$arrDt[2].' '.(strlen($arrDt[1]) == 1 ? '0' : '').$arrDt[1].':'.(strlen($arrDt[0]) == 1 ? '0' : '').$arrDt[0].':00';
}
//
// Function: match
//
// Description: Returns TRUE if the specified date and time corresponds to a scheduled point in time. FALSE otherwise.
//
// Parameters: $time If a string interpreted as a datetime string in the YYYY-MM-DD HH:II format and other parameters ignored.
// If an array $minute, $hour, $day, $month and $year are passed as keys 0-4 and other parameters ignored.
// If a string, interpreted as unix time.
// If omitted or specified FALSE, defaults to the current time.
//
// Result: TRUE if the schedule matches the specified datetime. FALSE otherwise.
//
final public function match($time = FALSE)
{
// Convert parameters to array datetime
$arrDT = $this->dtFromParameters($time);
// Verify match
// Years
if(!array_key_exists($arrDT[4], $this->_years)) return FALSE;
// Day of week
if(!array_key_exists(date('w', strtotime($arrDT[4].'-'.$arrDT[3].'-'.$arrDT[2])), $this->_daysOfWeek)) return FALSE;
// Month
if(!array_key_exists($arrDT[3], $this->_months)) return FALSE;
// Day of month
if(!array_key_exists($arrDT[2], $this->_daysOfMonth)) return FALSE;
// Hours
if(!array_key_exists($arrDT[1], $this->_hours)) return FALSE;
// Minutes
if(!array_key_exists($arrDT[0], $this->_minutes)) return FALSE;
return TRUE;
}
//
// Function:
//
// Description:
//
// Parameters:
//
// Result:
//
final private function getClass($spec)
{
if(!$this->classIsSpecified($spec)) return '0';
if($this->classIsSingleFixed($spec)) return '1';
return '2';
}
//
// Function:
//
// Description: Returns TRUE if the Cron Specification is specified. FALSE otherwise. This is true if the specification has more than one entry
// or is anything than the entire approved range ("*").
//
// Parameters:
//
// Result:
//
final private function classIsSpecified($spec)
{
if($spec['elements'][0]['hasInterval'] == FALSE) return TRUE;
if($spec['elements'][0]['number1'] != $spec['rangeMin']) return TRUE;
if($spec['elements'][0]['number2'] != $spec['rangeMax']) return TRUE;
if($spec['elements'][0]['interval'] != 1) return TRUE;
return FALSE;
}
//
// Function:
//
// Description: Returns TRUE if the Cron Specification is specified as a single value. FALSE otherwise. This is true only if there is only
// one entry and the entry is only a single number (e.g. "10")
//
// Parameters:
//
// Result:
//
final private function classIsSingleFixed($spec)
{
return (count($spec['elements']) == 1) && (!$spec['elements'][0]['hasInterval']);
}
final private function initLang($language = 'en')
{
switch($language)
{
case 'en':
$this->_lang['elemMin: at_the_hour'] = 'at the hour';
$this->_lang['elemMin: after_the_hour_every_X_minute'] = 'every minute';
$this->_lang['elemMin: after_the_hour_every_X_minute_plural'] = 'every @1 minutes';
$this->_lang['elemMin: every_consecutive_minute'] = 'every consecutive minute';
$this->_lang['elemMin: every_consecutive_minute_plural'] = 'every consecutive @1 minutes';
$this->_lang['elemMin: every_minute'] = 'every minute';
$this->_lang['elemMin: between_X_and_Y'] = 'from the @1 to the @2';
$this->_lang['elemMin: at_X:Y'] = 'At @1:@2';
$this->_lang['elemHour: past_X:00'] = 'past @1:00';
$this->_lang['elemHour: between_X:00_and_Y:59'] = 'between @1:00 and @2:59';
$this->_lang['elemHour: in_the_60_minutes_past_'] = 'in the 60 minutes past every consecutive hour';
$this->_lang['elemHour: in_the_60_minutes_past__plural'] = 'in the 60 minutes past every consecutive @1 hours';
$this->_lang['elemHour: past_every_consecutive_'] = 'past every consecutive hour';
$this->_lang['elemHour: past_every_consecutive__plural'] = 'past every consecutive @1 hours';
$this->_lang['elemHour: past_every_hour'] = 'past every hour';
$this->_lang['elemDOM: the_X'] = 'the @1';
$this->_lang['elemDOM: every_consecutive_day'] = 'every consecutive day';
$this->_lang['elemDOM: every_consecutive_day_plural'] = 'every consecutive @1 days';
$this->_lang['elemDOM: on_every_day'] = 'on every day';
$this->_lang['elemDOM: between_the_Xth_and_Yth'] = 'between the @1 and the @2';
$this->_lang['elemDOM: on_the_X'] = 'on the @1';
$this->_lang['elemDOM: on_X'] = 'on @1';
$this->_lang['elemMonth: every_X'] = 'every @1';
$this->_lang['elemMonth: every_consecutive_month'] = 'every consecutive month';
$this->_lang['elemMonth: every_consecutive_month_plural'] = 'every consecutive @1 months';
$this->_lang['elemMonth: between_X_and_Y'] = 'from @1 to @2';
$this->_lang['elemMonth: of_every_month'] = 'of every month';
$this->_lang['elemMonth: during_every_X'] = 'during every @1';
$this->_lang['elemMonth: during_X'] = 'during @1';
$this->_lang['elemYear: in_X'] = 'in @1';
$this->_lang['elemYear: every_consecutive_year'] = 'every consecutive year';
$this->_lang['elemYear: every_consecutive_year_plural'] = 'every consecutive @1 years';
$this->_lang['elemYear: from_X_through_Y'] = 'from @1 through @2';
$this->_lang['elemDOW: on_every_day'] = 'on every day';
$this->_lang['elemDOW: on_X'] = 'on @1';
$this->_lang['elemDOW: but_only_on_X'] = 'but only if the event takes place on @1';
$this->_lang['separator_and'] = 'and';
$this->_lang['separator_or'] = 'or';
$this->_lang['day: 0_plural'] = 'Sundays';
$this->_lang['day: 1_plural'] = 'Mondays';
$this->_lang['day: 2_plural'] = 'Tuesdays';
$this->_lang['day: 3_plural'] = 'Wednesdays';
$this->_lang['day: 4_plural'] = 'Thursdays';
$this->_lang['day: 5_plural'] = 'Fridays';
$this->_lang['day: 6_plural'] = 'Saturdays';
$this->_lang['month: 1'] = 'January';
$this->_lang['month: 2'] = 'February';
$this->_lang['month: 3'] = 'March';
$this->_lang['month: 4'] = 'April';
$this->_lang['month: 5'] = 'May';
$this->_lang['month: 6'] = 'June';
$this->_lang['month: 7'] = 'July';
$this->_lang['month: 8'] = 'Augustus';
$this->_lang['month: 9'] = 'September';
$this->_lang['month: 10'] = 'October';
$this->_lang['month: 11'] = 'November';
$this->_lang['month: 12'] = 'December';
$this->_lang['ordinal: 1'] = '1st';
$this->_lang['ordinal: 2'] = '2nd';
$this->_lang['ordinal: 3'] = '3rd';
$this->_lang['ordinal: 4'] = '4th';
$this->_lang['ordinal: 5'] = '5th';
$this->_lang['ordinal: 6'] = '6th';
$this->_lang['ordinal: 7'] = '7th';
$this->_lang['ordinal: 8'] = '8th';
$this->_lang['ordinal: 9'] = '9th';
$this->_lang['ordinal: 10'] = '10th';
$this->_lang['ordinal: 11'] = '11th';
$this->_lang['ordinal: 12'] = '12th';
$this->_lang['ordinal: 13'] = '13th';
$this->_lang['ordinal: 14'] = '14th';
$this->_lang['ordinal: 15'] = '15th';
$this->_lang['ordinal: 16'] = '16th';
$this->_lang['ordinal: 17'] = '17th';
$this->_lang['ordinal: 18'] = '18th';
$this->_lang['ordinal: 19'] = '19th';
$this->_lang['ordinal: 20'] = '20th';
$this->_lang['ordinal: 21'] = '21st';
$this->_lang['ordinal: 22'] = '22nd';
$this->_lang['ordinal: 23'] = '23rd';
$this->_lang['ordinal: 24'] = '24th';
$this->_lang['ordinal: 25'] = '25th';
$this->_lang['ordinal: 26'] = '26th';
$this->_lang['ordinal: 27'] = '27th';
$this->_lang['ordinal: 28'] = '28th';
$this->_lang['ordinal: 29'] = '29th';
$this->_lang['ordinal: 30'] = '30th';
$this->_lang['ordinal: 31'] = '31st';
$this->_lang['ordinal: 32'] = '32nd';
$this->_lang['ordinal: 33'] = '33rd';
$this->_lang['ordinal: 34'] = '34th';
$this->_lang['ordinal: 35'] = '35th';
$this->_lang['ordinal: 36'] = '36th';
$this->_lang['ordinal: 37'] = '37th';
$this->_lang['ordinal: 38'] = '38th';
$this->_lang['ordinal: 39'] = '39th';
$this->_lang['ordinal: 40'] = '40th';
$this->_lang['ordinal: 41'] = '41st';
$this->_lang['ordinal: 42'] = '42nd';
$this->_lang['ordinal: 43'] = '43rd';
$this->_lang['ordinal: 44'] = '44th';
$this->_lang['ordinal: 45'] = '45th';
$this->_lang['ordinal: 46'] = '46th';
$this->_lang['ordinal: 47'] = '47th';
$this->_lang['ordinal: 48'] = '48th';
$this->_lang['ordinal: 49'] = '49th';
$this->_lang['ordinal: 50'] = '50th';
$this->_lang['ordinal: 51'] = '51st';
$this->_lang['ordinal: 52'] = '52nd';
$this->_lang['ordinal: 53'] = '53rd';
$this->_lang['ordinal: 54'] = '54th';
$this->_lang['ordinal: 55'] = '55th';
$this->_lang['ordinal: 56'] = '56th';
$this->_lang['ordinal: 57'] = '57th';
$this->_lang['ordinal: 58'] = '58th';
$this->_lang['ordinal: 59'] = '59th';
break;
case 'nl':
$this->_lang['elemMin: at_the_hour'] = 'op het hele uur';
$this->_lang['elemMin: after_the_hour_every_X_minute'] = 'elke minuut';
$this->_lang['elemMin: after_the_hour_every_X_minute_plural'] = 'elke @1 minuten';
$this->_lang['elemMin: every_consecutive_minute'] = 'elke opeenvolgende minuut';
$this->_lang['elemMin: every_consecutive_minute_plural'] = 'elke opeenvolgende @1 minuten';
$this->_lang['elemMin: every_minute'] = 'elke minuut';
$this->_lang['elemMin: between_X_and_Y'] = 'van de @1 tot en met de @2';
$this->_lang['elemMin: at_X:Y'] = 'Om @1:@2';
$this->_lang['elemHour: past_X:00'] = 'na @1:00';
$this->_lang['elemHour: between_X:00_and_Y:59'] = 'tussen @1:00 en @2:59';
$this->_lang['elemHour: in_the_60_minutes_past_'] = 'in de 60 minuten na elk opeenvolgend uur';
$this->_lang['elemHour: in_the_60_minutes_past__plural'] = 'in de 60 minuten na elke opeenvolgende @1 uren';
$this->_lang['elemHour: past_every_consecutive_'] = 'na elk opeenvolgend uur';
$this->_lang['elemHour: past_every_consecutive__plural'] = 'na elke opeenvolgende @1 uren';
$this->_lang['elemHour: past_every_hour'] = 'na elk uur';
$this->_lang['elemDOM: the_X'] = 'de @1';
$this->_lang['elemDOM: every_consecutive_day'] = 'elke opeenvolgende dag';
$this->_lang['elemDOM: every_consecutive_day_plural'] = 'elke opeenvolgende @1 dagen';
$this->_lang['elemDOM: on_every_day'] = 'op elke dag';
$this->_lang['elemDOM: between_the_Xth_and_Yth'] = 'tussen de @1 en de @2';
$this->_lang['elemDOM: on_the_X'] = 'op de @1';
$this->_lang['elemDOM: on_X'] = 'op @1';
$this->_lang['elemMonth: every_X'] = 'elke @1';
$this->_lang['elemMonth: every_consecutive_month'] = 'elke opeenvolgende maand';
$this->_lang['elemMonth: every_consecutive_month_plural'] = 'elke opeenvolgende @1 maanden';
$this->_lang['elemMonth: between_X_and_Y'] = 'van @1 tot @2';
$this->_lang['elemMonth: of_every_month'] = 'van elke maand';
$this->_lang['elemMonth: during_every_X'] = 'tijdens elke @1';
$this->_lang['elemMonth: during_X'] = 'tijdens @1';
$this->_lang['elemYear: in_X'] = 'in @1';
$this->_lang['elemYear: every_consecutive_year'] = 'elk opeenvolgend jaar';
$this->_lang['elemYear: every_consecutive_year_plural'] = 'elke opeenvolgende @1 jaren';
$this->_lang['elemYear: from_X_through_Y'] = 'van @1 tot en met @2';
$this->_lang['elemDOW: on_every_day'] = 'op elke dag';
$this->_lang['elemDOW: on_X'] = 'op @1';
$this->_lang['elemDOW: but_only_on_X'] = 'maar alleen als het plaatsvindt op @1';
$this->_lang['separator_and'] = 'en';
$this->_lang['separator_of'] = 'of';
$this->_lang['day: 0_plural'] = 'zondagen';
$this->_lang['day: 1_plural'] = 'maandagen';
$this->_lang['day: 2_plural'] = 'dinsdagen';
$this->_lang['day: 3_plural'] = 'woensdagen';
$this->_lang['day: 4_plural'] = 'donderdagen';
$this->_lang['day: 5_plural'] = 'vrijdagen';
$this->_lang['day: 6_plural'] = 'zaterdagen';
$this->_lang['month: 1'] = 'januari';
$this->_lang['month: 2'] = 'februari';
$this->_lang['month: 3'] = 'maart';
$this->_lang['month: 4'] = 'april';
$this->_lang['month: 5'] = 'mei';
$this->_lang['month: 6'] = 'juni';
$this->_lang['month: 7'] = 'juli';
$this->_lang['month: 8'] = 'augustus';
$this->_lang['month: 9'] = 'september';
$this->_lang['month: 10'] = 'october';
$this->_lang['month: 11'] = 'november';
$this->_lang['month: 12'] = 'december';
$this->_lang['ordinal: 1'] = '1e';
$this->_lang['ordinal: 2'] = '2e';
$this->_lang['ordinal: 3'] = '3e';
$this->_lang['ordinal: 4'] = '4e';
$this->_lang['ordinal: 5'] = '5e';
$this->_lang['ordinal: 6'] = '6e';
$this->_lang['ordinal: 7'] = '7e';
$this->_lang['ordinal: 8'] = '8e';
$this->_lang['ordinal: 9'] = '9e';
$this->_lang['ordinal: 10'] = '10e';
$this->_lang['ordinal: 11'] = '11e';
$this->_lang['ordinal: 12'] = '12e';
$this->_lang['ordinal: 13'] = '13e';
$this->_lang['ordinal: 14'] = '14e';
$this->_lang['ordinal: 15'] = '15e';
$this->_lang['ordinal: 16'] = '16e';
$this->_lang['ordinal: 17'] = '17e';
$this->_lang['ordinal: 18'] = '18e';
$this->_lang['ordinal: 19'] = '19e';
$this->_lang['ordinal: 20'] = '20e';
$this->_lang['ordinal: 21'] = '21e';
$this->_lang['ordinal: 22'] = '22e';
$this->_lang['ordinal: 23'] = '23e';
$this->_lang['ordinal: 24'] = '24e';
$this->_lang['ordinal: 25'] = '25e';
$this->_lang['ordinal: 26'] = '26e';
$this->_lang['ordinal: 27'] = '27e';
$this->_lang['ordinal: 28'] = '28e';
$this->_lang['ordinal: 29'] = '29e';
$this->_lang['ordinal: 30'] = '30e';
$this->_lang['ordinal: 31'] = '31e';
$this->_lang['ordinal: 32'] = '32e';
$this->_lang['ordinal: 33'] = '33e';
$this->_lang['ordinal: 34'] = '34e';
$this->_lang['ordinal: 35'] = '35e';
$this->_lang['ordinal: 36'] = '36e';
$this->_lang['ordinal: 37'] = '37e';
$this->_lang['ordinal: 38'] = '38e';
$this->_lang['ordinal: 39'] = '39e';
$this->_lang['ordinal: 40'] = '40e';
$this->_lang['ordinal: 41'] = '41e';
$this->_lang['ordinal: 42'] = '42e';
$this->_lang['ordinal: 43'] = '43e';
$this->_lang['ordinal: 44'] = '44e';
$this->_lang['ordinal: 45'] = '45e';
$this->_lang['ordinal: 46'] = '46e';
$this->_lang['ordinal: 47'] = '47e';
$this->_lang['ordinal: 48'] = '48e';
$this->_lang['ordinal: 49'] = '49e';
$this->_lang['ordinal: 50'] = '50e';
$this->_lang['ordinal: 51'] = '51e';
$this->_lang['ordinal: 52'] = '52e';
$this->_lang['ordinal: 53'] = '53e';
$this->_lang['ordinal: 54'] = '54e';
$this->_lang['ordinal: 55'] = '55e';
$this->_lang['ordinal: 56'] = '56e';
$this->_lang['ordinal: 57'] = '57e';
$this->_lang['ordinal: 58'] = '58e';
$this->_lang['ordinal: 59'] = '59e';
break;
}
}
final private function natlangPad2($number)
{
return (strlen($number) == 1 ? '0' : '').$number;
}
final private function natlangApply($id, $p1 = FALSE, $p2 = FALSE, $p3 = FALSE, $p4 = FALSE, $p5 = FALSE, $p6 = FALSE)
{
$txt = $this->_lang[$id];
if($p1 !== FALSE) $txt = str_replace('@1', $p1, $txt);
if($p2 !== FALSE) $txt = str_replace('@2', $p2, $txt);
if($p3 !== FALSE) $txt = str_replace('@3', $p3, $txt);
if($p4 !== FALSE) $txt = str_replace('@4', $p4, $txt);
if($p5 !== FALSE) $txt = str_replace('@5', $p5, $txt);
if($p6 !== FALSE) $txt = str_replace('@6', $p6, $txt);
return $txt;
}
//
// Function: natlangRange
//
// Description: Converts a range into natural language
//
// Parameters:
//
// Result:
//
final private function natlangRange($spec, $entryFunction, $p1 = FALSE)
{
$arrIntervals = array();
foreach($spec['elements'] as $elem)
$arrIntervals[] = call_user_func($entryFunction, $elem, $p1);
$txt = "";
for($index = 0; $index < count($arrIntervals); $index++)
$txt .= ($index == 0 ? '' : ($index == (count($arrIntervals) - 1) ? ' '.$this->natlangApply('separator_and').' ' : ', ')).$arrIntervals[$index];
return $txt;
}
//
// Function: natlangElementMinute
//
// Description: Converts an entry from the minute specification to natural language.
//
final private function natlangElementMinute($elem)
{
if(!$elem['hasInterval'])
{
if($elem['number1'] == 0) return $this->natlangApply('elemMin: at_the_hour');
else return $this->natlangApply('elemMin: after_the_hour_every_X_minute'.($elem['number1'] == 1 ? '' : '_plural'), $elem['number1']);
}
$txt = $this->natlangApply('elemMin: every_consecutive_minute'.($elem['interval'] == 1 ? '' : '_plural'), $elem['interval']);
if(($elem['number1'] != $this->_cronMinutes['rangeMin']) || ($elem['number2'] != $this->_cronMinutes['rangeMax']))
$txt .= ' ('.$this->natlangApply('elemMin: between_X_and_Y', $this->natlangApply('ordinal: '.$elem['number1']), $this->natlangApply('ordinal: '.$elem['number2'])).')';
return $txt;
}
//
// Function: natlangElementHour
//
// Description: Converts an entry from the hour specification to natural language.
//
final private function natlangElementHour($elem, $asBetween)
{
if(!$elem['hasInterval'])
{
if($asBetween) return $this->natlangApply('elemHour: between_X:00_and_Y:59', $this->natlangPad2($elem['number1']), $this->natlangPad2($elem['number1']));
else return $this->natlangApply('elemHour: past_X:00', $this->natlangPad2($elem['number1']));
}
if($asBetween) $txt = $this->natlangApply('elemHour: in_the_60_minutes_past_'.($elem['interval'] == 1 ? '' : '_plural'), $elem['interval']);
else $txt = $this->natlangApply('elemHour: past_every_consecutive_'.($elem['interval'] == 1 ? '' : '_plural'), $elem['interval']);
if(($elem['number1'] != $this->_cronHours['rangeMin']) || ($elem['number2'] != $this->_cronHours['rangeMax']))
$txt .= ' ('.$this->natlangApply('elemHour: between_X:00_and_Y:59', $elem['number1'], $elem['number2']).')';
return $txt;
}
//
// Function: natlangElementDayOfMonth
//
// Description: Converts an entry from the day of month specification to natural language.
//
final private function natlangElementDayOfMonth($elem)
{
if(!$elem['hasInterval'])
return $this->natlangApply('elemDOM: the_X', $this->natlangApply('ordinal: '.$elem['number1']));
$txt = $this->natlangApply('elemDOM: every_consecutive_day'.($elem['interval'] == 1 ? '' : '_plural'), $elem['interval']);
if(($elem['number1'] != $this->_cronHours['rangeMin']) || ($elem['number2'] != $this->_cronHours['rangeMax']))
$txt .= ' ('.$this->natlangApply('elemDOM: between_the_Xth_and_Yth', $this->natlangApply('ordinal: '.$elem['number1']), $this->natlangApply('ordinal: '.$elem['number2'])).')';
return $txt;
}
//
// Function: natlangElementDayOfMonth
//
// Description: Converts an entry from the month specification to natural language.
//
final private function natlangElementMonth($elem)
{
if(!$elem['hasInterval'])
return $this->natlangApply('elemMonth: every_X', $this->natlangApply('month: '.$elem['number1']));
$txt = $this->natlangApply('elemMonth: every_consecutive_month'.($elem['interval'] == 1 ? '' : '_plural'), $elem['interval']);
if(($elem['number1'] != $this->_cronMonths['rangeMin']) || ($elem['number2'] != $this->_cronMonths['rangeMax']))
$txt .= ' ('.$this->natlangApply('elemMonth: between_X_and_Y', $this->natlangApply('month: '.$elem['number1']), $this->natlangApply('month: '.$elem['number2'])).')';
return $txt;
}
//
// Function: natlangElementYear
//
// Description: Converts an entry from the year specification to natural language.
//
final private function natlangElementYear($elem)
{
if(!$elem['hasInterval'])
return $elem['number1'];
$txt = $this->natlangApply('elemYear: every_consecutive_year'.($elem['interval'] == 1 ? '' : '_plural'), $elem['interval']);
if(($elem['number1'] != $this->_cronMonths['rangeMin']) || ($elem['number2'] != $this->_cronMonths['rangeMax']))
$txt .= ' ('.$this->natlangApply('elemYear: from_X_through_Y', $elem['number1'], $elem['number2']).')';
return $txt;
}
final public function asNaturalLanguage()
{
$switchForceDateExplaination = FALSE;
$switchDaysOfWeekAreExcluding = TRUE;
// Generate Time String
$txtMinutes = array();
$txtMinutes[0] = $this->natlangApply('elemMin: every_minute');
$txtMinutes[1] = $this->natlangElementMinute($this->_cronMinutes['elements'][0]);
$txtMinutes[2] = $this->natlangRange($this->_cronMinutes, array($this, 'natlangElementMinute'));
$txtHours = array();
$txtHours[0] = $this->natlangApply('elemHour: past_every_hour');
$txtHours[1] = array();
$txtHours[1]['between'] = $this->natlangRange($this->_cronHours, array($this, 'natlangElementHour'), TRUE);
$txtHours[1]['past'] = $this->natlangRange($this->_cronHours, array($this, 'natlangElementHour'), FALSE);
$txtHours[2] = array();
$txtHours[2]['between'] = $this->natlangRange($this->_cronHours, array($this, 'natlangElementHour'), TRUE);
$txtHours[2]['past'] = $this->natlangRange($this->_cronHours, array($this, 'natlangElementHour'), FALSE);
$classMinutes = $this->getClass($this->_cronMinutes);
$classHours = $this->getClass($this->_cronHours);
switch($classMinutes.$classHours)
{
// Special case: Unspecified date + Unspecified month
//
// Rule: The language for unspecified fields is omitted if a more detailed field has already been explained.
//
// The minutes field always yields an explaination, at the very least in the form of 'every minute'. This rule states that if the
// hour is not specified, it can be omitted because 'every minute' is already sufficiently clear.
//
case '00':
$txtTime = $txtMinutes[0];
break;
// Special case: Fixed minutes and fixed hours
//
// The default writing would be something like 'every 20 minutes past 04:00', but the more common phrasing would be: At 04:20.
//
// We will switch ForceDateExplaination on, so that even a non-specified date yields an explaination (e.g. 'every day')
//
case '11':
$txtTime = $this->natlangApply('elemMin: at_X:Y', $this->natlangPad2($this->_cronHours['elements'][0]['number1']), $this->natlangPad2($this->_cronMinutes['elements'][0]['number1']));
$switchForceDateExplaination = TRUE;
break;
// Special case: Between :00 and :59
//
// If hours are specified, but minutes are not, then the minutes string will yield something like 'every minute'. We must the
// differentiate the hour specification because the minutes specification does not relate to all minutes past the hour, but only to
// those minutes between :00 and :59
//
// We will switch ForceDateExplaination on, so that even a non-specified date yields an explaination (e.g. 'every day')
//
case '01':
case '02':
$txtTime = $txtMinutes[$classMinutes].' '.$txtHours[$classHours]['between'];
$switchForceDateExplaination = TRUE;
break;
// Special case: Past the hour
//
// If minutes are specified and hours are specified, then the specification of minutes is always limited to a maximum of 60 minutes
// and always applies to the minutes 'past the hour'.
//
// We will switch ForceDateExplaination on, so that even a non-specified date yields an explaination (e.g. 'every day')
//
case '12':
case '22':
case '21':
$txtTime = $txtMinutes[$classMinutes].' '.$txtHours[$classHours]['past'];
$switchForceDateExplaination = TRUE;
break;
default:
$txtTime = $txtMinutes[$classMinutes].' '.$txtHours[$classHours];
break;
}
// Generate Date String
$txtDaysOfMonth = array();
$txtDaysOfMonth[0] = '';
$txtDaysOfMonth[1] = $this->natlangApply('elemDOM: on_the_X', $this->natlangApply('ordinal: '.$this->_cronDaysOfMonth['elements'][0]['number1']));
$txtDaysOfMonth[2] = $this->natlangApply('elemDOM: on_X', $this->natlangRange($this->_cronDaysOfMonth, array($this, 'natlangElementDayOfMonth')));
$txtMonths = array();
$txtMonths[0] = $this->natlangApply('elemMonth: of_every_month');
$txtMonths[1] = $this->natlangApply('elemMonth: during_every_X', $this->natlangApply('month: '.$this->_cronMonths['elements'][0]['number1']));
$txtMonths[2] = $this->natlangApply('elemMonth: during_X', $this->natlangRange($this->_cronMonths, array($this, 'natlangElementMonth')));
$classDaysOfMonth = $this->getClass($this->_cronDaysOfMonth);
$classMonths = $this->getClass($this->_cronMonths);
if($classDaysOfMonth == '0')
$switchDaysOfWeekAreExcluding = FALSE;
switch($classDaysOfMonth.$classMonths)
{
// Special case: Unspecified date + Unspecified month
//
// Rule: The language for unspecified fields is omitted if a more detailed field has already been explained.
//
// The time fields always yield an explaination, at the very least in the form of 'every minute'. This rule states that if the date
// is not specified, it can be omitted because 'every minute' is already sufficiently clear.
//
// There are some time specifications that do not contain an 'every' reference, but reference a specific time of day. In those cases
// the date explaination is enforced.
//
case '00':
$txtDate = '';
break;
default:
$txtDate = ' '.$txtDaysOfMonth[$classDaysOfMonth].' '.$txtMonths[$classMonths];
break;
}
// Generate Year String
if ($this->_cronYears) {
$txtYears = array();
$txtYears[0] = '';
$txtYears[1] = ' '.$this->natlangApply('elemYear: in_X', $this->_cronYears['elements'][0]['number1']);
$txtYears[2] = ' '.$this->natlangApply('elemYear: in_X', $this->natlangRange($this->_cronYears, array($this, 'natlangElementYear')));
$classYears = $this->getClass($this->_cronYears);
$txtYear = $txtYears[$classYears];
}
// Generate DaysOfWeek String
$collectDays = 0;
foreach($this->_cronDaysOfWeek['elements'] as $elem)
{
if($elem['hasInterval'])
for($x = $elem['number1']; $x <= $elem['number2']; $x += $elem['interval'])
$collectDays |= pow(2, $x);
else
$collectDays |= pow(2, $elem['number1']);
}
if($collectDays == 127) // * all days
{
if(!$switchDaysOfWeekAreExcluding)
$txtDays = ' '.$this->natlangApply('elemDOM: on_every_day');
else
$txtDays = '';
}
else
{
$arrDays = array();
for($x = 0; $x <= 6; $x++)
if($collectDays & pow(2, $x))
$arrDays[] = $x;
$txtDays = '';
for($index = 0; $index < count($arrDays); $index++)
$txtDays .= ($index == 0 ? '' : ($index == (count($arrDays) - 1) ? ' '.$this->natlangApply($switchDaysOfWeekAreExcluding ? 'separator_or' : 'separator_and').' ' : ', ')).$this->natlangApply('day: '.$arrDays[$index].'_plural');
if($switchDaysOfWeekAreExcluding) $txtDays = ' '.$this->natlangApply('elemDOW: but_only_on_X', $txtDays);
else $txtDays = ' '.$this->natlangApply('elemDOW: on_X', $txtDays);
}
$txtResult = ucfirst($txtTime).$txtDate.$txtDays;
if (isset($txtYear)) {
if ($switchDaysOfWeekAreExcluding) {
$txtResult = ucfirst($txtTime).$txtDate.$txtYear.$txtDays;
} else {
$txtResult = ucfirst($txtTime).$txtDate.$txtDays.$txtYear;
}
}
return $txtResult.'.';
}
}
class csd_parser
{
/**
* Cron scheduling definition(s) string
*
* @var string
*/
protected $csds;
/**
* Time set constructino
*
* @var int
*/
protected $base_time;
/**
* Array with parsed time data