1: <?php
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: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123: $PC_cronTab = $cfg['path']['contenido_cronlog'] . 'crontab.txt';
124: $PC_localCronTab = $cfg['path']['contenido_cronlog'] . 'crontab.local.txt';
125:
126:
127:
128: $PC_writeDir = $cfg['path']['contenido_cronlog'];
129:
130:
131:
132: $PC_jobDir = cRegistry::getBackendPath() . $cfg['path']['cronjobs'];
133:
134:
135: $PC_reqDir = getcwd();
136:
137:
138: $PC_useLog = 1;
139:
140:
141:
142: $PC_debug = false;
143:
144:
145:
146:
147:
148: define("PC_MINUTE", 1);
149: define("PC_HOUR", 2);
150: define("PC_DOM", 3);
151: define("PC_MONTH", 4);
152: define("PC_DOW", 5);
153: define("PC_CMD", 7);
154: define("PC_CRONLINE", 8);
155:
156: if ($PC_debug) {
157: echo "<pre>Configuration:";
158: echo "\nPC_cronTab = " . $PC_cronTab;
159: echo "\nPC_writeDir = " . $PC_writeDir;
160: echo "\nPC_jobDir = " . $PC_jobDir;
161: echo "\nPC_reqDir = " . $PC_reqDir;
162: echo "\nPC_useLog = " . $PC_useLog;
163: echo "\n";
164: }
165:
166: global $bJobRunned;
167:
168: $bJobRunned = false;
169:
170: chdir($PC_jobDir);
171: $PC_jobs = parseCronFile($PC_cronTab, $PC_debug);
172:
173: for ($i = 0; $i < count($PC_jobs); $i++) {
174: $bJobRunned = true;
175: runJob($PC_jobs[$i], $PC_jobDir, $PC_writeDir, $PC_useLog, $PC_debug);
176: }
177:
178: $PC_jobs = parseCronFile($PC_localCronTab, $PC_debug);
179: for ($i = 0; $i < count($PC_jobs); $i++) {
180: $bJobRunned = true;
181: runJob($PC_jobs[$i], $PC_jobDir, $PC_writeDir, $PC_useLog, $PC_debug);
182: }
183: chdir($PC_reqDir);
184:
185: if ($PC_debug) {
186: echo "\n</pre>";
187: }
188:
189: function logMessage($msg, $PC_writeDir, $PC_useLog, $PC_debug) {
190: if ($PC_useLog == 1) {
191: $logfile = $PC_writeDir . "pseudo-cron.log";
192:
193: if (is_writable($logfile)) {
194: $file = fopen($logfile, "ab");
195: if ($msg[strlen($msg) - 1] != "\n") {
196: $msg.="\r\n";
197: }
198: if ($PC_debug) {
199: echo $msg;
200: }
201: fputs($file, date("r", time()) . " " . $msg);
202: fclose($file);
203: }
204: }
205: }
206:
207: function lTrimZeros($number) {
208: while ($number[0] == '0') {
209: $number = substr($number, 1);
210: }
211: return $number;
212: }
213:
214: function parseElement($element, &$targetArray, $numberOfElements) {
215: $subelements = explode(",", $element);
216: for ($i = 0; $i < $numberOfElements; $i++) {
217: $targetArray[$i] = $subelements[0] == "*";
218: }
219:
220: for ($i = 0; $i < count($subelements); $i++) {
221: if (preg_match("~^(\\*|([0-9]{1,2})(-([0-9]{1,2}))?)(/([0-9]{1,2}))?$~", $subelements[$i], $matches)) {
222: if ($matches[1] == "*") {
223: $matches[2] = 0;
224: $matches[4] = $numberOfElements;
225: } elseif (!array_key_exists(4, $matches) || $matches[4] == "") {
226: $matches[4] = $matches[2];
227: }
228: if (array_key_exists(5, $matches)) {
229: if ($matches[5][0] != "/") {
230: $matches[6] = 1;
231: }
232: } else {
233: $matches[6] = 1;
234: }
235: for ($j = lTrimZeros($matches[2]); $j <= lTrimZeros($matches[4]); $j+=lTrimZeros($matches[6])) {
236: $targetArray[$j] = TRUE;
237: }
238: }
239: }
240: }
241:
242: function decDate(&$dateArr, $amount, $unit, $PC_debug) {
243: if ($PC_debug) {
244: echo sprintf("Decreasing from %02d.%02d. %02d:%02d by %d %6s ", $dateArr['mday'], $dateArr['mon'], $dateArr['hours'], $dateArr['minutes'], $amount, $unit);
245: }
246: if ($unit == "mday") {
247: $dateArr["hours"] = 23;
248: $dateArr["minutes"] = 59;
249: $dateArr["seconds"] = 59;
250: $dateArr["mday"] -= $amount;
251: $dateArr["wday"] -= $amount % 7;
252: if ($dateArr["wday"] < 0) {
253: $dateArr["wday"]+=7;
254: }
255: if ($dateArr["mday"] < 1) {
256: $dateArr["mon"]--;
257: switch ($dateArr["mon"]) {
258: case 0:
259: $dateArr["mon"] = 12;
260: $dateArr["year"]--;
261:
262: case 1:
263: case 3:
264: case 5:
265: case 7:
266: case 8:
267: case 10:
268: case 12:
269: $dateArr["mday"] = 31;
270: break;
271: case 4:
272: case 6:
273: case 9:
274: case 11:
275: $dateArr["mday"] = 30;
276: break;
277: case 2:
278: $dateArr["mday"] = 28;
279: break;
280: }
281: }
282: } elseif ($unit == "hour") {
283: if ($dateArr["hours"] == 0) {
284: decDate($dateArr, 1, "mday", $PC_debug);
285: } else {
286: $dateArr["minutes"] = 59;
287: $dateArr["seconds"] = 59;
288: $dateArr["hours"]--;
289: }
290: } elseif ($unit == "minute") {
291: if ($dateArr["minutes"] == 0) {
292: decDate($dateArr, 1, "hour", $PC_debug);
293: } else {
294: $dateArr["seconds"] = 59;
295: $dateArr["minutes"]--;
296: }
297: }
298: if ($PC_debug) {
299: echo sprintf("to %02d.%02d. %02d:%02d\n", $dateArr[mday], $dateArr[mon], $dateArr[hours], $dateArr[minutes]);
300: }
301: }
302:
303: function getLastScheduledRunTime($job, $PC_debug) {
304: $dateArr = getdate();
305: $minutesBack = 0;
306: while (
307: $minutesBack < 525600 AND
308: (!$job[PC_MINUTE][$dateArr["minutes"]] OR
309: !$job[PC_HOUR][$dateArr["hours"]] OR
310: (!$job[PC_DOM][$dateArr["mday"]] OR !$job[PC_DOW][$dateArr["wday"]]) OR
311: !$job[PC_MONTH][$dateArr["mon"]])
312: ) {
313: if (!$job[PC_DOM][$dateArr["mday"]] OR !$job[PC_DOW][$dateArr["wday"]]) {
314: decDate($dateArr, 1, "mday", $PC_debug);
315: $minutesBack+=1440;
316: continue;
317: }
318: if (!$job[PC_HOUR][$dateArr["hours"]]) {
319: decDate($dateArr, 1, "hour", $PC_debug);
320: $minutesBack+=60;
321: continue;
322: }
323: if (!$job[PC_MINUTE][$dateArr["minutes"]]) {
324: decDate($dateArr, 1, "minute", $PC_debug);
325: $minutesBack++;
326: continue;
327: }
328: }
329:
330: if ($PC_debug) {
331: print_r($dateArr);
332: }
333:
334: return mktime($dateArr["hours"], $dateArr["minutes"], 0, $dateArr["mon"], $dateArr["mday"], $dateArr["year"]);
335: }
336:
337: function getJobFileName($jobname, $PC_writeDir) {
338: $jobfile = $PC_writeDir . urlencode($jobname) . ".job";
339: return $jobfile;
340: }
341:
342: function getLastActialRunTime($jobname, $PC_writeDir) {
343: $jobfile = getJobFileName($jobname, $PC_writeDir);
344: if (cFileHandler::exists($jobfile)) {
345: $file = fopen($jobfile, "rb");
346: $lastRun = fgets($file, 100);
347: fclose($file);
348: if (is_numeric($lastRun)) {
349: return $lastRun;
350: }
351: }
352: return 0;
353: }
354:
355: function markLastRun($jobname, $lastRun, $PC_writeDir) {
356: $jobfile = getJobFileName($jobname, $PC_writeDir);
357:
358: if ($file = @fopen($jobfile, "w")) {
359: fputs($file, $lastRun);
360: fclose($file);
361: } else {
362:
363: }
364: }
365:
366: function runJob($job, $PC_jobDir, $PC_writeDir, $PC_useLog, $PC_debug = false) {
367: global $cfg, $sess;
368: $extjob = Array();
369: $jobfile = getJobFileName($job[PC_CMD], $PC_writeDir);
370: parseElement($job[PC_MINUTE], $extjob[PC_MINUTE], 60);
371: parseElement($job[PC_HOUR], $extjob[PC_HOUR], 24);
372: parseElement($job[PC_DOM], $extjob[PC_DOM], 31);
373: parseElement($job[PC_MONTH], $extjob[PC_MONTH], 12);
374: parseElement($job[PC_DOW], $extjob[PC_DOW], 7);
375:
376: $lastActual = getLastActialRunTime($job[PC_CMD], $PC_writeDir);
377: $lastScheduled = getLastScheduledRunTime($extjob, $PC_debug);
378:
379: if ($lastScheduled > $lastActual) {
380: logMessage("Running " . $job[PC_CRONLINE], $PC_writeDir, $PC_useLog, $PC_debug);
381: logMessage(" Last run: " . date("r", $lastActual), $PC_writeDir, $PC_useLog, $PC_debug);
382: logMessage(" Last scheduled: " . date("r", $lastScheduled), $PC_writeDir, $PC_useLog, $PC_debug);
383: markLastRun($job[PC_CMD], $lastScheduled, $PC_writeDir);
384:
385: if ($PC_debug) {
386: echo getcwd();
387: include($PC_jobDir . $job[PC_CMD]);
388: } else {
389: if (is_object($sess)) {
390: $sess->freeze();
391: }
392: @include($PC_jobDir . $job[PC_CMD]);
393: if (is_object($sess)) {
394: $sess->thaw();
395: }
396: }
397: logMessage("Completed " . $job[PC_CRONLINE], $PC_writeDir, $PC_useLog, $PC_debug);
398: return true;
399: } else {
400: if ($PC_debug) {
401: logMessage("Skipping " . $job[PC_CRONLINE], $PC_writeDir, $PC_useLog, $PC_debug);
402: logMessage(" Last run: " . date("r", $lastActual), $PC_writeDir, $PC_useLog, $PC_debug);
403: logMessage(" Last scheduled: " . date("r", $lastScheduled), $PC_writeDir, $PC_useLog, $PC_debug);
404: logMessage("Completed " . $job[PC_CRONLINE], $PC_writeDir, $PC_useLog, $PC_debug);
405: }
406: return false;
407: }
408: }
409:
410: function parseCronFile($PC_cronTabFile, $PC_debug) {
411: $file = @file($PC_cronTabFile);
412: $job = Array();
413: $jobs = Array();
414: for ($i = 0; $i < count($file); $i++) {
415: if ($file[$i][0] != '#') {
416:
417:
418: if (preg_match("~^([-0-9,/*]+)\\s+([-0-9,/*]+)\\s+([-0-9,/*]+)\\s+([-0-9,/*]+)\\s+([-0-7,/*]+|(-|/|Sun|Mon|Tue|Wed|Thu|Fri|Sat)+)\\s+([^#]*)(#.*)?$~i", $file[$i], $job)) {
419: $jobNumber = count($jobs);
420: $jobs[$jobNumber] = $job;
421: if ($jobs[$jobNumber][PC_DOW][0] != '*' AND !is_numeric($jobs[$jobNumber][PC_DOW])) {
422: $jobs[$jobNumber][PC_DOW] = str_replace(
423: Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"), Array(0, 1, 2, 3, 4, 5, 6), $jobs[$jobNumber][PC_DOW]);
424: }
425: $jobs[$jobNumber][PC_CMD] = trim($job[PC_CMD]);
426: $jobs[$jobNumber][PC_CRONLINE] = $file[$i];
427: }
428: }
429: }
430: if ($PC_debug) {
431: var_dump($jobs);
432: }
433: return $jobs;
434: }
435:
436: ?>