From e107123780c12b4d43d0264cb9c6180fb02d9ab8 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Fri, 03 Oct 2008 16:58:23 -0400
Subject: [PATCH] Add methods to check and update local config files + show HTML output in installer test step

---
 installer/rcube_install.php |   85 +++++++++++++++++++++++++++
 installer/styles.css        |   17 +++++
 installer/test.php          |   44 ++++++++++++++
 installer/images/error.png  |    0 
 installer/index.php         |   17 +++++
 5 files changed, 159 insertions(+), 4 deletions(-)

diff --git a/installer/images/error.png b/installer/images/error.png
new file mode 100755
index 0000000..628cf2d
--- /dev/null
+++ b/installer/images/error.png
Binary files differ
diff --git a/installer/index.php b/installer/index.php
index d14e6ff..6ece823 100644
--- a/installer/index.php
+++ b/installer/index.php
@@ -45,6 +45,23 @@
   }
 }
 
+if (isset($_GET['_mergeconfig']) && in_array($_GET['_mergeconfig'], array('main', 'db'))) {
+  $filename = $_GET['_mergeconfig'] . '.inc.php';
+
+  header('Content-type: text/plain');
+  header('Content-Disposition: attachment; filename="'.$filename.'"');
+  
+  $RCI->merge_config();
+  echo $RCI->create_config($_GET['_mergeconfig'], true);
+  exit;
+}
+
+// go to 'test' step if we have a local configuration
+if ($RCI->configured && empty($_REQUEST['_step'])) {
+  header("Location: ./?_step=3");
+  exit;
+}
+
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
diff --git a/installer/rcube_install.php b/installer/rcube_install.php
index 37645ad..21e353c 100644
--- a/installer/rcube_install.php
+++ b/installer/rcube_install.php
@@ -31,6 +31,18 @@
   var $last_error = null;
   var $email_pattern = '([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9])';
   var $config_props = array();
+
+  var $obsolete_config = array('db_backend');
+  var $replaced_config = array('skin_path' => 'skin', 'locale_string' => 'language');
+  
+  // these config options are optional or can be set to null
+  var $optional_config = array(
+    'log_driver', 'syslog_id', 'syslog_facility', 'imap_auth_type',
+    'smtp_helo_host', 'sendmail_delay', 'double_auth', 'language',
+    'mail_header_delimiter', 'create_default_folders',
+    'quota_zero_as_unlimited', 'spellcheck_uri', 'spellcheck_languages',
+    'http_received_header', 'session_domain', 'mime_magic', 'log_logins',
+    'enable_installer', 'skin_include_php');
   
   /**
    * Constructor
@@ -116,7 +128,7 @@
    * @param string Which config file (either 'main' or 'db')
    * @return string The complete config file content
    */
-  function create_config($which)
+  function create_config($which, $force = false)
   {
     $out = file_get_contents("../config/{$which}.inc.php.dist");
     
@@ -166,7 +178,7 @@
       }
       
       // skip this property
-      if ($value == $default)
+      if (!$force && ($value == $default))
         continue;
 
       // save change
@@ -181,6 +193,75 @@
 
     return trim($out);
   }
