function loadDatabase() { global $db_persist, $db_connection, $db_server, $db_user, $db_passwd; global $db_type, $db_name, $ssi_db_user, $ssi_db_passwd, $sourcedir, $db_prefix; // Figure out what type of database we are using. $db_type = 'mysql'; // Load the file for the database. require_once $sourcedir . '/lib/Subs-Db-' . $db_type . '.php'; // If we are in SSI try them first, but don't worry if it doesn't work, we have the normal username and password we can use. if (SMF == 'SSI' && !empty($ssi_db_user) && !empty($ssi_db_passwd)) { $db_connection = smf_db_initiate($db_server, $db_name, $ssi_db_user, $ssi_db_passwd, array('persist' => $db_persist, 'non_fatal' => true, 'dont_select_db' => true)); } // Either we aren't in SSI mode, or it failed. if (empty($db_connection)) { $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, array('persist' => $db_persist, 'dont_select_db' => SMF == 'SSI')); } // Safe guard here, if there isn't a valid connection lets put a stop to it. if (!$db_connection) { db_fatal_error(); } // If in SSI mode fix up the prefix. if (SMF == 'SSI') { db_fix_prefix($db_prefix, $db_name); } }
function DatabaseSettings() { global $txt, $databases, $incontext; $incontext['sub_template'] = 'database_settings'; $incontext['page_title'] = $txt['db_settings']; $incontext['continue'] = 1; // Set up the defaults. $incontext['db']['server'] = 'localhost'; $incontext['db']['user'] = ''; $incontext['db']['name'] = ''; $incontext['db']['pass'] = ''; $incontext['db']['type'] = ''; $incontext['supported_databases'] = array(); $foundOne = false; foreach ($databases as $key => $db) { // Override with the defaults for this DB if appropriate. if ($db['supported']) { $incontext['supported_databases'][$key] = $db; if (!$foundOne) { if (isset($db['default_host'])) { $incontext['db']['server'] = @ini_get($db['default_host']) or $incontext['db']['server'] = 'localhost'; } if (isset($db['default_user'])) { $incontext['db']['user'] = @ini_get($db['default_user']); $incontext['db']['name'] = @ini_get($db['default_user']); } if (isset($db['default_password'])) { $incontext['db']['pass'] = @ini_get($db['default_password']); } if (isset($db['default_port'])) { $db_port = @ini_get($db['default_port']); } $incontext['db']['type'] = $key; $foundOne = true; } } } // Override for repost. if (isset($_POST['db_user'])) { $incontext['db']['user'] = $_POST['db_user']; $incontext['db']['name'] = $_POST['db_type'] == 'sqlite' && isset($_POST['db_filename']) ? $_POST['db_filename'] : $_POST['db_name']; $incontext['db']['server'] = $_POST['db_server']; $incontext['db']['prefix'] = $_POST['db_prefix']; } else { $incontext['db']['prefix'] = 'smf_'; } // Should we use a non standard port? if (!empty($db_port)) { $incontext['db']['server'] .= ':' . $db_port; } // Are we submitting? if (isset($_POST['db_type'])) { if (isset($_POST['db_filename'])) { // You better enter enter a database name for SQLite. if (trim($_POST['db_filename']) == '') { $incontext['error'] = $txt['error_db_filename']; return false; } // Duplicate name in the same dir? Can't do that with SQLite. Weird things happen. if (file_exists($_POST['db_filename'] . (substr($_POST['db_filename'], -3) != '.db' ? '.db' : ''))) { $incontext['error'] = $txt['error_db_filename_exists']; return false; } } // What type are they trying? $db_type = preg_replace('~[^A-Za-z0-9]~', '', $_POST['db_type']); $db_prefix = $_POST['db_prefix']; // Validate the prefix. $valid_prefix = $databases[$db_type]['validate_prefix']($db_prefix); if ($valid_prefix !== true) { $incontext['error'] = $valid_prefix; return false; } // Take care of these variables... $vars = array('db_type' => $db_type, 'db_name' => $_POST['db_type'] == 'sqlite' && isset($_POST['db_filename']) ? $_POST['db_filename'] : $_POST['db_name'], 'db_user' => $_POST['db_user'], 'db_passwd' => isset($_POST['db_passwd']) ? $_POST['db_passwd'] : '', 'db_server' => $_POST['db_server'], 'db_prefix' => $db_prefix, 'cookiename' => 'SMFCookie' . abs(crc32($_POST['db_name'] . preg_replace('~[^A-Za-z0-9_$]~', '', $_POST['db_prefix'])) % 1000)); // God I hope it saved! if (!updateSettingsFile($vars) && substr(__FILE__, 1, 2) == ':\\') { $incontext['error'] = $txt['error_windows_chmod']; return false; } // Make sure it works. require dirname(__FILE__) . '/Settings.php'; if (empty($sourcedir)) { $sourcedir = dirname(__FILE__) . '/Sources'; } // Better find the database file! if (!file_exists($sourcedir . '/lib/Subs-Db-' . $db_type . '.php')) { $incontext['error'] = sprintf($txt['error_db_file'], 'lib/Subs-Db-' . $db_type . '.php'); return false; } // Now include it for database functions! define('SMF', 1); $modSettings['disableQueryCheck'] = true; require_once $sourcedir . '/lib/Subs-Db-' . $db_type . '.php'; // Attempt a connection. $needsDB = !empty($databases[$db_type]['always_has_db']); $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, array('non_fatal' => true, 'dont_select_db' => !$needsDB)); // No dice? Let's try adding the prefix they specified, just in case they misread the instructions ;) if ($db_connection == null) { $db_error = @mysql_error($db_connection); $db_connection = smf_db_initiate($db_server, $db_name, $_POST['db_prefix'] . $db_user, $db_passwd, array('non_fatal' => true, 'dont_select_db' => !$needsDB)); if ($db_connection != null) { $db_user = $_POST['db_prefix'] . $db_user; updateSettingsFile(array('db_user' => $db_user)); } } // Still no connection? Big fat error message :P. if (!$db_connection) { $incontext['error'] = $txt['error_db_connect'] . '<div style="margin: 2.5ex; font-family: monospace;"><strong>' . $db_error . '</strong></div>'; return false; } // Do they meet the install requirements? // !!! Old client, new server? if (version_compare($databases[$db_type]['version'], preg_replace('~^\\D*|\\-.+?$~', '', eval($databases[$db_type]['version_check']))) > 0) { $incontext['error'] = $txt['error_db_too_low']; return false; } // Let's try that database on for size... assuming we haven't already lost the opportunity. if ($db_name != '' && !$needsDB) { smf_db_query("\n\t\t\t\tCREATE DATABASE IF NOT EXISTS `{$db_name}`", array('security_override' => true, 'db_error_skip' => true), $db_connection); // Okay, let's try the prefix if it didn't work... if (!mysql_select_db($db_name, $db_connection) && $db_name != '') { smf_db_query("\n\t\t\t\t\tCREATE DATABASE IF NOT EXISTS `{$_POST['db_prefix']}{$db_name}`", array('security_override' => true, 'db_error_skip' => true), $db_connection); if (mysql_select_db($_POST['db_prefix'] . $db_name, $db_connection)) { $db_name = $_POST['db_prefix'] . $db_name; updateSettingsFile(array('db_name' => $db_name)); } } // Okay, now let's try to connect... if (!mysql_select_db($db_name, $db_connection)) { $incontext['error'] = sprintf($txt['error_db_database'], $db_name); return false; } } return true; } return false; }
/** * Load the db connection * * Will add the db functions to $smcFunc array and set up and test the db connection * * @return bool if the db connection exists or not * @since 0.1.0 */ function smfapi_loadDatabase() { global $db_persist, $db_connection, $db_server, $db_user, $db_passwd; global $db_type, $db_name, $sourcedir, $db_prefix; // figure out what type of database we are using. if (empty($db_type) || !file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php')) { $db_type = 'mysql'; } // load the file for the database (safe to load) require_once $sourcedir . '/Subs-Db-' . $db_type . '.php'; // make connection if (empty($db_connection)) { $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('persist' => $db_persist, 'dont_select_db' => SMF == 'API')); } // safe guard here, if there isn't a valid connection lets put a stop to it. if (!$db_connection) { return false; } // defined in Subs-Db-*.php db_fix_prefix($db_prefix, $db_name); return true; }
function loadEssentialData() { global $db_server, $db_user, $db_passwd, $db_name, $db_connection, $db_prefix, $db_character_set, $db_type; global $modSettings, $sourcedir, $smcFunc, $upcontext; // Do the non-SSI stuff... @set_magic_quotes_runtime(0); error_reporting(E_ALL); define('SMF', 1); // Start the session. if (@ini_get('session.save_handler') == 'user') { @ini_set('session.save_handler', 'files'); } @session_start(); if (empty($smcFunc)) { $smcFunc = array(); } // Initialize everything... initialize_inputs(); // Get the database going! if (empty($db_type)) { $db_type = 'mysql'; } if (file_exists($sourcedir . '/lib/Subs-Db-' . $db_type . '.php')) { require_once $sourcedir . '/lib/Subs-Db-' . $db_type . '.php'; // Make the connection... $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, array('non_fatal' => true)); // Oh dear god!! if ($db_connection === null) { die('Unable to connect to database - please check username and password are correct in Settings.php'); } if ($db_type == 'mysql' && isset($db_character_set) && preg_match('~^\\w+$~', $db_character_set) === 1) { $smcFunc['db_query']('', ' SET NAMES ' . $db_character_set, array('db_error_skip' => true)); } // Load the modSettings data... $request = $smcFunc['db_query']('', ' SELECT variable, value FROM {db_prefix}settings', array('db_error_skip' => true)); $modSettings = array(); while ($row = $smcFunc['db_fetch_assoc']($request)) { $modSettings[$row['variable']] = $row['value']; } $smcFunc['db_free_result']($request); } else { return throw_error('Cannot find ' . $sourcedir . '/lib/Subs-Db-' . $db_type . '.php' . '. Please check you have uploaded all source files and have the correct paths set.'); } // If they don't have the file, they're going to get a warning anyway so we won't need to clean request vars. if (file_exists($sourcedir . '/QueryString.php')) { require_once $sourcedir . '/QueryString.php'; cleanRequest(); } if (!isset($_GET['substep'])) { $_GET['substep'] = 0; } }
function initialize_inputs() { global $smcFunc, $db_connection, $sourcedir, $db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_type, $context, $sources_exist, $sources_found_path; // Turn off magic quotes runtime and enable error reporting. @set_magic_quotes_runtime(0); error_reporting(E_ALL); if (ini_get('session.save_handler') == 'user') { ini_set('session.save_handler', 'files'); } @session_start(); if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0) { foreach ($_POST as $k => $v) { if (is_array($v)) { foreach ($v as $k2 => $v2) { $_POST[$k][$k2] = stripslashes($v2); } } else { $_POST[$k] = stripslashes($v); } } } foreach ($_POST as $k => $v) { if (is_array($v)) { foreach ($v as $k2 => $v2) { $_POST[$k][$k2] = addcslashes($v2, '\'\\'); } } else { $_POST[$k] = addcslashes($v, '\'\\'); } } // This is really quite simple; if ?delete is on the URL, delete the installer... if (isset($_GET['delete'])) { @unlink(__FILE__); // Now just redirect to a blank.gif... header('Location: http://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT']) . dirname($_SERVER['PHP_SELF']) . '/Themes/default/images/blank.gif'); exit; } $db_connection = false; $sources_exist = false; $sources_found_path = ''; if (isset($sourcedir) && file_exists(dirname(__FILE__) . '/Sources/Load.php')) { $sources_exist = true; } else { //Find Sources folder! $sourcedir = findSources(); $sources_found_path = $sourcedir; $sources_exist = !empty($sourcedir); } if ($sources_exist) { if (!defined('SMF')) { define('SMF', 1); } if (empty($smcFunc)) { $smcFunc = array(); } // Default the database type to MySQL. if (empty($db_type) || !file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php') || $db_type == 'mysqli') { $db_type = 'mysql'; } //require_once($sourcedir . '/Load.php'); //require_once($sourcedir . '/Subs-Auth.php'); // compat mode. Active! $context['is_legacy'] = true; if (!file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php') && $db_type == 'mysql') { $db_connection = smc_compat_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('non_fatal' => true)); } else { // Far as I know, this is 2.0. $context['is_legacy'] = false; require_once $sourcedir . '/Subs-Db-' . $db_type . '.php'; require_once $sourcedir . '/DbExtra-' . $db_type . '.php'; $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('non_fatal' => true)); db_extra_init(); //Fixes error with $db_connection not false if database name is incorrect. This is because the connection is established //without this parameter, thus performing incorrectly. if ($db_connection == true) { if (!$smcFunc['db_select_db']($db_name, $db_connection)) { $db_connection = null; } else { $tables = $smcFunc['db_list_tables']($db_name); if (!(is_array($tables) && in_array($db_prefix . 'settings', $tables))) { $db_connection = null; } } } else { $db_connection = null; } } } }
function initialize_inputs() { global $smcFunc, $db_connection, $sourcedir, $db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_type, $context; // Turn off magic quotes runtime and enable error reporting. @set_magic_quotes_runtime(0); error_reporting(E_ALL); if (ini_get('session.save_handler') == 'user') { ini_set('session.save_handler', 'files'); } @session_start(); // Slashes as soo old-fashion... if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0) { foreach ($_POST as $k => $v) { if (is_array($v)) { foreach ($v as $k2 => $v2) { $_POST[$k][$k2] = stripslashes($v2); } } else { $_POST[$k] = stripslashes($v); } } } foreach ($_POST as $k => $v) { if (is_array($v)) { foreach ($v as $k2 => $v2) { $_POST[$k][$k2] = addcslashes($v2, '\''); } } else { $_POST[$k] = addcslashes($v, '\''); } } // This is really quite simple; if ?delete is on the URL, delete the installer... if (isset($_GET['delete'])) { @unlink(__FILE__); // Now just redirect to a blank.gif... header('Location: http://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT']) . dirname($_SERVER['PHP_SELF']) . '/Themes/default/images/blank.gif'); exit; } $db_connection = false; if (isset($sourcedir)) { define('SMF', 1); if (empty($smcFunc)) { $smcFunc = array(); } // Default the database type to MySQL. if (empty($db_type) || !file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php')) { $db_type = 'mysql'; } require_once $sourcedir . '/Load.php'; require_once $sourcedir . '/Subs-Auth.php'; // compat mode. Active! $context['is_legacy'] = true; if (!file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php') && $db_type == 'mysql') { $db_connection = smc_compat_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('non_fatal' => true)); } else { // Far as I know, this is 2.0. $context['is_legacy'] = false; require_once $sourcedir . '/Subs-Db-' . $db_type . '.php'; require_once $sourcedir . '/DbExtra-' . $db_type . '.php'; $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('non_fatal' => true)); db_extra_init(); } } }