123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860 |
- <?php
- /**
- * The purpose of this file is... errors. (hard to guess, I guess?) It takes
- * care of logging, error messages, error handling, database errors, and
- * error log administration.
- *
- * Simple Machines Forum (SMF)
- *
- * @package SMF
- * @author Simple Machines http://www.simplemachines.org
- * @copyright 2011 Simple Machines
- * @license http://www.simplemachines.org/about/smf/license.php BSD
- *
- * @version 2.0
- */
- if (!defined('SMF'))
- die('Hacking attempt...');
- /**
- * Handle fatal errors - like connection errors or load average problems.
- * This calls show_db_error(), which is used for database connection error handling.
- * @todo when awake: clean up this terrible terrible ugliness.
- *
- * @param bool $loadavg - whether it's a load average problem...
- */
- function db_fatal_error($loadavg = false)
- {
- global $sourcedir;
- show_db_error($loadavg);
- // Since we use "or db_fatal_error();" this is needed...
- return false;
- }
- /**
- * Log an error, if the error logging is enabled.
- * filename and line should be __FILE__ and __LINE__, respectively.
- * Example use:
- * die(log_error($msg));
- * @param string $error_message
- * @param string $error_type = 'general'
- * @param string $file = null
- * @param int $line = null
- * @return string, the error message
- */
- function log_error($error_message, $error_type = 'general', $file = null, $line = null)
- {
- global $txt, $modSettings, $sc, $user_info, $smcFunc, $scripturl, $last_error;
- // Check if error logging is actually on.
- if (empty($modSettings['enableErrorLogging']))
- return $error_message;
- // Basically, htmlspecialchars it minus &. (for entities!)
- $error_message = strtr($error_message, array('<' => '<', '>' => '>', '"' => '"'));
- $error_message = strtr($error_message, array('<br />' => '<br />', '<b>' => '<strong>', '</b>' => '</strong>', "\n" => '<br />'));
- // Add a file and line to the error message?
- // Don't use the actual txt entries for file and line but instead use %1$s for file and %2$s for line
- if ($file == null)
- $file = '';
- else
- // Window style slashes don't play well, lets convert them to the unix style.
- $file = str_replace('\\', '/', $file);
- if ($line == null)
- $line = 0;
- else
- $line = (int) $line;
- // Just in case there's no id_member or IP set yet.
- if (empty($user_info['id']))
- $user_info['id'] = 0;
- if (empty($user_info['ip']))
- $user_info['ip'] = '';
- // Find the best query string we can...
- $query_string = empty($_SERVER['QUERY_STRING']) ? (empty($_SERVER['REQUEST_URL']) ? '' : str_replace($scripturl, '', $_SERVER['REQUEST_URL'])) : $_SERVER['QUERY_STRING'];
- // Don't log the session hash in the url twice, it's a waste.
- $query_string = htmlspecialchars((SMF == 'SSI' ? '' : '?') . preg_replace(array('~;sesc=[^&;]+~', '~' . session_name() . '=' . session_id() . '[&;]~'), array(';sesc', ''), $query_string));
- // Just so we know what board error messages are from.
- if (isset($_POST['board']) && !isset($_GET['board']))
- $query_string .= ($query_string == '' ? 'board=' : ';board=') . $_POST['board'];
- // What types of categories do we have?
- $known_error_types = array(
- 'general',
- 'critical',
- 'database',
- 'undefined_vars',
- 'user',
- 'template',
- 'debug',
- );
- // Make sure the category that was specified is a valid one
- $error_type = in_array($error_type, $known_error_types) && $error_type !== true ? $error_type : 'general';
- // Don't log the same error countless times, as we can get in a cycle of depression...
- $error_info = array($user_info['id'], time(), $user_info['ip'], $query_string, $error_message, (string) $sc, $error_type, $file, $line);
- if (empty($last_error) || $last_error != $error_info)
- {
- // Insert the error into the database.
- $smcFunc['db_insert']('',
- '{db_prefix}log_errors',
- array('id_member' => 'int', 'log_time' => 'int', 'ip' => 'string-16', 'url' => 'string-65534', 'message' => 'string-65534', 'session' => 'string', 'error_type' => 'string', 'file' => 'string-255', 'line' => 'int'),
- $error_info,
- array('id_error')
- );
- $last_error = $error_info;
- }
- // Return the message to make things simpler.
- return $error_message;
- }
- /**
- * This function logs an action in the respective log. (database log)
- * @example logAction('remove', array('starter' => $id_member_started));
- *
- * @param string $action
- * @param array $extra = array()
- * @param string $log_type, options 'moderate', 'admin', ...etc.
- */
- function logAction($action, $extra = array(), $log_type = 'moderate')
- {
- global $modSettings, $user_info, $smcFunc, $sourcedir;
- $log_types = array(
- 'moderate' => 1,
- 'user' => 2,
- 'admin' => 3,
- );
- if (!is_array($extra))
- trigger_error('logAction(): data is not an array with action \'' . $action . '\'', E_USER_NOTICE);
- // Pull out the parts we want to store separately, but also make sure that the data is proper
- if (isset($extra['topic']))
- {
- if (!is_numeric($extra['topic']))
- trigger_error('logAction(): data\'s topic is not a number', E_USER_NOTICE);
- $topic_id = empty($extra['topic']) ? '0' : (int)$extra['topic'];
- unset($extra['topic']);
- }
- else
- $topic_id = '0';
- if (isset($extra['message']))
- {
- if (!is_numeric($extra['message']))
- trigger_error('logAction(): data\'s message is not a number', E_USER_NOTICE);
- $msg_id = empty($extra['message']) ? '0' : (int)$extra['message'];
- unset($extra['message']);
- }
- else
- $msg_id = '0';
- // Is there an associated report on this?
- if (in_array($action, array('move', 'remove', 'split', 'merge')))
- {
- $request = $smcFunc['db_query']('', '
- SELECT id_report
- FROM {db_prefix}log_reported
- WHERE {raw:column_name} = {int:reported}
- LIMIT 1',
- array(
- 'column_name' => !empty($msg_id) ? 'id_msg' : 'id_topic',
- 'reported' => !empty($msg_id) ? $msg_id : $topic_id,
- ));
- // Alright, if we get any result back, update open reports.
- if ($smcFunc['db_num_rows']($request) > 0)
- {
- require_once($sourcedir . '/ModerationCenter.php');
- updateSettings(array('last_mod_report_action' => time()));
- recountOpenReports();
- }
- $smcFunc['db_free_result']($request);
- }
- // No point in doing anything else, if the log isn't even enabled.
- if (empty($modSettings['modlog_enabled']) || !isset($log_types[$log_type]))
- return false;
- if (isset($extra['member']) && !is_numeric($extra['member']))
- trigger_error('logAction(): data\'s member is not a number', E_USER_NOTICE);
- if (isset($extra['board']))
- {
- if (!is_numeric($extra['board']))
- trigger_error('logAction(): data\'s board is not a number', E_USER_NOTICE);
- $board_id = empty($extra['board']) ? '0' : (int)$extra['board'];
- unset($extra['board']);
- }
- else
- $board_id = '0';
- if (isset($extra['board_to']))
- {
- if (!is_numeric($extra['board_to']))
- trigger_error('logAction(): data\'s board_to is not a number', E_USER_NOTICE);
- if (empty($board_id))
- {
- $board_id = empty($extra['board_to']) ? '0' : (int)$extra['board_to'];
- unset($extra['board_to']);
- }
- }
- $smcFunc['db_insert']('',
- '{db_prefix}log_actions',
- array(
- 'log_time' => 'int', 'id_log' => 'int', 'id_member' => 'int', 'ip' => 'string-16', 'action' => 'string',
- 'id_board' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'extra' => 'string-65534',
- ),
- array(
- time(), $log_types[$log_type], $user_info['id'], $user_info['ip'], $action,
- $board_id, $topic_id, $msg_id, serialize($extra),
- ),
- array('id_action')
- );
- return $smcFunc['db_insert_id']('{db_prefix}log_actions', 'id_action');
- }
- /**
- * Debugging.
- */
- function db_debug_junk()
- {
- global $context, $scripturl, $boarddir, $modSettings, $boarddir;
- global $db_cache, $db_count, $db_show_debug, $cache_count, $cache_hits, $txt;
- // Add to Settings.php if you want to show the debugging information.
- if (!isset($db_show_debug) || $db_show_debug !== true || (isset($_GET['action']) && $_GET['action'] == 'viewquery') || WIRELESS)
- return;
- if (empty($_SESSION['view_queries']))
- $_SESSION['view_queries'] = 0;
- if (empty($context['debug']['language_files']))
- $context['debug']['language_files'] = array();
- if (empty($context['debug']['sheets']))
- $context['debug']['sheets'] = array();
- $files = get_included_files();
- $total_size = 0;
- for ($i = 0, $n = count($files); $i < $n; $i++)
- {
- if (file_exists($files[$i]))
- $total_size += filesize($files[$i]);
- $files[$i] = strtr($files[$i], array($boarddir => '.'));
- }
- $warnings = 0;
- if (!empty($db_cache))
- {
- foreach ($db_cache as $q => $qq)
- {
- if (!empty($qq['w']))
- $warnings += count($qq['w']);
- }
- $_SESSION['debug'] = &$db_cache;
- }
- // Gotta have valid HTML ;).
- $temp = ob_get_contents();
- if (function_exists('ob_clean'))
- ob_clean();
- else
- {
- ob_end_clean();
- ob_start('ob_sessrewrite');
- }
- echo preg_replace('~</body>\s*</html>~', '', $temp), '
- <div class="smalltext" style="text-align: left; margin: 1ex;">
- ', $txt['debug_templates'], count($context['debug']['templates']), ': <em>', implode('</em>, <em>', $context['debug']['templates']), '</em>.<br />
- ', $txt['debug_subtemplates'], count($context['debug']['sub_templates']), ': <em>', implode('</em>, <em>', $context['debug']['sub_templates']), '</em>.<br />
- ', $txt['debug_language_files'], count($context['debug']['language_files']), ': <em>', implode('</em>, <em>', $context['debug']['language_files']), '</em>.<br />
- ', $txt['debug_stylesheets'], count($context['debug']['sheets']), ': <em>', implode('</em>, <em>', $context['debug']['sheets']), '</em>.<br />
- ', $txt['debug_files_included'], count($files), ' - ', round($total_size / 1024), $txt['debug_kb'], ' (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_include_info\').style.display = \'inline\'; this.style.display = \'none\'; return false;">', $txt['debug_show'], '</a><span id="debug_include_info" style="display: none;"><em>', implode('</em>, <em>', $files), '</em></span>)<br />';
- if (!empty($modSettings['cache_enable']) && !empty($cache_hits))
- {
- $entries = array();
- $total_t = 0;
- $total_s = 0;
- foreach ($cache_hits as $cache_hit)
- {
- $entries[] = $cache_hit['d'] . ' ' . $cache_hit['k'] . ': ' . sprintf($txt['debug_cache_seconds_bytes'], comma_format($cache_hit['t'], 5), $cache_hit['s']);
- $total_t += $cache_hit['t'];
- $total_s += $cache_hit['s'];
- }
- echo '
- ', $txt['debug_cache_hits'], $cache_count, ': ', sprintf($txt['debug_cache_seconds_bytes_total'], comma_format($total_t, 5), comma_format($total_s)), ' (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_cache_info\').style.display = \'inline\'; this.style.display = \'none\'; return false;">', $txt['debug_show'], '</a><span id="debug_cache_info" style="display: none;"><em>', implode('</em>, <em>', $entries), '</em></span>)<br />';
- }
- echo '
- <a href="', $scripturl, '?action=viewquery" target="_blank" class="new_win">', $warnings == 0 ? sprintf($txt['debug_queries_used'], (int) $db_count) : sprintf($txt['debug_queries_used_and_warnings'], (int) $db_count, $warnings), '</a><br />
- <br />';
- if ($_SESSION['view_queries'] == 1 && !empty($db_cache))
- foreach ($db_cache as $q => $qq)
- {
- $is_select = substr(trim($qq['q']), 0, 6) == 'SELECT' || preg_match('~^INSERT(?: IGNORE)? INTO \w+(?:\s+\([^)]+\))?\s+SELECT .+$~s', trim($qq['q'])) != 0;
- // Temporary tables created in earlier queries are not explainable.
- if ($is_select)
- {
- foreach (array('log_topics_unread', 'topics_posted_in', 'tmp_log_search_topics', 'tmp_log_search_messages') as $tmp)
- if (strpos(trim($qq['q']), $tmp) !== false)
- {
- $is_select = false;
- break;
- }
- }
- // But actual creation of the temporary tables are.
- elseif (preg_match('~^CREATE TEMPORARY TABLE .+?SELECT .+$~s', trim($qq['q'])) != 0)
- $is_select = true;
- // Make the filenames look a bit better.
- if (isset($qq['f']))
- $qq['f'] = preg_replace('~^' . preg_quote($boarddir, '~') . '~', '...', $qq['f']);
- echo '
- <strong>', $is_select ? '<a href="' . $scripturl . '?action=viewquery;qq=' . ($q + 1) . '#qq' . $q . '" target="_blank" class="new_win" style="text-decoration: none;">' : '', nl2br(str_replace("\t", ' ', htmlspecialchars(ltrim($qq['q'], "\n\r")))) . ($is_select ? '</a></strong>' : '</strong>') . '<br />
- ';
- if (!empty($qq['f']) && !empty($qq['l']))
- echo sprintf($txt['debug_query_in_line'], $qq['f'], $qq['l']);
- if (isset($qq['s'], $qq['t']) && isset($txt['debug_query_which_took_at']))
- echo sprintf($txt['debug_query_which_took_at'], round($qq['t'], 8), round($qq['s'], 8)) . '<br />';
- elseif (isset($qq['t']))
- echo sprintf($txt['debug_query_which_took'], round($qq['t'], 8)) . '<br />';
- echo '
- <br />';
- }
- echo '
- <a href="' . $scripturl . '?action=viewquery;sa=hide">', $txt['debug_' . (empty($_SESSION['view_queries']) ? 'show' : 'hide') . '_queries'], '</a>
- </div></body></html>';
- }
- /**
- * Logs the last database error into a file.
- * Attempts to use the backup file first, to store the last database error
- * and only update Settings.php if the first was successful.
- */
- function updateLastDatabaseError()
- {
- global $boarddir;
- // Find out this way if we can even write things on this filesystem.
- // In addition, store things first in the backup file
- $last_settings_change = @filemtime($boarddir . '/Settings.php');
- // Make sure the backup file is there...
- $file = $boarddir . '/Settings_bak.php';
- if ((!file_exists($file) || filesize($file) == 0) && !copy($boarddir . '/Settings.php', $file))
- return false;
- // ...and writable!
- if (!is_writable($file))
- {
- chmod($file, 0755);
- if (!is_writable($file))
- {
- chmod($file, 0775);
- if (!is_writable($file))
- {
- chmod($file, 0777);
- if (!is_writable($file))
- return false;
- }
- }
- }
- // Put the new timestamp.
- $data = file_get_contents($file);
- $data = preg_replace('~\$db_last_error = \d+;~', '$db_last_error = ' . time() . ';', $data);
- // Open the backup file for writing
- if ($fp = @fopen($file, 'w'))
- {
- // Reset the file buffer.
- set_file_buffer($fp, 0);
- // Update the file.
- $t = flock($fp, LOCK_EX);
- $bytes = fwrite($fp, $data);
- flock($fp, LOCK_UN);
- fclose($fp);
- // Was it a success?
- // ...only relevant if we're still dealing with the same good ole' settings file.
- clearstatcache();
- if (($bytes == strlen($data)) && (filemtime($boarddir . '/Settings.php') === $last_settings_change))
- {
- // This is our new Settings file...
- // At least this one is an atomic operation
- @copy($file, $boarddir . '/Settings.php');
- return true;
- }
- else
- {
- // Oops. Someone might have been faster
- // or we have no more disk space left, troubles, troubles...
- // Copy the file back and run for your life!
- @copy($boarddir . '/Settings.php', $file);
- }
- }
- return false;
- }
- /**
- * An irrecoverable error. This function stops execution and displays an error message.
- * It logs the error message if $log is specified.
- * @param string $error
- * @param string $log = 'general'
- */
- function fatal_error($error, $log = 'general')
- {
- global $txt, $context, $modSettings;
- // We don't have $txt yet, but that's okay...
- if (empty($txt))
- die($error);
- setup_fatal_error_context($log || (!empty($modSettings['enableErrorLogging']) && $modSettings['enableErrorLogging'] == 2) ? log_error($error, $log) : $error);
- }
- /**
- * A fatal error with a message stored in the language file.
- * This function stops executing and displays an error message by key.
- * It uses the string with the error_message_key key.
- * It logs the error in the forum's default language while displaying the error
- * message in the user's language.
- * @uses Errors language file and applies the $sprintf information if specified.
- * the information is logged if log is specified.
- * @param $error
- * @param $log
- * @param $sprintf
- */
- function fatal_lang_error($error, $log = 'general', $sprintf = array())
- {
- global $txt, $language, $modSettings, $user_info, $context;
- static $fatal_error_called = false;
- // Try to load a theme if we don't have one.
- if (empty($context['theme_loaded']) && empty($fatal_error_called))
- {
- $fatal_error_called = true;
- loadTheme();
- }
- // If we have no theme stuff we can't have the language file...
- if (empty($context['theme_loaded']))
- die($error);
- $reload_lang_file = true;
- // Log the error in the forum's language, but don't waste the time if we aren't logging
- if ($log || (!empty($modSettings['enableErrorLogging']) && $modSettings['enableErrorLogging'] == 2))
- {
- 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);
- }
- // Load the language file, only if it needs to be reloaded
- if ($reload_lang_file)
- {
- loadLanguage('Errors');
- $error_message = empty($sprintf) ? $txt[$error] : vsprintf($txt[$error], $sprintf);
- }
- setup_fatal_error_context($error_message);
- }
- /**
- * Handler for standard error messages, standard PHP error handler replacement.
- * It dies with fatal_error() if the error_level matches with error_reporting.
- * @param int $error_level
- * @param string $error_string
- * @param string $file
- * @param int $line
- */
- function error_handler($error_level, $error_string, $file, $line)
- {
- global $settings, $modSettings, $db_show_debug;
- // Ignore errors if we're ignoring them or they are strict notices from PHP 5 (which cannot be solved without breaking PHP 4.)
- if (error_reporting() == 0 || (defined('E_STRICT') && $error_level == E_STRICT && (empty($modSettings['enableErrorLogging']) || $modSettings['enableErrorLogging'] != 2)))
- return;
- if (strpos($file, 'eval()') !== false && !empty($settings['current_include_filename']))
- {
- if (function_exists('debug_backtrace'))
- {
- $array = debug_backtrace();
- for ($i = 0; $i < count($array); $i++)
- {
- if ($array[$i]['function'] != 'loadSubTemplate')
- continue;
- // This is a bug in PHP, with eval, it seems!
- 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?)';
- }
- else
- $file = realpath($settings['current_include_filename']) . ' (eval?)';
- }
- if (isset($db_show_debug) && $db_show_debug === true)
- {
- // Commonly, undefined indexes will occur inside attributes; try to show them anyway!
- if ($error_level % 255 != E_ERROR)
- {
- $temporary = ob_get_contents();
- if (substr($temporary, -2) == '="')
- echo '"';
- }
- // Debugging! This should look like a PHP error message.
- 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 = strpos(strtolower($error_string), 'undefined') !== false ? 'undefined_vars' : 'general';
- $message = log_error($error_level . ': ' . $error_string, $error_type, $file, $line);
- // Let's give integrations a chance to ouput a bit differently
- call_integration_hook('integrate_output_error', array($message, $error_type, $error_level, $file, $line));
- // Dying on these errors only causes MORE problems (blank pages!)
- if ($file == 'Unknown')
- return;
- // If this is an E_ERROR or E_USER_ERROR.... die. Violently so.
- if ($error_level % 255 == E_ERROR)
- obExit(false);
- else
- return;
- // If this is an E_ERROR, E_USER_ERROR, E_WARNING, or E_USER_WARNING.... die. Violently so.
- if ($error_level % 255 == E_ERROR || $error_level % 255 == E_WARNING)
- fatal_error(allowedTo('admin_forum') ? $message : $error_string, false);
- // We should NEVER get to this point. Any fatal error MUST quit, or very bad things can happen.
- if ($error_level % 255 == E_ERROR)
- die('Hacking attempt...');
- }
- /**
- * It is called by fatal_error() and fatal_lang_error().
- * @uses Errors template, fatal_error sub template, or Wireless template,
- * error sub template.
- * @param string $error_message
- */
- function setup_fatal_error_context($error_message)
- {
- global $context, $txt, $ssi_on_error_method;
- static $level = 0;
- // Attempt to prevent a recursive loop.
- ++$level;
- if ($level > 1)
- return false;
- // Maybe they came from dlattach or similar?
- if (SMF != 'SSI' && empty($context['theme_loaded']))
- loadTheme();
- // Don't bother indexing errors mate...
- $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;
- if (empty($context['page_title']))
- $context['page_title'] = $context['error_title'];
- // Display the error message - wireless?
- if (defined('WIRELESS') && WIRELESS)
- $context['sub_template'] = WIRELESS_PROTOCOL . '_error';
- // Load the template and set the sub template.
- else
- {
- loadTemplate('Errors');
- $context['sub_template'] = 'fatal_error';
- }
- // If this is SSI, what do they want us to do?
- 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');
- // No layers?
- if (empty($ssi_on_error_method) || $ssi_on_error_method !== true)
- exit;
- }
- // We want whatever for the header, and a footer. (footer includes sub template!)
- obExit(null, true, false, true);
- /* DO NOT IGNORE:
- If you are creating a bridge to SMF or modifying this function, you MUST
- make ABSOLUTELY SURE that this function quits and DOES NOT RETURN TO NORMAL
- PROGRAM FLOW. Otherwise, security error messages will not be shown, and
- your forum will be in a very easily hackable state.
- */
- trigger_error('Hacking attempt...', E_USER_ERROR);
- }
- /**
- * Show an error message for the connection problems... or load average.
- * It is called by db_fatal_error() function.
- * It shows a complete page independent of language files or themes.
- * It is used only if there's no way to connect to the database or the load averages
- * are too high to do so.
- * It stops further execution of the script.
- * @param bool $loadavg - whether it's a load average problem...
- */
- function show_db_error($loadavg = false)
- {
- global $sourcedir, $mbname, $maintenance, $mtitle, $mmessage, $modSettings;
- global $db_connection, $webmaster_email, $db_last_error, $db_error_send, $smcFunc;
- // Don't cache this page!
- 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 the right error codes.
- header('HTTP/1.1 503 Service Temporarily Unavailable');
- header('Status: 503 Service Temporarily Unavailable');
- header('Retry-After: 3600');
- if ($loadavg == false)
- {
- // For our purposes, we're gonna want this on if at all possible.
- $modSettings['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))
- {
- // Avoid writing to the Settings.php file if at all possible; use shared memory instead.
- cache_put_data('db_last_error', time(), 600);
- if (($temp = cache_get_data('db_last_error', 600)) == null)
- updateLastDatabaseError();
- // Language files aren't loaded yet :(.
- $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.');
- }
- }
- if (!empty($maintenance))
- echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta name="robots" content="noindex" />
- <title>', $mtitle, '</title>
- </head>
- <body>
- <h3>', $mtitle, '</h3>
- ', $mmessage, '
- </body>
- </html>';
- // If this is a load average problem, display an appropriate message (but we still don't have language files!)
- elseif ($loadavg)
- echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <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>';
- // What to do? Language files haven't and can't be loaded yet...
- else
- echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <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;
- }
- /**
- * Put this user in the online log.
- *
- * @param bool $force = false
- */
- function writeLog($force = false)
- {
- global $user_info, $user_settings, $context, $modSettings, $settings, $topic, $board, $smcFunc, $sourcedir;
- // If we are showing who is viewing a topic, let's see if we are, and force an update if so - to make it accurate.
- if (!empty($settings['display_who_viewing']) && ($topic || $board))
- {
- // Take the opposite approach!
- $force = true;
- // Don't update for every page - this isn't wholly accurate but who cares.
- if ($topic)
- {
- if (isset($_SESSION['last_topic_id']) && $_SESSION['last_topic_id'] == $topic)
- $force = false;
- $_SESSION['last_topic_id'] = $topic;
- }
- }
- // Are they a spider we should be tracking? Mode = 1 gets tracked on its spider check...
- if (!empty($user_info['possibly_robot']) && !empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1)
- {
- require_once($sourcedir . '/ManageSearchEngines.php');
- logSpider();
- }
- // Don't mark them as online more than every so often.
- if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= (time() - 8) && !$force)
- return;
- if (!empty($modSettings['who_enabled']))
- {
- $serialized = $_GET + array('USER_AGENT' => $_SERVER['HTTP_USER_AGENT']);
- // In the case of a dlattach action, session_var may not be set.
- if (!isset($context['session_var']))
- $context['session_var'] = $_SESSION['session_var'];
- unset($serialized['sesc'], $serialized[$context['session_var']]);
- $serialized = serialize($serialized);
- }
- else
- $serialized = '';
- // Guests use 0, members use their session ID.
- $session_id = $user_info['is_guest'] ? 'ip' . $user_info['ip'] : session_id();
- // Grab the last all-of-SMF-specific log_online deletion time.
- $do_delete = cache_get_data('log_online-update', 30) < time() - 30;
- // If the last click wasn't a long time ago, and there was a last click...
- if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= time() - $modSettings['lastActive'] * 20)
- {
- if ($do_delete)
- {
- $smcFunc['db_query']('delete_log_online_interval', '
- DELETE FROM {db_prefix}log_online
- WHERE log_time < {int:log_time}
- AND session != {string:session}',
- array(
- 'log_time' => time() - $modSettings['lastActive'] * 60,
- 'session' => $session_id,
- )
- );
- // Cache when we did it last.
- cache_put_data('log_online-update', time(), 30);
- }
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}log_online
- SET log_time = {int:log_time}, ip = IFNULL(INET_ATON({string:ip}), 0), url = {string:url}
- WHERE session = {string:session}',
- array(
- 'log_time' => time(),
- 'ip' => $user_info['ip'],
- 'url' => $serialized,
- 'session' => $session_id,
- )
- );
- // Guess it got deleted.
- if ($smcFunc['db_affected_rows']() == 0)
- $_SESSION['log_time'] = 0;
- }
- else
- $_SESSION['log_time'] = 0;
- // Otherwise, we have to delete and insert.
- if (empty($_SESSION['log_time']))
- {
- if ($do_delete || !empty($user_info['id']))
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}log_online
- WHERE ' . ($do_delete ? 'log_time < {int:log_time}' : '') . ($do_delete && !empty($user_info['id']) ? ' OR ' : '') . (empty($user_info['id']) ? '' : 'id_member = {int:current_member}'),
- array(
- 'current_member' => $user_info['id'],
- 'log_time' => time() - $modSettings['lastActive'] * 60,
- )
- );
- $smcFunc['db_insert']($do_delete ? 'ignore' : 'replace',
- '{db_prefix}log_online',
- array('session' => 'string', 'id_member' => 'int', 'id_spider' => 'int', 'log_time' => 'int', 'ip' => 'raw', 'url' => 'string'),
- array($session_id, $user_info['id'], empty($_SESSION['id_robot']) ? 0 : $_SESSION['id_robot'], time(), 'IFNULL(INET_ATON(\'' . $user_info['ip'] . '\'), 0)', $serialized),
- array('session')
- );
- }
- // Mark your session as being logged.
- $_SESSION['log_time'] = time();
- // Well, they are online now.
- if (empty($_SESSION['timeOnlineUpdated']))
- $_SESSION['timeOnlineUpdated'] = time();
- // Set their login time, if not already done within the last minute.
- if (SMF != 'SSI' && !empty($user_info['last_login']) && $user_info['last_login'] < time() - 60)
- {
- // Don't count longer than 15 minutes.
- if (time() - $_SESSION['timeOnlineUpdated'] > 60 * 15)
- $_SESSION['timeOnlineUpdated'] = time();
- $user_settings['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated'];
- updateMemberData($user_info['id'], array('last_login' => time(), 'member_ip' => $user_info['ip'], 'member_ip2' => $_SERVER['BAN_CHECK_IP'], 'total_time_logged_in' => $user_settings['total_time_logged_in']));
- if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
- cache_put_data('user_settings-' . $user_info['id'], $user_settings, 60);
- $user_info['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated'];
- $_SESSION['timeOnlineUpdated'] = time();
- }
- }
- ?>
|