]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_config.php
Imported Upstream version 0.2.1
[roundcube.git] / program / include / rcube_config.php
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-2009, RoundCube Dev. - Switzerland                 |
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  */
27 class rcube_config
28 {
29   private $prop = array();
30   private $errors = array();
31
32
33   /**
34    * Object constructor
35    */
36   public function __construct()
37   {
38     $this->load();
39   }
40
41
42   /**
43    * Load config from local config file
44    *
45    * @todo Remove global $CONFIG
46    */
47   private function load()
48   {
49     // start output buffering, we don't need any output yet, 
50     // it'll be cleared after reading of config files, etc.
51     ob_start();
52     
53     // load main config file
54     if (include(RCMAIL_CONFIG_DIR . '/main.inc.php'))
55       $this->prop = (array)$rcmail_config;
56     else
57       $this->errors[] = 'main.inc.php was not found.';
58
59     // load database config
60     if (include(RCMAIL_CONFIG_DIR . '/db.inc.php'))
61       $this->prop += (array)$rcmail_config;
62     else
63       $this->errors[] = 'db.inc.php was not found.';
64     
65     // load host-specific configuration
66     $this->load_host_config();
67
68     // set skin (with fallback to old 'skin_path' property)
69     if (empty($this->prop['skin']) && !empty($this->prop['skin_path']))
70       $this->prop['skin'] = str_replace('skins/', '', unslashify($this->prop['skin_path']));
71     else if (empty($this->prop['skin']))
72       $this->prop['skin'] = 'default';
73
74     // fix paths
75     $this->prop['log_dir'] = $this->prop['log_dir'] ? unslashify($this->prop['log_dir']) : INSTALL_PATH . 'logs';
76     $this->prop['temp_dir'] = $this->prop['temp_dir'] ? unslashify($this->prop['temp_dir']) : INSTALL_PATH . 'temp';
77
78     // fix default imap folders encoding
79     foreach (array('drafts_mbox', 'junk_mbox', 'sent_mbox', 'trash_mbox') as $folder)
80       $this->prop[$folder] = rcube_charset_convert($this->prop[$folder], RCMAIL_CHARSET, 'UTF-7');
81
82     if (!empty($this->prop['default_imap_folders']))
83       foreach ($this->prop['default_imap_folders'] as $n => $folder)
84         $this->prop['default_imap_folders'][$n] = rcube_charset_convert($folder, RCMAIL_CHARSET, 'UTF-7');
85
86     // set PHP error logging according to config
87     if ($this->prop['debug_level'] & 1) {
88       ini_set('log_errors', 1);
89
90       if ($this->prop['log_driver'] == 'syslog') {
91         ini_set('error_log', 'syslog');
92       } else {
93         ini_set('error_log', $this->prop['log_dir'].'/errors');
94       }
95     }
96     if ($this->prop['debug_level'] & 4) {
97       ini_set('display_errors', 1);
98     }
99     else {
100       ini_set('display_errors', 0);
101     }
102     
103     // clear output buffer
104     ob_end_clean();
105
106     // export config data
107     $GLOBALS['CONFIG'] = &$this->prop;
108   }
109   
110   
111   /**
112    * Load a host-specific config file if configured
113    * This will merge the host specific configuration with the given one
114    */
115   private function load_host_config()
116   {
117     $fname = null;
118
119     if (is_array($this->prop['include_host_config'])) {
120       $fname = $this->prop['include_host_config'][$_SERVER['HTTP_HOST']];
121     }
122     else if (!empty($this->prop['include_host_config'])) {
123       $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php';
124     }
125
126     if ($fname && is_file(RCMAIL_CONFIG_DIR . '/' . $fname)) {
127       include(RCMAIL_CONFIG_DIR . '/' . $fname);
128       $this->prop = array_merge($this->prop, (array)$rcmail_config);
129     }
130   }
131   
132   
133   /**
134    * Getter for a specific config parameter
135    *
136    * @param  string Parameter name
137    * @param  mixed  Default value if not set
138    * @return mixed  The requested config value
139    */
140   public function get($name, $def = null)
141   {
142     return isset($this->prop[$name]) ? $this->prop[$name] : $def;
143   }
144   
145   
146   /**
147    * Setter for a config parameter
148    *
149    * @param string Parameter name
150    * @param mixed  Parameter value
151    */
152   public function set($name, $value)
153   {
154     $this->prop[$name] = $value;
155   }
156   
157   
158   /**
159    * Override config options with the given values (eg. user prefs)
160    *
161    * @param array Hash array with config props to merge over
162    */
163   public function merge($prefs)
164   {
165     $this->prop = array_merge($this->prop, $prefs);
166   }
167   
168   
169   /**
170    * Getter for all config options
171    *
172    * @return array  Hash array containg all config properties
173    */
174   public function all()
175   {
176     return $this->prop;
177   }
178   
179   
180   /**
181    * Return a 24 byte key for the DES encryption
182    *
183    * @return string DES encryption key
184    */
185   public function get_des_key()
186   {
187     $key = !empty($this->prop['des_key']) ? $this->prop['des_key'] : 'rcmail?24BitPwDkeyF**ECB';
188     $len = strlen($key);
189
190     // make sure the key is exactly 24 chars long
191     if ($len<24)
192       $key .= str_repeat('_', 24-$len);
193     else if ($len>24)
194       substr($key, 0, 24);
195
196     return $key;
197   }
198   
199   
200   /**
201    * Try to autodetect operating system and find the correct line endings
202    *
203    * @return string The appropriate mail header delimiter
204    */
205   public function header_delimiter()
206   {
207     // use the configured delimiter for headers
208     if (!empty($this->prop['mail_header_delimiter']))
209       return $this->prop['mail_header_delimiter'];
210     else if (strtolower(substr(PHP_OS, 0, 3) == 'win'))
211       return "\r\n";
212     else if (strtolower(substr(PHP_OS, 0, 3) == 'mac'))
213       return "\r\n";
214     else
215       return "\n";
216   }
217
218   
219   
220   /**
221    * Return the mail domain configured for the given host
222    *
223    * @param string IMAP host
224    * @return string Resolved SMTP host
225    */
226   public function mail_domain($host)
227   {
228     $domain = $host;
229     
230     if (is_array($this->prop['mail_domain'])) {
231       if (isset($this->prop['mail_domain'][$host]))
232         $domain = $this->prop['mail_domain'][$host];
233     }
234     else if (!empty($this->prop['mail_domain']))
235       $domain = $this->prop['mail_domain'];
236     
237     return $domain;
238   }
239   
240   
241   /**
242    * Getter for error state
243    *
244    * @return mixed Error message on error, False if no errors
245    */
246   public function get_error()
247   {
248     return empty($this->errors) ? false : join("\n", $this->errors);
249   }
250
251
252 }
253