4 +-----------------------------------------------------------------------+
5 | program/include/rcube_config.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2010, Roundcube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Class to read configuration settings |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: rcube_config.php 4509 2011-02-09 10:51:50Z thomasb $
23 * Configuration class for Roundcube
29 private $prop = array();
30 private $errors = array();
31 private $userprefs = array();
37 public function __construct()
44 * Load config from local config file
46 * @todo Remove global $CONFIG
48 private function load()
50 // load main config file
51 if (!$this->load_from_file(RCMAIL_CONFIG_DIR . '/main.inc.php'))
52 $this->errors[] = 'main.inc.php was not found.';
54 // load database config
55 if (!$this->load_from_file(RCMAIL_CONFIG_DIR . '/db.inc.php'))
56 $this->errors[] = 'db.inc.php was not found.';
58 // load host-specific configuration
59 $this->load_host_config();
61 // set skin (with fallback to old 'skin_path' property)
62 if (empty($this->prop['skin']) && !empty($this->prop['skin_path']))
63 $this->prop['skin'] = str_replace('skins/', '', unslashify($this->prop['skin_path']));
64 else if (empty($this->prop['skin']))
65 $this->prop['skin'] = 'default';
68 $this->prop['log_dir'] = $this->prop['log_dir'] ? realpath(unslashify($this->prop['log_dir'])) : INSTALL_PATH . 'logs';
69 $this->prop['temp_dir'] = $this->prop['temp_dir'] ? realpath(unslashify($this->prop['temp_dir'])) : INSTALL_PATH . 'temp';
71 // fix default imap folders encoding
72 foreach (array('drafts_mbox', 'junk_mbox', 'sent_mbox', 'trash_mbox') as $folder)
73 $this->prop[$folder] = rcube_charset_convert($this->prop[$folder], RCMAIL_CHARSET, 'UTF7-IMAP');
75 if (!empty($this->prop['default_imap_folders']))
76 foreach ($this->prop['default_imap_folders'] as $n => $folder)
77 $this->prop['default_imap_folders'][$n] = rcube_charset_convert($folder, RCMAIL_CHARSET, 'UTF7-IMAP');
79 // set PHP error logging according to config
80 if ($this->prop['debug_level'] & 1) {
81 ini_set('log_errors', 1);
83 if ($this->prop['log_driver'] == 'syslog') {
84 ini_set('error_log', 'syslog');
87 ini_set('error_log', $this->prop['log_dir'].'/errors');
90 if ($this->prop['debug_level'] & 4) {
91 ini_set('display_errors', 1);
94 ini_set('display_errors', 0);
98 $GLOBALS['CONFIG'] = &$this->prop;
102 * Load a host-specific config file if configured
103 * This will merge the host specific configuration with the given one
105 private function load_host_config()
109 if (is_array($this->prop['include_host_config'])) {
110 $fname = $this->prop['include_host_config'][$_SERVER['HTTP_HOST']];
112 else if (!empty($this->prop['include_host_config'])) {
113 $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php';
117 $this->load_from_file(RCMAIL_CONFIG_DIR . '/' . $fname);
123 * Read configuration from a file
124 * and merge with the already stored config values
126 * @param string $fpath Full path to the config file to be loaded
127 * @return booelan True on success, false on failure
129 public function load_from_file($fpath)
131 if (is_file($fpath) && is_readable($fpath)) {
132 // use output buffering, we don't need any output here
137 if (is_array($rcmail_config)) {
138 $this->prop = array_merge($this->prop, $rcmail_config, $this->userprefs);
148 * Getter for a specific config parameter
150 * @param string $name Parameter name
151 * @param mixed $def Default value if not set
152 * @return mixed The requested config value
154 public function get($name, $def = null)
156 return isset($this->prop[$name]) ? $this->prop[$name] : $def;
161 * Setter for a config parameter
163 * @param string $name Parameter name
164 * @param mixed $value Parameter value
166 public function set($name, $value)
168 $this->prop[$name] = $value;
173 * Override config options with the given values (eg. user prefs)
175 * @param array $prefs Hash array with config props to merge over
177 public function merge($prefs)
179 $this->prop = array_merge($this->prop, $prefs, $this->userprefs);
184 * Merge the given prefs over the current config
185 * and make sure that they survive further merging.
187 * @param array $prefs Hash array with user prefs
189 public function set_user_prefs($prefs)
191 $this->userprefs = $prefs;
192 $this->prop = array_merge($this->prop, $prefs);
197 * Getter for all config options
199 * @return array Hash array containg all config properties
201 public function all()
208 * Return requested DES crypto key.
210 * @param string $key Crypto key name
211 * @return string Crypto key
213 public function get_crypto_key($key)
215 // Bomb out if the requested key does not exist
216 if (!array_key_exists($key, $this->prop)) {
218 'code' => 500, 'type' => 'php',
219 'file' => __FILE__, 'line' => __LINE__,
220 'message' => "Request for unconfigured crypto key \"$key\""
224 $key = $this->prop[$key];
226 // Bomb out if the configured key is not exactly 24 bytes long
227 if (strlen($key) != 24) {
229 'code' => 500, 'type' => 'php',
230 'file' => __FILE__, 'line' => __LINE__,
231 'message' => "Configured crypto key '$key' is not exactly 24 bytes long"
240 * Try to autodetect operating system and find the correct line endings
242 * @return string The appropriate mail header delimiter
244 public function header_delimiter()
246 // use the configured delimiter for headers
247 if (!empty($this->prop['mail_header_delimiter'])) {
248 $delim = $this->prop['mail_header_delimiter'];
249 if ($delim == "\n" || $delim == "\r\n")
253 'code' => 500, 'type' => 'php',
254 'file' => __FILE__, 'line' => __LINE__,
255 'message' => "Invalid mail_header_delimiter setting"
259 $php_os = strtolower(substr(PHP_OS, 0, 3));
261 if ($php_os == 'win')
264 if ($php_os == 'mac')
272 * Return the mail domain configured for the given host
274 * @param string $host IMAP host
275 * @param boolean $encode If true, domain name will be converted to IDN ASCII
276 * @return string Resolved SMTP host
278 public function mail_domain($host, $encode=true)
282 if (is_array($this->prop['mail_domain'])) {
283 if (isset($this->prop['mail_domain'][$host]))
284 $domain = $this->prop['mail_domain'][$host];
286 else if (!empty($this->prop['mail_domain']))
287 $domain = rcube_parse_host($this->prop['mail_domain']);
290 $domain = rcube_idn_to_ascii($domain);
297 * Getter for error state
299 * @return mixed Error message on error, False if no errors
301 public function get_error()
303 return empty($this->errors) ? false : join("\n", $this->errors);