blob: 7e0443c1f3ae885766feaf5bcb1070ffbd0cfcad [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 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
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 */
46 function CI_Config()
47 {
Derek Jones6d743e22010-03-02 13:22:03 -060048 $this->config =& get_config();
Derek Allard2067d1a2008-11-13 22:59:24 +000049 log_message('debug', "Config Class Initialized");
50 }
51
52 // --------------------------------------------------------------------
53
54 /**
55 * Load Config File
56 *
57 * @access public
58 * @param string the config file name
59 * @return boolean if the file was loaded correctly
60 */
61 function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
62 {
63 $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
Derek Jones6d743e22010-03-02 13:22:03 -060064 $loaded = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000065
Derek Jones6d743e22010-03-02 13:22:03 -060066 foreach($this->_config_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
Derek Jones6d743e22010-03-02 13:22:03 -060068 $file_path = $path.'config/'.$file.EXT;
69
70 if (in_array($file_path, $this->is_loaded, TRUE))
71 {
72 $loaded = TRUE;
73 continue;
74 }
Derek Allard2067d1a2008-11-13 22:59:24 +000075
Derek Jones6d743e22010-03-02 13:22:03 -060076 if ( ! file_exists($path.'config/'.$file.EXT))
77 {
78 continue;
79 }
80
81 include($file_path);
82
83 if ( ! isset($config) OR ! is_array($config))
84 {
85 if ($fail_gracefully === TRUE)
86 {
87 return FALSE;
88 }
89 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
90 }
91
92 if ($use_sections === TRUE)
93 {
94 if (isset($this->config[$file]))
95 {
96 $this->config[$file] = array_merge($this->config[$file], $config);
97 }
98 else
99 {
100 $this->config[$file] = $config;
101 }
102 }
103 else
104 {
105 $this->config = array_merge($this->config, $config);
106 }
107
108 $this->is_loaded[] = $file_path;
109 unset($config);
110
111 $loaded = TRUE;
112 log_message('debug', 'Config file loaded: '.$file_path);
113 }
114
115 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
117 if ($fail_gracefully === TRUE)
118 {
119 return FALSE;
120 }
121 show_error('The configuration file '.$file.EXT.' does not exist.');
122 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 return TRUE;
125 }
126
127 // --------------------------------------------------------------------
128
129 /**
130 * Fetch a config file item
131 *
132 *
133 * @access public
134 * @param string the config item name
135 * @param string the index name
136 * @param bool
137 * @return string
138 */
139 function item($item, $index = '')
140 {
141 if ($index == '')
142 {
143 if ( ! isset($this->config[$item]))
144 {
145 return FALSE;
146 }
147
148 $pref = $this->config[$item];
149 }
150 else
151 {
152 if ( ! isset($this->config[$index]))
153 {
154 return FALSE;
155 }
156
157 if ( ! isset($this->config[$index][$item]))
158 {
159 return FALSE;
160 }
161
162 $pref = $this->config[$index][$item];
163 }
164
165 return $pref;
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Fetch a config file item - adds slash after item
172 *
173 * The second parameter allows a slash to be added to the end of
174 * the item, in the case of a path.
175 *
176 * @access public
177 * @param string the config item name
178 * @param bool
179 * @return string
180 */
181 function slash_item($item)
182 {
183 if ( ! isset($this->config[$item]))
184 {
185 return FALSE;
186 }
187
188 $pref = $this->config[$item];
189
190 if ($pref != '' && substr($pref, -1) != '/')
191 {
192 $pref .= '/';
193 }
194
195 return $pref;
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Site URL
202 *
203 * @access public
204 * @param string the URI string
205 * @return string
206 */
207 function site_url($uri = '')
208 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 if ($uri == '')
210 {
Derek Jones6d743e22010-03-02 13:22:03 -0600211 if ($this->item('base_url') == '')
212 {
213 return $this->item('index_page');
214 }
215 else
216 {
217 return $this->slash_item('base_url').$this->item('index_page');
218 }
219 }
220
221 if ($this->item('enable_query_strings') == FALSE)
222 {
223 if (is_array($uri))
224 {
225 $uri = implode('/', $uri);
226 }
227
228 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
229 return $this->slash_item('base_url').$this->slash_item('index_page').trim($uri, '/').$suffix;
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
231 else
232 {
Derek Jones6d743e22010-03-02 13:22:03 -0600233 if (is_array($uri))
234 {
235 $i = 0;
236 $str = '';
237 foreach ($uri as $key => $val)
238 {
239 $prefix = ($i == 0) ? '' : '&';
240 $str .= $prefix.$key.'='.$val;
241 $i++;
242 }
243
244 $uri = $str;
245 }
246
247 if ($this->item('base_url') == '')
248 {
249 return $this->item('index_page').'?'.$uri;
250 }
251 else
252 {
253 return $this->slash_item('base_url').$this->item('index_page').'?'.$uri;
254 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * System URL
262 *
263 * @access public
264 * @return string
265 */
266 function system_url()
267 {
268 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
269 return $this->slash_item('base_url').end($x).'/';
270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Set a config file item
276 *
277 * @access public
278 * @param string the config item key
279 * @param string the config item value
280 * @return void
281 */
282 function set_item($item, $value)
283 {
284 $this->config[$item] = $value;
285 }
Derek Jones6d743e22010-03-02 13:22:03 -0600286
287 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
Derek Jones6d743e22010-03-02 13:22:03 -0600289 /**
290 * Assign to Config
291 *
292 * This function is called by the front controller (CodeIgniter.php)
293 * after the Config class is instantiated. It permits config items
294 * to be assigned or overriden by variables contained in the index.php file
295 *
296 * @access private
297 * @param array
298 * @return void
299 */
300 function _assign_to_config($items = array())
301 {
302 if (is_array($items))
303 {
304 foreach ($items as $key => $val)
305 {
306 $this->set_item($key, $val);
307 }
308 }
309 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000310}
311
312// END CI_Config class
313
314/* End of file Config.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600315/* Location: ./system/core/Config.php */