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:
<?php
define('SMF', 'BACKGROUND');
define('SMF_VERSION', '2.1 RC2');
define('SMF_FULL_VERSION', 'SMF ' . SMF_VERSION);
define('SMF_SOFTWARE_YEAR', '2020');
define('FROM_CLI', empty($_SERVER['REQUEST_METHOD']));
define('JQUERY_VERSION', '3.4.1');
define('POSTGRE_TITLE', 'PostgreSQL');
define('MYSQL_TITLE', 'MySQL');
define('SMF_USER_AGENT', 'Mozilla/5.0 (' . php_uname('s') . ' ' . php_uname('m') . ') AppleWebKit/605.1.15 (KHTML, like Gecko) SMF/' . strtr(SMF_VERSION, ' ', '.'));
define('MAX_CRON_TIME', 10);
define('MAX_CLAIM_THRESHOLD', 300);
global $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;
if (!defined('TIME_START'))
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) || !is_dir($cachedir) || !is_writable($cachedir))
{
if (is_dir($boarddir . '/cache') && is_writable($boarddir . '/cache'))
$cachedir = $boarddir . '/cache';
else
{
$cachedir = sys_get_temp_dir() . '/smf_cache_' . md5($boarddir);
@mkdir($cachedir, 0750);
}
}
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)
{
header('Access-Control-Allow-Origin: *');
$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 . '/Security.php');
require_once($sourcedir . '/Subs.php');
$smcFunc = array();
unset ($db_show_debug);
loadDatabase();
reloadSettings();
set_error_handler('smf_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']('', '
SELECT id_task, task_file, task_class, task_data, claimed_time
FROM {db_prefix}background_tasks
WHERE claimed_time < {int:claim_limit}
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 $smcFunc, $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() : $smcFunc['json_decode']($task_details['task_data'], true);
$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 smf_error_handler_cron($error_level, $error_string, $file, $line)
{
global $modSettings;
if (error_reporting() == 0)
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
{
const RECEIVE_NOTIFY_EMAIL = 0x02;
const RECEIVE_NOTIFY_ALERT = 0x01;
protected $_details;
public function __construct($details)
{
$this->_details = $details;
}
abstract public function execute();
}
?>