From c5042d468e605a7ec9bb6883ba721c8fd222b4ec Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Mon, 25 Feb 2008 18:03:54 -0500
Subject: [PATCH] More installer stuff

---
 installer/images/delete.png |    0 
 installer/rcube_install.php |  119 +++++++++-
 installer/styles.css        |   23 ++
 installer/config.php        |  295 ++++++++++++++++++++++++--
 installer/check.php         |   29 ++
 installer/test.php          |  116 ++++++++++
 installer/client.js         |   36 +++
 installer/images/add.png    |    0 
 installer/index.php         |    7 
 9 files changed, 571 insertions(+), 54 deletions(-)

diff --git a/installer/check.php b/installer/check.php
index 33264a0..9f54d8f 100644
--- a/installer/check.php
+++ b/installer/check.php
@@ -1,9 +1,5 @@
 <?php
 
-$docroot = realpath(dirname(__FILE__) . '/../');
-$include_path  = $docroot . '/program/lib' . PATH_SEPARATOR . $docroot . '/program' . PATH_SEPARATOR . ini_get('include_path');
-set_include_path($include_path);
-
 $required_php_exts = array('PCRE' => 'pcre', 'Session' => 'session', 'Sockets' => 'sockets');
 
 $optional_php_exts = array('FileInfo' => 'fileinfo', 'Libiconv' => 'iconv', 'Multibyte' => 'mbstring', 'OpenSSL' => 'openssl');
@@ -13,6 +9,10 @@
 
 $supported_dbs = array('MySQL' => 'mysql', 'MySQLi' => 'mysqli',
     'PostgreSQL' => 'pgsql', 'SQLite (v2)' => 'sqlite');
+
+$ini_checks = array('file_uploads' => 1, 'session.auto_start' => 0,
+    'magic_quotes_gpc' => 0, 'magic_quotes_sybase' => 0,
+    'zlib.output_compression' => 0);
 
 $source_urls = array(
     'Sockets' => 'http://www.php.net/manual/en/ref.sockets.php',
@@ -122,6 +122,27 @@
     echo "<br />";
 }
 
+
+?>
+
+<h3>Checking php.ini/.htaccess settings</h3>
+
+<?php
+
+foreach ($ini_checks as $var => $val) {
+    $status = ini_get($var);
+    if ($status == $val) {
+        $RCI->pass($var);
+    }
+    else {
+      $RCI->fail($var, "is '$status', should be '$val'");
+    }
+    echo '<br />';
+}
+?>
+
+<?php
+
 if ($RCI->failures)
   echo '<p class="warning">Sorry but your webserver does not meet the requirements for RoundCube!<br />
             Please install the missing modules according to the above check results.</p>';
diff --git a/installer/client.js b/installer/client.js
new file mode 100644
index 0000000..3121b26
--- /dev/null
+++ b/installer/client.js
@@ -0,0 +1,36 @@
+
+function toggleblock(id, link)
+{
+  var block = document.getElementById(id);
+  
+  return false;
+}
+
+
+function addhostfield()
+{
+  var container = document.getElementById('defaulthostlist');
+  var row = document.createElement('div');
+  var input = document.createElement('input');
+  var link = document.createElement('a');
+  
+  input.name = '_default_host[]';
+  input.size = '30';
+  link.href = '#';
+  link.onclick = function() { removehostfield(this.parentNode); return false };
+  link.className = 'removelink';
+  link.innerHTML = 'remove';
+  
+  row.appendChild(input);
+  row.appendChild(link);
+  container.appendChild(row);
+}
+
+
+function removehostfield(row)
+{
+  var container = document.getElementById('defaulthostlist');
+  container.removeChild(row);
+}
+
+
diff --git a/installer/config.php b/installer/config.php
index f99c049..63031b6 100644
--- a/installer/config.php
+++ b/installer/config.php
@@ -3,14 +3,35 @@
 <?php
 
 ini_set('display_errors', 1);
