blob: 1096a9ea6d05324508a33273e64b4863ad37ace6 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
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 *
Derek Jones37f4b9c2011-07-01 17:56:50 -050040 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000045 */
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
Derek Jones37f4b9c2011-07-01 17:56:50 -050077 * @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 {
Greg Aker3a746652011-04-19 10:59:47 -050083 $file = ($file == '') ? 'config' : str_replace('.php', '', $file);
Phil Sturgeon05fa6112011-04-06 22:57:43 +010084 $found = FALSE;
Derek Jones6d743e22010-03-02 13:22:03 -060085 $loaded = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020086
Pascal Kriete5d5895f2011-02-14 13:27:07 -050087 foreach ($this->_config_paths as $path)
Phil Sturgeon05fa6112011-04-06 22:57:43 +010088 {
89 $check_locations = defined('ENVIRONMENT')
90 ? array(ENVIRONMENT.'/'.$file, $file)
91 : array($file);
Barry Mienydd671972010-10-04 16:33:58 +020092
Phil Sturgeon05fa6112011-04-06 22:57:43 +010093 foreach ($check_locations as $location)
Derek Jones6d743e22010-03-02 13:22:03 -060094 {
Greg Aker3a746652011-04-19 10:59:47 -050095 $file_path = $path.'config/'.$location.'.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +010096
97 if (in_array($file_path, $this->is_loaded, TRUE))
98 {
99 $loaded = TRUE;
100 continue 2;
101 }
102
103 if (file_exists($file_path))
104 {
105 $found = TRUE;
106 break;
107 }
108 }
109
110 if ($found === FALSE)
111 {
Derek Jones6d743e22010-03-02 13:22:03 -0600112 continue;
113 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000114
Derek Jones6d743e22010-03-02 13:22:03 -0600115 include($file_path);
116
117 if ( ! isset($config) OR ! is_array($config))
118 {
119 if ($fail_gracefully === TRUE)
120 {
121 return FALSE;
122 }
123 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Jones6d743e22010-03-02 13:22:03 -0600126 if ($use_sections === TRUE)
127 {
128 if (isset($this->config[$file]))
129 {
130 $this->config[$file] = array_merge($this->config[$file], $config);
131 }
132 else
133 {
134 $this->config[$file] = $config;
135 }
136 }
137 else
138 {
139 $this->config = array_merge($this->config, $config);
140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Jones6d743e22010-03-02 13:22:03 -0600142 $this->is_loaded[] = $file_path;
143 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Jones6d743e22010-03-02 13:22:03 -0600145 $loaded = TRUE;
146 log_message('debug', 'Config file loaded: '.$file_path);
katzgraud127e612011-04-22 10:59:25 -0400147 break;
Derek Jones6d743e22010-03-02 13:22:03 -0600148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Jones6d743e22010-03-02 13:22:03 -0600150 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
152 if ($fail_gracefully === TRUE)
153 {
154 return FALSE;
155 }
Greg Aker3a746652011-04-19 10:59:47 -0500156 show_error('The configuration file '.$file.'.php'.' does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 return TRUE;
160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // --------------------------------------------------------------------
163
164 /**
165 * Fetch a config file item
166 *
167 *
168 * @access public
169 * @param string the config item name
170 * @param string the index name
171 * @param bool
172 * @return string
173 */
174 function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200175 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200177 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 if ( ! isset($this->config[$item]))
179 {
180 return FALSE;
181 }
182
183 $pref = $this->config[$item];
184 }
185 else
186 {
187 if ( ! isset($this->config[$index]))
188 {
189 return FALSE;
190 }
191
192 if ( ! isset($this->config[$index][$item]))
193 {
194 return FALSE;
195 }
196
197 $pref = $this->config[$index][$item];
198 }
199
200 return $pref;
201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
203 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000204
205 /**
206 * Fetch a config file item - adds slash after item
207 *
208 * The second parameter allows a slash to be added to the end of
209 * the item, in the case of a path.
210 *
211 * @access public
212 * @param string the config item name
213 * @param bool
214 * @return string
215 */
216 function slash_item($item)
217 {
218 if ( ! isset($this->config[$item]))
219 {
220 return FALSE;
221 }
222
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000223 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 // --------------------------------------------------------------------
227
228 /**
229 * Site URL
230 *
231 * @access public
232 * @param string the URI string
233 * @return string
234 */
235 function site_url($uri = '')
236 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 if ($uri == '')
238 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000239 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600240 }
241
242 if ($this->item('enable_query_strings') == FALSE)
243 {
244 if (is_array($uri))
245 {
246 $uri = implode('/', $uri);
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Phil Sturgeon705a3ee2011-01-19 13:46:07 +0000249 $index = $this->item('index_page') == '' ? '' : $this->slash_item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600250 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
Phil Sturgeon705a3ee2011-01-19 13:46:07 +0000251 return $this->slash_item('base_url').$index.trim($uri, '/').$suffix;
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
253 else
254 {
Derek Jones6d743e22010-03-02 13:22:03 -0600255 if (is_array($uri))
256 {
257 $i = 0;
258 $str = '';
259 foreach ($uri as $key => $val)
260 {
261 $prefix = ($i == 0) ? '' : '&';
262 $str .= $prefix.$key.'='.$val;
263 $i++;
264 }
265
266 $uri = $str;
267 }
268
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000269 return $this->slash_item('base_url').$this->item('index_page').'?'.$uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // --------------------------------------------------------------------
274
275 /**
276 * System URL
277 *
278 * @access public
279 * @return string
280 */
281 function system_url()
282 {
283 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
284 return $this->slash_item('base_url').end($x).'/';
285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 // --------------------------------------------------------------------
288
289 /**
290 * Set a config file item
291 *
292 * @access public
293 * @param string the config item key
294 * @param string the config item value
295 * @return void
296 */
297 function set_item($item, $value)
298 {
299 $this->config[$item] = $value;
300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Jones6d743e22010-03-02 13:22:03 -0600302 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000303
Derek Jones6d743e22010-03-02 13:22:03 -0600304 /**
305 * Assign to Config
306 *
307 * This function is called by the front controller (CodeIgniter.php)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500308 * after the Config class is instantiated. It permits config items
Derek Jones6d743e22010-03-02 13:22:03 -0600309 * to be assigned or overriden by variables contained in the index.php file
310 *
311 * @access private
312 * @param array
313 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200314 */
Derek Jones6d743e22010-03-02 13:22:03 -0600315 function _assign_to_config($items = array())
316 {
317 if (is_array($items))
318 {
319 foreach ($items as $key => $val)
320 {
321 $this->set_item($key, $val);
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323 }
Derek Jones6d743e22010-03-02 13:22:03 -0600324 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000325}
326
327// END CI_Config class
328
329/* End of file Config.php */
katzgraud127e612011-04-22 10:59:25 -0400330/* Location: ./system/core/Config.php */