~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/php/phpBB3/includes/functions_install.php

  • Committer: dcoles
  • Date: 2008-02-13 04:10:55 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:443
Added Forum application along with unmodifed version of phpBB3 "Olympus" 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
*
 
4
* @package install
 
5
* @version $Id: functions_install.php,v 1.17 2007/11/19 16:43:59 acydburn Exp $
 
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
*/
 
14
if (!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
*/
 
22
function can_load_dll($dll)
 
23
{
 
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;
 
25
}
 
26
 
 
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.
 
30
*/
 
31
function get_available_dbms($dbms = false, $return_unavailable = false, $only_20x_options = false)
 
32
{
 
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,
 
44
                ),
 
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,
 
54
                ),
 
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,
 
64
                ),
 
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,
 
74
                ),
 
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,
 
84
                ),
 
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,
 
94
                ),
 
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,
 
104
                ),
 
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,
 
114
                ),
 
115
        );
 
116
 
 
117
        if ($dbms)
 
118
        {
 
119
                if (isset($available_dbms[$dbms]))
 
120
                {
 
121
                        $available_dbms = array($dbms => $available_dbms[$dbms]);
 
122
                }
 
123
                else
 
124
                {
 
125
                        return array();
 
126
                }
 
127
        }
 
128
 
 
129
        // now perform some checks whether they are really available
 
130
        foreach ($available_dbms as $db_name => $db_ary)
 
131
        {
 
132
                if ($only_20x_options && !$db_ary['2.0.x'])
 
133
                {
 
134
                        if ($return_unavailable)
 
135
                        {
 
136
                                $available_dbms[$db_name]['AVAILABLE'] = false;
 
137
                        }
 
138
                        else
 
139
                        {
 
140
                                unset($available_dbms[$db_name]);
 
141
                        }
 
142
                        continue;
 
143
                }
 
144
 
 
145
                $dll = $db_ary['MODULE'];
 
146
 
 
147
                if (!@extension_loaded($dll))
 
148
                {
 
149
                        if (!can_load_dll($dll))
 
150
                        {
 
151
                                if ($return_unavailable)
 
152
                                {
 
153
                                        $available_dbms[$db_name]['AVAILABLE'] = false;
 
154
                                }
 
155
                                else
 
156
                                {
 
157
                                        unset($available_dbms[$db_name]);
 
158
                                }
 
159
                                continue;
 
160
                        }
 
161
                }
 
162
                $any_db_support = true;
 
163
        }
 
164
 
 
165
        if ($return_unavailable)
 
166
        {
 
167
                $available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
 
168
        }
 
169
        return $available_dbms;
 
170
}
 
171
 
 
172
/**
 
173
* Generate the drop down of available database options
 
174
*/
 
175
function dbms_select($default = '', $only_20x_options = false)
 
176
{
 
177
        global $lang;
 
178
        
 
179
        $available_dbms = get_available_dbms(false, false, $only_20x_options);
 
180
        $dbms_options = '';
 
181
        foreach ($available_dbms as $dbms_name => $details)
 
182
        {
 
183
                $selected = ($dbms_name == $default) ? ' selected="selected"' : '';
 
184
                $dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
 
185
        }
 
186
        return $dbms_options;
 
187
}
 
188
 
 
189
/**
 
190
* Get tables of a database
 
191
*/
 
192
function get_tables($db)
 
193
{
 
194
        switch ($db->sql_layer)
 
195
        {
 
196
                case 'mysql':
 
197
                case 'mysql4':
 
198
                case 'mysqli':
 
199
                        $sql = 'SHOW TABLES';
 
200
                break;
 
201
 
 
202
                case 'sqlite':
 
203
                        $sql = 'SELECT name
 
204
                                FROM sqlite_master
 
205
                                WHERE type = "table"';
 
206
                break;
 
207
 
 
208
                case 'mssql':
 
209
                case 'mssql_odbc':
 
210
                        $sql = "SELECT name
 
211
                                FROM sysobjects
 
212
                                WHERE type='U'";
 
213
                break;
 
214
 
 
215
                case 'postgres':
 
216
                        $sql = 'SELECT relname
 
217
                                FROM pg_stat_user_tables';
 
218
                break;
 
219
 
 
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;
 
226
 
 
227
                case 'oracle':
 
228
                        $sql = 'SELECT table_name
 
229
                                FROM USER_TABLES';
 
230
                break;
 
231
        }
 
232
 
 
233
        $result = $db->sql_query($sql);
 
234
 
 
235
        $tables = array();
 
236
 
 
237
        while ($row = $db->sql_fetchrow($result))
 
238
        {
 
239
                $tables[] = current($row);
 
240
        }
 
241
 
 
242
        $db->sql_freeresult($result);
 
243
 
 
244
        return $tables;
 
245
}
 
