/**
  * Compile the correct schema filename (as per create_schema_files) and
  * load it into the database.
  */
 protected function load_schema_from_file($directory, \phpbb\db\driver\driver_interface $db)
 {
     $schema = $this->dbms['SCHEMA'];
     if ($this->config['dbms'] == 'phpbb\\db\\driver\\mysql') {
         $sth = $this->pdo->query('SELECT VERSION() AS version');
         $row = $sth->fetch(PDO::FETCH_ASSOC);
         if (version_compare($row['version'], '4.1.3', '>=')) {
             $schema .= '_41';
         } else {
             $schema .= '_40';
         }
     }
     $filename = $directory . $schema . '_schema.sql';
     if (file_exists($filename)) {
         $queries = file_get_contents($filename);
         $sql = phpbb_remove_comments($queries);
         $sql = split_sql_file($sql, $this->dbms['DELIM']);
         foreach ($sql as $query) {
             $this->pdo->exec($query);
         }
     }
     // Ok we have the db info go ahead and work on building the table
     if (file_exists($directory . 'schema.json')) {
         $db_table_schema = file_get_contents($directory . 'schema.json');
         $db_table_schema = json_decode($db_table_schema, true);
     } else {
         global $phpbb_root_path, $phpEx, $table_prefix;
         $finder = new \phpbb\finder(new \phpbb\filesystem(), $phpbb_root_path, null, $phpEx);
         $classes = $finder->core_path('phpbb/db/migration/data/')->get_classes();
         $db = new \phpbb\db\driver\sqlite();
         $schema_generator = new \phpbb\db\migration\schema_generator($classes, new \phpbb\config\config(array()), $db, new \phpbb\db\tools($db, true), $phpbb_root_path, $phpEx, $table_prefix);
         $db_table_schema = $schema_generator->get_schema();
     }
     $db_tools = new \phpbb\db\tools($db, true);
     foreach ($db_table_schema as $table_name => $table_data) {
         $queries = $db_tools->sql_create_table($table_name, $table_data);
         foreach ($queries as $query) {
             if ($query === 'begin') {
                 $this->pdo->beginTransaction();
             } else {
                 if ($query === 'commit') {
                     $this->pdo->commit();
                 } else {
                     $this->pdo->exec($query);
                 }
             }
         }
     }
 }
	/**
	* Compile the correct schema filename (as per create_schema_files) and
	* load it into the database.
	*/
	protected function load_schema_from_file($directory)
	{
		$schema = $this->dbms['SCHEMA'];
		
		if ($this->config['dbms'] == 'mysql')
		{
			$sth = $this->pdo->query('SELECT VERSION() AS version');
			$row = $sth->fetch(PDO::FETCH_ASSOC);

			if (version_compare($row['version'], '4.1.3', '>='))
			{
				$schema .= '_41';
			}
			else
			{
				$schema .= '_40';
			}
		}

		$filename = $directory . $schema . '_schema.sql';

		$queries = file_get_contents($filename);
		$sql = phpbb_remove_comments($queries);
		
		$sql = split_sql_file($sql, $this->dbms['DELIM']);

		foreach ($sql as $query)
		{
			$this->pdo->exec($query);
		}
	}
