blob: 728e28dd8bd3126f1cca7881e40190d41b9345f7 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 {
vlakoff2516f3b2014-03-31 20:46:04 +020079 $script_basename = basename($_SERVER['SCRIPT_NAME']);
80
vlakoffdfd51cb2014-03-31 20:41:07 +020081 $base_url = (is_https() ? 'https' : 'http') . '://'
82 . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost')
vlakoff2516f3b2014-03-31 20:46:04 +020083 . preg_replace('/'.preg_quote($script_basename).'$/', '', $_SERVER['SCRIPT_NAME']);
Phil Sturgeon4df8b222010-12-15 14:23:14 +000084
85 $this->set_item('base_url', $base_url);
86 }
Derek Allard2067d1a2008-11-13 22:59:24 +000087 }
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 // --------------------------------------------------------------------
90
91 /**
92 * Load Config File
93 *
Andrey Andreev5232ba02012-10-27 15:25:05 +030094 * @param string $file Configuration file name
95 * @param bool $use_sections Whether configuration values should be loaded into their own section
96 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
97 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Barry Mienydd671972010-10-04 16:33:58 +020098 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +020099 public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100101 $file = ($file === '') ? 'config' : str_replace('.php', '', $file);
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200102 $found = $loaded = FALSE;
Andrey Andreev9438e262012-10-05 13:16:27 +0300103
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500104 foreach ($this->_config_paths as $path)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100105 {
Andrey Andreevdb529ca2013-01-28 11:00:02 +0200106 foreach (array(ENVIRONMENT.'/'.$file, $file) as $location)
Derek Jones6d743e22010-03-02 13:22:03 -0600107 {
Andrey Andreevd52b2422012-01-07 21:28:32 +0200108 $file_path = $path.'config/'.$location.'.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100109
110 if (in_array($file_path, $this->is_loaded, TRUE))
111 {
112 $loaded = TRUE;
113 continue 2;
114 }
115
116 if (file_exists($file_path))
117 {
118 $found = TRUE;
119 break;
120 }
121 }
122
123 if ($found === FALSE)
124 {
Derek Jones6d743e22010-03-02 13:22:03 -0600125 continue;
126 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000127
Derek Jones6d743e22010-03-02 13:22:03 -0600128 include($file_path);
129
130 if ( ! isset($config) OR ! is_array($config))
131 {
132 if ($fail_gracefully === TRUE)
133 {
134 return FALSE;
135 }
136 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Jones6d743e22010-03-02 13:22:03 -0600139 if ($use_sections === TRUE)
140 {
141 if (isset($this->config[$file]))
142 {
143 $this->config[$file] = array_merge($this->config[$file], $config);
144 }
145 else
146 {
147 $this->config[$file] = $config;
148 }
149 }
150 else
151 {
152 $this->config = array_merge($this->config, $config);
153 }
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Jones6d743e22010-03-02 13:22:03 -0600155 $this->is_loaded[] = $file_path;
156 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Jones6d743e22010-03-02 13:22:03 -0600158 $loaded = TRUE;
159 log_message('debug', 'Config file loaded: '.$file_path);
katzgraud127e612011-04-22 10:59:25 -0400160 break;
Derek Jones6d743e22010-03-02 13:22:03 -0600161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Jones6d743e22010-03-02 13:22:03 -0600163 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
165 if ($fail_gracefully === TRUE)
166 {
167 return FALSE;
168 }
Eric Robertsaa141a52012-07-04 02:13:56 -0500169 show_error('The configuration file '.$file.'.php does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 return TRUE;
173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 // --------------------------------------------------------------------
176
177 /**
178 * Fetch a config file item
179 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300180 * @param string $item Config item name
181 * @param string $index Index name
vlakoff90f316a2013-07-25 04:33:56 +0200182 * @return string|null The configuration item or NULL if the item doesn't exist
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200184 public function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200185 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300186 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200187 {
vlakoff184cf1b2013-07-24 03:43:39 +0200188 return isset($this->config[$item]) ? $this->config[$item] : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
190
vlakoff184cf1b2013-07-24 03:43:39 +0200191 return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
194 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000195
196 /**
Andrey Andreev5232ba02012-10-27 15:25:05 +0300197 * Fetch a config file item with slash appended (if not empty)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300199 * @param string $item Config item name
vlakoffc1044cb2013-07-25 12:18:43 +0200200 * @return string|null The configuration item or NULL if the item doesn't exist
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200202 public function slash_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
204 if ( ! isset($this->config[$item]))
205 {
vlakoffc1044cb2013-07-25 12:18:43 +0200206 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
Alex Bilbieed944a32012-06-02 11:07:47 +0100208 elseif (trim($this->config[$item]) === '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800209 {
210 return '';
211 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000213 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 // --------------------------------------------------------------------
217
218 /**
219 * Site URL
Andrey Andreev5232ba02012-10-27 15:25:05 +0300220 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800221 * Returns base_url . index_page [. uri_string]
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300223 * @uses CI_Config::_uri_string()
224 *
225 * @param string|string[] $uri URI string or an array of segments
vlakoff4c07fce2013-10-25 01:20:32 +0200226 * @param string $protocol
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 * @return string
228 */
vlakoff4c07fce2013-10-25 01:20:32 +0200229 public function site_url($uri = '', $protocol = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
vlakoff4c07fce2013-10-25 01:20:32 +0200231 $base_url = $this->slash_item('base_url');
232
233 if (isset($protocol))
234 {
235 $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
236 }
237
Andrey Andreev1764dd72012-06-16 18:48:19 +0300238 if (empty($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
vlakoff4c07fce2013-10-25 01:20:32 +0200240 return $base_url.$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600241 }
242
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300243 $uri = $this->_uri_string($uri);
244
Alex Bilbieed944a32012-06-02 11:07:47 +0100245 if ($this->item('enable_query_strings') === FALSE)
Derek Jones6d743e22010-03-02 13:22:03 -0600246 {
vlakoff4d9fd192012-12-03 11:31:00 +0100247 $suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300248
vlakoff66cb4132012-12-03 10:43:44 +0100249 if ($suffix !== '')
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300250 {
vlakoff66cb4132012-12-03 10:43:44 +0100251 if (($offset = strpos($uri, '?')) !== FALSE)
252 {
253 $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
254 }
255 else
256 {
257 $uri .= $suffix;
258 }
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300259 }
260
vlakoff4c07fce2013-10-25 01:20:32 +0200261 return $base_url.$this->slash_item('index_page').$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800262 }
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300263 elseif (strpos($uri, '?') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800264 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300265 $uri = '?'.$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800266 }
Andrey Andreev1764dd72012-06-16 18:48:19 +0300267
vlakoff4c07fce2013-10-25 01:20:32 +0200268 return $base_url.$this->item('index_page').$uri;
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800269 }
David Behler2d2c9c62011-08-14 20:03:08 +0200270
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800271 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200272
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800273 /**
274 * Base URL
Andrey Andreev5232ba02012-10-27 15:25:05 +0300275 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800276 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200277 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300278 * @uses CI_Config::_uri_string()
279 *
280 * @param string|string[] $uri URI string or an array of segments
vlakoff4c07fce2013-10-25 01:20:32 +0200281 * @param string $protocol
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300282 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800283 */
vlakoff4c07fce2013-10-25 01:20:32 +0200284 public function base_url($uri = '', $protocol = NULL)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800285 {
vlakoff4c07fce2013-10-25 01:20:32 +0200286 $base_url = $this->slash_item('base_url');
287
288 if (isset($protocol))
289 {
290 $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
291 }
292
293 return $base_url.ltrim($this->_uri_string($uri), '/');
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800294 }
David Behler2d2c9c62011-08-14 20:03:08 +0200295
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800296 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200297
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800298 /**
Andrey Andreev5232ba02012-10-27 15:25:05 +0300299 * Build URI string
David Behler2d2c9c62011-08-14 20:03:08 +0200300 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300301 * @used-by CI_Config::site_url()
302 * @used-by CI_Config::base_url()
303 *
304 * @param string|string[] $uri URI string or an array of segments
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300305 * @return string
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800306 */
307 protected function _uri_string($uri)
308 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100309 if ($this->item('enable_query_strings') === FALSE)
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800310 {
Derek Jones6d743e22010-03-02 13:22:03 -0600311 if (is_array($uri))
312 {
313 $uri = implode('/', $uri);
314 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200315 return trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200317 elseif (is_array($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
Andrey Andreev1764dd72012-06-16 18:48:19 +0300319 return http_build_query($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200321
Greg Aker03abee32011-12-25 00:31:29 -0600322 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 /**
328 * System URL
329 *
Andrey Andreeve4e10912014-02-08 19:58:48 +0200330 * @deprecated 3.0.0 Encourages insecure practices
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * @return string
332 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200333 public function system_url()
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200335 $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 return $this->slash_item('base_url').end($x).'/';
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // --------------------------------------------------------------------
340
341 /**
342 * Set a config file item
343 *
Andrey Andreev5232ba02012-10-27 15:25:05 +0300344 * @param string $item Config item key
345 * @param string $value Config item value
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @return void
347 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200348 public function set_item($item, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 $this->config[$item] = $value;
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
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 */