示例#1
0
/**
 * Start things up
 *
 * - It sets up variables for other steps
 * - It makes the initial connection to the db
 */
function initialize_inputs()
{
    global $db_connection, $sourcedir, $boarddir, $languagedir, $extdir, $cachedir;
    global $db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_type;
    // Turn off magic quotes runtime and enable error reporting.
    if (function_exists('set_magic_quotes_runtime')) {
        @set_magic_quotes_runtime(0);
    }
    error_reporting(E_ALL);
    ob_start();
    if (ini_get('session.save_handler') == 'user') {
        @ini_set('session.save_handler', 'files');
    }
    if (function_exists('session_start')) {
        @session_start();
    }
    // Reject magic_quotes_sybase='on'.
    if (ini_get('magic_quotes_sybase') || strtolower(ini_get('magic_quotes_sybase')) == 'on') {
        die('magic_quotes_sybase=on was detected: your host is using an unsecure PHP configuration, deprecated and removed in current versions. Please upgrade PHP.');
    }
    if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc() != 0) {
        die('magic_quotes_gpc=on was detected: your host is using an unsecure PHP configuration, deprecated and removed in current versions. Please upgrade PHP.');
    }
    // Add slashes, as long as they aren't already being added.
    foreach ($_POST as $k => $v) {
        if (is_array($v)) {
            foreach ($v as $k2 => $v2) {
                $_POST[$k][$k2] = addcslashes($v2, '\\\'');
            }
        } else {
            $_POST[$k] = addcslashes($v, '\\\'');
        }
    }
    // PHP 5 might complain if we don't do this now.
    $server_offset = @mktime(0, 0, 0, 1, 1, 1970);
    date_default_timezone_set('Etc/GMT' . ($server_offset > 0 ? '+' : '') . $server_offset / 3600);
    $db_connection = false;
    if (isset($sourcedir) && file_exists($sourcedir)) {
        define('ELK', 1);
        // Time to set some constants
        DEFINE('BOARDDIR', $boarddir);
        DEFINE('CACHEDIR', $cachedir);
        DEFINE('EXTDIR', $extdir);
        DEFINE('LANGUAGEDIR', $languagedir);
        DEFINE('SOURCEDIR', $sourcedir);
        DEFINE('ADMINDIR', $sourcedir . '/admin');
        DEFINE('CONTROLLERDIR', $sourcedir . '/controllers');
        DEFINE('SUBSDIR', $sourcedir . '/subs');
        unset($boarddir, $cachedir, $sourcedir, $languagedir, $extdir);
        // Default the database type to MySQL if its not set in settings
        if (empty($db_type) || !file_exists(SOURCEDIR . '/database/Db-' . $db_type . '.subs.php')) {
            $db_type = 'mysql';
        }
        // Lets make a connection to the db
        require_once SOURCEDIR . '/Load.php';
        require_once SOURCEDIR . '/database/Database.subs.php';
        $db_connection = elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('non_fatal' => true));
    }
}
示例#2
0
/**
 * Ask for database settings, verify and save them.
 */
function action_databaseSettings()
{
    global $txt, $databases, $incontext, $db_type, $db_connection, $modSettings;
    $incontext['sub_template'] = 'database_settings';
    $incontext['page_title'] = $txt['db_settings'];
    $incontext['continue'] = 1;
    // Set up the defaults.
    $incontext['db']['server'] = 'localhost';
    $incontext['db']['port'] = '';
    $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_name'];
        $incontext['db']['server'] = $_POST['db_server'];
        $incontext['db']['port'] = !empty($_POST['db_port']) ? $_POST['db_port'] : '';
        $incontext['db']['prefix'] = $_POST['db_prefix'];
    } else {
        $incontext['db']['prefix'] = 'elkarte_';
    }
    // 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_name'], 'db_user' => $_POST['db_user'], 'db_passwd' => isset($_POST['db_passwd']) ? $_POST['db_passwd'] : '', 'db_server' => $_POST['db_server'], 'db_port' => !empty($_POST['db_port']) ? $_POST['db_port'] : '', 'db_prefix' => $db_prefix, 'cookiename' => 'ElkArteCookie' . 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 (!defined('SOURCEDIR')) {
            define('SOURCEDIR', dirname(__FILE__) . '/sources');
        }
        // Better find the database file!
        if (!file_exists(SOURCEDIR . '/database/Db-' . $db_type . '.class.php')) {
            $incontext['error'] = sprintf($txt['error_db_file'], 'Db-' . $db_type . '.class.php');
            return false;
        }
        // Now include it for database functions!
        define('ELK', 1);
        $modSettings['disableQueryCheck'] = true;
        require_once SOURCEDIR . '/database/Database.subs.php';
        // Attempt a connection.
        $db_connection = elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('non_fatal' => true, 'dont_select_db' => true, 'port' => $db_port), $db_type);
        $db = database();
        // No dice?  Let's try adding the prefix they specified, just in case they misread the instructions ;)
        if ($db_connection == null) {
            $db_error = $db->last_error();
            $db_connection = elk_db_initiate($db_server, $db_name, $_POST['db_prefix'] . $db_user, $db_passwd, $db_prefix, array('non_fatal' => true, 'dont_select_db' => true, 'port' => $db_port), $db_type);
            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?
        // @todo 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 != '') {
            $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 (!$db->select_db($db_name, $db_connection) && $db_name != '') {
                $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 ($db->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 (!$db->select_db($db_name, $db_connection)) {
                $incontext['error'] = sprintf($txt['error_db_database'], $db_name);
                return false;
            }
        }
        return true;
    }
    return false;
}
示例#3
0
/**
 * Load all essential data and connect to the DB as this is pre SSI.php
 */
