blob: 55da4e338c2a57e257883236ba38c665e6700b4a [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +020048 public $config = array();
David Behler2d2c9c62011-08-14 20:03:08 +020049 /**
50 * List of all loaded config files
51 *
52 * @var array
53 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +020054 public $is_loaded = array();
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 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +020061 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
67 *
Derek Jones4b9c6292011-07-01 17:40:48 -050068 * @param string the config file name
69 * @param boolean if configuration values should be loaded into their own section
70 * @param boolean true if errors should just return false, false if an error message should be displayed
71 * @return boolean if the file was successfully loaded or not
Derek Allard2067d1a2008-11-13 22:59:24 +000072 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +020073 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000074 {
Barry Mienydd671972010-10-04 16:33:58 +020075 $this->config =& get_config();
Derek Allard2067d1a2008-11-13 22:59:24 +000076 log_message('debug', "Config Class Initialized");
Phil Sturgeon4df8b222010-12-15 14:23:14 +000077
78 // Set the base_url automatically if none was provided
79 if ($this->config['base_url'] == '')
80 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -050081 if (isset($_SERVER['HTTP_HOST']))
Phil Sturgeon4df8b222010-12-15 14:23:14 +000082 {
Phil Sturgeonfb552382010-12-15 14:29:21 +000083 $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
Andrey Andreevccabcfd2012-01-07 19:30:47 +020084 $base_url .= '://'. $_SERVER['HTTP_HOST']
85 . str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
Phil Sturgeon4df8b222010-12-15 14:23:14 +000086 }
Phil Sturgeon4df8b222010-12-15 14:23:14 +000087 else
88 {
89 $base_url = 'http://localhost/';
90 }
91
92 $this->set_item('base_url', $base_url);
93 }
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 // --------------------------------------------------------------------
97
98 /**
99 * Load Config File
100 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 * @param string the config file name
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200102 * @param boolean if configuration values should be loaded into their own section
103 * @param boolean true if errors should just return false, false if an error message should be displayed
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 * @return boolean if the file was loaded correctly
Barry Mienydd671972010-10-04 16:33:58 +0200105 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200106 public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
Greg Aker3a746652011-04-19 10:59:47 -0500108 $file = ($file == '') ? 'config' : str_replace('.php', '', $file);
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200109 $found = $loaded = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200110
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500111 foreach ($this->_config_paths as $path)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100112 {
113 $check_locations = defined('ENVIRONMENT')
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200114 ? array(ENVIRONMENT.DIRECTORY_SEPARATOR.$file, $file)
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100115 : array($file);
Barry Mienydd671972010-10-04 16:33:58 +0200116
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100117 foreach ($check_locations as $location)
Derek Jones6d743e22010-03-02 13:22:03 -0600118 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200119 $file_path = $path.'config'.DIRECTORY_SEPARATOR.$location.'.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100120
121 if (in_array($file_path, $this->is_loaded, TRUE))
122 {
123 $loaded = TRUE;
124 continue 2;
125 }
126
127 if (file_exists($file_path))
128 {
129 $found = TRUE;
130 break;
131 }
132 }
133
134 if ($found === FALSE)
135 {
Derek Jones6d743e22010-03-02 13:22:03 -0600136 continue;
137 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000138
Derek Jones6d743e22010-03-02 13:22:03 -0600139 include($file_path);
140
141 if ( ! isset($config) OR ! is_array($config))
142 {
143 if ($fail_gracefully === TRUE)
144 {
145 return FALSE;
146 }
147 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Jones6d743e22010-03-02 13:22:03 -0600150 if ($use_sections === TRUE)
151 {
152 if (isset($this->config[$file]))
153 {
154 $this->config[$file] = array_merge($this->config[$file], $config);
155 }
156 else
157 {
158 $this->config[$file] = $config;
159 }
160 }
161 else
162 {
163 $this->config = array_merge($this->config, $config);
164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Jones6d743e22010-03-02 13:22:03 -0600166 $this->is_loaded[] = $file_path;
167 unset($config);
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Jones6d743e22010-03-02 13:22:03 -0600169 $loaded = TRUE;
170 log_message('debug', 'Config file loaded: '.$file_path);
katzgraud127e612011-04-22 10:59:25 -0400171 break;
Derek Jones6d743e22010-03-02 13:22:03 -0600172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Jones6d743e22010-03-02 13:22:03 -0600174 if ($loaded === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
176 if ($fail_gracefully === TRUE)
177 {
178 return FALSE;
179 }
Greg Aker3a746652011-04-19 10:59:47 -0500180 show_error('The configuration file '.$file.'.php'.' does not exist.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 return TRUE;
184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 // --------------------------------------------------------------------
187
188 /**
189 * Fetch a config file item
190 *
191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @param string the config item name
193 * @param string the index name
194 * @param bool
195 * @return string
196 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200197 public function item($item, $index = '')
Barry Mienydd671972010-10-04 16:33:58 +0200198 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 if ($index == '')
Barry Mienydd671972010-10-04 16:33:58 +0200200 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200201 return isset($this->config[$item]) ? $this->config[$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200204 return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
Barry Mienydd671972010-10-04 16:33:58 +0200206
207 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000208
209 /**
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800210 * Fetch a config file item - adds slash after item (if item is not empty)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 * @param string the config item name
213 * @param bool
214 * @return string
215 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200216 public function slash_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
218 if ( ! isset($this->config[$item]))
219 {
220 return FALSE;
221 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200222 elseif (trim($this->config[$item]) == '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800223 {
224 return '';
225 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000226
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000227 return rtrim($this->config[$item], '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // --------------------------------------------------------------------
231
232 /**
233 * Site URL
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800234 * Returns base_url . index_page [. uri_string]
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @param string the URI string
237 * @return string
238 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200239 public function site_url($uri = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 if ($uri == '')
242 {
Phil Sturgeon4df8b222010-12-15 14:23:14 +0000243 return $this->slash_item('base_url').$this->item('index_page');
Derek Jones6d743e22010-03-02 13:22:03 -0600244 }
245
246 if ($this->item('enable_query_strings') == FALSE)
247 {
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800248 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
249 return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
250 }
251 else
252 {
253 return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
254 }
255 }
David Behler2d2c9c62011-08-14 20:03:08 +0200256
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800257 // -------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200258
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800259 /**
260 * Base URL
261 * Returns base_url [. uri_string]
David Behler2d2c9c62011-08-14 20:03:08 +0200262 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800263 * @param string $uri
264 * @return string
265 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200266 public function base_url($uri = '')
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800267 {
268 return $this->slash_item('base_url').ltrim($this->_uri_string($uri),'/');
269 }
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 * Build URI string for use in Config::site_url() and Config::base_url()
David Behler2d2c9c62011-08-14 20:03:08 +0200275 *
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200276 * @param mixed $uri
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -0800277 * @return string
278 */
279 protected function _uri_string($uri)
280 {
281 if ($this->item('enable_query_strings') == FALSE)
282 {
Derek Jones6d743e22010-03-02 13:22:03 -0600283 if (is_array($uri))
284 {
285 $uri = implode('/', $uri);
286 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200287 return trim($uri, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200289 elseif (is_array($uri))
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200291 $i = 0;
292 $str = '';
293 foreach ($uri as $key => $val)
Derek Jones6d743e22010-03-02 13:22:03 -0600294 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200295 $prefix = ($i === 0) ? '' : '&';
296 $str .= $prefix.$key.'='.$val;
297 $i++;
Derek Jones6d743e22010-03-02 13:22:03 -0600298 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200299 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200301
Greg Aker03abee32011-12-25 00:31:29 -0600302 return $uri;
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 // --------------------------------------------------------------------
David Behler2d2c9c62011-08-14 20:03:08 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 /**
308 * System URL
309 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 * @return string
311 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200312 public function system_url()
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200314 $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 return $this->slash_item('base_url').end($x).'/';
316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // --------------------------------------------------------------------
319
320 /**
321 * Set a config file item
322 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 * @param string the config item key
324 * @param string the config item value
325 * @return void
326 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200327 public function set_item($item, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 $this->config[$item] = $value;
330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Jones6d743e22010-03-02 13:22:03 -0600332 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000333
Derek Jones6d743e22010-03-02 13:22:03 -0600334 /**
335 * Assign to Config
336 *
337 * This function is called by the front controller (CodeIgniter.php)
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200338 * after the Config class is instantiated. It permits config items
Derek Jones6d743e22010-03-02 13:22:03 -0600339 * to be assigned or overriden by variables contained in the index.php file
340 *
Derek Jones6d743e22010-03-02 13:22:03 -0600341 * @param array
342 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200343 */
Andrey Andreevccabcfd2012-01-07 19:30:47 +0200344 public function _assign_to_config($items = array())
Derek Jones6d743e22010-03-02 13:22:03 -0600345 {
346 if (is_array($items))
347 {
348 foreach ($items as $key => $val)
349 {
350 $this->set_item($key, $val);
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352 }
Derek Jones6d743e22010-03-02 13:22:03 -0600353 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000354}
355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356/* End of file Config.php */
katzgraud127e612011-04-22 10:59:25 -0400357/* Location: ./system/core/Config.php */