blob: 65638271626561d3a150338938e5559db47ca78f [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/**
29 * CodeIgniter Config Class
30 *
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 *
44 * @var array
45 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040046 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 *
51 * @var array
52 */
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.
57 * This must be public as it's used by the Loader class.
David Behler2d2c9c62011-08-14 20:03:08 +020058 *
59 * @var array
60 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040061 public $_config_paths = array(APPPATH);
Derek Allard2067d1a2008-11-13 22:59:24 +000062
63 /**
64 * Constructor
65 *
66 * Sets the $config data from the primary config.php file as a class variable
Derek Allard2067d1a2008-11-13 22:59:24 +000067 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +020068 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
Barry Mienydd671972010-10-04 16:33:58 +020070 $this->config =& get_config();
Andrey Andreevd52b2422012-01-07 21:28:32 +020071 log_message('debug', 'Config Class Initialized');
Phil Sturgeon4df8b222010-12-15 14:23:14 +000072
73 // Set the base_url automatically if none was provided
Taufan Aditya8749bc72012-03-11 05:43:45 +070074 if (empty($this->config['base_url']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000075 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -050076 if (isset($_SERVER['HTTP_HOST']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000077 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +030078 $base_url = ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') ? 'https' : 'http';
79 $base_url .= '://'.$_SERVER['HTTP_HOST']
80 .str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
Phil Sturgeon4df8b222010-12-15 14:23:14 +000081 }
Phil Sturgeon4df8b222010-12-15 14:23:14 +000082 else
83 {
84 $base_url = 'http://localhost/';
85 }
86
87 $this->set_item('base_url', $base_url);
88 }
Derek Allard2067d1a2008-11-13 22:59:24 +000089 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 // --------------------------------------------------------------------
92
93 /**
94 * Load Config File
95 *
Derek Allard2067d1a2008-11-13 22:59:24 +000096 * @param string the config file name
Andrey Andreev92ebfb62012-05-17 12:49:24 +030097 * @param bool if configuration values should be loaded into their own section
98 * @param bool true if errors should just return false, false if an error message should be displayed
99 * @return bool if the file was loaded correctly
Barry Mienydd671972010-10-04 16:33:58 +0200100 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200101 public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100103 $file = ($file === '') ? 'config' : str_replace('.php', '', $file);
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200104 $found = $loaded = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200105
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500106 foreach ($this->_config_paths as $path)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100107 {
108 $check_locations = defined('ENVIRONMENT')
Andrey Andreevd52b2422012-01-07 21:28:32 +0200109 ? array(ENVIRONMENT.'/'.$file, $file)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100110 : array($file);
Barry Mienydd671972010-10-04 16:33:58 +0200111
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100112 foreach ($check_locations as $location)
Derek Jones6d743e22010-03-02 13:22:03 -0600113 {
Andrey Andreevd52b2422012-01-07 21:28:32 +0200114 $file_path = $path.'config/'.$location.'.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100115
116 if (in_array($file_path, $this->is_loaded, TRUE))
117 {
118 $loaded = TRUE;
119 continue 2;
120 }
121
122 if (file_exists($file_path))
123 {
124 $found = TRUE;
125 break;
126 }
127 }
128
129 if ($found === FALSE)
130 {
Derek Jones6d743e22010-03-02 13:22:03 -0600131 continue;
132 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000133
Derek Jones6d743e22010-03-02 13:22:03 -0600134 include($file_path);
135
136 if ( ! isset($config) OR ! is_array($config))
137 {
138 if ($fail_gracefully === TRUE)
139 {
140 return FALSE;
141 }
142 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Jones6d743e22010-03-02 13:22:03 -0600145 if ($use_sections === TRUE)
146 {
147 if (isset($this->config[$file]))
148 {
149 $this->config[$file] = array_merge($this->config[$file], $config);
150 }
151 else
152 {
153 $this->config[$file] = $config;
154 }
155 }
156 else
157 {
158 $this->config = array_merge($this->config, $config);
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Jones6d743e22010-03-02 13:22:03 -0600161 $this->is_loaded[] = $file_path;
162 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Jones6d743e22010-03-02 13:22:03 -0600164 $loaded = TRUE;
165 log_message('debug', 'Config file loaded: '.$file_path);
katzgraud127e612011-04-22 10:59:25 -0400166 break;
Derek Jones6d743e22010-03-02 13:22:03 -0600167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Jones6d743e22010-03-02 13:22:03 -0600169 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 if ($fail_gracefully === TRUE)
172 {
173 return FALSE;
174 }
Greg Aker3a746652011-04-19 10:59:47 -0500175 show_error('The configuration file '.$file.'.php'.' does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 return TRUE;
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // --------------------------------------------------------------------
182
183 /**
184 * Fetch a config file item
185 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 * @param string the config item name
187 * @param string the index name
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * @return string
189 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200190 public function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200191 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300192 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200193 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200194 return isset($this->config[$item]) ? $this->config[$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
196
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200197 return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
200 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000201
202 /**
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800203 * Fetch a config file item - adds slash after item (if item is not empty)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 * @param string the config item name
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 * @return string
207 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200208 public function slash_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 if ( ! isset($this->config[$item]))
211 {
212 return FALSE;
213 }
Alex Bilbieed944a32012-06-02 11:07:47 +0100214 elseif (trim($this->config[$item]) === '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800215 {
216 return '';
217 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000218
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000219 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // --------------------------------------------------------------------
223
224 /**
225 * Site URL
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800226 * Returns base_url . index_page [. uri_string]
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 *
Andrey Andreev1764dd72012-06-16 18:48:19 +0300228 * @param mixed the URI string or an array of segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 * @return string
230 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200231 public function site_url($uri = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Andrey Andreev1764dd72012-06-16 18:48:19 +0300233 if (empty($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000235 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600236 }
237
Alex Bilbieed944a32012-06-02 11:07:47 +0100238 if ($this->item('enable_query_strings') === FALSE)
Derek Jones6d743e22010-03-02 13:22:03 -0600239 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100240 $suffix = ($this->item('url_suffix') === FALSE) ? '' : $this->item('url_suffix');
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800241 return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
242 }
Andrey Andreev1764dd72012-06-16 18:48:19 +0300243 elseif (is_array($uri) OR strpos($uri, '?') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800244 {
Andrey Andreev1764dd72012-06-16 18:48:19 +0300245 $uri = '?'.$this->_uri_string($uri);
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800246 }
Andrey Andreev1764dd72012-06-16 18:48:19 +0300247
248 return $this->slash_item('base_url').$this->item('index_page').$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800249 }
David Behler2d2c9c62011-08-14 20:03:08 +0200250
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800251 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200252
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800253 /**
254 * Base URL
255 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200256 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300257 * @param string $uri
258 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800259 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200260 public function base_url($uri = '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800261 {
262 return $this->slash_item('base_url').ltrim($this->_uri_string($uri),'/');
263 }
David Behler2d2c9c62011-08-14 20:03:08 +0200264
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800265 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200266
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800267 /**
268 * Build URI string for use in Config::site_url() and Config::base_url()
David Behler2d2c9c62011-08-14 20:03:08 +0200269 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300270 * @param mixed $uri
271 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800272 */
273 protected function _uri_string($uri)
274 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100275 if ($this->item('enable_query_strings') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800276 {
Derek Jones6d743e22010-03-02 13:22:03 -0600277 if (is_array($uri))
278 {
279 $uri = implode('/', $uri);
280 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200281 return trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200283 elseif (is_array($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Andrey Andreev1764dd72012-06-16 18:48:19 +0300285 return http_build_query($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200287
Greg Aker03abee32011-12-25 00:31:29 -0600288 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 /**
294 * System URL
295 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 * @return string
297 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200298 public function system_url()
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200300 $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 return $this->slash_item('base_url').end($x).'/';
302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // --------------------------------------------------------------------
305
306 /**
307 * Set a config file item
308 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * @param string the config item key
310 * @param string the config item value
311 * @return void
312 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200313 public function set_item($item, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
315 $this->config[$item] = $value;
316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Jones6d743e22010-03-02 13:22:03 -0600318 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000319
Derek Jones6d743e22010-03-02 13:22:03 -0600320 /**
321 * Assign to Config
322 *
323 * This function is called by the front controller (CodeIgniter.php)
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200324 * after the Config class is instantiated. It permits config items
Derek Jones6d743e22010-03-02 13:22:03 -0600325 * to be assigned or overriden by variables contained in the index.php file
326 *
Derek Jones6d743e22010-03-02 13:22:03 -0600327 * @param array
328 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200329 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200330 public function _assign_to_config($items = array())
Derek Jones6d743e22010-03-02 13:22:03 -0600331 {
332 if (is_array($items))
333 {
334 foreach ($items as $key => $val)
335 {
336 $this->set_item($key, $val);
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338 }
Derek Jones6d743e22010-03-02 13:22:03 -0600339 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300340
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 */