-require_once '../program/include/rcube_html.inc';
+require_once 'include/rcube_html.inc';
+
+$RCI->get_defaults();
+
+if (!empty($_POST['submit'])) {
+  
+  echo '<p class="notice">Copy the following configurations and save them in two files (names above the text box)';
+  echo ' within the <tt>config/</tt> directory of your RoundCube installation.</p>';
+  
+  $textbox = new textarea(array('rows' => 16, 'cols' => 60, 'class' => "configfile"));
+  
+  echo '<div><em>main.inc.php</em></div>';
+  echo $textbox->show($RCI->create_config('main'));
+  
+  echo '<div style="margin-top:1em"><em>db.inc.php</em></div>';
+  echo $textbox->show($RCI->create_config('db'));
+
+  echo '<p><input type="button" onclick="location.href=\'./index.php?_step=3\'" value="CONTINUE" /></p>';
+  
+  // echo '<style type="text/css"> .configblock { display:none } </style>';
+  echo "\n<hr style='margin-bottom:1.6em' />\n";
+}
 
 ?>
 <fieldset>
 <legend>General configuration</legend>
-<dl>
+<dl class="configblock">
 <!--
-<dt class="propname">debug_level</dt>
+<dt id="cgfblockgeneral" class="propname">debug_level</dt>
 <dd>
 <?php
 /*
@@ -81,7 +102,7 @@
 echo $check_ipcheck->show(intval($RCI->getprop('ip_check')), array('value' => 1));
 
 ?>
-<label for="cfgipcheck">Check client IP in session athorization</label><br />
+<label for="cfgipcheck">Check client IP in session authorization</label><br />
 
 <p class="hint">This increases security but can cause sudden logouts when someone uses a proxy with changeing IPs.</p>
 </dd>
@@ -110,19 +131,148 @@
 <label for="cfgcache">Cache messages in local database</label><br />
 </dd>
 
+<dt class="propname">enable_spellcheck</dt>
+<dd>
+<?php
+
+$check_caching = new checkbox(array('name' => '_enable_spellcheck', 'id' => "cfgspellcheck"));
+echo $check_caching->show(intval($RCI->getprop('enable_spellcheck')), array('value' => 1));
+
+?>
+<label for="cfgspellcheck">Make use of the built-in spell checker</label><br />
+
+<p class="hint">It is based on GoogieSpell what implies that the message content will be sent to Google in order to check the spelling.</p>
+</dd>
+
+<dt class="propname">mdn_requests</dt>
+<dd>
+<?php
+
+$select_mdnreq = new select(array('name' => '_mdn_requests', 'id' => "cfgmdnreq"));
+$select_mdnreq->add(array('ask the user', 'send automatically', 'ignore'), array(0, 1, 2));
+echo $select_mdnreq->show(intval($RCI->getprop('mdn_requests')));
+
+?>
+<div>Behavior if a received message requests a message delivery notification (read receipt)</div>
+</dd>
+
 </dl>
 </fieldset>
 
 <fieldset>
 <legend>Database setup</legend>
-<dl>
-<dd>TBD.</dd>
+<dl class="configblock" id="cgfblockdb">
+<dt class="propname">db_dsnw</dt>
+<dd>
+<p>Database settings for read/write operations:</p>
+<?php
+
+$supported_dbs = array('MySQL' => 'mysql', 'MySQLi' => 'mysqli',
+    'PgSQL' => 'pgsql', 'SQLite' => 'sqlite');
+
+$select_dbtype = new select(array('name' => '_dbtype', 'id' => "cfgdbtype"));
+foreach ($supported_dbs AS $database => $ext) {
+    if (extension_loaded($ext)) {
+        $select_dbtype->add($database, $ext);
+    }
+}
+
+$input_dbhost = new textfield(array('name' => '_dbhost', 'size' => 20, 'id' => "cfgdbhost"));
+$input_dbname = new textfield(array('name' => '_dbname', 'size' => 20, 'id' => "cfgdbname"));
+$input_dbuser = new textfield(array('name' => '_dbuser', 'size' => 20, 'id' => "cfgdbuser"));
+$input_dbpass = new textfield(array('name' => '_dbpass', 'size' => 20, 'id' => "cfgdbpass"));
+
+$dsnw = parse_url($RCI->getprop('db_dsnw'));
+
+echo $select_dbtype->show($_POST['_dbtype'] ? $_POST['_dbtype'] : $dsnw['scheme']);
+echo '<label for="cfgdbtype">Database type</label><br />';
+echo $input_dbhost->show($_POST['_dbhost'] ? $_POST['_dbhost'] : $dsnw['host']);
+echo '<label for="cfgdbhost">Database server</label><br />';
+echo $input_dbname->show($_POST['_dbname'] ? $_POST['_dbname'] : preg_replace('/^\//', '', $dsnw['path']));
+echo '<label for="cfgdbname">Database name</label><br />';
+echo $input_dbuser->show($_POST['_dbuser'] ? $_POST['_dbuser'] : $dsnw['user']);
+echo '<label for="cfgdbuser">Database user name (needs write permissions)</label><br />';
+echo $input_dbpass->show($_POST['_dbpass'] ? $_POST['_dbpass'] : $dsnw['pass']);
+echo '<label for="cfgdbpass">Database password</label><br />';
+
+?>
+</dd>
+
+<dt class="propname">db_backend</dt>
+<dd>
+<?php
+
+// check for existing PEAR classes
+@include_once 'DB.php';
+@include_once 'MDB2.php';
+
+$select_dbba = new select(array('name' => '_db_backend', 'id' => "cfgdbba"));
+
+if (class_exists('DB'))
+  $select_dbba->add('DB', 'db');
+if (class_exists('MDB2'))
+  $select_dbba->add('MDB2', 'mdb2');
+
+echo $select_dbba->show($RCI->getprop('db_backend'));
+
+?>
+<div>PEAR Database backend to use</div>
+</dd>
+
 </dl>
 </fieldset>
 
+
 <fieldset>
 <legend>IMAP Settings</legend>
-<dl>
+<dl class="configblock" id="cgfblockimap">
+<dt class="propname">default_host</dt>
+<dd>
+<div>The IMAP host(s) chosen to perform the log-in</div>
+<div id="defaulthostlist">
+<?php
+
+$default_hosts = array_unique((array)$RCI->getprop('default_host'));
+$text_imaphost = new textfield(array('name' => '_default_host[]', 'size' => 30));
+
+for ($i=0; $i < count($default_hosts); $i++) {
+  echo '<div id="defaulthostentry'.$i.'">' . $text_imaphost->show($default_hosts[$i]);
+  if ($i > 0)
+    echo '<a href="#" onclick="removehostfield(this.parentNode);return false" class="removelink" title="Remove this entry">remove</a>';
+  echo '</div>';
+}
+
+?>
+</div>
+<div><a href="javascript:addhostfield()" class="addlink" title="Add another field">add</a></div>
+
+<p class="hint">Leave blank to show a textbox at login</p>
+</dd>
+
+<dt class="propname">default_port</dt>
+<dd>
+<?php
+
+$text_imapport = new textfield(array('name' => '_default_port', 'size' => 6, 'id' => "cfgimapport"));
+echo $text_imapport->show($RCI->getprop('default_port'));
+
+?>
+<div>TCP port used for IMAP connections</div>
+</dd>
+
+<dt class="propname">username_domain</dt>
+<dd>
+<?php
+
+$text_userdomain = new textfield(array('name' => '_username_domain', 'size' => 30, 'id' => "cfguserdomain"));
+echo $text_userdomain->show($RCI->getprop('username_domain'));
+
+?>
+<div>Automatically add this domain to user names for login</div>
+
+<p class="hint">Only for IMAP servers that require full e-mail addresses for login</p>
+</dd>
+
 <dt class="propname">auto_create_user</dt>
 <dd>
 <?php
@@ -140,19 +290,126 @@
 what means that you have to create those records manually or disable this option after the first login.</p>
 </dd>
 
+<dt class="propname">sent_mbox</dt>
+<dd>
+<?php
+
+$text_sentmbox = new textfield(array('name' => '_sent_mbox', 'size' => 20, 'id' => "cfgsentmbox"));
+echo $text_sentmbox->show($RCI->getprop('sent_mbox'));
+
+?>
+<div>Store sent messages is this folder</div>
+
+<p class="hint">Leave blank if sent messages should not be stored</p>
+</dd>
+
+<dt class="propname">trash_mbox</dt>
+<dd>
+<?php
+
+$text_trashmbox = new textfield(array('name' => '_trash_mbox', 'size' => 20, 'id' => "cfgtrashmbox"));
+echo $text_trashmbox->show($RCI->getprop('trash_mbox'));
+
+?>
+<div>Move messages to this folder when deleting them</div>
+
+<p class="hint">Leave blank if they should be deleted directly</p>
+</dd>
+
+<dt class="propname">drafts_mbox</dt>
+<dd>
+<?php
+
+$text_draftsmbox = new textfield(array('name' => '_drafts_mbox', 'size' => 20, 'id' => "cfgdraftsmbox"));
+echo $text_draftsmbox->show($RCI->getprop('drafts_mbox'));
+
+?>
+<div>Store draft messages is this folder</div>
+</dd>
+
 </dl>
 </fieldset>
+
 
 <fieldset>
 <legend>SMTP Settings</legend>
-<dl>
-<dd>TBD.</dd>
+<dl class="configblock" id="cgfblocksmtp">
+<dt class="propname">smtp_server</dt>
+<dd>
+<?php
+
+$text_smtphost = new textfield(array('name' => '_smtp_server', 'size' => 30, 'id' => "cfgsmtphost"));
+echo $text_smtphost->show($RCI->getprop('smtp_server'));
+
+?>
+<div>Use this host for sending mails</div>
+
+<p class="hint">To use SSL connection, set ssl://smtp.host.com. If left blank, the PHP mail() function is used</p>
+</dd>
+
+<dt class="propname">smtp_port</dt>
+<dd>
+<?php
+
+$text_smtpport = new textfield(array('name' => '_smtp_port', 'size' => 6, 'id' => "cfgsmtpport"));
+echo $text_smtpport->show($RCI->getprop('smtp_port'));
+
+?>
+<div>SMTP port (default is 25; 465 for SSL)</div>
+</dd>
+
+<dt class="propname">smtp_user/smtp_pass</dt>
+<dd>
+<?php
+
+$text_smtpuser = new textfield(array('name' => '_smtp_user', 'size' => 20, 'id' => "cfgsmtpuser"));
+$text_smtppass = new textfield(array('name' => '_smtp_pass', 'size' => 20, 'id' => "cfgsmtppass"));
+echo $text_smtpuser->show($RCI->getprop('smtp_user'));
+echo $text_smtppass->show($RCI->getprop('smtp_pass'));
+
+?>
+<div>SMTP username and password (if required)</div>
+<p>
+<?php
+
+$check_smtpuser = new checkbox(array('name' => '_smtp_user_u', 'id' => "cfgsmtpuseru"));
+echo $check_smtpuser->show($RCI->getprop('smtp_user') == '%u' || $_POST['_smtp_user_u'] ? 1 : 0, array('value' => 1));
+
+?>
+<label for="cfgsmtpuseru">Use the current IMAP username and password for SMTP authentication</label>
+</p>
+</dd>
+<!--
+<dt class="propname">smtp_auth_type</dt>
+<dd>
+<?php
+/*
+$select_smtpauth = new select(array('name' => '_smtp_auth_type', 'id' => "cfgsmtpauth"));
+$select_smtpauth->add(array('(auto)', 'PLAIN', 'DIGEST-MD5', 'CRAM-MD5', 'LOGIN'), array('0', 'PLAIN', 'DIGEST-MD5', 'CRAM-MD5', 'LOGIN'));
+echo $select_smtpauth->show(intval($RCI->getprop('smtp_auth_type')));
+*/
+?>
+<div>Method to authenticate at the SMTP server. Choose (auto) if you don't know what this is</div>
+</dd>
+-->
+<dt class="propname">smtp_log</dt>
+<dd>
+<?php
+
+$check_smtplog = new checkbox(array('name' => '_smtp_log', 'id' => "cfgsmtplog"));
+echo $check_smtplog->show(intval($RCI->getprop('smtp_log')), array('value' => 1));
+
+?>
+<label for="cfgsmtplog">Log sent messages in <tt>logs/sendmail</tt></label><br />
+</dd>
+
 </dl>
 </fieldset>
 
