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: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572:
<?php
if (!defined('SMF'))
die('No direct access...');
function log_error($error_message, $error_type = 'general', $file = null, $line = null)
{
global $modSettings, $sc, $user_info, $smcFunc, $scripturl, $last_error, $context, $db_show_debug;
static $tried_hook = false;
static $error_call = 0;
$error_call++;
if (!isset($db_show_debug) || $db_show_debug === false)
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
else
$backtrace = debug_backtrace();
if ($error_call > 2)
{
var_dump($backtrace);
die('Error loop.');
}
if (empty($modSettings['enableErrorLogging']))
return $error_message;
$error_message = strtr($error_message, array('<' => '<', '>' => '>', '"' => '"'));
$error_message = strtr($error_message, array('<br />' => '<br>', '<br>' => '<br>', '<b>' => '<strong>', '</b>' => '</strong>', "\n" => '<br>'));
if ($file == null)
$file = '';
else
$file = str_replace('\\', '/', $file);
if ($line == null)
$line = 0;
else
$line = (int) $line;
if (empty($user_info['id']))
$user_info['id'] = 0;
if (empty($user_info['ip']))
$user_info['ip'] = '';
$query_string = empty($_SERVER['QUERY_STRING']) ? (empty($_SERVER['REQUEST_URL']) ? '' : str_replace($scripturl, '', $_SERVER['REQUEST_URL'])) : $_SERVER['QUERY_STRING'];
if (!empty($smcFunc['htmlspecialchars']))
$query_string = $smcFunc['htmlspecialchars']((SMF == 'SSI' || SMF == 'BACKGROUND' ? '' : '?') . preg_replace(array('~;sesc=[^&;]+~', '~' . session_name() . '=' . session_id() . '[&;]~'), array(';sesc', ''), $query_string));
if (isset($_POST['board']) && !isset($_GET['board']))
$query_string .= ($query_string == '' ? 'board=' : ';board=') . $_POST['board'];
$known_error_types = array(
'general',
'critical',
'database',
'undefined_vars',
'user',
'ban',
'template',
'debug',
'cron',
'paidsubs',
'backup',
'login',
);
$other_error_types = array();
if (empty($tried_hook))
{
$tried_hook = true;
call_integration_hook('integrate_error_types', array(&$other_error_types, &$error_type, $error_message, $file, $line));
$known_error_types += $other_error_types;
}
$error_type = in_array($error_type, $known_error_types) && $error_type !== true ? $error_type : 'general';
array_splice($backtrace, 0, 1);
$backtrace = !empty($smcFunc['json_encode']) ? $smcFunc['json_encode']($backtrace) : json_encode($backtrace);
$error_info = array($user_info['id'], time(), $user_info['ip'], $query_string, $error_message, (string) $sc, $error_type, $file, $line, $backtrace);
if (empty($last_error) || $last_error != $error_info)
{
$smcFunc['db_error_insert']($error_info);
$last_error = $error_info;
if (!isset($context['num_errors']))
{
$query = $smcFunc['db_query']('', '
SELECT COUNT(id_error)
FROM {db_prefix}log_errors',
array()
);
list($context['num_errors']) = $smcFunc['db_fetch_row']($query);
$smcFunc['db_free_result']($query);
}
else
$context['num_errors']++;
}
$error_call = 0;
return $error_message;
}
function fatal_error($error, $log = 'general', $status = 500)
{
global $txt;
if (!empty($status))
send_http_status($status);
if (empty($txt))
die($error);
log_error_online($error);
setup_fatal_error_context($log ? log_error($error, $log) : $error);
}
function fatal_lang_error($error, $log = 'general', $sprintf = array(), $status = 403)
{
global $txt, $language, $user_info, $context;
static $fatal_error_called = false;
if (!empty($status))
send_http_status($status);
if (empty($context['theme_loaded']) && empty($fatal_error_called))
{
$fatal_error_called = true;
loadTheme();
}
if (empty($context['theme_loaded']))
die($error);
$reload_lang_file = true;
if ($log)
{
loadLanguage('Errors', $language);
$reload_lang_file = $language != $user_info['language'];
$error_message = empty($sprintf) ? $txt[$error] : vsprintf($txt[$error], $sprintf);
log_error($error_message, $log);
}
if ($reload_lang_file)
{
loadLanguage('Errors');
$error_message = empty($sprintf) ? $txt[$error] : vsprintf($txt[$error], $sprintf);
}
log_error_online($error, $sprintf);
setup_fatal_error_context($error_message, $error);
}
function smf_error_handler($error_level, $error_string, $file, $line)
{
global $settings, $modSettings, $db_show_debug;
if (error_reporting() == 0)
return;
if (strpos($file, 'eval()') !== false && !empty($settings['current_include_filename']))
{
$array = debug_backtrace();
$count = count($array);
for ($i = 0; $i < $count; $i++)
{
if ($array[$i]['function'] != 'loadSubTemplate')
continue;
if (empty($array[$i]['args']))
$i++;
break;
}
if (isset($array[$i]) && !empty($array[$i]['args']))
$file = realpath($settings['current_include_filename']) . ' (' . $array[$i]['args'][0] . ' sub template - eval?)';
else
$file = realpath($settings['current_include_filename']) . ' (eval?)';
}
if (isset($db_show_debug) && $db_show_debug === true)
{
if ($error_level % 255 != E_ERROR)
{
$temporary = ob_get_contents();
if (substr($temporary, -2) == '="')
echo '"';
}
echo '<br>
<strong>', $error_level % 255 == E_ERROR ? 'Error' : ($error_level % 255 == E_WARNING ? 'Warning' : 'Notice'), '</strong>: ', $error_string, ' in <strong>', $file, '</strong> on line <strong>', $line, '</strong><br>';
}
$error_type = stripos($error_string, 'undefined') !== false ? 'undefined_vars' : 'general';
$message = log_error($error_level . ': ' . $error_string, $error_type, $file, $line);
call_integration_hook('integrate_output_error', array($message, $error_type, $error_level, $file, $line));
if ($file == 'Unknown')
return;
if ($error_level % 255 == E_ERROR)
obExit(false);
else
return;
if ($error_level % 255 == E_ERROR || $error_level % 255 == E_WARNING)
fatal_error(allowedTo('admin_forum') ? $message : $error_string, false);
if ($error_level % 255 == E_ERROR)
die('No direct access...');
}
function setup_fatal_error_context($error_message, $error_code = null)
{
global $context, $txt, $ssi_on_error_method;
static $level = 0;
++$level;
if ($level > 1)
return false;
if (SMF != 'SSI' && SMF != 'BACKGROUND' && empty($context['theme_loaded']))
loadTheme();
$context['robot_no_index'] = true;
if (!isset($context['error_title']))
$context['error_title'] = $txt['error_occured'];
$context['error_message'] = isset($context['error_message']) ? $context['error_message'] : $error_message;
$context['error_code'] = isset($error_code) ? 'id="' . $error_code . '" ' : '';
if (empty($context['page_title']))
$context['page_title'] = $context['error_title'];
loadTemplate('Errors');
$context['sub_template'] = 'fatal_error';
if (SMF == 'SSI')
{
if (!empty($ssi_on_error_method) && $ssi_on_error_method !== true && is_callable($ssi_on_error_method))
$ssi_on_error_method();
elseif (empty($ssi_on_error_method) || $ssi_on_error_method !== true)
loadSubTemplate('fatal_error');
if (empty($ssi_on_error_method) || $ssi_on_error_method !== true)
exit;
}
elseif (SMF == 'BACKGROUND')
{
if (defined('FROM_CLI') && FROM_CLI)
echo 'cron error: ', $context['error_message'];
else
echo 'An error occurred. More information may be available in your logs.';
exit;
}
obExit(null, true, false, true);
trigger_error('Hacking attempt...', E_USER_ERROR);
}
function display_maintenance_message()
{
global $maintenance, $mtitle, $mmessage;
set_fatal_error_headers();
if (!empty($maintenance))
echo '<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="noindex">
<title>', $mtitle, '</title>
</head>
<body>
<h3>', $mtitle, '</h3>
', $mmessage, '
</body>
</html>';
die();
}
function display_db_error()
{
global $mbname, $modSettings, $maintenance;
global $db_connection, $webmaster_email, $db_last_error, $db_error_send, $smcFunc, $sourcedir, $cache_enable;
require_once($sourcedir . '/Logging.php');
set_fatal_error_headers();
$cache_enable = '1';
if (($temp = cache_get_data('db_last_error', 600)) !== null)
$db_last_error = max($db_last_error, $temp);
if ($db_last_error < time() - 3600 * 24 * 3 && empty($maintenance) && !empty($db_error_send))
{
cache_put_data('db_last_error', time(), 600);
if (($temp = cache_get_data('db_last_error', 600)) === null)
logLastDatabaseError();
$db_error = @$smcFunc['db_error']($db_connection);
@mail($webmaster_email, $mbname . ': SMF Database Error!', 'There has been a problem with the database!' . ($db_error == '' ? '' : "\n" . $smcFunc['db_title'] . ' reported:' . "\n" . $db_error) . "\n\n" . 'This is a notice email to let you know that SMF could not connect to the database, contact your host if this continues.');
}
echo '<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="noindex">
<title>Connection Problems</title>
</head>
<body>
<h3>Connection Problems</h3>
Sorry, SMF was unable to connect to the database. This may be caused by the server being busy. Please try again later.
</body>
</html>';
die();
}
function display_loadavg_error()
{
set_fatal_error_headers();
echo '<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="noindex">
<title>Temporarily Unavailable</title>
</head>
<body>
<h3>Temporarily Unavailable</h3>
Due to high stress on the server the forum is temporarily unavailable. Please try again later.
</body>
</html>';
die();
}
function set_fatal_error_headers()
{
if (headers_sent())
return;
header('expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('last-modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('cache-control: no-cache');
send_http_status(503, 'Service Temporarily Unavailable');
header('status: 503 Service Temporarily Unavailable');
header('retry-after: 3600');
}
function log_error_online($error, $sprintf = array())
{
global $smcFunc, $user_info, $modSettings;
if (empty($modSettings['who_enabled']))
return;
if (SMF == 'SSI' || SMF == 'BACKGROUND')
return;
$session_id = !empty($user_info['is_guest']) ? 'ip' . $user_info['ip'] : session_id();
$request = $smcFunc['db_query']('', '
SELECT url
FROM {db_prefix}log_online
WHERE session = {string:session}',
array(
'session' => $session_id,
)
);
if ($smcFunc['db_num_rows']($request) != 0)
{
if (!isset($smcFunc['json_decode']))
{
$smcFunc['json_decode'] = 'smf_json_decode';
$smcFunc['json_encode'] = 'json_encode';
}
list ($url) = $smcFunc['db_fetch_row']($request);
$url = $smcFunc['json_decode']($url, true);
$url['error'] = $error;
if (strlen($url['error']) > 500)
$url['error'] = substr($url['error'], 0, 500);
if (!empty($sprintf))
$url['error_params'] = $sprintf;
$smcFunc['db_query']('', '
UPDATE {db_prefix}log_online
SET url = {string:url}
WHERE session = {string:session}',
array(
'url' => $smcFunc['json_encode']($url),
'session' => $session_id,
)
);
}
$smcFunc['db_free_result']($request);
}
?>