246
 
 
247
/**
 
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
 
252
*/
 
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)
 
254
{
 
255
        global $phpbb_root_path, $phpEx, $config, $lang;
 
256
 
 
257
        $dbms = $dbms_details['DRIVER'];
 
258
 
 
259
        if ($load_dbal)
 
260
        {
 
261
                // Include the DB layer
 
262
                include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
 
263
        }
 
264
 
 
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);
 
269
 
 
270
        // Check that we actually have a database name before going any further.....
 
271
        if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
 
272
        {
 
273
                $error[] = $lang['INST_ERR_DB_NO_NAME'];
 
274
                return false;
 
275
        }
 
276
 
 
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)
 
279
        {
 
280
                $error[] = $lang['INST_ERR_DB_FORUM_PATH'];
 
281
                return false;
 
282
        }
 
283
 
 
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'])
 
286
        {
 
287
                case 'mysql':
 
288
                case 'mysqli':
 
289
                        if (strpos($table_prefix, '-') !== false || strpos($table_prefix, '.') !== false)
 
290
                        {
 
291
                                $error[] = $lang['INST_ERR_PREFIX_INVALID'];
 
292
                                return false;
 
293
                        }
 
294
 
 
295
                // no break;
 
296
 
 
297
                case 'postgres':
 
298
                        $prefix_length = 36;
 
299
                break;
 
300
 
 
301
                case 'mssql':
 
302
                case 'mssql_odbc':
 
303
                        $prefix_length = 90;
 
304
                break;
 
305
 
 
306
                case 'sqlite':
 
307
                        $prefix_length = 200;
 
308
                break;
 
309
 
 
310
                case 'firebird':
 
311
                case 'oracle':
 
312
                        $prefix_length = 6;
 
313
                break;
 
314
        }
 
315
 
 
316
        if (strlen($table_prefix) > $prefix_length)
 
317
        {
 
318
                $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
 
319
                return false;
 
320
        }
 
321
 
 
322
        // Try and connect ...
 
323
        if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
 
324
        {
 
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']);
 
327
        }
 
328
        else
 
329
        {
 
330
                // Likely matches for an existing phpBB installation
 
331
                if (!$prefix_may_exist)
 
332
                {
 
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');
 
335
 
 
336
                        $tables = get_tables($db);
 
337
                        $tables = array_map('strtolower', $tables);
 
338
                        $table_intersect = array_intersect($tables, $table_ary);
 
339
 
 
340
                        if (sizeof($table_intersect))
 
341
                        {
 
342
                                $error[] = $lang['INST_ERR_PREFIX'];
 
343
                        }
 
344
                }
 
345
 
 
346
                // Make sure that the user has selected a sensible DBAL for the DBMS actually installed
 
347
                switch ($dbms_details['DRIVER'])
 
348
                {
 
349
                        case 'mysqli':
 
350
                                if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
 
351
                                {
 
352
                                        $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
 
353
                                }
 
354
                        break;
 
355
 
 
356
                        case 'sqlite':
 
357
                                if (version_compare(sqlite_libversion(), '2.8.2', '<'))
 
358
                                {
 
359
                                        $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
 
360
                                }
 
361
                        break;
 
362
 
 
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'))
 
366
                                {
 
367
                                        $val = @ibase_server_info($db->service_handle, IBASE_SVC_SERVER_VERSION);
 
368
                                        preg_match('#V([\d.]+)#', $val, $match);
 
369
                                        if ($match[1] < 2)
 
370
                                        {
 
371
                                                $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
 
372
                                        }
 
373
                                        $db_info = @ibase_db_info($db->service_handle, $dbname, IBASE_STS_HDR_PAGES);
 
374
 
 
375
                                        preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
 
376
                                        $page_size = intval($regs[1]);
 
377
                                        if ($page_size < 8192)
 
378
                                        {
 
379
                                                $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
 
380
                                        }
 
381
                                }
 
382
                                else
 
383
                                {
 
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);
 
391
 
 
392
                                        // if its a UDF, its too old
 
393
                                        if ($row)
 
394
                                        {
 
395
                                                $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
 
396
                                        }
 
397
                                        else
 
398
                                        {
 
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
 
403
                                                {
 
404
                                                        $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
 
405
                                                }
 
406
                                                $db->sql_freeresult($result);
 
407
                                        }
 
408
 
 
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;
 
413
 
 
414
                                        $final = '';
 
415
 
 
416
                                        for ($i = 0; $i < $char_len; $i++)
 
417
                                        {
 
418
                                                $final .= $char_array[mt_rand(0, $char_array_len)];
 
419
                                        }
 
420
 
 
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);
 
426
 
 
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);
 