function loadEssentialData()
{
    global $db_server, $db_user, $db_passwd, $db_name, $db_connection, $db_prefix, $db_character_set, $db_type, $db_port, $modSettings;
    // Do the non-SSI stuff...
    @set_magic_quotes_runtime(0);
    error_reporting(E_ALL);
    if (!defined('ELK')) {
        define('ELK', 1);
    }
    // Start the session.
    if (ini_get('session.save_handler') == 'user') {
        @ini_set('session.save_handler', 'files');
    }
    @session_start();
    // Initialize everything...
    initialize_inputs();
    // Get the database going!
    if (empty($db_type)) {
        $db_type = 'mysql';
    }
    if (file_exists(SOURCEDIR . '/database/Database.subs.php')) {
        require_once SOURCEDIR . '/database/Database.subs.php';
        // Make the connection...
        $db_connection = elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('non_fatal' => true, 'port' => $db_port), $db_type);
        // Oh dear god!!
        if ($db_connection === null) {
            die('Unable to connect to database - please check username and password are correct in Settings.php');
        }
        $db = database();
        if ($db_type == 'mysql' && isset($db_character_set) && preg_match('~^\\w+$~', $db_character_set) === 1) {
            $db->query('', '
			SET NAMES ' . $db_character_set, array('db_error_skip' => true));
        }
        // Load the modSettings data...
        $request = $db->query('', '
			SELECT variable, value
			FROM {db_prefix}settings', array('db_error_skip' => true));
        $modSettings = array();
        while ($row = $db->fetch_assoc($request)) {
            $modSettings[$row['variable']] = $row['value'];
        }
        $db->free_result($request);
    } else {
        return throw_error('Cannot find ' . SOURCEDIR . '/database/Database.subs.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 SUBSDIR . '/Util.class.php';
        require_once SOURCEDIR . '/Subs.php';
        require_once SOURCEDIR . '/QueryString.php';
        cleanRequest();
    }
    // Set a session life limit for the admin
    if (isset($modSettings['admin_session_lifetime'])) {
        $modSettings['admin_session_lifetime'] = 5;
    }
    if (!isset($_GET['substep'])) {
        $_GET['substep'] = 0;
    }
}
示例#4
0
文件: Load.php 项目: KeiroD/Elkarte
/**
 * Initialize a database connection.
 */
function loadDatabase()
{
    global $db_persist, $db_server, $db_user, $db_passwd, $db_port;
    global $db_type, $db_name, $ssi_db_user, $ssi_db_passwd, $db_prefix;
    // Database stuffs
    require_once SOURCEDIR . '/database/Database.subs.php';
    // Figure out what type of database we are using.
    if (empty($db_type) || !file_exists(SOURCEDIR . '/database/Db-' . $db_type . '.class.php')) {
        $db_type = 'mysql';
    }
    // 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 (ELK == 'SSI' && !empty($ssi_db_user) && !empty($ssi_db_passwd)) {
        $connection = elk_db_initiate($db_server, $db_name, $ssi_db_user, $ssi_db_passwd, $db_prefix, array('persist' => $db_persist, 'non_fatal' => true, 'dont_select_db' => true, 'port' => $db_port), $db_type);
    }
    // Either we aren't in SSI mode, or it failed.
    if (empty($connection)) {
        $connection = elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('persist' => $db_persist, 'dont_select_db' => ELK == 'SSI', 'port' => $db_port), $db_type);
    }
    // Safe guard here, if there isn't a valid connection lets put a stop to it.
    if (!$connection) {
        display_db_error();
    }
    // If in SSI mode fix up the prefix.
    $db = database();
    if (ELK == 'SSI') {
        $db_prefix = $db->fix_prefix($db_prefix, $db_name);
    }
    // Case sensitive database? Let's define a constant.
    if ($db->db_case_sensitive()) {
        DEFINE('DB_CASE_SENSITIVE', '1');
    }
}