Blame Extras/phpBB/3.0.4/includes/functions_install.php

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package install
4c79b5
* @version $Id: functions_install.php 8507 2008-04-20 04:57:29Z davidmj $
4c79b5
* @copyright (c) 2006 phpBB Group
4c79b5
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
4c79b5
*
4c79b5
*/
4c79b5
4c79b5
/**
4c79b5
* @ignore
4c79b5
*/
4c79b5
if (!defined('IN_PHPBB'))
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Determine if we are able to load a specified PHP module and do so if possible
4c79b5
*/
4c79b5
function can_load_dll($dll)
4c79b5
{
4c79b5
	return ((@ini_get('enable_dl') || strtolower(@ini_get('enable_dl')) == 'on') && (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') && @dl($dll . '.' . PHP_SHLIB_SUFFIX)) ? true : false;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Returns an array of available DBMS with some data, if a DBMS is specified it will only
4c79b5
* return data for that DBMS and will load its extension if necessary.
4c79b5
*/
4c79b5
function get_available_dbms($dbms = false, $return_unavailable = false, $only_20x_options = false)
4c79b5
{
4c79b5
	global $lang;
4c79b5
	$available_dbms = array(
4c79b5
		'firebird'	=> array(
4c79b5
			'LABEL'			=> 'FireBird',
4c79b5
			'SCHEMA'		=> 'firebird',
4c79b5
			'MODULE'		=> 'interbase',
4c79b5
			'DELIM'			=> ';;',
4c79b5
			'COMMENTS'		=> 'remove_remarks',
4c79b5
			'DRIVER'		=> 'firebird',
4c79b5
			'AVAILABLE'		=> true,
4c79b5
			'2.0.x'			=> false,
4c79b5
		),
4c79b5
		'mysqli'	=> array(
4c79b5
			'LABEL'			=> 'MySQL with MySQLi Extension',
4c79b5
			'SCHEMA'		=> 'mysql_41',
4c79b5
			'MODULE'		=> 'mysqli',
4c79b5
			'DELIM'			=> ';',
4c79b5
			'COMMENTS'		=> 'remove_remarks',
4c79b5
			'DRIVER'		=> 'mysqli',
4c79b5
			'AVAILABLE'		=> true,
4c79b5
			'2.0.x'			=> true,
4c79b5
		),
4c79b5
		'mysql'		=> array(
4c79b5
			'LABEL'			=> 'MySQL',
4c79b5
			'SCHEMA'		=> 'mysql',
4c79b5
			'MODULE'		=> 'mysql',
4c79b5
			'DELIM'			=> ';',
4c79b5
			'COMMENTS'		=> 'remove_remarks',
4c79b5
			'DRIVER'		=> 'mysql',
4c79b5
			'AVAILABLE'		=> true,
4c79b5
			'2.0.x'			=> true,
4c79b5
		),
4c79b5
		'mssql'		=> array(
4c79b5
			'LABEL'			=> 'MS SQL Server 2000+',
4c79b5
			'SCHEMA'		=> 'mssql',
4c79b5
			'MODULE'		=> 'mssql',
4c79b5
			'DELIM'			=> 'GO',
4c79b5
			'COMMENTS'		=> 'remove_comments',
4c79b5
			'DRIVER'		=> 'mssql',
4c79b5
			'AVAILABLE'		=> true,
4c79b5
			'2.0.x'			=> true,
4c79b5
		),
4c79b5
		'mssql_odbc'=>	array(
4c79b5
			'LABEL'			=> 'MS SQL Server [ ODBC ]',
4c79b5
			'SCHEMA'		=> 'mssql',
4c79b5
			'MODULE'		=> 'odbc',
4c79b5
			'DELIM'			=> 'GO',
4c79b5
			'COMMENTS'		=> 'remove_comments',
4c79b5
			'DRIVER'		=> 'mssql_odbc',
4c79b5
			'AVAILABLE'		=> true,
4c79b5
			'2.0.x'			=> true,
4c79b5
		),
4c79b5
		'oracle'	=>	array(
4c79b5
			'LABEL'			=> 'Oracle',
4c79b5
			'SCHEMA'		=> 'oracle',
4c79b5
			'MODULE'		=> 'oci8',
4c79b5
			'DELIM'			=> '/',
4c79b5
			'COMMENTS'		=> 'remove_comments',
4c79b5
			'DRIVER'		=> 'oracle',
4c79b5
			'AVAILABLE'		=> true,
4c79b5
			'2.0.x'			=> false,
4c79b5
		),
4c79b5
		'postgres' => array(
4c79b5
			'LABEL'			=> 'PostgreSQL 7.x/8.x',
4c79b5
			'SCHEMA'		=> 'postgres',
4c79b5
			'MODULE'		=> 'pgsql',
4c79b5
			'DELIM'			=> ';',
4c79b5
			'COMMENTS'		=> 'remove_comments',
4c79b5
			'DRIVER'		=> 'postgres',
4c79b5
			'AVAILABLE'		=> true,
4c79b5
			'2.0.x'			=> true,
4c79b5
		),
4c79b5
		'sqlite'		=> array(
4c79b5
			'LABEL'			=> 'SQLite',
4c79b5
			'SCHEMA'		=> 'sqlite',
4c79b5
			'MODULE'		=> 'sqlite',
4c79b5
			'DELIM'			=> ';',
4c79b5
			'COMMENTS'		=> 'remove_remarks',
4c79b5
			'DRIVER'		=> 'sqlite',
4c79b5
			'AVAILABLE'		=> true,
4c79b5
			'2.0.x'			=> false,
4c79b5
		),
4c79b5
	);
4c79b5
4c79b5
	if ($dbms)
4c79b5
	{
4c79b5
		if (isset($available_dbms[$dbms]))
4c79b5
		{
4c79b5
			$available_dbms = array($dbms => $available_dbms[$dbms]);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			return array();
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// now perform some checks whether they are really available
4c79b5
	foreach ($available_dbms as $db_name => $db_ary)
4c79b5
	{
4c79b5
		if ($only_20x_options && !$db_ary['2.0.x'])
4c79b5
		{
4c79b5
			if ($return_unavailable)
4c79b5
			{
4c79b5
				$available_dbms[$db_name]['AVAILABLE'] = false;
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				unset($available_dbms[$db_name]);
4c79b5
			}
4c79b5
			continue;
4c79b5
		}
4c79b5
4c79b5
		$dll = $db_ary['MODULE'];
4c79b5
4c79b5
		if (!@extension_loaded($dll))
4c79b5
		{
4c79b5
			if (!can_load_dll($dll))
4c79b5
			{
4c79b5
				if ($return_unavailable)
4c79b5
				{
4c79b5
					$available_dbms[$db_name]['AVAILABLE'] = false;
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					unset($available_dbms[$db_name]);
4c79b5
				}
4c79b5
				continue;
4c79b5
			}
4c79b5
		}
4c79b5
		$any_db_support = true;
4c79b5
	}
4c79b5
4c79b5
	if ($return_unavailable)
4c79b5
	{
4c79b5
		$available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
4c79b5
	}
4c79b5
	return $available_dbms;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Generate the drop down of available database options
4c79b5
*/
4c79b5
function dbms_select($default = '', $only_20x_options = false)
4c79b5
{
4c79b5
	global $lang;
4c79b5
	
4c79b5
	$available_dbms = get_available_dbms(false, false, $only_20x_options);
4c79b5
	$dbms_options = '';
4c79b5
	foreach ($available_dbms as $dbms_name => $details)
4c79b5
	{
4c79b5
		$selected = ($dbms_name == $default) ? ' selected="selected"' : '';
4c79b5
		$dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
4c79b5
	}
4c79b5
	return $dbms_options;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Get tables of a database
4c79b5
*/
4c79b5
function get_tables($db)
4c79b5
{
4c79b5
	switch ($db->sql_layer)
4c79b5
	{
4c79b5
		case 'mysql':
4c79b5
		case 'mysql4':
4c79b5
		case 'mysqli':
4c79b5
			$sql = 'SHOW TABLES';
4c79b5
		break;
4c79b5
4c79b5
		case 'sqlite':
4c79b5
			$sql = 'SELECT name
4c79b5
				FROM sqlite_master
4c79b5
				WHERE type = "table"';
4c79b5
		break;
4c79b5
4c79b5
		case 'mssql':
4c79b5
		case 'mssql_odbc':
4c79b5
			$sql = "SELECT name
4c79b5
				FROM sysobjects
4c79b5
				WHERE type='U'";
4c79b5
		break;
4c79b5
4c79b5
		case 'postgres':
4c79b5
			$sql = 'SELECT relname
4c79b5
				FROM pg_stat_user_tables';
4c79b5
		break;
4c79b5
4c79b5
		case 'firebird':
4c79b5
			$sql = 'SELECT rdb$relation_name
4c79b5
				FROM rdb$relations
4c79b5
				WHERE rdb$view_source is null
4c79b5
					AND rdb$system_flag = 0';
4c79b5
		break;
4c79b5
4c79b5
		case 'oracle':
4c79b5
			$sql = 'SELECT table_name
4c79b5
				FROM USER_TABLES';
4c79b5
		break;
4c79b5
	}
4c79b5
4c79b5
	$result = $db->sql_query($sql);
4c79b5
4c79b5
	$tables = array();
4c79b5
4c79b5
	while ($row = $db->sql_fetchrow($result))
4c79b5
	{
4c79b5
		$tables[] = current($row);
4c79b5
	}
4c79b5
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	return $tables;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Used to test whether we are able to connect to the database the user has specified
4c79b5
* and identify any problems (eg there are already tables with the names we want to use
4c79b5
* @param	array	$dbms should be of the format of an element of the array returned by {@link get_available_dbms get_available_dbms()}
4c79b5
*					necessary extensions should be loaded already
4c79b5
*/
4c79b5
function connect_check_db($error_connect, &$error, $dbms_details, $table_prefix, $dbhost, $dbuser, $dbpasswd, $dbname, $dbport, $prefix_may_exist = false, $load_dbal = true, $unicode_check = true)
4c79b5
{
4c79b5
	global $phpbb_root_path, $phpEx, $config, $lang;
4c79b5
4c79b5
	$dbms = $dbms_details['DRIVER'];
4c79b5
4c79b5
	if ($load_dbal)
4c79b5
	{
4c79b5
		// Include the DB layer
4c79b5
		include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
4c79b5
	}
4c79b5
4c79b5
	// Instantiate it and set return on error true
4c79b5
	$sql_db = 'dbal_' . $dbms;
4c79b5
	$db = new $sql_db();
4c79b5
	$db->sql_return_on_error(true);
4c79b5
4c79b5
	// Check that we actually have a database name before going any further.....
4c79b5
	if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
4c79b5
	{
4c79b5
		$error[] = $lang['INST_ERR_DB_NO_NAME'];
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	// Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea
4c79b5
	if ($dbms_details['DRIVER'] == 'sqlite' && stripos(phpbb_realpath($dbhost), phpbb_realpath('../')) === 0)
4c79b5
	{
4c79b5
		$error[] = $lang['INST_ERR_DB_FORUM_PATH'];
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	// Check the prefix length to ensure that index names are not too long and does not contain invalid characters
4c79b5
	switch ($dbms_details['DRIVER'])
4c79b5
	{
4c79b5
		case 'mysql':
4c79b5
		case 'mysqli':
4c79b5
			if (strspn($table_prefix, '-./\\') !== 0)
4c79b5
			{
4c79b5
				$error[] = $lang['INST_ERR_PREFIX_INVALID'];
4c79b5
				return false;
4c79b5
			}
4c79b5
4c79b5
		// no break;
4c79b5
4c79b5
		case 'postgres':
4c79b5
			$prefix_length = 36;
4c79b5
		break;
4c79b5
4c79b5
		case 'mssql':
4c79b5
		case 'mssql_odbc':
4c79b5
			$prefix_length = 90;
4c79b5
		break;
4c79b5
4c79b5
		case 'sqlite':
4c79b5
			$prefix_length = 200;
4c79b5
		break;
4c79b5
4c79b5
		case 'firebird':
4c79b5
		case 'oracle':
4c79b5
			$prefix_length = 6;
4c79b5
		break;
4c79b5
	}
4c79b5
4c79b5
	if (strlen($table_prefix) > $prefix_length)
4c79b5
	{
4c79b5
		$error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	// Try and connect ...
4c79b5
	if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
4c79b5
	{
4c79b5
		$db_error = $db->sql_error();
4c79b5
		$error[] = $lang['INST_ERR_DB_CONNECT'] . '
' . (($db_error['message']) ? $db_error['message'] : $lang['INST_ERR_DB_NO_ERROR']);
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		// Likely matches for an existing phpBB installation
4c79b5
		if (!$prefix_may_exist)
4c79b5
		{
4c79b5
			$temp_prefix = strtolower($table_prefix);
4c79b5
			$table_ary = array($temp_prefix . 'attachments', $temp_prefix . 'config', $temp_prefix . 'sessions', $temp_prefix . 'topics', $temp_prefix . 'users');
4c79b5
4c79b5
			$tables = get_tables($db);
4c79b5
			$tables = array_map('strtolower', $tables);
4c79b5
			$table_intersect = array_intersect($tables, $table_ary);
4c79b5
4c79b5
			if (sizeof($table_intersect))
4c79b5
			{
4c79b5
				$error[] = $lang['INST_ERR_PREFIX'];
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// Make sure that the user has selected a sensible DBAL for the DBMS actually installed
4c79b5
		switch ($dbms_details['DRIVER'])
4c79b5
		{
4c79b5
			case 'mysqli':
4c79b5
				if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
4c79b5
				{
4c79b5
					$error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
4c79b5
				}
4c79b5
			break;
4c79b5
4c79b5
			case 'sqlite':
4c79b5
				if (version_compare(sqlite_libversion(), '2.8.2', '<'))
4c79b5
				{
4c79b5
					$error[] = $lang['INST_ERR_DB_NO_SQLITE'];
4c79b5
				}
4c79b5
			break;
4c79b5
4c79b5
			case 'firebird':
4c79b5
				// check the version of FB, use some hackery if we can't get access to the server info
4c79b5
				if ($db->service_handle !== false && function_exists('ibase_server_info'))
4c79b5
				{
4c79b5
					$val = @ibase_server_info($db->service_handle, IBASE_SVC_SERVER_VERSION);
4c79b5
					preg_match('#V([\d.]+)#', $val, $match);
4c79b5
					if ($match[1] < 2)
4c79b5
					{
4c79b5
						$error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
4c79b5
					}
4c79b5
					$db_info = @ibase_db_info($db->service_handle, $dbname, IBASE_STS_HDR_PAGES);
4c79b5
4c79b5
					preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
4c79b5
					$page_size = intval($regs[1]);
4c79b5
					if ($page_size < 8192)
4c79b5
					{
4c79b5
						$error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
4c79b5
					}
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					$sql = "SELECT *
4c79b5
						FROM RDB$FUNCTIONS
4c79b5
						WHERE RDB$SYSTEM_FLAG IS NULL
4c79b5
							AND RDB$FUNCTION_NAME = 'CHAR_LENGTH'";
4c79b5
					$result = $db->sql_query($sql);
4c79b5
					$row = $db->sql_fetchrow($result);
4c79b5
					$db->sql_freeresult($result);
4c79b5
4c79b5
					// if its a UDF, its too old
4c79b5
					if ($row)
4c79b5
					{
4c79b5
						$error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						$sql = "SELECT FIRST 0 char_length('')
4c79b5
							FROM RDB\$DATABASE";
4c79b5
						$result = $db->sql_query($sql);
4c79b5
						if (!$result) // This can only fail if char_length is not defined
4c79b5
						{
4c79b5
							$error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
4c79b5
						}
4c79b5
						$db->sql_freeresult($result);
4c79b5
					}
4c79b5
4c79b5
					// Setup the stuff for our random table
4c79b5
					$char_array = array_merge(range('A', 'Z'), range('0', '9'));
4c79b5
					$char_len = mt_rand(7, 9);
4c79b5
					$char_array_len = sizeof($char_array) - 1;
4c79b5
4c79b5
					$final = '';
4c79b5
4c79b5
					for ($i = 0; $i < $char_len; $i++)
4c79b5
					{
4c79b5
						$final .= $char_array[mt_rand(0, $char_array_len)];
4c79b5
					}
4c79b5
4c79b5
					// Create some random table
4c79b5
					$sql = 'CREATE TABLE ' . $final . " (
4c79b5
						FIELD1 VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE,
4c79b5
						FIELD2 INTEGER DEFAULT 0 NOT NULL);";
4c79b5
					$db->sql_query($sql);
4c79b5
4c79b5
					// Create an index that should fail if the page size is less than 8192
4c79b5
					$sql = 'CREATE INDEX ' . $final . ' ON ' . $final . '(FIELD1, FIELD2);';
4c79b5
					$db->sql_query($sql);
4c79b5
4c79b5
					if (ibase_errmsg() !== false)
4c79b5
					{
4c79b5
						$error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						// Kill the old table
4c79b5
						$db->sql_query('DROP TABLE ' . $final . ';');
4c79b5
					}
4c79b5
					unset($final);
4c79b5
				}
4c79b5
			break;
4c79b5
			
4c79b5
			case 'oracle':
4c79b5
				if ($unicode_check)
4c79b5
				{
4c79b5
					$sql = "SELECT *
4c79b5
						FROM NLS_DATABASE_PARAMETERS
4c79b5
						WHERE PARAMETER = 'NLS_RDBMS_VERSION'
4c79b5
							OR PARAMETER = 'NLS_CHARACTERSET'";
4c79b5
					$result = $db->sql_query($sql);
4c79b5
4c79b5
					while ($row = $db->sql_fetchrow($result))
4c79b5
					{
4c79b5
						$stats[$row['parameter']] = $row['value'];
4c79b5
					}
4c79b5
					$db->sql_freeresult($result);
4c79b5
4c79b5
					if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<') && $stats['NLS_CHARACTERSET'] !== 'UTF8')
4c79b5
					{
4c79b5
						$error[] = $lang['INST_ERR_DB_NO_ORACLE'];
4c79b5
					}
4c79b5
				}
4c79b5
			break;
4c79b5
			
4c79b5
			case 'postgres':
4c79b5
				if ($unicode_check)
4c79b5
				{
4c79b5
					$sql = "SHOW server_encoding;";
4c79b5
					$result = $db->sql_query($sql);
4c79b5
					$row = $db->sql_fetchrow($result);
4c79b5
					$db->sql_freeresult($result);
4c79b5
4c79b5
					if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
4c79b5
					{
4c79b5
						$error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
4c79b5
					}
4c79b5
				}
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
	}
4c79b5
4c79b5
	if ($error_connect && (!isset($error) || !sizeof($error)))
4c79b5
	{
4c79b5
		return true;
4c79b5
	}
4c79b5
	return false;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* remove_remarks will strip the sql comment lines out of an uploaded sql file
4c79b5
*/
4c79b5
function remove_remarks(&$sql)
4c79b5
{
4c79b5
	$sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* split_sql_file will split an uploaded sql file into single sql statements.
4c79b5
* Note: expects trim() to have already been run on $sql.
4c79b5
*/
4c79b5
function split_sql_file($sql, $delimiter)
4c79b5
{
4c79b5
	$sql = str_replace("\r" , '', $sql);
4c79b5
	$data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
4c79b5
4c79b5
	$data = array_map('trim', $data);
4c79b5
4c79b5
	// The empty case
4c79b5
	$end_data = end($data);
4c79b5
4c79b5
	if (empty($end_data))
4c79b5
	{
4c79b5
		unset($data[key($data)]);
4c79b5
	}
4c79b5
4c79b5
	return $data;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* For replacing {L_*} strings with preg_replace_callback
4c79b5
*/
4c79b5
function adjust_language_keys_callback($matches)
4c79b5
{
4c79b5
	if (!empty($matches[1]))
4c79b5
	{
4c79b5
		global $lang, $db;
4c79b5
4c79b5
		return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
?>