Blame Identity/Webenv/App/phpBB/3.0.4/includes/functions_install.php

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