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