fogproject  1.5.9
About: FOG is a Linux-based network computer cloning solution for Windows, Mac OSX and various Linux distributions that ties together a few open-source tools with a PHP-based web interface. FOG doesn’t use any boot disks, or CDs; everything is done via TFTP and PXE.
  Fossies Dox: fogproject-1.5.9.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

fogcron.class.php
Go to the documentation of this file.
1 <?php
22 class FOGCron extends FOGBase
23 {
32  private static function _fit($str, $num)
33  {
34  if (strpos($str, ',')) {
35  $arr = explode(',', $str);
36  $test = array();
37  foreach ((array)$arr as &$ar) {
38  $test[] = (bool)self::_fit($ar, $num);
39  unset($ar);
40  }
41  return in_array(true, $test, true);
42  }
43  if (strpos($str, '-')) {
44  list($low, $high) = explode('-', $str);
45  return (bool)((int)$num >= (int)$low) && ((int)$num <= (int)$high);
46  }
47  if (strpos($str, '/')) {
48  list($pre, $pos) = explode('/', $str);
49  if ($pre == '*') {
50  return ((int)$num % (int)$pos == 0);
51  }
52  return ((int)$num % (int)$pos == (int)$pre);
53  }
54  return (bool)($str == $num);
55  }
64  public static function parse($cron, $lastrun = false)
65  {
66  list(
67  $min,
68  $hour,
69  $dom,
70  $month,
71  $dow
72  ) = array_map('trim', preg_split('/\s+/', $cron));
73  if (is_numeric($dow) && $dow == 0) {
74  $dow = 7;
75  }
76  $Start = self::niceDate();
77  do {
78  list(
79  $nmin,
80  $nhour,
81  $ndom,
82  $nmonth,
83  $ndow
84  ) = array_map(
85  function ($val) {
86  return intval(trim($val));
87  },
88  preg_split('/\s+/', $Start->format('i H j n N'))
89  );
90  // Fairly straight forward, check our minutes field.
91  if ($min != '*') {
92  if (!self::_fit($min, $nmin)) {
93  $Start->modify(sprintf('%s1 minute', $lastrun ? '-' : '+'));
94  continue;
95  }
96  }
97  // Check our hours field.
98  if ($hour != '*') {
99  if (!self::_fit($hour, $nhour)) {
100  $Start->modify(sprintf('%s1 hour', $lastrun ? '-' : '+'));
101  continue;
102  }
103  }
104  // Here comes tricky part. DOM and DOW checks.
105  // If dow AND dom are set to some value other than * the
106  // typical action is to run if the days are true OR the dow is true.
107  //
108  // For example:
109  // our crontab is:
110  // * * 2-5 * 5 it should run on the 2nd through 5th of the month
111  // AND every friday. If you only have the DOW, it will only run on
112  // that day of the week. (* * * * 5) would only run on fridays.
113  if ($dom != '*' && $dow != '*') {
114  if (!self::_fit($dom, $ndom) && !self::_fit($dow, $ndow)) {
115  $Start->modify(sprintf('%s1 day', $lastrun ? '-' : '+'));
116  continue;
117  }
118  }
119  if ($dow == '*' && $dom != '*') {
120  if (!self::_fit($dom, $ndom)) {
121  $Start->modify(sprintf('%s1 day', $lastrun ? '-' : '+'));
122  continue;
123  }
124  }
125  if ($dow != '*' && $dom == '*') {
126  if (!self::_fit($dow, $ndow)) {
127  $Start->modify(sprintf('%s1 day', $lastrun ? '-' : '+'));
128  continue;
129  }
130  }
131  if ($month != '*') {
132  if (!self::_fit($month, $nmonth)) {
133  $Start->modify(sprintf('%s1 month', $lastrun ? '-' : '+'));
134  continue;
135  }
136  }
137  return $Start->setTime(
138  $Start->format('H'),
139  $Start->format('i'),
140  0
141  )->getTimestamp();
142  } while (true);
143  }
153  private static function _checkField($field, $min, $max)
154  {
155  $field = trim($field);
156  if ($field != 0 && empty($field)) {
157  return false;
158  }
159  $v = explode(',', $field);
160  foreach ($v as &$vv) {
161  $vvv = explode('/', $vv);
162  $step = !$vvv[1] ? 1 : $vvv[1];
163  $vvvv = explode('-', $vvv[0]);
164  $_min = (
165  count($vvvv) == 2 ?
166  $vvvv[0] :
167  (
168  $vvv[0] == '*' ?
169  $min :
170  $vvv[0]
171  )
172  );
173  $_max = (
174  count($vvvv) == 2 ?
175  $vvvv[1] :
176  (
177  $vvv[0] == '*' ?
178  $max :
179  $vvv[0]
180  )
181  );
182  $res = self::_checkIntValue($step, $min, $max, true);
183  if ($res) {
184  $res = self::_checkIntValue($_min, $min, $max, true);
185  }
186  if ($res) {
187  $res = self::_checkIntValue($_max, $min, $max, true);
188  }
189  unset($vv);
190  }
191  return $res;
192  }
203  private static function _checkIntValue($value, $min, $max, $extremity)
204  {
205  $val = intval($value, 10);
206  if ($value != $val) {
207  return false;
208  }
209  if (!$extremity) {
210  return true;
211  }
212  if ($val >= $min && $val <= $max) {
213  return true;
214  }
215  return false;
216  }
224  public static function checkMinutesField($minutes)
225  {
226  return self::_checkField($minutes, 0, 59);
227  }
235  public static function checkHoursField($hours)
236  {
237  return self::_checkField($hours, 0, 23);
238  }
246  public static function checkDOMField($dom)
247  {
248  return self::_checkField($dom, 1, 31);
249  }
257  public static function checkMonthField($month)
258  {
259  return self::_checkField($month, 1, 12);
260  }
268  public static function checkDOWField($dow)
269  {
270  return self::_checkField($dow, 0, 7);
271  }
279  public static function shouldRunCron($time)
280  {
281  $time = self::niceDate()->setTimestamp($time);
282  $currTime = self::niceDate();
283  return (bool)($time <= $currTime);
284  }
292  public static function buildSpecialCron($id = '')
293  {
294  $specialCrons = array(
295  '' => _('Select a cron type'),
296  'yearly' => sprintf('%s/%s', _('Yearly'), _('Annually')),
297  'monthly' => _('Monthly'),
298  'weekly' => _('Weekly'),
299  'daily' => sprintf('%s/%s', _('Daily'), _('Midnight')),
300  'hourly' => _('Hourly')
301  );
302  ob_start();
303  foreach ($specialCrons as $val => &$name) {
304  echo '<option value="'
305  . $val
306  . '">'
307  . $name
308  . '</option>';
309  unset($name);
310  }
311  $cronOpts = ob_get_clean();
312  return '<select class="form-control specialCrons" name="specialCrons"'
313  . (
314  $id ?
315  ' id="'.$id.'"' :
316  ''
317  )
318  . '>'
319  . $cronOpts
320  . '</select>';
321  }
322 }
FOGBase\niceDate
static niceDate($date='now', $utc=false)
Definition: fogbase.class.php:1105
FOGCron\checkMinutesField
static checkMinutesField($minutes)
Definition: fogcron.class.php:224
FOGCron\checkDOMField
static checkDOMField($dom)
Definition: fogcron.class.php:246
FOGCron\checkMonthField
static checkMonthField($month)
Definition: fogcron.class.php:257
$test
$test
Definition: freespace.php:37
FOGCron\parse
static parse($cron, $lastrun=false)
Definition: fogcron.class.php:64
$val
$val
Definition: check_node_exists.php:25
FOGCron\_checkField
static _checkField($field, $min, $max)
Definition: fogcron.class.php:153
$str
if(! $Image->isValid()) $str
Definition: progress.php:41
FOGCron\checkDOWField
static checkDOWField($dow)
Definition: fogcron.class.php:268
FOGBase
Definition: fogbase.class.php:25
FOGCron\buildSpecialCron
static buildSpecialCron($id='')
Definition: fogcron.class.php:292
FOGCron\shouldRunCron
static shouldRunCron($time)
Definition: fogcron.class.php:279
FOGCron\checkHoursField
static checkHoursField($hours)
Definition: fogcron.class.php:235
FOGCron\_checkIntValue
static _checkIntValue($value, $min, $max, $extremity)
Definition: fogcron.class.php:203
FOGCron\_fit
static _fit($str, $num)
Definition: fogcron.class.php:32
FOGCron
Definition: fogcron.class.php:23