commit | author | age
|
354978
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| rcube_install.php | |
|
6 |
| | |
e019f2
|
7 |
| This file is part of the Roundcube Webmail package | |
471d55
|
8 |
| Copyright (C) 2008-2012, The Roundcube Dev Team | |
7fe381
|
9 |
| | |
T |
10 |
| Licensed under the GNU General Public License version 3 or | |
|
11 |
| any later version with exceptions for skins & plugins. | |
|
12 |
| See the README file for a full license statement. | |
354978
|
13 |
+-----------------------------------------------------------------------+ |
T |
14 |
*/ |
|
15 |
|
|
16 |
|
|
17 |
/** |
e019f2
|
18 |
* Class to control the installation process of the Roundcube Webmail package |
354978
|
19 |
* |
T |
20 |
* @category Install |
e019f2
|
21 |
* @package Roundcube |
354978
|
22 |
* @author Thomas Bruederli |
T |
23 |
*/ |
|
24 |
class rcube_install |
|
25 |
{ |
|
26 |
var $step; |
237119
|
27 |
var $is_post = false; |
354978
|
28 |
var $failures = 0; |
c5042d
|
29 |
var $config = array(); |
b3f9df
|
30 |
var $configured = false; |
c5042d
|
31 |
var $last_error = null; |
e6bb83
|
32 |
var $db_map = array('pgsql' => 'postgres', 'mysqli' => 'mysql', 'sqlsrv' => 'mssql'); |
ad43e6
|
33 |
var $email_pattern = '([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9])'; |
871ca9
|
34 |
var $bool_config_props = array(); |
e10712
|
35 |
|
e6bb83
|
36 |
var $obsolete_config = array('db_backend', 'double_auth'); |
cbffc2
|
37 |
var $replaced_config = array( |
651c7b
|
38 |
'skin_path' => 'skin', |
AM |
39 |
'locale_string' => 'language', |
|
40 |
'multiple_identities' => 'identities_level', |
2a4135
|
41 |
'addrbook_show_images' => 'show_images', |
651c7b
|
42 |
'imap_root' => 'imap_ns_personal', |
AM |
43 |
'pagesize' => 'mail_pagesize', |
c321a9
|
44 |
'default_imap_folders' => 'default_folders', |
651c7b
|
45 |
'top_posting' => 'reply_mode', |
cbffc2
|
46 |
); |
08ffd9
|
47 |
|
2491c6
|
48 |
// these config options are required for a working system |
e6bb83
|
49 |
var $required_config = array( |
T |
50 |
'db_dsnw', 'db_table_contactgroups', 'db_table_contactgroupmembers', |
c04b23
|
51 |
'des_key', 'session_lifetime', 'support_url', |
e6bb83
|
52 |
); |
08ffd9
|
53 |
|
398bff
|
54 |
// list of supported database drivers |
AM |
55 |
var $supported_dbs = array( |
|
56 |
'MySQL' => 'pdo_mysql', |
|
57 |
'PostgreSQL' => 'pdo_pgsql', |
|
58 |
'SQLite' => 'pdo_sqlite', |
|
59 |
'SQLite (v2)' => 'pdo_sqlite2', |
|
60 |
'SQL Server (SQLSRV)' => 'pdo_sqlsrv', |
|
61 |
'SQL Server (DBLIB)' => 'pdo_dblib', |
|
62 |
); |
|
63 |
|
|
64 |
|
354978
|
65 |
/** |
T |
66 |
* Constructor |
|
67 |
*/ |
|
68 |
function rcube_install() |
|
69 |
{ |
|
70 |
$this->step = intval($_REQUEST['_step']); |
237119
|
71 |
$this->is_post = $_SERVER['REQUEST_METHOD'] == 'POST'; |
354978
|
72 |
} |
d68c90
|
73 |
|
c5042d
|
74 |
/** |
T |
75 |
* Singleton getter |
|
76 |
*/ |
|
77 |
function get_instance() |
|
78 |
{ |
|
79 |
static $inst; |
d68c90
|
80 |
|
c5042d
|
81 |
if (!$inst) |
T |
82 |
$inst = new rcube_install(); |
d68c90
|
83 |
|
c5042d
|
84 |
return $inst; |
T |
85 |
} |
d68c90
|
86 |
|
354978
|
87 |
/** |
c5042d
|
88 |
* Read the default config files and store properties |
354978
|
89 |
*/ |
190e97
|
90 |
function load_defaults() |
354978
|
91 |
{ |
c5042d
|
92 |
$this->_load_config('.php.dist'); |
T |
93 |
} |
|
94 |
|
|
95 |
|
|
96 |
/** |
|
97 |
* Read the local config files and store properties |
|
98 |
*/ |
|
99 |
function load_config() |
|
100 |
{ |
b3f9df
|
101 |
$this->config = array(); |
c5042d
|
102 |
$this->_load_config('.php'); |
b3f9df
|
103 |
$this->configured = !empty($this->config); |
c5042d
|
104 |
} |
T |
105 |
|
|
106 |
/** |
|
107 |
* Read the default config file and store properties |
|
108 |
* @access private |
|
109 |
*/ |
|
110 |
function _load_config($suffix) |
|
111 |
{ |
dc088e
|
112 |
if (is_readable($main_inc = RCUBE_CONFIG_DIR . '/main.inc' . $suffix)) { |
434869
|
113 |
include($main_inc); |
T |
114 |
if (is_array($rcmail_config)) |
|
115 |
$this->config += $rcmail_config; |
354978
|
116 |
} |
dc088e
|
117 |
if (is_readable($db_inc = RCUBE_CONFIG_DIR . '/db.inc'. $suffix)) { |
434869
|
118 |
include($db_inc); |
T |
119 |
if (is_array($rcmail_config)) |
|
120 |
$this->config += $rcmail_config; |
354978
|
121 |
} |
T |
122 |
} |
d68c90
|
123 |
|
AM |
124 |
|
354978
|
125 |
/** |
T |
126 |
* Getter for a certain config property |
|
127 |
* |
|
128 |
* @param string Property name |
ad43e6
|
129 |
* @param string Default value |
354978
|
130 |
* @return string The property value |
T |
131 |
*/ |
fa7539
|
132 |
function getprop($name, $default = '') |
354978
|
133 |
{ |
b77d0d
|
134 |
$value = $this->config[$name]; |
d68c90
|
135 |
|
ccb412
|
136 |
if ($name == 'des_key' && !$this->configured && !isset($_REQUEST["_$name"])) |
807d17
|
137 |
$value = rcube_install::random_key(24); |
d68c90
|
138 |
|
fa7539
|
139 |
return $value !== null && $value !== '' ? $value : $default; |
354978
|
140 |
} |
403f0b
|
141 |
|
A |
142 |
|
354978
|
143 |
/** |
T |
144 |
* Take the default config file and replace the parameters |
|
145 |
* with the submitted form data |
|
146 |
* |
|
147 |
* @param string Which config file (either 'main' or 'db') |
|
148 |
* @return string The complete config file content |
|
149 |
*/ |
e10712
|
150 |
function create_config($which, $force = false) |
354978
|
151 |
{ |
dc088e
|
152 |
$out = @file_get_contents(RCUBE_CONFIG_DIR . "/{$which}.inc.php.dist"); |
403f0b
|
153 |
|
354978
|
154 |
if (!$out) |
2c3d81
|
155 |
return '[Warning: could not read the config template file]'; |
0c3bde
|
156 |
|
c5042d
|
157 |
foreach ($this->config as $prop => $default) { |
403f0b
|
158 |
|
0829b7
|
159 |
$is_default = !isset($_POST["_$prop"]); |
A |
160 |
$value = !$is_default || $this->bool_config_props[$prop] ? $_POST["_$prop"] : $default; |
403f0b
|
161 |
|
354978
|
162 |
// convert some form data |
0829b7
|
163 |
if ($prop == 'debug_level' && !$is_default) { |
A |
164 |
if (is_array($value)) { |
|
165 |
$val = 0; |
7d7f67
|
166 |
foreach ($value as $dbgval) |
b77d0d
|
167 |
$val += intval($dbgval); |
0829b7
|
168 |
$value = $val; |
A |
169 |
} |
354978
|
170 |
} |
0c3bde
|
171 |
else if ($which == 'db' && $prop == 'db_dsnw' && !empty($_POST['_dbtype'])) { |
237119
|
172 |
if ($_POST['_dbtype'] == 'sqlite') |
T |
173 |
$value = sprintf('%s://%s?mode=0646', $_POST['_dbtype'], $_POST['_dbname']{0} == '/' ? '/' . $_POST['_dbname'] : $_POST['_dbname']); |
0829b7
|
174 |
else if ($_POST['_dbtype']) |
b61965
|
175 |
$value = sprintf('%s://%s:%s@%s/%s', $_POST['_dbtype'], |
7d7f67
|
176 |
rawurlencode($_POST['_dbuser']), rawurlencode($_POST['_dbpass']), $_POST['_dbhost'], $_POST['_dbname']); |
c5042d
|
177 |
} |
T |
178 |
else if ($prop == 'smtp_auth_type' && $value == '0') { |
|
179 |
$value = ''; |
|
180 |
} |
|
181 |
else if ($prop == 'default_host' && is_array($value)) { |
807d17
|
182 |
$value = rcube_install::_clean_array($value); |
c5042d
|
183 |
if (count($value) <= 1) |
T |
184 |
$value = $value[0]; |
|
185 |
} |
08ffd9
|
186 |
else if ($prop == 'mail_pagesize' || $prop == 'addressbook_pagesize') { |
ccb412
|
187 |
$value = max(2, intval($value)); |
T |
188 |
} |
c5042d
|
189 |
else if ($prop == 'smtp_user' && !empty($_POST['_smtp_user_u'])) { |
T |
190 |
$value = '%u'; |
|
191 |
} |
|
192 |
else if ($prop == 'smtp_pass' && !empty($_POST['_smtp_user_u'])) { |
|
193 |
$value = '%p'; |
|
194 |
} |
c321a9
|
195 |
else if ($prop == 'default_folders') { |
d68c90
|
196 |
$value = array(); |
AM |
197 |
foreach ($this->config['default_folders'] as $_folder) { |
|
198 |
switch ($_folder) { |
|
199 |
case 'Drafts': $_folder = $this->config['drafts_mbox']; break; |
|
200 |
case 'Sent': $_folder = $this->config['sent_mbox']; break; |
|
201 |
case 'Junk': $_folder = $this->config['junk_mbox']; break; |
|
202 |
case 'Trash': $_folder = $this->config['trash_mbox']; break; |
569654
|
203 |
} |
d68c90
|
204 |
if (!in_array($_folder, $value)) |
AM |
205 |
$value[] = $_folder; |
569654
|
206 |
} |
A |
207 |
} |
c5042d
|
208 |
else if (is_bool($default)) { |
27564f
|
209 |
$value = (bool)$value; |
T |
210 |
} |
|
211 |
else if (is_numeric($value)) { |
|
212 |
$value = intval($value); |
c5042d
|
213 |
} |
403f0b
|
214 |
|
c5042d
|
215 |
// skip this property |
403f0b
|
216 |
if (!$force && !$this->configured && ($value == $default)) |
c5042d
|
217 |
continue; |
b77d0d
|
218 |
|
A |
219 |
// save change |
|
220 |
$this->config[$prop] = $value; |
|
221 |
|
354978
|
222 |
// replace the matching line in config file |
T |
223 |
$out = preg_replace( |
|
224 |
'/(\$rcmail_config\[\''.preg_quote($prop).'\'\])\s+=\s+(.+);/Uie', |
0829b7
|
225 |
"'\\1 = ' . rcube_install::_dump_var(\$value, \$prop) . ';'", |
354978
|
226 |
$out); |
T |
227 |
} |
0c3bde
|
228 |
|
967b34
|
229 |
return trim($out); |
c5042d
|
230 |
} |
e10712
|
231 |
|
T |
232 |
|
|
233 |
/** |
|
234 |
* Check the current configuration for missing properties |
|
235 |
* and deprecated or obsolete settings |
|
236 |
* |
|
237 |
* @return array List with problems detected |
|
238 |
*/ |
|
239 |
function check_config() |
|
240 |
{ |
|
241 |
$this->config = array(); |
|
242 |
$this->load_defaults(); |
|
243 |
$defaults = $this->config; |
d68c90
|
244 |
|
e10712
|
245 |
$this->load_config(); |
T |
246 |
if (!$this->configured) |
|
247 |
return null; |
d68c90
|
248 |
|
e10712
|
249 |
$out = $seen = array(); |
f8c06e
|
250 |
$required = array_flip($this->required_config); |
d68c90
|
251 |
|
cbffc2
|
252 |
// iterate over the current configuration |
e10712
|
253 |
foreach ($this->config as $prop => $value) { |
T |
254 |
if ($replacement = $this->replaced_config[$prop]) { |
|
255 |
$out['replaced'][] = array('prop' => $prop, 'replacement' => $replacement); |
|
256 |
$seen[$replacement] = true; |
|
257 |
} |
|
258 |
else if (!$seen[$prop] && in_array($prop, $this->obsolete_config)) { |
|
259 |
$out['obsolete'][] = array('prop' => $prop); |
|
260 |
$seen[$prop] = true; |
|
261 |
} |
|
262 |
} |
d68c90
|
263 |
|
e10712
|
264 |
// iterate over default config |
T |
265 |
foreach ($defaults as $prop => $value) { |
c04b23
|
266 |
if (!isset($seen[$prop]) && isset($required[$prop]) && !(is_bool($this->config[$prop]) || strlen($this->config[$prop]))) |
e10712
|
267 |
$out['missing'][] = array('prop' => $prop); |
T |
268 |
} |
f8c06e
|
269 |
|
871ca9
|
270 |
// check config dependencies and contradictions |
T |
271 |
if ($this->config['enable_spellcheck'] && $this->config['spellcheck_engine'] == 'pspell') { |
|
272 |
if (!extension_loaded('pspell')) { |
|
273 |
$out['dependencies'][] = array('prop' => 'spellcheck_engine', |
|
274 |
'explain' => 'This requires the <tt>pspell</tt> extension which could not be loaded.'); |
|
275 |
} |
924b1a
|
276 |
else if (!empty($this->config['spellcheck_languages'])) { |
01a8c5
|
277 |
foreach ($this->config['spellcheck_languages'] as $lang => $descr) |
471d55
|
278 |
if (!@pspell_new($lang)) |
01a8c5
|
279 |
$out['dependencies'][] = array('prop' => 'spellcheck_languages', |
T |
280 |
'explain' => "You are missing pspell support for language $lang ($descr)"); |
871ca9
|
281 |
} |
T |
282 |
} |
d68c90
|
283 |
|
871ca9
|
284 |
if ($this->config['log_driver'] == 'syslog') { |
T |
285 |
if (!function_exists('openlog')) { |
|
286 |
$out['dependencies'][] = array('prop' => 'log_driver', |
|
287 |
'explain' => 'This requires the <tt>sylog</tt> extension which could not be loaded.'); |
|
288 |
} |
|
289 |
if (empty($this->config['syslog_id'])) { |
|
290 |
$out['dependencies'][] = array('prop' => 'syslog_id', |
|
291 |
'explain' => 'Using <tt>syslog</tt> for logging requires a syslog ID to be configured'); |
|
292 |
} |
5f25a1
|
293 |
} |
d68c90
|
294 |
|
5f25a1
|
295 |
// check ldap_public sources having global_search enabled |
T |
296 |
if (is_array($this->config['ldap_public']) && !is_array($this->config['autocomplete_addressbooks'])) { |
|
297 |
foreach ($this->config['ldap_public'] as $ldap_public) { |
|
298 |
if ($ldap_public['global_search']) { |
|
299 |
$out['replaced'][] = array('prop' => 'ldap_public::global_search', 'replacement' => 'autocomplete_addressbooks'); |
|
300 |
break; |
|
301 |
} |
|
302 |
} |
871ca9
|
303 |
} |
d68c90
|
304 |
|
e10712
|
305 |
return $out; |
T |
306 |
} |
d68c90
|
307 |
|
AM |
308 |
|
e10712
|
309 |
/** |
T |
310 |
* Merge the current configuration with the defaults |
|
311 |
* and copy replaced values to the new options. |
|
312 |
*/ |
|
313 |
function merge_config() |
|
314 |
{ |
|
315 |
$current = $this->config; |
|
316 |
$this->config = array(); |
|
317 |
$this->load_defaults(); |
0829b7
|
318 |
|
e6bb83
|
319 |
foreach ($this->replaced_config as $prop => $replacement) { |
e10712
|
320 |
if (isset($current[$prop])) { |
T |
321 |
if ($prop == 'skin_path') |
|
322 |
$this->config[$replacement] = preg_replace('#skins/(\w+)/?$#', '\\1', $current[$prop]); |
cbffc2
|
323 |
else if ($prop == 'multiple_identities') |
T |
324 |
$this->config[$replacement] = $current[$prop] ? 2 : 0; |
e10712
|
325 |
else |
T |
326 |
$this->config[$replacement] = $current[$prop]; |
e6bb83
|
327 |
} |
T |
328 |
unset($current[$prop]); |
e10712
|
329 |
} |
d68c90
|
330 |
|
e10712
|
331 |
foreach ($this->obsolete_config as $prop) { |
T |
332 |
unset($current[$prop]); |
|
333 |
} |
d68c90
|
334 |
|
5f25a1
|
335 |
// add all ldap_public sources having global_search enabled to autocomplete_addressbooks |
T |
336 |
if (is_array($current['ldap_public'])) { |
|
337 |
foreach ($current['ldap_public'] as $key => $ldap_public) { |
|
338 |
if ($ldap_public['global_search']) { |
|
339 |
$this->config['autocomplete_addressbooks'][] = $key; |
|
340 |
unset($current['ldap_public'][$key]['global_search']); |
|
341 |
} |
|
342 |
} |
|
343 |
} |
d68c90
|
344 |
|
5f25a1
|
345 |
$this->config = array_merge($this->config, $current); |
0829b7
|
346 |
|
5f25a1
|
347 |
foreach ((array)$current['ldap_public'] as $key => $values) { |
T |
348 |
$this->config['ldap_public'][$key] = $current['ldap_public'][$key]; |
|
349 |
} |
e10712
|
350 |
} |
d68c90
|
351 |
|
2491c6
|
352 |
/** |
T |
353 |
* Compare the local database schema with the reference schema |
e019f2
|
354 |
* required for this version of Roundcube |
2491c6
|
355 |
* |
T |
356 |
* @param boolean True if the schema schould be updated |
|
357 |
* @return boolean True if the schema is up-to-date, false if not or an error occured |
|
358 |
*/ |
|
359 |
function db_schema_check($DB, $update = false) |
|
360 |
{ |
|
361 |
if (!$this->configured) |
|
362 |
return false; |
d68c90
|
363 |
|
e6bb83
|
364 |
// read reference schema from mysql.initial.sql |
T |
365 |
$db_schema = $this->db_read_schema(INSTALL_PATH . 'SQL/mysql.initial.sql'); |
2491c6
|
366 |
$errors = array(); |
d68c90
|
367 |
|
2491c6
|
368 |
// check list of tables |
T |
369 |
$existing_tables = $DB->list_tables(); |
f6ee6f
|
370 |
|
2491c6
|
371 |
foreach ($db_schema as $table => $cols) { |
f6ee6f
|
372 |
$table = !empty($this->config['db_table_'.$table]) ? $this->config['db_table_'.$table] : $table; |
e6bb83
|
373 |
if (!in_array($table, $existing_tables)) { |
T |
374 |
$errors[] = "Missing table '".$table."'"; |
|
375 |
} |
|
376 |
else { // compare cols |
|
377 |
$db_cols = $DB->list_cols($table); |
|
378 |
$diff = array_diff(array_keys($cols), $db_cols); |
|
379 |
if (!empty($diff)) |
|
380 |
$errors[] = "Missing columns in table '$table': " . join(',', $diff); |
|
381 |
} |
2491c6
|
382 |
} |
398bff
|
383 |
|
2491c6
|
384 |
return !empty($errors) ? $errors : false; |
T |
385 |
} |
e6bb83
|
386 |
|
T |
387 |
/** |
|
388 |
* Utility function to read database schema from an .sql file |
|
389 |
*/ |
|
390 |
private function db_read_schema($schemafile) |
|
391 |
{ |
|
392 |
$lines = file($schemafile); |
|
393 |
$table_block = false; |
|
394 |
$schema = array(); |
|
395 |
foreach ($lines as $line) { |
|
396 |
if (preg_match('/^\s*create table `?([a-z0-9_]+)`?/i', $line, $m)) { |
|
397 |
$table_block = $m[1]; |
|
398 |
} |
|
399 |
else if ($table_block && preg_match('/^\s*`?([a-z0-9_-]+)`?\s+([a-z]+)/', $line, $m)) { |
|
400 |
$col = $m[1]; |
|
401 |
if (!in_array(strtoupper($col), array('PRIMARY','KEY','INDEX','UNIQUE','CONSTRAINT','REFERENCES','FOREIGN'))) { |
|
402 |
$schema[$table_block][$col] = $m[2]; |
|
403 |
} |
|
404 |
} |
|
405 |
} |
398bff
|
406 |
|
e6bb83
|
407 |
return $schema; |
871ca9
|
408 |
} |
5fed07
|
409 |
|
AM |
410 |
|
871ca9
|
411 |
/** |
c5042d
|
412 |
* Getter for the last error message |
T |
413 |
* |
|
414 |
* @return string Error message or null if none exists |
|
415 |
*/ |
|
416 |
function get_error() |
|
417 |
{ |
|
418 |
return $this->last_error['message']; |
354978
|
419 |
} |
5fed07
|
420 |
|
AM |
421 |
|
354978
|
422 |
/** |
112c54
|
423 |
* Return a list with all imap hosts configured |
T |
424 |
* |
|
425 |
* @return array Clean list with imap hosts |
|
426 |
*/ |
|
427 |
function get_hostlist() |
|
428 |
{ |
|
429 |
$default_hosts = (array)$this->getprop('default_host'); |
|
430 |
$out = array(); |
5fed07
|
431 |
|
112c54
|
432 |
foreach ($default_hosts as $key => $name) { |
T |
433 |
if (!empty($name)) |
058eb6
|
434 |
$out[] = rcube_parse_host(is_numeric($key) ? $name : $key); |
112c54
|
435 |
} |
5fed07
|
436 |
|
112c54
|
437 |
return $out; |
e6bb83
|
438 |
} |
7177b5
|
439 |
|
e6bb83
|
440 |
/** |
T |
441 |
* Create a HTML dropdown to select a previous version of Roundcube |
|
442 |
*/ |
|
443 |
function versions_select($attrib = array()) |
|
444 |
{ |
|
445 |
$select = new html_select($attrib); |
7177b5
|
446 |
$select->add(array( |
A |
447 |
'0.1-stable', '0.1.1', |
|
448 |
'0.2-alpha', '0.2-beta', '0.2-stable', |
|
449 |
'0.3-stable', '0.3.1', |
|
450 |
'0.4-beta', '0.4.2', |
|
451 |
'0.5-beta', '0.5', '0.5.1', |
|
452 |
'0.6-beta', '0.6', |
577bf4
|
453 |
'0.7-beta', '0.7', '0.7.1', '0.7.2', '0.7.3', |
82d875
|
454 |
'0.8-beta', '0.8-rc', '0.8.0', '0.8.1', '0.8.2', '0.8.3', '0.8.4', |
7177b5
|
455 |
)); |
e6bb83
|
456 |
return $select; |
112c54
|
457 |
} |
7177b5
|
458 |
|
e49064
|
459 |
/** |
T |
460 |
* Return a list with available subfolders of the skin directory |
|
461 |
*/ |
|
462 |
function list_skins() |
|
463 |
{ |
|
464 |
$skins = array(); |
|
465 |
$skindir = INSTALL_PATH . 'skins/'; |
|
466 |
foreach (glob($skindir . '*') as $path) { |
|
467 |
if (is_dir($path) && is_readable($path)) { |
|
468 |
$skins[] = substr($path, strlen($skindir)); |
|
469 |
} |
|
470 |
} |
|
471 |
return $skins; |
|
472 |
} |
5fed07
|
473 |
|
112c54
|
474 |
/** |
354978
|
475 |
* Display OK status |
T |
476 |
* |
|
477 |
* @param string Test name |
|
478 |
* @param string Confirm message |
|
479 |
*/ |
|
480 |
function pass($name, $message = '') |
|
481 |
{ |
|
482 |
echo Q($name) . ': <span class="success">OK</span>'; |
6557d3
|
483 |
$this->_showhint($message); |
354978
|
484 |
} |
5fed07
|
485 |
|
AM |
486 |
|
354978
|
487 |
/** |
T |
488 |
* Display an error status and increase failure count |
|
489 |
* |
|
490 |
* @param string Test name |
|
491 |
* @param string Error message |
|
492 |
* @param string URL for details |
|
493 |
*/ |
|
494 |
function fail($name, $message = '', $url = '') |
|
495 |
{ |
|
496 |
$this->failures++; |
5fed07
|
497 |
|
354978
|
498 |
echo Q($name) . ': <span class="fail">NOT OK</span>'; |
6557d3
|
499 |
$this->_showhint($message, $url); |
354978
|
500 |
} |
11e670
|
501 |
|
A |
502 |
|
|
503 |
/** |
|
504 |
* Display an error status for optional settings/features |
|
505 |
* |
|
506 |
* @param string Test name |
|
507 |
* @param string Error message |
|
508 |
* @param string URL for details |
|
509 |
*/ |
|
510 |
function optfail($name, $message = '', $url = '') |
|
511 |
{ |
|
512 |
echo Q($name) . ': <span class="na">NOT OK</span>'; |
|
513 |
$this->_showhint($message, $url); |
|
514 |
} |
5fed07
|
515 |
|
AM |
516 |
|
354978
|
517 |
/** |
T |
518 |
* Display warning status |
|
519 |
* |
|
520 |
* @param string Test name |
|
521 |
* @param string Warning message |
|
522 |
* @param string URL for details |
|
523 |
*/ |
6557d3
|
524 |
function na($name, $message = '', $url = '') |
354978
|
525 |
{ |
6557d3
|
526 |
echo Q($name) . ': <span class="na">NOT AVAILABLE</span>'; |
T |
527 |
$this->_showhint($message, $url); |
|
528 |
} |
5fed07
|
529 |
|
AM |
530 |
|
6557d3
|
531 |
function _showhint($message, $url = '') |
T |
532 |
{ |
|
533 |
$hint = Q($message); |
5fed07
|
534 |
|
354978
|
535 |
if ($url) |
6557d3
|
536 |
$hint .= ($hint ? '; ' : '') . 'See <a href="' . Q($url) . '" target="_blank">' . Q($url) . '</a>'; |
5fed07
|
537 |
|
6557d3
|
538 |
if ($hint) |
T |
539 |
echo '<span class="indent">(' . $hint . ')</span>'; |
354978
|
540 |
} |
5fed07
|
541 |
|
AM |
542 |
|
5f25a1
|
543 |
static function _clean_array($arr) |
c5042d
|
544 |
{ |
T |
545 |
$out = array(); |
5fed07
|
546 |
|
5f25a1
|
547 |
foreach (array_unique($arr) as $k => $val) { |
T |
548 |
if (!empty($val)) { |
|
549 |
if (is_numeric($k)) |
|
550 |
$out[] = $val; |
|
551 |
else |
|
552 |
$out[$k] = $val; |
|
553 |
} |
|
554 |
} |
5fed07
|
555 |
|
c5042d
|
556 |
return $out; |
T |
557 |
} |
5fed07
|
558 |
|
AM |
559 |
|
0829b7
|
560 |
static function _dump_var($var, $name=null) { |
A |
561 |
// special values |
|
562 |
switch ($name) { |
|
563 |
case 'syslog_facility': |
|
564 |
$list = array(32 => 'LOG_AUTH', 80 => 'LOG_AUTHPRIV', 72 => ' LOG_CRON', |
|
565 |
24 => 'LOG_DAEMON', 0 => 'LOG_KERN', 128 => 'LOG_LOCAL0', |
|
566 |
136 => 'LOG_LOCAL1', 144 => 'LOG_LOCAL2', 152 => 'LOG_LOCAL3', |
|
567 |
160 => 'LOG_LOCAL4', 168 => 'LOG_LOCAL5', 176 => 'LOG_LOCAL6', |
|
568 |
184 => 'LOG_LOCAL7', 48 => 'LOG_LPR', 16 => 'LOG_MAIL', |
|
569 |
56 => 'LOG_NEWS', 40 => 'LOG_SYSLOG', 8 => 'LOG_USER', 64 => 'LOG_UUCP'); |
|
570 |
if ($val = $list[$var]) |
|
571 |
return $val; |
|
572 |
break; |
|
573 |
} |
|
574 |
|
|
575 |
|
5f25a1
|
576 |
if (is_array($var)) { |
T |
577 |
if (empty($var)) { |
|
578 |
return 'array()'; |
|
579 |
} |
|
580 |
else { // check if all keys are numeric |
|
581 |
$isnum = true; |
|
582 |
foreach ($var as $key => $value) { |
|
583 |
if (!is_numeric($key)) { |
|
584 |
$isnum = false; |
|
585 |
break; |
|
586 |
} |
|
587 |
} |
5fed07
|
588 |
|
5f25a1
|
589 |
if ($isnum) |
T |
590 |
return 'array(' . join(', ', array_map(array('rcube_install', '_dump_var'), $var)) . ')'; |
|
591 |
} |
|
592 |
} |
5fed07
|
593 |
|
5f25a1
|
594 |
return var_export($var, true); |
T |
595 |
} |
5fed07
|
596 |
|
AM |
597 |
|
190e97
|
598 |
/** |
T |
599 |
* Initialize the database with the according schema |
|
600 |
* |
|
601 |
* @param object rcube_db Database connection |
|
602 |
* @return boolen True on success, False on error |
|
603 |
*/ |
|
604 |
function init_db($DB) |
|
605 |
{ |
e6bb83
|
606 |
$engine = isset($this->db_map[$DB->db_provider]) ? $this->db_map[$DB->db_provider] : $DB->db_provider; |
5fed07
|
607 |
|
190e97
|
608 |
// read schema file from /SQL/* |
e6bb83
|
609 |
$fname = INSTALL_PATH . "SQL/$engine.initial.sql"; |
T |
610 |
if ($sql = @file_get_contents($fname)) { |
|
611 |
$this->exec_sql($sql, $DB); |
190e97
|
612 |
} |
T |
613 |
else { |
|
614 |
$this->fail('DB Schema', "Cannot read the schema file: $fname"); |
|
615 |
return false; |
|
616 |
} |
5fed07
|
617 |
|
190e97
|
618 |
if ($err = $this->get_error()) { |
T |
619 |
$this->fail('DB Schema', "Error creating database schema: $err"); |
|
620 |
return false; |
|
621 |
} |
|
622 |
|
|
623 |
return true; |
|
624 |
} |
5fed07
|
625 |
|
AM |
626 |
|
e6bb83
|
627 |
/** |
T |
628 |
* Update database with SQL statements from SQL/*.update.sql |
|
629 |
* |
|
630 |
* @param object rcube_db Database connection |
|
631 |
* @param string Version to update from |
|
632 |
* @return boolen True on success, False on error |
|
633 |
*/ |
|
634 |
function update_db($DB, $version) |
|
635 |
{ |
|
636 |
$version = strtolower($version); |
|
637 |
$engine = isset($this->db_map[$DB->db_provider]) ? $this->db_map[$DB->db_provider] : $DB->db_provider; |
5fed07
|
638 |
|
e6bb83
|
639 |
// read schema file from /SQL/* |
T |
640 |
$fname = INSTALL_PATH . "SQL/$engine.update.sql"; |
|
641 |
if ($lines = @file($fname, FILE_SKIP_EMPTY_LINES)) { |
|
642 |
$from = false; $sql = ''; |
|
643 |
foreach ($lines as $line) { |
|
644 |
$is_comment = preg_match('/^--/', $line); |
|
645 |
if (!$from && $is_comment && preg_match('/from version\s([0-9.]+[a-z-]*)/', $line, $m)) { |
|
646 |
$v = strtolower($m[1]); |
|
647 |
if ($v == $version || version_compare($version, $v, '<=')) |
|
648 |
$from = true; |
|
649 |
} |
|
650 |
if ($from && !$is_comment) |
|
651 |
$sql .= $line. "\n"; |
|
652 |
} |
5fed07
|
653 |
|
e6bb83
|
654 |
if ($sql) |
T |
655 |
$this->exec_sql($sql, $DB); |
|
656 |
} |
|
657 |
else { |
|
658 |
$this->fail('DB Schema', "Cannot read the update file: $fname"); |
|
659 |
return false; |
|
660 |
} |
5fed07
|
661 |
|
e6bb83
|
662 |
if ($err = $this->get_error()) { |
T |
663 |
$this->fail('DB Schema', "Error updating database: $err"); |
|
664 |
return false; |
|
665 |
} |
|
666 |
|
|
667 |
return true; |
|
668 |
} |
5fed07
|
669 |
|
AM |
670 |
|
e6bb83
|
671 |
/** |
T |
672 |
* Execute the given SQL queries on the database connection |
|
673 |
* |
|
674 |
* @param string SQL queries to execute |
|
675 |
* @param object rcube_db Database connection |
|
676 |
* @return boolen True on success, False on error |
|
677 |
*/ |
|
678 |
function exec_sql($sql, $DB) |
|
679 |
{ |
|
680 |
$buff = ''; |
|
681 |
foreach (explode("\n", $sql) as $line) { |
|
682 |
if (preg_match('/^--/', $line) || trim($line) == '') |
|
683 |
continue; |
5fed07
|
684 |
|
e6bb83
|
685 |
$buff .= $line . "\n"; |
T |
686 |
if (preg_match('/(;|^GO)$/', trim($line))) { |
|
687 |
$DB->query($buff); |
|
688 |
$buff = ''; |
|
689 |
if ($DB->is_error()) |
|
690 |
break; |
|
691 |
} |
|
692 |
} |
5fed07
|
693 |
|
e6bb83
|
694 |
return !$DB->is_error(); |
T |
695 |
} |
5fed07
|
696 |
|
AM |
697 |
|
c5042d
|
698 |
/** |
e019f2
|
699 |
* Handler for Roundcube errors |
c5042d
|
700 |
*/ |
T |
701 |
function raise_error($p) |
|
702 |
{ |
|
703 |
$this->last_error = $p; |
|
704 |
} |
5fed07
|
705 |
|
AM |
706 |
|
354978
|
707 |
/** |
T |
708 |
* Generarte a ramdom string to be used as encryption key |
|
709 |
* |
|
710 |
* @param int Key length |
|
711 |
* @return string The generated random string |
|
712 |
* @static |
|
713 |
*/ |
|
714 |
function random_key($length) |
|
715 |
{ |
|
716 |
$alpha = 'ABCDEFGHIJKLMNOPQERSTUVXYZabcdefghijklmnopqrtsuvwxyz0123456789+*%&?!$-_='; |
|
717 |
$out = ''; |
5fed07
|
718 |
|
354978
|
719 |
for ($i=0; $i < $length; $i++) |
T |
720 |
$out .= $alpha{rand(0, strlen($alpha)-1)}; |
5fed07
|
721 |
|
354978
|
722 |
return $out; |
T |
723 |
} |
5fed07
|
724 |
|
c5042d
|
725 |
} |
T |
726 |
|