4 +-----------------------------------------------------------------------+
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 +-----------------------------------------------------------------------+
18 * Class to control the installation process of the RoundCube Webmail package
22 * @author Thomas Bruederli
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();
38 function rcube_install()
40 $this->step = intval($_REQUEST['_step']);
41 $this->is_post = $_SERVER['REQUEST_METHOD'] == 'POST';
47 function get_instance()
52 $inst = new rcube_install();
58 * Read the default config files and store properties
60 function load_defaults()
62 $this->_load_config('.php.dist');
67 * Read the local config files and store properties
69 function load_config()
71 $this->config = array();
72 $this->_load_config('.php');
73 $this->configured = !empty($this->config);
77 * Read the default config file and store properties
80 function _load_config($suffix)
82 @include '../config/main.inc' . $suffix;
83 if (is_array($rcmail_config)) {
84 $this->config += $rcmail_config;
87 @include '../config/db.inc'. $suffix;
88 if (is_array($rcmail_config)) {
89 $this->config += $rcmail_config;
95 * Getter for a certain config property
97 * @param string Property name
98 * @param string Default value
99 * @return string The property value
101 function getprop($name, $default = '')
103 $value = $this->is_post && (isset($_POST["_$name"]) || $this->config_props[$name]) ? $_POST["_$name"] : $this->config[$name];
105 if ($name == 'des_key' && !isset($_REQUEST["_$name"]))
106 $value = rcube_install::random_key(24);
108 return $value !== null && $value !== '' ? $value : $default;
113 * Take the default config file and replace the parameters
114 * with the submitted form data
116 * @param string Which config file (either 'main' or 'db')
117 * @return string The complete config file content
119 function create_config($which)
121 $out = file_get_contents("../config/{$which}.inc.php.dist");
124 return '[Warning: could not read the template file]';
126 foreach ($this->config as $prop => $default) {
127 $value = (isset($_POST["_$prop"]) || $this->config_props[$prop]) ? $_POST["_$prop"] : $default;
129 // convert some form data
130 if ($prop == 'debug_level' && is_array($value)) {
132 foreach ($value as $i => $dbgval)
133 $val += intval($dbgval);
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']);
140 $value = sprintf('%s://%s:%s@%s/%s', $_POST['_dbtype'], $_POST['_dbuser'], $_POST['_dbpass'], $_POST['_dbhost'], $_POST['_dbname']);
142 else if ($prop == 'smtp_auth_type' && $value == '0') {
145 else if ($prop == 'default_host' && is_array($value)) {
146 $value = rcube_install::_clean_array($value);
147 if (count($value) <= 1)
150 else if ($prop == 'smtp_user' && !empty($_POST['_smtp_user_u'])) {
153 else if ($prop == 'smtp_pass' && !empty($_POST['_smtp_user_u'])) {
156 else if (is_bool($default)) {
157 $value = (bool)$value;
159 else if (is_numeric($value)) {
160 $value = intval($value);
163 // skip this property
164 if ($value == $default)
167 // replace the matching line in config file
169 '/(\$rcmail_config\[\''.preg_quote($prop).'\'\])\s+=\s+(.+);/Uie',
170 "'\\1 = ' . var_export(\$value, true) . ';'",
179 * Getter for the last error message
181 * @return string Error message or null if none exists
185 return $this->last_error['message'];
190 * Return a list with all imap hosts configured
192 * @return array Clean list with imap hosts
194 function get_hostlist()
196 $default_hosts = (array)$this->getprop('default_host');
199 foreach ($default_hosts as $key => $name) {
201 $out[] = is_numeric($key) ? $name : $key;
211 * @param string Test name
212 * @param string Confirm message
214 function pass($name, $message = '')
216 echo Q($name) . ': <span class="success">OK</span>';
217 $this->_showhint($message);
222 * Display an error status and increase failure count
224 * @param string Test name
225 * @param string Error message
226 * @param string URL for details
228 function fail($name, $message = '', $url = '')
232 echo Q($name) . ': <span class="fail">NOT OK</span>';
233 $this->_showhint($message, $url);
238 * Display warning status
240 * @param string Test name
241 * @param string Warning message
242 * @param string URL for details
244 function na($name, $message = '', $url = '')
246 echo Q($name) . ': <span class="na">NOT AVAILABLE</span>';
247 $this->_showhint($message, $url);
251 function _showhint($message, $url = '')
256 $hint .= ($hint ? '; ' : '') . 'See <a href="' . Q($url) . '" target="_blank">' . Q($url) . '</a>';
259 echo '<span class="indent">(' . $hint . ')</span>';
263 function _clean_array($arr)
267 foreach (array_unique($arr) as $i => $val)
276 * Initialize the database with the according schema
278 * @param object rcube_db Database connection
279 * @return boolen True on success, False on error
281 function init_db($DB)
283 $db_map = array('pgsql' => 'postgres', 'mysqli' => 'mysql');
284 $engine = isset($db_map[$DB->db_provider]) ? $db_map[$DB->db_provider] : $DB->db_provider;
286 // find out db version
287 if ($engine == 'mysql') {
288 $DB->query('SELECT VERSION() AS version');
289 $sql_arr = $DB->fetch_assoc();
290 $version = floatval($sql_arr['version']);
296 // read schema file from /SQL/*
297 $fname = "../SQL/$engine.initial.sql";
298 if ($lines = @file($fname, FILE_SKIP_EMPTY_LINES)) {
300 foreach ($lines as $i => $line) {
301 if (eregi('^--', $line))
304 $buff .= $line . "\n";
305 if (eregi(';$', trim($line))) {
312 $this->fail('DB Schema', "Cannot read the schema file: $fname");
316 if ($err = $this->get_error()) {
317 $this->fail('DB Schema', "Error creating database schema: $err");
325 * Handler for RoundCube errors
327 function raise_error($p)
329 $this->last_error = $p;
334 * Generarte a ramdom string to be used as encryption key
336 * @param int Key length
337 * @return string The generated random string
340 function random_key($length)
342 $alpha = 'ABCDEFGHIJKLMNOPQERSTUVXYZabcdefghijklmnopqrtsuvwxyz0123456789+*%&?!$-_=';
345 for ($i=0; $i < $length; $i++)
346 $out .= $alpha{rand(0, strlen($alpha)-1)};
355 * Shortcut function for htmlentities()
357 * @param string String to quote
358 * @return string The html-encoded string
362 return htmlentities($string);
367 * Fake rinternal error handler to catch errors
369 function raise_error($p)
371 $rci = rcube_install::get_instance();
372 $rci->raise_error($p);