blob: 02e6dd84ffc3c1b3b8afced2f262471f1e8ed0fc [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 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevccabcfd2012-01-07 19:30:47 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreevccabcfd2012-01-07 19:30:47 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
Andrey Andreev5232ba02012-10-27 15:25:05 +030041 * Config Class
Derek Allard2067d1a2008-11-13 22:59:24 +000042 *
43 * This class contains functions that enable config files to be managed
44 *
45 * @package CodeIgniter
46 * @subpackage Libraries
47 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @link http://codeigniter.com/user_guide/libraries/config.html
50 */
51class CI_Config {
52
David Behler2d2c9c62011-08-14 20:03:08 +020053 /**
54 * List of all loaded config values
55 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030056 * @var array
David Behler2d2c9c62011-08-14 20:03:08 +020057 */
Eric Robertsaa141a52012-07-04 02:13:56 -050058 public $config = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030059
David Behler2d2c9c62011-08-14 20:03:08 +020060 /**
61 * List of all loaded config files
62 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030063 * @var array
David Behler2d2c9c62011-08-14 20:03:08 +020064 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040065 public $is_loaded = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030066
David Behler2d2c9c62011-08-14 20:03:08 +020067 /**
Andrey Andreevccabcfd2012-01-07 19:30:47 +020068 * List of paths to search when trying to load a config file.
David Behler2d2c9c62011-08-14 20:03:08 +020069 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030070 * @used-by CI_Loader
Andrey Andreev5232ba02012-10-27 15:25:05 +030071 * @var array
David Behler2d2c9c62011-08-14 20:03:08 +020072 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040073 public $_config_paths = array(APPPATH);
Derek Allard2067d1a2008-11-13 22:59:24 +000074
75 /**
Andrey Andreev5232ba02012-10-27 15:25:05 +030076 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000077 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030078 * Sets the $config data from the primary config.php file as a class variable.
79 *
80 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000081 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +020082 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000083 {
Barry Mienydd671972010-10-04 16:33:58 +020084 $this->config =& get_config();
Andrey Andreevd52b2422012-01-07 21:28:32 +020085 log_message('debug', 'Config Class Initialized');
Phil Sturgeon4df8b222010-12-15 14:23:14 +000086
87 // Set the base_url automatically if none was provided
Taufan Aditya8749bc72012-03-11 05:43:45 +070088 if (empty($this->config['base_url']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000089 {
vlakoff7ead65b2014-04-01 02:09:25 +020090 if (isset($_SERVER['HTTP_HOST']))
91 {
Andrey Andreevf35ae5e2014-10-10 15:40:56 +030092 $base_url = (is_https() ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST']
93 .substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
vlakoff7ead65b2014-04-01 02:09:25 +020094 }
95 else
96 {
97 $base_url = 'http://localhost/';
98 }
Phil Sturgeon4df8b222010-12-15 14:23:14 +000099
100 $this->set_item('base_url', $base_url);
101 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 // --------------------------------------------------------------------
105
106 /**
107 * Load Config File
108 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300109 * @param string $file Configuration file name
110 * @param bool $use_sections Whether configuration values should be loaded into their own section
111 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
112 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Barry Mienydd671972010-10-04 16:33:58 +0200113 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200114 public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100116 $file = ($file === '') ? 'config' : str_replace('.php', '', $file);
Fu Xub3355192014-06-12 16:45:00 +0800117 $loaded = FALSE;
Andrey Andreev9438e262012-10-05 13:16:27 +0300118
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500119 foreach ($this->_config_paths as $path)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100120 {
Fu Xu66b181e2014-06-12 16:49:11 +0800121 $found = FALSE;
Andrey Andreevdb529ca2013-01-28 11:00:02 +0200122 foreach (array(ENVIRONMENT.'/'.$file, $file) as $location)
Derek Jones6d743e22010-03-02 13:22:03 -0600123 {
Andrey Andreevd52b2422012-01-07 21:28:32 +0200124 $file_path = $path.'config/'.$location.'.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100125
126 if (in_array($file_path, $this->is_loaded, TRUE))
127 {
128 $loaded = TRUE;
129 continue 2;
130 }
131
132 if (file_exists($file_path))
133 {
134 $found = TRUE;
135 break;
136 }
137 }
138
139 if ($found === FALSE)
140 {
Derek Jones6d743e22010-03-02 13:22:03 -0600141 continue;
142 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000143
Derek Jones6d743e22010-03-02 13:22:03 -0600144 include($file_path);
145
146 if ( ! isset($config) OR ! is_array($config))
147 {
148 if ($fail_gracefully === TRUE)
149 {
150 return FALSE;
151 }
152 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
153 }
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Jones6d743e22010-03-02 13:22:03 -0600155 if ($use_sections === TRUE)
156 {
157 if (isset($this->config[$file]))
158 {
159 $this->config[$file] = array_merge($this->config[$file], $config);
160 }
161 else
162 {
163 $this->config[$file] = $config;
164 }
165 }
166 else
167 {
168 $this->config = array_merge($this->config, $config);
169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Jones6d743e22010-03-02 13:22:03 -0600171 $this->is_loaded[] = $file_path;
172 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Jones6d743e22010-03-02 13:22:03 -0600174 $loaded = TRUE;
175 log_message('debug', 'Config file loaded: '.$file_path);
katzgraud127e612011-04-22 10:59:25 -0400176 break;
Derek Jones6d743e22010-03-02 13:22:03 -0600177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Jones6d743e22010-03-02 13:22:03 -0600179 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 if ($fail_gracefully === TRUE)
182 {
183 return FALSE;
184 }
Eric Robertsaa141a52012-07-04 02:13:56 -0500185 show_error('The configuration file '.$file.'.php does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 return TRUE;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // --------------------------------------------------------------------
192
193 /**
194 * Fetch a config file item
195 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300196 * @param string $item Config item name
197 * @param string $index Index name
vlakoff90f316a2013-07-25 04:33:56 +0200198 * @return string|null The configuration item or NULL if the item doesn't exist
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200200 public function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200201 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300202 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200203 {
vlakoff184cf1b2013-07-24 03:43:39 +0200204 return isset($this->config[$item]) ? $this->config[$item] : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
206
vlakoff184cf1b2013-07-24 03:43:39 +0200207 return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
210 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000211
212 /**
Andrey Andreev5232ba02012-10-27 15:25:05 +0300213 * Fetch a config file item with slash appended (if not empty)
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300215 * @param string $item Config item name
vlakoffc1044cb2013-07-25 12:18:43 +0200216 * @return string|null The configuration item or NULL if the item doesn't exist
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200218 public function slash_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
220 if ( ! isset($this->config[$item]))
221 {
vlakoffc1044cb2013-07-25 12:18:43 +0200222 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Alex Bilbieed944a32012-06-02 11:07:47 +0100224 elseif (trim($this->config[$item]) === '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800225 {
226 return '';
227 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000228
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000229 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 // --------------------------------------------------------------------
233
234 /**
235 * Site URL
Andrey Andreev5232ba02012-10-27 15:25:05 +0300236 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800237 * Returns base_url . index_page [. uri_string]
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300239 * @uses CI_Config::_uri_string()
240 *
241 * @param string|string[] $uri URI string or an array of segments
vlakoff4c07fce2013-10-25 01:20:32 +0200242 * @param string $protocol
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 * @return string
244 */
vlakoff4c07fce2013-10-25 01:20:32 +0200245 public function site_url($uri = '', $protocol = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
vlakoff4c07fce2013-10-25 01:20:32 +0200247 $base_url = $this->slash_item('base_url');
248
249 if (isset($protocol))
250 {
251 $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
252 }
253
Andrey Andreev1764dd72012-06-16 18:48:19 +0300254 if (empty($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
vlakoff4c07fce2013-10-25 01:20:32 +0200256 return $base_url.$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600257 }
258
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300259 $uri = $this->_uri_string($uri);
260
Alex Bilbieed944a32012-06-02 11:07:47 +0100261 if ($this->item('enable_query_strings') === FALSE)
Derek Jones6d743e22010-03-02 13:22:03 -0600262 {
vlakoff4d9fd192012-12-03 11:31:00 +0100263 $suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300264
vlakoff66cb4132012-12-03 10:43:44 +0100265 if ($suffix !== '')
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300266 {
vlakoff66cb4132012-12-03 10:43:44 +0100267 if (($offset = strpos($uri, '?')) !== FALSE)
268 {
269 $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
270 }
271 else
272 {
273 $uri .= $suffix;
274 }
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300275 }
276
vlakoff4c07fce2013-10-25 01:20:32 +0200277 return $base_url.$this->slash_item('index_page').$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800278 }
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300279 elseif (strpos($uri, '?') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800280 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300281 $uri = '?'.$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800282 }
Andrey Andreev1764dd72012-06-16 18:48:19 +0300283
vlakoff4c07fce2013-10-25 01:20:32 +0200284 return $base_url.$this->item('index_page').$uri;
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 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200288
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800289 /**
290 * Base URL
Andrey Andreev5232ba02012-10-27 15:25:05 +0300291 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800292 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200293 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300294 * @uses CI_Config::_uri_string()
295 *
296 * @param string|string[] $uri URI string or an array of segments
vlakoff4c07fce2013-10-25 01:20:32 +0200297 * @param string $protocol
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300298 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800299 */
vlakoff4c07fce2013-10-25 01:20:32 +0200300 public function base_url($uri = '', $protocol = NULL)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800301 {
vlakoff4c07fce2013-10-25 01:20:32 +0200302 $base_url = $this->slash_item('base_url');
303
304 if (isset($protocol))
305 {
306 $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
307 }
308
309 return $base_url.ltrim($this->_uri_string($uri), '/');
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800310 }
David Behler2d2c9c62011-08-14 20:03:08 +0200311
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800312 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200313
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800314 /**
Andrey Andreev5232ba02012-10-27 15:25:05 +0300315 * Build URI string
David Behler2d2c9c62011-08-14 20:03:08 +0200316 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300317 * @used-by CI_Config::site_url()
318 * @used-by CI_Config::base_url()
319 *
320 * @param string|string[] $uri URI string or an array of segments
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300321 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800322 */
323 protected function _uri_string($uri)
324 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100325 if ($this->item('enable_query_strings') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800326 {
Derek Jones6d743e22010-03-02 13:22:03 -0600327 if (is_array($uri))
328 {
329 $uri = implode('/', $uri);
330 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200331 return trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200333 elseif (is_array($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
Andrey Andreev1764dd72012-06-16 18:48:19 +0300335 return http_build_query($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200337
Greg Aker03abee32011-12-25 00:31:29 -0600338 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 /**
344 * System URL
345 *
Andrey Andreeve4e10912014-02-08 19:58:48 +0200346 * @deprecated 3.0.0 Encourages insecure practices
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 * @return string
348 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200349 public function system_url()
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200351 $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 return $this->slash_item('base_url').end($x).'/';
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // --------------------------------------------------------------------
356
357 /**
358 * Set a config file item
359 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300360 * @param string $item Config item key
361 * @param string $value Config item value
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 * @return void
363 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200364 public function set_item($item, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
366 $this->config[$item] = $value;
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369}
370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371/* End of file Config.php */
Fu Xu0bd32a62014-06-12 19:58:51 +0800372/* Location: ./system/core/Config.php */