source: subversion/branches/devel-framework/roundcubemail/program/include/rcube_config.php @ 5758

Last change on this file since 5758 was 5758, checked in by alec, 17 months ago

Removed globals $DB, $USER, $IMAP
Added abstract class rcube_storage, a parent for rcube_imap class

Removed rcmail methods:
imap_init() > storage_init() or get_storage()
imap_connect() > storage_connect()

Renamed session variables:
imap_host, imap_port, imap_ssl > storage_*

Renamed storage (rcube_imap) methods:
set_default_mailboxes > set_default_folders
set_mailbox > set_folder
get_mailbox_name > get_folder
mailbox_status > folder_status
get_mailbox_size > folder_size
list_mailboxes > list_folders_subscribed
list_unsubscribed > list_folders
create_mailbox > create_folder
rename_mailbox > rename_folder
delete_mailbox > delete_folder
mailbox_exists > folder_exists
mailbox_namespace > folder_namespace
mod_mailbox > mod_folder
mailbox_attributes > folder_attributes
mailbox_data > folder_data
mailbox_info > folder_info
mailbox_sync > folder_sync
expunge > expunge_folder
clear_mailbox > clear_folder
get_headers > get_message_headers
list_headers > list_messages
messagecount > count
message_index > index
message_index_direct > index_direct

Removed storage (rcube_imap) methods:
reconnect
get_response_str

Removed storage (rcube_imap) public properties:
page_size > get_pagesize(), set_pagesize()
list_page > get_page(), set_page()
threading > get_threading()
search_set > get_search_set()

Renamed config option default_imap_folders > default_folders

  • Property svn:keywords set to Id
