6 * @copyright (c) 2006 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * Determine if we are able to load a specified PHP module and do so if possible
22 function can_load_dll($dll)
24 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;
28 * Returns an array of available DBMS with some data, if a DBMS is specified it will only
29 * return data for that DBMS and will load its extension if necessary.
31 function get_available_dbms($dbms = false, $return_unavailable = false, $only_20x_options = false)
34 $available_dbms = array(
36 'LABEL' => 'FireBird',
37 'SCHEMA' => 'firebird',
38 'MODULE' => 'interbase',
40 'COMMENTS' => 'remove_remarks',
41 'DRIVER' => 'firebird',
46 'LABEL' => 'MySQL with MySQLi Extension',
47 'SCHEMA' => 'mysql_41',
50 'COMMENTS' => 'remove_remarks',
60 'COMMENTS' => 'remove_remarks',
66 'LABEL' => 'MS SQL Server 2000+',
70 'COMMENTS' => 'remove_comments',
76 'LABEL' => 'MS SQL Server [ ODBC ]',
80 'COMMENTS' => 'remove_comments',
81 'DRIVER' => 'mssql_odbc',
90 'COMMENTS' => 'remove_comments',
96 'LABEL' => 'PostgreSQL 7.x/8.x',
97 'SCHEMA' => 'postgres',
100 'COMMENTS' => 'remove_comments',
101 'DRIVER' => 'postgres',
107 'SCHEMA' => 'sqlite',
108 'MODULE' => 'sqlite',
110 'COMMENTS' => 'remove_remarks',
111 'DRIVER' => 'sqlite',
119 if (isset($available_dbms[$dbms]))
121 $available_dbms = array($dbms => $available_dbms[$dbms]);
129 // now perform some checks whether they are really available
130 foreach ($available_dbms as $db_name => $db_ary)
132 if ($only_20x_options && !$db_ary['2.0.x'])
134 if ($return_unavailable)
136 $available_dbms[$db_name]['AVAILABLE'] = false;
140 unset($available_dbms[$db_name]);
145 $dll = $db_ary['MODULE'];
147 if (!@extension_loaded
($dll))
149 if (!can_load_dll($dll))
151 if ($return_unavailable)
153 $available_dbms[$db_name]['AVAILABLE'] = false;
157 unset($available_dbms[$db_name]);
162 $any_db_support = true;
165 if ($return_unavailable)
167 $available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
169 return $available_dbms;
173 * Generate the drop down of available database options
175 function dbms_select($default = '', $only_20x_options = false)
179 $available_dbms = get_available_dbms(false, false, $only_20x_options);
181 foreach ($available_dbms as $dbms_name => $details)
183 $selected = ($dbms_name == $default) ?
' selected="selected"' : '';
184 $dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
186 return $dbms_options;
190 * Get tables of a database
192 function get_tables($db)
194 switch ($db->sql_layer
)
199 $sql = 'SHOW TABLES';
205 WHERE type = "table"';
216 $sql = 'SELECT relname
217 FROM pg_stat_user_tables';
221 $sql = 'SELECT rdb$relation_name
223 WHERE rdb$view_source is null
224 AND rdb$system_flag = 0';
228 $sql = 'SELECT table_name
233 $result = $db->sql_query($sql);
237 while ($row = $db->sql_fetchrow($result))
239 $tables[] = current($row);
242 $db->sql_freeresult($result);
248 * Used to test whether we are able to connect to the database the user has specified
249 * and identify any problems (eg there are already tables with the names we want to use
250 * @param array $dbms should be of the format of an element of the array returned by {@link get_available_dbms get_available_dbms()}
251 * necessary extensions should be loaded already
253 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)
255 global $phpbb_root_path, $phpEx, $config, $lang;
257 $dbms = $dbms_details['DRIVER'];
261 // Include the DB layer
262 include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
265 // Instantiate it and set return on error true
266 $sql_db = 'dbal_' . $dbms;
268 $db->sql_return_on_error(true);
270 // Check that we actually have a database name before going any further.....
271 if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
273 $error[] = $lang['INST_ERR_DB_NO_NAME'];
277 // Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea
278 if ($dbms_details['DRIVER'] == 'sqlite' && stripos(phpbb_realpath($dbhost), phpbb_realpath('../')) === 0)
280 $error[] = $lang['INST_ERR_DB_FORUM_PATH'];
284 // Check the prefix length to ensure that index names are not too long and does not contain invalid characters
285 switch ($dbms_details['DRIVER'])
289 if (strpos($table_prefix, '-') !== false ||
strpos($table_prefix, '.') !== false)
291 $error[] = $lang['INST_ERR_PREFIX_INVALID'];
307 $prefix_length = 200;
316 if (strlen($table_prefix) > $prefix_length)
318 $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
322 // Try and connect ...
323 if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
325 $db_error = $db->sql_error();
326 $error[] = $lang['INST_ERR_DB_CONNECT'] . '<br />' . (($db_error['message']) ?
$db_error['message'] : $lang['INST_ERR_DB_NO_ERROR']);
330 // Likely matches for an existing phpBB installation
331 if (!$prefix_may_exist)
333 $temp_prefix = strtolower($table_prefix);
334 $table_ary = array($temp_prefix . 'attachments', $temp_prefix . 'config', $temp_prefix . 'sessions', $temp_prefix . 'topics', $temp_prefix . 'users');
336 $tables = get_tables($db);
337 $tables = array_map('strtolower', $tables);
338 $table_intersect = array_intersect($tables, $table_ary);
340 if (sizeof($table_intersect))
342 $error[] = $lang['INST_ERR_PREFIX'];
346 // Make sure that the user has selected a sensible DBAL for the DBMS actually installed
347 switch ($dbms_details['DRIVER'])
350 if (version_compare(mysqli_get_server_info($db->db_connect_id
), '4.1.3', '<'))
352 $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
357 if (version_compare(sqlite_libversion(), '2.8.2', '<'))
359 $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
364 // check the version of FB, use some hackery if we can't get access to the server info
365 if ($db->service_handle
!== false && function_exists('ibase_server_info'))
367 $val = @ibase_server_info
($db->service_handle
, IBASE_SVC_SERVER_VERSION
);
368 preg_match('#V([\d.]+)#', $val, $match);
371 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
373 $db_info = @ibase_db_info
($db->service_handle
, $dbname, IBASE_STS_HDR_PAGES
);
375 preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
376 $page_size = intval($regs[1]);
377 if ($page_size < 8192)
379 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
386 WHERE RDB$SYSTEM_FLAG IS NULL
387 AND RDB$FUNCTION_NAME = 'CHAR_LENGTH'";
388 $result = $db->sql_query($sql);
389 $row = $db->sql_fetchrow($result);
390 $db->sql_freeresult($result);
392 // if its a UDF, its too old
395 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
399 $sql = "SELECT FIRST 0 char_length('')
401 $result = $db->sql_query($sql);
402 if (!$result) // This can only fail if char_length is not defined
404 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
406 $db->sql_freeresult($result);
409 // Setup the stuff for our random table
410 $char_array = array_merge(range('A', 'Z'), range('0', '9'));
411 $char_len = mt_rand(7, 9);
412 $char_array_len = sizeof($char_array) - 1;
416 for ($i = 0; $i < $char_len; $i++
)
418 $final .= $char_array[mt_rand(0, $char_array_len)];
421 // Create some random table
422 $sql = 'CREATE TABLE ' . $final . " (
423 FIELD1 VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE,
424 FIELD2 INTEGER DEFAULT 0 NOT NULL);";
425 $db->sql_query($sql);
427 // Create an index that should fail if the page size is less than 8192
428 $sql = 'CREATE INDEX ' . $final . ' ON ' . $final . '(FIELD1, FIELD2);';
429 $db->sql_query($sql);
431 if (ibase_errmsg() !== false)
433 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
437 // Kill the old table
438 $db->sql_query('DROP TABLE ' . $final . ';');
448 FROM NLS_DATABASE_PARAMETERS
449 WHERE PARAMETER = 'NLS_RDBMS_VERSION'
450 OR PARAMETER = 'NLS_CHARACTERSET'";
451 $result = $db->sql_query($sql);
453 while ($row = $db->sql_fetchrow($result))
455 $stats[$row['parameter']] = $row['value'];
457 $db->sql_freeresult($result);
459 if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<') && $stats['NLS_CHARACTERSET'] !== 'UTF8')
461 $error[] = $lang['INST_ERR_DB_NO_ORACLE'];
469 $sql = "SHOW server_encoding;";
470 $result = $db->sql_query($sql);
471 $row = $db->sql_fetchrow($result);
472 $db->sql_freeresult($result);
474 if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
476 $error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
484 if ($error_connect && (!isset($error) ||
!sizeof($error)))
492 * remove_remarks will strip the sql comment lines out of an uploaded sql file
494 function remove_remarks(&$sql)
496 $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
500 * split_sql_file will split an uploaded sql file into single sql statements.
501 * Note: expects trim() to have already been run on $sql.
503 function split_sql_file($sql, $delimiter)
505 $sql = str_replace("\r" , '', $sql);
506 $data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
508 $data = array_map('trim', $data);
511 $end_data = end($data);
513 if (empty($end_data))
515 unset($data[key($data)]);
522 * For replacing {L_*} strings with preg_replace_callback
524 function adjust_language_keys_callback($matches)
526 if (!empty($matches[1]))
530 return (!empty($lang[$matches[1]])) ?
$db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);