HomeHelpTrac

source: trunk/periodic/cron.php @ 15211

Revision 15211, 3.4 KB checked in by Alexander Trofimov, 12 months ago (diff)

Code cleaning:

  • converting new lines to \n
  • deleting spaces at the end of every line
  • converting all tabs to 4 spaces
  • automated script for future cleaning was added
Line 
1<?php
2/**
3 * @package     Dolphin Core
4 * @copyright   Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
5 * @license     CC-BY - http://creativecommons.org/licenses/by/3.0/
6 */
7
8$GLOBALS['bx_profiler_disable'] = true;
9define('BX_DOL_CRON_EXECUTE', '1');
10
11$aPathInfo = pathinfo(__FILE__);
12require_once ($aPathInfo['dirname'] . '/../inc/header.inc.php');
13
14
15function getRange($iLow, $iHigh, $iStep)
16{
17    $aResult = array();
18    for ($i = $iLow; $i <= $iHigh && $iStep; $i += $iStep)
19        $aResult[] = $i;
20    return $aResult;
21}
22
23function getPeriod($sPeriod, $iLow, $iHigh)
24{
25    $aRes = array();
26    $iStep = 1;
27    $sErr = '';
28
29    do
30    {
31        if ('' === $sPeriod)
32        {
33            $sErr = 'Variable sPeriod is emply';
34            break;
35        }
36
37        $aParam = explode('/', $sPeriod);
38
39        if (count($aParam) > 2)
40        {
41            $sErr = 'Error of format for string assigning period';
42            break;
43        }
44
45        if (count($aParam) == 2 && is_numeric($aParam[1]))
46            $iStep = $aParam[1];
47
48        $sPeriod = $aParam[0];
49
50        if ($sPeriod != '*')
51        {
52            $aParam = explode('-', $sPeriod);
53
54            if (count($aParam) > 2)
55            {
56                $sErr = 'Error of format for string assigning period';
57                break;
58            }
59
60            if (count($aParam) == 2)
61                $aRes = getRange($aParam[0], $aParam[1], $iStep);
62            else
63                $aRes = explode(',', $sPeriod);
64        }
65        else
66            $aRes = getRange($iLow, $iHigh, $iStep);
67    }
68    while(false);
69
70    if ($sErr)
71    {
72        // show error or add to log
73    }
74
75    return $aRes;
76}
77
78function checkCronJob($sPeriods, $aDate = array())
79{
80    $aParam = explode(' ', preg_replace("{ +}", ' ', trim($sPeriods)));
81    $bRes = true;
82
83    if(empty($aDate))
84        $aDate = getdate(time());
85
86    for ($i = 0; $i < count($aParam); $i++)
87    {
88        switch ($i)
89        {
90            case 0:
91                $aRes = getPeriod($aParam[$i], 0, 59);
92                $bRes = in_array($aDate['minutes'], $aRes);
93                break;
94            case 1:
95                $aRes = getPeriod($aParam[$i], 0, 23);
96                $bRes = in_array($aDate['hours'], $aRes);
97                break;
98            case 2:
99                $aRes = getPeriod($aParam[$i], 1, 31);
100                $bRes = in_array($aDate['mday'], $aRes);
101                break;
102            case 3:
103                $aRes = getPeriod($aParam[$i], 1, 12);
104                $bRes = in_array($aDate['mon'], $aRes);
105                break;
106            case 4:
107                $aRes = getPeriod($aParam[$i], 0, 6);
108                $bRes = in_array($aDate['wday'], $aRes);
109                break;
110        }
111
112        if (!$bRes)
113            break;
114    }
115
116    return $bRes;
117}
118
119function runJob($aJob)
120{
121    if(!empty($aJob['file']) && !empty($aJob['class']) && file_exists(BX_DIRECTORY_PATH_ROOT . $aJob['file'])) {
122        if(!class_exists($aJob['class']))
123            require_once(BX_DIRECTORY_PATH_ROOT . $aJob['file']);
124
125        $oHandler = new $aJob['class']();
126        $oHandler->processing();
127    }
128    else if(!empty($aJob['eval'])) {
129        eval($aJob['eval']);
130    }
131}
132
133$oDb = new BxDolDb();
134$aJobs = $oDb->fromCache('sys_cron_jobs', 'getAll', 'SELECT * FROM `sys_cron_jobs`');
135
136$aDate = getdate(time());
137
138foreach($aJobs as $aRow) {
139    if (checkCronJob($aRow['time'], $aDate))
140        runJob($aRow);
141}
142
Note: See TracBrowser for help on using the repository browser.