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