blob: 9cebe6c868bbb6e127662b02d57eb632b5d7ccee [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
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Config Class
32 *
33 * This class contains functions that enable config files to be managed
34 *
35 * @package CodeIgniter
36 * @subpackage Libraries
37 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/libraries/config.html
40 */
41class CI_Config {
42
David Behler2d2c9c62011-08-14 20:03:08 +020043 /**
44 * List of all loaded config values
45 *
46 * @var array
47 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040048 public $config = array();
Timothy Warren40403d22012-04-19 16:38:50 -040049
David Behler2d2c9c62011-08-14 20:03:08 +020050 /**
51 * List of all loaded config files
52 *
53 * @var array
54 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040055 public $is_loaded = array();
Timothy Warren40403d22012-04-19 16:38:50 -040056
David Behler2d2c9c62011-08-14 20:03:08 +020057 /**
Andrey Andreevccabcfd2012-01-07 19:30:47 +020058 * List of paths to search when trying to load a config file.
59 * This must be public as it's used by the Loader class.
David Behler2d2c9c62011-08-14 20:03:08 +020060 *
61 * @var array
62 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040063 public $_config_paths = array(APPPATH);
Derek Allard2067d1a2008-11-13 22:59:24 +000064
65 /**
66 * Constructor
67 *
68 * Sets the $config data from the primary config.php file as a class variable
Derek Allard2067d1a2008-11-13 22:59:24 +000069 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +020070 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Barry Mienydd671972010-10-04 16:33:58 +020072 $this->config =& get_config();
Andrey Andreevd52b2422012-01-07 21:28:32 +020073 log_message('debug', 'Config Class Initialized');
Phil Sturgeon4df8b222010-12-15 14:23:14 +000074
75 // Set the base_url automatically if none was provided
Taufan Aditya8749bc72012-03-11 05:43:45 +070076 if (empty($this->config['base_url']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000077 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -050078 if (isset($_SERVER['HTTP_HOST']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000079 {
Ross Duggan0a9325c2012-01-31 15:47:52 +000080 $base_url = ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
Andrey Andreevccabcfd2012-01-07 19:30:47 +020081 $base_url .= '://'. $_SERVER['HTTP_HOST']
82 . str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
Phil Sturgeon4df8b222010-12-15 14:23:14 +000083 }
Phil Sturgeon4df8b222010-12-15 14:23:14 +000084 else
85 {
86 $base_url = 'http://localhost/';
87 }
88
89 $this->set_item('base_url', $base_url);
90 }
Derek Allard2067d1a2008-11-13 22:59:24 +000091 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // --------------------------------------------------------------------
94
95 /**
96 * Load Config File
97 *
Derek Allard2067d1a2008-11-13 22:59:24 +000098 * @param string the config file name
Andrey Andreevccabcfd2012-01-07 19:30:47 +020099 * @param boolean if configuration values should be loaded into their own section
100 * @param boolean true if errors should just return false, false if an error message should be displayed
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 * @return boolean if the file was loaded correctly
Barry Mienydd671972010-10-04 16:33:58 +0200102 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200103 public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Greg Aker3a746652011-04-19 10:59:47 -0500105 $file = ($file == '') ? 'config' : str_replace('.php', '', $file);
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200106 $found = $loaded = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200107
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500108 foreach ($this->_config_paths as $path)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100109 {
110 $check_locations = defined('ENVIRONMENT')
Andrey Andreevd52b2422012-01-07 21:28:32 +0200111 ? array(ENVIRONMENT.'/'.$file, $file)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100112 : array($file);
Barry Mienydd671972010-10-04 16:33:58 +0200113
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100114 foreach ($check_locations as $location)
Derek Jones6d743e22010-03-02 13:22:03 -0600115 {
Andrey Andreevd52b2422012-01-07 21:28:32 +0200116 $file_path = $path.'config/'.$location.'.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100117
118 if (in_array($file_path, $this->is_loaded, TRUE))
119 {
120 $loaded = TRUE;
121 continue 2;
122 }
123
124 if (file_exists($file_path))
125 {
126 $found = TRUE;
127 break;
128 }
129 }
130
131 if ($found === FALSE)
132 {
Derek Jones6d743e22010-03-02 13:22:03 -0600133 continue;
134 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000135
Derek Jones6d743e22010-03-02 13:22:03 -0600136 include($file_path);
137
138 if ( ! isset($config) OR ! is_array($config))
139 {
140 if ($fail_gracefully === TRUE)
141 {
142 return FALSE;
143 }
144 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Jones6d743e22010-03-02 13:22:03 -0600147 if ($use_sections === TRUE)
148 {
149 if (isset($this->config[$file]))
150 {
151 $this->config[$file] = array_merge($this->config[$file], $config);
152 }
153 else
154 {
155 $this->config[$file] = $config;
156 }
157 }
158 else
159 {
160 $this->config = array_merge($this->config, $config);
161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Jones6d743e22010-03-02 13:22:03 -0600163 $this->is_loaded[] = $file_path;
164 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Jones6d743e22010-03-02 13:22:03 -0600166 $loaded = TRUE;
167 log_message('debug', 'Config file loaded: '.$file_path);
katzgraud127e612011-04-22 10:59:25 -0400168 break;
Derek Jones6d743e22010-03-02 13:22:03 -0600169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Jones6d743e22010-03-02 13:22:03 -0600171 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
173 if ($fail_gracefully === TRUE)
174 {
175 return FALSE;
176 }
Greg Aker3a746652011-04-19 10:59:47 -0500177 show_error('The configuration file '.$file.'.php'.' does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 return TRUE;
181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // --------------------------------------------------------------------
184
185 /**
186 * Fetch a config file item
187 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * @param string the config item name
189 * @param string the index name
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * @return string
191 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200192 public function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200193 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200195 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200196 return isset($this->config[$item]) ? $this->config[$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200199 return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
202 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000203
204 /**
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800205 * Fetch a config file item - adds slash after item (if item is not empty)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 * @param string the config item name
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 * @return string
209 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200210 public function slash_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 if ( ! isset($this->config[$item]))
213 {
214 return FALSE;
215 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200216 elseif (trim($this->config[$item]) == '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800217 {
218 return '';
219 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000220
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000221 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // --------------------------------------------------------------------
225
226 /**
227 * Site URL
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800228 * Returns base_url . index_page [. uri_string]
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 * @param string the URI string
231 * @return string
232 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200233 public function site_url($uri = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 if ($uri == '')
236 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000237 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600238 }
239
240 if ($this->item('enable_query_strings') == FALSE)
241 {
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800242 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
243 return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
244 }
245 else
246 {
247 return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
248 }
249 }
David Behler2d2c9c62011-08-14 20:03:08 +0200250
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800251 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200252
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800253 /**
254 * Base URL
255 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200256 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800257 * @param string $uri
258 * @return string
259 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200260 public function base_url($uri = '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800261 {
262 return $this->slash_item('base_url').ltrim($this->_uri_string($uri),'/');
263 }
David Behler2d2c9c62011-08-14 20:03:08 +0200264
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800265 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200266
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800267 /**
268 * Build URI string for use in Config::site_url() and Config::base_url()
David Behler2d2c9c62011-08-14 20:03:08 +0200269 *
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200270 * @param mixed $uri
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800271 * @return string
272 */
273 protected function _uri_string($uri)
274 {
275 if ($this->item('enable_query_strings') == FALSE)
276 {
Derek Jones6d743e22010-03-02 13:22:03 -0600277 if (is_array($uri))
278 {
279 $uri = implode('/', $uri);
280 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200281 return trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200283 elseif (is_array($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200285 $i = 0;
286 $str = '';
287 foreach ($uri as $key => $val)
Derek Jones6d743e22010-03-02 13:22:03 -0600288 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200289 $prefix = ($i === 0) ? '' : '&';
290 $str .= $prefix.$key.'='.$val;
291 $i++;
Derek Jones6d743e22010-03-02 13:22:03 -0600292 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200293 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200295
Greg Aker03abee32011-12-25 00:31:29 -0600296 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 /**
302 * System URL
303 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 * @return string
305 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200306 public function system_url()
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200308 $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 return $this->slash_item('base_url').end($x).'/';
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // --------------------------------------------------------------------
313
314 /**
315 * Set a config file item
316 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 * @param string the config item key
318 * @param string the config item value
319 * @return void
320 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200321 public function set_item($item, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 $this->config[$item] = $value;
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Jones6d743e22010-03-02 13:22:03 -0600326 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000327
Derek Jones6d743e22010-03-02 13:22:03 -0600328 /**
329 * Assign to Config
330 *
331 * This function is called by the front controller (CodeIgniter.php)
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200332 * after the Config class is instantiated. It permits config items
Derek Jones6d743e22010-03-02 13:22:03 -0600333 * to be assigned or overriden by variables contained in the index.php file
334 *
Derek Jones6d743e22010-03-02 13:22:03 -0600335 * @param array
336 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200337 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200338 public function _assign_to_config($items = array())
Derek Jones6d743e22010-03-02 13:22:03 -0600339 {
340 if (is_array($items))
341 {
342 foreach ($items as $key => $val)
343 {
344 $this->set_item($key, $val);
345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346 }
Derek Jones6d743e22010-03-02 13:22:03 -0600347 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000348}
349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350/* End of file Config.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400351/* Location: ./system/core/Config.php */