further updates
[phpbb.git] / phpBB / includes / functions_install.php
blobf43ef7c5c7c05008e959a7087a844f0a4ca3c5b9
1 <?php
2 /**
4 * @package install
5 * @version $Id$
6 * @copyright (c) 2006 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
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;
27 /**
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)
33 global $lang;
34 $available_dbms = array(
35 'firebird' => array(
36 'LABEL' => 'FireBird',
37 'SCHEMA' => 'firebird',
38 'MODULE' => 'interbase',
39 'DELIM' => ';;',
40 'COMMENTS' => 'remove_remarks',
41 'DRIVER' => 'firebird',
42 'AVAILABLE' => true,
43 '2.0.x' => false,
45 'mysqli' => array(
46 'LABEL' => 'MySQL with MySQLi Extension',
47 'SCHEMA' => 'mysql_41',
48 'MODULE' => 'mysqli',
49 'DELIM' => ';',
50 'COMMENTS' => 'remove_remarks',
51 'DRIVER' => 'mysqli',
52 'AVAILABLE' => true,
53 '2.0.x' => true,
55 'mysql' => array(
56 'LABEL' => 'MySQL',
57 'SCHEMA' => 'mysql',
58 'MODULE' => 'mysql',
59 'DELIM' => ';',
60 'COMMENTS' => 'remove_remarks',
61 'DRIVER' => 'mysql',
62 'AVAILABLE' => true,
63 '2.0.x' => true,
65 'mssql' => array(
66 'LABEL' => 'MS SQL Server 2000+',
67 'SCHEMA' => 'mssql',
68 'MODULE' => 'mssql',
69 'DELIM' => 'GO',
70 'COMMENTS' => 'remove_comments',
71 'DRIVER' => 'mssql',
72 'AVAILABLE' => true,
73 '2.0.x' => true,
75 'mssql_odbc'=> array(
76 'LABEL' => 'MS SQL Server [ ODBC ]',
77 'SCHEMA' => 'mssql',
78 'MODULE' => 'odbc',
79 'DELIM' => 'GO',
80 'COMMENTS' => 'remove_comments',
81 'DRIVER' => 'mssql_odbc',
82 'AVAILABLE' => true,
83 '2.0.x' => true,
85 'oracle' => array(
86 'LABEL' => 'Oracle',
87 'SCHEMA' => 'oracle',
88 'MODULE' => 'oci8',
89 'DELIM' => '/',
90 'COMMENTS' => 'remove_comments',
91 'DRIVER' => 'oracle',
92 'AVAILABLE' => true,
93 '2.0.x' => false,
95 'postgres' => array(
96 'LABEL' => 'PostgreSQL 7.x/8.x',
97 'SCHEMA' => 'postgres',
98 'MODULE' => 'pgsql',
99 'DELIM' => ';',
100 'COMMENTS' => 'remove_comments',
101 'DRIVER' => 'postgres',
102 'AVAILABLE' => true,
103 '2.0.x' => true,
105 'sqlite' => array(
106 'LABEL' => 'SQLite',
107 'SCHEMA' => 'sqlite',
108 'MODULE' => 'sqlite',
109 'DELIM' => ';',
110 'COMMENTS' => 'remove_remarks',
111 'DRIVER' => 'sqlite',
112 'AVAILABLE' => true,
113 '2.0.x' => false,
117 if ($dbms)
119 if (isset($available_dbms[$dbms]))
121 $available_dbms = array($dbms => $available_dbms[$dbms]);
123 else
125 return array();
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;
138 else
140 unset($available_dbms[$db_name]);
142 continue;
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;
155 else
157 unset($available_dbms[$db_name]);
159 continue;
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)
177 global $lang;
179 $available_dbms = get_available_dbms(false, false, $only_20x_options);
180 $dbms_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)
196 case 'mysql':
197 case 'mysql4':
198 case 'mysqli':
199 $sql = 'SHOW TABLES';
200 break;
202 case 'sqlite':
203 $sql = 'SELECT name
204 FROM sqlite_master
205 WHERE type = "table"';
206 break;
208 case 'mssql':
209 case 'mssql_odbc':
210 $sql = "SELECT name
211 FROM sysobjects
212 WHERE type='U'";
213 break;
215 case 'postgres':
216 $sql = 'SELECT relname
217 FROM pg_stat_user_tables';
218 break;
220 case 'firebird':
221 $sql = 'SELECT rdb$relation_name
222 FROM rdb$relations
223 WHERE rdb$view_source is null
224 AND rdb$system_flag = 0';
225 break;
227 case 'oracle':
228 $sql = 'SELECT table_name
229 FROM USER_TABLES';
230 break;
233 $result = $db->sql_query($sql);
235 $tables = array();
237 while ($row = $db->sql_fetchrow($result))
239 $tables[] = current($row);
242 $db->sql_freeresult($result);
244 return $tables;
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'];
259 if ($load_dbal)
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;
267 $db = new $sql_db();
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'];
274 return false;
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'];
281 return false;
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'])
287 case 'mysql':
288 case 'mysqli':
289 if (strpos($table_prefix, '-') !== false || strpos($table_prefix, '.') !== false)
291 $error[] = $lang['INST_ERR_PREFIX_INVALID'];
292 return false;
295 // no break;
297 case 'postgres':
298 $prefix_length = 36;
299 break;
301 case 'mssql':
302 case 'mssql_odbc':
303 $prefix_length = 90;
304 break;
306 case 'sqlite':
307 $prefix_length = 200;
308 break;
310 case 'firebird':
311 case 'oracle':
312 $prefix_length = 6;
313 break;
316 if (strlen($table_prefix) > $prefix_length)
318 $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
319 return false;
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']);
328 else
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'])
349 case 'mysqli':
350 if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
352 $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
354 break;
356 case 'sqlite':
357 if (version_compare(sqlite_libversion(), '2.8.2', '<'))
359 $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
361 break;
363 case 'firebird':
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);
369 if ($match[1] < 2)
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'];
382 else
384 $sql = "SELECT *
385 FROM RDB$FUNCTIONS
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
393 if ($row)
395 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
397 else
399 $sql = "SELECT FIRST 0 char_length('')
400 FROM RDB\$DATABASE";
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;
414 $final = '';
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'];
435 else
437 // Kill the old table
438 $db->sql_query('DROP TABLE ' . $final . ';');
440 unset($final);
442 break;
444 case 'oracle':
445 if ($unicode_check)
447 $sql = "SELECT *
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'];
464 break;
466 case 'postgres':
467 if ($unicode_check)
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'];
479 break;
484 if ($error_connect && (!isset($error) || !sizeof($error)))
486 return true;
488 return false;
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);
510 // The empty case
511 $end_data = end($data);
513 if (empty($end_data))
515 unset($data[key($data)]);
518 return $data;
522 * For replacing {L_*} strings with preg_replace_callback
524 function adjust_language_keys_callback($matches)
526 if (!empty($matches[1]))
528 global $lang, $db;
530 return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);