+
 <fieldset>
 <legend>Display settings</legend>
-<dl>
+<dl class="configblock" id="cgfblockdisplay">
 
 <dt class="propname">locale_string</dt>
 <dd>
@@ -172,24 +429,6 @@
 <?php
 
 echo '<p><input type="submit" name="submit" value="UPDATE" ' . ($RCI->failures ? 'disabled' : '') . ' /></p>';
-
-
-if (!empty($_POST['submit'])) {
-  echo "<hr />\n";
-  
-  echo '<p class="notice">Copy the following configurations and save them in two files (names above the text box)';
-  echo ' within the <tt>config/</tt> directory of your RoundCube installation.</p>';
-  
-  $textbox = new textarea(array('rows' => 20, 'cols' => 60, 'class' => "configfile"));
-  
-  echo '<div><em>main.inc.php</em></div>';
-  echo $textbox->show($RCI->create_config('main'));
-  
-  echo '<div style="margin-top:1em"><em>db.inc.php</em></div>';
-  echo $textbox->show($RCI->create_config('db'));
-
-  echo '<p><input type="button" onclick="location.href=\'./index.php?_step=3\'" value="CONTINUE" /></p>';
-}
 
 ?>
 </form>
diff --git a/installer/images/add.png b/installer/images/add.png
new file mode 100755
index 0000000..6332fef
--- /dev/null
+++ b/installer/images/add.png
Binary files differ
diff --git a/installer/images/delete.png b/installer/images/delete.png
new file mode 100755
index 0000000..08f2493
--- /dev/null
+++ b/installer/images/delete.png
Binary files differ
diff --git a/installer/index.php b/installer/index.php
index a7959e4..d62f6cb 100644
--- a/installer/index.php
+++ b/installer/index.php
@@ -5,6 +5,7 @@
 <title>RoundCube Webmail Installer</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <link rel="stylesheet" type="text/css" href="styles.css" />
