blob: f7f813f86b2d534ec291a20b836f3b3ca8e24561 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?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 */
29class 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 *
admin671dc752006-09-02 02:36:03 +000039 * @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 */
adminb0dd10f2006-08-25 17:25:49 +000045 function CI_Config()
46 {
47 $this->config =& _get_config();
48 log_message('debug', "Config Class Initialized");
49 }
50 // END CI_Config()
51
52
53 // --------------------------------------------------------------------
54
55 /**
56 * Load Config File
57 *
58 * @access public
59 * @param string the config file name
admin1082bdd2006-08-27 19:32:02 +000060 * @return boolean if the file was loaded correctly
adminb0dd10f2006-08-25 17:25:49 +000061 */
admin671dc752006-09-02 02:36:03 +000062 function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
adminb0dd10f2006-08-25 17:25:49 +000063 {
64 $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
65
66 if (in_array($file, $this->is_loaded))
67 {
admin1082bdd2006-08-27 19:32:02 +000068 return TRUE;
adminb0dd10f2006-08-25 17:25:49 +000069 }
admin671dc752006-09-02 02:36:03 +000070
71 if ( ! file_exists(APPPATH.'config/'.$file.EXT))
72 {
73 if ($fail_gracefully === TRUE)
74 {
75 return FALSE;
76 }
77 show_error('The configuration file '.$file.EXT.' does not exist.');
78 }
adminb0dd10f2006-08-25 17:25:49 +000079
80 include_once(APPPATH.'config/'.$file.EXT);
81
82 if ( ! isset($config) OR ! is_array($config))
83 {
admin671dc752006-09-02 02:36:03 +000084 if ($fail_gracefully === TRUE)
85 {
86 return FALSE;
87 }
adminb0dd10f2006-08-25 17:25:49 +000088 show_error('Your '.$file.EXT.' file does not appear to contain a valid configuration array.');
89 }
90
admin671dc752006-09-02 02:36:03 +000091 if ($use_sections === TRUE)
92 {
93 if (isset($this->config[$file]))
94 {
95 $this->config[$file] = array_merge($this->config[$file], $config);
96 }
97 else
98 {
99 $this->config[$file] = $config;
100 }
101 }
102 else
103 {
104 $this->config = array_merge($this->config, $config);
105 }
adminb0dd10f2006-08-25 17:25:49 +0000106
107 $this->is_loaded[] = $file;
108 unset($config);
109
110 log_message('debug', 'Config file loaded: config/'.$file.EXT);
admin1082bdd2006-08-27 19:32:02 +0000111 return TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000112 }
113 // END load()
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Fetch a config file item
119 *
admin671dc752006-09-02 02:36:03 +0000120 *
121 * @access public
122 * @param string the config item name
123 * @param string the index name
124 * @param bool
125 * @return string
126 */
127 function item($item, $index = '')
128 {
129 if ($index == '')
130 {
131 if ( ! isset($this->config[$item]))
132 {
133 return FALSE;
134 }
135
136 $pref = $this->config[$item];
137 }
138 else
139 {
140 if ( ! isset($this->config[$index]))
141 {
142 return FALSE;
143 }
144
145 if ( ! isset($this->config[$index][$item]))
146 {
147 return FALSE;
148 }
149
150 $pref = $this->config[$index][$item];
151 }
152
153 return $pref;
154 }
155 // END item()
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Fetch a config file item - adds slash after item
161 *
adminb0dd10f2006-08-25 17:25:49 +0000162 * The second parameter allows a slash to be added to the end of
163 * the item, in the case of a path.
164 *
165 * @access public
166 * @param string the config item name
167 * @param bool
168 * @return string
169 */
admin671dc752006-09-02 02:36:03 +0000170 function slash_item($item)
adminb0dd10f2006-08-25 17:25:49 +0000171 {
172 if ( ! isset($this->config[$item]))
173 {
174 return FALSE;
175 }
176
177 $pref = $this->config[$item];
178
admin671dc752006-09-02 02:36:03 +0000179 if ($pref != '')
180 {
181 if (ereg("/$", $pref) === FALSE)
182 {
183 $pref .= '/';
184 }
adminb0dd10f2006-08-25 17:25:49 +0000185 }
adminb0dd10f2006-08-25 17:25:49 +0000186
187 return $pref;
188 }
189 // END item()
190
admin671dc752006-09-02 02:36:03 +0000191
192
adminb0dd10f2006-08-25 17:25:49 +0000193 // --------------------------------------------------------------------
194
195 /**
196 * Site URL
197 *
198 * @access public
199 * @param string the URI string
200 * @return string
201 */
202 function site_url($uri = '')
203 {
204 if (is_array($uri))
205 {
206 $uri = implode('/', $uri);
207 }
208
209 if ($uri == '')
210 {
211 return $this->item('base_url', 1).$this->item('index_page');
212 }
213 else
214 {
215 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
216 return $this->item('base_url', 1).$this->item('index_page', 1).preg_replace("|^/*(.+?)/*$|", "\\1", $uri).$suffix;
217 }
218 }
219 // END site_url()
220
221 // --------------------------------------------------------------------
222
223 /**
224 * System URL
225 *
226 * @access public
227 * @return string
228 */
229 function system_url()
230 {
231 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
232 return $this->item('base_url', 1).end($x).'/';
233 }
234 // END system_url()
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Set a config file item
240 *
241 * @access public
242 * @param string the config item key
243 * @param string the config item value
244 * @return void
245 */
246 function set_item($item, $value)
247 {
248 $this->config[$item] = $value;
249 }
250 // END set_item()
251
252}
253
254// END CI_Config class
255?>