blob: ae914414d873b115d43ce0a675af1be76bf5b415 [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
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, 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();
joelcoxcee80752011-01-15 23:09:47 +010033 var $environment = ENVIRONMENT;
Derek Jones6d743e22010-03-02 13:22:03 -060034 var $_config_paths = array(APPPATH);
Derek Allard2067d1a2008-11-13 22:59:24 +000035
36 /**
37 * Constructor
38 *
39 * Sets the $config data from the primary config.php file as a class variable
40 *
41 * @access public
42 * @param string the config file name
43 * @param boolean if configuration values should be loaded into their own section
44 * @param boolean true if errors should just return false, false if an error message should be displayed
45 * @return boolean if the file was successfully loaded or not
46 */
Greg Akera9263282010-11-10 15:26:43 -060047 function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000048 {
Barry Mienydd671972010-10-04 16:33:58 +020049 $this->config =& get_config();
Derek Allard2067d1a2008-11-13 22:59:24 +000050 log_message('debug', "Config Class Initialized");
Phil Sturgeon4df8b222010-12-15 14:23:14 +000051
52 // Set the base_url automatically if none was provided
53 if ($this->config['base_url'] == '')
54 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +000055 if(isset($_SERVER['HTTP_HOST']))
56 {
Phil Sturgeonfb552382010-12-15 14:29:21 +000057 $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
Phil Sturgeon4df8b222010-12-15 14:23:14 +000058 $base_url .= '://'. $_SERVER['HTTP_HOST'];
59 $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
60 }
61
62 else
63 {
64 $base_url = 'http://localhost/';
65 }
66
67 $this->set_item('base_url', $base_url);
68 }
Derek Allard2067d1a2008-11-13 22:59:24 +000069 }
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 // --------------------------------------------------------------------
72
73 /**
74 * Load Config File
75 *
76 * @access public
77 * @param string the config file name
joelcoxcee80752011-01-15 23:09:47 +010078 * @param boolean if configuration values should be loaded into their own section
79 * @param boolean true if errors should just return false, false if an error message should be displayed
Derek Allard2067d1a2008-11-13 22:59:24 +000080 * @return boolean if the file was loaded correctly
Barry Mienydd671972010-10-04 16:33:58 +020081 */
Derek Allard2067d1a2008-11-13 22:59:24 +000082 function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
83 {
84 $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
Derek Jones6d743e22010-03-02 13:22:03 -060085 $loaded = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Jones6d743e22010-03-02 13:22:03 -060087 foreach($this->_config_paths as $path)
joelcoxcee80752011-01-15 23:09:47 +010088 {
89 $file_path = $path.'config/'.ENVIRONMENT.'/'.$file.EXT;
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Jones6d743e22010-03-02 13:22:03 -060091 if (in_array($file_path, $this->is_loaded, TRUE))
92 {
93 $loaded = TRUE;
94 continue;
95 }
Derek Allard2067d1a2008-11-13 22:59:24 +000096
joelcox96b72ae2011-01-16 14:16:18 +010097 if ( ! file_exists($file_path))
Derek Jones6d743e22010-03-02 13:22:03 -060098 {
joelcox96b72ae2011-01-16 14:16:18 +010099 log_message('debug', 'Config for '.ENVIRONMENT.' environment is not found. Trying global config.');
100 $file_path = $path.'config/'.$file.EXT;
101
102 if ( ! file_exists($file_path))
joelcoxcee80752011-01-15 23:09:47 +0100103 {
joelcox96b72ae2011-01-16 14:16:18 +0100104 continue;
joelcoxcee80752011-01-15 23:09:47 +0100105 }
Derek Jones6d743e22010-03-02 13:22:03 -0600106 }
joelcox96b72ae2011-01-16 14:16:18 +0100107
Derek Jones6d743e22010-03-02 13:22:03 -0600108 include($file_path);
109
110 if ( ! isset($config) OR ! is_array($config))
111 {
112 if ($fail_gracefully === TRUE)
113 {
114 return FALSE;
115 }
116 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Jones6d743e22010-03-02 13:22:03 -0600119 if ($use_sections === TRUE)
120 {
121 if (isset($this->config[$file]))
122 {
123 $this->config[$file] = array_merge($this->config[$file], $config);
124 }
125 else
126 {
127 $this->config[$file] = $config;
128 }
129 }
130 else
131 {
132 $this->config = array_merge($this->config, $config);
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Jones6d743e22010-03-02 13:22:03 -0600135 $this->is_loaded[] = $file_path;
136 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Jones6d743e22010-03-02 13:22:03 -0600138 $loaded = TRUE;
139 log_message('debug', 'Config file loaded: '.$file_path);
140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Jones6d743e22010-03-02 13:22:03 -0600142 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
144 if ($fail_gracefully === TRUE)
145 {
146 return FALSE;
147 }
joelcox96b72ae2011-01-16 14:16:18 +0100148 show_error('The configuration file '.ENVIRONMENT.'/'.$file.EXT.' and '.$file.EXT.' do not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
joelcoxcee80752011-01-15 23:09:47 +0100150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 return TRUE;
152 }
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 // --------------------------------------------------------------------
155
156 /**
157 * Fetch a config file item
158 *
159 *
160 * @access public
161 * @param string the config item name
162 * @param string the index name
163 * @param bool
164 * @return string
165 */
166 function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200167 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200169 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 if ( ! isset($this->config[$item]))
171 {
172 return FALSE;
173 }
174
175 $pref = $this->config[$item];
176 }
177 else
178 {
179 if ( ! isset($this->config[$index]))
180 {
181 return FALSE;
182 }
183
184 if ( ! isset($this->config[$index][$item]))
185 {
186 return FALSE;
187 }
188
189 $pref = $this->config[$index][$item];
190 }
191
192 return $pref;
193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
195 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000196
197 /**
198 * Fetch a config file item - adds slash after item
199 *
200 * The second parameter allows a slash to be added to the end of
201 * the item, in the case of a path.
202 *
203 * @access public
204 * @param string the config item name
205 * @param bool
206 * @return string
207 */
208 function slash_item($item)
209 {
210 if ( ! isset($this->config[$item]))
211 {
212 return FALSE;
213 }
214
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
222 *
223 * @access public
224 * @param string the URI string
225 * @return string
226 */
227 function site_url($uri = '')
228 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 if ($uri == '')
230 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000231 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600232 }
233
234 if ($this->item('enable_query_strings') == FALSE)
235 {
236 if (is_array($uri))
237 {
238 $uri = implode('/', $uri);
239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Jones6d743e22010-03-02 13:22:03 -0600241 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
Barry Mienydd671972010-10-04 16:33:58 +0200242 return $this->slash_item('base_url').$this->slash_item('index_page').trim($uri, '/').$suffix;
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
244 else
245 {
Derek Jones6d743e22010-03-02 13:22:03 -0600246 if (is_array($uri))
247 {
248 $i = 0;
249 $str = '';
250 foreach ($uri as $key => $val)
251 {
252 $prefix = ($i == 0) ? '' : '&';
253 $str .= $prefix.$key.'='.$val;
254 $i++;
255 }
256
257 $uri = $str;
258 }
259
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000260 return $this->slash_item('base_url').$this->item('index_page').'?'.$uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 // --------------------------------------------------------------------
265
266 /**
267 * System URL
268 *
269 * @access public
270 * @return string
271 */
272 function system_url()
273 {
274 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
275 return $this->slash_item('base_url').end($x).'/';
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 // --------------------------------------------------------------------
279
280 /**
281 * Set a config file item
282 *
283 * @access public
284 * @param string the config item key
285 * @param string the config item value
286 * @return void
287 */
288 function set_item($item, $value)
289 {
290 $this->config[$item] = $value;
291 }
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Jones6d743e22010-03-02 13:22:03 -0600293 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000294
Derek Jones6d743e22010-03-02 13:22:03 -0600295 /**
296 * Assign to Config
297 *
298 * This function is called by the front controller (CodeIgniter.php)
299 * after the Config class is instantiated. It permits config items
300 * to be assigned or overriden by variables contained in the index.php file
301 *
302 * @access private
303 * @param array
304 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200305 */
Derek Jones6d743e22010-03-02 13:22:03 -0600306 function _assign_to_config($items = array())
307 {
308 if (is_array($items))
309 {
310 foreach ($items as $key => $val)
311 {
312 $this->set_item($key, $val);
313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314 }
Derek Jones6d743e22010-03-02 13:22:03 -0600315 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000316}
317
318// END CI_Config class
319
320/* End of file Config.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600321/* Location: ./system/core/Config.php */