blob: 3de1bcb967dee9bced6279262c27d00282f31379 [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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 * @param string the URI string
229 * @return string
230 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200231 public function site_url($uri = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100233 if ($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 }
243 else
244 {
245 return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
246 }
247 }
David Behler2d2c9c62011-08-14 20:03:08 +0200248
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 /**
252 * Base URL
253 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200254 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300255 * @param string $uri
256 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800257 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200258 public function base_url($uri = '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800259 {
260 return $this->slash_item('base_url').ltrim($this->_uri_string($uri),'/');
261 }
David Behler2d2c9c62011-08-14 20:03:08 +0200262
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800263 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200264
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800265 /**
266 * Build URI string for use in Config::site_url() and Config::base_url()
David Behler2d2c9c62011-08-14 20:03:08 +0200267 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300268 * @param mixed $uri
269 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800270 */
271 protected function _uri_string($uri)
272 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100273 if ($this->item('enable_query_strings') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800274 {
Derek Jones6d743e22010-03-02 13:22:03 -0600275 if (is_array($uri))
276 {
277 $uri = implode('/', $uri);
278 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200279 return trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200281 elseif (is_array($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200283 $i = 0;
284 $str = '';
285 foreach ($uri as $key => $val)
Derek Jones6d743e22010-03-02 13:22:03 -0600286 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200287 $prefix = ($i === 0) ? '' : '&';
288 $str .= $prefix.$key.'='.$val;
289 $i++;
Derek Jones6d743e22010-03-02 13:22:03 -0600290 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200291 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200293
Greg Aker03abee32011-12-25 00:31:29 -0600294 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 /**
300 * System URL
301 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 * @return string
303 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200304 public function system_url()
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200306 $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 return $this->slash_item('base_url').end($x).'/';
308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // --------------------------------------------------------------------
311
312 /**
313 * Set a config file item
314 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * @param string the config item key
316 * @param string the config item value
317 * @return void
318 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200319 public function set_item($item, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 $this->config[$item] = $value;
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Jones6d743e22010-03-02 13:22:03 -0600324 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000325
Derek Jones6d743e22010-03-02 13:22:03 -0600326 /**
327 * Assign to Config
328 *
329 * This function is called by the front controller (CodeIgniter.php)
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200330 * after the Config class is instantiated. It permits config items
Derek Jones6d743e22010-03-02 13:22:03 -0600331 * to be assigned or overriden by variables contained in the index.php file
332 *
Derek Jones6d743e22010-03-02 13:22:03 -0600333 * @param array
334 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200335 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200336 public function _assign_to_config($items = array())
Derek Jones6d743e22010-03-02 13:22:03 -0600337 {
338 if (is_array($items))
339 {
340 foreach ($items as $key => $val)
341 {
342 $this->set_item($key, $val);
343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344 }
Derek Jones6d743e22010-03-02 13:22:03 -0600345 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347}
348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349/* End of file Config.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400350/* Location: ./system/core/Config.php */