source: github/program/include/rcube_config.php @ f4f4354f

HEADcourier-fixdev-browser-capabilitiespdorelease-0.7release-0.8
Last change on this file since f4f4354f was f4f4354f, checked in by thomascube <thomas@…>, 20 months ago

This timezone stuff really is a hard one...

  • Property mode set to 100644
File size: 10.8 KB
Line 
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-2010, The Roundcube Dev Team                       |
9 | Licensed under the GNU GPL                                            |
10 |                                                                       |
11 | PURPOSE:                                                              |
12 |   Class to read configuration settings                                |
13 |                                                                       |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16 +-----------------------------------------------------------------------+
17
18 $Id$
19
20*/
21
22/**
23 * Configuration class for Roundcube
24 *
25 * @package Core
26 */
27class rcube_config
28{
29    private $prop = array();
30    private $errors = array();
31    private $userprefs = array();
32
33
34    /**
35     * Object constructor
36     */
37    public function __construct()
38    {
39        $this->load();
40    }
41
42
43    /**
44     * Load config from local config file
45     *
46     * @todo Remove global $CONFIG
47     */
48    private function load()
49    {
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.';
53
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.';
57
58        // load host-specific configuration
59        $this->load_host_config();
60
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';
66
67        // fix paths
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';
70
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');
74
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');
78
79        // set PHP error logging according to config
80        if ($this->prop['debug_level'] & 1) {
81            ini_set('log_errors', 1);
82
83            if ($this->prop['log_driver'] == 'syslog') {
84                ini_set('error_log', 'syslog');
85            }
86            else {
87                ini_set('error_log', $this->prop['log_dir'].'/errors');
88            }
89        }
90
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)));
93       
94        // set timezone auto settings values
95        if ($this->prop['timezone'] == 'auto') {
96          $this->prop['dst_active'] = intval(date('I'));
97          $this->prop['_timezone_value']   = date('Z') / 3600 - $this->prop['dst_active'];
98        }
99
100        // export config data
101        $GLOBALS['CONFIG'] = &$this->prop;
102    }
103
104    /**
105     * Load a host-specific config file if configured
106     * This will merge the host specific configuration with the given one
107     */
108    private function load_host_config()
109    {
110        $fname = null;
111
112        if (is_array($this->prop['include_host_config'])) {
113            $fname = $this->prop['include_host_config'][$_SERVER['HTTP_HOST']];
114        }
115        else if (!empty($this->prop['include_host_config'])) {
116            $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php';
117        }
118
119        if ($fname) {
120            $this->load_from_file(RCMAIL_CONFIG_DIR . '/' . $fname);
121        }
122    }
123
124
125    /**
126     * Read configuration from a file
127     * and merge with the already stored config values
128     *
129     * @param string $fpath Full path to the config file to be loaded
130     * @return booelan True on success, false on failure
131     */
132    public function load_from_file($fpath)
133    {
134        if (is_file($fpath) && is_readable($fpath)) {
135            // use output buffering, we don't need any output here
136            ob_start();
137            include($fpath);
138            ob_end_clean();
139
140            if (is_array($rcmail_config)) {
141                $this->prop = array_merge($this->prop, $rcmail_config, $this->userprefs);
142                return true;
143            }
144        }
145
146        return false;
147    }
148
149
150    /**
151     * Getter for a specific config parameter
152     *
153     * @param  string $name Parameter name
154     * @param  mixed  $def  Default value if not set
155     * @return mixed  The requested config value
156     */
157    public function get($name, $def = null)
158    {
159        $result = isset($this->prop[$name]) ? $this->prop[$name] : $def;
160        $rcmail = rcmail::get_instance();
161       
162        if ($name == 'timezone' && isset($this->prop['_timezone_value']))
163            $result = $this->prop['_timezone_value'];
164
165        if (is_object($rcmail->plugins)) {
166            $plugin = $rcmail->plugins->exec_hook('config_get', array(
167                'name' => $name, 'default' => $def, 'result' => $result));
168
169            return $plugin['result'];
170        }
171
172        return $result;
173    }
174
175
176    /**
177     * Setter for a config parameter
178     *
179     * @param string $name  Parameter name
180     * @param mixed  $value Parameter value
181     */
182    public function set($name, $value)
183    {
184        $this->prop[$name] = $value;
185    }
186
187
188    /**
189     * Override config options with the given values (eg. user prefs)
190     *
191     * @param array $prefs Hash array with config props to merge over
192     */
193    public function merge($prefs)
194    {
195        $this->prop = array_merge($this->prop, $prefs, $this->userprefs);
196    }
197
198
199    /**
200     * Merge the given prefs over the current config
201     * and make sure that they survive further merging.
202     *
203     * @param array $prefs Hash array with user prefs
204     */
205    public function set_user_prefs($prefs)
206    {
207        // Honor the dont_override setting for any existing user preferences
208        $dont_override = $this->get('dont_override');
209        if (is_array($dont_override) && !empty($dont_override)) {
210            foreach ($prefs as $key => $pref) {
211                if (in_array($key, $dont_override)) {
212                    unset($prefs[$key]);
213                }
214            }
215        }
216
217        $this->userprefs = $prefs;
218        $this->prop      = array_merge($this->prop, $prefs);
219
220        // override timezone settings with client values
221        if ($this->prop['timezone'] == 'auto') {
222            $this->prop['_timezone_value'] = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : $this->prop['_timezone_value'];
223            $this->prop['dst_active'] = $this->userprefs['dst_active'] = isset($_SESSION['dst_active']) ? $_SESSION['dst_active'] : $this->prop['dst_active'];
224        }
225        else if (isset($this->prop['_timezone_value']))
226           unset($this->prop['_timezone_value']);
227    }
228
229
230    /**
231     * Getter for all config options
232     *
233     * @return array  Hash array containg all config properties
234     */
235    public function all()
236    {
237        return $this->prop;
238    }
239
240    /**
241     * Special getter for user's timezone offset including DST
242     *
243     * @return float  Timezone offset (in hours)
244     */
245    public function get_timezone()
246    {
247      return floatval($this->get('timezone')) + intval($this->get('dst_active'));
248    }
249
250    /**
251     * Return requested DES crypto key.
252     *
253     * @param string $key Crypto key name
254     * @return string Crypto key
255     */
256    public function get_crypto_key($key)
257    {
258        // Bomb out if the requested key does not exist
259        if (!array_key_exists($key, $this->prop)) {
260            raise_error(array(
261                'code' => 500, 'type' => 'php',
262                'file' => __FILE__, 'line' => __LINE__,
263                'message' => "Request for unconfigured crypto key \"$key\""
264            ), true, true);
265        }
266
267        $key = $this->prop[$key];
268
269        // Bomb out if the configured key is not exactly 24 bytes long
270        if (strlen($key) != 24) {
271            raise_error(array(
272                'code' => 500, 'type' => 'php',
273                    'file' => __FILE__, 'line' => __LINE__,
274                'message' => "Configured crypto key '$key' is not exactly 24 bytes long"
275            ), true, true);
276        }
277
278        return $key;
279    }
280
281
282    /**
283     * Try to autodetect operating system and find the correct line endings
284     *
285     * @return string The appropriate mail header delimiter
286     */
287    public function header_delimiter()
288    {
289        // use the configured delimiter for headers
290        if (!empty($this->prop['mail_header_delimiter'])) {
291            $delim = $this->prop['mail_header_delimiter'];
292            if ($delim == "\n" || $delim == "\r\n")
293                return $delim;
294            else
295                raise_error(array(
296                    'code' => 500, 'type' => 'php',
297                        'file' => __FILE__, 'line' => __LINE__,
298                    'message' => "Invalid mail_header_delimiter setting"
299                ), true, false);
300        }
301
302        $php_os = strtolower(substr(PHP_OS, 0, 3));
303
304        if ($php_os == 'win')
305            return "\r\n";
306
307        if ($php_os == 'mac')
308            return "\r\n";
309
310        return "\n";
311    }
312
313
314    /**
315     * Return the mail domain configured for the given host
316     *
317     * @param string  $host   IMAP host
318     * @param boolean $encode If true, domain name will be converted to IDN ASCII
319     * @return string Resolved SMTP host
320     */
321    public function mail_domain($host, $encode=true)
322    {
323        $domain = $host;
324
325        if (is_array($this->prop['mail_domain'])) {
326            if (isset($this->prop['mail_domain'][$host]))
327                $domain = $this->prop['mail_domain'][$host];
328        }
329        else if (!empty($this->prop['mail_domain']))
330            $domain = rcube_parse_host($this->prop['mail_domain']);
331
332        if ($encode)
333            $domain = rcube_idn_to_ascii($domain);
334
335        return $domain;
336    }
337
338
339    /**
340     * Getter for error state
341     *
342     * @return mixed Error message on error, False if no errors
343     */
344    public function get_error()
345    {
346        return empty($this->errors) ? false : join("\n", $this->errors);
347    }
348
349}
Note: See TracBrowser for help on using the repository browser.