source: github/program/include/rcube_config.php @ c73b195

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since c73b195 was c73b195, checked in by thomascube <thomas@…>, 4 years ago

Add function for plugins to load a local config file

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