replace constants with class constants.
[phpbb.git] / phpBB / includes / functions_install.php
blob2c81b73e7f0e8f28ba68a082f77886eeb4449b8a
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',
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 'mssql_2005'=> array(
86 'LABEL' => 'MS SQL Server [ 2005 ]',
87 'SCHEMA' => 'mssql',
88 'MODULE' => 'sqlsrv',
89 'DELIM' => 'GO',
90 'COMMENTS' => 'remove_comments',
91 'DRIVER' => 'mssql_2005',
92 'AVAILABLE' => true,
93 '2.0.x' => true,
95 'db2' => array(
96 'LABEL' => 'IBM DB2',
97 'SCHEMA' => 'db2',
98 'MODULE' => 'ibm_db2',
99 'DELIM' => ';',
100 'COMMENTS' => 'remove_comments',
101 'DRIVER' => 'db2',
102 'AVAILABLE' => true,
103 '2.0.x' => false,
105 'oracle' => array(
106 'LABEL' => 'Oracle',
107 'SCHEMA' => 'oracle',
108 'MODULE' => 'oci8',
109 'DELIM' => '/',
110 'COMMENTS' => 'remove_comments',
111 'DRIVER' => 'oracle',
112 'AVAILABLE' => true,
113 '2.0.x' => false,
115 'postgres' => array(
116 'LABEL' => 'PostgreSQL 7.x/8.x',
117 'SCHEMA' => 'postgres',
118 'MODULE' => 'pgsql',
119 'DELIM' => ';',
120 'COMMENTS' => 'remove_comments',
121 'DRIVER' => 'postgres',
122 'AVAILABLE' => true,
123 '2.0.x' => true,
125 'sqlite' => array(
126 'LABEL' => 'SQLite',
127 'SCHEMA' => 'sqlite',
128 'MODULE' => 'sqlite',
129 'DELIM' => ';',
130 'COMMENTS' => 'remove_remarks',
131 'DRIVER' => 'sqlite',
132 'AVAILABLE' => true,
133 '2.0.x' => false,
137 if ($dbms)
139 if (isset($available_dbms[$dbms]))
141 $available_dbms = array($dbms => $available_dbms[$dbms]);
143 else
145 return array();
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;
158 else
160 unset($available_dbms[$db_name]);
162 continue;
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;
175 else
177 unset($available_dbms[$db_name]);
179 continue;
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)
197 global $lang;
199 $available_dbms = get_available_dbms(false, false, $only_20x_options);
200 $dbms_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)
216 case 'mysql':
217 $sql = 'SHOW TABLES';
218 break;
220 case 'sqlite':
221 $sql = 'SELECT name
222 FROM sqlite_master
223 WHERE type = "table"';
224 break;
226 case 'mssql':
227 $sql = "SELECT name
228 FROM sysobjects
229 WHERE type='U'";
230 break;
232 case 'postgres':
233 $sql = 'SELECT relname
234 FROM pg_stat_user_tables';
235 break;
237 case 'firebird':
238 $sql = 'SELECT rdb$relation_name
239 FROM rdb$relations
240 WHERE rdb$view_source is null
241 AND rdb$system_flag = 0';
242 break;
244 case 'db2':
245 $sql = "SELECT tabname
246 FROM SYSCAT.TABLES
247 WHERE type = 'T'
248 AND tabschema = 'DB2ADMIN'";
249 $field = 'tabname';
250 break;
252 case 'oracle':
253 $sql = 'SELECT table_name
254 FROM USER_TABLES';
255 break;
258 $result = $db->sql_query($sql);
260 $tables = array();
262 while ($row = $db->sql_fetchrow($result))
264 $tables[] = current($row);
267 $db->sql_freeresult($result);
269 return $tables;
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)
280 global $config, $lang;
282 $dbms = $dbms_details['DRIVER'];
284 if ($load_dbal)
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;
292 $db = new $sql_db();
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'];
299 return false;
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'];
306 return false;
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'])
312 case 'mysql':
313 case 'mysqli':
314 if (strspn($table_prefix, '-./\\') !== 0)
316 $error[] = $lang['INST_ERR_PREFIX_INVALID'];
317 return false;
320 // no break;
322 case 'postgres':
323 $prefix_length = 36;
324 break;
326 case 'mssql':
327 case 'mssql_odbc':
328 case 'mssql_2005':
329 $prefix_length = 90;
330 break;
332 case 'db2':
333 $prefix_length = 108;
334 break;
336 case 'sqlite':
337 $prefix_length = 200;
338 break;
340 case 'firebird':
341 case 'oracle':
342 $prefix_length = 6;
343 break;
346 if (strlen($table_prefix) > $prefix_length)
348 $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
349 return false;
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']);
358 else
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'])
379 case 'mysql':
380 if (version_compare(mysql_get_server_info($db->db_connect_id), '4.1.3', '<'))
382 $error[] = $lang['INST_ERR_DB_NO_MYSQL'];
384 break;
386 case 'mysqli':
387 if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
389 $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
391 break;
393 case 'sqlite':
394 if (version_compare(sqlite_libversion(), '2.8.2', '<'))
396 $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
398 break;
400 case 'firebird':
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);
406 if ($match[1] < 2)
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'];
419 else
421 $sql = "SELECT *
422 FROM RDB$FUNCTIONS
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
430 if ($row)
432 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
434 else
436 $sql = "SELECT FIRST 0 char_length('')
437 FROM RDB\$DATABASE";
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;
451 $final = '';
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 . ';');
476 unset($final);
478 break;
480 case 'oracle':
481 if ($unicode_check)
483 $sql = "SELECT *
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'];
505 break;
507 case 'postgres':
508 if ($unicode_check)
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'];
520 break;
525 if ($error_connect && (!isset($error) || !sizeof($error)))
527 return true;
529 return false;
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);
551 // The empty case
552 $end_data = end($data);
554 if (empty($end_data))
556 unset($data[key($data)]);
559 return $data;
563 * For replacing {L_*} strings with preg_replace_callback
565 function adjust_language_keys_callback($matches)
567 if (!empty($matches[1]))
569 global $lang, $db;
571 return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);