Aleksander Machniak
2016-05-06 acf633c73bc8df9a5036bc52d7568f4213ab73c7
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;
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',
c321a9 45     'default_imap_folders' => 'default_folders',
651c7b 46     'top_posting'          => 'reply_mode',
68eb18 47     'keep_alive'           => 'refresh_interval',
TB 48     'min_keep_alive'       => 'min_refresh_interval',
e6bb83 49   );
08ffd9 50
398bff 51   // list of supported database drivers
AM 52   var $supported_dbs = array(
53     'MySQL'               => 'pdo_mysql',
54     'PostgreSQL'          => 'pdo_pgsql',
55     'SQLite'              => 'pdo_sqlite',
56     'SQLite (v2)'         => 'pdo_sqlite2',
57     'SQL Server (SQLSRV)' => 'pdo_sqlsrv',
58     'SQL Server (DBLIB)'  => 'pdo_dblib',
59   );
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)
T 79       $inst = new rcube_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"]))
807d17 166       $value = rcube_install::random_key(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])
196         $value = $this->random_key(24);
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)) {
807d17 218         $value = rcube_install::_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';
230       }
c321a9 231       else if ($prop == 'default_folders') {
d68c90 232         $value = array();
AM 233         foreach ($this->config['default_folders'] as $_folder) {
234           switch ($_folder) {
235           case 'Drafts': $_folder = $this->config['drafts_mbox']; break;
236           case 'Sent':   $_folder = $this->config['sent_mbox']; break;
237           case 'Junk':   $_folder = $this->config['junk_mbox']; break;
238           case 'Trash':  $_folder = $this->config['trash_mbox']; break;
569654 239           }
d68c90 240         if (!in_array($_folder, $value))
AM 241           $value[] = $_folder;
569654 242         }
A 243       }
c5042d 244       else if (is_bool($default)) {
27564f 245         $value = (bool)$value;
T 246       }
247       else if (is_numeric($value)) {
248         $value = intval($value);
c5042d 249       }
403f0b 250
c5042d 251       // skip this property
68eb18 252       if (($value == $this->defaults[$prop]) && !in_array($prop, $this->local_config)
TB 253           || in_array($prop, array_merge($this->obsolete_config, array_keys($this->replaced_config)))
254           || preg_match('/^db_(table|sequence)_/', $prop)) {
c5042d 255         continue;
461a30 256       }
b77d0d 257
A 258       // save change
259       $this->config[$prop] = $value;
461a30 260       $config[$prop] = $value;
354978 261     }
0c3bde 262
461a30 263     $out = "<?php\n\n";
472788 264     $out .= "/* Local configuration for Roundcube Webmail */\n\n";
461a30 265     foreach ($config as $prop => $value) {
472788 266       // copy option descriptions from existing config or defaults.inc.php
TB 267       $out .= $this->comments[$prop];
268       $out .= "\$config['$prop'] = " . rcube_install::_dump_var($value, $prop) . ";\n\n";
461a30 269     }
AM 270
271     return $out;
c5042d 272   }
e10712 273
T 274
275   /**
906953 276    * save generated config file in RCUBE_CONFIG_DIR
D 277    *
278    * @return boolean True if the file was saved successfully, false if not
279    */
fd6b19 280   function save_configfile($config)
906953 281   {
fd6b19 282     if (is_writable(RCUBE_CONFIG_DIR)) {
TB 283       return file_put_contents(RCUBE_CONFIG_DIR . 'config.inc.php', $config);
284     }
906953 285
fd6b19 286     return false;
906953 287   }
D 288
289   /**
e10712 290    * Check the current configuration for missing properties
T 291    * and deprecated or obsolete settings
292    *
293    * @return array List with problems detected
294    */
295   function check_config()
296   {
297     $this->load_config();
461a30 298
AM 299     if (!$this->configured) {
e10712 300       return null;
461a30 301     }
d68c90 302
e10712 303     $out = $seen = array();
d68c90 304
cbffc2 305     // iterate over the current configuration
e10712 306     foreach ($this->config as $prop => $value) {
T 307       if ($replacement = $this->replaced_config[$prop]) {
308         $out['replaced'][] = array('prop' => $prop, 'replacement' => $replacement);
309         $seen[$replacement] = true;
310       }
311       else if (!$seen[$prop] && in_array($prop, $this->obsolete_config)) {
312         $out['obsolete'][] = array('prop' => $prop);
313         $seen[$prop] = true;
314       }
315     }
d68c90 316
15a049 317     // the old default mime_magic reference is obsolete
TB 318     if ($this->config['mime_magic'] == '/usr/share/misc/magic') {
319         $out['obsolete'][] = array('prop' => 'mime_magic', 'explain' => "Set value to null in order to use system default");
e10712 320     }
f8c06e 321
871ca9 322     // check config dependencies and contradictions
T 323     if ($this->config['enable_spellcheck'] && $this->config['spellcheck_engine'] == 'pspell') {
324       if (!extension_loaded('pspell')) {
325         $out['dependencies'][] = array('prop' => 'spellcheck_engine',
326           'explain' => 'This requires the <tt>pspell</tt> extension which could not be loaded.');
327       }
924b1a 328       else if (!empty($this->config['spellcheck_languages'])) {
01a8c5 329         foreach ($this->config['spellcheck_languages'] as $lang => $descr)
471d55 330           if (!@pspell_new($lang))
01a8c5 331             $out['dependencies'][] = array('prop' => 'spellcheck_languages',
T 332               'explain' => "You are missing pspell support for language $lang ($descr)");
871ca9 333       }
T 334     }
d68c90 335
871ca9 336     if ($this->config['log_driver'] == 'syslog') {
T 337       if (!function_exists('openlog')) {
338         $out['dependencies'][] = array('prop' => 'log_driver',
3a81af 339           'explain' => 'This requires the <tt>syslog</tt> extension which could not be loaded.');
871ca9 340       }
T 341       if (empty($this->config['syslog_id'])) {
342         $out['dependencies'][] = array('prop' => 'syslog_id',
343           'explain' => 'Using <tt>syslog</tt> for logging requires a syslog ID to be configured');
344       }
5f25a1 345     }
d68c90 346
5f25a1 347     // check ldap_public sources having global_search enabled
T 348     if (is_array($this->config['ldap_public']) && !is_array($this->config['autocomplete_addressbooks'])) {
349       foreach ($this->config['ldap_public'] as $ldap_public) {
350         if ($ldap_public['global_search']) {
351           $out['replaced'][] = array('prop' => 'ldap_public::global_search', 'replacement' => 'autocomplete_addressbooks');
352           break;
353         }
354       }
871ca9 355     }
d68c90 356
e10712 357     return $out;
T 358   }
d68c90 359
AM 360
e10712 361   /**
T 362    * Merge the current configuration with the defaults
363    * and copy replaced values to the new options.
364    */
365   function merge_config()
366   {
367     $current = $this->config;
368     $this->config = array();
0829b7 369
e6bb83 370     foreach ($this->replaced_config as $prop => $replacement) {
e10712 371       if (isset($current[$prop])) {
T 372         if ($prop == 'skin_path')
373           $this->config[$replacement] = preg_replace('#skins/(\w+)/?$#', '\\1', $current[$prop]);
cbffc2 374         else if ($prop == 'multiple_identities')
T 375           $this->config[$replacement] = $current[$prop] ? 2 : 0;
e10712 376         else
T 377           $this->config[$replacement] = $current[$prop];
e6bb83 378       }
T 379       unset($current[$prop]);
e10712 380     }
d68c90 381
e10712 382     foreach ($this->obsolete_config as $prop) {
T 383       unset($current[$prop]);
384     }
d68c90 385
5f25a1 386     // add all ldap_public sources having global_search enabled to autocomplete_addressbooks
T 387     if (is_array($current['ldap_public'])) {
388       foreach ($current['ldap_public'] as $key => $ldap_public) {
389         if ($ldap_public['global_search']) {
390           $this->config['autocomplete_addressbooks'][] = $key;
391           unset($current['ldap_public'][$key]['global_search']);
392         }
393       }
394     }
d68c90 395
461a30 396     $this->config = array_merge($this->config, $current);
0829b7 397
3725cf 398     foreach (array_keys((array)$current['ldap_public']) as $key) {
5f25a1 399       $this->config['ldap_public'][$key] = $current['ldap_public'][$key];
T 400     }
e10712 401   }
d68c90 402
2491c6 403   /**
T 404    * Compare the local database schema with the reference schema
e019f2 405    * required for this version of Roundcube
2491c6 406    *
3725cf 407    * @param rcube_db Database object
AM 408    *
b3e259 409    * @return boolean True if the schema is up-to-date, false if not or an error occurred
2491c6 410    */
3725cf 411   function db_schema_check($DB)
2491c6 412   {
T 413     if (!$this->configured)
414       return false;
d68c90 415
e6bb83 416     // read reference schema from mysql.initial.sql
T 417     $db_schema = $this->db_read_schema(INSTALL_PATH . 'SQL/mysql.initial.sql');
2491c6 418     $errors = array();
d68c90 419
2491c6 420     // check list of tables
T 421     $existing_tables = $DB->list_tables();
f6ee6f 422
2491c6 423     foreach ($db_schema as $table => $cols) {
399db1 424       $table = $this->config['db_prefix'] . $table;
e6bb83 425       if (!in_array($table, $existing_tables)) {
T 426         $errors[] = "Missing table '".$table."'";
427       }
428       else {  // compare cols
429         $db_cols = $DB->list_cols($table);
430         $diff = array_diff(array_keys($cols), $db_cols);
431         if (!empty($diff))
432           $errors[] = "Missing columns in table '$table': " . join(',', $diff);
433       }
2491c6 434     }
398bff 435
2491c6 436     return !empty($errors) ? $errors : false;
T 437   }
e6bb83 438
T 439   /**
440    * Utility function to read database schema from an .sql file
441    */
442   private function db_read_schema($schemafile)
443   {
444     $lines = file($schemafile);
445     $table_block = false;
446     $schema = array();
447     foreach ($lines as $line) {
448       if (preg_match('/^\s*create table `?([a-z0-9_]+)`?/i', $line, $m)) {
449         $table_block = $m[1];
450       }
451       else if ($table_block && preg_match('/^\s*`?([a-z0-9_-]+)`?\s+([a-z]+)/', $line, $m)) {
452         $col = $m[1];
453         if (!in_array(strtoupper($col), array('PRIMARY','KEY','INDEX','UNIQUE','CONSTRAINT','REFERENCES','FOREIGN'))) {
454           $schema[$table_block][$col] = $m[2];
455         }
456       }
457     }
398bff 458
e6bb83 459     return $schema;
871ca9 460   }
5fed07 461
8f49e4 462   /**
TB 463    * Try to detect some file's mimetypes to test the correct behavior of fileinfo
464    */
465   function check_mime_detection()
466   {
467     $files = array(
468       'installer/images/roundcube_logo.png' => 'image/png',
469       'program/resources/blank.tif' => 'image/tiff',
3b338f 470       'skins/larry/images/buttons.gif' => 'image/gif',
AM 471       'skins/larry/README' => 'text/plain',
8f49e4 472     );
TB 473
474     $errors = array();
475     foreach ($files as $path => $expected) {
476       $mimetype = rcube_mime::file_content_type(INSTALL_PATH . $path, basename($path));
477       if ($mimetype != $expected) {
478         $errors[] = array($path, $mimetype, $expected);
479       }
480     }
481
482     return $errors;
483   }
484
485   /**
486    * Check the correct configuration of the 'mime_types' mapping option
487    */
488   function check_mime_extensions()
489   {
490     $types = array(
491       'application/zip'   => 'zip',
492       'application/x-tar' => 'tar',
9cf50d 493       'application/pdf'   => 'pdf',
3b338f 494       'image/gif'     => 'gif',
8f49e4 495       'image/svg+xml' => 'svg',
TB 496     );
497
498     $errors = array();
499     foreach ($types as $mimetype => $expected) {
500       $ext = rcube_mime::get_mime_extensions($mimetype);
9cf50d 501       if (!in_array($expected, (array) $ext)) {
8f49e4 502         $errors[] = array($mimetype, $ext, $expected);
TB 503       }
504     }
505
506     return $errors;
507   }
5fed07 508
871ca9 509   /**
c5042d 510    * Getter for the last error message
T 511    *
512    * @return string Error message or null if none exists
513    */
514   function get_error()
515   {
516       return $this->last_error['message'];
354978 517   }
5fed07 518
AM 519
354978 520   /**
112c54 521    * Return a list with all imap hosts configured
T 522    *
523    * @return array Clean list with imap hosts
524    */
525   function get_hostlist()
526   {
527     $default_hosts = (array)$this->getprop('default_host');
528     $out = array();
5fed07 529
112c54 530     foreach ($default_hosts as $key => $name) {
T 531       if (!empty($name))
058eb6 532         $out[] = rcube_parse_host(is_numeric($key) ? $name : $key);
112c54 533     }
5fed07 534
112c54 535     return $out;
e6bb83 536   }
7177b5 537
e6bb83 538   /**
T 539    * Create a HTML dropdown to select a previous version of Roundcube
540    */
541   function versions_select($attrib = array())
542   {
543     $select = new html_select($attrib);
7177b5 544     $select->add(array(
A 545         '0.1-stable', '0.1.1',
546         '0.2-alpha', '0.2-beta', '0.2-stable',
547         '0.3-stable', '0.3.1',
548         '0.4-beta', '0.4.2',
c2c1bb 549         '0.5-beta', '0.5', '0.5.1', '0.5.2', '0.5.3', '0.5.4',
7177b5 550         '0.6-beta', '0.6',
c2c1bb 551         '0.7-beta', '0.7', '0.7.1', '0.7.2', '0.7.3', '0.7.4',
AM 552         '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 553         '0.9-beta', '0.9-rc', '0.9-rc2',
AM 554         // Note: Do not add newer versions here
7177b5 555     ));
e6bb83 556     return $select;
112c54 557   }
7177b5 558
e49064 559   /**
T 560    * Return a list with available subfolders of the skin directory
561    */
562   function list_skins()
563   {
564     $skins = array();
565     $skindir = INSTALL_PATH . 'skins/';
566     foreach (glob($skindir . '*') as $path) {
567       if (is_dir($path) && is_readable($path)) {
568         $skins[] = substr($path, strlen($skindir));
569       }
570     }
571     return $skins;
572   }
5fed07 573
112c54 574   /**
354978 575    * Display OK status
T 576    *
577    * @param string Test name
578    * @param string Confirm message
579    */
580   function pass($name, $message = '')
581   {
582     echo Q($name) . ':&nbsp; <span class="success">OK</span>';
6557d3 583     $this->_showhint($message);
354978 584   }
5fed07 585
AM 586
354978 587   /**
T 588    * Display an error status and increase failure count
589    *
590    * @param string Test name
591    * @param string Error message
592    * @param string URL for details
e7fa2c 593    * @param bool   Do not count this failure
354978 594    */
e7fa2c 595   function fail($name, $message = '', $url = '', $optional=false)
354978 596   {
e7fa2c 597     if (!$optional) {
AM 598       $this->failures++;
599     }
5fed07 600
354978 601     echo Q($name) . ':&nbsp; <span class="fail">NOT OK</span>';
6557d3 602     $this->_showhint($message, $url);
354978 603   }
11e670 604
A 605
606   /**
607    * Display an error status for optional settings/features
608    *
609    * @param string Test name
610    * @param string Error message
611    * @param string URL for details
612    */
613   function optfail($name, $message = '', $url = '')
614   {
615     echo Q($name) . ':&nbsp; <span class="na">NOT OK</span>';
616     $this->_showhint($message, $url);
617   }
5fed07 618
AM 619
354978 620   /**
T 621    * Display warning status
622    *
623    * @param string Test name
624    * @param string Warning message
625    * @param string URL for details
626    */
6557d3 627   function na($name, $message = '', $url = '')
354978 628   {
6557d3 629     echo Q($name) . ':&nbsp; <span class="na">NOT AVAILABLE</span>';
T 630     $this->_showhint($message, $url);
631   }
5fed07 632
AM 633
6557d3 634   function _showhint($message, $url = '')
T 635   {
636     $hint = Q($message);
5fed07 637
354978 638     if ($url)
6557d3 639       $hint .= ($hint ? '; ' : '') . 'See <a href="' . Q($url) . '" target="_blank">' . Q($url) . '</a>';
5fed07 640
6557d3 641     if ($hint)
T 642       echo '<span class="indent">(' . $hint . ')</span>';
354978 643   }
5fed07 644
AM 645
5f25a1 646   static function _clean_array($arr)
c5042d 647   {
T 648     $out = array();
5fed07 649
5f25a1 650     foreach (array_unique($arr) as $k => $val) {
T 651       if (!empty($val)) {
652         if (is_numeric($k))
653           $out[] = $val;
654         else
655           $out[$k] = $val;
656       }
657     }
5fed07 658
c5042d 659     return $out;
T 660   }
5fed07 661
AM 662
461a30 663   static function _dump_var($var, $name=null)
AM 664   {
0829b7 665     // special values
A 666     switch ($name) {
667     case 'syslog_facility':
668       $list = array(32 => 'LOG_AUTH', 80 => 'LOG_AUTHPRIV', 72 => ' LOG_CRON',
669                     24 => 'LOG_DAEMON', 0 => 'LOG_KERN', 128 => 'LOG_LOCAL0',
670                     136 => 'LOG_LOCAL1', 144 => 'LOG_LOCAL2', 152 => 'LOG_LOCAL3',
671                     160 => 'LOG_LOCAL4', 168 => 'LOG_LOCAL5', 176 => 'LOG_LOCAL6',
672                     184 => 'LOG_LOCAL7', 48 => 'LOG_LPR', 16 => 'LOG_MAIL',
673                     56 => 'LOG_NEWS', 40 => 'LOG_SYSLOG', 8 => 'LOG_USER', 64 => 'LOG_UUCP');
674       if ($val = $list[$var])
675         return $val;
676       break;
677
461a30 678     case 'mail_header_delimiter':
AM 679       $var = str_replace(array("\r", "\n"), array('\r', '\n'), $var);
680       return '"' . $var. '"';
681       break;
682 /*
683     // RCMAIL_VERSION is undefined here
684     case 'useragent':
685       if (preg_match('|^(.*)/('.preg_quote(RCMAIL_VERSION, '|').')$|i', $var, $m)) {
686         return '"' . addcslashes($var, '"') . '/" . RCMAIL_VERSION';
687       }
688       break;
689 */
690     }
0829b7 691
5f25a1 692     if (is_array($var)) {
T 693       if (empty($var)) {
694         return 'array()';
695       }
696       else {  // check if all keys are numeric
697         $isnum = true;
3725cf 698         foreach (array_keys($var) as $key) {
5f25a1 699           if (!is_numeric($key)) {
T 700             $isnum = false;
701             break;
702           }
703         }
5fed07 704
5f25a1 705         if ($isnum)
T 706           return 'array(' . join(', ', array_map(array('rcube_install', '_dump_var'), $var)) . ')';
707       }
708     }
5fed07 709
5f25a1 710     return var_export($var, true);
T 711   }
5fed07 712
AM 713
190e97 714   /**
T 715    * Initialize the database with the according schema
716    *
717    * @param object rcube_db Database connection
718    * @return boolen True on success, False on error
719    */
720   function init_db($DB)
721   {
ff54e9 722     $engine = $DB->db_provider;
5fed07 723
190e97 724     // read schema file from /SQL/*
e6bb83 725     $fname = INSTALL_PATH . "SQL/$engine.initial.sql";
T 726     if ($sql = @file_get_contents($fname)) {
720e7d 727       $DB->set_option('table_prefix', $this->config['db_prefix']);
AM 728       $DB->exec_script($sql);
190e97 729     }
T 730     else {
731       $this->fail('DB Schema', "Cannot read the schema file: $fname");
732       return false;
733     }
5fed07 734
190e97 735     if ($err = $this->get_error()) {
T 736       $this->fail('DB Schema', "Error creating database schema: $err");
737       return false;
738     }
739
740     return true;
741   }
5fed07 742
AM 743
e6bb83 744   /**
4490d0 745    * Update database schema
e6bb83 746    *
T 747    * @param string Version to update from
4490d0 748    *
e6bb83 749    * @return boolen True on success, False on error
T 750    */
4490d0 751   function update_db($version)
e6bb83 752   {
f23ef1 753     system(INSTALL_PATH . "bin/updatedb.sh --package=roundcube"
AM 754       . " --version=" . escapeshellarg($version)
755       . " --dir=" . INSTALL_PATH . "SQL"
756       . " 2>&1", $result);
5fed07 757
4490d0 758     return !$result;
399db1 759   }
AM 760
761
762   /**
e019f2 763    * Handler for Roundcube errors
c5042d 764    */
T 765   function raise_error($p)
766   {
767       $this->last_error = $p;
768   }
5fed07 769
AM 770
354978 771   /**
T 772    * Generarte a ramdom string to be used as encryption key
773    *
774    * @param int Key length
775    * @return string The generated random string
776    * @static
777    */
778   function random_key($length)
779   {
780     $alpha = 'ABCDEFGHIJKLMNOPQERSTUVXYZabcdefghijklmnopqrtsuvwxyz0123456789+*%&?!$-_=';
781     $out = '';
5fed07 782
354978 783     for ($i=0; $i < $length; $i++)
T 784       $out .= $alpha{rand(0, strlen($alpha)-1)};
5fed07 785
354978 786     return $out;
T 787   }
5fed07 788
c5042d 789 }
T 790