Aleksander Machniak
2012-08-26 651c7b6e9df38a3b7cdf6daebed39021d88c3bea
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   }
T 73   
c5042d 74   /**
T 75    * Singleton getter
76    */
77   function get_instance()
78   {
79     static $inst;
80     
81     if (!$inst)
82       $inst = new rcube_install();
83     
84     return $inst;
85   }
354978 86   
T 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   {
434869 112     if (is_readable($main_inc = RCMAIL_CONFIG_DIR . '/main.inc' . $suffix)) {
T 113       include($main_inc);
114       if (is_array($rcmail_config))
115         $this->config += $rcmail_config;
354978 116     }
434869 117     if (is_readable($db_inc = RCMAIL_CONFIG_DIR . '/db.inc'. $suffix)) {
T 118       include($db_inc);
119       if (is_array($rcmail_config))
120         $this->config += $rcmail_config;
354978 121     }
T 122   }
123   
124   
125   /**
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];
354978 135     
ccb412 136     if ($name == 'des_key' && !$this->configured && !isset($_REQUEST["_$name"]))
807d17 137       $value = rcube_install::random_key(24);
354978 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   {
2c3d81 152     $out = @file_get_contents(RCMAIL_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') {
0829b7 196         $value = array();
c321a9 197         foreach ($this->config['default_folders'] as $_folder) {
0829b7 198           switch ($_folder) {
00290a 199           case 'Drafts': $_folder = $this->config['drafts_mbox']; break;
A 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           }
00290a 204         if (!in_array($_folder, $value))
A 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;
244     
245     $this->load_config();
246     if (!$this->configured)
247       return null;
248     
249     $out = $seen = array();
f8c06e 250     $required = array_flip($this->required_config);
e10712 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     }
263     
264     // iterate over default config
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     }
283     
284     if ($this->config['log_driver'] == 'syslog') {
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     }
T 294     
295     // check ldap_public sources having global_search enabled
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     }
T 304     
e10712 305     return $out;
T 306   }
307   
308   
309   /**
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     }
T 330     
331     foreach ($this->obsolete_config as $prop) {
332       unset($current[$prop]);
333     }
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     }
e6bb83 344     
T 345     if ($current['keep_alive'] && $current['session_lifetime'] < $current['keep_alive'])
346       $current['session_lifetime'] = max(10, ceil($current['keep_alive'] / 60) * 2);
0829b7 347
5f25a1 348     $this->config  = array_merge($this->config, $current);
0829b7 349
5f25a1 350     foreach ((array)$current['ldap_public'] as $key => $values) {
T 351       $this->config['ldap_public'][$key] = $current['ldap_public'][$key];
352     }
e10712 353   }
c5042d 354   
2491c6 355   /**
T 356    * Compare the local database schema with the reference schema
e019f2 357    * required for this version of Roundcube
2491c6 358    *
T 359    * @param boolean True if the schema schould be updated
360    * @return boolean True if the schema is up-to-date, false if not or an error occured
361    */
362   function db_schema_check($DB, $update = false)
363   {
364     if (!$this->configured)
365       return false;
366     
e6bb83 367     // read reference schema from mysql.initial.sql
T 368     $db_schema = $this->db_read_schema(INSTALL_PATH . 'SQL/mysql.initial.sql');
2491c6 369     $errors = array();
T 370     
371     // check list of tables
372     $existing_tables = $DB->list_tables();
f6ee6f 373
2491c6 374     foreach ($db_schema as $table => $cols) {
f6ee6f 375       $table = !empty($this->config['db_table_'.$table]) ? $this->config['db_table_'.$table] : $table;
e6bb83 376       if (!in_array($table, $existing_tables)) {
T 377         $errors[] = "Missing table '".$table."'";
378       }
379       else {  // compare cols
380         $db_cols = $DB->list_cols($table);
381         $diff = array_diff(array_keys($cols), $db_cols);
382         if (!empty($diff))
383           $errors[] = "Missing columns in table '$table': " . join(',', $diff);
384       }
2491c6 385     }
398bff 386
2491c6 387     return !empty($errors) ? $errors : false;
T 388   }
e6bb83 389
T 390   /**
391    * Utility function to read database schema from an .sql file
392    */
393   private function db_read_schema($schemafile)
394   {
395     $lines = file($schemafile);
396     $table_block = false;
397     $schema = array();
398     foreach ($lines as $line) {
399       if (preg_match('/^\s*create table `?([a-z0-9_]+)`?/i', $line, $m)) {
400         $table_block = $m[1];
401       }
402       else if ($table_block && preg_match('/^\s*`?([a-z0-9_-]+)`?\s+([a-z]+)/', $line, $m)) {
403         $col = $m[1];
404         if (!in_array(strtoupper($col), array('PRIMARY','KEY','INDEX','UNIQUE','CONSTRAINT','REFERENCES','FOREIGN'))) {
405           $schema[$table_block][$col] = $m[2];
406         }
407       }
408     }
398bff 409
e6bb83 410     return $schema;
871ca9 411   }
5fed07 412
AM 413
871ca9 414   /**
c5042d 415    * Getter for the last error message
T 416    *
417    * @return string Error message or null if none exists
418    */
419   function get_error()
420   {
421       return $this->last_error['message'];
354978 422   }
5fed07 423
AM 424
354978 425   /**
112c54 426    * Return a list with all imap hosts configured
T 427    *
428    * @return array Clean list with imap hosts
429    */
430   function get_hostlist()
431   {
432     $default_hosts = (array)$this->getprop('default_host');
433     $out = array();
5fed07 434
112c54 435     foreach ($default_hosts as $key => $name) {
T 436       if (!empty($name))
058eb6 437         $out[] = rcube_parse_host(is_numeric($key) ? $name : $key);
112c54 438     }
5fed07 439
112c54 440     return $out;
e6bb83 441   }
7177b5 442
e6bb83 443   /**
T 444    * Create a HTML dropdown to select a previous version of Roundcube
445    */
446   function versions_select($attrib = array())
447   {
448     $select = new html_select($attrib);
7177b5 449     $select->add(array(
A 450         '0.1-stable', '0.1.1',
451         '0.2-alpha', '0.2-beta', '0.2-stable',
452         '0.3-stable', '0.3.1',
453         '0.4-beta', '0.4.2',
454         '0.5-beta', '0.5', '0.5.1',
455         '0.6-beta', '0.6',
2c4d0b 456         '0.7-beta', '0.7', '0.7.1', '0.7.2',
d55c02 457         '0.8-beta', '0.8-rc', '0.8.0',
7177b5 458     ));
e6bb83 459     return $select;
112c54 460   }
7177b5 461
e49064 462   /**
T 463    * Return a list with available subfolders of the skin directory
464    */
465   function list_skins()
466   {
467     $skins = array();
468     $skindir = INSTALL_PATH . 'skins/';
469     foreach (glob($skindir . '*') as $path) {
470       if (is_dir($path) && is_readable($path)) {
471         $skins[] = substr($path, strlen($skindir));
472       }
473     }
474     return $skins;
475   }
5fed07 476
112c54 477   /**
354978 478    * Display OK status
T 479    *
480    * @param string Test name
481    * @param string Confirm message
482    */
483   function pass($name, $message = '')
484   {
485     echo Q($name) . ':&nbsp; <span class="success">OK</span>';
6557d3 486     $this->_showhint($message);
354978 487   }
5fed07 488
AM 489
354978 490   /**
T 491    * Display an error status and increase failure count
492    *
493    * @param string Test name
494    * @param string Error message
495    * @param string URL for details
496    */
497   function fail($name, $message = '', $url = '')
498   {
499     $this->failures++;
5fed07 500
354978 501     echo Q($name) . ':&nbsp; <span class="fail">NOT OK</span>';
6557d3 502     $this->_showhint($message, $url);
354978 503   }
11e670 504
A 505
506   /**
507    * Display an error status for optional settings/features
508    *
509    * @param string Test name
510    * @param string Error message
511    * @param string URL for details
512    */
513   function optfail($name, $message = '', $url = '')
514   {
515     echo Q($name) . ':&nbsp; <span class="na">NOT OK</span>';
516     $this->_showhint($message, $url);
517   }
5fed07 518
AM 519
354978 520   /**
T 521    * Display warning status
522    *
523    * @param string Test name
524    * @param string Warning message
525    * @param string URL for details
526    */
6557d3 527   function na($name, $message = '', $url = '')
354978 528   {
6557d3 529     echo Q($name) . ':&nbsp; <span class="na">NOT AVAILABLE</span>';
T 530     $this->_showhint($message, $url);
531   }
5fed07 532
AM 533
6557d3 534   function _showhint($message, $url = '')
T 535   {
536     $hint = Q($message);
5fed07 537
354978 538     if ($url)
6557d3 539       $hint .= ($hint ? '; ' : '') . 'See <a href="' . Q($url) . '" target="_blank">' . Q($url) . '</a>';
5fed07 540
6557d3 541     if ($hint)
T 542       echo '<span class="indent">(' . $hint . ')</span>';
354978 543   }
5fed07 544
AM 545
5f25a1 546   static function _clean_array($arr)
c5042d 547   {
T 548     $out = array();
5fed07 549
5f25a1 550     foreach (array_unique($arr) as $k => $val) {
T 551       if (!empty($val)) {
552         if (is_numeric($k))
553           $out[] = $val;
554         else
555           $out[$k] = $val;
556       }
557     }
5fed07 558
c5042d 559     return $out;
T 560   }
5fed07 561
AM 562
0829b7 563   static function _dump_var($var, $name=null) {
A 564     // special values
565     switch ($name) {
566     case 'syslog_facility':
567       $list = array(32 => 'LOG_AUTH', 80 => 'LOG_AUTHPRIV', 72 => ' LOG_CRON',
568                     24 => 'LOG_DAEMON', 0 => 'LOG_KERN', 128 => 'LOG_LOCAL0',
569                     136 => 'LOG_LOCAL1', 144 => 'LOG_LOCAL2', 152 => 'LOG_LOCAL3',
570                     160 => 'LOG_LOCAL4', 168 => 'LOG_LOCAL5', 176 => 'LOG_LOCAL6',
571                     184 => 'LOG_LOCAL7', 48 => 'LOG_LPR', 16 => 'LOG_MAIL',
572                     56 => 'LOG_NEWS', 40 => 'LOG_SYSLOG', 8 => 'LOG_USER', 64 => 'LOG_UUCP');
573       if ($val = $list[$var])
574         return $val;
575       break;
576     }
577
578
5f25a1 579     if (is_array($var)) {
T 580       if (empty($var)) {
581         return 'array()';
582       }
583       else {  // check if all keys are numeric
584         $isnum = true;
585         foreach ($var as $key => $value) {
586           if (!is_numeric($key)) {
587             $isnum = false;
588             break;
589           }
590         }
5fed07 591
5f25a1 592         if ($isnum)
T 593           return 'array(' . join(', ', array_map(array('rcube_install', '_dump_var'), $var)) . ')';
594       }
595     }
5fed07 596
5f25a1 597     return var_export($var, true);
T 598   }
5fed07 599
AM 600
190e97 601   /**
T 602    * Initialize the database with the according schema
603    *
604    * @param object rcube_db Database connection
605    * @return boolen True on success, False on error
606    */
607   function init_db($DB)
608   {
e6bb83 609     $engine = isset($this->db_map[$DB->db_provider]) ? $this->db_map[$DB->db_provider] : $DB->db_provider;
5fed07 610
190e97 611     // read schema file from /SQL/*
e6bb83 612     $fname = INSTALL_PATH . "SQL/$engine.initial.sql";
T 613     if ($sql = @file_get_contents($fname)) {
614       $this->exec_sql($sql, $DB);
190e97 615     }
T 616     else {
617       $this->fail('DB Schema', "Cannot read the schema file: $fname");
618       return false;
619     }
5fed07 620
190e97 621     if ($err = $this->get_error()) {
T 622       $this->fail('DB Schema', "Error creating database schema: $err");
623       return false;
624     }
625
626     return true;
627   }
5fed07 628
AM 629
e6bb83 630   /**
T 631    * Update database with SQL statements from SQL/*.update.sql
632    *
633    * @param object rcube_db Database connection
634    * @param string Version to update from
635    * @return boolen True on success, False on error
636    */
637   function update_db($DB, $version)
638   {
639     $version = strtolower($version);
640     $engine = isset($this->db_map[$DB->db_provider]) ? $this->db_map[$DB->db_provider] : $DB->db_provider;
5fed07 641
e6bb83 642     // read schema file from /SQL/*
T 643     $fname = INSTALL_PATH . "SQL/$engine.update.sql";
644     if ($lines = @file($fname, FILE_SKIP_EMPTY_LINES)) {
645       $from = false; $sql = '';
646       foreach ($lines as $line) {
647         $is_comment = preg_match('/^--/', $line);
648         if (!$from && $is_comment && preg_match('/from version\s([0-9.]+[a-z-]*)/', $line, $m)) {
649           $v = strtolower($m[1]);
650           if ($v == $version || version_compare($version, $v, '<='))
651             $from = true;
652         }
653         if ($from && !$is_comment)
654           $sql .= $line. "\n";
655       }
5fed07 656
e6bb83 657       if ($sql)
T 658         $this->exec_sql($sql, $DB);
659     }
660     else {
661       $this->fail('DB Schema', "Cannot read the update file: $fname");
662       return false;
663     }
5fed07 664
e6bb83 665     if ($err = $this->get_error()) {
T 666       $this->fail('DB Schema', "Error updating database: $err");
667       return false;
668     }
669
670     return true;
671   }
5fed07 672
AM 673
e6bb83 674   /**
T 675    * Execute the given SQL queries on the database connection
676    *
677    * @param string SQL queries to execute
678    * @param object rcube_db Database connection
679    * @return boolen True on success, False on error
680    */
681   function exec_sql($sql, $DB)
682   {
683     $buff = '';
684     foreach (explode("\n", $sql) as $line) {
685       if (preg_match('/^--/', $line) || trim($line) == '')
686         continue;
5fed07 687
e6bb83 688       $buff .= $line . "\n";
T 689       if (preg_match('/(;|^GO)$/', trim($line))) {
690         $DB->query($buff);
691         $buff = '';
692         if ($DB->is_error())
693           break;
694       }
695     }
5fed07 696
e6bb83 697     return !$DB->is_error();
T 698   }
5fed07 699
AM 700
c5042d 701   /**
e019f2 702    * Handler for Roundcube errors
c5042d 703    */
T 704   function raise_error($p)
705   {
706       $this->last_error = $p;
707   }
5fed07 708
AM 709
354978 710   /**
T 711    * Generarte a ramdom string to be used as encryption key
712    *
713    * @param int Key length
714    * @return string The generated random string
715    * @static
716    */
717   function random_key($length)
718   {
719     $alpha = 'ABCDEFGHIJKLMNOPQERSTUVXYZabcdefghijklmnopqrtsuvwxyz0123456789+*%&?!$-_=';
720     $out = '';
5fed07 721
354978 722     for ($i=0; $i < $length; $i++)
T 723       $out .= $alpha{rand(0, strlen($alpha)-1)};
5fed07 724
354978 725     return $out;
T 726   }
5fed07 727
c5042d 728 }
T 729