source: branches/posledni/forum/includes/functions_install.php

Last change on this file was 702, checked in by george, 15 years ago
  • Upraveno: Aktualizace fóra.
File size: 13.1 KB
Line 
1<?php
2/**
3*
4* @package install
5* @version $Id$
6* @copyright (c) 2006 phpBB Group
7* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8*
9*/
10
11/**
12* @ignore
13*/
14if (!defined('IN_PHPBB'))
15{
16 exit;
17}
18
19/**
20* Determine if we are able to load a specified PHP module and do so if possible
21*/
22function can_load_dll($dll)
23{
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.
26 //
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.
29 //
30 // Net result we'll disable automatic inclusion of SQLite support
31 //
32 // See: r9618 and #56105
33 if ($dll == 'sqlite')
34 {
35 return false;
36 }
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;
38}
39
40/**
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.
43*/
44function get_available_dbms($dbms = false, $return_unavailable = false, $only_20x_options = false)
45{
46 global $lang;
47 $available_dbms = array(
48 'firebird' => array(
49 'LABEL' => 'FireBird',
50 'SCHEMA' => 'firebird',
51 'MODULE' => 'interbase',
52 'DELIM' => ';;',
53 'COMMENTS' => 'remove_remarks',
54 'DRIVER' => 'firebird',
55 'AVAILABLE' => true,
56 '2.0.x' => false,
57 ),
58 'mysqli' => array(
59 'LABEL' => 'MySQL with MySQLi Extension',
60 'SCHEMA' => 'mysql_41',
61 'MODULE' => 'mysqli',
62 'DELIM' => ';',
63 'COMMENTS' => 'remove_remarks',
64 'DRIVER' => 'mysqli',
65 'AVAILABLE' => true,
66 '2.0.x' => true,
67 ),
68 'mysql' => array(
69 'LABEL' => 'MySQL',
70 'SCHEMA' => 'mysql',
71 'MODULE' => 'mysql',
72 'DELIM' => ';',
73 'COMMENTS' => 'remove_remarks',
74 'DRIVER' => 'mysql',
75 'AVAILABLE' => true,
76 '2.0.x' => true,
77 ),
78 'mssql' => array(
79 'LABEL' => 'MS SQL Server 2000+',
80 'SCHEMA' => 'mssql',
81 'MODULE' => 'mssql',
82 'DELIM' => 'GO',
83 'COMMENTS' => 'remove_comments',
84 'DRIVER' => 'mssql',
85 'AVAILABLE' => true,
86 '2.0.x' => true,
87 ),
88 'mssql_odbc'=> array(
89 'LABEL' => 'MS SQL Server [ ODBC ]',
90 'SCHEMA' => 'mssql',
91 'MODULE' => 'odbc',
92 'DELIM' => 'GO',
93 'COMMENTS' => 'remove_comments',
94 'DRIVER' => 'mssql_odbc',
95 'AVAILABLE' => true,
96 '2.0.x' => true,
97 ),
98 'oracle' => array(
99 'LABEL' => 'Oracle',
100 'SCHEMA' => 'oracle',
101 'MODULE' => 'oci8',
102 'DELIM' => '/',
103 'COMMENTS' => 'remove_comments',
104 'DRIVER' => 'oracle',
105 'AVAILABLE' => true,
106 '2.0.x' => false,
107 ),
108 'postgres' => array(
109 'LABEL' => 'PostgreSQL 7.x/8.x',
110 'SCHEMA' => 'postgres',
111 'MODULE' => 'pgsql',
112 'DELIM' => ';',
113 'COMMENTS' => 'remove_comments',
114 'DRIVER' => 'postgres',
115 'AVAILABLE' => true,
116 '2.0.x' => true,
117 ),
118 'sqlite' => array(
119 'LABEL' => 'SQLite',
120 'SCHEMA' => 'sqlite',
121 'MODULE' => 'sqlite',
122 'DELIM' => ';',
123 'COMMENTS' => 'remove_remarks',
124 'DRIVER' => 'sqlite',
125 'AVAILABLE' => true,
126 '2.0.x' => false,
127 ),
128 );
129
130 if ($dbms)
131 {
132 if (isset($available_dbms[$dbms]))
133 {
134 $available_dbms = array($dbms => $available_dbms[$dbms]);
135 }
136 else
137 {
138 return array();
139 }
140 }
141
142 // now perform some checks whether they are really available
143 foreach ($available_dbms as $db_name => $db_ary)
144 {
145 if ($only_20x_options && !$db_ary['2.0.x'])
146 {
147 if ($return_unavailable)
148 {
149 $available_dbms[$db_name]['AVAILABLE'] = false;
150 }
151 else
152 {
153 unset($available_dbms[$db_name]);
154 }
155 continue;
156 }
157
158 $dll = $db_ary['MODULE'];
159
160 if (!@extension_loaded($dll))
161 {
162 if (!can_load_dll($dll))
163 {
164 if ($return_unavailable)
165 {
166 $available_dbms[$db_name]['AVAILABLE'] = false;
167 }
168 else
169 {
170 unset($available_dbms[$db_name]);
171 }
172 continue;
173 }
174 }
175 $any_db_support = true;
176 }
177
178 if ($return_unavailable)
179 {
180 $available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
181 }
182 return $available_dbms;
183}
184
185/**
186* Generate the drop down of available database options
187*/
188function dbms_select($default = '', $only_20x_options = false)
189{
190 global $lang;
191
192 $available_dbms = get_available_dbms(false, false, $only_20x_options);
193 $dbms_options = '';
194 foreach ($available_dbms as $dbms_name => $details)
195 {
196 $selected = ($dbms_name == $default) ? ' selected="selected"' : '';
197 $dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
198 }
199 return $dbms_options;
200}
201
202/**
203* Get tables of a database
204*/
205function get_tables($db)
206{
207 switch ($db->sql_layer)
208 {
209 case 'mysql':
210 case 'mysql4':
211 case 'mysqli':
212 $sql = 'SHOW TABLES';
213 break;
214
215 case 'sqlite':
216 $sql = 'SELECT name
217 FROM sqlite_master
218 WHERE type = "table"';
219 break;
220
221 case 'mssql':
222 case 'mssql_odbc':
223 $sql = "SELECT name
224 FROM sysobjects
225 WHERE type='U'";
226 break;
227
228 case 'postgres':
229 $sql = 'SELECT relname
230 FROM pg_stat_user_tables';
231 break;
232
233 case 'firebird':
234 $sql = 'SELECT rdb$relation_name
235 FROM rdb$relations
236 WHERE rdb$view_source is null
237 AND rdb$system_flag = 0';
238 break;
239
240 case 'oracle':
241 $sql = 'SELECT table_name
242 FROM USER_TABLES';
243 break;
244 }
245
246 $result = $db->sql_query($sql);
247
248 $tables = array();
249
250 while ($row = $db->sql_fetchrow($result))
251 {
252 $tables[] = current($row);
253 }
254
255 $db->sql_freeresult($result);
256
257 return $tables;
258}
259
260/**
261* Used to test whether we are able to connect to the database the user has specified
262* and identify any problems (eg there are already tables with the names we want to use
263* @param array $dbms should be of the format of an element of the array returned by {@link get_available_dbms get_available_dbms()}
264* necessary extensions should be loaded already
265*/
266function 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)
267{
268 global $phpbb_root_path, $phpEx, $config, $lang;
269
270 $dbms = $dbms_details['DRIVER'];
271
272 if ($load_dbal)
273 {
274 // Include the DB layer
275 include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
276 }
277
278 // Instantiate it and set return on error true
279 $sql_db = 'dbal_' . $dbms;
280 $db = new $sql_db();
281 $db->sql_return_on_error(true);
282
283 // Check that we actually have a database name before going any further.....
284 if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
285 {
286 $error[] = $lang['INST_ERR_DB_NO_NAME'];
287 return false;
288 }
289
290 // Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea
291 if ($dbms_details['DRIVER'] == 'sqlite' && stripos(phpbb_realpath($dbhost), phpbb_realpath('../')) === 0)
292 {
293 $error[] = $lang['INST_ERR_DB_FORUM_PATH'];
294 return false;
295 }
296
297 // Check the prefix length to ensure that index names are not too long and does not contain invalid characters
298 switch ($dbms_details['DRIVER'])
299 {
300 case 'mysql':
301 case 'mysqli':
302 if (strspn($table_prefix, '-./\\') !== 0)
303 {
304 $error[] = $lang['INST_ERR_PREFIX_INVALID'];
305 return false;
306 }
307
308 // no break;
309
310 case 'postgres':
311 $prefix_length = 36;
312 break;
313
314 case 'mssql':
315 case 'mssql_odbc':
316 $prefix_length = 90;
317 break;
318
319 case 'sqlite':
320 $prefix_length = 200;
321 break;
322
323 case 'firebird':
324 case 'oracle':
325 $prefix_length = 6;
326 break;
327 }
328
329 if (strlen($table_prefix) > $prefix_length)
330 {
331 $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
332 return false;
333 }
334
335 // Try and connect ...
336 if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
337 {
338 $db_error = $db->sql_error();
339 $error[] = $lang['INST_ERR_DB_CONNECT'] . '<br />' . (($db_error['message']) ? $db_error['message'] : $lang['INST_ERR_DB_NO_ERROR']);
340 }
341 else
342 {
343 // Likely matches for an existing phpBB installation
344 if (!$prefix_may_exist)
345 {
346 $temp_prefix = strtolower($table_prefix);
347 $table_ary = array($temp_prefix . 'attachments', $temp_prefix . 'config', $temp_prefix . 'sessions', $temp_prefix . 'topics', $temp_prefix . 'users');
348
349 $tables = get_tables($db);
350 $tables = array_map('strtolower', $tables);
351 $table_intersect = array_intersect($tables, $table_ary);
352
353 if (sizeof($table_intersect))
354 {
355 $error[] = $lang['INST_ERR_PREFIX'];
356 }
357 }
358
359 // Make sure that the user has selected a sensible DBAL for the DBMS actually installed
360 switch ($dbms_details['DRIVER'])
361 {
362 case 'mysqli':
363 if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
364 {
365 $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
366 }
367 break;
368
369 case 'sqlite':
370 if (version_compare(sqlite_libversion(), '2.8.2', '<'))
371 {
372 $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
373 }
374 break;
375
376 case 'firebird':
377 // check the version of FB, use some hackery if we can't get access to the server info
378 if ($db->service_handle !== false && function_exists('ibase_server_info'))
379 {
380 $val = @ibase_server_info($db->service_handle, IBASE_SVC_SERVER_VERSION);
381 preg_match('#V([\d.]+)#', $val, $match);
382 if ($match[1] < 2)
383 {
384 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
385 }
386 $db_info = @ibase_db_info($db->service_handle, $dbname, IBASE_STS_HDR_PAGES);
387
388 preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
389 $page_size = intval($regs[1]);
390 if ($page_size < 8192)
391 {
392 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
393 }
394 }
395 else
396 {
397 $sql = "SELECT *
398 FROM RDB$FUNCTIONS
399 WHERE RDB$SYSTEM_FLAG IS NULL
400 AND RDB$FUNCTION_NAME = 'CHAR_LENGTH'";
401 $result = $db->sql_query($sql);
402 $row = $db->sql_fetchrow($result);
403 $db->sql_freeresult($result);
404
405 // if its a UDF, its too old
406 if ($row)
407 {
408 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
409 }
410 else
411 {
412 $sql = 'SELECT 1 FROM RDB$DATABASE
413 WHERE BIN_AND(10, 1) = 0';
414 $result = $db->sql_query($sql);
415 if (!$result) // This can only fail if BIN_AND is not defined
416 {
417 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
418 }
419 $db->sql_freeresult($result);
420 }
421
422 // Setup the stuff for our random table
423 $char_array = array_merge(range('A', 'Z'), range('0', '9'));
424 $char_len = mt_rand(7, 9);
425 $char_array_len = sizeof($char_array) - 1;
426
427 $final = '';
428
429 for ($i = 0; $i < $char_len; $i++)
430 {
431 $final .= $char_array[mt_rand(0, $char_array_len)];
432 }
433
434 // Create some random table
435 $sql = 'CREATE TABLE ' . $final . " (
436 FIELD1 VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE,
437 FIELD2 INTEGER DEFAULT 0 NOT NULL);";
438 $db->sql_query($sql);
439
440 // Create an index that should fail if the page size is less than 8192
441 $sql = 'CREATE INDEX ' . $final . ' ON ' . $final . '(FIELD1, FIELD2);';
442 $db->sql_query($sql);
443
444 if (ibase_errmsg() !== false)
445 {
446 $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
447 }
448 else
449 {
450 // Kill the old table
451 $db->sql_query('DROP TABLE ' . $final . ';');
452 }
453 unset($final);
454 }
455 break;
456
457 case 'oracle':
458 if ($unicode_check)
459 {
460 $sql = "SELECT *
461 FROM NLS_DATABASE_PARAMETERS
462 WHERE PARAMETER = 'NLS_RDBMS_VERSION'
463 OR PARAMETER = 'NLS_CHARACTERSET'";
464 $result = $db->sql_query($sql);
465
466 while ($row = $db->sql_fetchrow($result))
467 {
468 $stats[$row['parameter']] = $row['value'];
469 }
470 $db->sql_freeresult($result);
471
472 if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<') && $stats['NLS_CHARACTERSET'] !== 'UTF8')
473 {
474 $error[] = $lang['INST_ERR_DB_NO_ORACLE'];
475 }
476 }
477 break;
478
479 case 'postgres':
480 if ($unicode_check)
481 {
482 $sql = "SHOW server_encoding;";
483 $result = $db->sql_query($sql);
484 $row = $db->sql_fetchrow($result);
485 $db->sql_freeresult($result);
486
487 if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
488 {
489 $error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
490 }
491 }
492 break;
493 }
494
495 }
496
497 if ($error_connect && (!isset($error) || !sizeof($error)))
498 {
499 return true;
500 }
501 return false;
502}
503
504/**
505* remove_remarks will strip the sql comment lines out of an uploaded sql file
506*/
507function remove_remarks(&$sql)
508{
509 $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
510}
511
512/**
513* split_sql_file will split an uploaded sql file into single sql statements.
514* Note: expects trim() to have already been run on $sql.
515*/
516function split_sql_file($sql, $delimiter)
517{
518 $sql = str_replace("\r" , '', $sql);
519 $data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
520
521 $data = array_map('trim', $data);
522
523 // The empty case
524 $end_data = end($data);
525
526 if (empty($end_data))
527 {
528 unset($data[key($data)]);
529 }
530
531 return $data;
532}
533
534/**
535* For replacing {L_*} strings with preg_replace_callback
536*/
537function adjust_language_keys_callback($matches)
538{
539 if (!empty($matches[1]))
540 {
541 global $lang, $db;
542
543 return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);
544 }
545}
546
547?>
Note: See TracBrowser for help on using the repository browser.