blob: d6b97d7efe9d9f61062dbe6278be093b8b8d5c7f [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();
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 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +000054 if(isset($_SERVER['HTTP_HOST']))
55 {
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
Derek Jones6d743e22010-03-02 13:22:03 -060086 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
Derek Jones6d743e22010-03-02 13:22:03 -0600240 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
Barry Mienydd671972010-10-04 16:33:58 +0200241 return $this->slash_item('base_url').$this->slash_item('index_page').trim($uri, '/').$suffix;
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243 else
244 {
Derek Jones6d743e22010-03-02 13:22:03 -0600245 if (is_array($uri))
246 {
247 $i = 0;
248 $str = '';
249 foreach ($uri as $key => $val)
250 {
251 $prefix = ($i == 0) ? '' : '&';
252 $str .= $prefix.$key.'='.$val;
253 $i++;
254 }
255
256 $uri = $str;
257 }
258
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000259 return $this->slash_item('base_url').$this->item('index_page').'?'.$uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // --------------------------------------------------------------------
264
265 /**
266 * System URL
267 *
268 * @access public
269 * @return string
270 */
271 function system_url()
272 {
273 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
274 return $this->slash_item('base_url').end($x).'/';
275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 // --------------------------------------------------------------------
278
279 /**
280 * Set a config file item
281 *
282 * @access public
283 * @param string the config item key
284 * @param string the config item value
285 * @return void
286 */
287 function set_item($item, $value)
288 {
289 $this->config[$item] = $value;
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Jones6d743e22010-03-02 13:22:03 -0600292 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000293
Derek Jones6d743e22010-03-02 13:22:03 -0600294 /**
295 * Assign to Config
296 *
297 * This function is called by the front controller (CodeIgniter.php)
298 * after the Config class is instantiated. It permits config items
299 * to be assigned or overriden by variables contained in the index.php file
300 *
301 * @access private
302 * @param array
303 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200304 */
Derek Jones6d743e22010-03-02 13:22:03 -0600305 function _assign_to_config($items = array())
306 {
307 if (is_array($items))
308 {
309 foreach ($items as $key => $val)
310 {
311 $this->set_item($key, $val);
312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313 }
Derek Jones6d743e22010-03-02 13:22:03 -0600314 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000315}
316
317// END CI_Config class
318
319/* End of file Config.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600320/* Location: ./system/core/Config.php */