forked from AlternC/AlternC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_cron.php
392 lines (345 loc) · 14.5 KB
/
m_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
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
<?php
/*
----------------------------------------------------------------------
LICENSE
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
To read the license please visit http://www.gnu.org/copyleft/gpl.html
----------------------------------------------------------------------
*/
/**
* This class manage web-cron tasks
*
* @copyright AlternC-Team 2000-2017 https://alternc.com/
*/
class m_cron {
const MAX_SOCKETS = 8;
const DEFAULT_CAFILE = "/etc/ssl/certs/ca-certificates.crt";
/**
*
*/
function schedule() {
return Array(
Array('unit' => 1440, 'name' => _("Daily")),
Array('unit' => 60, 'name' => _("Hour")),
Array('unit' => 30, 'name' => _("Half Hour")),
);
}
/**
* List the crontab for the current user.
* @return array an hash for each crontab.
*/
function lst_cron() {
global $cuid, $db, $msg;
$msg->debug("cron", "lst_cron");
$db->query("SELECT * FROM cron WHERE uid = ? ORDER BY url;", array($cuid));
$r = Array();
while ($db->next_record()) {
$tmp = Array();
$tmp['id'] = $db->f('id');
$tmp['url'] = urldecode($db->f('url'));
$tmp['user'] = urldecode($db->f('user'));
$tmp['password'] = urldecode($db->f('password'));
$tmp['schedule'] = $db->f('schedule');
$tmp['email'] = urldecode($db->f('email'));
$tmp['next_execution'] = $db->f('next_execution');
$r[] = $tmp;
}
return $r;
}
/**
* Hook called by menu class to add menu to the left panel
*/
function hook_menu() {
$obj = array(
'title' => _("Scheduled tasks"),
'link' => 'cron.php',
'pos' => 120,
);
return $obj;
}
/**
* update the crontab
* @param $arr array the crontab information, including its ID
* @return boolean TRUE if the crontab has been edited
*/
function update($arr) {
$ok = true;
foreach ($arr as $a) {
if (!isset($a['id'])) {
$a['id'] = null;
}
if (empty($a['url']) && is_null($a['id'])) {
continue;
}
if (!$this->_update_one($a['url'], $a['user'], $a['password'], $a['email'], $a['schedule'], $a['id'])) {
$ok = false;
}
}
return $ok;
}
/**
* delete a crontab
* @param $id the id of the crontab to delete
* @return boolean TRUE if the crontab has been deleted
*/
function delete_one($id) {
global $db, $msg, $cuid;
$msg->log("cron", "delete_one");
return $db->query("DELETE FROM cron WHERE id= ? AND uid= ? LIMIT 1;", array(intval($id), $cuid));
}
/**
* update a crontab,
* @return boolean TRUE if the crontab has been edited
*/
private function _update_one($url, $user, $password, $email, $schedule, $id = null) {
global $db, $msg, $quota, $cuid;
$msg->log("cron", "update_one");
if (empty($url) && !is_null($id)) {
return $this->delete_one($id);
}
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
$msg->raise("ERROR", "cron", _("URL not valid"));
return false;
}
$url = urlencode($url);
$user = urlencode($user);
if (empty($user)) {
$password = '';
}
$password = urlencode($password);
//@todo remove checkmail cf functions.php
if (!empty($email) && !checkmail($email) == 0) {
$msg->raise("ERROR", "cron", _("Email address is not valid"));
return false;
}
$email = urlencode($email);
if (!$this->valid_schedule($schedule)) {
return false;
}
if (is_null($id)) { // if a new insert, quotacheck
$q = $quota->getquota("cron");
if ($q["u"] >= $q["t"]) {
$msg->raise("ERROR", "cron", _("You quota of cron entries is over. You cannot create more cron entries"));
return false;
}
} else { // if not a new insert, check the $cuid
$db->query("SELECT uid FROM cron WHERE id = ? ;", array($id));
if (!$db->next_record()) {
return "false";
} // return false if pb
if ($db->f('uid') != $cuid) {
$msg->raise("ERROR", "cron", _("Identity problem"));
return false;
}
}
return $db->query("REPLACE INTO cron (id, uid, url, user, password, schedule, email) VALUES (?, ?, ?, ?, ?, ?, ?) ;" , array($id, $cuid, $url, $user, $password, $schedule, $email));
}
/**
* validate a crontab schedule
* @param $s array schedule paramters
* @return boolean TRUE if the schedule is valid
*/
function valid_schedule($s) {
$s2 = intval($s);
if ($s2 != $s) {
return false;
}
$r = false;
foreach ($this->schedule() as $cs) {
if ($cs['unit'] == $s) {
return true;
}
}
return $r;
}
/**
* hook for quota computation
*/
function hook_quota_get() {
global $cuid, $db, $msg;
$msg->debug("cron", "alternc_get_quota");
$q = Array("name" => "cron", "description" => _("Scheduled tasks"), "used" => 0);
$db->query("select count(*) as cnt from cron where uid = ? ;", array($cuid));
if ($db->next_record()) {
$q['used'] = $db->f('cnt');
}
return $q;
}
/**
* Execute the required crontab of AlternC users
* this function EXIT at the end.
*/
function execute_cron() {
global $db,$msg;
$msg->debug("cron", "execute_cron");
if (!isset($GLOBALS["DEBUG"])) {
$GLOBALS["DEBUG"] = false;
}
$db->query("SELECT id, url, email, schedule, user, password FROM cron WHERE next_execution <= NOW();");
$urllist = array();
while ($db->next_record()) {
$db->Record["url"] = urldecode($db->Record["url"]);
$db->Record["user"] = urldecode($db->Record["user"]);
$db->Record["email"] = urldecode($db->Record["email"]);
$db->Record["password"] = urldecode($db->Record["password"]);
// we support only http or https schemes:
if (substr($db->Record["url"], 0, 7) == "http://" || substr($db->Record["url"], 0, 8) == "https://") {
$u = array(
"url" => $db->Record["url"],
"id" => $db->Record["id"], "email" => $db->Record["email"],
);
if ($db->Record["user"] && $db->Record["password"]) {
$u["login"] = $db->Record["user"];
$u["password"] = $db->Record["password"];
}
if ($GLOBALS["DEBUG"])
echo "Will run cron :\n" . print_r($u, true) . "\n";
$urllist[] = $u;
}
if (empty($urllist)) { // nothing to do :
exit(0);
}
}
// cron_callback($url, $content, $curlobj) will be called at the end of each http call.
$this->rolling_curl($urllist, array("m_cron", "cron_callback"));
}
/**
* Callback function called by rolling_curl when a cron resulr has been received
* schedule it for next run and send the mail if needed
*/
function cron_callback($url, $content, $curl) {
global $db, $L_FQDN;
if (empty($url["id"])) {
return; // not normal...
}
$id = intval($url["id"]);
if ($curl["http_code"] == 200) {
$ok = true;
} else {
$ok = false;
}
if (isset($url["email"]) && $url["email"] && $content) {
if (!mail($url["email"], "AlternC Cron #$id - Report " . date("r"), "Please find below the stdout content produced by your cron task.\n------------------------------------------------------------\n\n" . $content, "From: postmaster@$L_FQDN")) {
echo "Error sending mail for cron #$id to address '" . $url["email"] . "'\n";
}
}
// now schedule it for next run:
$db->query("UPDATE cron SET next_execution=FROM_UNIXTIME( UNIX_TIMESTAMP(NOW()) + schedule * 60) WHERE id= ?", array($id));
}
/**
* Launch parallel (using MAX_SOCKETS sockets maximum) retrieval
* of URL using CURL
* @param $urls array of associative array, each having the following keys :
* url = url to get (of the form http[s]://login:password@host/path/file?querystring )
* login & password = if set, tell the login and password to use as simple HTTP AUTH.
* - any other key will be sent as it is to the callback function
* @param $callback function called for each request when completing. First argument is the $url object, second is the content (output)
* third is the info structure from curl for the returned page. 200 for OK, 403 for AUTH FAILED, 0 for timeout, dump it to know it ;)
* this function should return as soon as possible to allow other curl calls to complete properly.
* @param $cursom_options array of custom CURL options for all transfers
*/
function rolling_curl($urls, $callback, $custom_options = null) {
// make sure the rolling window isn't greater than the # of urls
if (!isset($GLOBALS["DEBUG"]))
$GLOBALS["DEBUG"] = false;
$rolling_window = m_cron::MAX_SOCKETS;
$rolling_window = (count($urls) < $rolling_window) ? count($urls) : $rolling_window;
$master = curl_multi_init();
// add additional curl options here
$std_options = array(CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 240, // 4 minutes timeout for a page
CURLOPT_USERAGENT => "AlternC (Cron Daemon)",
CURLOPT_MAXREDIRS => 0);
if ($GLOBALS["DEBUG"]) {
$std_options[CURLOPT_VERBOSE] = true;
}
$options = ($custom_options) ? ($std_options + $custom_options) : $std_options;
// start the first batch of requests
for ($i = 0; $i < $rolling_window; $i++) {
$ch = curl_init();
$options[CURLOPT_URL] = $urls[$i]["url"];
if ($GLOBALS["DEBUG"]) {
echo "URL: " . $urls[$i]["url"] . "\n";
}
curl_setopt_array($ch, $options);
// Handle custom cafile for some https url
if (strtolower(substr($options[CURLOPT_URL], 0, 5)) == "https") {
curl_setopt($ch, CURLOPT_CAINFO, m_cron::DEFAULT_CAFILE);
if ($GLOBALS["DEBUG"]) {
echo "cainfo set to DEFAULT\n";
}
}
if (isset($urls[$i]["login"]) && isset($urls[$i]["password"])) { // set basic http authentication
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $urls[$i]["login"] . ":" . $urls[$i]["password"]);
if ($GLOBALS["DEBUG"]) {
echo "set basic auth\n";
}
}
curl_multi_add_handle($master, $ch);
}
do {
while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);
if ($execrun != CURLM_OK) {
break;
}
// a request was just completed -- find out which one
while ($done = curl_multi_info_read($master)) {
$info = curl_getinfo($done['handle']);
// TODO : since ssl_verify_result is buggy, if we have [header_size] => 0 && [request_size] => 0 && [http_code] => 0, AND https, we can pretend the SSL certificate is buggy.
if ($GLOBALS["DEBUG"]) {
echo "Info for " . $done['handle'] . " \n";
print_r($info);
}
if ($info['http_code'] == 200) {
$output = curl_multi_getcontent($done['handle']);
} else {
// request failed. add error handling.
$output = "";
}
// request terminated. process output using the callback function.
// Pass the url array to the callback, so we need to search it
foreach ($urls as $url) {
if ($url["url"] == $info["url"]) {
call_user_func($callback, $url, $output, $info);
break;
}
}
// If there is more: start a new request
// (it's important to do this before removing the old one)
if ($i < count($urls)) {
$ch = curl_init();
$options[CURLOPT_URL] = $urls[$i++]; // increment i
curl_setopt_array($ch, $options);
if (strtolower(substr($options[CURLOPT_URL], 0, 5)) == "https") {
curl_setopt($ch, CURLOPT_CAINFO, m_cron::DEFAULT_CAFILE);
if ($GLOBALS["DEBUG"]) {
echo "cainfo set to DEFAULT\n";
}
}
if (isset($urls[$i]["login"]) && isset($urls[$i]["password"])) { // set basic http authentication
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, urlencode($urls[$i]["login"]) . ":" . urlencode($urls[$i]["password"]));
if ($GLOBALS["DEBUG"]) {
echo "set basic auth\n";
}
}
curl_multi_add_handle($master, $ch);
}
// remove the curl handle that just completed
curl_multi_remove_handle($master, $done['handle']);
}
} while ($running);
curl_multi_close($master);
return true;
}
} /* Class cron */