1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/functions_install.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,518 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package install
1.8 +* @version $Id$
1.9 +* @copyright (c) 2006 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +/**
1.23 +* Determine if we are able to load a specified PHP module and do so if possible
1.24 +*/
1.25 +function can_load_dll($dll)
1.26 +{
1.27 + // SQLite2 is a tricky thing, from 5.0.0 it requires PDO; if PDO is not loaded we must state that SQLite is unavailable
1.28 + // as the installer doesn't understand that the extension has a prerequisite.
1.29 + //
1.30 + // On top of this sometimes the SQLite extension is compiled for a different version of PDO
1.31 + // by some Linux distributions which causes phpBB to bomb out with a blank page.
1.32 + //
1.33 + // Net result we'll disable automatic inclusion of SQLite support
1.34 + //
1.35 + // See: r9618 and #56105
1.36 + if ($dll == 'sqlite')
1.37 + {
1.38 + return false;
1.39 + }
1.40 + 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;
1.41 +}
1.42 +
1.43 +/**
1.44 +* Returns an array of available DBMS with some data, if a DBMS is specified it will only
1.45 +* return data for that DBMS and will load its extension if necessary.
1.46 +*/
1.47 +function get_available_dbms($dbms = false, $return_unavailable = false, $only_20x_options = false)
1.48 +{
1.49 + global $lang;
1.50 + $available_dbms = array(
1.51 + 'firebird' => array(
1.52 + 'LABEL' => 'FireBird',
1.53 + 'SCHEMA' => 'firebird',
1.54 + 'MODULE' => 'interbase',
1.55 + 'DELIM' => ';;',
1.56 + 'COMMENTS' => 'remove_remarks',
1.57 + 'DRIVER' => 'firebird',
1.58 + 'AVAILABLE' => true,
1.59 + '2.0.x' => false,
1.60 + ),
1.61 + 'mysqli' => array(
1.62 + 'LABEL' => 'MySQL with MySQLi Extension',
1.63 + 'SCHEMA' => 'mysql_41',
1.64 + 'MODULE' => 'mysqli',
1.65 + 'DELIM' => ';',
1.66 + 'COMMENTS' => 'remove_remarks',
1.67 + 'DRIVER' => 'mysqli',
1.68 + 'AVAILABLE' => true,
1.69 + '2.0.x' => true,
1.70 + ),
1.71 + 'mysql' => array(
1.72 + 'LABEL' => 'MySQL',
1.73 + 'SCHEMA' => 'mysql',
1.74 + 'MODULE' => 'mysql',
1.75 + 'DELIM' => ';',
1.76 + 'COMMENTS' => 'remove_remarks',
1.77 + 'DRIVER' => 'mysql',
1.78 + 'AVAILABLE' => true,
1.79 + '2.0.x' => true,
1.80 + ),
1.81 + 'mssql' => array(
1.82 + 'LABEL' => 'MS SQL Server 2000+',
1.83 + 'SCHEMA' => 'mssql',
1.84 + 'MODULE' => 'mssql',
1.85 + 'DELIM' => 'GO',
1.86 + 'COMMENTS' => 'remove_comments',
1.87 + 'DRIVER' => 'mssql',
1.88 + 'AVAILABLE' => true,
1.89 + '2.0.x' => true,
1.90 + ),
1.91 + 'mssql_odbc'=> array(
1.92 + 'LABEL' => 'MS SQL Server [ ODBC ]',
1.93 + 'SCHEMA' => 'mssql',
1.94 + 'MODULE' => 'odbc',
1.95 + 'DELIM' => 'GO',
1.96 + 'COMMENTS' => 'remove_comments',
1.97 + 'DRIVER' => 'mssql_odbc',
1.98 + 'AVAILABLE' => true,
1.99 + '2.0.x' => true,
1.100 + ),
1.101 + 'mssqlnative' => array(
1.102 + 'LABEL' => 'MS SQL Server 2005+ [ Native ]',
1.103 + 'SCHEMA' => 'mssql',
1.104 + 'MODULE' => 'sqlsrv',
1.105 + 'DELIM' => 'GO',
1.106 + 'COMMENTS' => 'remove_comments',
1.107 + 'DRIVER' => 'mssqlnative',
1.108 + 'AVAILABLE' => true,
1.109 + '2.0.x' => false,
1.110 + ),
1.111 + 'oracle' => array(
1.112 + 'LABEL' => 'Oracle',
1.113 + 'SCHEMA' => 'oracle',
1.114 + 'MODULE' => 'oci8',
1.115 + 'DELIM' => '/',
1.116 + 'COMMENTS' => 'remove_comments',
1.117 + 'DRIVER' => 'oracle',
1.118 + 'AVAILABLE' => true,
1.119 + '2.0.x' => false,
1.120 + ),
1.121 + 'postgres' => array(
1.122 + 'LABEL' => 'PostgreSQL 7.x/8.x',
1.123 + 'SCHEMA' => 'postgres',
1.124 + 'MODULE' => 'pgsql',
1.125 + 'DELIM' => ';',
1.126 + 'COMMENTS' => 'remove_comments',
1.127 + 'DRIVER' => 'postgres',
1.128 + 'AVAILABLE' => true,
1.129 + '2.0.x' => true,
1.130 + ),
1.131 + 'sqlite' => array(
1.132 + 'LABEL' => 'SQLite',
1.133 + 'SCHEMA' => 'sqlite',
1.134 + 'MODULE' => 'sqlite',
1.135 + 'DELIM' => ';',
1.136 + 'COMMENTS' => 'remove_remarks',
1.137 + 'DRIVER' => 'sqlite',
1.138 + 'AVAILABLE' => true,
1.139 + '2.0.x' => false,
1.140 + ),
1.141 + );
1.142 +
1.143 + if ($dbms)
1.144 + {
1.145 + if (isset($available_dbms[$dbms]))
1.146 + {
1.147 + $available_dbms = array($dbms => $available_dbms[$dbms]);
1.148 + }
1.149 + else
1.150 + {
1.151 + return array();
1.152 + }
1.153 + }
1.154 +
1.155 + // now perform some checks whether they are really available
1.156 + foreach ($available_dbms as $db_name => $db_ary)
1.157 + {
1.158 + if ($only_20x_options && !$db_ary['2.0.x'])
1.159 + {
1.160 + if ($return_unavailable)
1.161 + {
1.162 + $available_dbms[$db_name]['AVAILABLE'] = false;
1.163 + }
1.164 + else
1.165 + {
1.166 + unset($available_dbms[$db_name]);
1.167 + }
1.168 + continue;
1.169 + }
1.170 +
1.171 + $dll = $db_ary['MODULE'];
1.172 +
1.173 + if (!@extension_loaded($dll))
1.174 + {
1.175 + if (!can_load_dll($dll))
1.176 + {
1.177 + if ($return_unavailable)
1.178 + {
1.179 + $available_dbms[$db_name]['AVAILABLE'] = false;
1.180 + }
1.181 + else
1.182 + {
1.183 + unset($available_dbms[$db_name]);
1.184 + }
1.185 + continue;
1.186 + }
1.187 + }
1.188 + $any_db_support = true;
1.189 + }
1.190 +
1.191 + if ($return_unavailable)
1.192 + {
1.193 + $available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
1.194 + }
1.195 + return $available_dbms;
1.196 +}
1.197 +
1.198 +/**
1.199 +* Generate the drop down of available database options
1.200 +*/
1.201 +function dbms_select($default = '', $only_20x_options = false)
1.202 +{
1.203 + global $lang;
1.204 +
1.205 + $available_dbms = get_available_dbms(false, false, $only_20x_options);
1.206 + $dbms_options = '';
1.207 + foreach ($available_dbms as $dbms_name => $details)
1.208 + {
1.209 + $selected = ($dbms_name == $default) ? ' selected="selected"' : '';
1.210 + $dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
1.211 + }
1.212 + return $dbms_options;
1.213 +}
1.214 +
1.215 +/**
1.216 +* Get tables of a database
1.217 +*
1.218 +* @deprecated
1.219 +*/
1.220 +function get_tables(&$db)
1.221 +{
1.222 + if (!class_exists('phpbb_db_tools'))
1.223 + {
1.224 + global $phpbb_root_path, $phpEx;
1.225 + require($phpbb_root_path . 'includes/db/db_tools.' . $phpEx);
1.226 + }
1.227 +
1.228 + $db_tools = new phpbb_db_tools($db);
1.229 +
1.230 + return $db_tools->sql_list_tables();
1.231 +}
1.232 +
1.233 +/**
1.234 +* Used to test whether we are able to connect to the database the user has specified
1.235 +* and identify any problems (eg there are already tables with the names we want to use
1.236 +* @param array $dbms should be of the format of an element of the array returned by {@link get_available_dbms get_available_dbms()}
1.237 +* necessary extensions should be loaded already
1.238 +*/
1.239 +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)
1.240 +{
1.241 + global $phpbb_root_path, $phpEx, $config, $lang;
1.242 +
1.243 + $dbms = $dbms_details['DRIVER'];
1.244 +
1.245 + if ($load_dbal)
1.246 + {
1.247 + // Include the DB layer
1.248 + include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
1.249 + }
1.250 +
1.251 + // Instantiate it and set return on error true
1.252 + $sql_db = 'dbal_' . $dbms;
1.253 + $db = new $sql_db();
1.254 + $db->sql_return_on_error(true);
1.255 +
1.256 + // Check that we actually have a database name before going any further.....
1.257 + if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
1.258 + {
1.259 + $error[] = $lang['INST_ERR_DB_NO_NAME'];
1.260 + return false;
1.261 + }
1.262 +
1.263 + // Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea
1.264 + if ($dbms_details['DRIVER'] == 'sqlite' && stripos(phpbb_realpath($dbhost), phpbb_realpath('../')) === 0)
1.265 + {
1.266 + $error[] = $lang['INST_ERR_DB_FORUM_PATH'];
1.267 + return false;
1.268 + }
1.269 +
1.270 + // Check the prefix length to ensure that index names are not too long and does not contain invalid characters
1.271 + switch ($dbms_details['DRIVER'])
1.272 + {
1.273 + case 'mysql':
1.274 + case 'mysqli':
1.275 + if (strspn($table_prefix, '-./\\') !== 0)
1.276 + {
1.277 + $error[] = $lang['INST_ERR_PREFIX_INVALID'];
1.278 + return false;
1.279 + }
1.280 +
1.281 + // no break;
1.282 +
1.283 + case 'postgres':
1.284 + $prefix_length = 36;
1.285 + break;
1.286 +
1.287 + case 'mssql':
1.288 + case 'mssql_odbc':
1.289 + case 'mssqlnative':
1.290 + $prefix_length = 90;
1.291 + break;
1.292 +
1.293 + case 'sqlite':
1.294 + $prefix_length = 200;
1.295 + break;
1.296 +
1.297 + case 'firebird':
1.298 + case 'oracle':
1.299 + $prefix_length = 6;
1.300 + break;
1.301 + }
1.302 +
1.303 + if (strlen($table_prefix) > $prefix_length)
1.304 + {
1.305 + $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
1.306 + return false;
1.307 + }
1.308 +
1.309 + // Try and connect ...
1.310 + if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
1.311 + {
1.312 + $db_error = $db->sql_error();
1.313 + $error[] = $lang['INST_ERR_DB_CONNECT'] . '<br />' . (($db_error['message']) ? $db_error['message'] : $lang['INST_ERR_DB_NO_ERROR']);
1.314 + }
1.315 + else
1.316 + {
1.317 + // Likely matches for an existing phpBB installation
1.318 + if (!$prefix_may_exist)
1.319 + {
1.320 + $temp_prefix = strtolower($table_prefix);
1.321 + $table_ary = array($temp_prefix . 'attachments', $temp_prefix . 'config', $temp_prefix . 'sessions', $temp_prefix . 'topics', $temp_prefix . 'users');
1.322 +
1.323 + $tables = get_tables($db);
1.324 + $tables = array_map('strtolower', $tables);
1.325 + $table_intersect = array_intersect($tables, $table_ary);
1.326 +
1.327 + if (sizeof($table_intersect))
1.328 + {
1.329 + $error[] = $lang['INST_ERR_PREFIX'];
1.330 + }
1.331 + }
1.332 +
1.333 + // Make sure that the user has selected a sensible DBAL for the DBMS actually installed
1.334 + switch ($dbms_details['DRIVER'])
1.335 + {
1.336 + case 'mysqli':
1.337 + if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
1.338 + {
1.339 + $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
1.340 + }
1.341 + break;
1.342 +
1.343 + case 'sqlite':
1.344 + if (version_compare(sqlite_libversion(), '2.8.2', '<'))
1.345 + {
1.346 + $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
1.347 + }
1.348 + break;
1.349 +
1.350 + case 'firebird':
1.351 + // check the version of FB, use some hackery if we can't get access to the server info
1.352 + if ($db->service_handle !== false && function_exists('ibase_server_info'))
1.353 + {
1.354 + $val = @ibase_server_info($db->service_handle, IBASE_SVC_SERVER_VERSION);
1.355 + preg_match('#V([\d.]+)#', $val, $match);
1.356 + if ($match[1] < 2)
1.357 + {
1.358 + $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
1.359 + }
1.360 + $db_info = @ibase_db_info($db->service_handle, $dbname, IBASE_STS_HDR_PAGES);
1.361 +
1.362 + preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
1.363 + $page_size = intval($regs[1]);
1.364 + if ($page_size < 8192)
1.365 + {
1.366 + $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
1.367 + }
1.368 + }
1.369 + else
1.370 + {
1.371 + $sql = "SELECT *
1.372 + FROM RDB$FUNCTIONS
1.373 + WHERE RDB$SYSTEM_FLAG IS NULL
1.374 + AND RDB$FUNCTION_NAME = 'CHAR_LENGTH'";
1.375 + $result = $db->sql_query($sql);
1.376 + $row = $db->sql_fetchrow($result);
1.377 + $db->sql_freeresult($result);
1.378 +
1.379 + // if its a UDF, its too old
1.380 + if ($row)
1.381 + {
1.382 + $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
1.383 + }
1.384 + else
1.385 + {
1.386 + $sql = 'SELECT 1 FROM RDB$DATABASE
1.387 + WHERE BIN_AND(10, 1) = 0';
1.388 + $result = $db->sql_query($sql);
1.389 + if (!$result) // This can only fail if BIN_AND is not defined
1.390 + {
1.391 + $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
1.392 + }
1.393 + $db->sql_freeresult($result);
1.394 + }
1.395 +
1.396 + // Setup the stuff for our random table
1.397 + $char_array = array_merge(range('A', 'Z'), range('0', '9'));
1.398 + $char_len = mt_rand(7, 9);
1.399 + $char_array_len = sizeof($char_array) - 1;
1.400 +
1.401 + $final = '';
1.402 +
1.403 + for ($i = 0; $i < $char_len; $i++)
1.404 + {
1.405 + $final .= $char_array[mt_rand(0, $char_array_len)];
1.406 + }
1.407 +
1.408 + // Create some random table
1.409 + $sql = 'CREATE TABLE ' . $final . " (
1.410 + FIELD1 VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE,
1.411 + FIELD2 INTEGER DEFAULT 0 NOT NULL);";
1.412 + $db->sql_query($sql);
1.413 +
1.414 + // Create an index that should fail if the page size is less than 8192
1.415 + $sql = 'CREATE INDEX ' . $final . ' ON ' . $final . '(FIELD1, FIELD2);';
1.416 + $db->sql_query($sql);
1.417 +
1.418 + if (ibase_errmsg() !== false)
1.419 + {
1.420 + $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
1.421 + }
1.422 + else
1.423 + {
1.424 + // Kill the old table
1.425 + $db->sql_query('DROP TABLE ' . $final . ';');
1.426 + }
1.427 + unset($final);
1.428 + }
1.429 + break;
1.430 +
1.431 + case 'oracle':
1.432 + if ($unicode_check)
1.433 + {
1.434 + $sql = "SELECT *
1.435 + FROM NLS_DATABASE_PARAMETERS
1.436 + WHERE PARAMETER = 'NLS_RDBMS_VERSION'
1.437 + OR PARAMETER = 'NLS_CHARACTERSET'";
1.438 + $result = $db->sql_query($sql);
1.439 +
1.440 + while ($row = $db->sql_fetchrow($result))
1.441 + {
1.442 + $stats[$row['parameter']] = $row['value'];
1.443 + }
1.444 + $db->sql_freeresult($result);
1.445 +
1.446 + if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<') && $stats['NLS_CHARACTERSET'] !== 'UTF8')
1.447 + {
1.448 + $error[] = $lang['INST_ERR_DB_NO_ORACLE'];
1.449 + }
1.450 + }
1.451 + break;
1.452 +
1.453 + case 'postgres':
1.454 + if ($unicode_check)
1.455 + {
1.456 + $sql = "SHOW server_encoding;";
1.457 + $result = $db->sql_query($sql);
1.458 + $row = $db->sql_fetchrow($result);
1.459 + $db->sql_freeresult($result);
1.460 +
1.461 + if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
1.462 + {
1.463 + $error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
1.464 + }
1.465 + }
1.466 + break;
1.467 + }
1.468 +
1.469 + }
1.470 +
1.471 + if ($error_connect && (!isset($error) || !sizeof($error)))
1.472 + {
1.473 + return true;
1.474 + }
1.475 + return false;
1.476 +}
1.477 +
1.478 +/**
1.479 +* remove_remarks will strip the sql comment lines out of an uploaded sql file
1.480 +*/
1.481 +function remove_remarks(&$sql)
1.482 +{
1.483 + $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
1.484 +}
1.485 +
1.486 +/**
1.487 +* split_sql_file will split an uploaded sql file into single sql statements.
1.488 +* Note: expects trim() to have already been run on $sql.
1.489 +*/
1.490 +function split_sql_file($sql, $delimiter)
1.491 +{
1.492 + $sql = str_replace("\r" , '', $sql);
1.493 + $data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
1.494 +
1.495 + $data = array_map('trim', $data);
1.496 +
1.497 + // The empty case
1.498 + $end_data = end($data);
1.499 +
1.500 + if (empty($end_data))
1.501 + {
1.502 + unset($data[key($data)]);
1.503 + }
1.504 +
1.505 + return $data;
1.506 +}
1.507 +
1.508 +/**
1.509 +* For replacing {L_*} strings with preg_replace_callback
1.510 +*/
1.511 +function adjust_language_keys_callback($matches)
1.512 +{
1.513 + if (!empty($matches[1]))
1.514 + {
1.515 + global $lang, $db;
1.516 +
1.517 + return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);
1.518 + }
1.519 +}
1.520 +
1.521 +?>
1.522 \ No newline at end of file