Example #3
0
 /**
  * Load the contents of the schema into the database and then alter it based on what has been input during the installation
  */
 function load_schema($mode, $sub)
 {
     global $db, $lang, $template, $phpbb_root_path, $phpEx;
     $this->page_title = $lang['STAGE_CREATE_TABLE'];
     $s_hidden_fields = '';
     // Obtain any submitted data
     $data = $this->get_submitted_data();
     if ($data['dbms'] == '') {
         // Someone's been silly and tried calling this page direct
         // So we send them back to the start to do it again properly
         $this->p_master->redirect("index.{$phpEx}?mode=install");
     }
     // HTTP_HOST is having the correct browser url in most cases...
     $server_name = !empty($_SERVER['HTTP_HOST']) ? strtolower($_SERVER['HTTP_HOST']) : (!empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME'));
     $referer = !empty($_SERVER['HTTP_REFERER']) ? strtolower($_SERVER['HTTP_REFERER']) : getenv('HTTP_REFERER');
     // HTTP HOST can carry a port number...
     if (strpos($server_name, ':') !== false) {
         $server_name = substr($server_name, 0, strpos($server_name, ':'));
     }
     $cookie_domain = $data['server_name'] != '' ? $data['server_name'] : $server_name;
     // Try to come up with the best solution for cookie domain...
     if (strpos($cookie_domain, 'www.') === 0) {
         $cookie_domain = str_replace('www.', '.', $cookie_domain);
     }
     // If we get here and the extension isn't loaded it should be safe to just go ahead and load it
     $available_dbms = get_available_dbms($data['dbms']);
     if (!isset($available_dbms[$data['dbms']])) {
         // Someone's been silly and tried providing a non-existant dbms
         $this->p_master->redirect("index.{$phpEx}?mode=install");
     }
     $dbms = $available_dbms[$data['dbms']]['DRIVER'];
     // Load the appropriate database class if not already loaded
     include $phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx;
     // Instantiate the database
     $db = new $sql_db();
     $db->sql_connect($data['dbhost'], $data['dbuser'], htmlspecialchars_decode($data['dbpasswd']), $data['dbname'], $data['dbport'], false, false);
     // NOTE: trigger_error does not work here.
     $db->sql_return_on_error(true);
     // If mysql is chosen, we need to adjust the schema filename slightly to reflect the correct version. ;)
     if ($data['dbms'] == 'mysql') {
         if (version_compare($db->sql_server_info(true), '4.1.3', '>=')) {
             $available_dbms[$data['dbms']]['SCHEMA'] .= '_41';
         } else {
             $available_dbms[$data['dbms']]['SCHEMA'] .= '_40';
         }
     }
     // Ok we have the db info go ahead and read in the relevant schema
     // and work on building the table
     $dbms_schema = 'schemas/' . $available_dbms[$data['dbms']]['SCHEMA'] . '_schema.sql';
     // How should we treat this schema?
     $delimiter = $available_dbms[$data['dbms']]['DELIM'];
     $sql_query = @file_get_contents($dbms_schema);
     $sql_query = preg_replace('#phpbb_#i', $data['table_prefix'], $sql_query);
     $sql_query = phpbb_remove_comments($sql_query);
     $sql_query = split_sql_file($sql_query, $delimiter);
     foreach ($sql_query as $sql) {
         //$sql = trim(str_replace('|', ';', $sql));
         if (!$db->sql_query($sql)) {
             $error = $db->sql_error();
             $this->p_master->db_error($error['message'], $sql, __LINE__, __FILE__);
         }
     }
     unset($sql_query);
     // Ok tables have been built, let's fill in the basic information
     $sql_query = file_get_contents('schemas/schema_data.sql');
     // Deal with any special comments
     switch ($data['dbms']) {
         case 'mssql':
         case 'mssql_odbc':
         case 'mssqlnative':
             $sql_query = preg_replace('#\\# MSSQL IDENTITY (phpbb_[a-z_]+) (ON|OFF) \\##s', 'SET IDENTITY_INSERT \\1 \\2;', $sql_query);
             break;
         case 'postgres':
             $sql_query = preg_replace('#\\# POSTGRES (BEGIN|COMMIT) \\##s', '\\1; ', $sql_query);
             break;
     }
     // Change prefix
     $sql_query = preg_replace('# phpbb_([^\\s]*) #i', ' ' . $data['table_prefix'] . '\\1 ', $sql_query);
     // Change language strings...
     $sql_query = preg_replace_callback('#\\{L_([A-Z0-9\\-_]*)\\}#s', 'adjust_language_keys_callback', $sql_query);
     $sql_query = phpbb_remove_comments($sql_query);
     $sql_query = split_sql_file($sql_query, ';');
     foreach ($sql_query as $sql) {
         //$sql = trim(str_replace('|', ';', $sql));
         if (!$db->sql_query($sql)) {
             $error = $db->sql_error();
             $this->p_master->db_error($error['message'], $sql, __LINE__, __FILE__);
         }
     }
     unset($sql_query);
     $current_time = time();
     $user_ip = !empty($_SERVER['REMOTE_ADDR']) ? htmlspecialchars($_SERVER['REMOTE_ADDR']) : '';
     $user_ip = stripos($user_ip, '::ffff:') === 0 ? substr($user_ip, 7) : $user_ip;
     if ($data['script_path'] !== '/') {
         // Adjust destination path (no trailing slash)
         if (substr($data['script_path'], -1) == '/') {
             $data['script_path'] = substr($data['script_path'], 0, -1);
         }
         $data['script_path'] = str_replace(array('../', './'), '', $data['script_path']);
         if ($data['script_path'][0] != '/') {
             $data['script_path'] = '/' . $data['script_path'];
         }
     }
     // Set default config and post data, this applies to all DB's
     $sql_ary = array('INSERT INTO ' . $data['table_prefix'] . "config (config_name, config_value)\n\t\t\t\tVALUES ('board_startdate', '{$current_time}')", 'INSERT INTO ' . $data['table_prefix'] . "config (config_name, config_value)\n\t\t\t\tVALUES ('default_lang', '" . $db->sql_escape($data['default_lang']) . "')", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['img_imagick']) . "'\n\t\t\t\tWHERE config_name = 'img_imagick'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['server_name']) . "'\n\t\t\t\tWHERE config_name = 'server_name'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['server_port']) . "'\n\t\t\t\tWHERE config_name = 'server_port'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['board_email1']) . "'\n\t\t\t\tWHERE config_name = 'board_email'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['board_email1']) . "'\n\t\t\t\tWHERE config_name = 'board_contact'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($cookie_domain) . "'\n\t\t\t\tWHERE config_name = 'cookie_domain'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($lang['default_dateformat']) . "'\n\t\t\t\tWHERE config_name = 'default_dateformat'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['email_enable']) . "'\n\t\t\t\tWHERE config_name = 'email_enable'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['smtp_delivery']) . "'\n\t\t\t\tWHERE config_name = 'smtp_delivery'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['smtp_host']) . "'\n\t\t\t\tWHERE config_name = 'smtp_host'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['smtp_auth']) . "'\n\t\t\t\tWHERE config_name = 'smtp_auth_method'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['smtp_user']) . "'\n\t\t\t\tWHERE config_name = 'smtp_username'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['smtp_pass']) . "'\n\t\t\t\tWHERE config_name = 'smtp_password'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['cookie_secure']) . "'\n\t\t\t\tWHERE config_name = 'cookie_secure'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['force_server_vars']) . "'\n\t\t\t\tWHERE config_name = 'force_server_vars'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['script_path']) . "'\n\t\t\t\tWHERE config_name = 'script_path'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['server_protocol']) . "'\n\t\t\t\tWHERE config_name = 'server_protocol'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($data['admin_name']) . "'\n\t\t\t\tWHERE config_name = 'newest_username'", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . md5(mt_rand()) . "'\n\t\t\t\tWHERE config_name = 'avatar_salt'", 'UPDATE ' . $data['table_prefix'] . "users\n\t\t\t\tSET username = '******'admin_name']) . "', user_password='******'admin_pass1'])) . "', user_ip = '" . $db->sql_escape($user_ip) . "', user_lang = '" . $db->sql_escape($data['default_lang']) . "', user_email='" . $db->sql_escape($data['board_email1']) . "', user_dateformat='" . $db->sql_escape($lang['default_dateformat']) . "', user_email_hash = " . $db->sql_escape(phpbb_email_hash($data['board_email1'])) . ", username_clean = '" . $db->sql_escape(utf8_clean_string($data['admin_name'])) . "'\n\t\t\t\tWHERE username = '******'", 'UPDATE ' . $data['table_prefix'] . "moderator_cache\n\t\t\t\tSET username = '******'admin_name']) . "'\n\t\t\t\tWHERE username = '******'", 'UPDATE ' . $data['table_prefix'] . "forums\n\t\t\t\tSET forum_last_poster_name = '" . $db->sql_escape($data['admin_name']) . "'\n\t\t\t\tWHERE forum_last_poster_name = 'Admin'", 'UPDATE ' . $data['table_prefix'] . "topics\n\t\t\t\tSET topic_first_poster_name = '" . $db->sql_escape($data['admin_name']) . "', topic_last_poster_name = '" . $db->sql_escape($data['admin_name']) . "'\n\t\t\t\tWHERE topic_first_poster_name = 'Admin'\n\t\t\t\t\tOR topic_last_poster_name = 'Admin'", 'UPDATE ' . $data['table_prefix'] . "users\n\t\t\t\tSET user_regdate = {$current_time}", 'UPDATE ' . $data['table_prefix'] . "posts\n\t\t\t\tSET post_time = {$current_time}, poster_ip = '" . $db->sql_escape($user_ip) . "'", 'UPDATE ' . $data['table_prefix'] . "topics\n\t\t\t\tSET topic_time = {$current_time}, topic_last_post_time = {$current_time}", 'UPDATE ' . $data['table_prefix'] . "forums\n\t\t\t\tSET forum_last_post_time = {$current_time}", 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '" . $db->sql_escape($db->sql_server_info(true)) . "'\n\t\t\t\tWHERE config_name = 'dbms_version'");
     if (@extension_loaded('gd') || can_load_dll('gd')) {
         $sql_ary[] = 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = 'phpbb_captcha_gd'\n\t\t\t\tWHERE config_name = 'captcha_plugin'";
         $sql_ary[] = 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '1'\n\t\t\t\tWHERE config_name = 'captcha_gd'";
     }
     $ref = substr($referer, strpos($referer, '://') + 3);
     if (!(stripos($ref, $server_name) === 0)) {
         $sql_ary[] = 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\t\tSET config_value = '0'\n\t\t\t\tWHERE config_name = 'referer_validation'";
     }
     // We set a (semi-)unique cookie name to bypass login issues related to the cookie name.
     $cookie_name = 'phpbb3_';
     $rand_str = md5(mt_rand());
     $rand_str = str_replace('0', 'z', base_convert($rand_str, 16, 35));
     $rand_str = substr($rand_str, 0, 5);
     $cookie_name .= strtolower($rand_str);
     $sql_ary[] = 'UPDATE ' . $data['table_prefix'] . "config\n\t\t\tSET config_value = '" . $db->sql_escape($cookie_name) . "'\n\t\t\tWHERE config_name = 'cookie_name'";
     foreach ($sql_ary as $sql) {
         //$sql = trim(str_replace('|', ';', $sql));
         if (!$db->sql_query($sql)) {
             $error = $db->sql_error();
             $this->p_master->db_error($error['message'], $sql, __LINE__, __FILE__);
         }
     }
     $submit = $lang['NEXT_STEP'];
     $url = $this->p_master->module_url . "?mode={$mode}&sub=final";
     $template->assign_vars(array('BODY' => $lang['STAGE_CREATE_TABLE_EXPLAIN'], 'L_SUBMIT' => $submit, 'S_HIDDEN' => build_hidden_fields($data), 'U_ACTION' => $url));
 }
