blob: 56e3bccd86b0982d97e57f3c4cdefdbaaf94d2ef [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
joelcoxcee80752011-01-15 23:09:47 +010097 if ( ! $file_path)
Derek Jones6d743e22010-03-02 13:22:03 -060098 {
joelcoxcee80752011-01-15 23:09:47 +010099 if ( ! file_exists($path.'config/'.$file.EXT))
100 {
101 $file_path = $path.'config/'.$file.EXT;
102 }
Derek Jones6d743e22010-03-02 13:22:03 -0600103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Jones6d743e22010-03-02 13:22:03 -0600105 include($file_path);
106
107 if ( ! isset($config) OR ! is_array($config))
108 {
109 if ($fail_gracefully === TRUE)
110 {
111 return FALSE;
112 }
113 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
114 }
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Jones6d743e22010-03-02 13:22:03 -0600116 if ($use_sections === TRUE)
117 {
118 if (isset($this->config[$file]))
119 {
120 $this->config[$file] = array_merge($this->config[$file], $config);
121 }
122 else
123 {
124 $this->config[$file] = $config;
125 }
126 }
127 else
128 {
129 $this->config = array_merge($this->config, $config);
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Jones6d743e22010-03-02 13:22:03 -0600132 $this->is_loaded[] = $file_path;
133 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Jones6d743e22010-03-02 13:22:03 -0600135 $loaded = TRUE;
136 log_message('debug', 'Config file loaded: '.$file_path);
137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Jones6d743e22010-03-02 13:22:03 -0600139 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
141 if ($fail_gracefully === TRUE)
142 {
143 return FALSE;
144 }
joelcoxcee80752011-01-15 23:09:47 +0100145 show_error('The configuration file '.$environment.'/'.$file.EXT.' does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
joelcoxcee80752011-01-15 23:09:47 +0100147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 return TRUE;
149 }
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 // --------------------------------------------------------------------
152
153 /**
154 * Fetch a config file item
155 *
156 *
157 * @access public
158 * @param string the config item name
159 * @param string the index name
160 * @param bool
161 * @return string
162 */
163 function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200164 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200166 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 if ( ! isset($this->config[$item]))
168 {
169 return FALSE;
170 }
171
172 $pref = $this->config[$item];
173 }
174 else
175 {
176 if ( ! isset($this->config[$index]))
177 {
178 return FALSE;
179 }
180
181 if ( ! isset($this->config[$index][$item]))
182 {
183 return FALSE;
184 }
185
186 $pref = $this->config[$index][$item];
187 }
188
189 return $pref;
190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
192 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000193
194 /**
195 * Fetch a config file item - adds slash after item
196 *
197 * The second parameter allows a slash to be added to the end of
198 * the item, in the case of a path.
199 *
200 * @access public
201 * @param string the config item name
202 * @param bool
203 * @return string
204 */
205 function slash_item($item)
206 {
207 if ( ! isset($this->config[$item]))
208 {
209 return FALSE;
210 }
211
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000212 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 // --------------------------------------------------------------------
216
217 /**
218 * Site URL
219 *
220 * @access public
221 * @param string the URI string
222 * @return string
223 */
224 function site_url($uri = '')
225 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 if ($uri == '')
227 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000228 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600229 }
230
231 if ($this->item('enable_query_strings') == FALSE)
232 {
233 if (is_array($uri))
234 {
235 $uri = implode('/', $uri);
236 }
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Jones6d743e22010-03-02 13:22:03 -0600238 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
Barry Mienydd671972010-10-04 16:33:58 +0200239 return $this->slash_item('base_url').$this->slash_item('index_page').trim($uri, '/').$suffix;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241 else
242 {
Derek Jones6d743e22010-03-02 13:22:03 -0600243 if (is_array($uri))
244 {
245 $i = 0;
246 $str = '';
247 foreach ($uri as $key => $val)
248 {
249 $prefix = ($i == 0) ? '' : '&';
250 $str .= $prefix.$key.'='.$val;
251 $i++;
252 }
253
254 $uri = $str;
255 }
256
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000257 return $this->slash_item('base_url').$this->item('index_page').'?'.$uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // --------------------------------------------------------------------
262
263 /**
264 * System URL
265 *
266 * @access public
267 * @return string
268 */
269 function system_url()
270 {
271 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
272 return $this->slash_item('base_url').end($x).'/';
273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 // --------------------------------------------------------------------
276
277 /**
278 * Set a config file item
279 *
280 * @access public
281 * @param string the config item key
282 * @param string the config item value
283 * @return void
284 */
285 function set_item($item, $value)
286 {
287 $this->config[$item] = $value;
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Jones6d743e22010-03-02 13:22:03 -0600290 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000291
Derek Jones6d743e22010-03-02 13:22:03 -0600292 /**
293 * Assign to Config
294 *
295 * This function is called by the front controller (CodeIgniter.php)
296 * after the Config class is instantiated. It permits config items
297 * to be assigned or overriden by variables contained in the index.php file
298 *
299 * @access private
300 * @param array
301 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200302 */
Derek Jones6d743e22010-03-02 13:22:03 -0600303 function _assign_to_config($items = array())
304 {
305 if (is_array($items))
306 {
307 foreach ($items as $key => $val)
308 {
309 $this->set_item($key, $val);
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311 }
Derek Jones6d743e22010-03-02 13:22:03 -0600312 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000313}
314
315// END CI_Config class
316
317/* End of file Config.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600318/* Location: ./system/core/Config.php */