Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * CodeIgniter Config Class |
| 20 | * |
| 21 | * This class contains functions that enable config files to be managed |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
| 25 | * @category Libraries |
| 26 | * @author ExpressionEngine Dev Team |
| 27 | * @link http://codeigniter.com/user_guide/libraries/config.html |
| 28 | */ |
| 29 | class CI_Config { |
| 30 | |
| 31 | var $config = array(); |
| 32 | var $is_loaded = array(); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 33 | var $_config_paths = array(APPPATH); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Constructor |
| 37 | * |
| 38 | * Sets the $config data from the primary config.php file as a class variable |
| 39 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 40 | * @access public |
| 41 | * @param string the config file name |
| 42 | * @param boolean if configuration values should be loaded into their own section |
| 43 | * @param boolean true if errors should just return false, false if an error message should be displayed |
| 44 | * @return boolean if the file was successfully loaded or not |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 46 | function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 48 | $this->config =& get_config(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | log_message('debug', "Config Class Initialized"); |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 50 | |
| 51 | // Set the base_url automatically if none was provided |
| 52 | if ($this->config['base_url'] == '') |
| 53 | { |
Pascal Kriete | 5d5895f | 2011-02-14 13:27:07 -0500 | [diff] [blame] | 54 | if (isset($_SERVER['HTTP_HOST'])) |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 55 | { |
Phil Sturgeon | fb55238 | 2010-12-15 14:29:21 +0000 | [diff] [blame] | 56 | $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http'; |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 57 | $base_url .= '://'. $_SERVER['HTTP_HOST']; |
Eric Barnes | 32dbac2 | 2011-04-26 22:51:32 -0400 | [diff] [blame] | 58 | $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | else |
| 62 | { |
| 63 | $base_url = 'http://localhost/'; |
| 64 | } |
| 65 | |
| 66 | $this->set_item('base_url', $base_url); |
| 67 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 69 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | // -------------------------------------------------------------------- |
| 71 | |
| 72 | /** |
| 73 | * Load Config File |
| 74 | * |
| 75 | * @access public |
| 76 | * @param string the config file name |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 77 | * @param boolean if configuration values should be loaded into their own section |
| 78 | * @param boolean true if errors should just return false, false if an error message should be displayed |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | * @return boolean if the file was loaded correctly |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 80 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) |
| 82 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 83 | $file = ($file == '') ? 'config' : str_replace('.php', '', $file); |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 84 | $found = FALSE; |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 85 | $loaded = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 86 | |
Pascal Kriete | 5d5895f | 2011-02-14 13:27:07 -0500 | [diff] [blame] | 87 | foreach ($this->_config_paths as $path) |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 88 | { |
| 89 | $check_locations = defined('ENVIRONMENT') |
| 90 | ? array(ENVIRONMENT.'/'.$file, $file) |
| 91 | : array($file); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 93 | foreach ($check_locations as $location) |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 94 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 95 | $file_path = $path.'config/'.$location.'.php'; |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 96 | |
| 97 | if (in_array($file_path, $this->is_loaded, TRUE)) |
| 98 | { |
| 99 | $loaded = TRUE; |
| 100 | continue 2; |
| 101 | } |
| 102 | |
| 103 | if (file_exists($file_path)) |
| 104 | { |
| 105 | $found = TRUE; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if ($found === FALSE) |
| 111 | { |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 112 | continue; |
| 113 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 115 | include($file_path); |
| 116 | |
| 117 | if ( ! isset($config) OR ! is_array($config)) |
| 118 | { |
| 119 | if ($fail_gracefully === TRUE) |
| 120 | { |
| 121 | return FALSE; |
| 122 | } |
| 123 | show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.'); |
| 124 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 126 | if ($use_sections === TRUE) |
| 127 | { |
| 128 | if (isset($this->config[$file])) |
| 129 | { |
| 130 | $this->config[$file] = array_merge($this->config[$file], $config); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | $this->config[$file] = $config; |
| 135 | } |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | $this->config = array_merge($this->config, $config); |
| 140 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 142 | $this->is_loaded[] = $file_path; |
| 143 | unset($config); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 144 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 145 | $loaded = TRUE; |
| 146 | log_message('debug', 'Config file loaded: '.$file_path); |
katzgrau | d127e61 | 2011-04-22 10:59:25 -0400 | [diff] [blame] | 147 | break; |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 148 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 150 | if ($loaded === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | { |
| 152 | if ($fail_gracefully === TRUE) |
| 153 | { |
| 154 | return FALSE; |
| 155 | } |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 156 | show_error('The configuration file '.$file.'.php'.' does not exist.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | } |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | return TRUE; |
| 160 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 161 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Fetch a config file item |
| 166 | * |
| 167 | * |
| 168 | * @access public |
| 169 | * @param string the config item name |
| 170 | * @param string the index name |
| 171 | * @param bool |
| 172 | * @return string |
| 173 | */ |
| 174 | function item($item, $index = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | if ($index == '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | if ( ! isset($this->config[$item])) |
| 179 | { |
| 180 | return FALSE; |
| 181 | } |
| 182 | |
| 183 | $pref = $this->config[$item]; |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | if ( ! isset($this->config[$index])) |
| 188 | { |
| 189 | return FALSE; |
| 190 | } |
| 191 | |
| 192 | if ( ! isset($this->config[$index][$item])) |
| 193 | { |
| 194 | return FALSE; |
| 195 | } |
| 196 | |
| 197 | $pref = $this->config[$index][$item]; |
| 198 | } |
| 199 | |
| 200 | return $pref; |
| 201 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | |
| 203 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | |
| 205 | /** |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 206 | * Fetch a config file item - adds slash after item (if item is not empty) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | * |
| 208 | * @access public |
| 209 | * @param string the config item name |
| 210 | * @param bool |
| 211 | * @return string |
| 212 | */ |
| 213 | function slash_item($item) |
| 214 | { |
| 215 | if ( ! isset($this->config[$item])) |
| 216 | { |
| 217 | return FALSE; |
| 218 | } |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 219 | if( trim($this->config[$item]) == '') |
| 220 | { |
| 221 | return ''; |
| 222 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 224 | return rtrim($this->config[$item], '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | // -------------------------------------------------------------------- |
| 228 | |
| 229 | /** |
| 230 | * Site URL |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 231 | * Returns base_url . index_page [. uri_string] |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | * |
| 233 | * @access public |
| 234 | * @param string the URI string |
| 235 | * @return string |
| 236 | */ |
| 237 | function site_url($uri = '') |
| 238 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | if ($uri == '') |
| 240 | { |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 241 | return $this->slash_item('base_url').$this->item('index_page'); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | if ($this->item('enable_query_strings') == FALSE) |
| 245 | { |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 246 | $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix'); |
| 247 | return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix; |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // ------------------------------------------------------------- |
| 256 | |
| 257 | /** |
| 258 | * Base URL |
| 259 | * Returns base_url [. uri_string] |
| 260 | * |
| 261 | * @access public |
| 262 | * @param string $uri |
| 263 | * @return string |
| 264 | */ |
| 265 | function base_url($uri = '') |
| 266 | { |
| 267 | return $this->slash_item('base_url').ltrim($this->_uri_string($uri),'/'); |
| 268 | } |
| 269 | |
| 270 | // ------------------------------------------------------------- |
| 271 | |
| 272 | /** |
| 273 | * Build URI string for use in Config::site_url() and Config::base_url() |
| 274 | * |
| 275 | * @access protected |
| 276 | * @param $uri |
| 277 | * @return string |
| 278 | */ |
| 279 | protected function _uri_string($uri) |
| 280 | { |
| 281 | if ($this->item('enable_query_strings') == FALSE) |
| 282 | { |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 283 | if (is_array($uri)) |
| 284 | { |
| 285 | $uri = implode('/', $uri); |
| 286 | } |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 287 | $uri = trim($uri, '/'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | } |
| 289 | else |
| 290 | { |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 291 | if (is_array($uri)) |
| 292 | { |
| 293 | $i = 0; |
| 294 | $str = ''; |
| 295 | foreach ($uri as $key => $val) |
| 296 | { |
| 297 | $prefix = ($i == 0) ? '' : '&'; |
| 298 | $str .= $prefix.$key.'='.$val; |
| 299 | $i++; |
| 300 | } |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 301 | $uri = $str; |
| 302 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | } |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 304 | return $uri; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 306 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | // -------------------------------------------------------------------- |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 308 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | /** |
| 310 | * System URL |
| 311 | * |
| 312 | * @access public |
| 313 | * @return string |
| 314 | */ |
| 315 | function system_url() |
| 316 | { |
| 317 | $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH)); |
| 318 | return $this->slash_item('base_url').end($x).'/'; |
| 319 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | // -------------------------------------------------------------------- |
| 322 | |
| 323 | /** |
| 324 | * Set a config file item |
| 325 | * |
| 326 | * @access public |
| 327 | * @param string the config item key |
| 328 | * @param string the config item value |
| 329 | * @return void |
| 330 | */ |
| 331 | function set_item($item, $value) |
| 332 | { |
| 333 | $this->config[$item] = $value; |
| 334 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 336 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 338 | /** |
| 339 | * Assign to Config |
| 340 | * |
| 341 | * This function is called by the front controller (CodeIgniter.php) |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 342 | * after the Config class is instantiated. It permits config items |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 343 | * to be assigned or overriden by variables contained in the index.php file |
| 344 | * |
| 345 | * @access private |
| 346 | * @param array |
| 347 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 348 | */ |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 349 | function _assign_to_config($items = array()) |
| 350 | { |
| 351 | if (is_array($items)) |
| 352 | { |
| 353 | foreach ($items as $key => $val) |
| 354 | { |
| 355 | $this->set_item($key, $val); |
| 356 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 357 | } |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 358 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // END CI_Config class |
| 362 | |
| 363 | /* End of file Config.php */ |
katzgrau | d127e61 | 2011-04-22 10:59:25 -0400 | [diff] [blame] | 364 | /* Location: ./system/core/Config.php */ |