blob: 554b7360ad42c1c9dac3b388cc76440b1327abe2 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Config Class
20 *
21 * This class contains functions that enable config files to be managed
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category Libraries
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/config.html
28 */
29class CI_Config {
30
31 var $config = array();
32 var $is_loaded = array();
Derek Jones6d743e22010-03-02 13:22:03 -060033 var $_config_paths = array(APPPATH);
Derek Allard2067d1a2008-11-13 22:59:24 +000034
35 /**
36 * Constructor
37 *
38 * Sets the $config data from the primary config.php file as a class variable
39 *
40 * @access public
41 * @param string the config file name
42 * @param boolean if configuration values should be loaded into their own section
43 * @param boolean true if errors should just return false, false if an error message should be displayed
44 * @return boolean if the file was successfully loaded or not
45 */
Greg Akera9263282010-11-10 15:26:43 -060046 function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000047 {
Barry Mienydd671972010-10-04 16:33:58 +020048 $this->config =& get_config();
Derek Allard2067d1a2008-11-13 22:59:24 +000049 log_message('debug', "Config Class Initialized");
Phil Sturgeon4df8b222010-12-15 14:23:14 +000050
51 // Set the base_url automatically if none was provided
52 if ($this->config['base_url'] == '')
53 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +000054 if(isset($_SERVER['HTTP_HOST']))
55 {
Phil Sturgeonfb552382010-12-15 14:29:21 +000056 $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
Phil Sturgeon4df8b222010-12-15 14:23:14 +000057 $base_url .= '://'. $_SERVER['HTTP_HOST'];
58 $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
59 }
60
61 else
62 {
63 $base_url = 'http://localhost/';
64 }
65
66 $this->set_item('base_url', $base_url);
67 }
Derek Allard2067d1a2008-11-13 22:59:24 +000068 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 // --------------------------------------------------------------------
71
72 /**
73 * Load Config File
74 *
75 * @access public
76 * @param string the config file name
joelcoxcee80752011-01-15 23:09:47 +010077 * @param boolean if configuration values should be loaded into their own section
78 * @param boolean true if errors should just return false, false if an error message should be displayed
Derek Allard2067d1a2008-11-13 22:59:24 +000079 * @return boolean if the file was loaded correctly
Barry Mienydd671972010-10-04 16:33:58 +020080 */
Derek Allard2067d1a2008-11-13 22:59:24 +000081 function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
82 {
83 $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
Derek Jones6d743e22010-03-02 13:22:03 -060084 $loaded = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Jones6d743e22010-03-02 13:22:03 -060086 foreach($this->_config_paths as $path)
joelcoxcee80752011-01-15 23:09:47 +010087 {
88 $file_path = $path.'config/'.ENVIRONMENT.'/'.$file.EXT;
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Jones6d743e22010-03-02 13:22:03 -060090 if (in_array($file_path, $this->is_loaded, TRUE))
91 {
92 $loaded = TRUE;
93 continue;
94 }
Derek Allard2067d1a2008-11-13 22:59:24 +000095
joelcox96b72ae2011-01-16 14:16:18 +010096 if ( ! file_exists($file_path))
Derek Jones6d743e22010-03-02 13:22:03 -060097 {
joelcox96b72ae2011-01-16 14:16:18 +010098 log_message('debug', 'Config for '.ENVIRONMENT.' environment is not found. Trying global config.');
99 $file_path = $path.'config/'.$file.EXT;
100
101 if ( ! file_exists($file_path))
joelcoxcee80752011-01-15 23:09:47 +0100102 {
joelcox96b72ae2011-01-16 14:16:18 +0100103 continue;
joelcoxcee80752011-01-15 23:09:47 +0100104 }
Derek Jones6d743e22010-03-02 13:22:03 -0600105 }
joelcox96b72ae2011-01-16 14:16:18 +0100106
Derek Jones6d743e22010-03-02 13:22:03 -0600107 include($file_path);
108
109 if ( ! isset($config) OR ! is_array($config))
110 {
111 if ($fail_gracefully === TRUE)
112 {
113 return FALSE;
114 }
115 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
116 }
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Jones6d743e22010-03-02 13:22:03 -0600118 if ($use_sections === TRUE)
119 {
120 if (isset($this->config[$file]))
121 {
122 $this->config[$file] = array_merge($this->config[$file], $config);
123 }
124 else
125 {
126 $this->config[$file] = $config;
127 }
128 }
129 else
130 {
131 $this->config = array_merge($this->config, $config);
132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Jones6d743e22010-03-02 13:22:03 -0600134 $this->is_loaded[] = $file_path;
135 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Jones6d743e22010-03-02 13:22:03 -0600137 $loaded = TRUE;
138 log_message('debug', 'Config file loaded: '.$file_path);
139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Jones6d743e22010-03-02 13:22:03 -0600141 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
143 if ($fail_gracefully === TRUE)
144 {
145 return FALSE;
146 }
joelcox96b72ae2011-01-16 14:16:18 +0100147 show_error('The configuration file '.ENVIRONMENT.'/'.$file.EXT.' and '.$file.EXT.' do not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
joelcoxcee80752011-01-15 23:09:47 +0100149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 return TRUE;
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 // --------------------------------------------------------------------
154
155 /**
156 * Fetch a config file item
157 *
158 *
159 * @access public
160 * @param string the config item name
161 * @param string the index name
162 * @param bool
163 * @return string
164 */
165 function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200166 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200168 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 if ( ! isset($this->config[$item]))
170 {
171 return FALSE;
172 }
173
174 $pref = $this->config[$item];
175 }
176 else
177 {
178 if ( ! isset($this->config[$index]))
179 {
180 return FALSE;
181 }
182
183 if ( ! isset($this->config[$index][$item]))
184 {
185 return FALSE;
186 }
187
188 $pref = $this->config[$index][$item];
189 }
190
191 return $pref;
192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
194 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000195
196 /**
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800197 * Fetch a config file item - adds slash after item (if item is not empty)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 *
199 * @access public
200 * @param string the config item name
201 * @param bool
202 * @return string
203 */
204 function slash_item($item)
205 {
206 if ( ! isset($this->config[$item]))
207 {
208 return FALSE;
209 }
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800210 if( trim($this->config[$item]) == '')
211 {
212 return '';
213 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000214
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000215 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // --------------------------------------------------------------------
219
220 /**
221 * Site URL
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800222 * Returns base_url . index_page [. uri_string]
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 *
224 * @access public
225 * @param string the URI string
226 * @return string
227 */
228 function site_url($uri = '')
229 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 if ($uri == '')
231 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000232 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600233 }
234
235 if ($this->item('enable_query_strings') == FALSE)
236 {
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800237 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
238 return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
239 }
240 else
241 {
242 return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
243 }
244 }
245
246 // -------------------------------------------------------------
247
248 /**
249 * Base URL
250 * Returns base_url [. uri_string]
251 *
252 * @access public
253 * @param string $uri
254 * @return string
255 */
256 function base_url($uri = '')
257 {
258 return $this->slash_item('base_url').ltrim($this->_uri_string($uri),'/');
259 }
260
261 // -------------------------------------------------------------
262
263 /**
264 * Build URI string for use in Config::site_url() and Config::base_url()
265 *
266 * @access protected
267 * @param $uri
268 * @return string
269 */
270 protected function _uri_string($uri)
271 {
272 if ($this->item('enable_query_strings') == FALSE)
273 {
Derek Jones6d743e22010-03-02 13:22:03 -0600274 if (is_array($uri))
275 {
276 $uri = implode('/', $uri);
277 }
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800278 $uri = trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280 else
281 {
Derek Jones6d743e22010-03-02 13:22:03 -0600282 if (is_array($uri))
283 {
284 $i = 0;
285 $str = '';
286 foreach ($uri as $key => $val)
287 {
288 $prefix = ($i == 0) ? '' : '&';
289 $str .= $prefix.$key.'='.$val;
290 $i++;
291 }
Derek Jones6d743e22010-03-02 13:22:03 -0600292 $uri = $str;
293 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800295 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 /**
301 * System URL
302 *
303 * @access public
304 * @return string
305 */
306 function system_url()
307 {
308 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
309 return $this->slash_item('base_url').end($x).'/';
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // --------------------------------------------------------------------
313
314 /**
315 * Set a config file item
316 *
317 * @access public
318 * @param string the config item key
319 * @param string the config item value
320 * @return void
321 */
322 function set_item($item, $value)
323 {
324 $this->config[$item] = $value;
325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Jones6d743e22010-03-02 13:22:03 -0600327 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000328
Derek Jones6d743e22010-03-02 13:22:03 -0600329 /**
330 * Assign to Config
331 *
332 * This function is called by the front controller (CodeIgniter.php)
333 * after the Config class is instantiated. It permits config items
334 * to be assigned or overriden by variables contained in the index.php file
335 *
336 * @access private
337 * @param array
338 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200339 */
Derek Jones6d743e22010-03-02 13:22:03 -0600340 function _assign_to_config($items = array())
341 {
342 if (is_array($items))
343 {
344 foreach ($items as $key => $val)
345 {
346 $this->set_item($key, $val);
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348 }
Derek Jones6d743e22010-03-02 13:22:03 -0600349 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000350}
351
352// END CI_Config class
353
354/* End of file Config.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600355/* Location: ./system/core/Config.php */