forked from MightyGorgon/icy_phoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_install.php
309 lines (269 loc) · 7.38 KB
/
functions_install.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/**
*
* @package Icy Phoenix
* @version $Id$
* @copyright (c) 2008 Icy Phoenix
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
*
* @Icy Phoenix is based on phpBB
* @copyright (c) 2008 phpBB Group
*
*/
if (!defined('IN_ICYPHOENIX'))
{
die('Hacking attempt');
}
/**
* Determine if we are able to load a specified PHP module and do so if possible
*/
function can_load_dll($dll)
{
// SQLite2 is a tricky thing, from 5.0.0 it requires PDO; if PDO is not loaded we must state that SQLite is unavailable
// as the installer doesn't understand that the extension has a prerequisite.
if ($dll == 'sqlite' && version_compare(PHP_VERSION, '5.0.0', '>=') && !extension_loaded('pdo'))
{
return false;
}
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;
}
/**
* Returns an array of available DBMS with some data, if a DBMS is specified it will only
* return data for that DBMS and will load its extension if necessary.
*/
function get_available_dbms($dbms = false, $return_unavailable = false, $only_20x_options = false)
{
global $lang;
$available_dbms = array(
'mysqli' => array(
'LABEL' => 'MySQL with MySQLi Extension',
'SCHEMA' => 'mysql_41',
'MODULE' => 'mysqli',
'DELIM' => ';',
'COMMENTS' => 'remove_remarks',
'DRIVER' => 'mysqli',
'AVAILABLE' => true,
'2.0.x' => true,
),
'mysql' => array(
'LABEL' => 'MySQL',
'SCHEMA' => 'mysql',
'MODULE' => 'mysql',
'DELIM' => ';',
'COMMENTS' => 'remove_remarks',
'DRIVER' => 'mysql',
'AVAILABLE' => true,
'2.0.x' => true,
),
);
if ($dbms)
{
if (isset($available_dbms[$dbms]))
{
$available_dbms = array($dbms => $available_dbms[$dbms]);
}
else
{
return array();
}
}
// now perform some checks whether they are really available
foreach ($available_dbms as $db_name => $db_ary)
{
if ($only_20x_options && !$db_ary['2.0.x'])
{
if ($return_unavailable)
{
$available_dbms[$db_name]['AVAILABLE'] = false;
}
else
{
unset($available_dbms[$db_name]);
}
continue;
}
$dll = $db_ary['MODULE'];
if (!@extension_loaded($dll))
{
if (!can_load_dll($dll))
{
if ($return_unavailable)
{
$available_dbms[$db_name]['AVAILABLE'] = false;
}
else
{
unset($available_dbms[$db_name]);
}
continue;
}
}
$any_db_support = true;
}
if ($return_unavailable)
{
$available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
}
return $available_dbms;
}
/**
* Generate the drop down of available database options
*/
function dbms_select($default = '', $only_20x_options = false)
{
global $lang;
$available_dbms = get_available_dbms(false, false, $only_20x_options);
$dbms_options = '';
foreach ($available_dbms as $dbms_name => $details)
{
$selected = ($dbms_name == $default) ? ' selected="selected"' : '';
$dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
}
return $dbms_options;
}
/**
* Get tables of a database
*/
function get_tables($db)
{
switch ($db->sql_layer)
{
case 'mysql':
case 'mysql4':
case 'mysqli':
$sql = 'SHOW TABLES';
break;
}
$result = $db->sql_query($sql);
$tables = array();
while ($row = $db->sql_fetchrow($result))
{
$tables[] = current($row);
}
$db->sql_freeresult($result);
return $tables;
}
/**
* Used to test whether we are able to connect to the database the user has specified
* and identify any problems (eg there are already tables with the names we want to use
* @param array $dbms should be of the format of an element of the array returned by {@link get_available_dbms get_available_dbms()} necessary extensions should be loaded already
*/
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)
{
global $config, $lang;
$dbms = $dbms_details['DRIVER'];
if ($load_dbal)
{
// Include the DB layer
include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
}
// Instantiate it and set return on error true
$sql_db = 'dbal_' . $dbms;
$db = new $sql_db();
$db->sql_return_on_error(true);
// Check that we actually have a database name before going any further.....
if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
{
$error[] = $lang['INST_ERR_DB_NO_NAME'];
return false;
}
// Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea
if ($dbms_details['DRIVER'] == 'sqlite' && stripos(phpbb_realpath($dbhost), phpbb_realpath('../')) === 0)
{
$error[] = $lang['INST_ERR_DB_FORUM_PATH'];
return false;
}
// Check the prefix length to ensure that index names are not too long and does not contain invalid characters
switch ($dbms_details['DRIVER'])
{
case 'mysql':
case 'mysqli':
if (strspn($table_prefix, '-./\\') !== 0)
{
$error[] = $lang['INST_ERR_PREFIX_INVALID'];
return false;
}
break;
}
if (strlen($table_prefix) > $prefix_length)
{
$error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
return false;
}
// Try and connect ...
if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
{
$db_error = $db->sql_error();
$error[] = $lang['INST_ERR_DB_CONNECT'] . '<br />' . (($db_error['message']) ? $db_error['message'] : $lang['INST_ERR_DB_NO_ERROR']);
}
else
{
// Likely matches for an existing phpBB installation
if (!$prefix_may_exist)
{
$temp_prefix = strtolower($table_prefix);
$table_ary = array($temp_prefix . 'attachments', $temp_prefix . 'config', $temp_prefix . 'sessions', $temp_prefix . 'topics', $temp_prefix . 'users');
$tables = get_tables($db);
$tables = array_map('strtolower', $tables);
$table_intersect = array_intersect($tables, $table_ary);
if (sizeof($table_intersect))
{
$error[] = $lang['INST_ERR_PREFIX'];
}
}
// Make sure that the user has selected a sensible DBAL for the DBMS actually installed
switch ($dbms_details['DRIVER'])
{
case 'mysqli':
if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
{
$error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
}
break;
}
}
if ($error_connect && (!isset($error) || !sizeof($error)))
{
return true;
}
return false;
}
/**
* remove_remarks will strip the sql comment lines out of an uploaded sql file
*/
function remove_remarks(&$sql)
{
$sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
}
/**
* split_sql_file will split an uploaded sql file into single sql statements.
* Note: expects trim() to have already been run on $sql.
*/
function split_sql_file($sql, $delimiter)
{
$sql = str_replace("\r" , '', $sql);
$data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
$data = array_map('trim', $data);
// The empty case
$end_data = end($data);
if (empty($end_data))
{
unset($data[key($data)]);
}
return $data;
}
/**
* For replacing {L_*} strings with preg_replace_callback
*/
function adjust_language_keys_callback($matches)
{
if (!empty($matches[1]))
{
global $lang, $db;
return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);
}
}
?>