blob: 8ecfba73aebd7019532f8473ae8f7c60474c92c7 [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
77 * @return boolean if the file was loaded correctly
Barry Mienydd671972010-10-04 16:33:58 +020078 */
Derek Allard2067d1a2008-11-13 22:59:24 +000079 function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
80 {
81 $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
Derek Jones6d743e22010-03-02 13:22:03 -060082 $loaded = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Jones6d743e22010-03-02 13:22:03 -060084 foreach($this->_config_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +000085 {
Derek Jones6d743e22010-03-02 13:22:03 -060086 $file_path = $path.'config/'.$file.EXT;
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Jones6d743e22010-03-02 13:22:03 -060088 if (in_array($file_path, $this->is_loaded, TRUE))
89 {
90 $loaded = TRUE;
91 continue;
92 }
Derek Allard2067d1a2008-11-13 22:59:24 +000093
Derek Jones6d743e22010-03-02 13:22:03 -060094 if ( ! file_exists($path.'config/'.$file.EXT))
95 {
96 continue;
97 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Jones6d743e22010-03-02 13:22:03 -060099 include($file_path);
100
101 if ( ! isset($config) OR ! is_array($config))
102 {
103 if ($fail_gracefully === TRUE)
104 {
105 return FALSE;
106 }
107 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Jones6d743e22010-03-02 13:22:03 -0600110 if ($use_sections === TRUE)
111 {
112 if (isset($this->config[$file]))
113 {
114 $this->config[$file] = array_merge($this->config[$file], $config);
115 }
116 else
117 {
118 $this->config[$file] = $config;
119 }
120 }
121 else
122 {
123 $this->config = array_merge($this->config, $config);
124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Jones6d743e22010-03-02 13:22:03 -0600126 $this->is_loaded[] = $file_path;
127 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Jones6d743e22010-03-02 13:22:03 -0600129 $loaded = TRUE;
130 log_message('debug', 'Config file loaded: '.$file_path);
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Jones6d743e22010-03-02 13:22:03 -0600133 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
135 if ($fail_gracefully === TRUE)
136 {
137 return FALSE;
138 }
139 show_error('The configuration file '.$file.EXT.' does not exist.');
140 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 return TRUE;
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // --------------------------------------------------------------------
146
147 /**
148 * Fetch a config file item
149 *
150 *
151 * @access public
152 * @param string the config item name
153 * @param string the index name
154 * @param bool
155 * @return string
156 */
157 function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200158 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200160 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 if ( ! isset($this->config[$item]))
162 {
163 return FALSE;
164 }
165
166 $pref = $this->config[$item];
167 }
168 else
169 {
170 if ( ! isset($this->config[$index]))
171 {
172 return FALSE;
173 }
174
175 if ( ! isset($this->config[$index][$item]))
176 {
177 return FALSE;
178 }
179
180 $pref = $this->config[$index][$item];
181 }
182
183 return $pref;
184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
186 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000187
188 /**
189 * Fetch a config file item - adds slash after item
190 *
191 * The second parameter allows a slash to be added to the end of
192 * the item, in the case of a path.
193 *
194 * @access public
195 * @param string the config item name
196 * @param bool
197 * @return string
198 */
199 function slash_item($item)
200 {
201 if ( ! isset($this->config[$item]))
202 {
203 return FALSE;
204 }
205
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000206 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 // --------------------------------------------------------------------
210
211 /**
212 * Site URL
213 *
214 * @access public
215 * @param string the URI string
216 * @return string
217 */
218 function site_url($uri = '')
219 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 if ($uri == '')
221 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000222 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600223 }
224
225 if ($this->item('enable_query_strings') == FALSE)
226 {
227 if (is_array($uri))
228 {
229 $uri = implode('/', $uri);
230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Jones6d743e22010-03-02 13:22:03 -0600232 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
Barry Mienydd671972010-10-04 16:33:58 +0200233 return $this->slash_item('base_url').$this->slash_item('index_page').trim($uri, '/').$suffix;
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235 else
236 {
Derek Jones6d743e22010-03-02 13:22:03 -0600237 if (is_array($uri))
238 {
239 $i = 0;
240 $str = '';
241 foreach ($uri as $key => $val)
242 {
243 $prefix = ($i == 0) ? '' : '&';
244 $str .= $prefix.$key.'='.$val;
245 $i++;
246 }
247
248 $uri = $str;
249 }
250
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000251 return $this->slash_item('base_url').$this->item('index_page').'?'.$uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 // --------------------------------------------------------------------
256
257 /**
258 * System URL
259 *
260 * @access public
261 * @return string
262 */
263 function system_url()
264 {
265 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
266 return $this->slash_item('base_url').end($x).'/';
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // --------------------------------------------------------------------
270
271 /**
272 * Set a config file item
273 *
274 * @access public
275 * @param string the config item key
276 * @param string the config item value
277 * @return void
278 */
279 function set_item($item, $value)
280 {
281 $this->config[$item] = $value;
282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Jones6d743e22010-03-02 13:22:03 -0600284 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000285
Derek Jones6d743e22010-03-02 13:22:03 -0600286 /**
287 * Assign to Config
288 *
289 * This function is called by the front controller (CodeIgniter.php)
290 * after the Config class is instantiated. It permits config items
291 * to be assigned or overriden by variables contained in the index.php file
292 *
293 * @access private
294 * @param array
295 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200296 */
Derek Jones6d743e22010-03-02 13:22:03 -0600297 function _assign_to_config($items = array())
298 {
299 if (is_array($items))
300 {
301 foreach ($items as $key => $val)
302 {
303 $this->set_item($key, $val);
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305 }
Derek Jones6d743e22010-03-02 13:22:03 -0600306 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000307}
308
309// END CI_Config class
310
311/* End of file Config.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600312/* Location: ./system/core/Config.php */