-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcronjob.php
More file actions
423 lines (395 loc) · 15.4 KB
/
Copy pathcronjob.php
File metadata and controls
423 lines (395 loc) · 15.4 KB
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
/**
* kitCronjob
*
* @author Ralf Hertsch <ralf.hertsch@phpmanufaktur.de>
* @link https://addons.phpmanufaktur.de/kitCronjob
* @copyright 2012 phpManufaktur by Ralf Hertsch
* @license MIT License (MIT) http://www.opensource.org/licenses/MIT
*/
// first we need the LEPTON config.php
require_once('../../config.php');
// wb2lepton compatibility
if (!defined('LEPTON_PATH'))
require_once WB_PATH . '/modules/' . basename(dirname(__FILE__)) . '/wb2lepton.php';
require_once LEPTON_PATH.'/modules/manufaktur_config/library.php';
global $manufakturConfig;
if (!is_object($manufakturConfig)) $manufakturConfig = new manufakturConfig('kit_cronjob');
class cronjobExec {
private $error = '';
private $cronjob_active = false;
private $cronjob_key = '';
private $use_ssl = false;
private $timezone = 'Europe/Berlin';
private $cronjob_executed = false;
private $php_exec = '/usr/bin/exec';
const LOG_STATUS = 'log_status';
const LOG_MESSAGE = 'log_message';
const CRONJOB_ID = 'cronjob_id';
const CRONJOB_NAME = 'cronjob_name';
const CRONJOB_STATUS = 'cronjob_status';
const CRONJOB_RUN = 'cronjob_run';
const STATUS_OK = 'OK';
const STATUS_INACTIVE = 'INACTIVE';
const STATUS_ERROR = 'ERROR';
private $log = array(
self::LOG_STATUS => self::STATUS_OK,
self::LOG_MESSAGE => '',
self::CRONJOB_ID => '-1',
self::CRONJOB_NAME => '',
self::CRONJOB_STATUS => '',
self::CRONJOB_RUN => '0000-00-00 00:00:00'
);
public function __construct() {
} // __construct()
/**
* Set an error and write to Logfile
*
* @param unknown_type $error
*/
protected function setError($error) {
global $database;
$this->error = $error;
$this->log[self::LOG_STATUS] = self::STATUS_ERROR;
$this->log[self::LOG_MESSAGE] = addslashes($error);
$this->writeLog();
} // setError()
/**
* Get the last error message
*
* @return string
*/
protected function getError() {
return $this->error;
} // getError()
protected function writeLog() {
global $database;
$fields = '';
$values = '';
$start = true;
foreach ($this->log as $key => $value) {
if (!$start) {
$fields .=',';
$values .= ',';
}
$fields .= "`$key`";
$values .= "'$value'";
$start = false;
}
$SQL = sprintf("INSERT INTO %smod_kit_cj_log (%s) VALUES (%s)", TABLE_PREFIX, $fields, $values);
if (!$database->query($SQL)) {
trigger_error(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $database->get_error()), E_USER_ERROR);
exit();
}
return true;
} // writeLog()
/**
* Get the general cronjob settings
*
* @return boolean
*/
protected function getSettings() {
global $manufakturConfig;
if (null === ($this->cronjob_active = $manufakturConfig->getValue('cfg_cronjob_active', 'kit_cronjob'))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $manufakturConfig->getError()));
exit($this->getError());
}
if (null === ($this->cronjob_key = $manufakturConfig->getValue('cfg_cronjob_key', 'kit_cronjob'))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $manufakturConfig->getError()));
exit($this->getError());
}
if (null === ($this->use_ssl = $manufakturConfig->getValue('cfg_use_ssl', 'kit_cronjob'))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $manufakturConfig->getError()));
exit($this->getError());
}
if (null === ($this->timezone = $manufakturConfig->getValue('cfg_use_timezone', 'kit_cronjob'))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $manufakturConfig->getError()));
exit($this->getError());
}
if (null === ($this->php_exec = $manufakturConfig->getValue('cfg_php_exec', 'kit_cronjob'))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $manufakturConfig->getError()));
exit($this->getError());
}
return true;
} // getSettings
/**
* Check if the KEY parameter is set and correct.
*
* @return boolean
*/
protected function checkCronjobKey() {
return (!isset($_REQUEST['key']) || ($_REQUEST['key'] != $this->cronjob_key)) ? false : true;
} // checkCronjobKey()
/**
* Calculate the next date and time the specified cronjob must be executed
*
* @param array $cronjob record with the cronjob data
* @return string in MySQL format Y-m-d H:i:s
*/
protected function calculateNextRun($cronjob) {
$hours = explode(',', $cronjob['cronjob_hour']);
$minutes = explode(',', $cronjob['cronjob_minute']);
$days = explode(',', $cronjob['cronjob_day_of_month']);
$months = explode(',', $cronjob['cronjob_month']);
$days_of_week = explode(',', $cronjob['cronjob_day_of_week']);
if ((count($days) == 31) && (count($months) == 12) && (count($days_of_week) == 7)) {
// this job runs daily!
$act_minute = (int) date('i');
$next_minute = 0;
$minutes = explode(',', $cronjob['cronjob_minute']);
foreach ($minutes as $minute) {
if ($minute > $act_minute) {
$next_minute = $minute;
break;
}
}
if ($next_minute == 0) $next_minute = $minutes[0];
$act_hour = (int) date('H');
$next_hour = 0;
$hours = explode(',', $cronjob['cronjob_hour']);
foreach ($hours as $hour) {
if (strtotime(date('H:i', mktime($hour, $next_minute, 0, date('m'), date('d'), date('Y')))) > time()) {
$next_hour = $hour;
break;
}
}
if ($next_hour == 0) $next_hour = $hours[0];
$day_add = 0;
if ($next_hour < (int) date('H')) $day_add = 1;
return date('Y-m-d H:i:s', mktime($next_hour, $next_minute, 0, date('m'), date('d')+$day_add, date('Y')));
}
elseif ((count($days) == 31) && (count($months) == 12)) {
// this job runs weekly!
$today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
$difference = 0;
$use_date = 0;
foreach ($days_of_week as $weekday) {
$x_date = strtotime(sprintf('%s this week', $weekday));
if ($x_date >= $today) {
$x = $x_date-$today;
// choose the nearest day!
if (($difference == 0) || ($x < $difference)) {
if ($x_date == $today) {
// special situation: it's today!
foreach ($hours as $hour) {
// walk through the hours...
if ($hour >= (int) date('H')) {
if ($hour == (int) date('H')) {
// it's the actual hour, just now!
foreach ($minutes as $minute) {
if (mktime($hour, $minute, 0, date('m'), date('d'), date('Y')) > time()) {
// success: choose this date!
return date('Y-m-d H:i:s', mktime($hour, $minute, 0, date('m'), date('d'), date('Y')));
}
}
}
else {
// success: choose this date!
return date('Y-m-d H:i:s', mktime($hour, $minutes[0], 0, date('m'), date('d'), date('Y')));
}
}
}
}
else {
// mark as best choice
$difference = $x;
$use_date = $x_date;
}
}
}
}
// ok - choose the best choice and return
return date('Y-m-d H:i:s', mktime($hours[0], $minutes[0], 0, date('m', $use_date), date('d', $use_date), date('Y', $use_date)));
}
elseif ((count($months) == 12) && (count($days_of_week) == 7)) {
// this job runs monthly
foreach ($days as $day) {
if ($day >= (int) date('d')) {
if ($day == (int) date('d')) {
// special: it's today!
foreach ($hours as $hour) {
if ($hour >= (int) date('H')) {
foreach ($minutes as $minute) {
if (mktime($hour, $minute, 0, date('m'), $day, date('Y')) > time()) {
// success: choose this date!
return date('Y-m-d H:i:s', mktime($hour, $minute, 0, date('m'), $day, date('Y')));
}
}
}
}
}
else {
// choose this day and the first time of the day
return date('Y-m-d H:i:s', mktime($hours[0], $minutes[0], 0, date('m'), $day, date('Y')));
}
}
}
// no date within the actual month, select the next month!
return date('Y-m-d H:i:s', mktime($hours[0], $minutes[0], 0, date('m')+1, $days[0], date('Y')));
}
else {
// this job runs at specific dates
foreach ($months as $month_name) {
// convert monthname to integer:
$month = (int) date("m", strtotime($month_name));
if ($month >= (int) date('m')) {
foreach ($days as $day) {
if ($day >= (int) date('d')) {
if ($day == (int) date('d')) {
// it's today!
foreach ($hours as $hour) {
if ($hour >= (int) date('H')) {
foreach ($minutes as $minute) {
if (mktime($hour, $minute, 0, $month, $day, date('Y')) > time()) {
// success: choose this date
return date('Y-m-d H:i:s', mktime($hour, $minute, 0, $month, $day, date('Y')));
}
}
}
}
}
else {
// choose this day for the date
return date('Y-m-d H:i:s', mktime($hours[0], $minutes[0], 0, $month, $days[0], date('Y')));
}
}
}
}
}
// date is not within the actual year, choose the first entry for the next one!
return date('Y-m-d H:i:s', mktime($hours[0], $minutes[0], 0, date('m', strtotime($months[0])), $days[0], date('Y')+1));
}
} // calculateNextRun()
/**
* Update a cronjob record
*
* @param array $cronjob data record
*/
protected function updateCronjob($cronjob) {
global $database;
$data = $cronjob;
unset($data['cronjob_id']);
unset($data['cronjob_timestamp']);
$SQL = sprintf("UPDATE %smod_kit_cj_cronjob SET ", TABLE_PREFIX);
$start = true;
foreach ($data as $key => $value) {
if (!$start) $SQL .= ',';
$SQL .= sprintf("`%s`='%s'", $key, $value);
$start = false;
}
$SQL .= sprintf(" WHERE `cronjob_id`='%d'", $cronjob['cronjob_id']);
$database->query($SQL);
if ($database->is_error()) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $database->get_error()));
exit($this->getError());
}
} // updateCronjob()
/**
* Check if there are any cronjobs to execute
*
*/
protected function checkScheduledJobs() {
global $database;
// first get all active cronjobs
$SQL = sprintf("SELECT * FROM %smod_kit_cj_cronjob WHERE `cronjob_status`='ACTIVE'", TABLE_PREFIX);
if (null ===($result = $database->query($SQL))) {
$this->setError(sprintf('[%s _ %s] %s', __METHOD__, __LINE__, $database->get_error()));
exit($this->getError());
}
$cronjobs = array();
while (false !== ($job = $result->fetchRow(MYSQL_ASSOC))) {
$cronjobs[$job['cronjob_id']] = $job;
}
foreach ($cronjobs as $cronjob) {
$this->log[self::CRONJOB_ID] = $cronjob['cronjob_id'];
$this->log[self::CRONJOB_NAME] = $cronjob['cronjob_name'];
if ($cronjob['cronjob_next_run'] == '0000-00-00 00:00:00') {
// this cronjob was never executed...
$cronjob['cronjob_next_run'] = $this->calculateNextRun($cronjob);
$cronjob['cronjob_last_status'] = 'First call, calculate the next regular run of this cronjob.';
$cronjob['cronjob_last_run'] = date('Y-m-d H:i:s');
$this->updateCronjob($cronjob);
$this->log[self::LOG_MESSAGE] = 'First call, calculate the next regular run of this cronjob.';
$this->log[self::LOG_STATUS] = self::STATUS_OK;
$this->log[self::CRONJOB_RUN] = $cronjob['cronjob_last_run'];
$this->log[self::CRONJOB_STATUS] = 'OK';
$this->writeLog();
}
if (strtotime($cronjob['cronjob_next_run']) < time()) {
// execute this cronjob
if (false !== strpos($cronjob['cronjob_command'], LEPTON_URL)) {
$cronjob['cronjob_command'] = substr($cronjob['cronjob_command'], strlen(LEPTON_URL));
}
if (false !== strpos($cronjob['cronjob_command'], LEPTON_PATH)) {
$cronjob['cronjob_command'] = substr($cronjob['cronjob_command'], strlen(LEPTON_PATH));
}
if (strpos($cronjob['cronjob_command'], DIRECTORY_SEPARATOR) == 0) {
$cronjob['cronjob_command'] = substr($cronjob['cronjob_command'], 1);
}
$execute = LEPTON_PATH.DIRECTORY_SEPARATOR.$cronjob['cronjob_command'];
if (!file_exists($execute)) {
// write the error to the log but continue with the next cronjob.
$this->setError(sprintf('The relative path %s does not exists, please check the settings for this cronjob!', $cronjob['cronjob_command']));
continue;
}
if (false === ($result = exec(sprintf("%s %s", $this->php_exec, $execute)))) {
// error executing - write log and continue
$this->setError(sprintf('Error executing %s - no more informations available.', $cronjob['cronjob_command']));
continue;
}
$this->log[self::LOG_MESSAGE] = $result;
$this->log[self::LOG_STATUS] = self::STATUS_OK;
$this->log[self::CRONJOB_RUN] = date('Y-m-d H:i:s');
$this->log[self::CRONJOB_STATUS] = 'OK';
$this->writeLog();
$cronjob['cronjob_last_run'] = $this->log[self::CRONJOB_RUN];
$cronjob['cronjob_last_status'] = $result;
$cronjob['cronjob_next_run'] = $this->calculateNextRun($cronjob);
$this->updateCronjob($cronjob);
$this->cronjob_executed = true;
}
}
} // checkScheduledJobs()
/**
* The action handler for cronjob.php
*
* @return string exit code
*/
public function action() {
$this->getSettings();
if (!date_default_timezone_set($this->timezone)) {
// invalid timezone
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__,
sprintf("The timezone '%s' is invalid, please check the settings for kitCronjob!", $this->timezone)));
// set Europe/Berlin as timezone and continue the job.
date_default_timezone_set('Europe/Berlin');
}
if (!$this->cronjob_active) {
// the cronjob is not active, so terminate immediate!
$this->log[self::LOG_STATUS] = self::STATUS_INACTIVE;
$this->log[self::LOG_MESSAGE] = 'The cronjob is inactive and does not execute any job.';
$this->writeLog();
exit('OK');
}
if (!$this->checkCronjobKey()) {
// the cronjob KEY is not set or invalid
$this->log[self::LOG_STATUS] = self::STATUS_ERROR;
$this->log[self::LOG_MESSAGE] = 'The cronjob.php was called without the parameter KEY or with a wrong passphrase for the KEY.';
$this->writeLog();
exit($this->getError());
}
// check the scheduled jobs
$this->checkScheduledJobs();
if (!$this->cronjob_executed) {
// nothing to do - still quit
$this->log[self::LOG_STATUS] = self::STATUS_OK;
$this->log[self::LOG_MESSAGE] = 'Nothing to do.';
$this->log[self::CRONJOB_ID] = -1;
$this->log[self::CRONJOB_NAME] = '';
$this->writeLog();
}
exit('OK');
} // action()
} // class cronjobExec
$cronjob = new cronjobExec();
$cronjob->action();