blob: 9e6d2cbf2c630a67c6c8e4d4e6f77a09eb3b4ea7 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
Andrey Andreev5232ba02012-10-27 15:25:05 +030030 * Config Class
Derek Allard2067d1a2008-11-13 22:59:24 +000031 *
32 * This class contains functions that enable config files to be managed
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/config.html
39 */
40class CI_Config {
41
David Behler2d2c9c62011-08-14 20:03:08 +020042 /**
43 * List of all loaded config values
44 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030045 * @var array
David Behler2d2c9c62011-08-14 20:03:08 +020046 */
Eric Robertsaa141a52012-07-04 02:13:56 -050047 public $config = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048
David Behler2d2c9c62011-08-14 20:03:08 +020049 /**
50 * List of all loaded config files
51 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030052 * @var array
David Behler2d2c9c62011-08-14 20:03:08 +020053 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040054 public $is_loaded = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030055
David Behler2d2c9c62011-08-14 20:03:08 +020056 /**
Andrey Andreevccabcfd2012-01-07 19:30:47 +020057 * List of paths to search when trying to load a config file.
David Behler2d2c9c62011-08-14 20:03:08 +020058 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030059 * @used-by CI_Loader
Andrey Andreev5232ba02012-10-27 15:25:05 +030060 * @var array
David Behler2d2c9c62011-08-14 20:03:08 +020061 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040062 public $_config_paths = array(APPPATH);
Derek Allard2067d1a2008-11-13 22:59:24 +000063
64 /**
Andrey Andreev5232ba02012-10-27 15:25:05 +030065 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000066 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030067 * Sets the $config data from the primary config.php file as a class variable.
68 *
69 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000070 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +020071 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000072 {
Barry Mienydd671972010-10-04 16:33:58 +020073 $this->config =& get_config();
Andrey Andreevd52b2422012-01-07 21:28:32 +020074 log_message('debug', 'Config Class Initialized');
Phil Sturgeon4df8b222010-12-15 14:23:14 +000075
76 // Set the base_url automatically if none was provided
Taufan Aditya8749bc72012-03-11 05:43:45 +070077 if (empty($this->config['base_url']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000078 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -050079 if (isset($_SERVER['HTTP_HOST']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000080 {
Andrey Andreev3fb02672012-10-22 16:48:01 +030081 $base_url = is_https() ? 'https' : 'http';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030082 $base_url .= '://'.$_SERVER['HTTP_HOST']
83 .str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
Phil Sturgeon4df8b222010-12-15 14:23:14 +000084 }
Phil Sturgeon4df8b222010-12-15 14:23:14 +000085 else
86 {
87 $base_url = 'http://localhost/';
88 }
89
90 $this->set_item('base_url', $base_url);
91 }
Derek Allard2067d1a2008-11-13 22:59:24 +000092 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // --------------------------------------------------------------------
95
96 /**
97 * Load Config File
98 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030099 * @param string $file Configuration file name
100 * @param bool $use_sections Whether configuration values should be loaded into their own section
101 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
102 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Barry Mienydd671972010-10-04 16:33:58 +0200103 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200104 public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100106 $file = ($file === '') ? 'config' : str_replace('.php', '', $file);
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200107 $found = $loaded = FALSE;
Andrey Andreev9438e262012-10-05 13:16:27 +0300108
Eric Robertsd6ab7a22012-07-04 01:56:04 -0500109 $check_locations = defined('ENVIRONMENT')
110 ? array(ENVIRONMENT.'/'.$file, $file)
111 : array($file);
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 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100115 foreach ($check_locations as $location)
Derek Jones6d743e22010-03-02 13:22:03 -0600116 {
Andrey Andreevd52b2422012-01-07 21:28:32 +0200117 $file_path = $path.'config/'.$location.'.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100118
119 if (in_array($file_path, $this->is_loaded, TRUE))
120 {
121 $loaded = TRUE;
122 continue 2;
123 }
124
125 if (file_exists($file_path))
126 {
127 $found = TRUE;
128 break;
129 }
130 }
131
132 if ($found === FALSE)
133 {
Derek Jones6d743e22010-03-02 13:22:03 -0600134 continue;
135 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000136
Derek Jones6d743e22010-03-02 13:22:03 -0600137 include($file_path);
138
139 if ( ! isset($config) OR ! is_array($config))
140 {
141 if ($fail_gracefully === TRUE)
142 {
143 return FALSE;
144 }
145 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Jones6d743e22010-03-02 13:22:03 -0600148 if ($use_sections === TRUE)
149 {
150 if (isset($this->config[$file]))
151 {
152 $this->config[$file] = array_merge($this->config[$file], $config);
153 }
154 else
155 {
156 $this->config[$file] = $config;
157 }
158 }
159 else
160 {
161 $this->config = array_merge($this->config, $config);
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Jones6d743e22010-03-02 13:22:03 -0600164 $this->is_loaded[] = $file_path;
165 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Jones6d743e22010-03-02 13:22:03 -0600167 $loaded = TRUE;
168 log_message('debug', 'Config file loaded: '.$file_path);
katzgraud127e612011-04-22 10:59:25 -0400169 break;
Derek Jones6d743e22010-03-02 13:22:03 -0600170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Jones6d743e22010-03-02 13:22:03 -0600172 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
174 if ($fail_gracefully === TRUE)
175 {
176 return FALSE;
177 }
Eric Robertsaa141a52012-07-04 02:13:56 -0500178 show_error('The configuration file '.$file.'.php does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 return TRUE;
182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 // --------------------------------------------------------------------
185
186 /**
187 * Fetch a config file item
188 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300189 * @param string $item Config item name
190 * @param string $index Index name
191 * @return string|bool The configuration item or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200193 public function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200194 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300195 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200196 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200197 return isset($this->config[$item]) ? $this->config[$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
199
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200200 return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
203 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000204
205 /**
Andrey Andreev5232ba02012-10-27 15:25:05 +0300206 * Fetch a config file item with slash appended (if not empty)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300208 * @param string $item Config item name
209 * @return string|bool The configuration item or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200211 public function slash_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 if ( ! isset($this->config[$item]))
214 {
215 return FALSE;
216 }
Alex Bilbieed944a32012-06-02 11:07:47 +0100217 elseif (trim($this->config[$item]) === '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800218 {
219 return '';
220 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000221
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000222 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // --------------------------------------------------------------------
226
227 /**
228 * Site URL
Andrey Andreev5232ba02012-10-27 15:25:05 +0300229 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800230 * Returns base_url . index_page [. uri_string]
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300232 * @uses CI_Config::_uri_string()
233 *
234 * @param string|string[] $uri URI string or an array of segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 * @return string
236 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200237 public function site_url($uri = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Andrey Andreev1764dd72012-06-16 18:48:19 +0300239 if (empty($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000241 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600242 }
243
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300244 $uri = $this->_uri_string($uri);
245
Alex Bilbieed944a32012-06-02 11:07:47 +0100246 if ($this->item('enable_query_strings') === FALSE)
Derek Jones6d743e22010-03-02 13:22:03 -0600247 {
vlakoff66cb4132012-12-03 10:43:44 +0100248 $suffix = isset($this->config['url_suffix']) ? (string) $this->config['url_suffix'] : '';
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300249
vlakoff66cb4132012-12-03 10:43:44 +0100250 if ($suffix !== '')
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300251 {
vlakoff66cb4132012-12-03 10:43:44 +0100252 if (($offset = strpos($uri, '?')) !== FALSE)
253 {
254 $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
255 }
256 else
257 {
258 $uri .= $suffix;
259 }
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300260 }
261
262 return $this->slash_item('base_url').$this->slash_item('index_page').$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800263 }
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300264 elseif (strpos($uri, '?') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800265 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300266 $uri = '?'.$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800267 }
Andrey Andreev1764dd72012-06-16 18:48:19 +0300268
269 return $this->slash_item('base_url').$this->item('index_page').$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800270 }
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 * Base URL
Andrey Andreev5232ba02012-10-27 15:25:05 +0300276 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800277 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200278 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300279 * @uses CI_Config::_uri_string()
280 *
281 * @param string|string[] $uri URI string or an array of segments
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300282 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800283 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200284 public function base_url($uri = '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800285 {
Eric Robertsaa141a52012-07-04 02:13:56 -0500286 return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800287 }
David Behler2d2c9c62011-08-14 20:03:08 +0200288
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800289 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200290
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800291 /**
Andrey Andreev5232ba02012-10-27 15:25:05 +0300292 * Build URI string
David Behler2d2c9c62011-08-14 20:03:08 +0200293 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300294 * @used-by CI_Config::site_url()
295 * @used-by CI_Config::base_url()
296 *
297 * @param string|string[] $uri URI string or an array of segments
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300298 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800299 */
300 protected function _uri_string($uri)
301 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100302 if ($this->item('enable_query_strings') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800303 {
Derek Jones6d743e22010-03-02 13:22:03 -0600304 if (is_array($uri))
305 {
306 $uri = implode('/', $uri);
307 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200308 return trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200310 elseif (is_array($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Andrey Andreev1764dd72012-06-16 18:48:19 +0300312 return http_build_query($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200314
Greg Aker03abee32011-12-25 00:31:29 -0600315 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 /**
321 * System URL
322 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 * @return string
324 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200325 public function system_url()
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200327 $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 return $this->slash_item('base_url').end($x).'/';
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // --------------------------------------------------------------------
332
333 /**
334 * Set a config file item
335 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300336 * @param string $item Config item key
337 * @param string $value Config item value
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * @return void
339 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200340 public function set_item($item, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 $this->config[$item] = $value;
343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345}
346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347/* End of file Config.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400348/* Location: ./system/core/Config.php */