admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Code Igniter 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 Rick Ellis |
| 27 | * @link http://www.codeigniter.com/user_guide/libraries/config.html |
| 28 | */ |
| 29 | class CI_Config { |
| 30 | |
| 31 | var $config = array(); |
| 32 | var $is_loaded = array(); |
| 33 | |
| 34 | /** |
| 35 | * Constructor |
| 36 | * |
| 37 | * Sets the $config data from the primary config.php file as a class variable |
| 38 | * |
admin | 671dc75 | 2006-09-02 02:36:03 +0000 | [diff] [blame] | 39 | * @access public |
| 40 | * @param string the config file name |
| 41 | * @param boolean if configuration values should be loaded into their own section |
| 42 | * @param boolean true if errors should just return false, false if an error message should be displayed |
| 43 | * @return boolean if the file was successfully loaded or not |
| 44 | */ |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 45 | function CI_Config() |
| 46 | { |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 47 | $this->config = get_config(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 48 | log_message('debug', "Config Class Initialized"); |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame^] | 49 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 50 | |
| 51 | // -------------------------------------------------------------------- |
| 52 | |
| 53 | /** |
| 54 | * Load Config File |
| 55 | * |
| 56 | * @access public |
| 57 | * @param string the config file name |
admin | 1082bdd | 2006-08-27 19:32:02 +0000 | [diff] [blame] | 58 | * @return boolean if the file was loaded correctly |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 59 | */ |
admin | 671dc75 | 2006-09-02 02:36:03 +0000 | [diff] [blame] | 60 | function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 61 | { |
| 62 | $file = ($file == '') ? 'config' : str_replace(EXT, '', $file); |
| 63 | |
admin | ee54c11 | 2006-09-28 17:13:38 +0000 | [diff] [blame] | 64 | if (in_array($file, $this->is_loaded, TRUE)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 65 | { |
admin | 1082bdd | 2006-08-27 19:32:02 +0000 | [diff] [blame] | 66 | return TRUE; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 67 | } |
admin | 671dc75 | 2006-09-02 02:36:03 +0000 | [diff] [blame] | 68 | |
| 69 | if ( ! file_exists(APPPATH.'config/'.$file.EXT)) |
| 70 | { |
| 71 | if ($fail_gracefully === TRUE) |
| 72 | { |
| 73 | return FALSE; |
| 74 | } |
| 75 | show_error('The configuration file '.$file.EXT.' does not exist.'); |
| 76 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 77 | |
| 78 | include_once(APPPATH.'config/'.$file.EXT); |
| 79 | |
| 80 | if ( ! isset($config) OR ! is_array($config)) |
| 81 | { |
admin | 671dc75 | 2006-09-02 02:36:03 +0000 | [diff] [blame] | 82 | if ($fail_gracefully === TRUE) |
| 83 | { |
| 84 | return FALSE; |
| 85 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 86 | show_error('Your '.$file.EXT.' file does not appear to contain a valid configuration array.'); |
| 87 | } |
| 88 | |
admin | 671dc75 | 2006-09-02 02:36:03 +0000 | [diff] [blame] | 89 | if ($use_sections === TRUE) |
| 90 | { |
| 91 | if (isset($this->config[$file])) |
| 92 | { |
| 93 | $this->config[$file] = array_merge($this->config[$file], $config); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | $this->config[$file] = $config; |
| 98 | } |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | $this->config = array_merge($this->config, $config); |
| 103 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 104 | |
| 105 | $this->is_loaded[] = $file; |
| 106 | unset($config); |
| 107 | |
| 108 | log_message('debug', 'Config file loaded: config/'.$file.EXT); |
admin | 1082bdd | 2006-08-27 19:32:02 +0000 | [diff] [blame] | 109 | return TRUE; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 110 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 111 | |
| 112 | // -------------------------------------------------------------------- |
| 113 | |
| 114 | /** |
| 115 | * Fetch a config file item |
| 116 | * |
admin | 671dc75 | 2006-09-02 02:36:03 +0000 | [diff] [blame] | 117 | * |
| 118 | * @access public |
| 119 | * @param string the config item name |
| 120 | * @param string the index name |
| 121 | * @param bool |
| 122 | * @return string |
| 123 | */ |
| 124 | function item($item, $index = '') |
| 125 | { |
| 126 | if ($index == '') |
| 127 | { |
| 128 | if ( ! isset($this->config[$item])) |
| 129 | { |
| 130 | return FALSE; |
| 131 | } |
| 132 | |
| 133 | $pref = $this->config[$item]; |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | if ( ! isset($this->config[$index])) |
| 138 | { |
| 139 | return FALSE; |
| 140 | } |
| 141 | |
| 142 | if ( ! isset($this->config[$index][$item])) |
| 143 | { |
| 144 | return FALSE; |
| 145 | } |
| 146 | |
| 147 | $pref = $this->config[$index][$item]; |
| 148 | } |
| 149 | |
| 150 | return $pref; |
| 151 | } |
admin | 671dc75 | 2006-09-02 02:36:03 +0000 | [diff] [blame] | 152 | |
| 153 | // -------------------------------------------------------------------- |
| 154 | |
| 155 | /** |
| 156 | * Fetch a config file item - adds slash after item |
| 157 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 158 | * The second parameter allows a slash to be added to the end of |
| 159 | * the item, in the case of a path. |
| 160 | * |
| 161 | * @access public |
| 162 | * @param string the config item name |
| 163 | * @param bool |
| 164 | * @return string |
| 165 | */ |
admin | 671dc75 | 2006-09-02 02:36:03 +0000 | [diff] [blame] | 166 | function slash_item($item) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 167 | { |
| 168 | if ( ! isset($this->config[$item])) |
| 169 | { |
| 170 | return FALSE; |
| 171 | } |
| 172 | |
| 173 | $pref = $this->config[$item]; |
| 174 | |
admin | 671dc75 | 2006-09-02 02:36:03 +0000 | [diff] [blame] | 175 | if ($pref != '') |
| 176 | { |
| 177 | if (ereg("/$", $pref) === FALSE) |
| 178 | { |
| 179 | $pref .= '/'; |
| 180 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 181 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 182 | |
| 183 | return $pref; |
| 184 | } |
admin | 671dc75 | 2006-09-02 02:36:03 +0000 | [diff] [blame] | 185 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 186 | // -------------------------------------------------------------------- |
| 187 | |
| 188 | /** |
| 189 | * Site URL |
| 190 | * |
| 191 | * @access public |
| 192 | * @param string the URI string |
| 193 | * @return string |
| 194 | */ |
| 195 | function site_url($uri = '') |
| 196 | { |
| 197 | if (is_array($uri)) |
| 198 | { |
| 199 | $uri = implode('/', $uri); |
| 200 | } |
| 201 | |
| 202 | if ($uri == '') |
| 203 | { |
admin | 6cd2ee9 | 2006-09-04 07:11:16 +0000 | [diff] [blame] | 204 | return $this->slash_item('base_url').$this->item('index_page'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 205 | } |
| 206 | else |
| 207 | { |
| 208 | $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix'); |
admin | 6cd2ee9 | 2006-09-04 07:11:16 +0000 | [diff] [blame] | 209 | return $this->slash_item('base_url').$this->slash_item('index_page').preg_replace("|^/*(.+?)/*$|", "\\1", $uri).$suffix; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame^] | 212 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 213 | // -------------------------------------------------------------------- |
| 214 | |
| 215 | /** |
| 216 | * System URL |
| 217 | * |
| 218 | * @access public |
| 219 | * @return string |
| 220 | */ |
| 221 | function system_url() |
| 222 | { |
| 223 | $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH)); |
admin | 813711e | 2006-09-04 07:13:57 +0000 | [diff] [blame] | 224 | return $this->slash_item('base_url').end($x).'/'; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 225 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 226 | |
| 227 | // -------------------------------------------------------------------- |
| 228 | |
| 229 | /** |
| 230 | * Set a config file item |
| 231 | * |
| 232 | * @access public |
| 233 | * @param string the config item key |
| 234 | * @param string the config item value |
| 235 | * @return void |
| 236 | */ |
| 237 | function set_item($item, $value) |
| 238 | { |
| 239 | $this->config[$item] = $value; |
| 240 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 241 | |
| 242 | } |
| 243 | |
| 244 | // END CI_Config class |
| 245 | ?> |