blob: 73f1986a0d65056d1216a2609681dca0d1647616 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000026 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000027 * @link http://codeigniter.com/user_guide/libraries/config.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000028 */
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 *
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 */
45 function CI_Config()
46 {
47 $this->config =& get_config();
48 log_message('debug', "Config Class Initialized");
49 }
50
51 // --------------------------------------------------------------------
52
53 /**
54 * Load Config File
55 *
56 * @access public
57 * @param string the config file name
58 * @return boolean if the file was loaded correctly
59 */
60 function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
61 {
62 $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
63
64 if (in_array($file, $this->is_loaded, TRUE))
65 {
66 return TRUE;
67 }
68
Derek Allard73274992008-05-05 16:39:18 +000069 if (! file_exists(APPPATH.'config/'.$file.EXT))
Derek Allardd2df9bc2007-04-15 17:41:17 +000070 {
71 if ($fail_gracefully === TRUE)
72 {
73 return FALSE;
74 }
75 show_error('The configuration file '.$file.EXT.' does not exist.');
76 }
77
78 include(APPPATH.'config/'.$file.EXT);
79
Derek Allard73274992008-05-05 16:39:18 +000080 if (! isset($config) OR ! is_array($config))
Derek Allardd2df9bc2007-04-15 17:41:17 +000081 {
82 if ($fail_gracefully === TRUE)
83 {
84 return FALSE;
85 }
86 show_error('Your '.$file.EXT.' file does not appear to contain a valid configuration array.');
87 }
88
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 }
104
105 $this->is_loaded[] = $file;
106 unset($config);
107
108 log_message('debug', 'Config file loaded: config/'.$file.EXT);
109 return TRUE;
110 }
111
112 // --------------------------------------------------------------------
113
114 /**
115 * Fetch a config file item
116 *
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 {
Derek Allard73274992008-05-05 16:39:18 +0000128 if (! isset($this->config[$item]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000129 {
130 return FALSE;
131 }
132
133 $pref = $this->config[$item];
134 }
135 else
136 {
Derek Allard73274992008-05-05 16:39:18 +0000137 if (! isset($this->config[$index]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000138 {
139 return FALSE;
140 }
141
Derek Allard73274992008-05-05 16:39:18 +0000142 if (! isset($this->config[$index][$item]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000143 {
144 return FALSE;
145 }
146
147 $pref = $this->config[$index][$item];
148 }
149
150 return $pref;
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Fetch a config file item - adds slash after item
157 *
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 */
166 function slash_item($item)
167 {
Derek Allard73274992008-05-05 16:39:18 +0000168 if (! isset($this->config[$item]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000169 {
170 return FALSE;
171 }
172
173 $pref = $this->config[$item];
174
175 if ($pref != '')
176 {
177 if (ereg("/$", $pref) === FALSE)
178 {
179 $pref .= '/';
180 }
181 }
182
183 return $pref;
184 }
185
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 {
204 return $this->slash_item('base_url').$this->item('index_page');
205 }
206 else
207 {
208 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
209 return $this->slash_item('base_url').$this->slash_item('index_page').preg_replace("|^/*(.+?)/*$|", "\\1", $uri).$suffix;
210 }
211 }
212
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));
224 return $this->slash_item('base_url').end($x).'/';
225 }
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 }
241
242}
243
244// END CI_Config class
adminb0dd10f2006-08-25 17:25:49 +0000245?>