From b545d3e8388d18a64d50b6f7879804cf4e7812ca Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Thu, 18 Feb 2010 13:01:53 -0500
Subject: [PATCH] Fix loading of plugin configs: user prefs will always survive (#1486368)

---
 program/include/rcube_config.php |   78 ++++++++++++++++++++++++++------------
 1 files changed, 53 insertions(+), 25 deletions(-)

diff --git a/program/include/rcube_config.php b/program/include/rcube_config.php
index 56f577b..1e2aae3 100644
--- a/program/include/rcube_config.php
+++ b/program/include/rcube_config.php
@@ -15,7 +15,7 @@
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  +-----------------------------------------------------------------------+
 
- $Id: $
+ $Id$
 
 */
 
@@ -28,6 +28,7 @@
 {
   private $prop = array();
   private $errors = array();
+  private $userprefs = array();
 
 
   /**
@@ -51,15 +52,11 @@
     ob_start();
     
     // load main config file
-    if (include(RCMAIL_CONFIG_DIR . '/main.inc.php'))
-      $this->prop = (array)$rcmail_config;
-    else
+    if (!$this->load_from_file(RCMAIL_CONFIG_DIR . '/main.inc.php'))
       $this->errors[] = 'main.inc.php was not found.';
 
     // load database config
-    if (include(RCMAIL_CONFIG_DIR . '/db.inc.php'))
-      $this->prop += (array)$rcmail_config;
-    else
+    if (!$this->load_from_file(RCMAIL_CONFIG_DIR . '/db.inc.php'))
       $this->errors[] = 'db.inc.php was not found.';
     
     // load host-specific configuration
@@ -72,10 +69,9 @@
       $this->prop['skin'] = 'default';
 
     // fix paths
-    $this->prop['log_dir'] = $this->prop['log_dir'] ? unslashify($this->prop['log_dir']) : INSTALL_PATH . 'logs';
-    $this->prop['temp_dir'] = $this->prop['temp_dir'] ? unslashify($this->prop['temp_dir']) : INSTALL_PATH . 'temp';
-    $this->prop['plugins_dir'] = $this->prop['plugins_dir'] ? unslashify($this->prop['plugins_dir']) : INSTALL_PATH . 'plugins';
-
+    $this->prop['log_dir'] = $this->prop['log_dir'] ? realpath(unslashify($this->prop['log_dir'])) : INSTALL_PATH . 'logs';
+    $this->prop['temp_dir'] = $this->prop['temp_dir'] ? realpath(unslashify($this->prop['temp_dir'])) : INSTALL_PATH . 'temp';
+    
     // fix default imap folders encoding
     foreach (array('drafts_mbox', 'junk_mbox', 'sent_mbox', 'trash_mbox') as $folder)
       $this->prop[$folder] = rcube_charset_convert($this->prop[$folder], RCMAIL_CHARSET, 'UTF7-IMAP');
@@ -124,10 +120,30 @@
       $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php';
     }
 
-    if ($fname && is_file(RCMAIL_CONFIG_DIR . '/' . $fname)) {
-      include(RCMAIL_CONFIG_DIR . '/' . $fname);
-      $this->prop = array_merge($this->prop, (array)$rcmail_config);
+    if ($fname) {
+      $this->load_from_file(RCMAIL_CONFIG_DIR . '/' . $fname);
     }
+  }
+  
+  
+  /**
+   * Read configuration from a file
+   * and merge with the already stored config values
+   *
+   * @param string Full path to the config file to be loaded
+   * @return booelan True on success, false on failure
+   */
+  public function load_from_file($fpath)
+  {
+    if (is_file($fpath) && is_readable($fpath)) {
+      include($fpath);
+      if (is_array($rcmail_config)) {
+        $this->prop = array_merge($this->prop, $rcmail_config, $this->userprefs);
+        return true;
+      }
+    }
+    
+    return false;
   }
   
   
@@ -163,9 +179,22 @@
    */
   public function merge($prefs)
   {
-    $this->prop = array_merge($this->prop, $prefs);
+    $this->prop = array_merge($this->prop, $prefs, $this->userprefs);
   }
   
+  
+  /**
+   * Merge the given prefs over the current config
+   * and make sure that they survive further merging.
+   *
+   * @param array  Hash array with user prefs
+   */
+  public function set_user_prefs($prefs)
+  {
+    $this->userprefs = $prefs;
+    $this->prop = array_merge($this->prop, $prefs);
+  }
+
   
   /**
    * Getter for all config options
@@ -176,6 +205,7 @@
   {
     return $this->prop;
   }
+
 
   /**
    * Return requested DES crypto key.
@@ -189,9 +219,8 @@
     if (!array_key_exists($key, $this->prop))
     {
       raise_error(array(
-        'code' => 500,
-        'type' => 'php',
-        'file' => __FILE__,
+        'code' => 500, 'type' => 'php',
+        'file' => __FILE__, 'line' => __LINE__,
         'message' => "Request for unconfigured crypto key \"$key\""
       ), true, true);
     }
@@ -202,15 +231,15 @@
     if (strlen($key) != 24)
     {
       raise_error(array(
-        'code' => 500,
-        'type' => 'php',
-        'file' => __FILE__,
+        'code' => 500, 'type' => 'php',
+	'file' => __FILE__, 'line' => __LINE__,
         'message' => "Configured crypto key \"$key\" is not exactly 24 bytes long"
       ), true, true);
     }
 
     return $key;
   }
+
 
   /**
    * Try to autodetect operating system and find the correct line endings
@@ -222,16 +251,15 @@
     // use the configured delimiter for headers
     if (!empty($this->prop['mail_header_delimiter']))
       return $this->prop['mail_header_delimiter'];
-    else if (strtolower(substr(PHP_OS, 0, 3) == 'win'))
+    else if (strtolower(substr(PHP_OS, 0, 3)) == 'win')
       return "\r\n";
-    else if (strtolower(substr(PHP_OS, 0, 3) == 'mac'))
+    else if (strtolower(substr(PHP_OS, 0, 3)) == 'mac')
       return "\r\n";
     else
       return "\n";
   }
+  
 
-  
-  
   /**
    * Return the mail domain configured for the given host
    *

--
Gitblit v1.9.1