430
 
 
431
                                        if (ibase_errmsg() !== false)
 
432
                                        {
 
433
                                                $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
 
434
                                        }
 
435
                                        else
 
436
                                        {
 
437
                                                // Kill the old table
 
438
                                                $db->sql_query('DROP TABLE ' . $final . ';');
 
439
                                        }
 
440
                                        unset($final);
 
441
                                }
 
442
                        break;
 
443
                        
 
444
                        case 'oracle':
 
445
                                if ($unicode_check)
 
446
                                {
 
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);
 
452
 
 
453
                                        while ($row = $db->sql_fetchrow($result))
 
454
                                        {
 
455
                                                $stats[$row['parameter']] = $row['value'];
 
456
                                        }
 
457
                                        $db->sql_freeresult($result);
 
458
 
 
459
                                        if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<') && $stats['NLS_CHARACTERSET'] !== 'UTF8')
 
460
                                        {
 
461
                                                $error[] = $lang['INST_ERR_DB_NO_ORACLE'];
 
462
                                        }
 
463
                                }
 
464
                        break;
 
465
                        
 
466
                        case 'postgres':
 
467
                                if ($unicode_check)
 
468
                                {
 
469
                                        $sql = "SHOW server_encoding;";
 
470
                                        $result = $db->sql_query($sql);
 
471
                                        $row = $db->sql_fetchrow($result);
 
472
                                        $db->sql_freeresult($result);
 
473
 
 
474
                                        if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
 
475
                                        {
 
476
                                                $error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
 
477
                                        }
 
478
                                }
 
479
                        break;
 
480
                }
 
481
 
 
482
        }
 
483
 
 
484
        if ($error_connect && (!isset($error) || !sizeof($error)))
 
485
        {
 
486
                return true;
 
487
        }
 
488
        return false;
 
489
}
 
490
 
 
491
/**
 
492
* remove_remarks will strip the sql comment lines out of an uploaded sql file
 
493
*/
 
494
function remove_remarks(&$sql)
 
495
{
 
496
        $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
 
497
}
 
498
 
 
499
/**
 
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.
 
502
*/
 
503
function split_sql_file($sql, $delimiter)
 
504
{
 
505
        $sql = str_replace("\r" , '', $sql);
 
506
        $data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
 
507
 
 
508
        $data = array_map('trim', $data);
 
509
 
 
510
        // The empty case
 
511
        $end_data = end($data);
 
512
 
 
513
        if (empty($end_data))
 
514
        {
 
515
                unset($data[key($data)]);
 
516
        }
 
517
 
 
518
        return $data;
 
519
}
 
520
 
 
521
/**
 
522
* For replacing {L_*} strings with preg_replace_callback
 
523
*/
 
524
function adjust_language_keys_callback($matches)
 
525
{
 
526
        if (!empty($matches[1]))
 
527
        {
 
528
                global $lang, $db;
 
529
 
 
530
                return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);
 
531
        }
 
532
}
 
533
 
 
534
?>
 
 
b'\\ No newline at end of file'