blob: 5cacf867cbddc68135e557574f392ded11a5b88d [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -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 Behler050abb82011-08-14 20:30:50 +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 Behler050abb82011-08-14 20:30:50 +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 Behler050abb82011-08-14 20:30:50 +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 Jones37f4b9c2011-07-01 17:56:50 -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'];
73 $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
74 }
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 Jones37f4b9c2011-07-01 17:56:50 -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 /**
221 * Fetch a config file item - adds slash after item
222 *
223 * The second parameter allows a slash to be added to the end of
224 * the item, in the case of a path.
225 *
226 * @access public
227 * @param string the config item name
228 * @param bool
229 * @return string
230 */
231 function slash_item($item)
232 {
233 if ( ! isset($this->config[$item]))
234 {
235 return FALSE;
236 }
237
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000238 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // --------------------------------------------------------------------
242
243 /**
244 * Site URL
245 *
246 * @access public
247 * @param string the URI string
248 * @return string
249 */
250 function site_url($uri = '')
251 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 if ($uri == '')
253 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000254 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600255 }
256
257 if ($this->item('enable_query_strings') == FALSE)
258 {
259 if (is_array($uri))
260 {
261 $uri = implode('/', $uri);
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Phil Sturgeon705a3ee2011-01-19 13:46:07 +0000264 $index = $this->item('index_page') == '' ? '' : $this->slash_item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600265 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
Phil Sturgeon705a3ee2011-01-19 13:46:07 +0000266 return $this->slash_item('base_url').$index.trim($uri, '/').$suffix;
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
268 else
269 {
Derek Jones6d743e22010-03-02 13:22:03 -0600270 if (is_array($uri))
271 {
272 $i = 0;
273 $str = '';
274 foreach ($uri as $key => $val)
275 {
276 $prefix = ($i == 0) ? '' : '&';
277 $str .= $prefix.$key.'='.$val;
278 $i++;
279 }
280
281 $uri = $str;
282 }
283
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000284 return $this->slash_item('base_url').$this->item('index_page').'?'.$uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // --------------------------------------------------------------------
289
290 /**
291 * System URL
292 *
293 * @access public
294 * @return string
295 */
296 function system_url()
297 {
298 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
299 return $this->slash_item('base_url').end($x).'/';
300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // --------------------------------------------------------------------
303
304 /**
305 * Set a config file item
306 *
307 * @access public
308 * @param string the config item key
309 * @param string the config item value
310 * @return void
311 */
312 function set_item($item, $value)
313 {
314 $this->config[$item] = $value;
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Jones6d743e22010-03-02 13:22:03 -0600317 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000318
Derek Jones6d743e22010-03-02 13:22:03 -0600319 /**
320 * Assign to Config
321 *
322 * This function is called by the front controller (CodeIgniter.php)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500323 * after the Config class is instantiated. It permits config items
Derek Jones6d743e22010-03-02 13:22:03 -0600324 * to be assigned or overriden by variables contained in the index.php file
325 *
326 * @access private
327 * @param array
328 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200329 */
Derek Jones6d743e22010-03-02 13:22:03 -0600330 function _assign_to_config($items = array())
331 {
332 if (is_array($items))
333 {
334 foreach ($items as $key => $val)
335 {
336 $this->set_item($key, $val);
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338 }
Derek Jones6d743e22010-03-02 13:22:03 -0600339 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000340}
341
342// END CI_Config class
343
344/* End of file Config.php */
katzgraud127e612011-04-22 10:59:25 -0400345/* Location: ./system/core/Config.php */