+<script type="text/javascript" src="client.js"></script>
 </head>
 
 <body>
@@ -27,8 +28,12 @@
 
   ini_set('display_errors', 1);
 
+  $docroot = realpath(dirname(__FILE__) . '/../');
+  $include_path  = $docroot . '/program/lib' . PATH_SEPARATOR . $docroot . '/program' . PATH_SEPARATOR . ini_get('include_path');
+  set_include_path($include_path);
+
   require_once 'rcube_install.php';
-  $RCI = new rcube_install();
+  $RCI = rcube_install::get_instance();
 
 ?>
 
diff --git a/installer/rcube_install.php b/installer/rcube_install.php
index 2e1df00..136fdd0 100644
--- a/installer/rcube_install.php
+++ b/installer/rcube_install.php
@@ -25,7 +25,8 @@
 {
   var $step;
   var $failures = 0;
-  var $defaults = array();
+  var $config = array();
+  var $last_error = null;
   
   /**
    * Constructor
@@ -33,25 +34,52 @@
   function rcube_install()
   {
     $this->step = intval($_REQUEST['_step']);
-    $this->get_defaults();
   }
   
+  /**
+   * Singleton getter
+   */
+  function get_instance()
+  {
+    static $inst;
+    
+    if (!$inst)
+      $inst = new rcube_install();
+    
+    return $inst;
+  }
   
   /**
-   * Read the default config file and store properties
+   * Read the default config files and store properties
    */
   function get_defaults()
   {
-    $suffix = is_readable('../config/main.inc.php.dist') ? '.dist' : '';
-    
-    include '../config/main.inc.php' . $suffix;
+    $this->_load_config('.php.dist');
+  }
+
+
+  /**
+   * Read the local config files and store properties
+   */
+  function load_config()
+  {
+    $this->_load_config('.php');
+  }
+
+  /**
+   * Read the default config file and store properties
+   * @access private
+   */
+  function _load_config($suffix)
+  {
+    include '../config/main.inc' . $suffix;
     if (is_array($rcmail_config)) {
-      $this->defaults = $rcmail_config;
+      $this->config = $rcmail_config;
     }
       
-    include '../config/db.inc.php'. $suffix;
+    @include '../config/db.inc'. $suffix;
     if (is_array($rcmail_config)) {
-      $this->defaults += $rcmail_config;
+      $this->config += $rcmail_config;
     }
   }
   
@@ -64,7 +92,7 @@
    */
   function getprop($name)
   {
-    $value = isset($_REQUEST["_$name"]) ? $_REQUEST["_$name"] : $this->defaults[$name];
+    $value = isset($_REQUEST["_$name"]) ? $_REQUEST["_$name"] : $this->config[$name];
     
     if ($name == 'des_key' && !isset($_REQUEST["_$name"]))
       $value = self::random_key(24);
@@ -87,12 +115,8 @@
     if (!$out)
       return '[Warning: could not read the template file]';
     
-    foreach ($this->defaults as $prop => $default) {
+    foreach ($this->config as $prop => $default) {
       $value = $_POST["_$prop"] ? $_POST["_$prop"] : $default;
-      
-      // skip this property
-      if (!isset($_POST["_$prop"]) || $value == $default)
-        continue;
       
       // convert some form data
       if ($prop == 'debug_level' && is_array($value)) {
@@ -101,8 +125,30 @@
           $val += intval($dbgval);
         $value = $val;
       }
-      else if (is_bool($default))
+      else if ($prop == 'db_dsnw' && !empty($_POST['_dbtype'])) {
+        $value = sprintf('%s://%s:%s@%s/%s', $_POST['_dbtype'], $_POST['_dbuser'], $_POST['_dbpass'], $_POST['_dbhost'], $_POST['_dbname']);
+      }
+      else if ($prop == 'smtp_auth_type' && $value == '0') {
+        $value = '';
+      }
+      else if ($prop == 'default_host' && is_array($value)) {
+        $value = self::_clean_array($value);
+        if (count($value) <= 1)
+          $value = $value[0];
+      }
+      else if ($prop == 'smtp_user' && !empty($_POST['_smtp_user_u'])) {
+        $value = '%u';
+      }
+      else if ($prop == 'smtp_pass' && !empty($_POST['_smtp_user_u'])) {
+        $value = '%p';
+      }
+      else if (is_bool($default)) {
         $value = is_numeric($value) ? (bool)$value : $value;
+      }
+      
+      // skip this property
+      if ($value == $default)
+        continue;
       
       // replace the matching line in config file
       $out = preg_replace(
@@ -112,6 +158,17 @@
     }
     
     return $out;
+  }
+  
+  
+  /**
+   * Getter for the last error message
+   *
+   * @return string Error message or null if none exists
+   */
+  function get_error()
+  {
+      return $this->last_error['message'];
   }
   
   
@@ -170,6 +227,26 @@
   }
   
   
+  function _clean_array($arr)
+  {
+    $out = array();
+    
+    foreach (array_unique($arr) as $i => $val)
+      if (!empty($val))
+        $out[] = $val;
+    
+    return $out;
+  }
+  
+  /**
+   * Handler for RoundCube errors
+   */
+  function raise_error($p)
+  {
+      $this->last_error = $p;
+  }
+  
+  
   /**
    * Generarte a ramdom string to be used as encryption key
    *
@@ -202,3 +279,13 @@
   return htmlentities($string);
 }
 
+
+/**
+ * Fake rinternal error handler to catch errors
+ */
+function raise_error($p)
+{
+  $rci = rcube_install::get_instance();
+  $rci->raise_error($p);
+}
+
diff --git a/installer/styles.css b/installer/styles.css
index 4045f9d..de4a9ca 100644
--- a/installer/styles.css
+++ b/installer/styles.css
@@ -104,7 +104,7 @@
 }
 
 fieldset {
-  margin-bottom: 2em;
+  margin-bottom: 1.5em;
   border: 1px solid #aaa;
   background-color: #f9f9f9;
 }
@@ -163,6 +163,27 @@
   margin-bottom: 0.2em;
 }
 
