blob: 1eab08b8215628eab8034408e1d4c5419b55cfb7 [file] [log] [blame]
Andrey Andreevccabcfd2012-01-07 19:30:47 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevccabcfd2012-01-07 19:30:47 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevccabcfd2012-01-07 19:30:47 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040048 public $config = array();
Timothy Warren40403d22012-04-19 16:38:50 -040049
David Behler2d2c9c62011-08-14 20:03:08 +020050 /**
51 * List of all loaded config files
52 *
53 * @var array
54 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040055 public $is_loaded = array();
Timothy Warren40403d22012-04-19 16:38:50 -040056
David Behler2d2c9c62011-08-14 20:03:08 +020057 /**
Andrey Andreevccabcfd2012-01-07 19:30:47 +020058 * List of paths to search when trying to load a config file.
59 * This must be public as it's used by the Loader class.
David Behler2d2c9c62011-08-14 20:03:08 +020060 *
61 * @var array
62 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040063 public $_config_paths = array(APPPATH);
Derek Allard2067d1a2008-11-13 22:59:24 +000064
65 /**
66 * Constructor
67 *
68 * Sets the $config data from the primary config.php file as a class variable
69 *
Derek Jones4b9c6292011-07-01 17:40:48 -050070 * @param string the config file name
71 * @param boolean if configuration values should be loaded into their own section
72 * @param boolean true if errors should just return false, false if an error message should be displayed
73 * @return boolean if the file was successfully loaded or not
Derek Allard2067d1a2008-11-13 22:59:24 +000074 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +020075 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000076 {
Barry Mienydd671972010-10-04 16:33:58 +020077 $this->config =& get_config();
Andrey Andreevd52b2422012-01-07 21:28:32 +020078 log_message('debug', 'Config Class Initialized');
Phil Sturgeon4df8b222010-12-15 14:23:14 +000079
80 // Set the base_url automatically if none was provided
Taufan Aditya8749bc72012-03-11 05:43:45 +070081 if (empty($this->config['base_url']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000082 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -050083 if (isset($_SERVER['HTTP_HOST']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000084 {
Ross Duggan0a9325c2012-01-31 15:47:52 +000085 $base_url = ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
Andrey Andreevccabcfd2012-01-07 19:30:47 +020086 $base_url .= '://'. $_SERVER['HTTP_HOST']
87 . str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
Phil Sturgeon4df8b222010-12-15 14:23:14 +000088 }
Phil Sturgeon4df8b222010-12-15 14:23:14 +000089 else
90 {
91 $base_url = 'http://localhost/';
92 }
93
94 $this->set_item('base_url', $base_url);
95 }
Derek Allard2067d1a2008-11-13 22:59:24 +000096 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 // --------------------------------------------------------------------
99
100 /**
101 * Load Config File
102 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 * @param string the config file name
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200104 * @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 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200108 public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Greg Aker3a746652011-04-19 10:59:47 -0500110 $file = ($file == '') ? 'config' : str_replace('.php', '', $file);
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200111 $found = $loaded = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200112
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500113 foreach ($this->_config_paths as $path)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100114 {
115 $check_locations = defined('ENVIRONMENT')
Andrey Andreevd52b2422012-01-07 21:28:32 +0200116 ? array(ENVIRONMENT.'/'.$file, $file)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100117 : array($file);
Barry Mienydd671972010-10-04 16:33:58 +0200118
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100119 foreach ($check_locations as $location)
Derek Jones6d743e22010-03-02 13:22:03 -0600120 {
Andrey Andreevd52b2422012-01-07 21:28:32 +0200121 $file_path = $path.'config/'.$location.'.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100122
123 if (in_array($file_path, $this->is_loaded, TRUE))
124 {
125 $loaded = TRUE;
126 continue 2;
127 }
128
129 if (file_exists($file_path))
130 {
131 $found = TRUE;
132 break;
133 }
134 }
135
136 if ($found === FALSE)
137 {
Derek Jones6d743e22010-03-02 13:22:03 -0600138 continue;
139 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000140
Derek Jones6d743e22010-03-02 13:22:03 -0600141 include($file_path);
142
143 if ( ! isset($config) OR ! is_array($config))
144 {
145 if ($fail_gracefully === TRUE)
146 {
147 return FALSE;
148 }
149 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jones6d743e22010-03-02 13:22:03 -0600152 if ($use_sections === TRUE)
153 {
154 if (isset($this->config[$file]))
155 {
156 $this->config[$file] = array_merge($this->config[$file], $config);
157 }
158 else
159 {
160 $this->config[$file] = $config;
161 }
162 }
163 else
164 {
165 $this->config = array_merge($this->config, $config);
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Jones6d743e22010-03-02 13:22:03 -0600168 $this->is_loaded[] = $file_path;
169 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Jones6d743e22010-03-02 13:22:03 -0600171 $loaded = TRUE;
172 log_message('debug', 'Config file loaded: '.$file_path);
katzgraud127e612011-04-22 10:59:25 -0400173 break;
Derek Jones6d743e22010-03-02 13:22:03 -0600174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Jones6d743e22010-03-02 13:22:03 -0600176 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
178 if ($fail_gracefully === TRUE)
179 {
180 return FALSE;
181 }
Greg Aker3a746652011-04-19 10:59:47 -0500182 show_error('The configuration file '.$file.'.php'.' does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 return TRUE;
186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 // --------------------------------------------------------------------
189
190 /**
191 * Fetch a config file item
192 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @param string the config item name
194 * @param string the index name
195 * @param bool
196 * @return string
197 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200198 public function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200199 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200201 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200202 return isset($this->config[$item]) ? $this->config[$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200205 return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
208 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000209
210 /**
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800211 * Fetch a config file item - adds slash after item (if item is not empty)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * @param string the config item name
214 * @param bool
215 * @return string
216 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200217 public function slash_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
219 if ( ! isset($this->config[$item]))
220 {
221 return FALSE;
222 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200223 elseif (trim($this->config[$item]) == '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800224 {
225 return '';
226 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000227
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000228 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 // --------------------------------------------------------------------
232
233 /**
234 * Site URL
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800235 * Returns base_url . index_page [. uri_string]
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 * @param string the URI string
238 * @return string
239 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200240 public function site_url($uri = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 if ($uri == '')
243 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000244 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600245 }
246
247 if ($this->item('enable_query_strings') == FALSE)
248 {
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800249 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
250 return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
251 }
252 else
253 {
254 return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
255 }
256 }
David Behler2d2c9c62011-08-14 20:03:08 +0200257
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800258 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200259
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800260 /**
261 * Base URL
262 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200263 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800264 * @param string $uri
265 * @return string
266 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200267 public function base_url($uri = '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800268 {
269 return $this->slash_item('base_url').ltrim($this->_uri_string($uri),'/');
270 }
David Behler2d2c9c62011-08-14 20:03:08 +0200271
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800272 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200273
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800274 /**
275 * Build URI string for use in Config::site_url() and Config::base_url()
David Behler2d2c9c62011-08-14 20:03:08 +0200276 *
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200277 * @param mixed $uri
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800278 * @return string
279 */
280 protected function _uri_string($uri)
281 {
282 if ($this->item('enable_query_strings') == FALSE)
283 {
Derek Jones6d743e22010-03-02 13:22:03 -0600284 if (is_array($uri))
285 {
286 $uri = implode('/', $uri);
287 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200288 return trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200290 elseif (is_array($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200292 $i = 0;
293 $str = '';
294 foreach ($uri as $key => $val)
Derek Jones6d743e22010-03-02 13:22:03 -0600295 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200296 $prefix = ($i === 0) ? '' : '&';
297 $str .= $prefix.$key.'='.$val;
298 $i++;
Derek Jones6d743e22010-03-02 13:22:03 -0600299 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200300 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200302
Greg Aker03abee32011-12-25 00:31:29 -0600303 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 /**
309 * System URL
310 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 * @return string
312 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200313 public function system_url()
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200315 $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 return $this->slash_item('base_url').end($x).'/';
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // --------------------------------------------------------------------
320
321 /**
322 * Set a config file item
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string the config item key
325 * @param string the config item value
326 * @return void
327 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200328 public function set_item($item, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 $this->config[$item] = $value;
331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Jones6d743e22010-03-02 13:22:03 -0600333 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000334
Derek Jones6d743e22010-03-02 13:22:03 -0600335 /**
336 * Assign to Config
337 *
338 * This function is called by the front controller (CodeIgniter.php)
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200339 * after the Config class is instantiated. It permits config items
Derek Jones6d743e22010-03-02 13:22:03 -0600340 * to be assigned or overriden by variables contained in the index.php file
341 *
Derek Jones6d743e22010-03-02 13:22:03 -0600342 * @param array
343 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200344 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200345 public function _assign_to_config($items = array())
Derek Jones6d743e22010-03-02 13:22:03 -0600346 {
347 if (is_array($items))
348 {
349 foreach ($items as $key => $val)
350 {
351 $this->set_item($key, $val);
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353 }
Derek Jones6d743e22010-03-02 13:22:03 -0600354 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000355}
356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357/* End of file Config.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400358/* Location: ./system/core/Config.php */