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