37 Array(
'unit' => 1440,
'name' => _(
"Daily")),
38 Array(
'unit' => 60,
'name' => _(
"Hour")),
39 Array(
'unit' => 30,
'name' => _(
"Half Hour")),
50 $msg->debug(
"cron",
"lst_cron");
51 $db->query(
"SELECT * FROM cron WHERE uid = ? ORDER BY url;", array(
$cuid));
53 while (
$db->next_record()) {
55 $tmp[
'id'] =
$db->f(
'id');
56 $tmp[
'url'] = urldecode(
$db->f(
'url'));
57 $tmp[
'user'] = urldecode(
$db->f(
'user'));
58 $tmp[
'password'] = urldecode(
$db->f(
'password'));
59 $tmp[
'schedule'] =
$db->f(
'schedule');
60 $tmp[
'email'] = urldecode(
$db->f(
'email'));
61 $tmp[
'next_execution'] =
$db->f(
'next_execution');
72 'title' => _(
"Scheduled tasks"),
88 foreach ($arr as $a) {
89 if (!isset($a[
'id'])) {
92 if (empty($a[
'url']) && is_null($a[
'id'])) {
95 if (!$this->
_update_one($a[
'url'], $a[
'user'], $a[
'password'], $a[
'email'], $a[
'schedule'], $a[
'id'])) {
110 $msg->log(
"cron",
"delete_one");
111 return $db->query(
"DELETE FROM cron WHERE id= ? AND uid= ? LIMIT 1;", array(intval(
$id),
$cuid));
121 $msg->log(
"cron",
"update_one");
123 if (empty($url) && !is_null(
$id)) {
128 if (filter_var($url, FILTER_VALIDATE_URL) ===
false) {
129 $msg->raise(
"ERROR",
"cron", _(
"URL not valid"));
132 $url = urlencode($url);
140 if (!empty($email) && !
checkmail($email) == 0) {
141 $msg->raise(
"ERROR",
"cron", _(
"Email address is not valid"));
144 $email = urlencode($email);
150 $q = $quota->getquota(
"cron");
151 if (
$q[
"u"] >=
$q[
"t"]) {
152 $msg->raise(
"ERROR",
"cron", _(
"You quota of cron entries is over. You cannot create more cron entries"));
156 $db->query(
"SELECT uid FROM cron WHERE id = ? ;", array(
$id));
157 if (!
$db->next_record()) {
161 $msg->raise(
"ERROR",
"cron", _(
"Identity problem"));
165 return $db->query(
"REPLACE INTO cron (id, uid, url, user, password, schedule, email) VALUES (?, ?, ?, ?, ?, ?, ?) ;" , array(
$id,
$cuid, $url,
$user,
$password, $schedule, $email));
180 foreach ($this->
schedule() as $cs) {
181 if ($cs[
'unit'] == $s) {
194 $msg->debug(
"cron",
"alternc_get_quota");
195 $q = Array(
"name" =>
"cron",
"description" => _(
"Scheduled tasks"),
"used" => 0);
196 $db->query(
"select count(*) as cnt from cron where uid = ? ;", array(
$cuid));
197 if (
$db->next_record()) {
198 $q[
'used'] =
$db->f(
'cnt');
211 $msg->debug(
"cron",
"execute_cron");
212 if (!isset($GLOBALS[
"DEBUG"])) {
213 $GLOBALS[
"DEBUG"] =
false;
215 $db->query(
"SELECT id, url, email, schedule, user, password FROM cron WHERE next_execution <= NOW();");
218 while (
$db->next_record()) {
219 $db->Record[
"url"] = urldecode(
$db->Record[
"url"]);
220 $db->Record[
"user"] = urldecode(
$db->Record[
"user"]);
221 $db->Record[
"email"] = urldecode(
$db->Record[
"email"]);
222 $db->Record[
"password"] = urldecode(
$db->Record[
"password"]);
225 if (substr(
$db->Record[
"url"], 0, 7) ==
"http://" || substr(
$db->Record[
"url"], 0, 8) ==
"https://") {
227 "url" =>
$db->Record[
"url"],
228 "id" =>
$db->Record[
"id"],
"email" =>
$db->Record[
"email"],
231 if (
$db->Record[
"user"] &&
$db->Record[
"password"]) {
232 $u[
"login"] =
$db->Record[
"user"];
233 $u[
"password"] =
$db->Record[
"password"];
235 if ($GLOBALS[
"DEBUG"])
236 echo
"Will run cron :\n" . print_r($u,
true) .
"\n";
240 if (empty($urllist)) {
245 $this->
rolling_curl($urllist, array(
"m_cron",
"cron_callback"));
255 if (empty($url[
"id"])) {
258 $id = intval($url[
"id"]);
260 if ($curl[
"http_code"] == 200) {
265 if (isset($url[
"email"]) && $url[
"email"] &&
$content) {
266 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")) {
267 echo
"Error sending mail for cron #$id to address '" . $url[
"email"] .
"'\n";
271 $db->query(
"UPDATE cron SET next_execution=FROM_UNIXTIME( UNIX_TIMESTAMP(NOW()) + schedule * 60) WHERE id= ?", array(
$id));
289 if (!isset($GLOBALS[
"DEBUG"]))
290 $GLOBALS[
"DEBUG"] =
false;
292 $rolling_window = (count($urls) < $rolling_window) ? count($urls) : $rolling_window;
294 $master = curl_multi_init();
297 $std_options = array(CURLOPT_RETURNTRANSFER =>
true,
298 CURLOPT_FOLLOWLOCATION =>
false,
299 CURLOPT_CONNECTTIMEOUT => 5,
300 CURLOPT_TIMEOUT => 240,
301 CURLOPT_USERAGENT =>
"AlternC (Cron Daemon)",
302 CURLOPT_MAXREDIRS => 0);
304 if ($GLOBALS[
"DEBUG"]) {
305 $std_options[CURLOPT_VERBOSE] =
true;
307 $options = ($custom_options) ? ($std_options + $custom_options) : $std_options;
310 for (
$i = 0;
$i < $rolling_window;
$i++) {
312 $options[CURLOPT_URL] = $urls[
$i][
"url"];
313 if ($GLOBALS[
"DEBUG"]) {
314 echo
"URL: " . $urls[
$i][
"url"] .
"\n";
316 curl_setopt_array($ch, $options);
318 if (strtolower(substr($options[CURLOPT_URL], 0, 5)) ==
"https") {
320 if ($GLOBALS[
"DEBUG"]) {
321 echo
"cainfo set to DEFAULT\n";
324 if (isset($urls[
$i][
"login"]) && isset($urls[
$i][
"password"])) {
325 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
326 curl_setopt($ch, CURLOPT_USERPWD, $urls[
$i][
"login"] .
":" . $urls[
$i][
"password"]);
327 if ($GLOBALS[
"DEBUG"]) {
328 echo
"set basic auth\n";
331 curl_multi_add_handle($master, $ch);
335 while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);
336 if ($execrun != CURLM_OK) {
340 while ($done = curl_multi_info_read($master)) {
341 $info = curl_getinfo($done[
'handle']);
343 if ($GLOBALS[
"DEBUG"]) {
344 echo
"Info for " . $done[
'handle'] .
" \n";
347 if (
$info[
'http_code'] == 200) {
348 $output = curl_multi_getcontent($done[
'handle']);
355 foreach ($urls as $url) {
356 if ($url[
"url"] ==
$info[
"url"]) {
357 call_user_func($callback, $url, $output,
$info);
364 if (
$i < count($urls)) {
366 $options[CURLOPT_URL] = $urls[
$i++];
367 curl_setopt_array($ch, $options);
368 if (strtolower(substr($options[CURLOPT_URL], 0, 5)) ==
"https") {
370 if ($GLOBALS[
"DEBUG"]) {
371 echo
"cainfo set to DEFAULT\n";
374 if (isset($urls[
$i][
"login"]) && isset($urls[
$i][
"password"])) {
375 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
376 curl_setopt($ch, CURLOPT_USERPWD, urlencode($urls[
$i][
"login"]) .
":" . urlencode($urls[
$i][
"password"]));
377 if ($GLOBALS[
"DEBUG"]) {
378 echo
"set basic auth\n";
381 curl_multi_add_handle($master, $ch);
384 curl_multi_remove_handle($master, $done[
'handle']);
388 curl_multi_close($master);
This class manage web-cron tasks.
update($arr)
update the crontab
delete_one($id)
delete a crontab
rolling_curl($urls, $callback, $custom_options=null)
Launch parallel (using MAX_SOCKETS sockets maximum) retrieval of URL using CURL.
hook_menu()
Hook called by menu class to add menu to the left panel.
_update_one($url, $user, $password, $email, $schedule, $id=null)
update a crontab,
lst_cron()
List the crontab for the current user.
cron_callback($url, $content, $curl)
Callback function called by rolling_curl when a cron resulr has been received schedule it for next ru...
valid_schedule($s)
validate a crontab schedule
execute_cron()
Execute the required crontab of AlternC users this function EXIT at the end.
hook_quota_get()
hook for quota computation
checkmail($mail)
Check an email address, use filter_var with emails, which works great ;)
if(!isset($is_include)) if(! $key &&! $crt) $id