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(); |
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 | * |
| 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 |
| 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 | { |
| 54 | // Base URL (keeps this crazy sh*t out of the config.php |
| 55 | if(isset($_SERVER['HTTP_HOST'])) |
| 56 | { |
| 57 | $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http'; |
| 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 |
| 78 | * @return boolean if the file was loaded correctly |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) |
| 81 | { |
| 82 | $file = ($file == '') ? 'config' : str_replace(EXT, '', $file); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 83 | $loaded = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 84 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 85 | foreach($this->_config_paths as $path) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | { |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 87 | $file_path = $path.'config/'.$file.EXT; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 88 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 89 | if (in_array($file_path, $this->is_loaded, TRUE)) |
| 90 | { |
| 91 | $loaded = TRUE; |
| 92 | continue; |
| 93 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 95 | if ( ! file_exists($path.'config/'.$file.EXT)) |
| 96 | { |
| 97 | continue; |
| 98 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 99 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 100 | include($file_path); |
| 101 | |
| 102 | if ( ! isset($config) OR ! is_array($config)) |
| 103 | { |
| 104 | if ($fail_gracefully === TRUE) |
| 105 | { |
| 106 | return FALSE; |
| 107 | } |
| 108 | show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.'); |
| 109 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 110 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 111 | if ($use_sections === TRUE) |
| 112 | { |
| 113 | if (isset($this->config[$file])) |
| 114 | { |
| 115 | $this->config[$file] = array_merge($this->config[$file], $config); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | $this->config[$file] = $config; |
| 120 | } |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | $this->config = array_merge($this->config, $config); |
| 125 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 126 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 127 | $this->is_loaded[] = $file_path; |
| 128 | unset($config); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 130 | $loaded = TRUE; |
| 131 | log_message('debug', 'Config file loaded: '.$file_path); |
| 132 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 133 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 134 | if ($loaded === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | { |
| 136 | if ($fail_gracefully === TRUE) |
| 137 | { |
| 138 | return FALSE; |
| 139 | } |
| 140 | show_error('The configuration file '.$file.EXT.' does not exist.'); |
| 141 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | return TRUE; |
| 144 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | // -------------------------------------------------------------------- |
| 147 | |
| 148 | /** |
| 149 | * Fetch a config file item |
| 150 | * |
| 151 | * |
| 152 | * @access public |
| 153 | * @param string the config item name |
| 154 | * @param string the index name |
| 155 | * @param bool |
| 156 | * @return string |
| 157 | */ |
| 158 | function item($item, $index = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 159 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | if ($index == '') |
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 | if ( ! isset($this->config[$item])) |
| 163 | { |
| 164 | return FALSE; |
| 165 | } |
| 166 | |
| 167 | $pref = $this->config[$item]; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | if ( ! isset($this->config[$index])) |
| 172 | { |
| 173 | return FALSE; |
| 174 | } |
| 175 | |
| 176 | if ( ! isset($this->config[$index][$item])) |
| 177 | { |
| 178 | return FALSE; |
| 179 | } |
| 180 | |
| 181 | $pref = $this->config[$index][$item]; |
| 182 | } |
| 183 | |
| 184 | return $pref; |
| 185 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | |
| 187 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Fetch a config file item - adds slash after item |
| 191 | * |
| 192 | * The second parameter allows a slash to be added to the end of |
| 193 | * the item, in the case of a path. |
| 194 | * |
| 195 | * @access public |
| 196 | * @param string the config item name |
| 197 | * @param bool |
| 198 | * @return string |
| 199 | */ |
| 200 | function slash_item($item) |
| 201 | { |
| 202 | if ( ! isset($this->config[$item])) |
| 203 | { |
| 204 | return FALSE; |
| 205 | } |
| 206 | |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame^] | 207 | return rtrim($this->config[$item], '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 209 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | // -------------------------------------------------------------------- |
| 211 | |
| 212 | /** |
| 213 | * Site URL |
| 214 | * |
| 215 | * @access public |
| 216 | * @param string the URI string |
| 217 | * @return string |
| 218 | */ |
| 219 | function site_url($uri = '') |
| 220 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | if ($uri == '') |
| 222 | { |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame^] | 223 | return $this->slash_item('base_url').$this->item('index_page'); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if ($this->item('enable_query_strings') == FALSE) |
| 227 | { |
| 228 | if (is_array($uri)) |
| 229 | { |
| 230 | $uri = implode('/', $uri); |
| 231 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 233 | $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | 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] | 235 | } |
| 236 | else |
| 237 | { |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 238 | if (is_array($uri)) |
| 239 | { |
| 240 | $i = 0; |
| 241 | $str = ''; |
| 242 | foreach ($uri as $key => $val) |
| 243 | { |
| 244 | $prefix = ($i == 0) ? '' : '&'; |
| 245 | $str .= $prefix.$key.'='.$val; |
| 246 | $i++; |
| 247 | } |
| 248 | |
| 249 | $uri = $str; |
| 250 | } |
| 251 | |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame^] | 252 | return $this->slash_item('base_url').$this->item('index_page').'?'.$uri; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | // -------------------------------------------------------------------- |
| 257 | |
| 258 | /** |
| 259 | * System URL |
| 260 | * |
| 261 | * @access public |
| 262 | * @return string |
| 263 | */ |
| 264 | function system_url() |
| 265 | { |
| 266 | $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH)); |
| 267 | return $this->slash_item('base_url').end($x).'/'; |
| 268 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 269 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | // -------------------------------------------------------------------- |
| 271 | |
| 272 | /** |
| 273 | * Set a config file item |
| 274 | * |
| 275 | * @access public |
| 276 | * @param string the config item key |
| 277 | * @param string the config item value |
| 278 | * @return void |
| 279 | */ |
| 280 | function set_item($item, $value) |
| 281 | { |
| 282 | $this->config[$item] = $value; |
| 283 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 285 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 287 | /** |
| 288 | * Assign to Config |
| 289 | * |
| 290 | * This function is called by the front controller (CodeIgniter.php) |
| 291 | * after the Config class is instantiated. It permits config items |
| 292 | * to be assigned or overriden by variables contained in the index.php file |
| 293 | * |
| 294 | * @access private |
| 295 | * @param array |
| 296 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 297 | */ |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 298 | function _assign_to_config($items = array()) |
| 299 | { |
| 300 | if (is_array($items)) |
| 301 | { |
| 302 | foreach ($items as $key => $val) |
| 303 | { |
| 304 | $this->set_item($key, $val); |
| 305 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 306 | } |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 307 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // END CI_Config class |
| 311 | |
| 312 | /* End of file Config.php */ |
Derek Jones | c68dfbf | 2010-03-02 12:59:23 -0600 | [diff] [blame] | 313 | /* Location: ./system/core/Config.php */ |