blob: abd2767d51926f65c550cf7ca1fa782ae1ab40c3 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Config Class
32 *
33 * This class contains functions that enable config files to be managed
34 *
35 * @package CodeIgniter
36 * @subpackage Libraries
37 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/libraries/config.html
40 */
41class CI_Config {
42
David Behler2d2c9c62011-08-14 20:03:08 +020043 /**
44 * List of all loaded config values
45 *
46 * @var array
47 */
Derek Allard2067d1a2008-11-13 22:59:24 +000048 var $config = array();
David Behler2d2c9c62011-08-14 20:03:08 +020049 /**
50 * List of all loaded config files
51 *
52 * @var array
53 */
Derek Allard2067d1a2008-11-13 22:59:24 +000054 var $is_loaded = array();
David Behler2d2c9c62011-08-14 20:03:08 +020055 /**
56 * List of paths to search when trying to load a config file
57 *
58 * @var array
59 */
Derek Jones6d743e22010-03-02 13:22:03 -060060 var $_config_paths = array(APPPATH);
Derek Allard2067d1a2008-11-13 22:59:24 +000061
62 /**
63 * Constructor
64 *
65 * Sets the $config data from the primary config.php file as a class variable
66 *
Derek Jones4b9c6292011-07-01 17:40:48 -050067 * @access public
68 * @param string the config file name
69 * @param boolean if configuration values should be loaded into their own section
70 * @param boolean true if errors should just return false, false if an error message should be displayed
71 * @return boolean if the file was successfully loaded or not
Derek Allard2067d1a2008-11-13 22:59:24 +000072 */
Greg Akera9263282010-11-10 15:26:43 -060073 function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000074 {
Barry Mienydd671972010-10-04 16:33:58 +020075 $this->config =& get_config();
Derek Allard2067d1a2008-11-13 22:59:24 +000076 log_message('debug', "Config Class Initialized");
Phil Sturgeon4df8b222010-12-15 14:23:14 +000077
78 // Set the base_url automatically if none was provided
79 if ($this->config['base_url'] == '')
80 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -050081 if (isset($_SERVER['HTTP_HOST']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000082 {
Phil Sturgeonfb552382010-12-15 14:29:21 +000083 $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
Phil Sturgeon4df8b222010-12-15 14:23:14 +000084 $base_url .= '://'. $_SERVER['HTTP_HOST'];
Eric Barnes32dbac22011-04-26 22:51:32 -040085 $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
Phil Sturgeon4df8b222010-12-15 14:23:14 +000086 }
87
88 else
89 {
90 $base_url = 'http://localhost/';
91 }
92
93 $this->set_item('base_url', $base_url);
94 }
Derek Allard2067d1a2008-11-13 22:59:24 +000095 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 // --------------------------------------------------------------------
98
99 /**
100 * Load Config File
101 *
102 * @access public
103 * @param string the config file name
Derek Jones4b9c6292011-07-01 17:40:48 -0500104 * @param boolean if configuration values should be loaded into their own section
105 * @param boolean true if errors should just return false, false if an error message should be displayed
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 * @return boolean if the file was loaded correctly
Barry Mienydd671972010-10-04 16:33:58 +0200107 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
109 {
Greg Aker3a746652011-04-19 10:59:47 -0500110 $file = ($file == '') ? 'config' : str_replace('.php', '', $file);
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100111 $found = FALSE;
Derek Jones6d743e22010-03-02 13:22:03 -0600112 $loaded = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200113
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500114 foreach ($this->_config_paths as $path)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100115 {
116 $check_locations = defined('ENVIRONMENT')
117 ? array(ENVIRONMENT.'/'.$file, $file)
118 : array($file);
Barry Mienydd671972010-10-04 16:33:58 +0200119
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100120 foreach ($check_locations as $location)
Derek Jones6d743e22010-03-02 13:22:03 -0600121 {
Greg Aker3a746652011-04-19 10:59:47 -0500122 $file_path = $path.'config/'.$location.'.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100123
124 if (in_array($file_path, $this->is_loaded, TRUE))
125 {
126 $loaded = TRUE;
127 continue 2;
128 }
129
130 if (file_exists($file_path))
131 {
132 $found = TRUE;
133 break;
134 }
135 }
136
137 if ($found === FALSE)
138 {
Derek Jones6d743e22010-03-02 13:22:03 -0600139 continue;
140 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000141
Derek Jones6d743e22010-03-02 13:22:03 -0600142 include($file_path);
143
144 if ( ! isset($config) OR ! is_array($config))
145 {
146 if ($fail_gracefully === TRUE)
147 {
148 return FALSE;
149 }
150 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Jones6d743e22010-03-02 13:22:03 -0600153 if ($use_sections === TRUE)
154 {
155 if (isset($this->config[$file]))
156 {
157 $this->config[$file] = array_merge($this->config[$file], $config);
158 }
159 else
160 {
161 $this->config[$file] = $config;
162 }
163 }
164 else
165 {
166 $this->config = array_merge($this->config, $config);
167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Jones6d743e22010-03-02 13:22:03 -0600169 $this->is_loaded[] = $file_path;
170 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Jones6d743e22010-03-02 13:22:03 -0600172 $loaded = TRUE;
173 log_message('debug', 'Config file loaded: '.$file_path);
katzgraud127e612011-04-22 10:59:25 -0400174 break;
Derek Jones6d743e22010-03-02 13:22:03 -0600175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Jones6d743e22010-03-02 13:22:03 -0600177 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
179 if ($fail_gracefully === TRUE)
180 {
181 return FALSE;
182 }
Greg Aker3a746652011-04-19 10:59:47 -0500183 show_error('The configuration file '.$file.'.php'.' does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 return TRUE;
187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 // --------------------------------------------------------------------
190
191 /**
192 * Fetch a config file item
193 *
194 *
195 * @access public
196 * @param string the config item name
197 * @param string the index name
198 * @param bool
199 * @return string
200 */
201 function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200202 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200204 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 if ( ! isset($this->config[$item]))
206 {
207 return FALSE;
208 }
209
210 $pref = $this->config[$item];
211 }
212 else
213 {
214 if ( ! isset($this->config[$index]))
215 {
216 return FALSE;
217 }
218
219 if ( ! isset($this->config[$index][$item]))
220 {
221 return FALSE;
222 }
223
224 $pref = $this->config[$index][$item];
225 }
226
227 return $pref;
228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
230 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000231
232 /**
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800233 * Fetch a config file item - adds slash after item (if item is not empty)
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 *
235 * @access public
236 * @param string the config item name
237 * @param bool
238 * @return string
239 */
240 function slash_item($item)
241 {
242 if ( ! isset($this->config[$item]))
243 {
244 return FALSE;
245 }
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800246 if( trim($this->config[$item]) == '')
247 {
248 return '';
249 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000250
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000251 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // --------------------------------------------------------------------
255
256 /**
257 * Site URL
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800258 * Returns base_url . index_page [. uri_string]
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 *
260 * @access public
261 * @param string the URI string
262 * @return string
263 */
264 function site_url($uri = '')
265 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 if ($uri == '')
267 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000268 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600269 }
270
271 if ($this->item('enable_query_strings') == FALSE)
272 {
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800273 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
274 return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
275 }
276 else
277 {
278 return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
279 }
280 }
David Behler2d2c9c62011-08-14 20:03:08 +0200281
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800282 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200283
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800284 /**
285 * Base URL
286 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200287 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800288 * @access public
289 * @param string $uri
290 * @return string
291 */
292 function base_url($uri = '')
293 {
294 return $this->slash_item('base_url').ltrim($this->_uri_string($uri),'/');
295 }
David Behler2d2c9c62011-08-14 20:03:08 +0200296
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800297 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200298
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800299 /**
300 * Build URI string for use in Config::site_url() and Config::base_url()
David Behler2d2c9c62011-08-14 20:03:08 +0200301 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800302 * @access protected
303 * @param $uri
304 * @return string
305 */
306 protected function _uri_string($uri)
307 {
308 if ($this->item('enable_query_strings') == FALSE)
309 {
Derek Jones6d743e22010-03-02 13:22:03 -0600310 if (is_array($uri))
311 {
312 $uri = implode('/', $uri);
313 }
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800314 $uri = trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316 else
317 {
Derek Jones6d743e22010-03-02 13:22:03 -0600318 if (is_array($uri))
319 {
320 $i = 0;
321 $str = '';
322 foreach ($uri as $key => $val)
323 {
324 $prefix = ($i == 0) ? '' : '&';
325 $str .= $prefix.$key.'='.$val;
326 $i++;
327 }
Derek Jones6d743e22010-03-02 13:22:03 -0600328 $uri = $str;
329 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800331 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 /**
337 * System URL
338 *
339 * @access public
340 * @return string
341 */
342 function system_url()
343 {
344 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
345 return $this->slash_item('base_url').end($x).'/';
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // --------------------------------------------------------------------
349
350 /**
351 * Set a config file item
352 *
353 * @access public
354 * @param string the config item key
355 * @param string the config item value
356 * @return void
357 */
358 function set_item($item, $value)
359 {
360 $this->config[$item] = $value;
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Jones6d743e22010-03-02 13:22:03 -0600363 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000364
Derek Jones6d743e22010-03-02 13:22:03 -0600365 /**
366 * Assign to Config
367 *
368 * This function is called by the front controller (CodeIgniter.php)
Derek Jones4b9c6292011-07-01 17:40:48 -0500369 * after the Config class is instantiated. It permits config items
Derek Jones6d743e22010-03-02 13:22:03 -0600370 * to be assigned or overriden by variables contained in the index.php file
371 *
372 * @access private
373 * @param array
374 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200375 */
Derek Jones6d743e22010-03-02 13:22:03 -0600376 function _assign_to_config($items = array())
377 {
378 if (is_array($items))
379 {
380 foreach ($items as $key => $val)
381 {
382 $this->set_item($key, $val);
383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384 }
Derek Jones6d743e22010-03-02 13:22:03 -0600385 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000386}
387
388// END CI_Config class
389
390/* End of file Config.php */
katzgraud127e612011-04-22 10:59:25 -0400391/* Location: ./system/core/Config.php */