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 // 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.
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.
30 // Net result we'll disable automatic inclusion of SQLite support
32 // See: r9618 and #56105
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;
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.
44 function get_available_dbms($dbms = false, $return_unavailable = false, $only_20x_options = false)
47 $available_dbms = array(
49 'LABEL' => 'FireBird',
50 'SCHEMA' => 'firebird',
51 'MODULE' => 'interbase',
53 'COMMENTS' => 'remove_remarks',
54 'DRIVER' => 'firebird',
59 'LABEL' => 'MySQL with MySQLi Extension',
60 'SCHEMA' => 'mysql_41',
63 'COMMENTS' => 'remove_remarks',
73 'COMMENTS' => 'remove_remarks',
79 'LABEL' => 'MS SQL Server 2000+',
83 'COMMENTS' => 'remove_comments',
89 'LABEL' => 'MS SQL Server [ ODBC ]',
93 'COMMENTS' => 'remove_comments',
94 'DRIVER' => 'mssql_odbc',
98 'mssqlnative' => array(
99 'LABEL' => 'MS SQL Server 2005+ [ Native ]',
101 'MODULE' => 'sqlsrv',
103 'COMMENTS' => 'remove_comments',
104 'DRIVER' => 'mssqlnative',
110 'SCHEMA' => 'oracle',
113 'COMMENTS' => 'remove_comments',
114 'DRIVER' => 'oracle',
119 'LABEL' => 'PostgreSQL 7.x/8.x',
120 'SCHEMA' => 'postgres',
123 'COMMENTS' => 'remove_comments',
124 'DRIVER' => 'postgres',
130 'SCHEMA' => 'sqlite',
131 'MODULE' => 'sqlite',
133 'COMMENTS' => 'remove_remarks',
134 'DRIVER' => 'sqlite',
142 if (isset($available_dbms[$dbms]))
144 $available_dbms = array($dbms => $available_dbms[$dbms]);
152 // now perform some checks whether they are really available
153 foreach ($available_dbms as $db_name => $db_ary)
155 if ($only_20x_options && !$db_ary['2.0.x'])
157 if ($return_unavailable)
159 $available_dbms[$db_name]['AVAILABLE'] = false;
163 unset($available_dbms[$db_name]);
168 $dll = $db_ary['MODULE'];
170 if (!@extension_loaded($dll))
172 if (!can_load_dll($dll))
174 if ($return_unavailable)
176 $available_dbms[$db_name]['AVAILABLE'] = false;
180 unset($available_dbms[$db_name]);
185 $any_db_support = true;
188 if ($return_unavailable)
190 $available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
192 return $available_dbms;
196 * Generate the drop down of available database options
198 function dbms_select($default = '', $only_20x_options = false)
202 $available_dbms = get_available_dbms(false, false, $only_20x_options);
204 foreach ($available_dbms as $dbms_name => $details)
206 $selected = ($dbms_name == $default) ? ' selected="selected"' : '';
207 $dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
209 return $dbms_options;
213 * Get tables of a database
217 function get_tables(&$db)
219 if (!class_exists('phpbb_db_tools'))
221 global $phpbb_root_path, $phpEx;
222 require($phpbb_root_path . 'includes/db/db_tools.' . $phpEx);
225 $db_tools = new phpbb_db_tools($db);
227 return $db_tools->sql_list_tables();
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
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)
238 global $phpbb_root_path, $phpEx, $config, $lang;
240 $dbms = $dbms_details['DRIVER'];
244 // Include the DB layer
245 include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
248 // Instantiate it and set return on error true
249 $sql_db = 'dbal_' . $dbms;
251 $db->sql_return_on_error(true);
253 // Check that we actually have a database name before going any further.....
254 if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
256 $error[] = $lang['INST_ERR_DB_NO_NAME'];
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)
263 $error[] = $lang['INST_ERR_DB_FORUM_PATH'];
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'])
272 if (strspn($table_prefix, '-./\\') !== 0)
274 $error[] = $lang['INST_ERR_PREFIX_INVALID'];
291 $prefix_length = 200;
300 if (strlen($table_prefix) > $prefix_length)
302 $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
306 // Try and connect ...
307 if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
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']);
314 // Likely matches for an existing phpBB installation
315 if (!$prefix_may_exist)
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');
320 $tables = get_tables($db);
321 $tables = array_map('strtolower', $tables);
322 $table_intersect = array_intersect($tables, $table_ary);
324 if (sizeof($table_intersect))
326 $error[] = $lang['INST_ERR_PREFIX'];
330 // Make sure that the user has selected a sensible DBAL for the DBMS actually installed
331 switch ($dbms_details['DRIVER'])
334 if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
336 $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
341 if (version_compare(sqlite_libversion(), '2.8.2', '<'))
343 $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
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'))
351 $val = @ibase_server_info($db->service_handle, IBASE_SVC_SERVER_VERSION);
352 preg_match('#V([\d.]+)#', $val, $match);
355 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
357 $db_info = @ibase_db_info($db->service_handle, $dbname, IBASE_STS_HDR_PAGES);
359 preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
360 $page_size = intval($regs[1]);
361 if ($page_size < 8192)
363 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
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);
376 // if its a UDF, its too old
379 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
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
388 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
390 $db->sql_freeresult($result);
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;
400 for ($i = 0; $i < $char_len; $i++)
402 $final .= $char_array[mt_rand(0, $char_array_len)];
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);
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);
415 if (ibase_errmsg() !== false)
417 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
421 // Kill the old table
422 $db->sql_query('DROP TABLE ' . $final . ';');
432 FROM NLS_DATABASE_PARAMETERS
433 WHERE PARAMETER = 'NLS_RDBMS_VERSION'
434 OR PARAMETER = 'NLS_CHARACTERSET'";
435 $result = $db->sql_query($sql);
437 while ($row = $db->sql_fetchrow($result))
439 $stats[$row['parameter']] = $row['value'];
441 $db->sql_freeresult($result);
443 if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<') && $stats['NLS_CHARACTERSET'] !== 'UTF8')
445 $error[] = $lang['INST_ERR_DB_NO_ORACLE'];
453 $sql = "SHOW server_encoding;";
454 $result = $db->sql_query($sql);
455 $row = $db->sql_fetchrow($result);
456 $db->sql_freeresult($result);
458 if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
460 $error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
468 if ($error_connect && (!isset($error) || !sizeof($error)))
476 * remove_remarks will strip the sql comment lines out of an uploaded sql file
478 function remove_remarks(&$sql)
480 $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
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.
487 function split_sql_file($sql, $delimiter)
489 $sql = str_replace("\r" , '', $sql);
490 $data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
492 $data = array_map('trim', $data);
495 $end_data = end($data);
497 if (empty($end_data))
499 unset($data[key($data)]);
506 * For replacing {L_*} strings with preg_replace_callback
508 function adjust_language_keys_callback($matches)
510 if (!empty($matches[1]))
514 return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);