]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_config.php
Imported Upstream version 0.3
[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 (!$this->load_from_file(RCMAIL_CONFIG_DIR . '/main.inc.php'))
55       $this->errors[] = 'main.inc.php was not found.';
56
57     // load database config
58     if (!$this->load_from_file(RCMAIL_CONFIG_DIR . '/db.inc.php'))
59       $this->errors[] = 'db.inc.php was not found.';
60     
61     // load host-specific configuration
62     $this->load_host_config();
63
64     // set skin (with fallback to old 'skin_path' property)
65     if (empty($this->prop['skin']) && !empty($this->prop['skin_path']))
66       $this->prop['skin'] = str_replace('skins/', '', unslashify($this->prop['skin_path']));
67     else if (empty($this->prop['skin']))
68       $this->prop['skin'] = 'default';
69
70     // fix paths
71     $this->prop['log_dir'] = $this->prop['log_dir'] ? unslashify($this->prop['log_dir']) : INSTALL_PATH . 'logs';
72     $this->prop['temp_dir'] = $this->prop['temp_dir'] ? unslashify($this->prop['temp_dir']) : INSTALL_PATH . 'temp';
73
74     // fix default imap folders encoding
75     foreach (array('drafts_mbox', 'junk_mbox', 'sent_mbox', 'trash_mbox') as $folder)
76       $this->prop[$folder] = rcube_charset_convert($this->prop[$folder], RCMAIL_CHARSET, 'UTF7-IMAP');
77
78     if (!empty($this->prop['default_imap_folders']))
79       foreach ($this->prop['default_imap_folders'] as $n => $folder)
80         $this->prop['default_imap_folders'][$n] = rcube_charset_convert($folder, RCMAIL_CHARSET, 'UTF7-IMAP');
81
82     // set PHP error logging according to config
83     if ($this->prop['debug_level'] & 1) {
84       ini_set('log_errors', 1);
85
86       if ($this->prop['log_driver'] == 'syslog') {
87         ini_set('error_log', 'syslog');
88       } else {
89         ini_set('error_log', $this->prop['log_dir'].'/errors');
90       }
91     }
92     if ($this->prop['debug_level'] & 4) {
93       ini_set('display_errors', 1);
94     }
95     else {
96       ini_set('display_errors', 0);
97     }
98     
99     // clear output buffer
100     ob_end_clean();
101
102     // export config data
103     $GLOBALS['CONFIG'] = &$this->prop;
104   }
105   
106   
107   /**
108    * Load a host-specific config file if configured
109    * This will merge the host specific configuration with the given one
110    */
111   private function load_host_config()
112   {
113     $fname = null;
114
115     if (is_array($this->prop['include_host_config'])) {
116       $fname = $this->prop['include_host_config'][$_SERVER['HTTP_HOST']];
117     }
118     else if (!empty($this->prop['include_host_config'])) {
119       $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php';
120     }
121
122     if ($fname) {
123       $this->load_from_file(RCMAIL_CONFIG_DIR . '/' . $fname);
124     }
125   }
126   
127   
128   /**
129    * Read configuration from a file
130    * and merge with the already stored config values
131    *
132    * @param string Full path to the config file to be loaded
133    * @return booelan True on success, false on failure
134    */
135   public function load_from_file($fpath)
136   {
137     if (is_file($fpath) && is_readable($fpath)) {
138       include($fpath);
139       if (is_array($rcmail_config)) {
140         $this->prop = array_merge($this->prop, $rcmail_config);
141         return true;
142       }
143     }
144     
145     return false;
146   }
147   
148   
149   /**
150    * Getter for a specific config parameter
151    *
152    * @param  string Parameter name
153    * @param  mixed  Default value if not set
154    * @return mixed  The requested config value
155    */
156   public function get($name, $def = null)
157   {
158     return isset($this->prop[$name]) ? $this->prop[$name] : $def;
159   }
160   
161   
162   /**
163    * Setter for a config parameter
164    *
165    * @param string Parameter name
166    * @param mixed  Parameter value
167    */
168   public function set($name, $value)
169   {
170     $this->prop[$name] = $value;
171   }
172   
173   
174   /**
175    * Override config options with the given values (eg. user prefs)
176    *
177    * @param array Hash array with config props to merge over
178    */
179   public function merge($prefs)
180   {
181     $this->prop = array_merge($this->prop, $prefs);
182   }
183   
184   
185   /**
186    * Getter for all config options
187    *
188    * @return array  Hash array containg all config properties
189    */
190   public function all()
191   {
192     return $this->prop;
193   }
194
195   /**
196    * Return requested DES crypto key.
197    *
198    * @param string Crypto key name
199    * @return string Crypto key
200    */
201   public function get_crypto_key($key)
202   {
203     // Bomb out if the requested key does not exist
204     if (!array_key_exists($key, $this->prop))
205     {
206       raise_error(array(
207         'code' => 500,
208         'type' => 'php',
209         'file' => __FILE__,
210         'message' => "Request for unconfigured crypto key \"$key\""
211       ), true, true);
212     }
213   
214     $key = $this->prop[$key];
215   
216     // Bomb out if the configured key is not exactly 24 bytes long
217     if (strlen($key) != 24)
218     {
219       raise_error(array(
220         'code' => 500,
221         'type' => 'php',
222         'file' => __FILE__,
223         'message' => "Configured crypto key \"$key\" is not exactly 24 bytes long"
224       ), true, true);
225     }
226
227     return $key;
228   }
229
230   /**
231    * Try to autodetect operating system and find the correct line endings
232    *
233    * @return string The appropriate mail header delimiter
234    */
235   public function header_delimiter()
236   {
237     // use the configured delimiter for headers
238     if (!empty($this->prop['mail_header_delimiter']))
239       return $this->prop['mail_header_delimiter'];
240     else if (strtolower(substr(PHP_OS, 0, 3)) == 'win')
241       return "\r\n";
242     else if (strtolower(substr(PHP_OS, 0, 3)) == 'mac')
243       return "\r\n";
244     else
245       return "\n";
246   }
247
248   
249   
250   /**
251    * Return the mail domain configured for the given host
252    *
253    * @param string IMAP host
254    * @return string Resolved SMTP host
255    */
256   public function mail_domain($host)
257   {
258     $domain = $host;
259     
260     if (is_array($this->prop['mail_domain'])) {
261       if (isset($this->prop['mail_domain'][$host]))
262         $domain = $this->prop['mail_domain'][$host];
263     }
264     else if (!empty($this->prop['mail_domain']))
265       $domain = $this->prop['mail_domain'];
266     
267     return $domain;
268   }
269   
270   
271   /**
272    * Getter for error state
273    *
274    * @return mixed Error message on error, False if no errors
275    */
276   public function get_error()
277   {
278     return empty($this->errors) ? false : join("\n", $this->errors);
279   }
280
281
282 }
283