alecpl
2008-09-14 108d3b0836d9fd576a21f2518296aa2d9e6d5837
commit | author | age
354978 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | rcube_install.php                                                     |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail package                    |
8  | Copyright (C) 2008, RoundCube Dev. - Switzerland                      |
9  | Licensed under the GNU Public License                                 |
10  +-----------------------------------------------------------------------+
11
12  $Id:  $
13
14 */
15
16
17 /**
18  * Class to control the installation process of the RoundCube Webmail package
19  *
20  * @category Install
21  * @package  RoundCube
22  * @author Thomas Bruederli
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])';
27564f 33   var $config_props = array();
354978 34   
T 35   /**
36    * Constructor
37    */
38   function rcube_install()
39   {
40     $this->step = intval($_REQUEST['_step']);
237119 41     $this->is_post = $_SERVER['REQUEST_METHOD'] == 'POST';
354978 42   }
T 43   
c5042d 44   /**
T 45    * Singleton getter
46    */
47   function get_instance()
48   {
49     static $inst;
50     
51     if (!$inst)
52       $inst = new rcube_install();
53     
54     return $inst;
55   }
354978 56   
T 57   /**
c5042d 58    * Read the default config files and store properties
354978 59    */
190e97 60   function load_defaults()
354978 61   {
c5042d 62     $this->_load_config('.php.dist');
T 63   }
64
65
66   /**
67    * Read the local config files and store properties
68    */
69   function load_config()
70   {
b3f9df 71     $this->config = array();
c5042d 72     $this->_load_config('.php');
b3f9df 73     $this->configured = !empty($this->config);
c5042d 74   }
T 75
76   /**
77    * Read the default config file and store properties
78    * @access private
79    */
80   function _load_config($suffix)
81   {
1c4e5d 82     @include '../config/main.inc' . $suffix;
354978 83     if (is_array($rcmail_config)) {
190e97 84       $this->config += $rcmail_config;
354978 85     }
T 86       
c5042d 87     @include '../config/db.inc'. $suffix;
354978 88     if (is_array($rcmail_config)) {
c5042d 89       $this->config += $rcmail_config;
354978 90     }
T 91   }
92   
93   
94   /**
95    * Getter for a certain config property
96    *
97    * @param string Property name
ad43e6 98    * @param string Default value
354978 99    * @return string The property value
T 100    */
fa7539 101   function getprop($name, $default = '')
354978 102   {
b77d0d 103     $value = $this->config[$name];
354978 104     
ccb412 105     if ($name == 'des_key' && !$this->configured && !isset($_REQUEST["_$name"]))
807d17 106       $value = rcube_install::random_key(24);
354978 107     
fa7539 108     return $value !== null && $value !== '' ? $value : $default;
354978 109   }
T 110   
111   
112   /**
113    * Take the default config file and replace the parameters
114    * with the submitted form data
115    *
116    * @param string Which config file (either 'main' or 'db')
117    * @return string The complete config file content
118    */
119   function create_config($which)
120   {
121     $out = file_get_contents("../config/{$which}.inc.php.dist");
122     
123     if (!$out)
124       return '[Warning: could not read the template file]';
0c3bde 125
c5042d 126     foreach ($this->config as $prop => $default) {
27564f 127       $value = (isset($_POST["_$prop"]) || $this->config_props[$prop]) ? $_POST["_$prop"] : $default;
354978 128       
T 129       // convert some form data
b77d0d 130       if ($prop == 'debug_level') {
354978 131         $val = 0;
b77d0d 132         if (isset($value))
A 133       foreach ($value as $dbgval)
134             $val += intval($dbgval);
135     $value = $val;
354978 136       }
0c3bde 137       else if ($which == 'db' && $prop == 'db_dsnw' && !empty($_POST['_dbtype'])) {
237119 138         if ($_POST['_dbtype'] == 'sqlite')
T 139           $value = sprintf('%s://%s?mode=0646', $_POST['_dbtype'], $_POST['_dbname']{0} == '/' ? '/' . $_POST['_dbname'] : $_POST['_dbname']);
140         else
b61965 141           $value = sprintf('%s://%s:%s@%s/%s', $_POST['_dbtype'], 
A 142             rawurlencode($_POST['_dbuser']), rawurlencode($_POST['_dbpass']),
143             $_POST['_dbhost'], $_POST['_dbname']);
c5042d 144       }
T 145       else if ($prop == 'smtp_auth_type' && $value == '0') {
146         $value = '';
147       }
148       else if ($prop == 'default_host' && is_array($value)) {
807d17 149         $value = rcube_install::_clean_array($value);
c5042d 150         if (count($value) <= 1)
T 151           $value = $value[0];
152       }
ccb412 153       else if ($prop == 'pagesize') {
T 154         $value = max(2, intval($value));
155       }
c5042d 156       else if ($prop == 'smtp_user' && !empty($_POST['_smtp_user_u'])) {
T 157         $value = '%u';
158       }
159       else if ($prop == 'smtp_pass' && !empty($_POST['_smtp_user_u'])) {
160         $value = '%p';
161       }
162       else if (is_bool($default)) {
27564f 163         $value = (bool)$value;
T 164       }
165       else if (is_numeric($value)) {
166         $value = intval($value);
c5042d 167       }
T 168       
169       // skip this property
170       if ($value == $default)
171         continue;
b77d0d 172
A 173       // save change
174       $this->config[$prop] = $value;
175
354978 176       // replace the matching line in config file
T 177       $out = preg_replace(
178         '/(\$rcmail_config\[\''.preg_quote($prop).'\'\])\s+=\s+(.+);/Uie',
179         "'\\1 = ' . var_export(\$value, true) . ';'",
180         $out);
181     }
0c3bde 182
967b34 183     return trim($out);
c5042d 184   }
T 185   
186   
187   /**
188    * Getter for the last error message
189    *
190    * @return string Error message or null if none exists
191    */
192   function get_error()
193   {
194       return $this->last_error['message'];
354978 195   }
T 196   
197   
198   /**
112c54 199    * Return a list with all imap hosts configured
T 200    *
201    * @return array Clean list with imap hosts
202    */
203   function get_hostlist()
204   {
205     $default_hosts = (array)$this->getprop('default_host');
206     $out = array();
207     
208     foreach ($default_hosts as $key => $name) {
209       if (!empty($name))
210         $out[] = is_numeric($key) ? $name : $key;
211     }
212     
213     return $out;
214   }
215   
216   
217   /**
354978 218    * Display OK status
T 219    *
220    * @param string Test name
221    * @param string Confirm message
222    */
223   function pass($name, $message = '')
224   {
225     echo Q($name) . ':&nbsp; <span class="success">OK</span>';
6557d3 226     $this->_showhint($message);
354978 227   }
T 228   
229   
230   /**
231    * Display an error status and increase failure count
232    *
233    * @param string Test name
234    * @param string Error message
235    * @param string URL for details
236    */
237   function fail($name, $message = '', $url = '')
238   {
239     $this->failures++;
240     
241     echo Q($name) . ':&nbsp; <span class="fail">NOT OK</span>';
6557d3 242     $this->_showhint($message, $url);
354978 243   }
T 244   
245   
246   /**
247    * Display warning status
248    *
249    * @param string Test name
250    * @param string Warning message
251    * @param string URL for details
252    */
6557d3 253   function na($name, $message = '', $url = '')
354978 254   {
6557d3 255     echo Q($name) . ':&nbsp; <span class="na">NOT AVAILABLE</span>';
T 256     $this->_showhint($message, $url);
257   }
258   
259   
260   function _showhint($message, $url = '')
261   {
262     $hint = Q($message);
263     
354978 264     if ($url)
6557d3 265       $hint .= ($hint ? '; ' : '') . 'See <a href="' . Q($url) . '" target="_blank">' . Q($url) . '</a>';
T 266       
267     if ($hint)
268       echo '<span class="indent">(' . $hint . ')</span>';
354978 269   }
T 270   
271   
c5042d 272   function _clean_array($arr)
T 273   {
274     $out = array();
275     
276     foreach (array_unique($arr) as $i => $val)
277       if (!empty($val))
278         $out[] = $val;
279     
280     return $out;
281   }
282   
190e97 283   
T 284   /**
285    * Initialize the database with the according schema
286    *
287    * @param object rcube_db Database connection
288    * @return boolen True on success, False on error
289    */
290   function init_db($DB)
291   {
292     $db_map = array('pgsql' => 'postgres', 'mysqli' => 'mysql');
293     $engine = isset($db_map[$DB->db_provider]) ? $db_map[$DB->db_provider] : $DB->db_provider;
294     
295     // find out db version
296     if ($engine == 'mysql') {
297       $DB->query('SELECT VERSION() AS version');
298       $sql_arr = $DB->fetch_assoc();
299       $version = floatval($sql_arr['version']);
300       
301       if ($version >= 4.1)
302         $engine = 'mysql5';
303     }
304     
305     // read schema file from /SQL/*
306     $fname = "../SQL/$engine.initial.sql";
307     if ($lines = @file($fname, FILE_SKIP_EMPTY_LINES)) {
308       $buff = '';
309       foreach ($lines as $i => $line) {
310         if (eregi('^--', $line))
311           continue;
312           
313         $buff .= $line . "\n";
314         if (eregi(';$', trim($line))) {
315           $DB->query($buff);
316           $buff = '';
c0dc90 317           if ($this->get_error())
T 318             break;
190e97 319         }
T 320       }
321     }
322     else {
323       $this->fail('DB Schema', "Cannot read the schema file: $fname");
324       return false;
325     }
326     
327     if ($err = $this->get_error()) {
328       $this->fail('DB Schema', "Error creating database schema: $err");
329       return false;
330     }
331
332     return true;
333   }
334   
c5042d 335   /**
T 336    * Handler for RoundCube errors
337    */
338   function raise_error($p)
339   {
340       $this->last_error = $p;
341   }
342   
343   
354978 344   /**
T 345    * Generarte a ramdom string to be used as encryption key
346    *
347    * @param int Key length
348    * @return string The generated random string
349    * @static
350    */
351   function random_key($length)
352   {
353     $alpha = 'ABCDEFGHIJKLMNOPQERSTUVXYZabcdefghijklmnopqrtsuvwxyz0123456789+*%&?!$-_=';
354     $out = '';
355     
356     for ($i=0; $i < $length; $i++)
357       $out .= $alpha{rand(0, strlen($alpha)-1)};
358     
359     return $out;
360   }
361   
362 }
363
364
365 /**
366  * Shortcut function for htmlentities()
367  *
368  * @param string String to quote
369  * @return string The html-encoded string
370  */
371 function Q($string)
372 {
373   return htmlentities($string);
374 }
375
c5042d 376
T 377 /**
378  * Fake rinternal error handler to catch errors
379  */
380 function raise_error($p)
381 {
382   $rci = rcube_install::get_instance();
383   $rci->raise_error($p);
384 }
385