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