File: cron.php

package info (click to toggle)
horde3 3.1.3-4etch7
  • links: PTS
  • area: main
  • in suites: etch
  • size: 22,876 kB
  • ctags: 18,071
  • sloc: php: 75,151; xml: 2,979; sql: 1,069; makefile: 79; sh: 64
file content (423 lines) | stat: -rw-r--r-- 11,536 bytes parent folder | download | duplicates (2)
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
<?php

require_once 'Horde/Scheduler.php';
require_once 'Horde/String.php';

/**
 * Horde_Scheduler_cron:: Sort of a cron replacement in a PHP cli
 * script.
 *
 * Date Syntax Examples.
 *
 * Remember:
 *   - Whitespace (space, tab, newline) delimited fields
 *   - Single values, sets, ranges, wildcards
 *
 * SECOND   MINUTE              HOUR        DAY     MONTH
 * *        *                   *           *       *       (every second)
 * 0,30     *                   *           *       *       (every 30 seconds)
 * 0        0,10,20,30,40,50    *           *       *       (every 10 minutes)
 * 0        0                   *           *       *       (beginning of every hour)
 * 0        0                   0,6,12,18   *       *       (at midnight, 6am, noon, 6pm)
 * 0        0                   0           1-7&Fri *       (midnight, first Fri of the month)
 * 0        0                   0           1-7!Fri *       (midnight, first Mon-Thu,Sat-Sun of the month)
 *
 *
 * Example usage:
 *
 * @set_time_limit(0);
 * require_once 'Horde/Scheduler.php';
 * $cron = &Horde_Scheduler::singleton('cron');
 *
 * // Run this command every 5 minutes.
 * $cron->addTask('perl somescript.pl', '0 0,5,10,15,20,25,30,35,40,45,50,55 * * *');
 *
 * // Run this command midnight of the first Friday of odd numbered months.
 * $cron->addTask('php -q somescript.php', '0 0 0 1-7&Fri 1,3,5,7,9,11');
 *
 * // Also run this command midnight of the second Thursday and Saturday of the even numbered months.
 * $cron->addTask('php -q somescript.php', '0 0 0 8-15&Thu,8-15&Sat 2,4,6,8,10,12');
 *
 * $cron->run();
 *
 * $Horde: framework/Scheduler/Scheduler/cron.php,v 1.13.10.3 2005/10/18 11:01:26 jan Exp $
 *
 * @author  Ryan Flynn <ryan@ryanflynn.com>
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @package Horde_Scheduler
 */
class Horde_Scheduler_cron extends Horde_Scheduler {

    var $_tasks = array();

    /**
     * Every time a task is added it will get a fresh uid even if
     * immediately removed.
     */
    var $_counter = 1;

    function addTask($cmd, $rules)
    {
        $ds = &new Horde_Scheduler_cronDate($rules);

        $this->_counter++;

        $this->_tasks[] =
            array(
                'uid' => $this->_counter,
                'rules' => $ds,
                'cmd' => $cmd
            );

        return $this->_counter;
    }

    function removeTask($uid)
    {
        $count = count($this->_tasks);
        for ($i = 0; $i < $count; $i++) {
            if ($this->_tasks['uid'] == $uid) {
                $found = $i;
                array_splice($this->_tasks, $i);
                return $i;
            }
        }

        return 0;
    }

    function run()
    {
        if (!count($this->_tasks)) {
            exit("crond: Nothing to schedule; exiting.\n");
        }

        while (true) {
            $t = time();

            // Check each task.
            foreach ($this->_tasks as $task) {
                if ($task['rules']->nowMatches()) {
                    $this->runcmd($task);
                }
            }

            // Wait until the next second.
            while (time() == $t) {
                $this->sleep(100000);
            }
        }
    }

    function runcmd(&$task)
    {
        Horde::logMessage('Horde_Scheduler_Cron::runcmd(): ' . $task['cmd'] . ' run by ' . $task['uid'], __FILE__, __LINE__, PEAR_LOG_INFO);
        return shell_exec($task['cmd']);
    }

}

