blob: 714c4667bf87e0a559dbfab4e9a1ff27bdbde706 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
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
David Behler2d2c9c62011-08-14 20:03:08 +020031 /**
32 * List of all loaded config values
33 *
34 * @var array
35 */
Derek Allard2067d1a2008-11-13 22:59:24 +000036 var $config = array();
David Behler2d2c9c62011-08-14 20:03:08 +020037 /**
38 * List of all loaded config files
39 *
40 * @var array
41 */
Derek Allard2067d1a2008-11-13 22:59:24 +000042 var $is_loaded = array();
David Behler2d2c9c62011-08-14 20:03:08 +020043 /**
44 * List of paths to search when trying to load a config file
45 *
46 * @var array
47 */
Derek Jones6d743e22010-03-02 13:22:03 -060048 var $_config_paths = array(APPPATH);
Derek Allard2067d1a2008-11-13 22:59:24 +000049
50 /**
51 * Constructor
52 *
53 * Sets the $config data from the primary config.php file as a class variable
54 *
Derek Jones4b9c6292011-07-01 17:40:48 -050055 * @access public
56 * @param string the config file name
57 * @param boolean if configuration values should be loaded into their own section
58 * @param boolean true if errors should just return false, false if an error message should be displayed
59 * @return boolean if the file was successfully loaded or not
Derek Allard2067d1a2008-11-13 22:59:24 +000060 */
Greg Akera9263282010-11-10 15:26:43 -060061 function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000062 {
Barry Mienydd671972010-10-04 16:33:58 +020063 $this->config =& get_config();
Derek Allard2067d1a2008-11-13 22:59:24 +000064 log_message('debug', "Config Class Initialized");
Phil Sturgeon4df8b222010-12-15 14:23:14 +000065
66 // Set the base_url automatically if none was provided
67 if ($this->config['base_url'] == '')
68 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -050069 if (isset($_SERVER['HTTP_HOST']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000070 {
Phil Sturgeonfb552382010-12-15 14:29:21 +000071 $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
Phil Sturgeon4df8b222010-12-15 14:23:14 +000072 $base_url .= '://'. $_SERVER['HTTP_HOST'];
Eric Barnes32dbac22011-04-26 22:51:32 -040073 $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
Phil Sturgeon4df8b222010-12-15 14:23:14 +000074 }
75
76 else
77 {
78 $base_url = 'http://localhost/';
79 }
80
81 $this->set_item('base_url', $base_url);
82 }
Derek Allard2067d1a2008-11-13 22:59:24 +000083 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Allard2067d1a2008-11-13 22:59:24 +000085 // --------------------------------------------------------------------
86
87 /**
88 * Load Config File
89 *
90 * @access public
91 * @param string the config file name
Derek Jones4b9c6292011-07-01 17:40:48 -050092 * @param boolean if configuration values should be loaded into their own section
93 * @param boolean true if errors should just return false, false if an error message should be displayed
Derek Allard2067d1a2008-11-13 22:59:24 +000094 * @return boolean if the file was loaded correctly
Barry Mienydd671972010-10-04 16:33:58 +020095 */
Derek Allard2067d1a2008-11-13 22:59:24 +000096 function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
97 {
Greg Aker3a746652011-04-19 10:59:47 -050098 $file = ($file == '') ? 'config' : str_replace('.php', '', $file);
Phil Sturgeon05fa6112011-04-06 22:57:43 +010099 $found = FALSE;
Derek Jones6d743e22010-03-02 13:22:03 -0600100 $loaded = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200101
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500102 foreach ($this->_config_paths as $path)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100103 {
104 $check_locations = defined('ENVIRONMENT')
105 ? array(ENVIRONMENT.'/'.$file, $file)
106 : array($file);
Barry Mienydd671972010-10-04 16:33:58 +0200107
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100108 foreach ($check_locations as $location)
Derek Jones6d743e22010-03-02 13:22:03 -0600109 {
Greg Aker3a746652011-04-19 10:59:47 -0500110 $file_path = $path.'config/'.$location.'.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100111
112 if (in_array($file_path, $this->is_loaded, TRUE))
113 {
114 $loaded = TRUE;
115 continue 2;
116 }
117
118 if (file_exists($file_path))
119 {
120 $found = TRUE;
121 break;
122 }
123 }
124
125 if ($found === FALSE)
126 {
Derek Jones6d743e22010-03-02 13:22:03 -0600127 continue;
128 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000129
Derek Jones6d743e22010-03-02 13:22:03 -0600130 include($file_path);
131
132 if ( ! isset($config) OR ! is_array($config))
133 {
134 if ($fail_gracefully === TRUE)
135 {
136 return FALSE;
137 }
138 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Jones6d743e22010-03-02 13:22:03 -0600141 if ($use_sections === TRUE)
142 {
143 if (isset($this->config[$file]))
144 {
145 $this->config[$file] = array_merge($this->config[$file], $config);
146 }
147 else
148 {
149 $this->config[$file] = $config;
150 }
151 }
152 else
153 {
154 $this->config = array_merge($this->config, $config);
155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Jones6d743e22010-03-02 13:22:03 -0600157 $this->is_loaded[] = $file_path;
158 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Jones6d743e22010-03-02 13:22:03 -0600160 $loaded = TRUE;
161 log_message('debug', 'Config file loaded: '.$file_path);
katzgraud127e612011-04-22 10:59:25 -0400162 break;
Derek Jones6d743e22010-03-02 13:22:03 -0600163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Jones6d743e22010-03-02 13:22:03 -0600165 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
167 if ($fail_gracefully === TRUE)
168 {
169 return FALSE;
170 }
Greg Aker3a746652011-04-19 10:59:47 -0500171 show_error('The configuration file '.$file.'.php'.' does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 return TRUE;
175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 // --------------------------------------------------------------------
178
179 /**
180 * Fetch a config file item
181 *
182 *
183 * @access public
184 * @param string the config item name
185 * @param string the index name
186 * @param bool
187 * @return string
188 */
189 function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200190 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200192 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 if ( ! isset($this->config[$item]))
194 {
195 return FALSE;
196 }
197
198 $pref = $this->config[$item];
199 }
200 else
201 {
202 if ( ! isset($this->config[$index]))
203 {
204 return FALSE;
205 }
206
207 if ( ! isset($this->config[$index][$item]))
208 {
209 return FALSE;
210 }
211
212 $pref = $this->config[$index][$item];
213 }
214
215 return $pref;
216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
218 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000219
220 /**
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800221 * Fetch a config file item - adds slash after item (if item is not empty)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 *
223 * @access public
224 * @param string the config item name
225 * @param bool
226 * @return string
227 */
228 function slash_item($item)
229 {
230 if ( ! isset($this->config[$item]))
231 {
232 return FALSE;
233 }
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800234 if( trim($this->config[$item]) == '')
235 {
236 return '';
237 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000238
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000239 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 // --------------------------------------------------------------------
243
244 /**
245 * Site URL
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800246 * Returns base_url . index_page [. uri_string]
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 *
248 * @access public
249 * @param string the URI string
250 * @return string
251 */
252 function site_url($uri = '')
253 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 if ($uri == '')
255 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000256 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600257 }
258
259 if ($this->item('enable_query_strings') == FALSE)
260 {
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800261 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
262 return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
263 }
264 else
265 {
266 return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
267 }
268 }
David Behler2d2c9c62011-08-14 20:03:08 +0200269
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800270 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200271
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800272 /**
273 * Base URL
274 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200275 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800276 * @access public
277 * @param string $uri
278 * @return string
279 */
280 function base_url($uri = '')
281 {
282 return $this->slash_item('base_url').ltrim($this->_uri_string($uri),'/');
283 }
David Behler2d2c9c62011-08-14 20:03:08 +0200284
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800285 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200286
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800287 /**
288 * Build URI string for use in Config::site_url() and Config::base_url()
David Behler2d2c9c62011-08-14 20:03:08 +0200289 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800290 * @access protected
291 * @param $uri
292 * @return string
293 */
294 protected function _uri_string($uri)
295 {
296 if ($this->item('enable_query_strings') == FALSE)
297 {
Derek Jones6d743e22010-03-02 13:22:03 -0600298 if (is_array($uri))
299 {
300 $uri = implode('/', $uri);
301 }
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800302 $uri = trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304 else
305 {
Derek Jones6d743e22010-03-02 13:22:03 -0600306 if (is_array($uri))
307 {
308 $i = 0;
309 $str = '';
310 foreach ($uri as $key => $val)
311 {
312 $prefix = ($i == 0) ? '' : '&';
313 $str .= $prefix.$key.'='.$val;
314 $i++;
315 }
Derek Jones6d743e22010-03-02 13:22:03 -0600316 $uri = $str;
317 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800319 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 /**
325 * System URL
326 *
327 * @access public
328 * @return string
329 */
330 function system_url()
331 {
332 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
333 return $this->slash_item('base_url').end($x).'/';
334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // --------------------------------------------------------------------
337
338 /**
339 * Set a config file item
340 *
341 * @access public
342 * @param string the config item key
343 * @param string the config item value
344 * @return void
345 */
346 function set_item($item, $value)
347 {
348 $this->config[$item] = $value;
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Jones6d743e22010-03-02 13:22:03 -0600351 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000352
Derek Jones6d743e22010-03-02 13:22:03 -0600353 /**
354 * Assign to Config
355 *
356 * This function is called by the front controller (CodeIgniter.php)
Derek Jones4b9c6292011-07-01 17:40:48 -0500357 * after the Config class is instantiated. It permits config items
Derek Jones6d743e22010-03-02 13:22:03 -0600358 * to be assigned or overriden by variables contained in the index.php file
359 *
360 * @access private
361 * @param array
362 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200363 */
Derek Jones6d743e22010-03-02 13:22:03 -0600364 function _assign_to_config($items = array())
365 {
366 if (is_array($items))
367 {
368 foreach ($items as $key => $val)
369 {
370 $this->set_item($key, $val);
371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372 }
Derek Jones6d743e22010-03-02 13:22:03 -0600373 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000374}
375
376// END CI_Config class
377
378/* End of file Config.php */
katzgraud127e612011-04-22 10:59:25 -0400379/* Location: ./system/core/Config.php */