1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
---|
2 | /** |
---|
3 | * CodeIgniter |
---|
4 | * |
---|
5 | * An open source application development framework for PHP 5.1.6 or newer |
---|
6 | * |
---|
7 | * @package CodeIgniter |
---|
8 | * @author ExpressionEngine Dev Team |
---|
9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
---|
10 | * @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 | */ |
---|
29 | class CI_Config { |
---|
30 | |
---|
31 | /** |
---|
32 | * List of all loaded config values |
---|
33 | * |
---|
34 | * @var array |
---|
35 | */ |
---|
36 | var $config = array(); |
---|
37 | /** |
---|
38 | * List of all loaded config files |
---|
39 | * |
---|
40 | * @var array |
---|
41 | */ |
---|
42 | var $is_loaded = array(); |
---|
43 | /** |
---|
44 | * List of paths to search when trying to load a config file |
---|
45 | * |
---|
46 | * @var array |
---|
47 | */ |
---|
48 | var $_config_paths = array(APPPATH); |
---|
49 | |
---|
50 | /** |
---|
51 | * Constructor |
---|
52 | * |
---|
53 | * Sets the $config data from the primary config.php file as a class variable |
---|
54 | * |
---|
55 | * @access public |
---|
56 | * @param string the config file name |
---|
57 | * @param boolean if configuration values should be loaded into their own section |
---|
58 | * @param boolean true if errors should just return false, false if an error message should be displayed |
---|
59 | * @return boolean if the file was successfully loaded or not |
---|
60 | */ |
---|
61 | function __construct() |
---|
62 | { |
---|
63 | $this->config =& get_config(); |
---|
64 | log_message('debug', "Config Class Initialized"); |
---|
65 | |
---|
66 | // Set the base_url automatically if none was provided |
---|
67 | if ($this->config['base_url'] == '') |
---|
68 | { |
---|
69 | if (isset($_SERVER['HTTP_HOST'])) |
---|
70 | { |
---|
71 | $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http'; |
---|
72 | $base_url .= '://'. $_SERVER['HTTP_HOST']; |
---|
73 | $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); |
---|
74 | } |
---|
75 | |
---|
76 | else |
---|
77 | { |
---|
78 | $base_url = 'http://localhost/'; |
---|
79 | } |
---|
80 | |
---|
81 | $this->set_item('base_url', $base_url); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | // -------------------------------------------------------------------- |
---|
86 | |
---|
87 | /** |
---|
88 | * Load Config File |
---|
89 | * |
---|
90 | * @access public |
---|
91 | * @param string the config file name |
---|
92 | * @param boolean if configuration values should be loaded into their own section |
---|
93 | * @param boolean true if errors should just return false, false if an error message should be displayed |
---|
94 | * @return boolean if the file was loaded correctly |
---|
95 | */ |
---|
96 | function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) |
---|
97 | { |
---|
98 | $file = ($file == '') ? 'config' : str_replace('.php', '', $file); |
---|
99 | $found = FALSE; |
---|
100 | $loaded = FALSE; |
---|
101 | |
---|
102 | $check_locations = defined('ENVIRONMENT') |
---|
103 | ? array(ENVIRONMENT.'/'.$file, $file) |
---|
104 | : array($file); |
---|
105 | |
---|
106 | foreach ($this->_config_paths as $path) |
---|
107 | { |
---|
108 | foreach ($check_locations as $location) |
---|
109 | { |
---|
110 | $file_path = $path.'config/'.$location.'.php'; |
---|
111 | |
---|
112 | if (in_array($file_path, $this->is_loaded, TRUE)) |
---|
113 | { |
---|
114 | $loaded = TRUE; |
---|
115 | continue 2; |
---|
116 | } |
---|
117 | |
---|
118 | if (file_exists($file_path)) |
---|
119 | { |
---|
120 | $found = TRUE; |
---|
121 | break; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | if ($found === FALSE) |
---|
126 | { |
---|
127 | continue; |
---|
128 | } |
---|
129 | |
---|
130 | include($file_path); |
---|
131 | |
---|
132 | if ( ! isset($config) OR ! is_array($config)) |
---|
133 | { |
---|
134 | if ($fail_gracefully === TRUE) |
---|
135 | { |
---|
136 | return FALSE; |
---|
137 | } |
---|
138 | show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.'); |
---|
139 | } |
---|
140 | |
---|
141 | if ($use_sections === TRUE) |
---|
142 | { |
---|
143 | if (isset($this->config[$file])) |
---|
144 | { |
---|
145 | $this->config[$file] = array_merge($this->config[$file], $config); |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | $this->config[$file] = $config; |
---|
150 | } |
---|
151 | } |
---|
152 | else |
---|
153 | { |
---|
154 | $this->config = array_merge($this->config, $config); |
---|
155 | } |
---|
156 | |
---|
157 | $this->is_loaded[] = $file_path; |
---|
158 | unset($config); |
---|
159 | |
---|
160 | $loaded = TRUE; |
---|
161 | log_message('debug', 'Config file loaded: '.$file_path); |
---|
162 | break; |
---|
163 | } |
---|
164 | |
---|
165 | if ($loaded === FALSE) |
---|
166 | { |
---|
167 | if ($fail_gracefully === TRUE) |
---|
168 | { |
---|
169 | return FALSE; |
---|
170 | } |
---|
171 | show_error('The configuration file '.$file.'.php does not exist.'); |
---|
172 | } |
---|
173 | |
---|
174 | return TRUE; |
---|
175 | } |
---|
176 | |
---|
177 | // -------------------------------------------------------------------- |
---|
178 | |
---|
179 | /** |
---|
180 | * Fetch a config file item |
---|
181 | * |
---|
182 | * |
---|
183 | * @access public |
---|
184 | * @param string the config item name |
---|
185 | * @param string the index name |
---|
186 | * @param bool |
---|
187 | * @return string |
---|
188 | */ |
---|
189 | function item($item, $index = '') |
---|
190 | { |
---|
191 | if ($index == '') |
---|
192 | { |
---|
193 | if ( ! isset($this->config[$item])) |
---|
194 | { |
---|
195 | return FALSE; |
---|
196 | } |
---|
197 | |
---|
198 | $pref = $this->config[$item]; |
---|
199 | } |
---|
200 | else |
---|
201 | { |
---|
202 | if ( ! isset($this->config[$index])) |
---|
203 | { |
---|
204 | return FALSE; |
---|
205 | } |
---|
206 | |
---|
207 | if ( ! isset($this->config[$index][$item])) |
---|
208 | { |
---|
209 | return FALSE; |
---|
210 | } |
---|
211 | |
---|
212 | $pref = $this->config[$index][$item]; |
---|
213 | } |
---|
214 | |
---|
215 | return $pref; |
---|
216 | } |
---|
217 | |
---|
218 | // -------------------------------------------------------------------- |
---|
219 | |
---|
220 | /** |
---|
221 | * Fetch a config file item - adds slash after item (if item is not empty) |
---|
222 | * |
---|
223 | * @access public |
---|
224 | * @param string the config item name |
---|
225 | * @param bool |
---|
226 | * @return string |
---|
227 | */ |
---|
228 | function slash_item($item) |
---|
229 | { |
---|
230 | if ( ! isset($this->config[$item])) |
---|
231 | { |
---|
232 | return FALSE; |
---|
233 | } |
---|
234 | if( trim($this->config[$item]) == '') |
---|
235 | { |
---|
236 | return ''; |
---|
237 | } |
---|
238 | |
---|
239 | return rtrim($this->config[$item], '/').'/'; |
---|
240 | } |
---|
241 | |
---|
242 | // -------------------------------------------------------------------- |
---|
243 | |
---|
244 | /** |
---|
245 | * Site URL |
---|
246 | * Returns base_url . index_page [. uri_string] |
---|
247 | * |
---|
248 | * @access public |
---|
249 | * @param string the URI string |
---|
250 | * @return string |
---|
251 | */ |
---|
252 | function site_url($uri = '') |
---|
253 | { |
---|
254 | if ($uri == '') |
---|
255 | { |
---|
256 | return $this->slash_item('base_url').$this->item('index_page'); |
---|
257 | } |
---|
258 | |
---|
259 | if ($this->item('enable_query_strings') == FALSE) |
---|
260 | { |
---|
261 | $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix'); |
---|
262 | return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix; |
---|
263 | } |
---|
264 | else |
---|
265 | { |
---|
266 | return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri); |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | // ------------------------------------------------------------- |
---|
271 | |
---|
272 | /** |
---|
273 | * Base URL |
---|
274 | * Returns base_url [. uri_string] |
---|
275 | * |
---|
276 | * @access public |
---|
277 | * @param string $uri |
---|
278 | * @return string |
---|
279 | */ |
---|
280 | function base_url($uri = '') |
---|
281 | { |
---|
282 | return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/'); |
---|
283 | } |
---|
284 | |
---|
285 | // ------------------------------------------------------------- |
---|
286 | |
---|
287 | /** |
---|
288 | * Build URI string for use in Config::site_url() and Config::base_url() |
---|
289 | * |
---|
290 | * @access protected |
---|
291 | * @param $uri |
---|
292 | * @return string |
---|
293 | */ |
---|
294 | protected function _uri_string($uri) |
---|
295 | { |
---|
296 | if ($this->item('enable_query_strings') == FALSE) |
---|
297 | { |
---|
298 | if (is_array($uri)) |
---|
299 | { |
---|
300 | $uri = implode('/', $uri); |
---|
301 | } |
---|
302 | $uri = trim($uri, '/'); |
---|
303 | } |
---|
304 | else |
---|
305 | { |
---|
306 | if (is_array($uri)) |
---|
307 | { |
---|
308 | $i = 0; |
---|
309 | $str = ''; |
---|
310 | foreach ($uri as $key => $val) |
---|
311 | { |
---|
312 | $prefix = ($i == 0) ? '' : '&'; |
---|
313 | $str .= $prefix.$key.'='.$val; |
---|
314 | $i++; |
---|
315 | } |
---|
316 | $uri = $str; |
---|
317 | } |
---|
318 | } |
---|
319 | return $uri; |
---|
320 | } |
---|
321 | |
---|
322 | // -------------------------------------------------------------------- |
---|
323 | |
---|
324 | /** |
---|
325 | * System URL |
---|
326 | * |
---|
327 | * @access public |
---|
328 | * @return string |
---|
329 | */ |
---|
330 | function system_url() |
---|
331 | { |
---|
332 | $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH)); |
---|
333 | return $this->slash_item('base_url').end($x).'/'; |
---|
334 | } |
---|
335 | |
---|
336 | // -------------------------------------------------------------------- |
---|
337 | |
---|
338 | /** |
---|
339 | * Set a config file item |
---|
340 | * |
---|
341 | * @access public |
---|
342 | * @param string the config item key |
---|
343 | * @param string the config item value |
---|
344 | * @return void |
---|
345 | */ |
---|
346 | function set_item($item, $value) |
---|
347 | { |
---|
348 | $this->config[$item] = $value; |
---|
349 | } |
---|
350 | |
---|
351 | // -------------------------------------------------------------------- |
---|
352 | |
---|
353 | /** |
---|
354 | * Assign to Config |
---|
355 | * |
---|
356 | * This function is called by the front controller (CodeIgniter.php) |
---|
357 | * after the Config class is instantiated. It permits config items |
---|
358 | * to be assigned or overriden by variables contained in the index.php file |
---|
359 | * |
---|
360 | * @access private |
---|
361 | * @param array |
---|
362 | * @return void |
---|
363 | */ |
---|
364 | function _assign_to_config($items = array()) |
---|
365 | { |
---|
366 | if (is_array($items)) |
---|
367 | { |
---|
368 | foreach ($items as $key => $val) |
---|
369 | { |
---|
370 | $this->set_item($key, $val); |
---|
371 | } |
---|
372 | } |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | // END CI_Config class |
---|
377 | |
---|
378 | /* End of file Config.php */ |
---|
379 | /* Location: ./system/core/Config.php */ |
---|