/**
 * @package Horde_Scheduler
 */
class Horde_Scheduler_cronDate {

    var $legalDays = array('MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN');

    var $sec;
    var $min;
    var $hour;
    var $day;
    var $month;

    function Horde_Scheduler_cronDate($raw)
    {
        $this->parse(String::upper($raw));
    }

    function nowMatches()
    {
        return $this->scheduledAt(time());
    }

    function scheduledAt($ts = null)
    {
        if ($ts === null) {
            $ts = time();
        }
        return ($this->monthMatches($ts) &&
                $this->monthMatches($ts) &&
                $this->dayMatches($ts) &&
                $this->hourMatches($ts) &&
                $this->minMatches($ts) &&
                $this->secMatches($ts));
    }

    function monthMatches($ts)
    {
        if ($this->month == '*') {
            return true;
        }

        $currentmonth = '-' . date('n', $ts) . '-';

        return (bool)strpos($this->month, $currentmonth);
    }

    function dayMatches($ts)
    {
        if (!empty($this->day['value']) && $this->day['value'] == '*') {
            return true;
        }

        $currentdaynum = '-' . date('j', $ts) . '-';
        $currentdaytxt = String::upper(date('D'));

        foreach ($this->day as $day) {
            if (@strpos($day['not'], $currentdaytxt) === false) {
                $v1 = (@strpos($day['value'], $currentdaynum) !== false);
                $v2 = (@strpos($day['and'], $currentdaytxt) !== false);

                if (!empty($day['and']) && ($v1 && $v2)) {
                    return true;
                } elseif (empty($day['and']) && $v1) {
                    return true;
                }
            }
        }

        return false;
    }

    function hourMatches($ts)
    {
        if ($this->hour == '*') {
            return true;
        }

        $currenthour = '-' . date('G', $ts) . '-';

        return (strpos($this->hour, $currenthour) !== false);
    }

    function minMatches($ts)
    {
        if ($this->min == '*') {
            return true;
        }

        $currentmin = '-' . intval(date('i', $ts)) . '-';

        return (strpos($this->min, $currentmin) !== false);
    }

    function secMatches($ts)
    {
        if ($this->sec == '*') {
            return true;
        }

        $currentsec = '-' . intval(date('s', $ts)) . '-';

        return (strpos($this->sec, $currentsec) !== false);
    }

