| 1 |
<?php |
<?php |
| 2 |
// $Id: cron.module,v 1.1 2008/07/14 17:20:29 denraf Exp $ |
// $Id: cron.module,v 1.3 2009/04/28 21:19:40 denraf Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 44 |
'title' => 'Edit', |
'title' => 'Edit', |
| 45 |
'description' => 'Administer all your crons from one drupal', |
'description' => 'Administer all your crons from one drupal', |
| 46 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 47 |
'page arguments' => array('cron_edit_form', 7), |
'page arguments' => array('cron_edit_form', 5), |
| 48 |
'access arguments' => array('authenticated user'), |
'access arguments' => array('authenticated user'), |
| 49 |
'type' => MENU_CALLBACK |
'type' => MENU_CALLBACK |
| 50 |
); |
); |
| 62 |
function cron_list_form() { |
function cron_list_form() { |
| 63 |
$header = array( |
$header = array( |
| 64 |
t('Site'), |
t('Site'), |
| 65 |
|
t('Cron key (Drupal 7)'), |
| 66 |
t('Description'), |
t('Description'), |
| 67 |
array('data' => t('Checks'), 'field' => 'checks'), |
array('data' => t('Checks'), 'field' => 'checks'), |
| 68 |
t('Last check'), |
t('Last check'), |
| 70 |
t('Last error') |
t('Last error') |
| 71 |
); |
); |
| 72 |
|
|
| 73 |
$sql = 'SELECT DISTINCT sid, url, description, checks, errors, last_check, last_error FROM {cron}'; |
$sql = 'SELECT DISTINCT sid, url, cron_key, description, checks, errors, last_check, last_error FROM {cron}'; |
| 74 |
$sql .= tablesort_sql($header); |
$sql .= tablesort_sql($header); |
| 75 |
$query_count = 'SELECT COUNT(DISTINCT url) FROM {cron}'; |
$query_count = 'SELECT COUNT(DISTINCT url) FROM {cron}'; |
| 76 |
$result = pager_query($sql, 50, 0, $query_count); |
$result = pager_query($sql, 50, 0, $query_count); |
| 79 |
while ($site = db_fetch_object($result)) { |
while ($site = db_fetch_object($result)) { |
| 80 |
$sites[$site->sid] = ''; |
$sites[$site->sid] = ''; |
| 81 |
$form['url'][$site->sid] = array('#value' => $site->url); |
$form['url'][$site->sid] = array('#value' => $site->url); |
| 82 |
$form['description'][$site->sid] = array('#value' => $site->description); |
$form['cron_key'][$site->sid] = array('#value' => $site->cron_key); |
| 83 |
$form['checks'][$site->sid] = array('#value' => $site->checks); |
$form['description'][$site->sid] = array('#value' => $site->description); |
| 84 |
|
$form['checks'][$site->sid] = array('#value' => $site->checks); |
| 85 |
$form['last_check'][$site->sid] = array('#value' => format_interval(time() - $site->last_check)); |
$form['last_check'][$site->sid] = array('#value' => format_interval(time() - $site->last_check)); |
| 86 |
$form['errors'][$site->sid] = array('#value' => $site->errors); |
$form['errors'][$site->sid] = array('#value' => $site->errors); |
| 87 |
$form['last_error'][$site->sid] = array('#value' => ($site->last_error == 0) ? 'N/A' : format_interval(time() - $site->last_error)); |
$form['last_error'][$site->sid] = array('#value' => ($site->last_error == 0) ? 'N/A' : format_interval(time() - $site->last_error)); |
| 106 |
'#size' => 20, |
'#size' => 20, |
| 107 |
'#required' => TRUE, |
'#required' => TRUE, |
| 108 |
); |
); |
| 109 |
|
$form['cron_key'] = array( |
| 110 |
|
'#type' => 'textfield', |
| 111 |
|
'#size' => 20, |
| 112 |
|
); |
| 113 |
$form['description'] = array( |
$form['description'] = array( |
| 114 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 115 |
'#size' => 20, |
'#size' => 20, |
| 153 |
|
|
| 154 |
$form_state['values']['url'] = $url; |
$form_state['values']['url'] = $url; |
| 155 |
$url .= '/cron.php'; |
$url .= '/cron.php'; |
| 156 |
|
if(isset($form_state['values']['cron_key'])) { |
| 157 |
|
$url .= '?cron_key='.$form_state['values']['cron_key']; |
| 158 |
|
} |
| 159 |
|
|
| 160 |
$ch = curl_init($url); |
$ch = curl_init($url); |
| 161 |
curl_setopt($ch, CURLOPT_HEADER, TRUE); |
curl_setopt($ch, CURLOPT_HEADER, TRUE); |
| 162 |
curl_setopt($ch, CURLOPT_NOBODY, TRUE); |
curl_setopt($ch, CURLOPT_NOBODY, TRUE); |
| 173 |
} |
} |
| 174 |
|
|
| 175 |
function cron_add_form_submit($form, $form_state) { |
function cron_add_form_submit($form, $form_state) { |
| 176 |
db_query("INSERT INTO {cron} (url, description, checks, last_check) VALUES ('%s', '%s', 1, '%d')", $form_state['values']['url'], $form_state['values']['description'], time()); |
db_query("INSERT INTO {cron} (url, cron_key, description, checks, last_check) VALUES ('%s', '%s', 1, '%d')", $form_state['values']['url'], $form_state['values']['cron_key'], $form_state['values']['description'], time()); |
| 177 |
drupal_goto('admin/settings/tools/cron'); |
drupal_goto('admin/settings/tools/cron'); |
| 178 |
} |
} |
| 179 |
|
|
| 180 |
function cron_edit_form($form_state, $sid = 0) { |
function cron_edit_form($form_state, $sid = 0) { |
| 181 |
$sql = db_query("SELECT url, description, checks, errors FROM {cron} WHERE sid = '%d'", $sid); |
$sql = db_query("SELECT url, cron_key, description, checks, errors FROM {cron} WHERE sid = '%d'", $sid); |
| 182 |
$row = db_fetch_object($sql); |
$row = db_fetch_object($sql); |
| 183 |
$form['sid'] = array( |
$form['sid'] = array( |
| 184 |
'#type' => 'hidden', |
'#type' => 'hidden', |
| 193 |
'#size' => 20, |
'#size' => 20, |
| 194 |
'#default_value' => $row->url, |
'#default_value' => $row->url, |
| 195 |
); |
); |
| 196 |
|
$form['cron_key'] = array( |
| 197 |
|
'#type' => 'textfield', |
| 198 |
|
'#size' => 20, |
| 199 |
|
'#default_value' => $row->cron_key, |
| 200 |
|
); |
| 201 |
$form['description'] = array( |
$form['description'] = array( |
| 202 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 203 |
'#size' => 20, |
'#size' => 20, |
| 238 |
|
|
| 239 |
$form_state['values']['url'] = $url; |
$form_state['values']['url'] = $url; |
| 240 |
$url .= '/cron.php'; |
$url .= '/cron.php'; |
| 241 |
|
if(isset($form_state['values']['cron_key'])) { |
| 242 |
|
$url .= '?cron_key='.$form_state['values']['cron_key']; |
| 243 |
|
} |
| 244 |
|
|
| 245 |
$ch = curl_init($url); |
$ch = curl_init($url); |
| 246 |
curl_setopt($ch, CURLOPT_HEADER, TRUE); |
curl_setopt($ch, CURLOPT_HEADER, TRUE); |
| 247 |
curl_setopt($ch, CURLOPT_NOBODY, TRUE); |
curl_setopt($ch, CURLOPT_NOBODY, TRUE); |
| 258 |
} |
} |
| 259 |
|
|
| 260 |
function cron_edit_form_submit($form, $form_state) { |
function cron_edit_form_submit($form, $form_state) { |
| 261 |
db_query("UPDATE {cron} SET url = '%s', description = '%s', checks = '%d', last_check = '%d' WHERE sid = '%d'", $form_state['values']['url'], $form_state['values']['description'], $form_state['values']['checks'] + 1, time(), $form_state['values']['sid']); |
db_query("UPDATE {cron} SET url = '%s', cron_key = '%s', description = '%s', checks = '%d', last_check = '%d' WHERE sid = '%d'", $form_state['values']['url'], $form_state['values']['cron_key'], $form_state['values']['description'], $form_state['values']['checks'] + 1, time(), $form_state['values']['sid']); |
| 262 |
drupal_goto('admin/settings/tools/cron'); |
drupal_goto('admin/settings/tools/cron'); |
| 263 |
} |
} |
| 264 |
|
|
| 294 |
$header = array( |
$header = array( |
| 295 |
theme('table_select_header_cell'), |
theme('table_select_header_cell'), |
| 296 |
t('Site'), |
t('Site'), |
| 297 |
|
t('Cron key (Drupal 7)'), |
| 298 |
t('Description'), |
t('Description'), |
| 299 |
array('data' => t('Checks'), 'field' => 'checks'), |
array('data' => t('Checks'), 'field' => 'checks'), |
| 300 |
t('Last check'), |
t('Last check'), |
| 307 |
$rows[] = array( |
$rows[] = array( |
| 308 |
drupal_render($form['sites'][$key]), |
drupal_render($form['sites'][$key]), |
| 309 |
drupal_render($form['url'][$key]), |
drupal_render($form['url'][$key]), |
| 310 |
|
drupal_render($form['cron_key'][$key]), |
| 311 |
drupal_render($form['description'][$key]), |
drupal_render($form['description'][$key]), |
| 312 |
drupal_render($form['checks'][$key]), |
drupal_render($form['checks'][$key]), |
| 313 |
drupal_render($form['last_check'][$key]), |
drupal_render($form['last_check'][$key]), |
| 340 |
// Overview table: |
// Overview table: |
| 341 |
$header = array( |
$header = array( |
| 342 |
t('Site'), |
t('Site'), |
| 343 |
|
t('Cron key (Drupal 7)'), |
| 344 |
t('Description'), |
t('Description'), |
| 345 |
t('Operations') |
t('Operations') |
| 346 |
); |
); |
| 348 |
if (isset($form['url']) && is_array($form['url'])) { |
if (isset($form['url']) && is_array($form['url'])) { |
| 349 |
$rows[] = array( |
$rows[] = array( |
| 350 |
drupal_render($form['url']), |
drupal_render($form['url']), |
| 351 |
|
drupal_render($form['cron_key']), |
| 352 |
drupal_render($form['description']), |
drupal_render($form['description']), |
| 353 |
drupal_render($form['operations']), |
drupal_render($form['operations']), |
| 354 |
); |
); |
| 371 |
// Overview table: |
// Overview table: |
| 372 |
$header = array( |
$header = array( |
| 373 |
t('Site'), |
t('Site'), |
| 374 |
|
t('Cron key (Drupal 7)'), |
| 375 |
t('Description'), |
t('Description'), |
| 376 |
t('Operations') |
t('Operations') |
| 377 |
); |
); |
| 378 |
if (isset($form['url']) && is_array($form['url'])) { |
if (isset($form['url']) && is_array($form['url'])) { |
| 379 |
$rows[] = array( |
$rows[] = array( |
| 380 |
drupal_render($form['url']), |
drupal_render($form['url']), |
| 381 |
|
drupal_render($form['cron_key']), |
| 382 |
drupal_render($form['description']), |
drupal_render($form['description']), |
| 383 |
drupal_render($form['operations']), |
drupal_render($form['operations']), |
| 384 |
); |
); |
| 397 |
*/ |
*/ |
| 398 |
function cron_cron() { |
function cron_cron() { |
| 399 |
|
|
| 400 |
$sql = db_query("SELECT sid, url, checks, errors, last_error FROM {cron}"); |
$sql = db_query("SELECT sid, url, cron_key, checks, errors, last_error FROM {cron}"); |
| 401 |
while ($row = db_fetch_object($sql)) { |
while ($row = db_fetch_object($sql)) { |
| 402 |
|
|
| 403 |
$checks = $row->checks + 1; |
$checks = $row->checks + 1; |
| 418 |
CURLOPT_FOLLOWLOCATION => FALSE, // don't follow redirects |
CURLOPT_FOLLOWLOCATION => FALSE, // don't follow redirects |
| 419 |
CURLOPT_USERAGENT => 'spider', // who am i |
CURLOPT_USERAGENT => 'spider', // who am i |
| 420 |
CURLOPT_AUTOREFERER => TRUE, // set referer on redirect |
CURLOPT_AUTOREFERER => TRUE, // set referer on redirect |
| 421 |
CURLOPT_CUSTOMREQUEST => 'HEAD'. |
CURLOPT_CUSTOMREQUEST => 'HEAD', |
| 422 |
CURLOPT_CONNECTTIMEOUT => 60, // timeout on connect |
CURLOPT_CONNECTTIMEOUT => 60, // timeout on connect |
| 423 |
CURLOPT_TIMEOUT => 60, // timeout on response |
CURLOPT_TIMEOUT => 60, // timeout on response |
| 424 |
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects |
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects |