let's see if i can break something. :o
[phpbb.git] / phpBB / includes / functions_install.php
blob133ef294b0f6e3756a06959c73a50ab67572249b
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 * 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;
19 /**
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)
25 global $lang;
26 $available_dbms = array(
27 'firebird' => array(
28 'LABEL' => 'FireBird',
29 'SCHEMA' => 'firebird',
30 'MODULE' => 'interbase',
31 'DELIM' => ';;',
32 'COMMENTS' => 'remove_remarks',
33 'DRIVER' => 'firebird',
34 'AVAILABLE' => true,
35 '2.0.x' => false,
37 'mysqli' => array(
38 'LABEL' => 'MySQL with MySQLi Extension',
39 'SCHEMA' => 'mysql_41',
40 'MODULE' => 'mysqli',
41 'DELIM' => ';',
42 'COMMENTS' => 'remove_remarks',
43 'DRIVER' => 'mysqli',
44 'AVAILABLE' => true,
45 '2.0.x' => true,
47 'mysql' => array(
48 'LABEL' => 'MySQL',
49 'SCHEMA' => 'mysql',
50 'MODULE' => 'mysql',
51 'DELIM' => ';',
52 'COMMENTS' => 'remove_remarks',
53 'DRIVER' => 'mysql',
54 'AVAILABLE' => true,
55 '2.0.x' => true,
57 'mssql' => array(
58 'LABEL' => 'MS SQL Server 2000+',
59 'SCHEMA' => 'mssql',
60 'MODULE' => 'mssql',
61 'DELIM' => 'GO',
62 'COMMENTS' => 'remove_comments',
63 'DRIVER' => 'mssql',
64 'AVAILABLE' => true,
65 '2.0.x' => true,
67 'mssql_odbc'=> array(
68 'LABEL' => 'MS SQL Server [ ODBC ]',
69 'SCHEMA' => 'mssql',
70 'MODULE' => 'odbc',
71 'DELIM' => 'GO',
72 'COMMENTS' => 'remove_comments',
73 'DRIVER' => 'mssql_odbc',
74 'AVAILABLE' => true,
75 '2.0.x' => true,
77 'oracle' => array(
78 'LABEL' => 'Oracle',
79 'SCHEMA' => 'oracle',
80 'MODULE' => 'oci8',
81 'DELIM' => '/',
82 'COMMENTS' => 'remove_comments',
83 'DRIVER' => 'oracle',
84 'AVAILABLE' => true,
85 '2.0.x' => false,
87 'postgres' => array(
88 'LABEL' => 'PostgreSQL 7.x/8.x',
89 'SCHEMA' => 'postgres',
90 'MODULE' => 'pgsql',
91 'DELIM' => ';',
92 'COMMENTS' => 'remove_comments',
93 'DRIVER' => 'postgres',
94 'AVAILABLE' => true,
95 '2.0.x' => true,
97 'sqlite' => array(
98 'LABEL' => 'SQLite',
99 'SCHEMA' => 'sqlite',
100 'MODULE' => 'sqlite',
101 'DELIM' => ';',
102 'COMMENTS' => 'remove_remarks',
103 'DRIVER' => 'sqlite',
104 'AVAILABLE' => true,
105 '2.0.x' => false,
109 if ($dbms)
111 if (isset($available_dbms[$dbms]))
113 $available_dbms = array($dbms => $available_dbms[$dbms]);
115 else
117 return array();
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;
130 else
132 unset($available_dbms[$db_name]);
134 continue;
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;
147 else
149 unset($available_dbms[$db_name]);
151 continue;
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)
169 global $lang;
171 $available_dbms = get_available_dbms(false, false, $only_20x_options);
172 $dbms_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)
188 case 'mysql':
189 case 'mysql4':
190 case 'mysqli':
191 $sql = 'SHOW TABLES';
192 break;
194 case 'sqlite':
195 $sql = 'SELECT name
196 FROM sqlite_master
197 WHERE type = "table"';
198 break;
200 case 'mssql':
201 case 'mssql_odbc':
202 $sql = "SELECT name
203 FROM sysobjects
204 WHERE type='U'";
205 break;
207 case 'postgres':
208 $sql = 'SELECT relname
209 FROM pg_stat_user_tables';
210 break;
212 case 'firebird':
213 $sql = 'SELECT rdb$relation_name
214 FROM rdb$relations
215 WHERE rdb$view_source is null
216 AND rdb$system_flag = 0';
217 break;
219 case 'oracle':
220 $sql = 'SELECT table_name
221 FROM USER_TABLES';
222 break;
225 $result = $db->sql_query($sql);
227 $tables = array();
229 while ($row = $db->sql_fetchrow($result))
231 $tables[] = current($row);
234 $db->sql_freeresult($result);
236 return $tables;
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;
249 if ($load_dbal)
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'];
257 $db = new $sql_db();
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'];
264 return false;
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'];
271 return false;
274 // Check the prefix length to ensure that index names are not too long and does not contain invalid characters
275 switch ($dbms['DRIVER'])
277 case 'mysql':
278 case 'mysqli':
279 if (strpos($table_prefix, '-') !== false || strpos($table_prefix, '.') !== false)
281 $error[] = $lang['INST_ERR_PREFIX_INVALID'];
282 return false;
285 // no break;
287 case 'postgres':
288 $prefix_length = 36;
289 break;
291 case 'mssql':
292 case 'mssql_odbc':
293 $prefix_length = 90;
294 break;
296 case 'sqlite':
297 $prefix_length = 200;
298 break;
300 case 'firebird':
301 case 'oracle':
302 $prefix_length = 6;
303 break;
306 if (strlen($table_prefix) > $prefix_length)
308 $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
309 return false;
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']);
318 else
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'])
338 case 'mysqli':
339 if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
341 $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
343 break;
345 case 'sqlite':
346 if (version_compare(sqlite_libversion(), '2.8.2', '<'))
348 $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
350 break;
352 case 'firebird':
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);
358 if ($match[1] < 2)
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'];
371 else
373 $sql = "SELECT *
374 FROM RDB$FUNCTIONS
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
382 if ($row)
384 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
386 else
388 $sql = "SELECT FIRST 0 char_length('')
389 FROM RDB\$DATABASE";
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;
403 $final = '';
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'];
424 else
426 // Kill the old table
427 $db->sql_query('DROP TABLE ' . $final . ';');
429 unset($final);
431 break;
433 case 'oracle':
434 if ($unicode_check)
436 $sql = "SELECT *
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'];
453 break;
455 case 'postgres':
456 if ($unicode_check)
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'];
468 break;
473 if ($error_connect && (!isset($error) || !sizeof($error)))
475 return true;
477 return false;
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);
499 // The empty case
500 $end_data = end($data);
502 if (empty($end_data))
504 unset($data[key($data)]);
507 return $data;
511 * For replacing {L_*} strings with preg_replace_callback
513 function adjust_language_keys_callback($matches)
515 if (!empty($matches[1]))
517 global $lang, $db;
519 return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);