123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- define('SMF', 'BACKGROUND');
- define('FROM_CLI', empty($_SERVER['REQUEST_METHOD']));
- define('WIRELESS', false);
- define('MAX_CRON_TIME', 10);
- define('MAX_CLAIM_THRESHOLD', 300);
- global $time_start, $maintenance, $msubject, $mmessage, $mbname, $language;
- global $boardurl, $boarddir, $sourcedir, $webmaster_email;
- global $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error;
- global $db_connection, $modSettings, $context, $sc, $user_info, $txt;
- global $smcFunc, $ssi_db_user, $scripturl, $db_passwd, $cachedir;
- define('TIME_START', microtime(true));
- foreach (array('db_character_set', 'cachedir') as $variable)
- if (isset($GLOBALS[$variable]))
- unset($GLOBALS[$variable]);
- require_once(dirname(__FILE__) . '/Settings.php');
- if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache'))
- $cachedir = $boarddir . '/cache';
- if ($maintenance == 2)
- die($mmessage);
- if (substr($sourcedir, 0, 1) == '.' && substr($sourcedir, 1, 1) != '.')
- $sourcedir = dirname(__FILE__) . substr($sourcedir, 1);
- if (file_exists($cachedir . '/cron.lock'))
- obExit_cron();
- if (!FROM_CLI)
- {
-
- $ts = isset($_GET['ts']) ? (int) $_GET['ts'] : 0;
- if ($ts <= 0 || $ts % 15 != 0 || time() - $ts < 0 || time() - $ts > 20)
- obExit_cron();
- }
- require_once($sourcedir . '/Errors.php');
- require_once($sourcedir . '/Load.php');
- require_once($sourcedir . '/Subs.php');
- if (version_compare(PHP_VERSION, '5.1', '<'))
- require_once($sourcedir . '/Subs-Compat.php');
- $smcFunc = array();
- unset ($db_show_debug);
- loadDatabase();
- reloadSettings();
- set_error_handler('error_handler_cron');
- $sc = '';
- $_SERVER['QUERY_STRING'] = '';
- $_SERVER['REQUEST_URL'] = FROM_CLI ? 'CLI cron.php' : $boardurl . '/cron.php';
- cleanRequest_cron();
- while ($task_details = fetch_task())
- {
- $result = perform_task($task_details);
- if ($result)
- {
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}background_tasks
- WHERE id_task = {int:task}',
- array(
- 'task' => $task_details['id_task'],
- )
- );
- }
- }
- obExit_cron();
- exit;
- function fetch_task()
- {
- global $smcFunc;
-
- if (microtime(true) - TIME_START > MAX_CRON_TIME)
- return false;
-
-
-
- $request = $smcFunc['db_query']('cron_find_task', '
- SELECT id_task, task_file, task_class, task_data, claimed_time
- FROM {db_prefix}background_tasks
- WHERE claimed_time < {int:claim_limit}
- ORDER BY null
- LIMIT 1',
- array(
- 'claim_limit' => time() - MAX_CLAIM_THRESHOLD,
- )
- );
- if ($row = $smcFunc['db_fetch_assoc']($request))
- {
-
- $smcFunc['db_free_result']($request);
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}background_tasks
- SET claimed_time = {int:new_claimed}
- WHERE id_task = {int:task}
- AND claimed_time = {int:old_claimed}',
- array(
- 'new_claimed' => time(),
- 'task' => $row['id_task'],
- 'old_claimed' => $row['claimed_time'],
- )
- );
-
- if ($smcFunc['db_affected_rows']() != 0)
- {
-
- $row['claimed_time'] = time();
- return $row;
- }
- else
- {
-
- return fetch_task();
- }
- }
- else
- {
-
- $smcFunc['db_free_result']($request);
- return false;
- }
- }
- function perform_task($task_details)
- {
- global $sourcedir, $boarddir;
-
- if (!empty($task_details['task_file']))
- {
- $include = strtr(trim($task_details['task_file']), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir));
- if (file_exists($include))
- require_once($include);
- }
- if (empty($task_details['task_class']))
- {
-
- log_error('Invalid background task specified (no class, ' . (empty($task_details['task_file']) ? ' no file' : ' to load ' . $task_details['task_file']) . ')');
- return true;
- }
-
- elseif (class_exists($task_details['task_class']) && is_subclass_of($task_details['task_class'], 'SMF_BackgroundTask'))
- {
- $details = empty($task_details['task_data']) ? array() : unserialize($task_details['task_data']);
- $bgtask = new $task_details['task_class']($details);
- return $bgtask->execute();
- }
- else
- {
- log_error('Invalid background task specified: (class: ' . $task_details['task_class'] . ', ' . (empty($task_details['task_file']) ? ' no file' : ' to load ' . $task_details['task_file']) . ')');
- return true;
- }
- }
- function cleanRequest_cron()
- {
- global $scripturl, $boardurl;
- $scripturl = $boardurl . '/index.php';
-
- if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS']))
- die('Invalid request variable.');
-
- unset($GLOBALS['HTTP_POST_VARS'], $GLOBALS['HTTP_POST_VARS']);
- unset($GLOBALS['HTTP_POST_FILES'], $GLOBALS['HTTP_POST_FILES']);
- unset($GLOBALS['_GET'], $GLOBALS['_POST'], $GLOBALS['_REQUEST'], $GLOBALS['_COOKIE'], $GLOBALS['_FILES']);
- }
- function error_handler_cron($error_level, $error_string, $file, $line)
- {
- global $modSettings;
-
- if (error_reporting() == 0 || (defined('E_STRICT') && $error_level == E_STRICT && !empty($modSettings['enableErrorLogging'])))
- return;
- $error_type = 'cron';
- log_error($error_level . ': ' . $error_string, $error_type, $file, $line);
-
- if ($error_level % 255 == E_ERROR)
- die('No direct access...');
- }
- function obExit_cron()
- {
- if (FROM_CLI)
- die(0);
- else
- {
- header('Content-Type: image/gif');
- die("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B");
- }
- }
- abstract class SMF_BackgroundTask
- {
- protected $_details;
- public function __construct($details)
- {
- $this->_details = $details;
- }
- abstract public function execute();
- }
- ?>
|