blob: 28409fbc4fdcf9c15f52f62cd84c6e1ba427d114 [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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
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 *
admine334c472006-10-21 19:44:22 +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 {
admin49439ff2006-10-30 04:39:12 +000047 $this->config =& get_config();
adminb0dd10f2006-08-25 17:25:49 +000048 log_message('debug', "Config Class Initialized");
adminbd6bee72006-10-21 19:39:00 +000049 }
adminb0dd10f2006-08-25 17:25:49 +000050
51 // --------------------------------------------------------------------
52
53 /**
54 * Load Config File
55 *
56 * @access public
57 * @param string the config file name
admin1082bdd2006-08-27 19:32:02 +000058 * @return boolean if the file was loaded correctly
adminb0dd10f2006-08-25 17:25:49 +000059 */
admin671dc752006-09-02 02:36:03 +000060 function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
adminb0dd10f2006-08-25 17:25:49 +000061 {
62 $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
63
adminee54c112006-09-28 17:13:38 +000064 if (in_array($file, $this->is_loaded, TRUE))
admine334c472006-10-21 19:44:22 +000065 {
admin1082bdd2006-08-27 19:32:02 +000066 return TRUE;
adminb0dd10f2006-08-25 17:25:49 +000067 }
admin671dc752006-09-02 02:36:03 +000068
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 }
adminb0dd10f2006-08-25 17:25:49 +000077
adminf3a62042006-10-29 20:24:11 +000078 include(APPPATH.'config/'.$file.EXT);
adminb0dd10f2006-08-25 17:25:49 +000079
80 if ( ! isset($config) OR ! is_array($config))
81 {
admin671dc752006-09-02 02:36:03 +000082 if ($fail_gracefully === TRUE)
83 {
84 return FALSE;
85 }
adminb0dd10f2006-08-25 17:25:49 +000086 show_error('Your '.$file.EXT.' file does not appear to contain a valid configuration array.');
87 }
88
admin671dc752006-09-02 02:36:03 +000089 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 }
adminb0dd10f2006-08-25 17:25:49 +0000104
105 $this->is_loaded[] = $file;
106 unset($config);
107
108 log_message('debug', 'Config file loaded: config/'.$file.EXT);
admin1082bdd2006-08-27 19:32:02 +0000109 return TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000110 }
adminb0dd10f2006-08-25 17:25:49 +0000111
112 // --------------------------------------------------------------------
113
114 /**
115 * Fetch a config file item
116 *
admin671dc752006-09-02 02:36:03 +0000117 *
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 {
admine334c472006-10-21 19:44:22 +0000128 if ( ! isset($this->config[$item]))
admin671dc752006-09-02 02:36:03 +0000129 {
130 return FALSE;
131 }
132
133 $pref = $this->config[$item];
134 }
135 else
136 {
admine334c472006-10-21 19:44:22 +0000137 if ( ! isset($this->config[$index]))
admin671dc752006-09-02 02:36:03 +0000138 {
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
admine334c472006-10-21 19:44:22 +0000150 return $pref;
admin671dc752006-09-02 02:36:03 +0000151 }
admin671dc752006-09-02 02:36:03 +0000152
153 // --------------------------------------------------------------------
154
155 /**
156 * Fetch a config file item - adds slash after item
157 *
adminb0dd10f2006-08-25 17:25:49 +0000158 * 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 */
admin671dc752006-09-02 02:36:03 +0000166 function slash_item($item)
adminb0dd10f2006-08-25 17:25:49 +0000167 {
admine334c472006-10-21 19:44:22 +0000168 if ( ! isset($this->config[$item]))
adminb0dd10f2006-08-25 17:25:49 +0000169 {
170 return FALSE;
171 }
172
173 $pref = $this->config[$item];
174
admin671dc752006-09-02 02:36:03 +0000175 if ($pref != '')
176 {
177 if (ereg("/$", $pref) === FALSE)
178 {
179 $pref .= '/';
180 }
adminb0dd10f2006-08-25 17:25:49 +0000181 }
admine334c472006-10-21 19:44:22 +0000182
183 return $pref;
adminb0dd10f2006-08-25 17:25:49 +0000184 }
admin671dc752006-09-02 02:36:03 +0000185
adminb0dd10f2006-08-25 17:25:49 +0000186 // --------------------------------------------------------------------
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))
admine334c472006-10-21 19:44:22 +0000198 {
adminb0dd10f2006-08-25 17:25:49 +0000199 $uri = implode('/', $uri);
200 }
201
202 if ($uri == '')
203 {
admin6cd2ee92006-09-04 07:11:16 +0000204 return $this->slash_item('base_url').$this->item('index_page');
adminb0dd10f2006-08-25 17:25:49 +0000205 }
206 else
207 {
208 $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
admin6cd2ee92006-09-04 07:11:16 +0000209 return $this->slash_item('base_url').$this->slash_item('index_page').preg_replace("|^/*(.+?)/*$|", "\\1", $uri).$suffix;
adminb0dd10f2006-08-25 17:25:49 +0000210 }
211 }
adminbd6bee72006-10-21 19:39:00 +0000212
adminb0dd10f2006-08-25 17:25:49 +0000213 // --------------------------------------------------------------------
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));
admin813711e2006-09-04 07:13:57 +0000224 return $this->slash_item('base_url').end($x).'/';
adminb0dd10f2006-08-25 17:25:49 +0000225 }
adminb0dd10f2006-08-25 17:25:49 +0000226
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 }
adminb0dd10f2006-08-25 17:25:49 +0000241
242}
243
244// END CI_Config class
245?>