Aleksander Machniak
2013-01-10 4490d03649e1ce1a90274a0dce56fe8e5d2859ae
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   {
592668 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     }
592668 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   {
592668 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',
64e218 455         '0.9-beta',
7177b5 456     ));
e6bb83 457     return $select;
112c54 458   }
7177b5 459
e49064 460   /**
T 461    * Return a list with available subfolders of the skin directory
462    */
463   function list_skins()
464   {
465     $skins = array();
466     $skindir = INSTALL_PATH . 'skins/';
467     foreach (glob($skindir . '*') as $path) {
468       if (is_dir($path) && is_readable($path)) {
469         $skins[] = substr($path, strlen($skindir));
470       }
471     }
472     return $skins;
473   }
5fed07 474
112c54 475   /**
354978 476    * Display OK status
T 477    *
478    * @param string Test name
479    * @param string Confirm message
480    */
481   function pass($name, $message = '')
482   {
483     echo Q($name) . ':&nbsp; <span class="success">OK</span>';
6557d3 484     $this->_showhint($message);
354978 485   }
5fed07 486
AM 487
354978 488   /**
T 489    * Display an error status and increase failure count
490    *
491    * @param string Test name
492    * @param string Error message
493    * @param string URL for details
494    */
495   function fail($name, $message = '', $url = '')
496   {
497     $this->failures++;
5fed07 498
354978 499     echo Q($name) . ':&nbsp; <span class="fail">NOT OK</span>';
6557d3 500     $this->_showhint($message, $url);
354978 501   }
11e670 502
A 503
504   /**
505    * Display an error status for optional settings/features
506    *
507    * @param string Test name
508    * @param string Error message
509    * @param string URL for details
510    */
511   function optfail($name, $message = '', $url = '')
512   {
513     echo Q($name) . ':&nbsp; <span class="na">NOT OK</span>';
514     $this->_showhint($message, $url);
515   }
5fed07 516
AM 517
354978 518   /**
T 519    * Display warning status
520    *
521    * @param string Test name
522    * @param string Warning message
523    * @param string URL for details
524    */
6557d3 525   function na($name, $message = '', $url = '')
354978 526   {
6557d3 527     echo Q($name) . ':&nbsp; <span class="na">NOT AVAILABLE</span>';
T 528     $this->_showhint($message, $url);
529   }
5fed07 530
AM 531
6557d3 532   function _showhint($message, $url = '')
T 533   {
534     $hint = Q($message);
5fed07 535
354978 536     if ($url)
6557d3 537       $hint .= ($hint ? '; ' : '') . 'See <a href="' . Q($url) . '" target="_blank">' . Q($url) . '</a>';
5fed07 538
6557d3 539     if ($hint)
T 540       echo '<span class="indent">(' . $hint . ')</span>';
354978 541   }
5fed07 542
AM 543
5f25a1 544   static function _clean_array($arr)
c5042d 545   {
T 546     $out = array();
5fed07 547
5f25a1 548     foreach (array_unique($arr) as $k => $val) {
T 549       if (!empty($val)) {
550         if (is_numeric($k))
551           $out[] = $val;
552         else
553           $out[$k] = $val;
554       }
555     }
5fed07 556
c5042d 557     return $out;
T 558   }
5fed07 559
AM 560
0829b7 561   static function _dump_var($var, $name=null) {
A 562     // special values
563     switch ($name) {
564     case 'syslog_facility':
565       $list = array(32 => 'LOG_AUTH', 80 => 'LOG_AUTHPRIV', 72 => ' LOG_CRON',
566                     24 => 'LOG_DAEMON', 0 => 'LOG_KERN', 128 => 'LOG_LOCAL0',
567                     136 => 'LOG_LOCAL1', 144 => 'LOG_LOCAL2', 152 => 'LOG_LOCAL3',
568                     160 => 'LOG_LOCAL4', 168 => 'LOG_LOCAL5', 176 => 'LOG_LOCAL6',
569                     184 => 'LOG_LOCAL7', 48 => 'LOG_LPR', 16 => 'LOG_MAIL',
570                     56 => 'LOG_NEWS', 40 => 'LOG_SYSLOG', 8 => 'LOG_USER', 64 => 'LOG_UUCP');
571       if ($val = $list[$var])
572         return $val;
573       break;
574     }
575
576
5f25a1 577     if (is_array($var)) {
T 578       if (empty($var)) {
579         return 'array()';
580       }
581       else {  // check if all keys are numeric
582         $isnum = true;
583         foreach ($var as $key => $value) {
584           if (!is_numeric($key)) {
585             $isnum = false;
586             break;
587           }
588         }
5fed07 589
5f25a1 590         if ($isnum)
T 591           return 'array(' . join(', ', array_map(array('rcube_install', '_dump_var'), $var)) . ')';
592       }
593     }
5fed07 594
5f25a1 595     return var_export($var, true);
T 596   }
5fed07 597
AM 598
190e97 599   /**
T 600    * Initialize the database with the according schema
601    *
602    * @param object rcube_db Database connection
603    * @return boolen True on success, False on error
604    */
605   function init_db($DB)
606   {
e6bb83 607     $engine = isset($this->db_map[$DB->db_provider]) ? $this->db_map[$DB->db_provider] : $DB->db_provider;
5fed07 608
190e97 609     // read schema file from /SQL/*
e6bb83 610     $fname = INSTALL_PATH . "SQL/$engine.initial.sql";
T 611     if ($sql = @file_get_contents($fname)) {
612       $this->exec_sql($sql, $DB);
190e97 613     }
T 614     else {
615       $this->fail('DB Schema', "Cannot read the schema file: $fname");
616       return false;
617     }
5fed07 618
190e97 619     if ($err = $this->get_error()) {
T 620       $this->fail('DB Schema', "Error creating database schema: $err");
621       return false;
622     }
623
624     return true;
625   }
5fed07 626
AM 627
e6bb83 628   /**
4490d0 629    * Update database schema
e6bb83 630    *
T 631    * @param string Version to update from
4490d0 632    *
e6bb83 633    * @return boolen True on success, False on error
T 634    */
4490d0 635   function update_db($version)
e6bb83 636   {
4490d0 637     system(INSTALL_PATH . "bin/updatedb.sh --label=roundcube --version=" . $version
AM 638       . " --dir=" . INSTALL_PATH . "SQL", $result);
5fed07 639
4490d0 640     return !$result;
e6bb83 641   }
5fed07 642
AM 643
e6bb83 644   /**
T 645    * Execute the given SQL queries on the database connection
646    *
647    * @param string SQL queries to execute
648    * @param object rcube_db Database connection
649    * @return boolen True on success, False on error
650    */
651   function exec_sql($sql, $DB)
652   {
653     $buff = '';
654     foreach (explode("\n", $sql) as $line) {
655       if (preg_match('/^--/', $line) || trim($line) == '')
656         continue;
5fed07 657
e6bb83 658       $buff .= $line . "\n";
T 659       if (preg_match('/(;|^GO)$/', trim($line))) {
660         $DB->query($buff);
661         $buff = '';
662         if ($DB->is_error())
663           break;
664       }
665     }
5fed07 666
e6bb83 667     return !$DB->is_error();
T 668   }
5fed07 669
AM 670
c5042d 671   /**
e019f2 672    * Handler for Roundcube errors
c5042d 673    */
T 674   function raise_error($p)
675   {
676       $this->last_error = $p;
677   }
5fed07 678
AM 679
354978 680   /**
T 681    * Generarte a ramdom string to be used as encryption key
682    *
683    * @param int Key length
684    * @return string The generated random string
685    * @static
686    */
687   function random_key($length)
688   {
689     $alpha = 'ABCDEFGHIJKLMNOPQERSTUVXYZabcdefghijklmnopqrtsuvwxyz0123456789+*%&?!$-_=';
690     $out = '';
5fed07 691
354978 692     for ($i=0; $i < $length; $i++)
T 693       $out .= $alpha{rand(0, strlen($alpha)-1)};
5fed07 694
354978 695     return $out;
T 696   }
5fed07 697
c5042d 698 }
T 699