function load_schema_31($install_path = '', $install_dbms = false)
{
    global $db, $settings, $table_prefix, $phpbb_root_path, $phpEx;
    static $available_dbms = false;
    if ($install_dbms === false) {
        $dbms = $settings->get_config('dbms');
        $install_dbms = $dbms;
    }
    if (!$available_dbms) {
        $available_dbms = get_available_dbms($install_dbms);
        // If mysql is chosen, we need to adjust the schema filename slightly to reflect the correct version. ;)
        if ($install_dbms == 'mysql') {
            if (version_compare($db->sql_server_info(true), '4.1.3', '>=')) {
                $available_dbms[$install_dbms]['SCHEMA'] .= '_41';
            } else {
                $available_dbms[$install_dbms]['SCHEMA'] .= '_40';
            }
        }
    }
    // Ok we have the db info go ahead and read in the relevant schema
    // and work on building the table
    $dbms_schema = $install_path . $available_dbms[$install_dbms]['SCHEMA'] . '_schema.sql';
    // How should we treat this schema?
    $delimiter = $available_dbms[$install_dbms]['DELIM'];
    if (file_exists($dbms_schema)) {
        $sql_query = file_get_contents($dbms_schema);
        $sql_query = preg_replace('#phpbb_#i', $table_prefix, $sql_query);
        $sql_query = phpbb_remove_comments($sql_query);
        $sql_query = split_sql_file($sql_query, $delimiter);
        foreach ($sql_query as $sql) {
            $db->sql_query($sql);
        }
        unset($sql_query);
    }
    // Ok we have the db info go ahead and work on building the table
    if (file_exists($install_path . 'schema.json')) {
        $db_table_schema = file_get_contents($install_path . 'schema.json');
        $db_table_schema = json_decode($db_table_schema, true);
    } else {
        $table_prefix = 'phpbb_';
        if (!defined('CONFIG_TABLE')) {
            // We need to include the constants file for the table constants
            // when we generate the schema from the migration files.
            include $phpbb_root_path . 'includes/constants.' . $phpEx;
        }
        $finder = new \phpbb\finder(new \phpbb\filesystem(), $phpbb_root_path, null, $phpEx);
        $classes = $finder->core_path('phpbb/db/migration/data/')->get_classes();
        $sqlite_db = new \phpbb\db\driver\sqlite();
        $schema_generator = new \phpbb\db\migration\schema_generator($classes, new \phpbb\config\config(array()), $sqlite_db, new \phpbb\db\tools($sqlite_db, true), $phpbb_root_path, $phpEx, $table_prefix);
        $db_table_schema = $schema_generator->get_schema();
    }
    if (!defined('CONFIG_TABLE')) {
        // CONFIG_TABLE is required by sql_create_index() to check the
        // length of index names. However table_prefix is not defined
        // here yet, so we need to create the constant ourselves.
        define('CONFIG_TABLE', $table_prefix . 'config');
    }
    $db_tools = new \phpbb\db\tools($db);
    foreach ($db_table_schema as $table_name => $table_data) {
        $db_tools->sql_create_table($table_prefix . substr($table_name, 6), $table_data);
    }
    // Ok tables have been built, let's fill in the basic information
    $sql_query = file_get_contents($install_path . 'schema_data.sql');
    // Deal with any special comments and characters
    switch ($install_dbms) {
        case 'mssql':
        case 'mssql_odbc':
        case 'mssqlnative':
            $sql_query = preg_replace('#\\# MSSQL IDENTITY (phpbb_[a-z_]+) (ON|OFF) \\##s', 'SET IDENTITY_INSERT \\1 \\2;', $sql_query);
            break;
        case 'postgres':
            $sql_query = preg_replace('#\\# POSTGRES (BEGIN|COMMIT) \\##s', '\\1; ', $sql_query);
            break;
        case 'mysql':
        case 'mysqli':
            $sql_query = str_replace('\\', '\\\\', $sql_query);
            break;
    }
    // Change prefix
    $sql_query = preg_replace('# phpbb_([^\\s]*) #i', ' ' . $table_prefix . '\\1 ', $sql_query);
    // Change language strings...
    $sql_query = preg_replace_callback('#\\{L_([A-Z0-9\\-_]*)\\}#s', 'adjust_language_keys_callback', $sql_query);
    $sql_query = phpbb_remove_comments($sql_query);
    $sql_query = split_sql_file($sql_query, ';');
    foreach ($sql_query as $sql) {
        $db->sql_query($sql);
    }
    unset($sql_query);
}
Example #5
0
	/**
	* Load the contents of the schema into the database and then alter it based on what has been input during the installation
	*/
	function load_schema($mode, $sub)
	{
		global $db, $lang, $template, $phpbb_root_path, $phpEx, $request;

		$this->page_title = $lang['STAGE_CREATE_TABLE'];
		$s_hidden_fields = '';

		// Obtain any submitted data
		$data = $this->get_submitted_data();

		if ($data['dbms'] == '')
		{
			// Someone's been silly and tried calling this page direct
			// So we send them back to the start to do it again properly
			$this->p_master->redirect("index.$phpEx?mode=install");
		}

		// HTTP_HOST is having the correct browser url in most cases...
		$server_name = strtolower(htmlspecialchars_decode($request->header('Host', $request->server('SERVER_NAME'))));
		$referer = strtolower($request->header('Referer'));

		// HTTP HOST can carry a port number...
		if (strpos($server_name, ':') !== false)
		{
			$server_name = substr($server_name, 0, strpos($server_name, ':'));
		}

		$cookie_domain = ($data['server_name'] != '') ? $data['server_name'] : $server_name;

		// Try to come up with the best solution for cookie domain...
		if (strpos($cookie_domain, 'www.') === 0)
		{
			$cookie_domain = str_replace('www.', '.', $cookie_domain);
		}

		// If we get here and the extension isn't loaded it should be safe to just go ahead and load it
		$available_dbms = get_available_dbms($data['dbms']);

		if (!isset($available_dbms[$data['dbms']]))
		{
			// Someone's been silly and tried providing a non-existant dbms
			$this->p_master->redirect("index.$phpEx?mode=install");
		}

		$dbms = $available_dbms[$data['dbms']]['DRIVER'];

		// Instantiate the database
		$db = new $dbms();
		$db->sql_connect($data['dbhost'], $data['dbuser'], htmlspecialchars_decode($data['dbpasswd']), $data['dbname'], $data['dbport'], false, false);

		// NOTE: trigger_error does not work here.
		$db->sql_return_on_error(true);

		// If mysql is chosen, we need to adjust the schema filename slightly to reflect the correct version. ;)
		if ($data['dbms'] == 'mysql')
		{
			if (version_compare($db->sql_server_info(true), '4.1.3', '>='))
			{
				$available_dbms[$data['dbms']]['SCHEMA'] .= '_41';
			}
			else
			{
				$available_dbms[$data['dbms']]['SCHEMA'] .= '_40';
			}
		}

		// Ok we have the db info go ahead and read in the relevant schema
		// and work on building the table
		$dbms_schema = 'schemas/' . $available_dbms[$data['dbms']]['SCHEMA'] . '_schema.sql';

		// How should we treat this schema?
		$delimiter = $available_dbms[$data['dbms']]['DELIM'];

		if (file_exists($dbms_schema))
		{
			$sql_query = @file_get_contents($dbms_schema);
			$sql_query = preg_replace('#phpbb_#i', $data['table_prefix'], $sql_query);
			$sql_query = phpbb_remove_comments($sql_query);
			$sql_query = split_sql_file($sql_query, $delimiter);

			foreach ($sql_query as $sql)
			{
				// Ignore errors when the functions or types already exist
				// to allow installing phpBB twice in the same database with
				// a different prefix
				$db->sql_query($sql);
			}
			unset($sql_query);
		}

		// Ok we have the db info go ahead and work on building the table
		if (file_exists('schemas/schema.json'))
		{
			$db_table_schema = @file_get_contents('schemas/schema.json');
			$db_table_schema = json_decode($db_table_schema, true);
		}
		else
		{
			global $phpbb_root_path, $phpEx, $table_prefix;
			$table_prefix = 'phpbb_';

			if (!defined('CONFIG_TABLE'))
			{
				// We need to include the constants file for the table constants
				// when we generate the schema from the migration files.
				include($phpbb_root_path . 'includes/constants.' . $phpEx);
			}

			$finder = new \phpbb\finder(new \phpbb\filesystem\filesystem(), $phpbb_root_path, null, $phpEx);
			$classes = $finder->core_path('phpbb/db/migration/data/')
				->get_classes();

			$sqlite_db = new \phpbb\db\driver\sqlite();
			$factory = new \phpbb\db\tools\factory();
			$db_tools = $factory->get($sqlite_db, true);
			$schema_generator = new \phpbb\db\migration\schema_generator($classes, new \phpbb\config\config(array()), $sqlite_db, $db_tools, $phpbb_root_path, $phpEx, $table_prefix);
			$db_table_schema = $schema_generator->get_schema();
		}

		if (!defined('CONFIG_TABLE'))
		{
			// CONFIG_TABLE is required by sql_create_index() to check the
			// length of index names. However table_prefix is not defined
			// here yet, so we need to create the constant ourselves.
			define('CONFIG_TABLE', $data['table_prefix'] . 'config');
		}

		$factory = new \phpbb\db\tools\factory();
		$db_tools = $factory->get($db);
		foreach ($db_table_schema as $table_name => $table_data)
		{
			$db_tools->sql_create_table(
				$data['table_prefix'] . substr($table_name, 6),
				$table_data
			);
		}

		// Ok tables have been built, let's fill in the basic information
		$sql_query = file_get_contents('schemas/schema_data.sql');

		// Deal with any special comments and characters
		switch ($data['dbms'])
		{
			case 'mssql':
			case 'mssql_odbc':
			case 'mssqlnative':
				$sql_query = preg_replace('#\# MSSQL IDENTITY (phpbb_[a-z_]+) (ON|OFF) \##s', 'SET IDENTITY_INSERT \1 \2;', $sql_query);
			break;

			case 'postgres':
				$sql_query = preg_replace('#\# POSTGRES (BEGIN|COMMIT) \##s', '\1; ', $sql_query);
			break;

			case 'mysql':
			case 'mysqli':
				$sql_query = str_replace('\\', '\\\\', $sql_query);
			break;
		}

		// Change prefix
		$sql_query = preg_replace('# phpbb_([^\s]*) #i', ' ' . $data['table_prefix'] . '\1 ', $sql_query);

		// Change language strings...
		$sql_query = preg_replace_callback('#\{L_([A-Z0-9\-_]*)\}#s', 'adjust_language_keys_callback', $sql_query);

		$sql_query = phpbb_remove_comments($sql_query);
		$sql_query = split_sql_file($sql_query, ';');

		foreach ($sql_query as $sql)
		{
			//$sql = trim(str_replace('|', ';', $sql));
			if (!$db->sql_query($sql))
			{
				$error = $db->sql_error();
				$this->p_master->db_error($error['message'], $sql, __LINE__, __FILE__);
			}
		}
		unset($sql_query);

		$current_time = time();

		$user_ip = $request->server('REMOTE_ADDR') ? phpbb_ip_normalise($request->server('REMOTE_ADDR')) : '';

		if ($data['script_path'] !== '/')
		{
			// Adjust destination path (no trailing slash)
			if (substr($data['script_path'], -1) == '/')
			{
				$data['script_path'] = substr($data['script_path'], 0, -1);
			}

			$data['script_path'] = str_replace(array('../', './'), '', $data['script_path']);

			if ($data['script_path'][0] != '/')
			{
				$data['script_path'] = '/' . $data['script_path'];
			}
		}

		// Set default config and post data, this applies to all DB's
		$sql_ary = array(
			'INSERT INTO ' . $data['table_prefix'] . "config (config_name, config_value)
				VALUES ('board_startdate', '$current_time')",

			'INSERT INTO ' . $data['table_prefix'] . "config (config_name, config_value)
				VALUES ('default_lang', '" . $db->sql_escape($data['default_lang']) . "')",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['img_imagick']) . "'
				WHERE config_name = 'img_imagick'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['server_name']) . "'
				WHERE config_name = 'server_name'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['server_port']) . "'
				WHERE config_name = 'server_port'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['board_email']) . "'
				WHERE config_name = 'board_email'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['board_email']) . "'
				WHERE config_name = 'board_contact'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($cookie_domain) . "'
				WHERE config_name = 'cookie_domain'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($lang['default_dateformat']) . "'
				WHERE config_name = 'default_dateformat'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['email_enable']) . "'
				WHERE config_name = 'email_enable'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['smtp_delivery']) . "'
				WHERE config_name = 'smtp_delivery'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['smtp_host']) . "'
				WHERE config_name = 'smtp_host'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['smtp_auth']) . "'
				WHERE config_name = 'smtp_auth_method'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['smtp_user']) . "'
				WHERE config_name = 'smtp_username'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['smtp_pass']) . "'
				WHERE config_name = 'smtp_password'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['cookie_secure']) . "'
				WHERE config_name = 'cookie_secure'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['force_server_vars']) . "'
				WHERE config_name = 'force_server_vars'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['script_path']) . "'
				WHERE config_name = 'script_path'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['server_protocol']) . "'
				WHERE config_name = 'server_protocol'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($data['admin_name']) . "'
				WHERE config_name = 'newest_username'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . md5(mt_rand()) . "'
				WHERE config_name = 'avatar_salt'",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . md5(mt_rand()) . "'
				WHERE config_name = 'plupload_salt'",

			'UPDATE ' . $data['table_prefix'] . "users
				SET username = '******'admin_name']) . "', user_password='******'admin_pass1'])) . "', user_ip = '" . $db->sql_escape($user_ip) . "', user_lang = '" . $db->sql_escape($data['default_lang']) . "', user_email='" . $db->sql_escape($data['board_email']) . "', user_dateformat='" . $db->sql_escape($lang['default_dateformat']) . "', user_email_hash = " . $db->sql_escape(phpbb_email_hash($data['board_email'])) . ", username_clean = '" . $db->sql_escape(utf8_clean_string($data['admin_name'])) . "'
				WHERE username = '******'",

			'UPDATE ' . $data['table_prefix'] . "moderator_cache
				SET username = '******'admin_name']) . "'
				WHERE username = '******'",

			'UPDATE ' . $data['table_prefix'] . "forums
				SET forum_last_poster_name = '" . $db->sql_escape($data['admin_name']) . "'
				WHERE forum_last_poster_name = 'Admin'",

			'UPDATE ' . $data['table_prefix'] . "topics
				SET topic_first_poster_name = '" . $db->sql_escape($data['admin_name']) . "', topic_last_poster_name = '" . $db->sql_escape($data['admin_name']) . "'
				WHERE topic_first_poster_name = 'Admin'
					OR topic_last_poster_name = 'Admin'",

			'UPDATE ' . $data['table_prefix'] . "users
				SET user_regdate = $current_time",

			'UPDATE ' . $data['table_prefix'] . "posts
				SET post_time = $current_time, poster_ip = '" . $db->sql_escape($user_ip) . "'",

			'UPDATE ' . $data['table_prefix'] . "topics
				SET topic_time = $current_time, topic_last_post_time = $current_time",

			'UPDATE ' . $data['table_prefix'] . "forums
				SET forum_last_post_time = $current_time",

			'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '" . $db->sql_escape($db->sql_server_info(true)) . "'
				WHERE config_name = 'dbms_version'",
		);

		if (@extension_loaded('gd'))
		{
			$sql_ary[] = 'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = 'core.captcha.plugins.gd'
				WHERE config_name = 'captcha_plugin'";

			$sql_ary[] = 'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '1'
				WHERE config_name = 'captcha_gd'";
		}

		$ref = substr($referer, strpos($referer, '://') + 3);

		if (!(stripos($ref, $server_name) === 0))
		{
			$sql_ary[] = 'UPDATE ' . $data['table_prefix'] . "config
				SET config_value = '0'
				WHERE config_name = 'referer_validation'";
		}

		// We set a (semi-)unique cookie name to bypass login issues related to the cookie name.
		$cookie_name = 'phpbb3_';
		$rand_str = md5(mt_rand());
		$rand_str = str_replace('0', 'z', base_convert($rand_str, 16, 35));
		$rand_str = substr($rand_str, 0, 5);
		$cookie_name .= strtolower($rand_str);

		$sql_ary[] = 'UPDATE ' . $data['table_prefix'] . "config
			SET config_value = '" . $db->sql_escape($cookie_name) . "'
			WHERE config_name = 'cookie_name'";

		foreach ($sql_ary as $sql)
		{
			//$sql = trim(str_replace('|', ';', $sql));

			if (!$db->sql_query($sql))
			{
				$error = $db->sql_error();
				$this->p_master->db_error($error['message'], $sql, __LINE__, __FILE__);
			}
		}

		$submit = $lang['NEXT_STEP'];

		$url = $this->p_master->module_url . "?mode=$mode&sub=final";

		$template->assign_vars(array(
			'BODY'		=> $lang['STAGE_CREATE_TABLE_EXPLAIN'],
			'L_SUBMIT'	=> $submit,
			'S_HIDDEN'	=> build_hidden_fields($data),
			'U_ACTION'	=> $url,
		));
	}