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',
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',
86 'LABEL' => 'MS SQL Server [ 2005 ]',
90 'COMMENTS' => 'remove_comments',
91 'DRIVER' => 'mssql_2005',
98 'MODULE' => 'ibm_db2',
100 'COMMENTS' => 'remove_comments',
107 'SCHEMA' => 'oracle',
110 'COMMENTS' => 'remove_comments',
111 'DRIVER' => 'oracle',
116 'LABEL' => 'PostgreSQL 7.x/8.x',
117 'SCHEMA' => 'postgres',
120 'COMMENTS' => 'remove_comments',
121 'DRIVER' => 'postgres',
127 'SCHEMA' => 'sqlite',
128 'MODULE' => 'sqlite',
130 'COMMENTS' => 'remove_remarks',
131 'DRIVER' => 'sqlite',
139 if (isset($available_dbms[$dbms]))
141 $available_dbms = array($dbms => $available_dbms[$dbms]);
149 // now perform some checks whether they are really available
150 foreach ($available_dbms as $db_name => $db_ary)
152 if ($only_20x_options && !$db_ary['2.0.x'])
154 if ($return_unavailable)
156 $available_dbms[$db_name]['AVAILABLE'] = false;
160 unset($available_dbms[$db_name]);
165 $dll = $db_ary['MODULE'];
167 if (!@extension_loaded
($dll))
169 if (!can_load_dll($dll))
171 if ($return_unavailable)
173 $available_dbms[$db_name]['AVAILABLE'] = false;
177 unset($available_dbms[$db_name]);
182 $any_db_support = true;
185 if ($return_unavailable)
187 $available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
189 return $available_dbms;
193 * Generate the drop down of available database options
195 function dbms_select($default = '', $only_20x_options = false)
199 $available_dbms = get_available_dbms(false, false, $only_20x_options);
201 foreach ($available_dbms as $dbms_name => $details)
203 $selected = ($dbms_name == $default) ?
' selected="selected"' : '';
204 $dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
206 return $dbms_options;
210 * Get tables of a database
212 function get_tables($db)
214 switch ($db->dbms_type
)
217 $sql = 'SHOW TABLES';
223 WHERE type = "table"';
233 $sql = 'SELECT relname
234 FROM pg_stat_user_tables';
238 $sql = 'SELECT rdb$relation_name
240 WHERE rdb$view_source is null
241 AND rdb$system_flag = 0';
245 $sql = "SELECT tabname
248 AND tabschema = 'DB2ADMIN'";
253 $sql = 'SELECT table_name
258 $result = $db->sql_query($sql);
262 while ($row = $db->sql_fetchrow($result))
264 $tables[] = current($row);
267 $db->sql_freeresult($result);
273 * Used to test whether we are able to connect to the database the user has specified
274 * and identify any problems (eg there are already tables with the names we want to use
275 * @param array $dbms should be of the format of an element of the array returned by {@link get_available_dbms get_available_dbms()}
276 * necessary extensions should be loaded already
278 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)
282 $dbms = $dbms_details['DRIVER'];
286 // Include the DB layer
287 include(PHPBB_ROOT_PATH
. 'includes/db/' . $dbms . '.' . PHP_EXT
);
290 // Instantiate it and set return on error true
291 $sql_db = 'dbal_' . $dbms;
293 $db->sql_return_on_error(true);
295 // Check that we actually have a database name before going any further.....
296 if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
298 $error[] = $lang['INST_ERR_DB_NO_NAME'];
302 // Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea
303 if ($dbms_details['DRIVER'] == 'sqlite' && stripos(phpbb_realpath($dbhost), phpbb_realpath('../')) === 0)
305 $error[] = $lang['INST_ERR_DB_FORUM_PATH'];
309 // Check the prefix length to ensure that index names are not too long and does not contain invalid characters
310 switch ($dbms_details['DRIVER'])
314 if (strspn($table_prefix, '-./\\') !== 0)
316 $error[] = $lang['INST_ERR_PREFIX_INVALID'];
333 $prefix_length = 108;
337 $prefix_length = 200;
346 if (strlen($table_prefix) > $prefix_length)
348 $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
352 // Try and connect ...
353 if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
355 $db_error = $db->sql_error();
356 $error[] = $lang['INST_ERR_DB_CONNECT'] . '<br />' . (($db_error['message']) ?
$db_error['message'] : $lang['INST_ERR_DB_NO_ERROR']);
360 // Likely matches for an existing phpBB installation
361 if (!$prefix_may_exist)
363 $temp_prefix = strtolower($table_prefix);
364 $table_ary = array($temp_prefix . 'attachments', $temp_prefix . 'config', $temp_prefix . 'sessions', $temp_prefix . 'topics', $temp_prefix . 'users');
366 $tables = get_tables($db);
367 $tables = array_map('strtolower', $tables);
368 $table_intersect = array_intersect($tables, $table_ary);
370 if (sizeof($table_intersect))
372 $error[] = $lang['INST_ERR_PREFIX'];
376 // Make sure that the user has selected a sensible DBAL for the DBMS actually installed
377 switch ($dbms_details['DRIVER'])
380 if (version_compare(mysql_get_server_info($db->db_connect_id
), '4.1.3', '<'))
382 $error[] = $lang['INST_ERR_DB_NO_MYSQL'];
387 if (version_compare(mysqli_get_server_info($db->db_connect_id
), '4.1.3', '<'))
389 $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
394 if (version_compare(sqlite_libversion(), '2.8.2', '<'))
396 $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
401 // check the version of FB, use some hackery if we can't get access to the server info
402 if ($db->service_handle
!== false && strtolower($dbuser) == 'sysdba')
404 $val = @ibase_server_info
($db->service_handle
, IBASE_SVC_SERVER_VERSION
);
405 preg_match('#V([\d.]+)#', $val, $match);
408 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
410 $db_info = @ibase_db_info
($db->service_handle
, $dbname, IBASE_STS_HDR_PAGES
);
412 preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
413 $page_size = intval($regs[1]);
414 if ($page_size < 8192)
416 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
423 WHERE RDB$SYSTEM_FLAG IS NULL
424 AND RDB$FUNCTION_NAME = 'CHAR_LENGTH'";
425 $result = $db->sql_query($sql);
426 $row = $db->sql_fetchrow($result);
427 $db->sql_freeresult($result);
429 // if its a UDF, its too old
432 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
436 $sql = "SELECT FIRST 0 char_length('')
438 $result = $db->sql_query($sql);
439 if (!$result) // This can only fail if char_length is not defined
441 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
443 $db->sql_freeresult($result);
446 // Setup the stuff for our random table
447 $char_array = array_merge(range('A', 'Z'), range('0', '9'));
448 $char_len = mt_rand(7, 9);
449 $char_array_len = sizeof($char_array) - 1;
453 for ($i = 0; $i < $char_len; $i++
)
455 $final .= $char_array[mt_rand(0, $char_array_len)];
458 // Create some random table
459 $sql = 'CREATE TABLE ' . $final . " (
460 FIELD1 VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE,
461 FIELD2 INTEGER DEFAULT 0 NOT NULL);";
462 $db->sql_query($sql);
464 // Create an index that should fail if the page size is less than 8192
465 $sql = 'CREATE INDEX ' . $final . ' ON ' . $final . '(FIELD1, FIELD2);';
466 $db->sql_query($sql);
468 if (ibase_errmsg() !== false)
470 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
473 // Kill the old table
474 $db->sql_query('DROP TABLE ' . $final . ';');
484 FROM NLS_DATABASE_PARAMETERS
485 WHERE PARAMETER = 'NLS_RDBMS_VERSION'
486 OR PARAMETER = 'NLS_CHARACTERSET'";
487 $result = $db->sql_query($sql);
489 while ($row = $db->sql_fetchrow($result))
491 $stats[$row['parameter']] = $row['value'];
493 $db->sql_freeresult($result);
495 if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<'))
497 $error[] = $lang['INST_ERR_DB_NO_ORACLE'];
500 if ($stats['NLS_CHARACTERSET'] !== 'AL32UTF8')
502 $error[] = $lang['INST_ERR_DB_NO_ORACLE_NLS'];
510 $sql = "SHOW server_encoding;";
511 $result = $db->sql_query($sql);
512 $row = $db->sql_fetchrow($result);
513 $db->sql_freeresult($result);
515 if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
517 $error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
525 if ($error_connect && (!isset($error) ||
!sizeof($error)))
533 * remove_remarks will strip the sql comment lines out of an uploaded sql file
535 function remove_remarks(&$sql)
537 $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
541 * split_sql_file will split an uploaded sql file into single sql statements.
542 * Note: expects trim() to have already been run on $sql.
544 function split_sql_file($sql, $delimiter)
546 $sql = str_replace("\r" , '', $sql);
547 $data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
549 $data = array_map('trim', $data);
552 $end_data = end($data);
554 if (empty($end_data))
556 unset($data[key($data)]);
563 * For replacing {L_*} strings with preg_replace_callback
565 function adjust_language_keys_callback($matches)
567 if (!empty($matches[1]))
571 return (!empty($lang[$matches[1]])) ?
$db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);