Chris@76: 'smf_db_query', Chris@76: 'db_quote' => 'smf_db_quote', Chris@76: 'db_fetch_assoc' => 'mysql_fetch_assoc', Chris@76: 'db_fetch_row' => 'mysql_fetch_row', Chris@76: 'db_free_result' => 'mysql_free_result', Chris@76: 'db_insert' => 'smf_db_insert', Chris@76: 'db_insert_id' => 'smf_db_insert_id', Chris@76: 'db_num_rows' => 'mysql_num_rows', Chris@76: 'db_data_seek' => 'mysql_data_seek', Chris@76: 'db_num_fields' => 'mysql_num_fields', Chris@76: 'db_escape_string' => 'addslashes', Chris@76: 'db_unescape_string' => 'stripslashes', Chris@76: 'db_server_info' => 'mysql_get_server_info', Chris@76: 'db_affected_rows' => 'smf_db_affected_rows', Chris@76: 'db_transaction' => 'smf_db_transaction', Chris@76: 'db_error' => 'mysql_error', Chris@76: 'db_select_db' => 'mysql_select_db', Chris@76: 'db_title' => 'MySQL', Chris@76: 'db_sybase' => false, Chris@76: 'db_case_sensitive' => false, Chris@76: 'db_escape_wildcard_string' => 'smf_db_escape_wildcard_string', Chris@76: ); Chris@76: Chris@76: if (!empty($db_options['persist'])) Chris@76: $connection = @mysql_pconnect($db_server, $db_user, $db_passwd); Chris@76: else Chris@76: $connection = @mysql_connect($db_server, $db_user, $db_passwd); Chris@76: Chris@76: // Something's wrong, show an error if its fatal (which we assume it is) Chris@76: if (!$connection) Chris@76: { Chris@76: if (!empty($db_options['non_fatal'])) Chris@76: return null; Chris@76: else Chris@76: db_fatal_error(); Chris@76: } Chris@76: Chris@76: // Select the database, unless told not to Chris@76: if (empty($db_options['dont_select_db']) && !@mysql_select_db($db_name, $connection) && empty($db_options['non_fatal'])) Chris@76: db_fatal_error(); Chris@76: Chris@76: // This makes it possible to have SMF automatically change the sql_mode and autocommit if needed. Chris@76: if (isset($mysql_set_mode) && $mysql_set_mode === true) Chris@76: $smcFunc['db_query']('', 'SET sql_mode = \'\', AUTOCOMMIT = 1', Chris@76: array(), Chris@76: false Chris@76: ); Chris@76: Chris@76: return $connection; Chris@76: } Chris@76: Chris@76: // Extend the database functionality. Chris@76: function db_extend($type = 'extra') Chris@76: { Chris@76: global $sourcedir, $db_type; Chris@76: Chris@76: require_once($sourcedir . '/Db' . strtoupper($type[0]) . substr($type, 1) . '-' . $db_type . '.php'); Chris@76: $initFunc = 'db_' . $type . '_init'; Chris@76: $initFunc(); Chris@76: } Chris@76: Chris@76: // Fix up the prefix so it doesn't require the database to be selected. Chris@76: function db_fix_prefix(&$db_prefix, $db_name) Chris@76: { Chris@76: $db_prefix = is_numeric(substr($db_prefix, 0, 1)) ? $db_name . '.' . $db_prefix : '`' . $db_name . '`.' . $db_prefix; Chris@76: } Chris@76: Chris@76: function smf_db_replacement__callback($matches) Chris@76: { Chris@76: global $db_callback, $user_info, $db_prefix; Chris@76: Chris@76: list ($values, $connection) = $db_callback; Chris@76: Chris@76: if (!is_resource($connection)) Chris@76: db_fatal_error(); Chris@76: Chris@76: if ($matches[1] === 'db_prefix') Chris@76: return $db_prefix; Chris@76: Chris@76: if ($matches[1] === 'query_see_board') Chris@76: return $user_info['query_see_board']; Chris@76: Chris@76: if ($matches[1] === 'query_wanna_see_board') Chris@76: return $user_info['query_wanna_see_board']; Chris@76: Chris@76: if (!isset($matches[2])) Chris@76: smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); Chris@76: Chris@76: if (!isset($values[$matches[2]])) Chris@76: smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . htmlspecialchars($matches[2]), '', E_USER_ERROR, __FILE__, __LINE__); Chris@76: Chris@76: $replacement = $values[$matches[2]]; Chris@76: Chris@76: switch ($matches[1]) Chris@76: { Chris@76: case 'int': Chris@76: if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) Chris@76: smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); Chris@76: return (string) (int) $replacement; Chris@76: break; Chris@76: Chris@76: case 'string': Chris@76: case 'text': Chris@76: return sprintf('\'%1$s\'', mysql_real_escape_string($replacement, $connection)); Chris@76: break; Chris@76: Chris@76: case 'array_int': Chris@76: if (is_array($replacement)) Chris@76: { Chris@76: if (empty($replacement)) Chris@76: smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); Chris@76: Chris@76: foreach ($replacement as $key => $value) Chris@76: { Chris@76: if (!is_numeric($value) || (string) $value !== (string) (int) $value) Chris@76: smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); Chris@76: Chris@76: $replacement[$key] = (string) (int) $value; Chris@76: } Chris@76: Chris@76: return implode(', ', $replacement); Chris@76: } Chris@76: else Chris@76: smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); Chris@76: Chris@76: break; Chris@76: Chris@76: case 'array_string': Chris@76: if (is_array($replacement)) Chris@76: { Chris@76: if (empty($replacement)) Chris@76: smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); Chris@76: Chris@76: foreach ($replacement as $key => $value) Chris@76: $replacement[$key] = sprintf('\'%1$s\'', mysql_real_escape_string($value, $connection)); Chris@76: Chris@76: return implode(', ', $replacement); Chris@76: } Chris@76: else Chris@76: smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); Chris@76: break; Chris@76: Chris@76: case 'date': Chris@76: if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) Chris@76: return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]); Chris@76: else Chris@76: smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); Chris@76: break; Chris@76: Chris@76: case 'float': Chris@76: if (!is_numeric($replacement)) Chris@76: smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); Chris@76: return (string) (float) $replacement; Chris@76: break; Chris@76: Chris@76: case 'identifier': Chris@76: // Backticks inside identifiers are supported as of MySQL 4.1. We don't need them for SMF. Chris@76: return '`' . strtr($replacement, array('`' => '', '.' => '')) . '`'; Chris@76: break; Chris@76: Chris@76: case 'raw': Chris@76: return $replacement; Chris@76: break; Chris@76: Chris@76: default: Chris@76: smf_db_error_backtrace('Undefined type used in the database query. (' . $matches[1] . ':' . $matches[2] . ')', '', false, __FILE__, __LINE__); Chris@76: break; Chris@76: } Chris@76: } Chris@76: Chris@76: // Just like the db_query, escape and quote a string, but not executing the query. Chris@76: function smf_db_quote($db_string, $db_values, $connection = null) Chris@76: { Chris@76: global $db_callback, $db_connection; Chris@76: Chris@76: // Only bother if there's something to replace. Chris@76: if (strpos($db_string, '{') !== false) Chris@76: { Chris@76: // This is needed by the callback function. Chris@76: $db_callback = array($db_values, $connection == null ? $db_connection : $connection); Chris@76: Chris@76: // Do the quoting and escaping Chris@76: $db_string = preg_replace_callback('~{([a-z_]+)(?::([a-zA-Z0-9_-]+))?}~', 'smf_db_replacement__callback', $db_string); Chris@76: Chris@76: // Clear this global variable. Chris@76: $db_callback = array(); Chris@76: } Chris@76: Chris@76: return $db_string; Chris@76: } Chris@76: Chris@76: // Do a query. Takes care of errors too. Chris@76: function smf_db_query($identifier, $db_string, $db_values = array(), $connection = null) Chris@76: { Chris@76: global $db_cache, $db_count, $db_connection, $db_show_debug, $time_start; Chris@76: global $db_unbuffered, $db_callback, $modSettings; Chris@76: Chris@76: // Comments that are allowed in a query are preg_removed. Chris@76: static $allowed_comments_from = array( Chris@76: '~\s+~s', Chris@76: '~/\*!40001 SQL_NO_CACHE \*/~', Chris@76: '~/\*!40000 USE INDEX \([A-Za-z\_]+?\) \*/~', Chris@76: '~/\*!40100 ON DUPLICATE KEY UPDATE id_msg = \d+ \*/~', Chris@76: ); Chris@76: static $allowed_comments_to = array( Chris@76: ' ', Chris@76: '', Chris@76: '', Chris@76: '', Chris@76: ); Chris@76: Chris@76: // Decide which connection to use. Chris@76: $connection = $connection == null ? $db_connection : $connection; Chris@76: Chris@76: // One more query.... Chris@76: $db_count = !isset($db_count) ? 1 : $db_count + 1; Chris@76: Chris@76: if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) Chris@76: smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); Chris@76: Chris@76: // Use "ORDER BY null" to prevent Mysql doing filesorts for Group By clauses without an Order By Chris@76: if (strpos($db_string, 'GROUP BY') !== false && strpos($db_string, 'ORDER BY') === false && strpos($db_string, 'INSERT INTO') === false) Chris@76: { Chris@76: // Add before LIMIT Chris@76: if ($pos = strpos($db_string, 'LIMIT ')) Chris@76: $db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string)); Chris@76: else Chris@76: // Append it. Chris@76: $db_string .= "\n\t\t\tORDER BY null"; Chris@76: } Chris@76: Chris@76: if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false)) Chris@76: { Chris@76: // Pass some values to the global space for use in the callback function. Chris@76: $db_callback = array($db_values, $connection); Chris@76: Chris@76: // Inject the values passed to this function. Chris@76: $db_string = preg_replace_callback('~{([a-z_]+)(?::([a-zA-Z0-9_-]+))?}~', 'smf_db_replacement__callback', $db_string); Chris@76: Chris@76: // This shouldn't be residing in global space any longer. Chris@76: $db_callback = array(); Chris@76: } Chris@76: Chris@76: // Debugging. Chris@76: if (isset($db_show_debug) && $db_show_debug === true) Chris@76: { Chris@76: // Get the file and line number this function was called. Chris@76: list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__); Chris@76: Chris@76: // Initialize $db_cache if not already initialized. Chris@76: if (!isset($db_cache)) Chris@76: $db_cache = array(); Chris@76: Chris@76: if (!empty($_SESSION['debug_redirect'])) Chris@76: { Chris@76: $db_cache = array_merge($_SESSION['debug_redirect'], $db_cache); Chris@76: $db_count = count($db_cache) + 1; Chris@76: $_SESSION['debug_redirect'] = array(); Chris@76: } Chris@76: Chris@76: $st = microtime(); Chris@76: // Don't overload it. Chris@76: $db_cache[$db_count]['q'] = $db_count < 50 ? $db_string : '...'; Chris@76: $db_cache[$db_count]['f'] = $file; Chris@76: $db_cache[$db_count]['l'] = $line; Chris@76: $db_cache[$db_count]['s'] = array_sum(explode(' ', $st)) - array_sum(explode(' ', $time_start)); Chris@76: } Chris@76: Chris@76: // First, we clean strings out of the query, reduce whitespace, lowercase, and trim - so we can check it over. Chris@76: if (empty($modSettings['disableQueryCheck'])) Chris@76: { Chris@76: $clean = ''; Chris@76: $old_pos = 0; Chris@76: $pos = -1; Chris@76: while (true) Chris@76: { Chris@76: $pos = strpos($db_string, '\'', $pos + 1); Chris@76: if ($pos === false) Chris@76: break; Chris@76: $clean .= substr($db_string, $old_pos, $pos - $old_pos); Chris@76: Chris@76: while (true) Chris@76: { Chris@76: $pos1 = strpos($db_string, '\'', $pos + 1); Chris@76: $pos2 = strpos($db_string, '\\', $pos + 1); Chris@76: if ($pos1 === false) Chris@76: break; Chris@76: elseif ($pos2 == false || $pos2 > $pos1) Chris@76: { Chris@76: $pos = $pos1; Chris@76: break; Chris@76: } Chris@76: Chris@76: $pos = $pos2 + 1; Chris@76: } Chris@76: $clean .= ' %s '; Chris@76: Chris@76: $old_pos = $pos + 1; Chris@76: } Chris@76: $clean .= substr($db_string, $old_pos); Chris@76: $clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean))); Chris@76: Chris@76: // We don't use UNION in SMF, at least so far. But it's useful for injections. Chris@76: if (strpos($clean, 'union') !== false && preg_match('~(^|[^a-z])union($|[^[a-z])~s', $clean) != 0) Chris@76: $fail = true; Chris@76: // Comments? We don't use comments in our queries, we leave 'em outside! Chris@76: elseif (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) Chris@76: $fail = true; Chris@76: // Trying to change passwords, slow us down, or something? Chris@76: elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) Chris@76: $fail = true; Chris@76: elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) Chris@76: $fail = true; Chris@76: // Sub selects? We don't use those either. Chris@76: elseif (preg_match('~\([^)]*?select~s', $clean) != 0) Chris@76: $fail = true; Chris@76: Chris@76: if (!empty($fail) && function_exists('log_error')) Chris@76: smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); Chris@76: } Chris@76: Chris@76: if (empty($db_unbuffered)) Chris@76: $ret = @mysql_query($db_string, $connection); Chris@76: else Chris@76: $ret = @mysql_unbuffered_query($db_string, $connection); Chris@76: if ($ret === false && empty($db_values['db_error_skip'])) Chris@76: $ret = smf_db_error($db_string, $connection); Chris@76: Chris@76: // Debugging. Chris@76: if (isset($db_show_debug) && $db_show_debug === true) Chris@76: $db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); Chris@76: Chris@76: return $ret; Chris@76: } Chris@76: Chris@76: function smf_db_affected_rows($connection = null) Chris@76: { Chris@76: global $db_connection; Chris@76: Chris@76: return mysql_affected_rows($connection == null ? $db_connection : $connection); Chris@76: } Chris@76: Chris@76: function smf_db_insert_id($table, $field = null, $connection = null) Chris@76: { Chris@76: global $db_connection, $db_prefix; Chris@76: Chris@76: $table = str_replace('{db_prefix}', $db_prefix, $table); Chris@76: Chris@76: // MySQL doesn't need the table or field information. Chris@76: return mysql_insert_id($connection == null ? $db_connection : $connection); Chris@76: } Chris@76: Chris@76: // Do a transaction. Chris@76: function smf_db_transaction($type = 'commit', $connection = null) Chris@76: { Chris@76: global $db_connection; Chris@76: Chris@76: // Decide which connection to use Chris@76: $connection = $connection == null ? $db_connection : $connection; Chris@76: Chris@76: if ($type == 'begin') Chris@76: return @mysql_query('BEGIN', $connection); Chris@76: elseif ($type == 'rollback') Chris@76: return @mysql_query('ROLLBACK', $connection); Chris@76: elseif ($type == 'commit') Chris@76: return @mysql_query('COMMIT', $connection); Chris@76: Chris@76: return false; Chris@76: } Chris@76: Chris@76: // Database error! Chris@76: function smf_db_error($db_string, $connection = null) Chris@76: { Chris@76: global $txt, $context, $sourcedir, $webmaster_email, $modSettings; Chris@76: global $forum_version, $db_connection, $db_last_error, $db_persist; Chris@76: global $db_server, $db_user, $db_passwd, $db_name, $db_show_debug, $ssi_db_user, $ssi_db_passwd; Chris@76: global $smcFunc; Chris@76: Chris@76: // Get the file and line numbers. Chris@76: list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__); Chris@76: Chris@76: // Decide which connection to use Chris@76: $connection = $connection == null ? $db_connection : $connection; Chris@76: Chris@76: // This is the error message... Chris@76: $query_error = mysql_error($connection); Chris@76: $query_errno = mysql_errno($connection); Chris@76: Chris@76: // Error numbers: Chris@76: // 1016: Can't open file '....MYI' Chris@76: // 1030: Got error ??? from table handler. Chris@76: // 1034: Incorrect key file for table. Chris@76: // 1035: Old key file for table. Chris@76: // 1205: Lock wait timeout exceeded. Chris@76: // 1213: Deadlock found. Chris@76: // 2006: Server has gone away. Chris@76: // 2013: Lost connection to server during query. Chris@76: Chris@76: // Log the error. Chris@76: if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error')) Chris@76: log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line); Chris@76: Chris@76: // Database error auto fixing ;). Chris@76: if (function_exists('cache_get_data') && (!isset($modSettings['autoFixDatabase']) || $modSettings['autoFixDatabase'] == '1')) Chris@76: { Chris@76: // Force caching on, just for the error checking. Chris@76: $old_cache = @$modSettings['cache_enable']; Chris@76: $modSettings['cache_enable'] = '1'; Chris@76: Chris@76: if (($temp = cache_get_data('db_last_error', 600)) !== null) Chris@76: $db_last_error = max(@$db_last_error, $temp); Chris@76: Chris@76: if (@$db_last_error < time() - 3600 * 24 * 3) Chris@76: { Chris@76: // We know there's a problem... but what? Try to auto detect. Chris@76: if ($query_errno == 1030 && strpos($query_error, ' 127 ') !== false) Chris@76: { Chris@76: preg_match_all('~(?:[\n\r]|^)[^\']+?(?:FROM|JOIN|UPDATE|TABLE) ((?:[^\n\r(]+?(?:, )?)*)~s', $db_string, $matches); Chris@76: Chris@76: $fix_tables = array(); Chris@76: foreach ($matches[1] as $tables) Chris@76: { Chris@76: $tables = array_unique(explode(',', $tables)); Chris@76: foreach ($tables as $table) Chris@76: { Chris@76: // Now, it's still theoretically possible this could be an injection. So backtick it! Chris@76: if (trim($table) != '') Chris@76: $fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`'; Chris@76: } Chris@76: } Chris@76: Chris@76: $fix_tables = array_unique($fix_tables); Chris@76: } Chris@76: // Table crashed. Let's try to fix it. Chris@76: elseif ($query_errno == 1016) Chris@76: { Chris@76: if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0) Chris@76: $fix_tables = array('`' . $match[1] . '`'); Chris@76: } Chris@76: // Indexes crashed. Should be easy to fix! Chris@76: elseif ($query_errno == 1034 || $query_errno == 1035) Chris@76: { Chris@76: preg_match('~\'([^\']+?)\'~', $query_error, $match); Chris@76: $fix_tables = array('`' . $match[1] . '`'); Chris@76: } Chris@76: } Chris@76: Chris@76: // Check for errors like 145... only fix it once every three days, and send an email. (can't use empty because it might not be set yet...) Chris@76: if (!empty($fix_tables)) Chris@76: { Chris@76: // Subs-Admin.php for updateSettingsFile(), Subs-Post.php for sendmail(). Chris@76: require_once($sourcedir . '/Subs-Admin.php'); Chris@76: require_once($sourcedir . '/Subs-Post.php'); Chris@76: Chris@76: // Make a note of the REPAIR... Chris@76: cache_put_data('db_last_error', time(), 600); Chris@76: if (($temp = cache_get_data('db_last_error', 600)) === null) Chris@76: updateSettingsFile(array('db_last_error' => time())); Chris@76: Chris@76: // Attempt to find and repair the broken table. Chris@76: foreach ($fix_tables as $table) Chris@76: $smcFunc['db_query']('', " Chris@76: REPAIR TABLE $table", false, false); Chris@76: Chris@76: // And send off an email! Chris@76: sendmail($webmaster_email, $txt['database_error'], $txt['tried_to_repair']); Chris@76: Chris@76: $modSettings['cache_enable'] = $old_cache; Chris@76: Chris@76: // Try the query again...? Chris@76: $ret = $smcFunc['db_query']('', $db_string, false, false); Chris@76: if ($ret !== false) Chris@76: return $ret; Chris@76: } Chris@76: else Chris@76: $modSettings['cache_enable'] = $old_cache; Chris@76: Chris@76: // Check for the "lost connection" or "deadlock found" errors - and try it just one more time. Chris@76: if (in_array($query_errno, array(1205, 1213, 2006, 2013))) Chris@76: { Chris@76: if (in_array($query_errno, array(2006, 2013)) && $db_connection == $connection) Chris@76: { Chris@76: // Are we in SSI mode? If so try that username and password first Chris@76: if (SMF == 'SSI' && !empty($ssi_db_user) && !empty($ssi_db_passwd)) Chris@76: { Chris@76: if (empty($db_persist)) Chris@76: $db_connection = @mysql_connect($db_server, $ssi_db_user, $ssi_db_passwd); Chris@76: else Chris@76: $db_connection = @mysql_pconnect($db_server, $ssi_db_user, $ssi_db_passwd); Chris@76: } Chris@76: // Fall back to the regular username and password if need be Chris@76: if (!$db_connection) Chris@76: { Chris@76: if (empty($db_persist)) Chris@76: $db_connection = @mysql_connect($db_server, $db_user, $db_passwd); Chris@76: else Chris@76: $db_connection = @mysql_pconnect($db_server, $db_user, $db_passwd); Chris@76: } Chris@76: Chris@76: if (!$db_connection || !@mysql_select_db($db_name, $db_connection)) Chris@76: $db_connection = false; Chris@76: } Chris@76: Chris@76: if ($db_connection) Chris@76: { Chris@76: // Try a deadlock more than once more. Chris@76: for ($n = 0; $n < 4; $n++) Chris@76: { Chris@76: $ret = $smcFunc['db_query']('', $db_string, false, false); Chris@76: Chris@76: $new_errno = mysql_errno($db_connection); Chris@76: if ($ret !== false || in_array($new_errno, array(1205, 1213))) Chris@76: break; Chris@76: } Chris@76: Chris@76: // If it failed again, shucks to be you... we're not trying it over and over. Chris@76: if ($ret !== false) Chris@76: return $ret; Chris@76: } Chris@76: } Chris@76: // Are they out of space, perhaps? Chris@76: elseif ($query_errno == 1030 && (strpos($query_error, ' -1 ') !== false || strpos($query_error, ' 28 ') !== false || strpos($query_error, ' 12 ') !== false)) Chris@76: { Chris@76: if (!isset($txt)) Chris@76: $query_error .= ' - check database storage space.'; Chris@76: else Chris@76: { Chris@76: if (!isset($txt['mysql_error_space'])) Chris@76: loadLanguage('Errors'); Chris@76: Chris@76: $query_error .= !isset($txt['mysql_error_space']) ? ' - check database storage space.' : $txt['mysql_error_space']; Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@76: // Nothing's defined yet... just die with it. Chris@76: if (empty($context) || empty($txt)) Chris@76: die($query_error); Chris@76: Chris@76: // Show an error message, if possible. Chris@76: $context['error_title'] = $txt['database_error']; Chris@76: if (allowedTo('admin_forum')) Chris@76: $context['error_message'] = nl2br($query_error) . '
' . $txt['file'] . ': ' . $file . '
' . $txt['line'] . ': ' . $line; Chris@76: else Chris@76: $context['error_message'] = $txt['try_again']; Chris@76: Chris@76: // A database error is often the sign of a database in need of upgrade. Check forum versions, and if not identical suggest an upgrade... (not for Demo/CVS versions!) Chris@76: if (allowedTo('admin_forum') && !empty($forum_version) && $forum_version != 'SMF ' . @$modSettings['smfVersion'] && strpos($forum_version, 'Demo') === false && strpos($forum_version, 'CVS') === false) Chris@76: $context['error_message'] .= '