    function parse($str)
    {
        $s = array();

        list($s['sec'], $s['min'], $s['hour'], $s['day'], $s['month']) = preg_split('|[\n\t ]+|', $str);

        foreach ($s as $k => $v) {
            if (strpos($v, '*') !== false) {
                $s[$k] = array('*');
            } elseif (!$this->generallyDecentSyntax($v)) {
                die("Illegal syntax in '$v'\n");
            } else {
                $s[$k] = explode(',', $s[$k]);
            }
        }

        if ($s['sec'][0] == '*') {
            $this->sec = '*';
        } else {
            for ($i = 0; $i < sizeof($s['sec']); $i++) {
                if ($this->isRange($s['sec'][$i])) {
                    $s['sec'][$i] = $this->expandRange($this->rangeVals($s['sec'][$i]));
                }
            }
            $this->sec = '-' . join('-', $s['sec']) . '-';
        }

        if ($s['min'][0] == '*') {
            $this->min = '*';
        } else {
            for ($i = 0; $i < sizeof($s['min']); $i++) {
                if ($this->isRange($s['min'][$i])) {
                    $s['min'][$i] = $this->expandRange($this->rangeVals($s['min'][$i]));
                }
            }
            $this->min = '-' . join('-', $s['min']) . '-';
        }

        if ($s['hour'][0] == '*') {
            $this->hour = '*';
        } else {
            for ($i = 0; $i < sizeof($s['hour']); $i++) {
                if ($this->isRange($s['hour'][$i])) {
                    $s['hour'][$i] = $this->expandRange($this->rangeVals($s['hour'][$i]));
                }
            }
            $this->hour = '-' . join('-', $s['hour']) . '-';
        }

        if ($s['day'][0] == '*') {
            $this->day = '*';
        } else {
            for ($i = 0; $i < sizeof($s['day']); $i++) {
                $tmp = array();
                if (($char = $this->isCond($s['day'][$i])) !== false) {
                    if ($char == '&') {
                        list($tmp['value'], $tmp['and']) = explode($char, $s['day'][$i]);
                        if ($this->isRange($tmp['and'])) {
                            $tmp['and'] = $this->expandRange($this->rangeVals($tmp['and']));
                        }
                    } else {
                        list($tmp['value'], $tmp['not']) = explode($char, $s['day'][$i]);
                        if ($this->isRange($tmp['not'])) {
                            $tmp['not'] = $this->expandRange($this->rangeVals($tmp['not']));
                        }
                    }
                } else {
                    $tmp = array('value' => $s['day'][$i]);
                }

                $s['day'][$i] = $tmp;

                if ($this->isRange($s['day'][$i]['value'])) {
                    $s['day'][$i]['value'] = $this->expandRange($this->rangeVals($s['day'][$i]['value']));
                }
            }

            $this->day = $s['day'];
        }

        if ($s['month'][0] == '*') {
            $this->month = '*';
        } else {
            for ($i = 0; $i < sizeof($s['month']); $i++) {
                if ($this->isRange($s['month'][$i])) {
                    $s['month'][$i] = $this->expandRange($this->rangeVals($s['month'][$i]));
                }
            }
            $this->month = '-' . join('-', $s['month']) . '-';
        }
    }

    function isCond($s)
    {
        if (strpos($s, '&') !== false) {
            return '&';
        } elseif (strpos($s, '!') !== false) {
            return '!';
        } else {
            return false;
        }
    }

    function isRange($s)
    {
        return preg_match('/^\w+\-\w+/', $s);
    }

    function isCondRange($s)
    {
        return (isCond($s) && isRange($s));
    }

    function isCondVal($s)
    {
        return (isCond($s) && !isRange($s));
    }

    function rangeVals($s)
    {
        return explode('-', $s);
    }

    function expandRange($l, $h = '')
    {
        // Expand range from 1-5 -> '-1-2-3-4-5-'.
        if (is_array($l)) {
            $h = $l[1];
            $l = $l[0];
        }

        if ($this->isDigit($l)) {
            if (!$this->isDigit($h)) {
                die("Invalid value '$h' in range '$l-$h'");
            }

            // Currently there is no possible reason to need to do a
            // range beyond 0-59 for anything.
            if ($l < 0) {
                $l = 0;
            } elseif ($l > 59) {
                $l = 59;
            }

            if ($h < 0) {
                $h = 0;
            } elseif ($h > 59) {
                $h = 59;
            }

            if ($l > $h) {
                $tmp = $l;
                $l = $h;
                $h = $tmp;
                unset($tmp);
            }

            // For some reason range() doesn't work w/o the explicit
            // intval() calls.
            return '-' . join('-', range(intval($l), intval($h))) . '-';
        } else {
            // Invalid.
            die("Invalid value '$l' in range '$l-$h'");
        }
    }

    function dayValue($s)
    {
        for ($i = 0; $i < count($this->legalDays); $i++) {
            if ($this->legalDays[$i] == $s) {
                return $i;
            }
        }

        return -1;
    }

    function isDigit($s)
    {
        return preg_match('/^\d+$/', $s);
    }

    function isAlpha($s)
    {
        return $this->isLegalDay($s);
    }

    function isLegalDay($s)
    {
        return in_array($s, $this->legalDays);
    }

    function generallyDecentSyntax($s)
    {
        return ($s == '*' ||
                preg_match('/^\d+(-\d+)?([!&][A-Z\*]+(-[A-Z\*]+)?)?(,\d+(-\d+)?([!&][A-Z\*]+(-[A-Z\*]+)?)?)*$/', $s));
    }

}