File size: 11.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_folders']))
76            foreach ($this->prop['default_folders'] as $n => $folder)
77                $this->prop['default_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['_timezone_value'] = $this->client_timezone();
97        }
98        else if (is_numeric($this->prop['timezone'])) {
99          $this->prop['timezone'] = timezone_name_from_abbr("", $this->prop['timezone'] * 3600, 0);
100        }
101
102        // remove deprecated properties
103        unset($this->prop['dst_active']);
104
105        // export config data
106        $GLOBALS['CONFIG'] = &$this->prop;
107    }
108
109    /**
110     * Load a host-specific config file if configured
111     * This will merge the host specific configuration with the given one
112     */
113    private function load_host_config()
114    {
115        $fname = null;
116
117        if (is_array($this->prop['include_host_config'])) {
118            $fname = $this->prop['include_host_config'][$_SERVER['HTTP_HOST']];
119        }
120        else if (!empty($this->prop['include_host_config'])) {
121            $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php';
122        }
123
124        if ($fname) {
125            $this->load_from_file(RCMAIL_CONFIG_DIR . '/' . $fname);
126        }
127    }
128
129
130    /**
131     * Read configuration from a file
132     * and merge with the already stored config values
133     *
134     * @param string $fpath Full path to the config file to be loaded
135     * @return booelan True on success, false on failure
136     */
137    public function load_from_file($fpath)
138    {
139        if (is_file($fpath) && is_readable($fpath)) {
140            // use output buffering, we don't need any output here
141            ob_start();
142            include($fpath);
143            ob_end_clean();
144
145            if (is_array($rcmail_config)) {
146                $this->prop = array_merge($this->prop, $rcmail_config, $this->userprefs);
147                return true;
148            }
149        }
150
151        return false;
152    }
153
154
155    /**
156     * Getter for a specific config parameter
157     *
158     * @param  string $name Parameter name
159     * @param  mixed  $def  Default value if not set
160     * @return mixed  The requested config value
161     */
162    public function get($name, $def = null)
163    {
164        $result = isset($this->prop[$name]) ? $this->prop[$name] : $def;
165        $rcmail = rcmail::get_instance();
166       
167        if ($name == 'timezone' && isset($this->prop['_timezone_value']))
168            $result = $this->prop['_timezone_value'];
169
170        if (is_object($rcmail->plugins)) {
171            $plugin = $rcmail->plugins->exec_hook('config_get', array(
172                'name' => $name, 'default' => $def, 'result' => $result));
173
174            return $plugin['result'];
175        }
176
177        return $result;
178    }
179
180
181    /**
182     * Setter for a config parameter
183     *
184     * @param string $name  Parameter name
185     * @param mixed  $value Parameter value
186     */
187    public function set($name, $value)
188    {
189        $this->prop[$name] = $value;
190    }
191
192
193    /**
194     * Override config options with the given values (eg. user prefs)
195     *
196     * @param array $prefs Hash array with config props to merge over
197     */
198    public function merge($prefs)
199    {
200        $this->prop = array_merge($this->prop, $prefs, $this->userprefs);
201    }
202
203
204    /**
205     * Merge the given prefs over the current config
206     * and make sure that they survive further merging.
207     *
208     * @param array $prefs Hash array with user prefs
209     */
210    public function set_user_prefs($prefs)
211    {
212        // Honor the dont_override setting for any existing user preferences
213        $dont_override = $this->get('dont_override');
214        if (is_array($dont_override) && !empty($dont_override)) {
215            foreach ($prefs as $key => $pref) {
216                if (in_array($key, $dont_override)) {
217                    unset($prefs[$key]);
218                }
219            }
220        }
221
222        // convert user's timezone into the new format
223        if (is_numeric($prefs['timezone'])) {
224            $prefs['timezone'] = timezone_name_from_abbr('', $prefs['timezone'] * 3600, 0);
225        }
226
227        $this->userprefs = $prefs;
228        $this->prop      = array_merge($this->prop, $prefs);
229
230        // override timezone settings with client values
231        if ($this->prop['timezone'] == 'auto') {
232            $this->prop['_timezone_value'] = isset($_SESSION['timezone']) ? $this->client_timezone() : $this->prop['_timezone_value'];
233        }
234        else if (isset($this->prop['_timezone_value']))
235           unset($this->prop['_timezone_value']);
236    }
237
238
239    /**
240     * Getter for all config options
241     *
242     * @return array  Hash array containg all config properties
243     */
244    public function all()
245    {
246        return $this->prop;
247    }
248
249    /**
250     * Special getter for user's timezone offset including DST
251     *
252     * @return float  Timezone offset (in hours)
253     * @deprecated
254     */
255    public function get_timezone()
256    {
257      if ($tz = $this->get('timezone')) {
258        try {
259          $tz = new DateTimeZone($tz);
260          return $tz->getOffset(new DateTime('now')) / 3600;
261        }
262        catch (Exception $e) {
263        }
264      }
265
266      return 0;
267    }
268
269    /**
270     * Return requested DES crypto key.
271     *
272     * @param string $key Crypto key name
273     * @return string Crypto key
274     */
275    public function get_crypto_key($key)
276    {
277        // Bomb out if the requested key does not exist
278        if (!array_key_exists($key, $this->prop)) {
279            raise_error(array(
280                'code' => 500, 'type' => 'php',
281                'file' => __FILE__, 'line' => __LINE__,
282                'message' => "Request for unconfigured crypto key \"$key\""
283            ), true, true);
284        }
285
286        $key = $this->prop[$key];
287
288        // Bomb out if the configured key is not exactly 24 bytes long
289        if (strlen($key) != 24) {
290            raise_error(array(
291                'code' => 500, 'type' => 'php',
292                    'file' => __FILE__, 'line' => __LINE__,
293                'message' => "Configured crypto key '$key' is not exactly 24 bytes long"
294            ), true, true);
295        }
296
297        return $key;
298    }
299
300
301    /**
302     * Try to autodetect operating system and find the correct line endings
303     *
304     * @return string The appropriate mail header delimiter
305     */
306    public function header_delimiter()
307    {
308        // use the configured delimiter for headers
309        if (!empty($this->prop['mail_header_delimiter'])) {
310            $delim = $this->prop['mail_header_delimiter'];
311            if ($delim == "\n" || $delim == "\r\n")
312                return $delim;
313            else
314                raise_error(array(
315                    'code' => 500, 'type' => 'php',
316                        'file' => __FILE__, 'line' => __LINE__,
317                    'message' => "Invalid mail_header_delimiter setting"
318                ), true, false);
319        }
320
321        $php_os = strtolower(substr(PHP_OS, 0, 3));
322
323        if ($php_os == 'win')
324            return "\r\n";
325
326        if ($php_os == 'mac')
327            return "\r\n";
328
329        return "\n";
330    }
331
332
333    /**
334     * Return the mail domain configured for the given host
335     *
336     * @param string  $host   IMAP host
337     * @param boolean $encode If true, domain name will be converted to IDN ASCII
338     * @return string Resolved SMTP host
339     */
340    public function mail_domain($host, $encode=true)
341    {
342        $domain = $host;
343
344        if (is_array($this->prop['mail_domain'])) {
345            if (isset($this->prop['mail_domain'][$host]))
346                $domain = $this->prop['mail_domain'][$host];
347        }
348        else if (!empty($this->prop['mail_domain']))
349            $domain = rcube_parse_host($this->prop['mail_domain']);
350
351        if ($encode)
352            $domain = rcube_idn_to_ascii($domain);
353
354        return $domain;
355    }
356
357
358    /**
359     * Getter for error state
360     *
361     * @return mixed Error message on error, False if no errors
362     */
363    public function get_error()
364    {
365        return empty($this->errors) ? false : join("\n", $this->errors);
366    }
367
368
369    /**
370     * Internal getter for client's (browser) timezone identifier
371     */
372    private function client_timezone()
373    {
374        return isset($_SESSION['timezone']) ? timezone_name_from_abbr("", $_SESSION['timezone'] * 3600, 0) : date_default_timezone_get();
375    }
376
377}
Note: See TracBrowser for help on using the repository browser.