4 +-----------------------------------------------------------------------+
5 | program/include/rcube_config.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Class to read configuration settings |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
23 * Configuration class for RoundCube
29 private $prop = array();
30 private $errors = array();
36 public function __construct()
43 * Load config from local config file
45 * @todo Remove global $CONFIG
47 private function load()
49 // start output buffering, we don't need any output yet,
50 // it'll be cleared after reading of config files, etc.
53 // load main config file
54 if (!$this->load_from_file(RCMAIL_CONFIG_DIR . '/main.inc.php'))
55 $this->errors[] = 'main.inc.php was not found.';
57 // load database config
58 if (!$this->load_from_file(RCMAIL_CONFIG_DIR . '/db.inc.php'))
59 $this->errors[] = 'db.inc.php was not found.';
61 // load host-specific configuration
62 $this->load_host_config();
64 // set skin (with fallback to old 'skin_path' property)
65 if (empty($this->prop['skin']) && !empty($this->prop['skin_path']))
66 $this->prop['skin'] = str_replace('skins/', '', unslashify($this->prop['skin_path']));
67 else if (empty($this->prop['skin']))
68 $this->prop['skin'] = 'default';
71 $this->prop['log_dir'] = $this->prop['log_dir'] ? unslashify($this->prop['log_dir']) : INSTALL_PATH . 'logs';
72 $this->prop['temp_dir'] = $this->prop['temp_dir'] ? unslashify($this->prop['temp_dir']) : INSTALL_PATH . 'temp';
74 // fix default imap folders encoding
75 foreach (array('drafts_mbox', 'junk_mbox', 'sent_mbox', 'trash_mbox') as $folder)
76 $this->prop[$folder] = rcube_charset_convert($this->prop[$folder], RCMAIL_CHARSET, 'UTF7-IMAP');
78 if (!empty($this->prop['default_imap_folders']))
79 foreach ($this->prop['default_imap_folders'] as $n => $folder)
80 $this->prop['default_imap_folders'][$n] = rcube_charset_convert($folder, RCMAIL_CHARSET, 'UTF7-IMAP');
82 // set PHP error logging according to config
83 if ($this->prop['debug_level'] & 1) {
84 ini_set('log_errors', 1);
86 if ($this->prop['log_driver'] == 'syslog') {
87 ini_set('error_log', 'syslog');
89 ini_set('error_log', $this->prop['log_dir'].'/errors');
92 if ($this->prop['debug_level'] & 4) {
93 ini_set('display_errors', 1);
96 ini_set('display_errors', 0);
99 // clear output buffer
102 // export config data
103 $GLOBALS['CONFIG'] = &$this->prop;
108 * Load a host-specific config file if configured
109 * This will merge the host specific configuration with the given one
111 private function load_host_config()
115 if (is_array($this->prop['include_host_config'])) {
116 $fname = $this->prop['include_host_config'][$_SERVER['HTTP_HOST']];
118 else if (!empty($this->prop['include_host_config'])) {
119 $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php';
123 $this->load_from_file(RCMAIL_CONFIG_DIR . '/' . $fname);
129 * Read configuration from a file
130 * and merge with the already stored config values
132 * @param string Full path to the config file to be loaded
133 * @return booelan True on success, false on failure
135 public function load_from_file($fpath)
137 if (is_file($fpath) && is_readable($fpath)) {
139 if (is_array($rcmail_config)) {
140 $this->prop = array_merge($this->prop, $rcmail_config);
150 * Getter for a specific config parameter
152 * @param string Parameter name
153 * @param mixed Default value if not set
154 * @return mixed The requested config value
156 public function get($name, $def = null)
158 return isset($this->prop[$name]) ? $this->prop[$name] : $def;
163 * Setter for a config parameter
165 * @param string Parameter name
166 * @param mixed Parameter value
168 public function set($name, $value)
170 $this->prop[$name] = $value;
175 * Override config options with the given values (eg. user prefs)
177 * @param array Hash array with config props to merge over
179 public function merge($prefs)
181 $this->prop = array_merge($this->prop, $prefs);
186 * Getter for all config options
188 * @return array Hash array containg all config properties
190 public function all()
196 * Return requested DES crypto key.
198 * @param string Crypto key name
199 * @return string Crypto key
201 public function get_crypto_key($key)
203 // Bomb out if the requested key does not exist
204 if (!array_key_exists($key, $this->prop))
210 'message' => "Request for unconfigured crypto key \"$key\""
214 $key = $this->prop[$key];
216 // Bomb out if the configured key is not exactly 24 bytes long
217 if (strlen($key) != 24)
223 'message' => "Configured crypto key \"$key\" is not exactly 24 bytes long"
231 * Try to autodetect operating system and find the correct line endings
233 * @return string The appropriate mail header delimiter
235 public function header_delimiter()
237 // use the configured delimiter for headers
238 if (!empty($this->prop['mail_header_delimiter']))
239 return $this->prop['mail_header_delimiter'];
240 else if (strtolower(substr(PHP_OS, 0, 3)) == 'win')
242 else if (strtolower(substr(PHP_OS, 0, 3)) == 'mac')
251 * Return the mail domain configured for the given host
253 * @param string IMAP host
254 * @return string Resolved SMTP host
256 public function mail_domain($host)
260 if (is_array($this->prop['mail_domain'])) {
261 if (isset($this->prop['mail_domain'][$host]))
262 $domain = $this->prop['mail_domain'][$host];
264 else if (!empty($this->prop['mail_domain']))
265 $domain = $this->prop['mail_domain'];
272 * Getter for error state
274 * @return mixed Error message on error, False if no errors
276 public function get_error()
278 return empty($this->errors) ? false : join("\n", $this->errors);