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