source: github/program/include/rcube_config.php @ 23b4955

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 23b4955 was 23b4955, checked in by alecpl <alec@…>, 2 years ago
  • Fix handling of debug_level=4 in ajax requests (#1487831)
  • Property mode set to 100644
File size: 9.4 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        // export config data
95        $GLOBALS['CONFIG'] = &$this->prop;
96    }
97
98    /**
99     * Load a host-specific config file if configured
100     * This will merge the host specific configuration with the given one
101     */
102    private function load_host_config()
103    {
104        $fname = null;
105
106        if (is_array($this->prop['include_host_config'])) {
107            $fname = $this->prop['include_host_config'][$_SERVER['HTTP_HOST']];
108        }
109        else if (!empty($this->prop['include_host_config'])) {
110            $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php';
111        }
112
113        if ($fname) {
114            $this->load_from_file(RCMAIL_CONFIG_DIR . '/' . $fname);
115        }
116    }
117
118
119    /**
120     * Read configuration from a file
121     * and merge with the already stored config values
122     *
123     * @param string $fpath Full path to the config file to be loaded
124     * @return booelan True on success, false on failure
125     */
126    public function load_from_file($fpath)
127    {
128        if (is_file($fpath) && is_readable($fpath)) {
129            // use output buffering, we don't need any output here
130            ob_start();
131            include($fpath);
132            ob_end_clean();
133
134            if (is_array($rcmail_config)) {
135                $this->prop = array_merge($this->prop, $rcmail_config, $this->userprefs);
136                return true;
137            }
138        }
139
140        return false;
141    }
142
143
144    /**
145     * Getter for a specific config parameter
146     *
147     * @param  string $name Parameter name
148     * @param  mixed  $def  Default value if not set
149     * @return mixed  The requested config value
150     */
151    public function get($name, $def = null)
152    {
153        return isset($this->prop[$name]) ? $this->prop[$name] : $def;
154    }
155
156
157    /**
158     * Setter for a config parameter
159     *
160     * @param string $name  Parameter name
161     * @param mixed  $value Parameter value
162     */
163    public function set($name, $value)
164    {
165        $this->prop[$name] = $value;
166    }
167
168
169    /**
170     * Override config options with the given values (eg. user prefs)
171     *
172     * @param array $prefs Hash array with config props to merge over
173     */
174    public function merge($prefs)
175    {
176        $this->prop = array_merge($this->prop, $prefs, $this->userprefs);
177    }
178
179
180    /**
181     * Merge the given prefs over the current config
182     * and make sure that they survive further merging.
183     *
184     * @param array $prefs Hash array with user prefs
185     */
186    public function set_user_prefs($prefs)
187    {
188        // Honor the dont_override setting for any existing user preferences
189        $dont_override = $this->get('dont_override');
190        if (is_array($dont_override) && !empty($dont_override)) {
191            foreach ($prefs as $key => $pref) {
192                if (in_array($key, $dont_override)) {
193                    unset($prefs[$key]);
194                }
195            }
196        }
197
198        $this->userprefs = $prefs;
199        $this->prop      = array_merge($this->prop, $prefs);
200    }
201
202
203    /**
204     * Getter for all config options
205     *
206     * @return array  Hash array containg all config properties
207     */
208    public function all()
209    {
210        return $this->prop;
211    }
212
213
214    /**
215     * Return requested DES crypto key.
216     *
217     * @param string $key Crypto key name
218     * @return string Crypto key
219     */
220    public function get_crypto_key($key)
221    {
222        // Bomb out if the requested key does not exist
223        if (!array_key_exists($key, $this->prop)) {
224            raise_error(array(
225                'code' => 500, 'type' => 'php',
226                'file' => __FILE__, 'line' => __LINE__,
227                'message' => "Request for unconfigured crypto key \"$key\""
228            ), true, true);
229        }
230
231        $key = $this->prop[$key];
232
233        // Bomb out if the configured key is not exactly 24 bytes long
234        if (strlen($key) != 24) {
235            raise_error(array(
236                'code' => 500, 'type' => 'php',
237                    'file' => __FILE__, 'line' => __LINE__,
238                'message' => "Configured crypto key '$key' is not exactly 24 bytes long"
239            ), true, true);
240        }
241
242        return $key;
243    }
244
245
246    /**
247     * Try to autodetect operating system and find the correct line endings
248     *
249     * @return string The appropriate mail header delimiter
250     */
251    public function header_delimiter()
252    {
253        // use the configured delimiter for headers
254        if (!empty($this->prop['mail_header_delimiter'])) {
255            $delim = $this->prop['mail_header_delimiter'];
256            if ($delim == "\n" || $delim == "\r\n")
257                return $delim;
258            else
259                raise_error(array(
260                    'code' => 500, 'type' => 'php',
261                        'file' => __FILE__, 'line' => __LINE__,
262                    'message' => "Invalid mail_header_delimiter setting"
263                ), true, false);
264        }
265
266        $php_os = strtolower(substr(PHP_OS, 0, 3));
267
268        if ($php_os == 'win')
269            return "\r\n";
270
271        if ($php_os == 'mac')
272            return "\r\n";
273
274        return "\n";
275    }
276
277
278    /**
279     * Return the mail domain configured for the given host
280     *
281     * @param string  $host   IMAP host
282     * @param boolean $encode If true, domain name will be converted to IDN ASCII
283     * @return string Resolved SMTP host
284     */
285    public function mail_domain($host, $encode=true)
286    {
287        $domain = $host;
288
289        if (is_array($this->prop['mail_domain'])) {
290            if (isset($this->prop['mail_domain'][$host]))
291                $domain = $this->prop['mail_domain'][$host];
292        }
293        else if (!empty($this->prop['mail_domain']))
294            $domain = rcube_parse_host($this->prop['mail_domain']);
295
296        if ($encode)
297            $domain = rcube_idn_to_ascii($domain);
298
299        return $domain;
300    }
301
302
303    /**
304     * Getter for error state
305     *
306     * @return mixed Error message on error, False if no errors
307     */
308    public function get_error()
309    {
310        return empty($this->errors) ? false : join("\n", $this->errors);
311    }
312
313}
Note: See TracBrowser for help on using the repository browser.