4 +-----------------------------------------------------------------------+
5 | program/include/rcube_config.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2010, The Roundcube Dev Team |
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 5151 2011-08-31 12:49:44Z alec $
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');
91 // enable display_errors in 'show' level, but not for ajax requests
92 ini_set('display_errors', intval(empty($_REQUEST['_remote']) && ($this->prop['debug_level'] & 4)));
95 $GLOBALS['CONFIG'] = &$this->prop;
99 * Load a host-specific config file if configured
100 * This will merge the host specific configuration with the given one
102 private function load_host_config()
106 if (is_array($this->prop['include_host_config'])) {
107 $fname = $this->prop['include_host_config'][$_SERVER['HTTP_HOST']];
109 else if (!empty($this->prop['include_host_config'])) {
110 $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php';
114 $this->load_from_file(RCMAIL_CONFIG_DIR . '/' . $fname);
120 * Read configuration from a file
121 * and merge with the already stored config values
123 * @param string $fpath Full path to the config file to be loaded
124 * @return booelan True on success, false on failure
126 public function load_from_file($fpath)
128 if (is_file($fpath) && is_readable($fpath)) {
129 // use output buffering, we don't need any output here
134 if (is_array($rcmail_config)) {
135 $this->prop = array_merge($this->prop, $rcmail_config, $this->userprefs);
145 * Getter for a specific config parameter
147 * @param string $name Parameter name
148 * @param mixed $def Default value if not set
149 * @return mixed The requested config value
151 public function get($name, $def = null)
153 $result = isset($this->prop[$name]) ? $this->prop[$name] : $def;
154 $rcmail = rcmail::get_instance();
156 if (is_object($rcmail->plugins)) {
157 $plugin = $rcmail->plugins->exec_hook('config_get', array(
158 'name' => $name, 'default' => $def, 'result' => $result));
160 return $plugin['result'];
168 * Setter for a config parameter
170 * @param string $name Parameter name
171 * @param mixed $value Parameter value
173 public function set($name, $value)
175 $this->prop[$name] = $value;
180 * Override config options with the given values (eg. user prefs)
182 * @param array $prefs Hash array with config props to merge over
184 public function merge($prefs)
186 $this->prop = array_merge($this->prop, $prefs, $this->userprefs);
191 * Merge the given prefs over the current config
192 * and make sure that they survive further merging.
194 * @param array $prefs Hash array with user prefs
196 public function set_user_prefs($prefs)
198 // Honor the dont_override setting for any existing user preferences
199 $dont_override = $this->get('dont_override');
200 if (is_array($dont_override) && !empty($dont_override)) {
201 foreach ($prefs as $key => $pref) {
202 if (in_array($key, $dont_override)) {
208 $this->userprefs = $prefs;
209 $this->prop = array_merge($this->prop, $prefs);
214 * Getter for all config options
216 * @return array Hash array containg all config properties
218 public function all()
224 * Special getter for user's timezone
226 public function get_timezone()
228 $tz = $this->get('timezone');
230 $tz = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : date('Z') / 3600;
232 $tz = intval($tz) + intval($this->get('dst_active'));
238 * Return requested DES crypto key.
240 * @param string $key Crypto key name
241 * @return string Crypto key
243 public function get_crypto_key($key)
245 // Bomb out if the requested key does not exist
246 if (!array_key_exists($key, $this->prop)) {
248 'code' => 500, 'type' => 'php',
249 'file' => __FILE__, 'line' => __LINE__,
250 'message' => "Request for unconfigured crypto key \"$key\""
254 $key = $this->prop[$key];
256 // Bomb out if the configured key is not exactly 24 bytes long
257 if (strlen($key) != 24) {
259 'code' => 500, 'type' => 'php',
260 'file' => __FILE__, 'line' => __LINE__,
261 'message' => "Configured crypto key '$key' is not exactly 24 bytes long"
270 * Try to autodetect operating system and find the correct line endings
272 * @return string The appropriate mail header delimiter
274 public function header_delimiter()
276 // use the configured delimiter for headers
277 if (!empty($this->prop['mail_header_delimiter'])) {
278 $delim = $this->prop['mail_header_delimiter'];
279 if ($delim == "\n" || $delim == "\r\n")
283 'code' => 500, 'type' => 'php',
284 'file' => __FILE__, 'line' => __LINE__,
285 'message' => "Invalid mail_header_delimiter setting"
289 $php_os = strtolower(substr(PHP_OS, 0, 3));
291 if ($php_os == 'win')
294 if ($php_os == 'mac')
302 * Return the mail domain configured for the given host
304 * @param string $host IMAP host
305 * @param boolean $encode If true, domain name will be converted to IDN ASCII
306 * @return string Resolved SMTP host
308 public function mail_domain($host, $encode=true)
312 if (is_array($this->prop['mail_domain'])) {
313 if (isset($this->prop['mail_domain'][$host]))
314 $domain = $this->prop['mail_domain'][$host];
316 else if (!empty($this->prop['mail_domain']))
317 $domain = rcube_parse_host($this->prop['mail_domain']);
320 $domain = rcube_idn_to_ascii($domain);
327 * Getter for error state
329 * @return mixed Error message on error, False if no errors
331 public function get_error()
333 return empty($this->errors) ? false : join("\n", $this->errors);