forked from impresspages/ImpressPages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_cron.php
196 lines (161 loc) · 5.3 KB
/
ip_cron.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
<?php
/**
* ImpressPages CMS main cron file
*
* This file should be triggered to launch repeatedly few times a day.
* He makes global maintenance and trows cron.php on all installed modules.
*
* @package ImpressPages
*
*
*/
/** @private */
if (!defined('CMS')) {
define('CMS', true); // make sure other files are accessed through this file.
}
if (!defined('BACKEND')) {
define('BACKEND', true); // make sure other files are accessed through this file.
}
if (!defined('FRONTEND')) {
define('FRONTEND', true); // make sure other files are accessed through this file.
}
if (!defined('CRON')) {
define('CRON', true);
}
if(is_file(__DIR__.'/ip_config.php')) {
require (__DIR__.'/ip_config.php');
} else {
require (__DIR__.'/../ip_config.php');
}
error_reporting(E_ALL|E_STRICT);
if (DEVELOPMENT_ENVIRONMENT){
ini_set('display_errors', '1');
} else {
ini_set('display_errors', '0');
}
require_once(BASE_DIR.FRONTEND_DIR.'init.php');
$db = new db();
if($db->connect()){
$log = new \Modules\administrator\log\Module();
try {
$dispatcher = new \Ip\Dispatcher();
$parametersMod = new ParametersMod();
$session = new Frontend\Session();
$site = new \Site();
$site->init();
$dispatcher->notify(new \Ip\Event($site, 'site.afterInit', null));
$cron = new Cron();
$cron->execute();
$db->disconnect();
} catch (\Exception $e) {
$log->log('System', 'Fatal error', $e->getMessage().' in '.$e->getFile().':'.$e->getLine());
throw $e;
}
}else trigger_error('Database access');
/**
* A class for cron jobs.
* @package ImpressPages
*/
class Cron{
/**
* Stores information about execution time.
* @var cron_information
*/
var $information;
function __construct(){
$this->information = new cronInformation();
}
/**
* Executes required maintenance in global and modules scope.
*/
function execute(){
global $log;
if (function_exists('ignore_user_abort')) {
ignore_user_abort(true);
}
$log->log('system/cron', 'start');
$sql = 'select m.core, m.name as m_name, mg.name as mg_name from `'.DB_PREF.'module_group` mg, `'.DB_PREF.'module` m where m.group_id = mg.id';
$rs = mysql_query($sql);
if($rs){
while($lock = mysql_fetch_assoc($rs)){
if($lock['core']){
$file = MODULE_DIR.$lock['mg_name'].'/'.$lock['m_name'].'/cron.php';
} else {
$file = PLUGIN_DIR.$lock['mg_name'].'/'.$lock['m_name'].'/cron.php';
}
if(file_exists($file)){
require($file);
eval('$tmpCron = new \\Modules\\'.$lock['mg_name'].'\\'.$lock['m_name'].'\\Cron();');
$tmpCron->execute($this->information);
}
}
}
$log->log('system/cron', 'executed');
}
}
/**
* Saves information about cron jobs.
* @package ImpressPages
*/
class cronInformation{
/** true if cron is executed first time this year
* @var bool */
var $firstTimeThisYear;
/** true if cron is executed first time this month
* @var bool */
var $firstTimeThisMonth;
/** true if cron is executed first time this week
* @var bool */
var $firstTimeThisWeek;
/** true if cron is executed first time this day
* @var bool */
var $firstTimeThisDay;
/** true if cron is executed first time this hour
* @var bool */
var $firstTimeThisHour;
/** last cron execution time
* @var string */
var $lastTimeBefore;
/**
* Collects information about cron jobs.
*/
function __construct(){
$this->firstTimeThisYear = true;
$this->firstTimeThisMonth = true;
$this->firstTimeThisWeek = true;
$this->firstTimeThisDay = true;
$this->firstTimeThisHour = true;
$this->lastTimeBefore = null;
$sql = "
SELECT
`time`,
year(CURRENT_TIMESTAMP) = year(`time`) as `same_year`,
month(CURRENT_TIMESTAMP) = month(`time`) as `same_month`,
day(CURRENT_TIMESTAMP) = day(`time`) as `same_day`,
week(CURRENT_TIMESTAMP) = week(`time`) as `same_week`,
hour(CURRENT_TIMESTAMP) = hour(`time`) as `same_hour`
FROM `".DB_PREF."log`
WHERE `module` = 'system/cron' AND `name` = 'executed'
ORDER BY id desc LIMIT 1
";
$rs = mysql_query($sql);
if($rs){
if(($lock = mysql_fetch_assoc($rs)) && !(isset($_GET['test']) && isset($_SESSION['backend_session']['user_id']))){
if($lock['same_year'])
$this->firstTimeThisYear = false;
if($lock['same_year'] && $lock['same_month'])
$this->firstTimeThisMonth = false;
if($lock['same_year'] && $lock['same_month'] && $lock['same_day'])
$this->firstTimeThisDay = false;
if($lock['same_year'] && $lock['same_month'] && $lock['same_day']&& $lock['same_hour'])
$this->firstTimeThisHour = false;
if($lock['same_year'] && $lock['same_week'])
$this->firstTimeThisWeek = false;
$this->last_time_before = $lock['time'];
}
}else{
trigger_error($sql.' '.mysql_error());
}
}
}
?>