' . sprintf($txt['database_error_versions'], $forum_version, $modSettings['smfVersion']); Chris@76: Chris@76: if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true) Chris@76: { Chris@76: $context['error_message'] .= '

' . nl2br($db_string); Chris@76: } Chris@76: Chris@76: // It's already been logged... don't log it again. Chris@76: fatal_error($context['error_message'], false); Chris@76: } Chris@76: Chris@76: // Insert some data... Chris@76: function smf_db_insert($method = 'replace', $table, $columns, $data, $keys, $disable_trans = false, $connection = null) Chris@76: { Chris@76: global $smcFunc, $db_connection, $db_prefix; Chris@76: Chris@76: $connection = $connection === null ? $db_connection : $connection; Chris@76: Chris@76: // With nothing to insert, simply return. Chris@76: if (empty($data)) Chris@76: return; Chris@76: Chris@76: // Replace the prefix holder with the actual prefix. Chris@76: $table = str_replace('{db_prefix}', $db_prefix, $table); Chris@76: Chris@76: // Inserting data as a single row can be done as a single array. Chris@76: if (!is_array($data[array_rand($data)])) Chris@76: $data = array($data); Chris@76: Chris@76: // Create the mold for a single row insert. Chris@76: $insertData = '('; Chris@76: foreach ($columns as $columnName => $type) Chris@76: { Chris@76: // Are we restricting the length? Chris@76: if (strpos($type, 'string-') !== false) Chris@76: $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); Chris@76: else Chris@76: $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); Chris@76: } Chris@76: $insertData = substr($insertData, 0, -2) . ')'; Chris@76: Chris@76: // Create an array consisting of only the columns. Chris@76: $indexed_columns = array_keys($columns); Chris@76: Chris@76: // Here's where the variables are injected to the query. Chris@76: $insertRows = array(); Chris@76: foreach ($data as $dataRow) Chris@76: $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); Chris@76: Chris@76: // Determine the method of insertion. Chris@76: $queryTitle = $method == 'replace' ? 'REPLACE' : ($method == 'ignore' ? 'INSERT IGNORE' : 'INSERT'); Chris@76: Chris@76: // Do the insert. Chris@76: $smcFunc['db_query']('', ' Chris@76: ' . $queryTitle . ' INTO ' . $table . '(`' . implode('`, `', $indexed_columns) . '`) Chris@76: VALUES Chris@76: ' . implode(', Chris@76: ', $insertRows), Chris@76: array( Chris@76: 'security_override' => true, Chris@76: 'db_error_skip' => $table === $db_prefix . 'log_errors', Chris@76: ), Chris@76: $connection Chris@76: ); Chris@76: } Chris@76: Chris@76: // This function tries to work out additional error information from a back trace. Chris@76: function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null) Chris@76: { Chris@76: if (empty($log_message)) Chris@76: $log_message = $error_message; Chris@76: Chris@76: if (function_exists('debug_backtrace')) Chris@76: { Chris@76: foreach (debug_backtrace() as $step) Chris@76: { Chris@76: // Found it? Chris@76: if (strpos($step['function'], 'query') === false && !in_array(substr($step['function'], 0, 7), array('smf_db_', 'preg_re', 'db_erro', 'call_us')) && substr($step['function'], 0, 2) != '__') Chris@76: { Chris@76: $log_message .= '
Function: ' . $step['function']; Chris@76: break; Chris@76: } Chris@76: Chris@76: if (isset($step['line'])) Chris@76: { Chris@76: $file = $step['file']; Chris@76: $line = $step['line']; Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@76: // A special case - we want the file and line numbers for debugging. Chris@76: if ($error_type == 'return') Chris@76: return array($file, $line); Chris@76: Chris@76: // Is always a critical error. Chris@76: if (function_exists('log_error')) Chris@76: log_error($log_message, 'critical', $file, $line); Chris@76: Chris@76: if (function_exists('fatal_error')) Chris@76: { Chris@76: fatal_error($error_message, false); Chris@76: Chris@76: // Cannot continue... Chris@76: exit; Chris@76: } Chris@76: elseif ($error_type) Chris@76: trigger_error($error_message . ($line !== null ? '(' . basename($file) . '-' . $line . ')' : ''), $error_type); Chris@76: else Chris@76: trigger_error($error_message . ($line !== null ? '(' . basename($file) . '-' . $line . ')' : '')); Chris@76: } Chris@76: Chris@76: // Escape the LIKE wildcards so that they match the character and not the wildcard. Chris@76: // The optional second parameter turns human readable wildcards into SQL wildcards. Chris@76: function smf_db_escape_wildcard_string($string, $translate_human_wildcards=false) Chris@76: { Chris@76: $replacements = array( Chris@76: '%' => '\%', Chris@76: '_' => '\_', Chris@76: '\\' => '\\\\', Chris@76: ); Chris@76: Chris@76: if ($translate_human_wildcards) Chris@76: $replacements += array( Chris@76: '*' => '%', Chris@76: ); Chris@76: Chris@76: return strtr($string, $replacements); Chris@76: } Chris@76: ?>