Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 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 |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, 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(); |
joelcox | cee8075 | 2011-01-15 23:09:47 +0100 | [diff] [blame^] | 33 | var $environment = ENVIRONMENT; |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 34 | var $_config_paths = array(APPPATH); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | * |
| 39 | * Sets the $config data from the primary config.php file as a class variable |
| 40 | * |
| 41 | * @access public |
| 42 | * @param string the config file name |
| 43 | * @param boolean if configuration values should be loaded into their own section |
| 44 | * @param boolean true if errors should just return false, false if an error message should be displayed |
| 45 | * @return boolean if the file was successfully loaded or not |
| 46 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 47 | function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 49 | $this->config =& get_config(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | log_message('debug', "Config Class Initialized"); |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 51 | |
| 52 | // Set the base_url automatically if none was provided |
| 53 | if ($this->config['base_url'] == '') |
| 54 | { |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 55 | if(isset($_SERVER['HTTP_HOST'])) |
| 56 | { |
Phil Sturgeon | fb55238 | 2010-12-15 14:29:21 +0000 | [diff] [blame] | 57 | $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http'; |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 58 | $base_url .= '://'. $_SERVER['HTTP_HOST']; |
| 59 | $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); |
| 60 | } |
| 61 | |
| 62 | else |
| 63 | { |
| 64 | $base_url = 'http://localhost/'; |
| 65 | } |
| 66 | |
| 67 | $this->set_item('base_url', $base_url); |
| 68 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 70 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | // -------------------------------------------------------------------- |
| 72 | |
| 73 | /** |
| 74 | * Load Config File |
| 75 | * |
| 76 | * @access public |
| 77 | * @param string the config file name |
joelcox | cee8075 | 2011-01-15 23:09:47 +0100 | [diff] [blame^] | 78 | * @param boolean if configuration values should be loaded into their own section |
| 79 | * @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] | 80 | * @return boolean if the file was loaded correctly |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) |
| 83 | { |
| 84 | $file = ($file == '') ? 'config' : str_replace(EXT, '', $file); |
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 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 87 | foreach($this->_config_paths as $path) |
joelcox | cee8075 | 2011-01-15 23:09:47 +0100 | [diff] [blame^] | 88 | { |
| 89 | $file_path = $path.'config/'.ENVIRONMENT.'/'.$file.EXT; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 91 | if (in_array($file_path, $this->is_loaded, TRUE)) |
| 92 | { |
| 93 | $loaded = TRUE; |
| 94 | continue; |
| 95 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | |
joelcox | cee8075 | 2011-01-15 23:09:47 +0100 | [diff] [blame^] | 97 | if ( ! $file_path) |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 98 | { |
joelcox | cee8075 | 2011-01-15 23:09:47 +0100 | [diff] [blame^] | 99 | if ( ! file_exists($path.'config/'.$file.EXT)) |
| 100 | { |
| 101 | $file_path = $path.'config/'.$file.EXT; |
| 102 | } |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 103 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 104 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 105 | include($file_path); |
| 106 | |
| 107 | if ( ! isset($config) OR ! is_array($config)) |
| 108 | { |
| 109 | if ($fail_gracefully === TRUE) |
| 110 | { |
| 111 | return FALSE; |
| 112 | } |
| 113 | show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.'); |
| 114 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 115 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 116 | if ($use_sections === TRUE) |
| 117 | { |
| 118 | if (isset($this->config[$file])) |
| 119 | { |
| 120 | $this->config[$file] = array_merge($this->config[$file], $config); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | $this->config[$file] = $config; |
| 125 | } |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | $this->config = array_merge($this->config, $config); |
| 130 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 131 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 132 | $this->is_loaded[] = $file_path; |
| 133 | unset($config); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 134 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 135 | $loaded = TRUE; |
| 136 | log_message('debug', 'Config file loaded: '.$file_path); |
| 137 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 139 | if ($loaded === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | { |
| 141 | if ($fail_gracefully === TRUE) |
| 142 | { |
| 143 | return FALSE; |
| 144 | } |
joelcox | cee8075 | 2011-01-15 23:09:47 +0100 | [diff] [blame^] | 145 | show_error('The configuration file '.$environment.'/'.$file.EXT.' does not exist.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | } |
joelcox | cee8075 | 2011-01-15 23:09:47 +0100 | [diff] [blame^] | 147 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | return TRUE; |
| 149 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | // -------------------------------------------------------------------- |
| 152 | |
| 153 | /** |
| 154 | * Fetch a config file item |
| 155 | * |
| 156 | * |
| 157 | * @access public |
| 158 | * @param string the config item name |
| 159 | * @param string the index name |
| 160 | * @param bool |
| 161 | * @return string |
| 162 | */ |
| 163 | function item($item, $index = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | if ($index == '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | if ( ! isset($this->config[$item])) |
| 168 | { |
| 169 | return FALSE; |
| 170 | } |
| 171 | |
| 172 | $pref = $this->config[$item]; |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | if ( ! isset($this->config[$index])) |
| 177 | { |
| 178 | return FALSE; |
| 179 | } |
| 180 | |
| 181 | if ( ! isset($this->config[$index][$item])) |
| 182 | { |
| 183 | return FALSE; |
| 184 | } |
| 185 | |
| 186 | $pref = $this->config[$index][$item]; |
| 187 | } |
| 188 | |
| 189 | return $pref; |
| 190 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | |
| 192 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * Fetch a config file item - adds slash after item |
| 196 | * |
| 197 | * The second parameter allows a slash to be added to the end of |
| 198 | * the item, in the case of a path. |
| 199 | * |
| 200 | * @access public |
| 201 | * @param string the config item name |
| 202 | * @param bool |
| 203 | * @return string |
| 204 | */ |
| 205 | function slash_item($item) |
| 206 | { |
| 207 | if ( ! isset($this->config[$item])) |
| 208 | { |
| 209 | return FALSE; |
| 210 | } |
| 211 | |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 212 | return rtrim($this->config[$item], '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 214 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | // -------------------------------------------------------------------- |
| 216 | |
| 217 | /** |
| 218 | * Site URL |
| 219 | * |
| 220 | * @access public |
| 221 | * @param string the URI string |
| 222 | * @return string |
| 223 | */ |
| 224 | function site_url($uri = '') |
| 225 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | if ($uri == '') |
| 227 | { |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 228 | return $this->slash_item('base_url').$this->item('index_page'); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | if ($this->item('enable_query_strings') == FALSE) |
| 232 | { |
| 233 | if (is_array($uri)) |
| 234 | { |
| 235 | $uri = implode('/', $uri); |
| 236 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 237 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 238 | $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 239 | return $this->slash_item('base_url').$this->slash_item('index_page').trim($uri, '/').$suffix; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | } |
| 241 | else |
| 242 | { |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 243 | if (is_array($uri)) |
| 244 | { |
| 245 | $i = 0; |
| 246 | $str = ''; |
| 247 | foreach ($uri as $key => $val) |
| 248 | { |
| 249 | $prefix = ($i == 0) ? '' : '&'; |
| 250 | $str .= $prefix.$key.'='.$val; |
| 251 | $i++; |
| 252 | } |
| 253 | |
| 254 | $uri = $str; |
| 255 | } |
| 256 | |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 257 | return $this->slash_item('base_url').$this->item('index_page').'?'.$uri; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 260 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | // -------------------------------------------------------------------- |
| 262 | |
| 263 | /** |
| 264 | * System URL |
| 265 | * |
| 266 | * @access public |
| 267 | * @return string |
| 268 | */ |
| 269 | function system_url() |
| 270 | { |
| 271 | $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH)); |
| 272 | return $this->slash_item('base_url').end($x).'/'; |
| 273 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 274 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | // -------------------------------------------------------------------- |
| 276 | |
| 277 | /** |
| 278 | * Set a config file item |
| 279 | * |
| 280 | * @access public |
| 281 | * @param string the config item key |
| 282 | * @param string the config item value |
| 283 | * @return void |
| 284 | */ |
| 285 | function set_item($item, $value) |
| 286 | { |
| 287 | $this->config[$item] = $value; |
| 288 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 289 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 290 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 292 | /** |
| 293 | * Assign to Config |
| 294 | * |
| 295 | * This function is called by the front controller (CodeIgniter.php) |
| 296 | * after the Config class is instantiated. It permits config items |
| 297 | * to be assigned or overriden by variables contained in the index.php file |
| 298 | * |
| 299 | * @access private |
| 300 | * @param array |
| 301 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 302 | */ |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 303 | function _assign_to_config($items = array()) |
| 304 | { |
| 305 | if (is_array($items)) |
| 306 | { |
| 307 | foreach ($items as $key => $val) |
| 308 | { |
| 309 | $this->set_item($key, $val); |
| 310 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 311 | } |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 312 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // END CI_Config class |
| 316 | |
| 317 | /* End of file Config.php */ |
Derek Jones | c68dfbf | 2010-03-02 12:59:23 -0600 | [diff] [blame] | 318 | /* Location: ./system/core/Config.php */ |