+
+
+  /**
+   * Check the current configuration for missing properties
+   * and deprecated or obsolete settings
+   *
+   * @return array List with problems detected
+   */
+  function check_config()
+  {
+    $this->config = array();
+    $this->load_defaults();
+    $defaults = $this->config;
+    
+    $this->load_config();
+    if (!$this->configured)
+      return null;
+    
+    $out = $seen = array();
+    $optional = array_flip($this->optional_config);
+    
+    // ireate over the current configuration
+    foreach ($this->config as $prop => $value) {
+      if ($replacement = $this->replaced_config[$prop]) {
+        $out['replaced'][] = array('prop' => $prop, 'replacement' => $replacement);
+        $seen[$replacement] = true;
+      }
+      else if (!$seen[$prop] && in_array($prop, $this->obsolete_config)) {
+        $out['obsolete'][] = array('prop' => $prop);
+        $seen[$prop] = true;
+      }
+    }
+    
+    // iterate over default config
+    foreach ($defaults as $prop => $value) {
+      if (!$seen[$prop] && !isset($this->config[$prop]) && !isset($optional[$prop]))
+        $out['missing'][] = array('prop' => $prop);
+    }
+    
+    return $out;
+  }
+  
+  
+  /**
+   * Merge the current configuration with the defaults
+   * and copy replaced values to the new options.
+   */
+  function merge_config()
+  {
+    $current = $this->config;
+    $this->config = array();
+    $this->load_defaults();
+    
+    foreach ($this->replaced_config as $prop => $replacement)
+      if (isset($current[$prop])) {
+        if ($prop == 'skin_path')
+          $this->config[$replacement] = preg_replace('#skins/(\w+)/?$#', '\\1', $current[$prop]);
+        else
+          $this->config[$replacement] = $current[$prop];
+        
+        unset($current[$prop]);
+    }
+    
+    foreach ($this->obsolete_config as $prop) {
+      unset($current[$prop]);
+    }
+    
+    $this->config  = array_merge($current, $this->config);
+  }
   
   
   /**
diff --git a/installer/styles.css b/installer/styles.css
index ed64ff0..1acdc9c 100644
--- a/installer/styles.css
+++ b/installer/styles.css
@@ -131,7 +131,7 @@
   height: 30em;
 }
 
-dt.propname {
+.propname {
   font-family: monospace;
   font-size: 9pt;
   margin-top: 1em;
@@ -219,12 +219,25 @@
   border: 2px solid #c2d071;
 }
 
-.warning {
+.suggestion {
+  padding: 0.6em;
+  background-color: #ebebeb;
+  border: 1px solid #999;
+}
+
+p.warning,
+div.warning {
   padding: 1em;
   background-color: #ef9398;
   border: 2px solid #dc5757;
 }
 
+h3.warning {
+  color: #c00;
+  background: url('images/error.png') top left no-repeat;
+  padding-left: 24px;
+}
+
 .userconf {
   color: #00c;
   font-family: "Lucida Grande", Verdana, Arial, Helvetica, sans-serif;
diff --git a/installer/test.php b/installer/test.php
index ca7c7ad..8af6a6a 100644
--- a/installer/test.php
+++ b/installer/test.php
@@ -27,6 +27,50 @@
   $RCI->fail('db.inc.php', 'Unable to read file. Did you create the config files?');
 }
 
+if ($RCI->configured && ($messages = $RCI->check_config())) {
+  
+  if (is_array($messages['missing'])) {
+    echo '<h3 class="warning">Missing config options</h3>';
+    echo '<p class="hint">The following config options are not present in the current configuration.<br/>';
+    echo 'Please check the default config files and add the missing properties to your local config files.</p>';
+    
+    echo '<ul class="configwarings">';
+    foreach ($messages['missing'] as $msg) {
+      echo html::tag('li', null, html::span('propname', $msg['prop']) . ($msg['name'] ? ':&nbsp;' . $msg['name'] : ''));
+    }    
+    echo '</ul>';
+  }
+
+  if (is_array($messages['replaced'])) {
+    echo '<h3 class="warning">Replaced config options</h3>';
+    echo '<p class="hint">The following config options have been replaced or renamed. ';
+    echo 'Please update them accordingly in your config files.</p>';
+    
+    echo '<ul class="configwarings">';
+    foreach ($messages['replaced'] as $msg) {
+      echo html::tag('li', null, html::span('propname', $msg['prop']) .
+        ' was replaced by ' . html::span('propname', $msg['replacement']));
+    }
+    echo '</ul>';
+  }
+
+  if (is_array($messages['obsolete'])) {
+    echo '<h3>Obsolete config options</h3>';
+    echo '<p class="hint">You still have some obsolete or inexistent properties set. This isn\'t a problem but should be noticed.</p>';
+    
+    echo '<ul class="configwarings">';
+    foreach ($messages['obsolete'] as $msg) {
+      echo html::tag('li', null, html::span('propname', $msg['prop']) . ($msg['name'] ? ':&nbsp;' . $msg['name'] : ''));
+    }
+    echo '</ul>';
+  }
+  
+  echo '<p class="suggestion">OK, lazy people can download the updated config files here: ';
+  echo html::a(array('href' => './?_mergeconfig=main'), 'main.inc.php') . ' &nbsp;';
+  echo html::a(array('href' => './?_mergeconfig=db'), 'db.inc.php');
+  echo "</p>";
+}
+
 ?>
 
 <h3>Check if directories are writable</h3>

--
Gitblit v1.9.1