blob: 152e9af68fff8f5a7f1baa506eae5ecd7b622b70 [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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Andrey Andreev5232ba02012-10-27 15:25:05 +030029 * Config Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
31 * This class contains functions that enable config files to be managed
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/config.html
38 */
39class CI_Config {
40
David Behler2d2c9c62011-08-14 20:03:08 +020041 /**
42 * List of all loaded config values
43 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030044 * @var array
David Behler2d2c9c62011-08-14 20:03:08 +020045 */
Eric Robertsaa141a52012-07-04 02:13:56 -050046 public $config = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030047
David Behler2d2c9c62011-08-14 20:03:08 +020048 /**
49 * List of all loaded config files
50 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030051 * @var array
David Behler2d2c9c62011-08-14 20:03:08 +020052 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040053 public $is_loaded = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030054
David Behler2d2c9c62011-08-14 20:03:08 +020055 /**
Andrey Andreevccabcfd2012-01-07 19:30:47 +020056 * List of paths to search when trying to load a config file.
David Behler2d2c9c62011-08-14 20:03:08 +020057 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030058 * @used-by CI_Loader Must be public
59 * @var array
David Behler2d2c9c62011-08-14 20:03:08 +020060 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040061 public $_config_paths = array(APPPATH);
Derek Allard2067d1a2008-11-13 22:59:24 +000062
63 /**
Andrey Andreev5232ba02012-10-27 15:25:05 +030064 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000065 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030066 * Sets the $config data from the primary config.php file as a class variable.
67 *
68 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000069 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +020070 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Barry Mienydd671972010-10-04 16:33:58 +020072 $this->config =& get_config();
Andrey Andreevd52b2422012-01-07 21:28:32 +020073 log_message('debug', 'Config Class Initialized');
Phil Sturgeon4df8b222010-12-15 14:23:14 +000074
75 // Set the base_url automatically if none was provided
Taufan Aditya8749bc72012-03-11 05:43:45 +070076 if (empty($this->config['base_url']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000077 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -050078 if (isset($_SERVER['HTTP_HOST']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000079 {
Andrey Andreev3fb02672012-10-22 16:48:01 +030080 $base_url = is_https() ? 'https' : 'http';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030081 $base_url .= '://'.$_SERVER['HTTP_HOST']
82 .str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
Phil Sturgeon4df8b222010-12-15 14:23:14 +000083 }
Phil Sturgeon4df8b222010-12-15 14:23:14 +000084 else
85 {
86 $base_url = 'http://localhost/';
87 }
88
89 $this->set_item('base_url', $base_url);
90 }
Derek Allard2067d1a2008-11-13 22:59:24 +000091 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // --------------------------------------------------------------------
94
95 /**
96 * Load Config File
97 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030098 * @param string $file Configuration file name
99 * @param bool $use_sections Whether configuration values should be loaded into their own section
100 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
101 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Barry Mienydd671972010-10-04 16:33:58 +0200102 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200103 public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100105 $file = ($file === '') ? 'config' : str_replace('.php', '', $file);
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200106 $found = $loaded = FALSE;
Andrey Andreev9438e262012-10-05 13:16:27 +0300107
Eric Robertsd6ab7a22012-07-04 01:56:04 -0500108 $check_locations = defined('ENVIRONMENT')
109 ? array(ENVIRONMENT.'/'.$file, $file)
110 : array($file);
Barry Mienydd671972010-10-04 16:33:58 +0200111
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500112 foreach ($this->_config_paths as $path)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100113 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100114 foreach ($check_locations as $location)
Derek Jones6d743e22010-03-02 13:22:03 -0600115 {
Andrey Andreevd52b2422012-01-07 21:28:32 +0200116 $file_path = $path.'config/'.$location.'.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100117
118 if (in_array($file_path, $this->is_loaded, TRUE))
119 {
120 $loaded = TRUE;
121 continue 2;
122 }
123
124 if (file_exists($file_path))
125 {
126 $found = TRUE;
127 break;
128 }
129 }
130
131 if ($found === FALSE)
132 {
Derek Jones6d743e22010-03-02 13:22:03 -0600133 continue;
134 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000135
Derek Jones6d743e22010-03-02 13:22:03 -0600136 include($file_path);
137
138 if ( ! isset($config) OR ! is_array($config))
139 {
140 if ($fail_gracefully === TRUE)
141 {
142 return FALSE;
143 }
144 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Jones6d743e22010-03-02 13:22:03 -0600147 if ($use_sections === TRUE)
148 {
149 if (isset($this->config[$file]))
150 {
151 $this->config[$file] = array_merge($this->config[$file], $config);
152 }
153 else
154 {
155 $this->config[$file] = $config;
156 }
157 }
158 else
159 {
160 $this->config = array_merge($this->config, $config);
161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Jones6d743e22010-03-02 13:22:03 -0600163 $this->is_loaded[] = $file_path;
164 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Jones6d743e22010-03-02 13:22:03 -0600166 $loaded = TRUE;
167 log_message('debug', 'Config file loaded: '.$file_path);
katzgraud127e612011-04-22 10:59:25 -0400168 break;
Derek Jones6d743e22010-03-02 13:22:03 -0600169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Jones6d743e22010-03-02 13:22:03 -0600171 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
173 if ($fail_gracefully === TRUE)
174 {
175 return FALSE;
176 }
Eric Robertsaa141a52012-07-04 02:13:56 -0500177 show_error('The configuration file '.$file.'.php does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 return TRUE;
181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // --------------------------------------------------------------------
184
185 /**
186 * Fetch a config file item
187 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300188 * @param string $item Config item name
189 * @param string $index Index name
190 * @return string|bool The configuration item or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200192 public function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200193 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300194 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200195 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200196 return isset($this->config[$item]) ? $this->config[$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200199 return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
202 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000203
204 /**
Andrey Andreev5232ba02012-10-27 15:25:05 +0300205 * Fetch a config file item with slash appended (if not empty)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300207 * @param string $item Config item name
208 * @return string|bool The configuration item or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200210 public function slash_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 if ( ! isset($this->config[$item]))
213 {
214 return FALSE;
215 }
Alex Bilbieed944a32012-06-02 11:07:47 +0100216 elseif (trim($this->config[$item]) === '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800217 {
218 return '';
219 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000220
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000221 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // --------------------------------------------------------------------
225
226 /**
227 * Site URL
Andrey Andreev5232ba02012-10-27 15:25:05 +0300228 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800229 * Returns base_url . index_page [. uri_string]
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300231 * @uses CI_Config::_uri_string()
232 *
233 * @param string|string[] $uri URI string or an array of segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * @return string
235 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200236 public function site_url($uri = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev1764dd72012-06-16 18:48:19 +0300238 if (empty($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000240 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600241 }
242
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300243 $uri = $this->_uri_string($uri);
244
Alex Bilbieed944a32012-06-02 11:07:47 +0100245 if ($this->item('enable_query_strings') === FALSE)
Derek Jones6d743e22010-03-02 13:22:03 -0600246 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100247 $suffix = ($this->item('url_suffix') === FALSE) ? '' : $this->item('url_suffix');
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300248
249 if ($suffix !== '' && ($offset = strpos($uri, '?')) !== FALSE)
250 {
251 $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
252 }
253 else
254 {
255 $uri .= $suffix;
256 }
257
258 return $this->slash_item('base_url').$this->slash_item('index_page').$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800259 }
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300260 elseif (strpos($uri, '?') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800261 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300262 $uri = '?'.$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800263 }
Andrey Andreev1764dd72012-06-16 18:48:19 +0300264
265 return $this->slash_item('base_url').$this->item('index_page').$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800266 }
David Behler2d2c9c62011-08-14 20:03:08 +0200267
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800268 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200269
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800270 /**
271 * Base URL
Andrey Andreev5232ba02012-10-27 15:25:05 +0300272 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800273 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200274 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300275 * @uses CI_Config::_uri_string()
276 *
277 * @param string|string[] $uri URI string or an array of segments
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300278 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800279 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200280 public function base_url($uri = '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800281 {
Eric Robertsaa141a52012-07-04 02:13:56 -0500282 return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800283 }
David Behler2d2c9c62011-08-14 20:03:08 +0200284
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800285 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200286
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800287 /**
Andrey Andreev5232ba02012-10-27 15:25:05 +0300288 * Build URI string
David Behler2d2c9c62011-08-14 20:03:08 +0200289 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300290 * @used-by CI_Config::site_url()
291 * @used-by CI_Config::base_url()
292 *
293 * @param string|string[] $uri URI string or an array of segments
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300294 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800295 */
296 protected function _uri_string($uri)
297 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100298 if ($this->item('enable_query_strings') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800299 {
Derek Jones6d743e22010-03-02 13:22:03 -0600300 if (is_array($uri))
301 {
302 $uri = implode('/', $uri);
303 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200304 return trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200306 elseif (is_array($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Andrey Andreev1764dd72012-06-16 18:48:19 +0300308 return http_build_query($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200310
Greg Aker03abee32011-12-25 00:31:29 -0600311 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 /**
317 * System URL
318 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * @return string
320 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200321 public function system_url()
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200323 $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 return $this->slash_item('base_url').end($x).'/';
325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // --------------------------------------------------------------------
328
329 /**
330 * Set a config file item
331 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300332 * @param string $item Config item key
333 * @param string $value Config item value
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * @return void
335 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200336 public function set_item($item, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $this->config[$item] = $value;
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341}
342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343/* End of file Config.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400344/* Location: ./system/core/Config.php */