blob: 75f945efd30cb8f56b1770d5ea6914fab1457429 [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 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -050054 if (isset($_SERVER['HTTP_HOST']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000055 {
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
Pascal Kriete5d5895f2011-02-14 13:27:07 -050086 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 /**
197 * Fetch a config file item - adds slash after item
198 *
199 * The second parameter allows a slash to be added to the end of
200 * the item, in the case of a path.
201 *
202 * @access public
203 * @param string the config item name
204 * @param bool
205 * @return string
206 */
207 function slash_item($item)
208 {
209 if ( ! isset($this->config[$item]))
210 {
211 return FALSE;
212 }
213
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000214 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // --------------------------------------------------------------------
218
219 /**
220 * Site URL
221 *
222 * @access public
223 * @param string the URI string
224 * @return string
225 */
226 function site_url($uri = '')
227 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 if ($uri == '')
229 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000230 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600231 }
232
233 if ($this->item('enable_query_strings') == FALSE)
234 {
235 if (is_array($uri))
236 {
237 $uri = implode('/', $uri);
238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Phil Sturgeon705a3ee2011-01-19 13:46:07 +0000240 $index = $this->item('index_page') == '' ? '' : $this->slash_item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600241 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
Phil Sturgeon705a3ee2011-01-19 13:46:07 +0000242 return $this->slash_item('base_url').$index.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 */