blob: 8e4f998efe070a530c8da880f869b53853a89309 [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 */
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 *
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;
Andrey Andreev9438e262012-10-05 13:16:27 +0300105
Eric Robertsd6ab7a22012-07-04 01:56:04 -0500106 $check_locations = defined('ENVIRONMENT')
107 ? array(ENVIRONMENT.'/'.$file, $file)
108 : array($file);
Barry Mienydd671972010-10-04 16:33:58 +0200109
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500110 foreach ($this->_config_paths as $path)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100111 {
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 }
Eric Robertsaa141a52012-07-04 02:13:56 -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
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300238 $uri = $this->_uri_string($uri);
239
Alex Bilbieed944a32012-06-02 11:07:47 +0100240 if ($this->item('enable_query_strings') === FALSE)
Derek Jones6d743e22010-03-02 13:22:03 -0600241 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100242 $suffix = ($this->item('url_suffix') === FALSE) ? '' : $this->item('url_suffix');
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300243
244 if ($suffix !== '' && ($offset = strpos($uri, '?')) !== FALSE)
245 {
246 $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
247 }
248 else
249 {
250 $uri .= $suffix;
251 }
252
253 return $this->slash_item('base_url').$this->slash_item('index_page').$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800254 }
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300255 elseif (strpos($uri, '?') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800256 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300257 $uri = '?'.$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800258 }
Andrey Andreev1764dd72012-06-16 18:48:19 +0300259
260 return $this->slash_item('base_url').$this->item('index_page').$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800261 }
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 * Base URL
267 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200268 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300269 * @param string $uri
270 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800271 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200272 public function base_url($uri = '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800273 {
Eric Robertsaa141a52012-07-04 02:13:56 -0500274 return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800275 }
David Behler2d2c9c62011-08-14 20:03:08 +0200276
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800277 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200278
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800279 /**
280 * Build URI string for use in Config::site_url() and Config::base_url()
David Behler2d2c9c62011-08-14 20:03:08 +0200281 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300282 * @param mixed $uri
283 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800284 */
285 protected function _uri_string($uri)
286 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100287 if ($this->item('enable_query_strings') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800288 {
Derek Jones6d743e22010-03-02 13:22:03 -0600289 if (is_array($uri))
290 {
291 $uri = implode('/', $uri);
292 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200293 return trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200295 elseif (is_array($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
Andrey Andreev1764dd72012-06-16 18:48:19 +0300297 return http_build_query($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200299
Greg Aker03abee32011-12-25 00:31:29 -0600300 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 /**
306 * System URL
307 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @return string
309 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200310 public function system_url()
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200312 $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 return $this->slash_item('base_url').end($x).'/';
314 }
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 // --------------------------------------------------------------------
317
318 /**
319 * Set a config file item
320 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 * @param string the config item key
322 * @param string the config item value
323 * @return void
324 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200325 public function set_item($item, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
327 $this->config[$item] = $value;
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Jones6d743e22010-03-02 13:22:03 -0600330 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000331
Derek Jones6d743e22010-03-02 13:22:03 -0600332 /**
333 * Assign to Config
334 *
335 * This function is called by the front controller (CodeIgniter.php)
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200336 * after the Config class is instantiated. It permits config items
Derek Jones6d743e22010-03-02 13:22:03 -0600337 * to be assigned or overriden by variables contained in the index.php file
338 *
Derek Jones6d743e22010-03-02 13:22:03 -0600339 * @param array
340 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200341 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200342 public function _assign_to_config($items = array())
Derek Jones6d743e22010-03-02 13:22:03 -0600343 {
344 if (is_array($items))
345 {
346 foreach ($items as $key => $val)
347 {
348 $this->set_item($key, $val);
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350 }
Derek Jones6d743e22010-03-02 13:22:03 -0600351 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353}
354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355/* End of file Config.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400356/* Location: ./system/core/Config.php */