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