alecpl
2008-10-03 f613a1e4e004dd03d7de80ea5a45bee71d3a3454
commit | author | age
197601 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_config.php                                      |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2008, 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();
8c72e3 30   private $errors = array();
197601 31
T 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
033478 44    *
T 45    * @todo Remove global $CONFIG
197601 46    */
T 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
bba657 54     if (include(RCMAIL_CONFIG_DIR . '/main.inc.php'))
8c72e3 55       $this->prop = (array)$rcmail_config;
T 56     else
57       $this->errors[] = 'main.inc.php was not found.';
197601 58
T 59     // load database config
bba657 60     if (include(RCMAIL_CONFIG_DIR . '/db.inc.php'))
8c72e3 61       $this->prop += (array)$rcmail_config;
T 62     else
63       $this->errors[] = 'db.inc.php was not found.';
2ca388 64     
T 65     // load host-specific configuration
66     $this->load_host_config();
197601 67
423065 68     // set skin (with fallback to old 'skin_path' property)
T 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
197601 74     // fix paths
T 75     $this->prop['log_dir'] = $this->prop['log_dir'] ? unslashify($this->prop['log_dir']) : INSTALL_PATH . 'logs';
423065 76     $this->prop['temp_dir'] = $this->prop['temp_dir'] ? unslashify($this->prop['temp_dir']) : INSTALL_PATH . 'temp';
197601 77     
T 78     // set PHP error logging according to config
79     if ($this->prop['debug_level'] & 1) {
80       ini_set('log_errors', 1);
b77d0d 81
A 82       if ($this->prop['log_driver'] == 'syslog') {
c8ae24 83         ini_set('error_log', 'syslog');
b77d0d 84       } else {
c8ae24 85         ini_set('error_log', $this->prop['log_dir'].'/errors');
b77d0d 86       }
197601 87     }
T 88     if ($this->prop['debug_level'] & 4) {
89       ini_set('display_errors', 1);
90     }
91     else {
92       ini_set('display_errors', 0);
93     }
94     
95     // clear output buffer
96     ob_end_clean();
033478 97
T 98     // export config data
99     $GLOBALS['CONFIG'] = &$this->prop;
197601 100   }
T 101   
102   
103   /**
104    * Load a host-specific config file if configured
105    * This will merge the host specific configuration with the given one
106    */
107   private function load_host_config()
108   {
109     $fname = null;
110
111     if (is_array($this->prop['include_host_config'])) {
112       $fname = $this->prop['include_host_config'][$_SERVER['HTTP_HOST']];
113     }
114     else if (!empty($this->prop['include_host_config'])) {
115       $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php';
116     }
117
bba657 118     if ($fname && is_file(RCMAIL_CONFIG_DIR . '/' . $fname)) {
T 119       include(RCMAIL_CONFIG_DIR . '/' . $fname);
197601 120       $this->prop = array_merge($this->prop, (array)$rcmail_config);
T 121     }
122   }
123   
124   
125   /**
126    * Getter for a specific config parameter
127    *
128    * @param  string Parameter name
129    * @param  mixed  Default value if not set
130    * @return mixed  The requested config value
131    */
132   public function get($name, $def = null)
133   {
134     return isset($this->prop[$name]) ? $this->prop[$name] : $def;
135   }
136   
137   
138   /**
139    * Setter for a config parameter
140    *
141    * @param string Parameter name
142    * @param mixed  Parameter value
143    */
144   public function set($name, $value)
145   {
146     $this->prop[$name] = $value;
147   }
148   
149   
150   /**
151    * Override config options with the given values (eg. user prefs)
152    *
153    * @param array Hash array with config props to merge over
154    */
155   public function merge($prefs)
156   {
157     $this->prop = array_merge($this->prop, $prefs);
158   }
159   
160   
161   /**
162    * Getter for all config options
163    *
164    * @return array  Hash array containg all config properties
165    */
166   public function all()
167   {
168     return $this->prop;
169   }
170   
1854c4 171   
T 172   /**
173    * Return a 24 byte key for the DES encryption
174    *
175    * @return string DES encryption key
176    */
177   public function get_des_key()
178   {
179     $key = !empty($this->prop['des_key']) ? $this->prop['des_key'] : 'rcmail?24BitPwDkeyF**ECB';
180     $len = strlen($key);
181
182     // make sure the key is exactly 24 chars long
183     if ($len<24)
184       $key .= str_repeat('_', 24-$len);
185     else if ($len>24)
186       substr($key, 0, 24);
187
188     return $key;
189   }
190   
191   
83a763 192   /**
T 193    * Try to autodetect operating system and find the correct line endings
194    *
195    * @return string The appropriate mail header delimiter
196    */
197   public function header_delimiter()
198   {
199     // use the configured delimiter for headers
200     if (!empty($this->prop['mail_header_delimiter']))
201       return $this->prop['mail_header_delimiter'];
202     else if (strtolower(substr(PHP_OS, 0, 3) == 'win'))
203       return "\r\n";
204     else if (strtolower(substr(PHP_OS, 0, 3) == 'mac'))
205       return "\r\n";
206     else
207       return "\n";
208   }
209
210   
211   
212   /**
213    * Return the mail domain configured for the given host
214    *
215    * @param string IMAP host
216    * @return string Resolved SMTP host
217    */
218   public function mail_domain($host)
219   {
220     $domain = $host;
221     
222     if (is_array($this->prop['mail_domain'])) {
223       if (isset($this->prop['mail_domain'][$host]))
224         $domain = $this->prop['mail_domain'][$host];
225     }
226     else if (!empty($this->prop['mail_domain']))
227       $domain = $this->prop['mail_domain'];
228     
229     return $domain;
230   }
8c72e3 231   
T 232   
233   /**
234    * Getter for error state
235    *
236    * @return mixed Error message on error, False if no errors
237    */
238   public function get_error()
239   {
240     return empty($this->errors) ? false : join("\n", $this->errors);
241   }
83a763 242
T 243
197601 244 }
T 245