Aleksander Machniak
2016-03-14 126d099e8314eabb3f4e1dcb53b01e00f44916e8
commit | author | age
354978 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
eea11e 5  | rcmail_install.php                                                    |
354978 6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail package                    |
eea11e 8  | Copyright (C) 2008-2014, 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  */
eea11e 24 class rcmail_install
354978 25 {
T 26   var $step;
237119 27   var $is_post = false;
354978 28   var $failures = 0;
c5042d 29   var $config = array();
b3f9df 30   var $configured = false;
9bacb2 31   var $legacy_config = false;
c5042d 32   var $last_error = null;
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
9bacb2 36   var $local_config = array('db_dsnw', 'default_host', 'support_url', 'des_key', 'plugins');
68eb18 37   var $obsolete_config = array('db_backend', 'db_max_length', 'double_auth');
cbffc2 38   var $replaced_config = array(
651c7b 39     'skin_path'            => 'skin',
AM 40     'locale_string'        => 'language',
41     'multiple_identities'  => 'identities_level',
2a4135 42     'addrbook_show_images' => 'show_images',
651c7b 43     'imap_root'            => 'imap_ns_personal',
AM 44     'pagesize'             => 'mail_pagesize',
45     'top_posting'          => 'reply_mode',
68eb18 46     'keep_alive'           => 'refresh_interval',
TB 47     'min_keep_alive'       => 'min_refresh_interval',
e6bb83 48   );
08ffd9 49
398bff 50   // list of supported database drivers
AM 51   var $supported_dbs = array(
52     'MySQL'               => 'pdo_mysql',
53     'PostgreSQL'          => 'pdo_pgsql',
54     'SQLite'              => 'pdo_sqlite',
55     'SQLite (v2)'         => 'pdo_sqlite2',
56     'SQL Server (SQLSRV)' => 'pdo_sqlsrv',
57     'SQL Server (DBLIB)'  => 'pdo_dblib',
c23457 58     'Oracle'              => 'oci8',
398bff 59   );
AM 60
61
354978 62   /**
T 63    * Constructor
64    */
66d309 65   function __construct()
354978 66   {
T 67     $this->step = intval($_REQUEST['_step']);
237119 68     $this->is_post = $_SERVER['REQUEST_METHOD'] == 'POST';
354978 69   }
d68c90 70
c5042d 71   /**
T 72    * Singleton getter
73    */
66d309 74   static function get_instance()
c5042d 75   {
T 76     static $inst;
d68c90 77
c5042d 78     if (!$inst)
eea11e 79       $inst = new rcmail_install();
d68c90 80
c5042d 81     return $inst;
T 82   }
d68c90 83
354978 84   /**
c5042d 85    * Read the local config files and store properties
T 86    */
87   function load_config()
88   {
461a30 89     // defaults
AM 90     if ($config = $this->load_config_file(RCUBE_CONFIG_DIR . 'defaults.inc.php')) {
91         $this->config = (array) $config;
92         $this->defaults = $this->config;
93     }
94
95     $config = null;
96
97     // config
98     if ($config = $this->load_config_file(RCUBE_CONFIG_DIR . 'config.inc.php')) {
99         $this->config = array_merge($this->config, $config);
100     }
101     else {
102       if ($config = $this->load_config_file(RCUBE_CONFIG_DIR . 'main.inc.php')) {
103         $this->config = array_merge($this->config, $config);
d67074 104         $this->legacy_config = true;
461a30 105       }
AM 106       if ($config = $this->load_config_file(RCUBE_CONFIG_DIR . 'db.inc.php')) {
107         $this->config = array_merge($this->config, $config);
d67074 108         $this->legacy_config = true;
461a30 109       }
AM 110     }
111
112     $this->configured = !empty($config);
c5042d 113   }
T 114
115   /**
116    * Read the default config file and store properties
117    */
461a30 118   public function load_config_file($file)
c5042d 119   {
461a30 120     if (is_readable($file)) {
AM 121       include $file;
122
472788 123       // read comments from config file
TB 124       if (function_exists('token_get_all')) {
125         $tokens = token_get_all(file_get_contents($file));
126         $in_config = false;
127         $buffer = '';
128         for ($i=0; $i < count($tokens); $i++) {
129           $token = $tokens[$i];
130           if ($token[0] == T_VARIABLE && $token[1] == '$config' || $token[1] == '$rcmail_config') {
131             $in_config = true;
132             if ($buffer && $tokens[$i+1] == '[' && $tokens[$i+2][0] == T_CONSTANT_ENCAPSED_STRING) {
133               $propname = trim($tokens[$i+2][1], "'\"");
134               $this->comments[$propname] = $buffer;
135               $buffer = '';
136               $i += 3;
137             }
138           }
139           else if ($in_config && $token[0] == T_COMMENT) {
140             $buffer .= strtr($token[1], array('\n' => "\n"));
141           }
142         }
143       }
144
461a30 145       // deprecated name of config variable
AM 146       if (is_array($rcmail_config)) {
147         return $rcmail_config;
148       }
149
150       return $config;
354978 151     }
T 152   }
d68c90 153
354978 154   /**
T 155    * Getter for a certain config property
156    *
157    * @param string Property name
ad43e6 158    * @param string Default value
354978 159    * @return string The property value
T 160    */
fa7539 161   function getprop($name, $default = '')
354978 162   {
b77d0d 163     $value = $this->config[$name];
d68c90 164
ccb412 165     if ($name == 'des_key' && !$this->configured && !isset($_REQUEST["_$name"]))
5529d9 166       $value = rcube_utils::random_bytes(24);
d68c90 167
fa7539 168     return $value !== null && $value !== '' ? $value : $default;
354978 169   }
403f0b 170
A 171
354978 172   /**
461a30 173    * Create configuration file that contains parameters
AM 174    * that differ from default values.
354978 175    *
T 176    * @return string The complete config file content
177    */
461a30 178   function create_config()
354978 179   {
461a30 180     $config = array();
0c3bde 181
c5042d 182     foreach ($this->config as $prop => $default) {
0829b7 183       $is_default = !isset($_POST["_$prop"]);
A 184       $value      = !$is_default || $this->bool_config_props[$prop] ? $_POST["_$prop"] : $default;
403f0b 185
77effa 186       // always disable installer
9bacb2 187       if ($prop == 'enable_installer')
TB 188         $value = false;
354978 189
77effa 190       // reset useragent to default (keeps version up-to-date)
TB 191       if ($prop == 'useragent' && stripos($value, 'Roundcube Webmail/') !== false)
192         $value = $this->defaults[$prop];
193
194       // generate new encryption key, never use the default value
195       if ($prop == 'des_key' && $value == $this->defaults[$prop])
5529d9 196         $value = rcube_utils::random_bytes(24);
77effa 197
354978 198       // convert some form data
0829b7 199       if ($prop == 'debug_level' && !$is_default) {
A 200         if (is_array($value)) {
201           $val = 0;
7d7f67 202           foreach ($value as $dbgval)
b77d0d 203             $val += intval($dbgval);
0829b7 204           $value = $val;
A 205         }
354978 206       }
d67074 207       else if ($prop == 'db_dsnw' && !empty($_POST['_dbtype'])) {
237119 208         if ($_POST['_dbtype'] == 'sqlite')
T 209           $value = sprintf('%s://%s?mode=0646', $_POST['_dbtype'], $_POST['_dbname']{0} == '/' ? '/' . $_POST['_dbname'] : $_POST['_dbname']);
0829b7 210         else if ($_POST['_dbtype'])
b61965 211           $value = sprintf('%s://%s:%s@%s/%s', $_POST['_dbtype'], 
7d7f67 212             rawurlencode($_POST['_dbuser']), rawurlencode($_POST['_dbpass']), $_POST['_dbhost'], $_POST['_dbname']);
c5042d 213       }
T 214       else if ($prop == 'smtp_auth_type' && $value == '0') {
215         $value = '';
216       }
217       else if ($prop == 'default_host' && is_array($value)) {
eea11e 218         $value = self::_clean_array($value);
c5042d 219         if (count($value) <= 1)
T 220           $value = $value[0];
221       }
08ffd9 222       else if ($prop == 'mail_pagesize' || $prop == 'addressbook_pagesize') {
ccb412 223         $value = max(2, intval($value));
T 224       }
c5042d 225       else if ($prop == 'smtp_user' && !empty($_POST['_smtp_user_u'])) {
T 226         $value = '%u';
227       }
228       else if ($prop == 'smtp_pass' && !empty($_POST['_smtp_user_u'])) {
229         $value = '%p';
569654 230       }
c5042d 231       else if (is_bool($default)) {
27564f 232         $value = (bool)$value;
T 233       }
234       else if (is_numeric($value)) {
235         $value = intval($value);
c5042d 236       }
be140e 237       else if ($prop == 'plugins' && !empty($_POST['submit'])) {
8f576d 238         $value = array();
be140e 239         foreach (array_keys($_POST) as $key) {
8f576d 240           if (preg_match('/^_plugins_*/', $key))
F 241             array_push($value, $_POST[$key]);
242         }
243       }
403f0b 244
c5042d 245       // skip this property
68eb18 246       if (($value == $this->defaults[$prop]) && !in_array($prop, $this->local_config)
TB 247           || in_array($prop, array_merge($this->obsolete_config, array_keys($this->replaced_config)))
248           || preg_match('/^db_(table|sequence)_/', $prop)) {
c5042d 249         continue;
461a30 250       }
b77d0d 251
A 252       // save change
253       $this->config[$prop] = $value;
461a30 254       $config[$prop] = $value;
354978 255     }
0c3bde 256
461a30 257     $out = "<?php\n\n";
472788 258     $out .= "/* Local configuration for Roundcube Webmail */\n\n";
461a30 259     foreach ($config as $prop => $value) {
472788 260       // copy option descriptions from existing config or defaults.inc.php
TB 261       $out .= $this->comments[$prop];
eea11e 262       $out .= "\$config['$prop'] = " . self::_dump_var($value, $prop) . ";\n\n";
461a30 263     }
AM 264
265     return $out;
c5042d 266   }
e10712 267
T 268
269   /**
906953 270    * save generated config file in RCUBE_CONFIG_DIR
D 271    *
272    * @return boolean True if the file was saved successfully, false if not
273    */
fd6b19 274   function save_configfile($config)
906953 275   {
fd6b19 276     if (is_writable(RCUBE_CONFIG_DIR)) {
TB 277       return file_put_contents(RCUBE_CONFIG_DIR . 'config.inc.php', $config);
278     }
906953 279
fd6b19 280     return false;
906953 281   }
D 282
283   /**
e10712 284    * Check the current configuration for missing properties
T 285    * and deprecated or obsolete settings
286    *
287    * @return array List with problems detected
288    */
289   function check_config()
290   {
291     $this->load_config();
461a30 292
AM 293     if (!$this->configured) {
e10712 294       return null;
461a30 295     }
d68c90 296
e10712 297     $out = $seen = array();
d68c90 298
cbffc2 299     // iterate over the current configuration
9e4246 300     foreach (array_keys($this->config) as $prop) {
e10712 301       if ($replacement = $this->replaced_config[$prop]) {
T 302         $out['replaced'][] = array('prop' => $prop, 'replacement' => $replacement);
303         $seen[$replacement] = true;
304       }
305       else if (!$seen[$prop] && in_array($prop, $this->obsolete_config)) {
306         $out['obsolete'][] = array('prop' => $prop);
307         $seen[$prop] = true;
308       }
309     }
d68c90 310
15a049 311     // the old default mime_magic reference is obsolete
TB 312     if ($this->config['mime_magic'] == '/usr/share/misc/magic') {
313         $out['obsolete'][] = array('prop' => 'mime_magic', 'explain' => "Set value to null in order to use system default");
e10712 314     }
f8c06e 315
871ca9 316     // check config dependencies and contradictions
T 317     if ($this->config['enable_spellcheck'] && $this->config['spellcheck_engine'] == 'pspell') {
318       if (!extension_loaded('pspell')) {
319         $out['dependencies'][] = array('prop' => 'spellcheck_engine',
320           'explain' => 'This requires the <tt>pspell</tt> extension which could not be loaded.');
321       }
924b1a 322       else if (!empty($this->config['spellcheck_languages'])) {
01a8c5 323         foreach ($this->config['spellcheck_languages'] as $lang => $descr)
471d55 324           if (!@pspell_new($lang))
01a8c5 325             $out['dependencies'][] = array('prop' => 'spellcheck_languages',
T 326               'explain' => "You are missing pspell support for language $lang ($descr)");
871ca9 327       }
T 328     }
d68c90 329
871ca9 330     if ($this->config['log_driver'] == 'syslog') {
T 331       if (!function_exists('openlog')) {
332         $out['dependencies'][] = array('prop' => 'log_driver',
3a81af 333           'explain' => 'This requires the <tt>syslog</tt> extension which could not be loaded.');
871ca9 334       }
T 335       if (empty($this->config['syslog_id'])) {
336         $out['dependencies'][] = array('prop' => 'syslog_id',
337           'explain' => 'Using <tt>syslog</tt> for logging requires a syslog ID to be configured');
338       }
5f25a1 339     }
d68c90 340
5f25a1 341     // check ldap_public sources having global_search enabled
T 342     if (is_array($this->config['ldap_public']) && !is_array($this->config['autocomplete_addressbooks'])) {
343       foreach ($this->config['ldap_public'] as $ldap_public) {
344         if ($ldap_public['global_search']) {
345           $out['replaced'][] = array('prop' => 'ldap_public::global_search', 'replacement' => 'autocomplete_addressbooks');
346           break;
347         }
348       }
871ca9 349     }
d68c90 350
e10712 351     return $out;
T 352   }
d68c90 353
AM 354
e10712 355   /**
T 356    * Merge the current configuration with the defaults
357    * and copy replaced values to the new options.
358    */
359   function merge_config()
360   {
361     $current = $this->config;
362     $this->config = array();
0829b7 363
e6bb83 364     foreach ($this->replaced_config as $prop => $replacement) {
e10712 365       if (isset($current[$prop])) {
T 366         if ($prop == 'skin_path')
367           $this->config[$replacement] = preg_replace('#skins/(\w+)/?$#', '\\1', $current[$prop]);
cbffc2 368         else if ($prop == 'multiple_identities')
T 369           $this->config[$replacement] = $current[$prop] ? 2 : 0;
e10712 370         else
T 371           $this->config[$replacement] = $current[$prop];
e6bb83 372       }
T 373       unset($current[$prop]);
e10712 374     }
d68c90 375
e10712 376     foreach ($this->obsolete_config as $prop) {
T 377       unset($current[$prop]);
378     }
d68c90 379
5f25a1 380     // add all ldap_public sources having global_search enabled to autocomplete_addressbooks
T 381     if (is_array($current['ldap_public'])) {
382       foreach ($current['ldap_public'] as $key => $ldap_public) {
383         if ($ldap_public['global_search']) {
384           $this->config['autocomplete_addressbooks'][] = $key;
385           unset($current['ldap_public'][$key]['global_search']);
386         }
387       }
388     }
d68c90 389
461a30 390     $this->config = array_merge($this->config, $current);
0829b7 391
3725cf 392     foreach (array_keys((array)$current['ldap_public']) as $key) {
5f25a1 393       $this->config['ldap_public'][$key] = $current['ldap_public'][$key];
T 394     }
e10712 395   }
d68c90 396
2491c6 397   /**
T 398    * Compare the local database schema with the reference schema
e019f2 399    * required for this version of Roundcube
2491c6 400    *
3725cf 401    * @param rcube_db Database object
AM 402    *
b3e259 403    * @return boolean True if the schema is up-to-date, false if not or an error occurred
2491c6 404    */
3725cf 405   function db_schema_check($DB)
2491c6 406   {
T 407     if (!$this->configured)
408       return false;
d68c90 409
e6bb83 410     // read reference schema from mysql.initial.sql
T 411     $db_schema = $this->db_read_schema(INSTALL_PATH . 'SQL/mysql.initial.sql');
2491c6 412     $errors = array();
d68c90 413
2491c6 414     // check list of tables
T 415     $existing_tables = $DB->list_tables();
f6ee6f 416
2491c6 417     foreach ($db_schema as $table => $cols) {
399db1 418       $table = $this->config['db_prefix'] . $table;
e6bb83 419       if (!in_array($table, $existing_tables)) {
T 420         $errors[] = "Missing table '".$table."'";
421       }
422       else {  // compare cols
423         $db_cols = $DB->list_cols($table);
424         $diff = array_diff(array_keys($cols), $db_cols);
425         if (!empty($diff))
426           $errors[] = "Missing columns in table '$table': " . join(',', $diff);
427       }
2491c6 428     }
398bff 429
2491c6 430     return !empty($errors) ? $errors : false;
T 431   }
e6bb83 432
T 433   /**
434    * Utility function to read database schema from an .sql file
435    */
436   private function db_read_schema($schemafile)
437   {
438     $lines = file($schemafile);
439     $table_block = false;
440     $schema = array();
441     foreach ($lines as $line) {
442       if (preg_match('/^\s*create table `?([a-z0-9_]+)`?/i', $line, $m)) {
443         $table_block = $m[1];
444       }
445       else if ($table_block && preg_match('/^\s*`?([a-z0-9_-]+)`?\s+([a-z]+)/', $line, $m)) {
446         $col = $m[1];
447         if (!in_array(strtoupper($col), array('PRIMARY','KEY','INDEX','UNIQUE','CONSTRAINT','REFERENCES','FOREIGN'))) {
448           $schema[$table_block][$col] = $m[2];
449         }
450       }
451     }
398bff 452
e6bb83 453     return $schema;
871ca9 454   }
5fed07 455
8f49e4 456   /**
TB 457    * Try to detect some file's mimetypes to test the correct behavior of fileinfo
458    */
459   function check_mime_detection()
460   {
461     $files = array(
eea11e 462       'skins/larry/images/roundcube_logo.png' => 'image/png',
8f49e4 463       'program/resources/blank.tif' => 'image/tiff',
eea11e 464       'program/resources/blocked.gif' => 'image/gif',
3b338f 465       'skins/larry/README' => 'text/plain',
8f49e4 466     );
TB 467
468     $errors = array();
469     foreach ($files as $path => $expected) {
470       $mimetype = rcube_mime::file_content_type(INSTALL_PATH . $path, basename($path));
471       if ($mimetype != $expected) {
472         $errors[] = array($path, $mimetype, $expected);
473       }
474     }
475
476     return $errors;
477   }
478
479   /**
480    * Check the correct configuration of the 'mime_types' mapping option
481    */
482   function check_mime_extensions()
483   {
484     $types = array(
485       'application/zip'   => 'zip',
486       'application/x-tar' => 'tar',
6b0106 487       'application/pdf'   => 'pdf',
3b338f 488       'image/gif'     => 'gif',
8f49e4 489       'image/svg+xml' => 'svg',
TB 490     );
491
492     $errors = array();
493     foreach ($types as $mimetype => $expected) {
494       $ext = rcube_mime::get_mime_extensions($mimetype);
6b0106 495       if (!in_array($expected, (array) $ext)) {
8f49e4 496         $errors[] = array($mimetype, $ext, $expected);
TB 497       }
498     }
499
500     return $errors;
501   }
5fed07 502
871ca9 503   /**
c5042d 504    * Getter for the last error message
T 505    *
506    * @return string Error message or null if none exists
507    */
508   function get_error()
509   {
510       return $this->last_error['message'];
354978 511   }
5fed07 512
AM 513
354978 514   /**
112c54 515    * Return a list with all imap hosts configured
T 516    *
517    * @return array Clean list with imap hosts
518    */
519   function get_hostlist()
520   {
521     $default_hosts = (array)$this->getprop('default_host');
522     $out = array();
5fed07 523
112c54 524     foreach ($default_hosts as $key => $name) {
T 525       if (!empty($name))
058eb6 526         $out[] = rcube_parse_host(is_numeric($key) ? $name : $key);
112c54 527     }
5fed07 528
112c54 529     return $out;
e6bb83 530   }
7177b5 531
e6bb83 532   /**
T 533    * Create a HTML dropdown to select a previous version of Roundcube
534    */
535   function versions_select($attrib = array())
536   {
537     $select = new html_select($attrib);
7177b5 538     $select->add(array(
A 539         '0.1-stable', '0.1.1',
540         '0.2-alpha', '0.2-beta', '0.2-stable',
541         '0.3-stable', '0.3.1',
542         '0.4-beta', '0.4.2',
c2c1bb 543         '0.5-beta', '0.5', '0.5.1', '0.5.2', '0.5.3', '0.5.4',
7177b5 544         '0.6-beta', '0.6',
c2c1bb 545         '0.7-beta', '0.7', '0.7.1', '0.7.2', '0.7.3', '0.7.4',
AM 546         '0.8-beta', '0.8-rc', '0.8.0', '0.8.1', '0.8.2', '0.8.3', '0.8.4', '0.8.5', '0.8.6',
1aff0e 547         '0.9-beta', '0.9-rc', '0.9-rc2',
AM 548         // Note: Do not add newer versions here
7177b5 549     ));
e6bb83 550     return $select;
112c54 551   }
7177b5 552
e49064 553   /**
T 554    * Return a list with available subfolders of the skin directory
555    */
556   function list_skins()
557   {
558     $skins = array();
559     $skindir = INSTALL_PATH . 'skins/';
560     foreach (glob($skindir . '*') as $path) {
561       if (is_dir($path) && is_readable($path)) {
562         $skins[] = substr($path, strlen($skindir));
563       }
564     }
565     return $skins;
566   }
5fed07 567
112c54 568   /**
8f576d 569   * Return a list with available subfolders of the plugins directory
F 570   * (with their associated description in composer.json)
571   */
126d09 572   function list_plugins()
8f576d 573   {
F 574     $plugins = array();
575     $plugin_dir = INSTALL_PATH . 'plugins/';
576
126d09 577     foreach (glob($plugin_dir . '*') as $path) {
AM 578       if (!is_dir($path)) {
579         continue;
580       }
8f576d 581
126d09 582       if (is_readable($path.'/composer.json')) {
AM 583         $file_json   = json_decode(file_get_contents($path.'/composer.json'));
b73702 584         $plugin_desc = $file_json->description ?: 'N/A';
8f576d 585       }
126d09 586       else {
8f576d 587         $plugin_desc = 'N/A';
F 588       }
589
126d09 590       $name      = substr($path, strlen($plugin_dir));
AM 591       $plugins[] = array(
592         'name'    => $name,
593         'desc'    => $plugin_desc,
594         'enabled' => in_array($name, (array) $this->config['plugins'])
595       );
8f576d 596     }
F 597
598     return $plugins;
599   }
600
601   /**
354978 602    * Display OK status
T 603    *
604    * @param string Test name
605    * @param string Confirm message
606    */
607   function pass($name, $message = '')
608   {
609     echo Q($name) . ':&nbsp; <span class="success">OK</span>';
6557d3 610     $this->_showhint($message);
354978 611   }
5fed07 612
AM 613
354978 614   /**
T 615    * Display an error status and increase failure count
616    *
617    * @param string Test name
618    * @param string Error message
619    * @param string URL for details
e7fa2c 620    * @param bool   Do not count this failure
354978 621    */
e7fa2c 622   function fail($name, $message = '', $url = '', $optional=false)
354978 623   {
e7fa2c 624     if (!$optional) {
AM 625       $this->failures++;
626     }
5fed07 627
354978 628     echo Q($name) . ':&nbsp; <span class="fail">NOT OK</span>';
6557d3 629     $this->_showhint($message, $url);
354978 630   }
11e670 631
A 632
633   /**
634    * Display an error status for optional settings/features
635    *
636    * @param string Test name
637    * @param string Error message
638    * @param string URL for details
639    */
640   function optfail($name, $message = '', $url = '')
641   {
642     echo Q($name) . ':&nbsp; <span class="na">NOT OK</span>';
643     $this->_showhint($message, $url);
644   }
5fed07 645
AM 646
354978 647   /**
T 648    * Display warning status
649    *
650    * @param string Test name
651    * @param string Warning message
652    * @param string URL for details
653    */
6557d3 654   function na($name, $message = '', $url = '')
354978 655   {
6557d3 656     echo Q($name) . ':&nbsp; <span class="na">NOT AVAILABLE</span>';
T 657     $this->_showhint($message, $url);
658   }
5fed07 659
AM 660
6557d3 661   function _showhint($message, $url = '')
T 662   {
663     $hint = Q($message);
5fed07 664
354978 665     if ($url)
6557d3 666       $hint .= ($hint ? '; ' : '') . 'See <a href="' . Q($url) . '" target="_blank">' . Q($url) . '</a>';
5fed07 667
6557d3 668     if ($hint)
T 669       echo '<span class="indent">(' . $hint . ')</span>';
354978 670   }
5fed07 671
AM 672
5f25a1 673   static function _clean_array($arr)
c5042d 674   {
T 675     $out = array();
5fed07 676
5f25a1 677     foreach (array_unique($arr) as $k => $val) {
T 678       if (!empty($val)) {
679         if (is_numeric($k))
680           $out[] = $val;
681         else
682           $out[$k] = $val;
683       }
684     }
5fed07 685
c5042d 686     return $out;
T 687   }
5fed07 688
AM 689
461a30 690   static function _dump_var($var, $name=null)
AM 691   {
0829b7 692     // special values
A 693     switch ($name) {
694     case 'syslog_facility':
695       $list = array(32 => 'LOG_AUTH', 80 => 'LOG_AUTHPRIV', 72 => ' LOG_CRON',
696                     24 => 'LOG_DAEMON', 0 => 'LOG_KERN', 128 => 'LOG_LOCAL0',
697                     136 => 'LOG_LOCAL1', 144 => 'LOG_LOCAL2', 152 => 'LOG_LOCAL3',
698                     160 => 'LOG_LOCAL4', 168 => 'LOG_LOCAL5', 176 => 'LOG_LOCAL6',
699                     184 => 'LOG_LOCAL7', 48 => 'LOG_LPR', 16 => 'LOG_MAIL',
700                     56 => 'LOG_NEWS', 40 => 'LOG_SYSLOG', 8 => 'LOG_USER', 64 => 'LOG_UUCP');
701       if ($val = $list[$var])
702         return $val;
703       break;
704
461a30 705     case 'mail_header_delimiter':
AM 706       $var = str_replace(array("\r", "\n"), array('\r', '\n'), $var);
707       return '"' . $var. '"';
708       break;
709 /*
710     // RCMAIL_VERSION is undefined here
711     case 'useragent':
712       if (preg_match('|^(.*)/('.preg_quote(RCMAIL_VERSION, '|').')$|i', $var, $m)) {
713         return '"' . addcslashes($var, '"') . '/" . RCMAIL_VERSION';
714       }
715       break;
716 */
717     }
0829b7 718
5f25a1 719     if (is_array($var)) {
T 720       if (empty($var)) {
721         return 'array()';
722       }
723       else {  // check if all keys are numeric
724         $isnum = true;
3725cf 725         foreach (array_keys($var) as $key) {
5f25a1 726           if (!is_numeric($key)) {
T 727             $isnum = false;
728             break;
729           }
730         }
5fed07 731
5f25a1 732         if ($isnum)
eea11e 733           return 'array(' . join(', ', array_map(array('rcmail_install', '_dump_var'), $var)) . ')';
5f25a1 734       }
T 735     }
5fed07 736
5f25a1 737     return var_export($var, true);
T 738   }
5fed07 739
AM 740
190e97 741   /**
T 742    * Initialize the database with the according schema
743    *
744    * @param object rcube_db Database connection
745    * @return boolen True on success, False on error
746    */
747   function init_db($DB)
748   {
ff54e9 749     $engine = $DB->db_provider;
5fed07 750
190e97 751     // read schema file from /SQL/*
e6bb83 752     $fname = INSTALL_PATH . "SQL/$engine.initial.sql";
T 753     if ($sql = @file_get_contents($fname)) {
90f7aa 754       $DB->set_option('table_prefix', $this->config['db_prefix']);
AM 755       $DB->exec_script($sql);
190e97 756     }
T 757     else {
758       $this->fail('DB Schema', "Cannot read the schema file: $fname");
759       return false;
760     }
5fed07 761
190e97 762     if ($err = $this->get_error()) {
T 763       $this->fail('DB Schema', "Error creating database schema: $err");
764       return false;
765     }
766
767     return true;
768   }
5fed07 769
AM 770
e6bb83 771   /**
4490d0 772    * Update database schema
e6bb83 773    *
T 774    * @param string Version to update from
4490d0 775    *
e6bb83 776    * @return boolen True on success, False on error
T 777    */
4490d0 778   function update_db($version)
e6bb83 779   {
f23ef1 780     system(INSTALL_PATH . "bin/updatedb.sh --package=roundcube"
AM 781       . " --version=" . escapeshellarg($version)
782       . " --dir=" . INSTALL_PATH . "SQL"
783       . " 2>&1", $result);
5fed07 784
4490d0 785     return !$result;
399db1 786   }
AM 787
788
789   /**
e019f2 790    * Handler for Roundcube errors
c5042d 791    */
T 792   function raise_error($p)
793   {
794       $this->last_error = $p;
795   }
796 }