includes/functions_install.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 02:49:04 +0400
changeset 19 6d3ca1857104
parent 0 ceef6d64c21d
permissions -rw-r--r--
RU translate fix
     1 <?php
     2 /**
     3 *
     4 * @package install
     5 * @version $Id$
     6 * @copyright (c) 2006 phpBB Group
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     8 *
     9 */
    10 
    11 /**
    12 * @ignore
    13 */
    14 if (!defined('IN_PHPBB'))
    15 {
    16 	exit;
    17 }
    18 
    19 /**
    20 * Determine if we are able to load a specified PHP module and do so if possible
    21 */
    22 function can_load_dll($dll)
    23 {
    24 	// SQLite2 is a tricky thing, from 5.0.0 it requires PDO; if PDO is not loaded we must state that SQLite is unavailable
    25 	// as the installer doesn't understand that the extension has a prerequisite.
    26 	//
    27 	// On top of this sometimes the SQLite extension is compiled for a different version of PDO
    28 	// by some Linux distributions which causes phpBB to bomb out with a blank page.
    29 	//
    30 	// Net result we'll disable automatic inclusion of SQLite support
    31 	//
    32 	// See: r9618 and #56105
    33 	if ($dll == 'sqlite')
    34 	{
    35 		return false;
    36 	}
    37 	return ((@ini_get('enable_dl') || strtolower(@ini_get('enable_dl')) == 'on') && (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') && function_exists('dl') && @dl($dll . '.' . PHP_SHLIB_SUFFIX)) ? true : false;
    38 }
    39 
    40 /**
    41 * Returns an array of available DBMS with some data, if a DBMS is specified it will only
    42 * return data for that DBMS and will load its extension if necessary.
    43 */
    44 function get_available_dbms($dbms = false, $return_unavailable = false, $only_20x_options = false)
    45 {
    46 	global $lang;
    47 	$available_dbms = array(
    48 		'firebird'	=> array(
    49 			'LABEL'			=> 'FireBird',
    50 			'SCHEMA'		=> 'firebird',
    51 			'MODULE'		=> 'interbase',
    52 			'DELIM'			=> ';;',
    53 			'COMMENTS'		=> 'remove_remarks',
    54 			'DRIVER'		=> 'firebird',
    55 			'AVAILABLE'		=> true,
    56 			'2.0.x'			=> false,
    57 		),
    58 		'mysqli'	=> array(
    59 			'LABEL'			=> 'MySQL with MySQLi Extension',
    60 			'SCHEMA'		=> 'mysql_41',
    61 			'MODULE'		=> 'mysqli',
    62 			'DELIM'			=> ';',
    63 			'COMMENTS'		=> 'remove_remarks',
    64 			'DRIVER'		=> 'mysqli',
    65 			'AVAILABLE'		=> true,
    66 			'2.0.x'			=> true,
    67 		),
    68 		'mysql'		=> array(
    69 			'LABEL'			=> 'MySQL',
    70 			'SCHEMA'		=> 'mysql',
    71 			'MODULE'		=> 'mysql',
    72 			'DELIM'			=> ';',
    73 			'COMMENTS'		=> 'remove_remarks',
    74 			'DRIVER'		=> 'mysql',
    75 			'AVAILABLE'		=> true,
    76 			'2.0.x'			=> true,
    77 		),
    78 		'mssql'		=> array(
    79 			'LABEL'			=> 'MS SQL Server 2000+',
    80 			'SCHEMA'		=> 'mssql',
    81 			'MODULE'		=> 'mssql',
    82 			'DELIM'			=> 'GO',
    83 			'COMMENTS'		=> 'remove_comments',
    84 			'DRIVER'		=> 'mssql',
    85 			'AVAILABLE'		=> true,
    86 			'2.0.x'			=> true,
    87 		),
    88 		'mssql_odbc'=>	array(
    89 			'LABEL'			=> 'MS SQL Server [ ODBC ]',
    90 			'SCHEMA'		=> 'mssql',
    91 			'MODULE'		=> 'odbc',
    92 			'DELIM'			=> 'GO',
    93 			'COMMENTS'		=> 'remove_comments',
    94 			'DRIVER'		=> 'mssql_odbc',
    95 			'AVAILABLE'		=> true,
    96 			'2.0.x'			=> true,
    97 		),
    98 		'mssqlnative'		=> array(
    99 			'LABEL'			=> 'MS SQL Server 2005+ [ Native ]',
   100 			'SCHEMA'		=> 'mssql',
   101 			'MODULE'		=> 'sqlsrv',
   102 			'DELIM'			=> 'GO',
   103 			'COMMENTS'		=> 'remove_comments',
   104 			'DRIVER'		=> 'mssqlnative',
   105 			'AVAILABLE'		=> true,
   106 			'2.0.x'			=> false,
   107 		),			
   108 		'oracle'	=>	array(
   109 			'LABEL'			=> 'Oracle',
   110 			'SCHEMA'		=> 'oracle',
   111 			'MODULE'		=> 'oci8',
   112 			'DELIM'			=> '/',
   113 			'COMMENTS'		=> 'remove_comments',
   114 			'DRIVER'		=> 'oracle',
   115 			'AVAILABLE'		=> true,
   116 			'2.0.x'			=> false,
   117 		),
   118 		'postgres' => array(
   119 			'LABEL'			=> 'PostgreSQL 7.x/8.x',
   120 			'SCHEMA'		=> 'postgres',
   121 			'MODULE'		=> 'pgsql',
   122 			'DELIM'			=> ';',
   123 			'COMMENTS'		=> 'remove_comments',
   124 			'DRIVER'		=> 'postgres',
   125 			'AVAILABLE'		=> true,
   126 			'2.0.x'			=> true,
   127 		),
   128 		'sqlite'		=> array(
   129 			'LABEL'			=> 'SQLite',
   130 			'SCHEMA'		=> 'sqlite',
   131 			'MODULE'		=> 'sqlite',
   132 			'DELIM'			=> ';',
   133 			'COMMENTS'		=> 'remove_remarks',
   134 			'DRIVER'		=> 'sqlite',
   135 			'AVAILABLE'		=> true,
   136 			'2.0.x'			=> false,
   137 		),
   138 	);
   139 
   140 	if ($dbms)
   141 	{
   142 		if (isset($available_dbms[$dbms]))
   143 		{
   144 			$available_dbms = array($dbms => $available_dbms[$dbms]);
   145 		}
   146 		else
   147 		{
   148 			return array();
   149 		}
   150 	}
   151 
   152 	// now perform some checks whether they are really available
   153 	foreach ($available_dbms as $db_name => $db_ary)
   154 	{
   155 		if ($only_20x_options && !$db_ary['2.0.x'])
   156 		{
   157 			if ($return_unavailable)
   158 			{
   159 				$available_dbms[$db_name]['AVAILABLE'] = false;
   160 			}
   161 			else
   162 			{
   163 				unset($available_dbms[$db_name]);
   164 			}
   165 			continue;
   166 		}
   167 
   168 		$dll = $db_ary['MODULE'];
   169 
   170 		if (!@extension_loaded($dll))
   171 		{
   172 			if (!can_load_dll($dll))
   173 			{
   174 				if ($return_unavailable)
   175 				{
   176 					$available_dbms[$db_name]['AVAILABLE'] = false;
   177 				}
   178 				else
   179 				{
   180 					unset($available_dbms[$db_name]);
   181 				}
   182 				continue;
   183 			}
   184 		}
   185 		$any_db_support = true;
   186 	}
   187 
   188 	if ($return_unavailable)
   189 	{
   190 		$available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
   191 	}
   192 	return $available_dbms;
   193 }
   194 
   195 /**
   196 * Generate the drop down of available database options
   197 */
   198 function dbms_select($default = '', $only_20x_options = false)
   199 {
   200 	global $lang;
   201 
   202 	$available_dbms = get_available_dbms(false, false, $only_20x_options);
   203 	$dbms_options = '';
   204 	foreach ($available_dbms as $dbms_name => $details)
   205 	{
   206 		$selected = ($dbms_name == $default) ? ' selected="selected"' : '';
   207 		$dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
   208 	}
   209 	return $dbms_options;
   210 }
   211 
   212 /**
   213 * Get tables of a database
   214 *
   215 * @deprecated
   216 */
   217 function get_tables(&$db)
   218 {
   219 	if (!class_exists('phpbb_db_tools'))
   220 	{
   221 		global $phpbb_root_path, $phpEx;
   222 		require($phpbb_root_path . 'includes/db/db_tools.' . $phpEx);
   223 	}
   224 
   225 	$db_tools = new phpbb_db_tools($db);
   226 
   227 	return $db_tools->sql_list_tables();
   228 }
   229 
   230 /**
   231 * Used to test whether we are able to connect to the database the user has specified
   232 * and identify any problems (eg there are already tables with the names we want to use
   233 * @param	array	$dbms should be of the format of an element of the array returned by {@link get_available_dbms get_available_dbms()}
   234 *					necessary extensions should be loaded already
   235 */
   236 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)
   237 {
   238 	global $phpbb_root_path, $phpEx, $config, $lang;
   239 
   240 	$dbms = $dbms_details['DRIVER'];
   241 
   242 	if ($load_dbal)
   243 	{
   244 		// Include the DB layer
   245 		include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
   246 	}
   247 
   248 	// Instantiate it and set return on error true
   249 	$sql_db = 'dbal_' . $dbms;
   250 	$db = new $sql_db();
   251 	$db->sql_return_on_error(true);
   252 
   253 	// Check that we actually have a database name before going any further.....
   254 	if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
   255 	{
   256 		$error[] = $lang['INST_ERR_DB_NO_NAME'];
   257 		return false;
   258 	}
   259 
   260 	// Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea
   261 	if ($dbms_details['DRIVER'] == 'sqlite' && stripos(phpbb_realpath($dbhost), phpbb_realpath('../')) === 0)
   262 	{
   263 		$error[] = $lang['INST_ERR_DB_FORUM_PATH'];
   264 		return false;
   265 	}
   266 
   267 	// Check the prefix length to ensure that index names are not too long and does not contain invalid characters
   268 	switch ($dbms_details['DRIVER'])
   269 	{
   270 		case 'mysql':
   271 		case 'mysqli':
   272 			if (strspn($table_prefix, '-./\\') !== 0)
   273 			{
   274 				$error[] = $lang['INST_ERR_PREFIX_INVALID'];
   275 				return false;
   276 			}
   277 
   278 		// no break;
   279 
   280 		case 'postgres':
   281 			$prefix_length = 36;
   282 		break;
   283 
   284 		case 'mssql':
   285 		case 'mssql_odbc':
   286 		case 'mssqlnative':
   287 			$prefix_length = 90;
   288 		break;
   289 
   290 		case 'sqlite':
   291 			$prefix_length = 200;
   292 		break;
   293 
   294 		case 'firebird':
   295 		case 'oracle':
   296 			$prefix_length = 6;
   297 		break;
   298 	}
   299 
   300 	if (strlen($table_prefix) > $prefix_length)
   301 	{
   302 		$error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
   303 		return false;
   304 	}
   305 
   306 	// Try and connect ...
   307 	if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
   308 	{
   309 		$db_error = $db->sql_error();
   310 		$error[] = $lang['INST_ERR_DB_CONNECT'] . '<br />' . (($db_error['message']) ? $db_error['message'] : $lang['INST_ERR_DB_NO_ERROR']);
   311 	}
   312 	else
   313 	{
   314 		// Likely matches for an existing phpBB installation
   315 		if (!$prefix_may_exist)
   316 		{
   317 			$temp_prefix = strtolower($table_prefix);
   318 			$table_ary = array($temp_prefix . 'attachments', $temp_prefix . 'config', $temp_prefix . 'sessions', $temp_prefix . 'topics', $temp_prefix . 'users');
   319 
   320 			$tables = get_tables($db);
   321 			$tables = array_map('strtolower', $tables);
   322 			$table_intersect = array_intersect($tables, $table_ary);
   323 
   324 			if (sizeof($table_intersect))
   325 			{
   326 				$error[] = $lang['INST_ERR_PREFIX'];
   327 			}
   328 		}
   329 
   330 		// Make sure that the user has selected a sensible DBAL for the DBMS actually installed
   331 		switch ($dbms_details['DRIVER'])
   332 		{
   333 			case 'mysqli':
   334 				if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
   335 				{
   336 					$error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
   337 				}
   338 			break;
   339 
   340 			case 'sqlite':
   341 				if (version_compare(sqlite_libversion(), '2.8.2', '<'))
   342 				{
   343 					$error[] = $lang['INST_ERR_DB_NO_SQLITE'];
   344 				}
   345 			break;
   346 
   347 			case 'firebird':
   348 				// check the version of FB, use some hackery if we can't get access to the server info
   349 				if ($db->service_handle !== false && function_exists('ibase_server_info'))
   350 				{
   351 					$val = @ibase_server_info($db->service_handle, IBASE_SVC_SERVER_VERSION);
   352 					preg_match('#V([\d.]+)#', $val, $match);
   353 					if ($match[1] < 2)
   354 					{
   355 						$error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
   356 					}
   357 					$db_info = @ibase_db_info($db->service_handle, $dbname, IBASE_STS_HDR_PAGES);
   358 
   359 					preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
   360 					$page_size = intval($regs[1]);
   361 					if ($page_size < 8192)
   362 					{
   363 						$error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
   364 					}
   365 				}
   366 				else
   367 				{
   368 					$sql = "SELECT *
   369 						FROM RDB$FUNCTIONS
   370 						WHERE RDB$SYSTEM_FLAG IS NULL
   371 							AND RDB$FUNCTION_NAME = 'CHAR_LENGTH'";
   372 					$result = $db->sql_query($sql);
   373 					$row = $db->sql_fetchrow($result);
   374 					$db->sql_freeresult($result);
   375 
   376 					// if its a UDF, its too old
   377 					if ($row)
   378 					{
   379 						$error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
   380 					}
   381 					else
   382 					{
   383 						$sql = 'SELECT 1 FROM RDB$DATABASE
   384 							WHERE BIN_AND(10, 1) = 0';
   385 						$result = $db->sql_query($sql);
   386 						if (!$result) // This can only fail if BIN_AND is not defined
   387 						{
   388 							$error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
   389 						}
   390 						$db->sql_freeresult($result);
   391 					}
   392 
   393 					// Setup the stuff for our random table
   394 					$char_array = array_merge(range('A', 'Z'), range('0', '9'));
   395 					$char_len = mt_rand(7, 9);
   396 					$char_array_len = sizeof($char_array) - 1;
   397 
   398 					$final = '';
   399 
   400 					for ($i = 0; $i < $char_len; $i++)
   401 					{
   402 						$final .= $char_array[mt_rand(0, $char_array_len)];
   403 					}
   404 
   405 					// Create some random table
   406 					$sql = 'CREATE TABLE ' . $final . " (
   407 						FIELD1 VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE,
   408 						FIELD2 INTEGER DEFAULT 0 NOT NULL);";
   409 					$db->sql_query($sql);
   410 
   411 					// Create an index that should fail if the page size is less than 8192
   412 					$sql = 'CREATE INDEX ' . $final . ' ON ' . $final . '(FIELD1, FIELD2);';
   413 					$db->sql_query($sql);
   414 
   415 					if (ibase_errmsg() !== false)
   416 					{
   417 						$error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
   418 					}
   419 					else
   420 					{
   421 						// Kill the old table
   422 						$db->sql_query('DROP TABLE ' . $final . ';');
   423 					}
   424 					unset($final);
   425 				}
   426 			break;
   427 
   428 			case 'oracle':
   429 				if ($unicode_check)
   430 				{
   431 					$sql = "SELECT *
   432 						FROM NLS_DATABASE_PARAMETERS
   433 						WHERE PARAMETER = 'NLS_RDBMS_VERSION'
   434 							OR PARAMETER = 'NLS_CHARACTERSET'";
   435 					$result = $db->sql_query($sql);
   436 
   437 					while ($row = $db->sql_fetchrow($result))
   438 					{
   439 						$stats[$row['parameter']] = $row['value'];
   440 					}
   441 					$db->sql_freeresult($result);
   442 
   443 					if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<') && $stats['NLS_CHARACTERSET'] !== 'UTF8')
   444 					{
   445 						$error[] = $lang['INST_ERR_DB_NO_ORACLE'];
   446 					}
   447 				}
   448 			break;
   449 
   450 			case 'postgres':
   451 				if ($unicode_check)
   452 				{
   453 					$sql = "SHOW server_encoding;";
   454 					$result = $db->sql_query($sql);
   455 					$row = $db->sql_fetchrow($result);
   456 					$db->sql_freeresult($result);
   457 
   458 					if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
   459 					{
   460 						$error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
   461 					}
   462 				}
   463 			break;
   464 		}
   465 
   466 	}
   467 
   468 	if ($error_connect && (!isset($error) || !sizeof($error)))
   469 	{
   470 		return true;
   471 	}
   472 	return false;
   473 }
   474 
   475 /**
   476 * remove_remarks will strip the sql comment lines out of an uploaded sql file
   477 */
   478 function remove_remarks(&$sql)
   479 {
   480 	$sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
   481 }
   482 
   483 /**
   484 * split_sql_file will split an uploaded sql file into single sql statements.
   485 * Note: expects trim() to have already been run on $sql.
   486 */
   487 function split_sql_file($sql, $delimiter)
   488 {
   489 	$sql = str_replace("\r" , '', $sql);
   490 	$data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
   491 
   492 	$data = array_map('trim', $data);
   493 
   494 	// The empty case
   495 	$end_data = end($data);
   496 
   497 	if (empty($end_data))
   498 	{
   499 		unset($data[key($data)]);
   500 	}
   501 
   502 	return $data;
   503 }
   504 
   505 /**
   506 * For replacing {L_*} strings with preg_replace_callback
   507 */
   508 function adjust_language_keys_callback($matches)
   509 {
   510 	if (!empty($matches[1]))
   511 	{
   512 		global $lang, $db;
   513 
   514 		return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);
   515 	}
   516 }
   517 
   518 ?>