6 * @copyright (c) 2006 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * Determine if we are able to load a specified PHP module and do so if possible
14 function can_load_dll($dll)
16 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;
20 * Returns an array of available DBMS with some data, if a DBMS is specified it will only
21 * return data for that DBMS and will load its extension if necessary.
23 function get_available_dbms($dbms = false, $return_unavailable = false, $only_20x_options = false)
26 $available_dbms = array(
28 'LABEL' => 'FireBird',
29 'SCHEMA' => 'firebird',
30 'MODULE' => 'interbase',
32 'COMMENTS' => 'remove_remarks',
33 'DRIVER' => 'firebird',
38 'LABEL' => 'MySQL with MySQLi Extension',
39 'SCHEMA' => 'mysql_41',
42 'COMMENTS' => 'remove_remarks',
52 'COMMENTS' => 'remove_remarks',
58 'LABEL' => 'MS SQL Server 2000+',
62 'COMMENTS' => 'remove_comments',
68 'LABEL' => 'MS SQL Server [ ODBC ]',
72 'COMMENTS' => 'remove_comments',
73 'DRIVER' => 'mssql_odbc',
82 'COMMENTS' => 'remove_comments',
88 'LABEL' => 'PostgreSQL 7.x/8.x',
89 'SCHEMA' => 'postgres',
92 'COMMENTS' => 'remove_comments',
93 'DRIVER' => 'postgres',
100 'MODULE' => 'sqlite',
102 'COMMENTS' => 'remove_remarks',
103 'DRIVER' => 'sqlite',
111 if (isset($available_dbms[$dbms]))
113 $available_dbms = array($dbms => $available_dbms[$dbms]);
121 // now perform some checks whether they are really available
122 foreach ($available_dbms as $db_name => $db_ary)
124 if ($only_20x_options && !$db_ary['2.0.x'])
126 if ($return_unavailable)
128 $available_dbms[$db_name]['AVAILABLE'] = false;
132 unset($available_dbms[$db_name]);
137 $dll = $db_ary['MODULE'];
139 if (!@extension_loaded
($dll))
141 if (!can_load_dll($dll))
143 if ($return_unavailable)
145 $available_dbms[$db_name]['AVAILABLE'] = false;
149 unset($available_dbms[$db_name]);
154 $any_db_support = true;
157 if ($return_unavailable)
159 $available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
161 return $available_dbms;
165 * Generate the drop down of available database options
167 function dbms_select($default = '', $only_20x_options = false)
171 $available_dbms = get_available_dbms(false, false, $only_20x_options);
173 foreach ($available_dbms as $dbms_name => $details)
175 $selected = ($dbms_name == $default) ?
' selected="selected"' : '';
176 $dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
178 return $dbms_options;
182 * Get tables of a database
184 function get_tables($db)
186 switch ($db->sql_layer
)
191 $sql = 'SHOW TABLES';
197 WHERE type = "table"';
208 $sql = 'SELECT relname
209 FROM pg_stat_user_tables';
213 $sql = 'SELECT rdb$relation_name
215 WHERE rdb$view_source is null
216 AND rdb$system_flag = 0';
220 $sql = 'SELECT table_name
225 $result = $db->sql_query($sql);
229 while ($row = $db->sql_fetchrow($result))
231 $tables[] = current($row);
234 $db->sql_freeresult($result);
240 * Used to test whether we are able to connect to the database the user has specified
241 * and identify any problems (eg there are already tables with the names we want to use
242 * @param array $dbms should be of the format of an element of the array returned by {@link get_available_dbms get_available_dbms()}
243 * necessary extensions should be loaded already
245 function connect_check_db($error_connect, &$error, $dbms, $table_prefix, $dbhost, $dbuser, $dbpasswd, $dbname, $dbport, $prefix_may_exist = false, $load_dbal = true, $unicode_check = true)
247 global $phpbb_root_path, $phpEx, $config, $lang;
251 // Include the DB layer
252 include($phpbb_root_path . 'includes/db/' . $dbms['DRIVER'] . '.' . $phpEx);
255 // Instantiate it and set return on error true
256 $sql_db = 'dbal_' . $dbms['DRIVER'];
258 $db->sql_return_on_error(true);
260 // Check that we actually have a database name before going any further.....
261 if ($dbms['DRIVER'] != 'sqlite' && $dbname === '')
263 $error[] = $lang['INST_ERR_DB_NO_NAME'];
267 // Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea
268 if ($dbms['DRIVER'] == 'sqlite' && stripos(phpbb_realpath($dbhost), phpbb_realpath('../')) === 0)
270 $error[] = $lang['INST_ERR_DB_FORUM_PATH'];
274 // Check the prefix length to ensure that index names are not too long and does not contain invalid characters
275 switch ($dbms['DRIVER'])
279 if (strpos($table_prefix, '-') !== false ||
strpos($table_prefix, '.') !== false)
281 $error[] = $lang['INST_ERR_PREFIX_INVALID'];
297 $prefix_length = 200;
306 if (strlen($table_prefix) > $prefix_length)
308 $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
312 // Try and connect ...
313 if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
315 $db_error = $db->sql_error();
316 $error[] = $lang['INST_ERR_DB_CONNECT'] . '<br />' . (($db_error['message']) ?
$db_error['message'] : $lang['INST_ERR_DB_NO_ERROR']);
320 // Likely matches for an existing phpBB installation
321 if (!$prefix_may_exist)
323 $temp_prefix = strtolower($table_prefix);
324 $table_ary = array($temp_prefix . 'attachments', $temp_prefix . 'config', $temp_prefix . 'sessions', $temp_prefix . 'topics', $temp_prefix . 'users');
326 $tables = get_tables($db);
327 $table_intersect = array_intersect($tables, $table_ary);
329 if (sizeof($table_intersect))
331 $error[] = $lang['INST_ERR_PREFIX'];
335 // Make sure that the user has selected a sensible DBAL for the DBMS actually installed
336 switch ($dbms['DRIVER'])
339 if (version_compare(mysqli_get_server_info($db->db_connect_id
), '4.1.3', '<'))
341 $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
346 if (version_compare(sqlite_libversion(), '2.8.2', '<'))
348 $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
353 // check the version of FB, use some hackery if we can't get access to the server info
354 if ($db->service_handle
!== false && function_exists('ibase_server_info'))
356 $val = @ibase_server_info
($db->service_handle
, IBASE_SVC_SERVER_VERSION
);
357 preg_match('#V([\d.]+)#', $val, $match);
360 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
362 $db_info = @ibase_db_info
($db->service_handle
, $dbname, IBASE_STS_HDR_PAGES
);
364 preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
365 $page_size = intval($regs[1]);
366 if ($page_size < 8192)
368 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
375 WHERE RDB$SYSTEM_FLAG IS NULL
376 AND RDB$FUNCTION_NAME = 'CHAR_LENGTH'";
377 $result = $db->sql_query($sql);
378 $row = $db->sql_fetchrow($result);
379 $db->sql_freeresult($result);
381 // if its a UDF, its too old
384 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
388 $sql = "SELECT FIRST 0 char_length('')
390 $result = $db->sql_query($sql);
391 if (!$result) // This can only fail if char_length is not defined
393 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
395 $db->sql_freeresult($result);
398 // Setup the stuff for our random table
399 $char_array = array_merge(range('A', 'Z'), range('0', '9'));
400 $char_len = mt_rand(7, 9);
401 $char_array_len = sizeof($char_array) - 1;
405 for ($i = 0; $i < $char_len; $i++
)
407 $final .= $char_array[mt_rand(0, $char_array_len)];
410 // Create some random table
411 $sql = 'CREATE TABLE ' . $final . " (
412 FIELD1 VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE,
413 FIELD2 INTEGER DEFAULT 0 NOT NULL);";
414 $db->sql_query($sql);
416 // Create an index that should fail if the page size is less than 8192
417 $sql = 'CREATE INDEX ' . $final . ' ON ' . $final . '(FIELD1, FIELD2);';
418 $db->sql_query($sql);
420 if (ibase_errmsg() !== false)
422 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
426 // Kill the old table
427 $db->sql_query('DROP TABLE ' . $final . ';');
437 FROM NLS_DATABASE_PARAMETERS
438 WHERE PARAMETER = 'NLS_RDBMS_VERSION'
439 OR PARAMETER = 'NLS_CHARACTERSET'";
440 $result = $db->sql_query($sql);
442 while ($row = $db->sql_fetchrow($result))
444 $stats[$row['parameter']] = $row['value'];
446 $db->sql_freeresult($result);
448 if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<') && $stats['NLS_CHARACTERSET'] !== 'UTF8')
450 $error[] = $lang['INST_ERR_DB_NO_ORACLE'];
458 $sql = "SHOW server_encoding;";
459 $result = $db->sql_query($sql);
460 $row = $db->sql_fetchrow($result);
461 $db->sql_freeresult($result);
463 if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
465 $error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
473 if ($error_connect && (!isset($error) ||
!sizeof($error)))
481 * remove_remarks will strip the sql comment lines out of an uploaded sql file
483 function remove_remarks(&$sql)
485 $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
489 * split_sql_file will split an uploaded sql file into single sql statements.
490 * Note: expects trim() to have already been run on $sql.
492 function split_sql_file($sql, $delimiter)
494 $sql = str_replace("\r" , '', $sql);
495 $data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
497 $data = array_map('trim', $data);
500 $end_data = end($data);
502 if (empty($end_data))
504 unset($data[key($data)]);
511 * For replacing {L_*} strings with preg_replace_callback
513 function adjust_language_keys_callback($matches)
515 if (!empty($matches[1]))
519 return (!empty($lang[$matches[1]])) ?
$db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);