| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/include/rcube_config.php | |
|---|
| 6 | | | |
|---|
| 7 | | This file is part of the Roundcube Webmail client | |
|---|
| 8 | | Copyright (C) 2008-2012, The Roundcube Dev Team | |
|---|
| 9 | | | |
|---|
| 10 | | Licensed under the GNU General Public License version 3 or | |
|---|
| 11 | | any later version with exceptions for skins & plugins. | |
|---|
| 12 | | See the README file for a full license statement. | |
|---|
| 13 | | | |
|---|
| 14 | | PURPOSE: | |
|---|
| 15 | | Class to read configuration settings | |
|---|
| 16 | | | |
|---|
| 17 | +-----------------------------------------------------------------------+ |
|---|
| 18 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 19 | +-----------------------------------------------------------------------+ |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * Configuration class for Roundcube |
|---|
| 24 | * |
|---|
| 25 | * @package Core |
|---|
| 26 | */ |
|---|
| 27 | class rcube_config |
|---|
| 28 | { |
|---|
| 29 | private $prop = array(); |
|---|
| 30 | private $errors = array(); |
|---|
| 31 | private $userprefs = array(); |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Renamed options |
|---|
| 35 | * |
|---|
| 36 | * @var array |
|---|
| 37 | */ |
|---|
| 38 | private $legacy_props = array( |
|---|
| 39 | // new name => old name |
|---|
| 40 | 'default_folders' => 'default_imap_folders', |
|---|
| 41 | 'mail_pagesize' => 'pagesize', |
|---|
| 42 | 'addressbook_pagesize' => 'pagesize', |
|---|
| 43 | ); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Object constructor |
|---|
| 48 | */ |
|---|
| 49 | public function __construct() |
|---|
| 50 | { |
|---|
| 51 | $this->load(); |
|---|
| 52 | |
|---|
| 53 | // Defaults, that we do not require you to configure, |
|---|
| 54 | // but contain information that is used in various |
|---|
| 55 | // locations in the code: |
|---|
| 56 | $this->set('contactlist_fields', array('name', 'firstname', 'surname', 'email')); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Load config from local config file |
|---|
| 62 | * |
|---|
| 63 | * @todo Remove global $CONFIG |
|---|
| 64 | */ |
|---|
| 65 | private function load() |
|---|
| 66 | { |
|---|
| 67 | // load main config file |
|---|
| 68 | if (!$this->load_from_file(RCMAIL_CONFIG_DIR . '/main.inc.php')) |
|---|
| 69 | $this->errors[] = 'main.inc.php was not found.'; |
|---|
| 70 | |
|---|
| 71 | // load database config |
|---|
| 72 | if (!$this->load_from_file(RCMAIL_CONFIG_DIR . '/db.inc.php')) |
|---|
| 73 | $this->errors[] = 'db.inc.php was not found.'; |
|---|
| 74 | |
|---|
| 75 | // load host-specific configuration |
|---|
| 76 | $this->load_host_config(); |
|---|
| 77 | |
|---|
| 78 | // set skin (with fallback to old 'skin_path' property) |
|---|
| 79 | if (empty($this->prop['skin'])) { |
|---|
| 80 | if (!empty($this->prop['skin_path'])) { |
|---|
| 81 | $this->prop['skin'] = str_replace('skins/', '', unslashify($this->prop['skin_path'])); |
|---|
| 82 | } |
|---|
| 83 | else { |
|---|
| 84 | $this->prop['skin'] = 'larry'; |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | // larry is the new default skin :-) |
|---|
| 89 | if ($this->prop['skin'] == 'default') |
|---|
| 90 | $this->prop['skin'] = 'larry'; |
|---|
| 91 | |
|---|
| 92 | // fix paths |
|---|
| 93 | $this->prop['log_dir'] = $this->prop['log_dir'] ? realpath(unslashify($this->prop['log_dir'])) : INSTALL_PATH . 'logs'; |
|---|
| 94 | $this->prop['temp_dir'] = $this->prop['temp_dir'] ? realpath(unslashify($this->prop['temp_dir'])) : INSTALL_PATH . 'temp'; |
|---|
| 95 | |
|---|
| 96 | // fix default imap folders encoding |
|---|
| 97 | foreach (array('drafts_mbox', 'junk_mbox', 'sent_mbox', 'trash_mbox') as $folder) |
|---|
| 98 | $this->prop[$folder] = rcube_charset::convert($this->prop[$folder], RCMAIL_CHARSET, 'UTF7-IMAP'); |
|---|
| 99 | |
|---|
| 100 | if (!empty($this->prop['default_folders'])) |
|---|
| 101 | foreach ($this->prop['default_folders'] as $n => $folder) |
|---|
| 102 | $this->prop['default_folders'][$n] = rcube_charset::convert($folder, RCMAIL_CHARSET, 'UTF7-IMAP'); |
|---|
| 103 | |
|---|
| 104 | // set PHP error logging according to config |
|---|
| 105 | if ($this->prop['debug_level'] & 1) { |
|---|
| 106 | ini_set('log_errors', 1); |
|---|
| 107 | |
|---|
| 108 | if ($this->prop['log_driver'] == 'syslog') { |
|---|
| 109 | ini_set('error_log', 'syslog'); |
|---|
| 110 | } |
|---|
| 111 | else { |
|---|
| 112 | ini_set('error_log', $this->prop['log_dir'].'/errors'); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | // enable display_errors in 'show' level, but not for ajax requests |
|---|
| 117 | ini_set('display_errors', intval(empty($_REQUEST['_remote']) && ($this->prop['debug_level'] & 4))); |
|---|
| 118 | |
|---|
| 119 | // set timezone auto settings values |
|---|
| 120 | if ($this->prop['timezone'] == 'auto') { |
|---|
| 121 | $this->prop['_timezone_value'] = $this->client_timezone(); |
|---|
| 122 | } |
|---|
| 123 | else if (is_numeric($this->prop['timezone'])) { |
|---|
| 124 | $this->prop['timezone'] = timezone_name_from_abbr("", $this->prop['timezone'] * 3600, 0); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | // remove deprecated properties |
|---|
| 128 | unset($this->prop['dst_active']); |
|---|
| 129 | |
|---|
| 130 | // export config data |
|---|
| 131 | $GLOBALS['CONFIG'] = &$this->prop; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * Load a host-specific config file if configured |
|---|
| 136 | * This will merge the host specific configuration with the given one |
|---|
| 137 | */ |
|---|
| 138 | private function load_host_config() |
|---|
| 139 | { |
|---|
| 140 | $fname = null; |
|---|
| 141 | |
|---|
| 142 | if (is_array($this->prop['include_host_config'])) { |
|---|
| 143 | $fname = $this->prop['include_host_config'][$_SERVER['HTTP_HOST']]; |
|---|
| 144 | } |
|---|
| 145 | else if (!empty($this->prop['include_host_config'])) { |
|---|
| 146 | $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php'; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | if ($fname) { |
|---|
| 150 | $this->load_from_file(RCMAIL_CONFIG_DIR . '/' . $fname); |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * Read configuration from a file |
|---|
| 157 | * and merge with the already stored config values |
|---|
| 158 | * |
|---|
| 159 | * @param string $fpath Full path to the config file to be loaded |
|---|
| 160 | * @return booelan True on success, false on failure |
|---|
| 161 | */ |
|---|
| 162 | public function load_from_file($fpath) |
|---|
| 163 | { |
|---|
| 164 | if (is_file($fpath) && is_readable($fpath)) { |
|---|
| 165 | // use output buffering, we don't need any output here |
|---|
| 166 | ob_start(); |
|---|
| 167 | include($fpath); |
|---|
| 168 | ob_end_clean(); |
|---|
| 169 | |
|---|
| 170 | if (is_array($rcmail_config)) { |
|---|
| 171 | $this->prop = array_merge($this->prop, $rcmail_config, $this->userprefs); |
|---|
| 172 | return true; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | return false; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | * Getter for a specific config parameter |
|---|
| 182 | * |
|---|
| 183 | * @param string $name Parameter name |
|---|
| 184 | * @param mixed $def Default value if not set |
|---|
| 185 | * @return mixed The requested config value |
|---|
| 186 | */ |
|---|
| 187 | public function get($name, $def = null) |
|---|
| 188 | { |
|---|
| 189 | if (isset($this->prop[$name])) { |
|---|
| 190 | $result = $this->prop[$name]; |
|---|
| 191 | } |
|---|
| 192 | else if (isset($this->legacy_props[$name])) { |
|---|
| 193 | return $this->get($this->legacy_props[$name], $def); |
|---|
| 194 | } |
|---|
| 195 | else { |
|---|
| 196 | $result = $def; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | $rcube = rcube::get_instance(); |
|---|
| 200 | |
|---|
| 201 | if ($name == 'timezone' && isset($this->prop['_timezone_value'])) |
|---|
| 202 | $result = $this->prop['_timezone_value']; |
|---|
| 203 | |
|---|
| 204 | $plugin = $rcube->plugins->exec_hook('config_get', array( |
|---|
| 205 | 'name' => $name, 'default' => $def, 'result' => $result)); |
|---|
| 206 | |
|---|
| 207 | return $plugin['result']; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | /** |
|---|
| 212 | * Setter for a config parameter |
|---|
| 213 | * |
|---|
| 214 | * @param string $name Parameter name |
|---|
| 215 | * @param mixed $value Parameter value |
|---|
| 216 | */ |
|---|
| 217 | public function set($name, $value) |
|---|
| 218 | { |
|---|
| 219 | $this->prop[$name] = $value; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | /** |
|---|
| 224 | * Override config options with the given values (eg. user prefs) |
|---|
| 225 | * |
|---|
| 226 | * @param array $prefs Hash array with config props to merge over |
|---|
| 227 | */ |
|---|
| 228 | public function merge($prefs) |
|---|
| 229 | { |
|---|
| 230 | $this->prop = array_merge($this->prop, $prefs, $this->userprefs); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | /** |
|---|
| 235 | * Merge the given prefs over the current config |
|---|
| 236 | * and make sure that they survive further merging. |
|---|
| 237 | * |
|---|
| 238 | * @param array $prefs Hash array with user prefs |
|---|
| 239 | */ |
|---|
| 240 | public function set_user_prefs($prefs) |
|---|
| 241 | { |
|---|
| 242 | // Honor the dont_override setting for any existing user preferences |
|---|
| 243 | $dont_override = $this->get('dont_override'); |
|---|
| 244 | if (is_array($dont_override) && !empty($dont_override)) { |
|---|
| 245 | foreach ($dont_override as $key) { |
|---|
| 246 | unset($prefs[$key]); |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | // convert user's timezone into the new format |
|---|
| 251 | if (is_numeric($prefs['timezone'])) { |
|---|
| 252 | $prefs['timezone'] = timezone_name_from_abbr('', $prefs['timezone'] * 3600, 0); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | // larry is the new default skin :-) |
|---|
| 256 | if ($prefs['skin'] == 'default') { |
|---|
| 257 | $prefs['skin'] = 'larry'; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | $this->userprefs = $prefs; |
|---|
| 261 | $this->prop = array_merge($this->prop, $prefs); |
|---|
| 262 | |
|---|
| 263 | // override timezone settings with client values |
|---|
| 264 | if ($this->prop['timezone'] == 'auto') { |
|---|
| 265 | $this->prop['_timezone_value'] = isset($_SESSION['timezone']) ? $this->client_timezone() : $this->prop['_timezone_value']; |
|---|
| 266 | } |
|---|
| 267 | else if (isset($this->prop['_timezone_value'])) |
|---|
| 268 | unset($this->prop['_timezone_value']); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | /** |
|---|
| 273 | * Getter for all config options |
|---|
| 274 | * |
|---|
| 275 | * @return array Hash array containg all config properties |
|---|
| 276 | */ |
|---|
| 277 | public function all() |
|---|
| 278 | { |
|---|
| 279 | return $this->prop; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | /** |
|---|
| 283 | * Special getter for user's timezone offset including DST |
|---|
| 284 | * |
|---|
| 285 | * @return float Timezone offset (in hours) |
|---|
| 286 | * @deprecated |
|---|
| 287 | */ |
|---|
| 288 | public function get_timezone() |
|---|
| 289 | { |
|---|
| 290 | if ($tz = $this->get('timezone')) { |
|---|
| 291 | try { |
|---|
| 292 | $tz = new DateTimeZone($tz); |
|---|
| 293 | return $tz->getOffset(new DateTime('now')) / 3600; |
|---|
| 294 | } |
|---|
| 295 | catch (Exception $e) { |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | return 0; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | /** |
|---|
| 303 | * Return requested DES crypto key. |
|---|
| 304 | * |
|---|
| 305 | * @param string $key Crypto key name |
|---|
| 306 | * @return string Crypto key |
|---|
| 307 | */ |
|---|
| 308 | public function get_crypto_key($key) |
|---|
| 309 | { |
|---|
| 310 | // Bomb out if the requested key does not exist |
|---|
| 311 | if (!array_key_exists($key, $this->prop)) { |
|---|
| 312 | rcube::raise_error(array( |
|---|
| 313 | 'code' => 500, 'type' => 'php', |
|---|
| 314 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 315 | 'message' => "Request for unconfigured crypto key \"$key\"" |
|---|
| 316 | ), true, true); |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | $key = $this->prop[$key]; |
|---|
| 320 | |
|---|
| 321 | // Bomb out if the configured key is not exactly 24 bytes long |
|---|
| 322 | if (strlen($key) != 24) { |
|---|
| 323 | rcube::raise_error(array( |
|---|
| 324 | 'code' => 500, 'type' => 'php', |
|---|
| 325 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 326 | 'message' => "Configured crypto key '$key' is not exactly 24 bytes long" |
|---|
| 327 | ), true, true); |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | return $key; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | /** |
|---|
| 335 | * Try to autodetect operating system and find the correct line endings |
|---|
| 336 | * |
|---|
| 337 | * @return string The appropriate mail header delimiter |
|---|
| 338 | */ |
|---|
| 339 | public function header_delimiter() |
|---|
| 340 | { |
|---|
| 341 | // use the configured delimiter for headers |
|---|
| 342 | if (!empty($this->prop['mail_header_delimiter'])) { |
|---|
| 343 | $delim = $this->prop['mail_header_delimiter']; |
|---|
| 344 | if ($delim == "\n" || $delim == "\r\n") |
|---|
| 345 | return $delim; |
|---|
| 346 | else |
|---|
| 347 | rcube::raise_error(array( |
|---|
| 348 | 'code' => 500, 'type' => 'php', |
|---|
| 349 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 350 | 'message' => "Invalid mail_header_delimiter setting" |
|---|
| 351 | ), true, false); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | $php_os = strtolower(substr(PHP_OS, 0, 3)); |
|---|
| 355 | |
|---|
| 356 | if ($php_os == 'win') |
|---|
| 357 | return "\r\n"; |
|---|
| 358 | |
|---|
| 359 | if ($php_os == 'mac') |
|---|
| 360 | return "\r\n"; |
|---|
| 361 | |
|---|
| 362 | return "\n"; |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | /** |
|---|
| 367 | * Return the mail domain configured for the given host |
|---|
| 368 | * |
|---|
| 369 | * @param string $host IMAP host |
|---|
| 370 | * @param boolean $encode If true, domain name will be converted to IDN ASCII |
|---|
| 371 | * @return string Resolved SMTP host |
|---|
| 372 | */ |
|---|
| 373 | public function mail_domain($host, $encode=true) |
|---|
| 374 | { |
|---|
| 375 | $domain = $host; |
|---|
| 376 | |
|---|
| 377 | if (is_array($this->prop['mail_domain'])) { |
|---|
| 378 | if (isset($this->prop['mail_domain'][$host])) |
|---|
| 379 | $domain = $this->prop['mail_domain'][$host]; |
|---|
| 380 | } |
|---|
| 381 | else if (!empty($this->prop['mail_domain'])) { |
|---|
| 382 | $domain = rcube_utils::parse_host($this->prop['mail_domain']); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | if ($encode) { |
|---|
| 386 | $domain = rcube_utils::idn_to_ascii($domain); |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | return $domain; |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | /** |
|---|
| 394 | * Getter for error state |
|---|
| 395 | * |
|---|
| 396 | * @return mixed Error message on error, False if no errors |
|---|
| 397 | */ |
|---|
| 398 | public function get_error() |
|---|
| 399 | { |
|---|
| 400 | return empty($this->errors) ? false : join("\n", $this->errors); |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | /** |
|---|
| 405 | * Internal getter for client's (browser) timezone identifier |
|---|
| 406 | */ |
|---|
| 407 | private function client_timezone() |
|---|
| 408 | { |
|---|
| 409 | return isset($_SESSION['timezone']) ? timezone_name_from_abbr("", $_SESSION['timezone'] * 3600, 0) : date_default_timezone_get(); |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | } |
|---|