thomascube
2008-02-25 c5042d468e605a7ec9bb6883ba721c8fd222b4ec
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;
27   var $failures = 0;
c5042d 28   var $config = array();
T 29   var $last_error = null;
354978 30   
T 31   /**
32    * Constructor
33    */
34   function rcube_install()
35   {
36     $this->step = intval($_REQUEST['_step']);
37   }
38   
c5042d 39   /**
T 40    * Singleton getter
41    */
42   function get_instance()
43   {
44     static $inst;
45     
46     if (!$inst)
47       $inst = new rcube_install();
48     
49     return $inst;
50   }
354978 51   
T 52   /**
c5042d 53    * Read the default config files and store properties
354978 54    */
T 55   function get_defaults()
56   {
c5042d 57     $this->_load_config('.php.dist');
T 58   }
59
60
61   /**
62    * Read the local config files and store properties
63    */
64   function load_config()
65   {
66     $this->_load_config('.php');
67   }
68
69   /**
70    * Read the default config file and store properties
71    * @access private
72    */
73   function _load_config($suffix)
74   {
75     include '../config/main.inc' . $suffix;
354978 76     if (is_array($rcmail_config)) {
c5042d 77       $this->config = $rcmail_config;
354978 78     }
T 79       
c5042d 80     @include '../config/db.inc'. $suffix;
354978 81     if (is_array($rcmail_config)) {
c5042d 82       $this->config += $rcmail_config;
354978 83     }
T 84   }
85   
86   
87   /**
88    * Getter for a certain config property
89    *
90    * @param string Property name
91    * @return string The property value
92    */
93   function getprop($name)
94   {
c5042d 95     $value = isset($_REQUEST["_$name"]) ? $_REQUEST["_$name"] : $this->config[$name];
354978 96     
T 97     if ($name == 'des_key' && !isset($_REQUEST["_$name"]))
98       $value = self::random_key(24);
99     
100     return $value;
101   }
102   
103   
104   /**
105    * Take the default config file and replace the parameters
106    * with the submitted form data
107    *
108    * @param string Which config file (either 'main' or 'db')
109    * @return string The complete config file content
110    */
111   function create_config($which)
112   {
113     $out = file_get_contents("../config/{$which}.inc.php.dist");
114     
115     if (!$out)
116       return '[Warning: could not read the template file]';
117     
c5042d 118     foreach ($this->config as $prop => $default) {
354978 119       $value = $_POST["_$prop"] ? $_POST["_$prop"] : $default;
T 120       
121       // convert some form data
122       if ($prop == 'debug_level' && is_array($value)) {
123         $val = 0;
124         foreach ($value as $i => $dbgval)
125           $val += intval($dbgval);
126         $value = $val;
127       }
c5042d 128       else if ($prop == 'db_dsnw' && !empty($_POST['_dbtype'])) {
T 129         $value = sprintf('%s://%s:%s@%s/%s', $_POST['_dbtype'], $_POST['_dbuser'], $_POST['_dbpass'], $_POST['_dbhost'], $_POST['_dbname']);
130       }
131       else if ($prop == 'smtp_auth_type' && $value == '0') {
132         $value = '';
133       }
134       else if ($prop == 'default_host' && is_array($value)) {
135         $value = self::_clean_array($value);
136         if (count($value) <= 1)
137           $value = $value[0];
138       }
139       else if ($prop == 'smtp_user' && !empty($_POST['_smtp_user_u'])) {
140         $value = '%u';
141       }
142       else if ($prop == 'smtp_pass' && !empty($_POST['_smtp_user_u'])) {
143         $value = '%p';
144       }
145       else if (is_bool($default)) {
354978 146         $value = is_numeric($value) ? (bool)$value : $value;
c5042d 147       }
T 148       
149       // skip this property
150       if ($value == $default)
151         continue;
354978 152       
T 153       // replace the matching line in config file
154       $out = preg_replace(
155         '/(\$rcmail_config\[\''.preg_quote($prop).'\'\])\s+=\s+(.+);/Uie',
156         "'\\1 = ' . var_export(\$value, true) . ';'",
157         $out);
158     }
159     
160     return $out;
c5042d 161   }
T 162   
163   
164   /**
165    * Getter for the last error message
166    *
167    * @return string Error message or null if none exists
168    */
169   function get_error()
170   {
171       return $this->last_error['message'];
354978 172   }
T 173   
174   
175   /**
176    * Display OK status
177    *
178    * @param string Test name
179    * @param string Confirm message
180    */
181   function pass($name, $message = '')
182   {
183     echo Q($name) . ':&nbsp; <span class="success">OK</span>';
6557d3 184     $this->_showhint($message);
354978 185   }
T 186   
187   
188   /**
189    * Display an error status and increase failure count
190    *
191    * @param string Test name
192    * @param string Error message
193    * @param string URL for details
194    */
195   function fail($name, $message = '', $url = '')
196   {
197     $this->failures++;
198     
199     echo Q($name) . ':&nbsp; <span class="fail">NOT OK</span>';
6557d3 200     $this->_showhint($message, $url);
354978 201   }
T 202   
203   
204   /**
205    * Display warning status
206    *
207    * @param string Test name
208    * @param string Warning message
209    * @param string URL for details
210    */
6557d3 211   function na($name, $message = '', $url = '')
354978 212   {
6557d3 213     echo Q($name) . ':&nbsp; <span class="na">NOT AVAILABLE</span>';
T 214     $this->_showhint($message, $url);
215   }
216   
217   
218   function _showhint($message, $url = '')
219   {
220     $hint = Q($message);
221     
354978 222     if ($url)
6557d3 223       $hint .= ($hint ? '; ' : '') . 'See <a href="' . Q($url) . '" target="_blank">' . Q($url) . '</a>';
T 224       
225     if ($hint)
226       echo '<span class="indent">(' . $hint . ')</span>';
354978 227   }
T 228   
229   
c5042d 230   function _clean_array($arr)
T 231   {
232     $out = array();
233     
234     foreach (array_unique($arr) as $i => $val)
235       if (!empty($val))
236         $out[] = $val;
237     
238     return $out;
239   }
240   
241   /**
242    * Handler for RoundCube errors
243    */
244   function raise_error($p)
245   {
246       $this->last_error = $p;
247   }
248   
249   
354978 250   /**
T 251    * Generarte a ramdom string to be used as encryption key
252    *
253    * @param int Key length
254    * @return string The generated random string
255    * @static
256    */
257   function random_key($length)
258   {
259     $alpha = 'ABCDEFGHIJKLMNOPQERSTUVXYZabcdefghijklmnopqrtsuvwxyz0123456789+*%&?!$-_=';
260     $out = '';
261     
262     for ($i=0; $i < $length; $i++)
263       $out .= $alpha{rand(0, strlen($alpha)-1)};
264     
265     return $out;
266   }
267   
268 }
269
270
271 /**
272  * Shortcut function for htmlentities()
273  *
274  * @param string String to quote
275  * @return string The html-encoded string
276  */
277 function Q($string)
278 {
279   return htmlentities($string);
280 }
281
c5042d 282
T 283 /**
284  * Fake rinternal error handler to catch errors
285  */
286 function raise_error($p)
287 {
288   $rci = rcube_install::get_instance();
289   $rci->raise_error($p);
290 }
291