blob: 027f6bf8e1be0fc90e7043e4dfed180f27ee46e7 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +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
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
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 */
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
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 }
77
78 include(APPPATH.'config/'.$file.EXT);
79
80 if ( ! isset($config) OR ! is_array($config))
81 {
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 {
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 }
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 {
168 if ( ! isset($this->config[$item]))
169 {
170 return FALSE;
171 }
172
173 $pref = $this->config[$item];
174
175 if ($pref != '' && substr($pref, -1) != '/')
176 {
177 $pref .= '/';
178 }
179
180 return $pref;
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Site URL
187 *
188 * @access public
189 * @param string the URI string
190 * @return string
191 */
192 function site_url($uri = '')
193 {
194 if (is_array($uri))
195 {
196 $uri = implode('/', $uri);
197 }
198
199 if ($uri == '')
200 {
201 return $this->slash_item('base_url').$this->item('index_page');
202 }
203 else
204 {
205 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
206 return $this->slash_item('base_url').$this->slash_item('index_page').preg_replace("|^/*(.+?)/*$|", "\\1", $uri).$suffix;
207 }
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * System URL
214 *
215 * @access public
216 * @return string
217 */
218 function system_url()
219 {
220 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
221 return $this->slash_item('base_url').end($x).'/';
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Set a config file item
228 *
229 * @access public
230 * @param string the config item key
231 * @param string the config item value
232 * @return void
233 */
234 function set_item($item, $value)
235 {
236 $this->config[$item] = $value;
237 }
238
239}
240
241// END CI_Config class
242
243/* End of file Config.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000244/* Location: ./system/libraries/Config.php */