+a.blocktoggle {
+  color: #666;
+  text-decoration: none;
+}
+
+a.addlink {
+  color: #999;
+  font-size: 0.9em;
+  padding: 1px 0 1px 20px;
+  background: url('images/add.png') top left no-repeat;
+  text-decoration: none;
+}
+
+a.removelink {
+  color: #999;
+  font-size: 0.9em;
+  padding: 1px 0 1px 24px;
+  background: url('images/delete.png') 4px 0 no-repeat;
+  text-decoration: none;
+}
+
 .hint {
   color: #666;
   font-size: 0.95em;
diff --git a/installer/test.php b/installer/test.php
index 254144e..783cf3c 100644
--- a/installer/test.php
+++ b/installer/test.php
@@ -1,12 +1,120 @@
-<form action="index.php" method="get">
-<input type="hidden" name="_step" value="3" />
+<form action="index.php?_step=3" method="post">
+
+<h3>Check config files</h3>
 <?php
 
-echo '<p>[do some tests as in check.php-dist here]</p>';
+// load local config files
+$RCI->load_config();
 
-echo '<input type="submit" value="EXECUTE TESTS" />';
+$read_main = is_readable('../config/main.inc.php');
+$read_db = is_readable('../config/db.inc.php');
+
+if ($read_main && !empty($RCI->config)) {
+  $RCI->pass('main.inc.php');
+}
+else if ($read_main) {
+  $RCI->fail('main.inc.php', 'Syntax error');
+}
+else if (!$read_main) {
+  $RCI->fail('main.inc.php', 'Unable to read file. Did you create the config files?');
+}
+echo '<br />';
+
+if ($read_db && !empty($RCI->config['db_table_users'])) {
+  $RCI->pass('db.inc.php');
+}
+else if ($read_db) {
+  $RCI->fail('db.inc.php', 'Syntax error');
+}
+else if (!$read_db) {
+  $RCI->fail('db.inc.php', 'Unable to read file. Did you create the config files?');
+}
 
 ?>
+
+<h3>Check configured database settings</h3>
+<?php
+
+$db_working = false;
+if (!empty($RCI->config)) {
+    if (!empty($RCI->config['db_backend']) && !empty($RCI->config['db_dsnw'])) {
+
+        echo 'Backend: ';
+        echo 'PEAR::' . strtoupper($RCI->config['db_backend']) . '<br />';
+
+        $_class = 'rcube_' . strtolower($RCI->config['db_backend']);
+        require_once 'include/' . $_class . '.inc';
+
+        $DB = new $_class($RCI->config['db_dsnw'], '', false);
+        $DB->db_connect('w');
+        if (!($db_error_msg = $DB->is_error())) {
+            $RCI->pass('DSN (write)');
+            $db_working = true;
+        }
+        else {
+            $RCI->fail('DSN (write)', "Error: $db_error_msg");
+        }
+        echo '<br />';
+    }
+    else {
+        $RCI->fail('DSN (write)', 'not set');
+    }
+}
+else {
+    $RCI->fail('Config', 'Could not read config files');
+}
+
+// initialize db with schema found in /SQL/*
+if ($db_working && $_POST['initdb']) {
+    $engine = preg_match('/^([a-z]+):/i', $RCI->config['db_dsnw'], $regs) ? $regs[1] : 'mysql';
+    $fname = '../SQL/' . ($engine == 'pgsql' ? 'postgres' : $engine) . '.initial.sql';
+    if ($sql = @file_get_contents($fname)) {
+        $DB->query($sql);
+    }
+    else {
+        $RCI->fail('DB Schema', "Cannot read the schema file: $fname");
+    }
+    
+    if ($err = $RCI->get_error()) {
+        $RCI->fail('DB Schema', "Error creating database schema: $err");
+        $db_working = false;
+        echo '<p class="warning">Please try to inizialize the database manually as described in the INSTALL guide.</p>';
+        echo '<br />';
+    }
+}
+
+if ($db_working) {
+    $success = $DB->query("SELECT count(*) FROM {$RCI->config['db_table_users']}");
+    if (!$success) {
+        $RCI->fail('DB Schema', "Database not initialized");
+        echo '<p><input type="submit" name="initdb" value="Initialize database" /></p>';
+    }
+    else {
+        $RCI->pass('DB Schema');
+    }
+    echo '<br />';
+    
+    $tz_db = 'SELECT ' . $DB->unixtimestamp($DB->now()) . ' AS tz_db';
+    $tz_db = $DB->query($tz_db);
+    $tz_db = $DB->fetch_assoc($tz_db);
+    $tz_db = (int) $tz_db['tz_db'];
+    $tz_local = (int) time();
+    $tz_diff  = $tz_local - $tz_db;
+
+    // sometimes db and web servers are on separate hosts, so allow a 30 minutes delta
+    if (abs($tz_diff) > 1800) {
+        $RCI->fail('DB Time', "Database time differs {$td_ziff}s from PHP time");
+    } else {
+        $RCI->pass('DB Time');
+    }
+    
+}
+
+
+?>
+
+<p>[@todo Add tests for IMAP and SMTP settings]</p>
+
 </form>
 
 <p class="warning">

--
Gitblit v1.9.1