Provide better error reporting and error checking when updating a network
[www-register-wizard.git] / codeigniter / Common.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  * Common Functions\r
20  *\r
21  * Loads the base classes and executes the request.\r
22  *\r
23  * @package             CodeIgniter\r
24  * @subpackage  codeigniter\r
25  * @category    Common Functions\r
26  * @author              ExpressionEngine Dev Team\r
27  * @link                http://codeigniter.com/user_guide/\r
28  */\r
29 \r
30 // ------------------------------------------------------------------------\r
31 \r
32 /**\r
33  * Tests for file writability\r
34  *\r
35  * is_writable() returns TRUE on Windows servers when you really can't write to \r
36  * the file, based on the read-only attribute.  is_writable() is also unreliable\r
37  * on Unix servers if safe_mode is on. \r
38  *\r
39  * @access      private\r
40  * @return      void\r
41  */\r
42 function is_really_writable($file)\r
43 {       \r
44         // If we're on a Unix server with safe_mode off we call is_writable\r
45         if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)\r
46         {\r
47                 return is_writable($file);\r
48         }\r
49 \r
50         // For windows servers and safe_mode "on" installations we'll actually\r
51         // write a file then read it.  Bah...\r
52         if (is_dir($file))\r
53         {\r
54                 $file = rtrim($file, '/').'/'.md5(rand(1,100));\r
55 \r
56                 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)\r
57                 {\r
58                         return FALSE;\r
59                 }\r
60 \r
61                 fclose($fp);\r
62                 @chmod($file, DIR_WRITE_MODE);\r
63                 @unlink($file);\r
64                 return TRUE;\r
65         }\r
66         elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)\r
67         {\r
68                 return FALSE;\r
69         }\r
70 \r
71         fclose($fp);\r
72         return TRUE;\r
73 }\r
74 \r
75 // ------------------------------------------------------------------------\r
76 \r
77 /**\r
78 * Class registry\r
79 *\r
80 * This function acts as a singleton.  If the requested class does not\r
81 * exist it is instantiated and set to a static variable.  If it has\r
82 * previously been instantiated the variable is returned.\r
83 *\r
84 * @access       public\r
85 * @param        string  the class name being requested\r
86 * @param        bool    optional flag that lets classes get loaded but not instantiated\r
87 * @return       object\r
88 */\r
89 function &load_class($class, $instantiate = TRUE)\r
90 {\r
91         static $objects = array();\r
92 \r
93         // Does the class exist?  If so, we're done...\r
94         if (isset($objects[$class]))\r
95         {\r
96                 return $objects[$class];\r
97         }\r
98 \r
99         // If the requested class does not exist in the application/libraries\r
100         // folder we'll load the native class from the system/libraries folder. \r
101         if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))\r
102         {\r
103                 require(BASEPATH.'libraries/'.$class.EXT);\r
104                 require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);\r
105                 $is_subclass = TRUE;\r
106         }\r
107         else\r
108         {\r
109                 if (file_exists(APPPATH.'libraries/'.$class.EXT))\r
110                 {\r
111                         require(APPPATH.'libraries/'.$class.EXT);\r
112                         $is_subclass = FALSE;\r
113                 }\r
114                 else\r
115                 {\r
116                         require(BASEPATH.'libraries/'.$class.EXT);\r
117                         $is_subclass = FALSE;\r
118                 }\r
119         }\r
120 \r
121         if ($instantiate == FALSE)\r
122         {\r
123                 $objects[$class] = TRUE;\r
124                 return $objects[$class];\r
125         }\r
126 \r
127         if ($is_subclass == TRUE)\r
128         {\r
129                 $name = config_item('subclass_prefix').$class;\r
130                 $objects[$class] =& new $name();\r
131                 return $objects[$class];\r
132         }\r
133 \r
134         $name = ($class != 'Controller') ? 'CI_'.$class : $class;\r
135         \r
136         $objects[$class] =& new $name();\r
137         return $objects[$class];\r
138 }\r
139 \r
140 /**\r
141 * Loads the main config.php file\r
142 *\r
143 * @access       private\r
144 * @return       array\r
145 */\r
146 function &get_config()\r
147 {\r
148         static $main_conf;\r
149 \r
150         if ( ! isset($main_conf))\r
151         {\r
152                 if ( ! file_exists(APPPATH.'config/config'.EXT))\r
153                 {\r
154                         exit('The configuration file config'.EXT.' does not exist.');\r
155                 }\r
156 \r
157                 require(APPPATH.'config/config'.EXT);\r
158 \r
159                 if ( ! isset($config) OR ! is_array($config))\r
160                 {\r
161                         exit('Your config file does not appear to be formatted correctly.');\r
162                 }\r
163 \r
164                 $main_conf[0] =& $config;\r
165         }\r
166         return $main_conf[0];\r
167 }\r
168 \r
169 /**\r
170 * Gets a config item\r
171 *\r
172 * @access       public\r
173 * @return       mixed\r
174 */\r
175 function config_item($item)\r
176 {\r
177         static $config_item = array();\r
178 \r
179         if ( ! isset($config_item[$item]))\r
180         {\r
181                 $config =& get_config();\r
182 \r
183                 if ( ! isset($config[$item]))\r
184                 {\r
185                         return FALSE;\r
186                 }\r
187                 $config_item[$item] = $config[$item];\r
188         }\r
189 \r
190         return $config_item[$item];\r
191 }\r
192 \r
193 \r
194 /**\r
195 * Error Handler\r
196 *\r
197 * This function lets us invoke the exception class and\r
198 * display errors using the standard error template located\r
199 * in application/errors/errors.php\r
200 * This function will send the error page directly to the\r
201 * browser and exit.\r
202 *\r
203 * @access       public\r
204 * @return       void\r
205 */\r
206 function show_error($message)\r
207 {\r
208         $error =& load_class('Exceptions');\r
209         echo $error->show_error('An Error Was Encountered', $message);\r
210         exit;\r
211 }\r
212 \r
213 \r
214 /**\r
215 * 404 Page Handler\r
216 *\r
217 * This function is similar to the show_error() function above\r
218 * However, instead of the standard error template it displays\r
219 * 404 errors.\r
220 *\r
221 * @access       public\r
222 * @return       void\r
223 */\r
224 function show_404($page = '')\r
225 {\r
226         $error =& load_class('Exceptions');\r
227         $error->show_404($page);\r
228         exit;\r
229 }\r
230 \r
231 \r
232 /**\r
233 * Error Logging Interface\r
234 *\r
235 * We use this as a simple mechanism to access the logging\r
236 * class and send messages to be logged.\r
237 *\r
238 * @access       public\r
239 * @return       void\r
240 */\r
241 function log_message($level = 'error', $message, $php_error = FALSE)\r
242 {\r
243         static $LOG;\r
244         \r
245         $config =& get_config();\r
246         if ($config['log_threshold'] == 0)\r
247         {\r
248                 return;\r
249         }\r
250 \r
251         $LOG =& load_class('Log');\r
252         $LOG->write_log($level, $message, $php_error);\r
253 }\r
254 \r
255 /**\r
256 * Exception Handler\r
257 *\r
258 * This is the custom exception handler that is declaired at the top\r
259 * of Codeigniter.php.  The main reason we use this is permit\r
260 * PHP errors to be logged in our own log files since we may\r
261 * not have access to server logs. Since this function\r
262 * effectively intercepts PHP errors, however, we also need\r
263 * to display errors based on the current error_reporting level.\r
264 * We do that with the use of a PHP error template.\r
265 *\r
266 * @access       private\r
267 * @return       void\r
268 */\r
269 function _exception_handler($severity, $message, $filepath, $line)\r
270 {       \r
271          // We don't bother with "strict" notices since they will fill up\r
272          // the log file with information that isn't normally very\r
273          // helpful.  For example, if you are running PHP 5 and you\r
274          // use version 4 style class functions (without prefixes\r
275          // like "public", "private", etc.) you'll get notices telling\r
276          // you that these have been deprecated.\r
277         \r
278         if ($severity == E_STRICT)\r
279         {\r
280                 return;\r
281         }\r
282 \r
283         $error =& load_class('Exceptions');\r
284 \r
285         // Should we display the error?\r
286         // We'll get the current error_reporting level and add its bits\r
287         // with the severity bits to find out.\r
288         \r
289         if (($severity & error_reporting()) == $severity)\r
290         {\r
291                 $error->show_php_error($severity, $message, $filepath, $line);\r
292         }\r
293         \r
294         // Should we log the error?  No?  We're done...\r
295         $config =& get_config();\r
296         if ($config['log_threshold'] == 0)\r
297         {\r
298                 return;\r
299         }\r
300 \r
301         $error->log_exception($severity, $message, $filepath, $line);\r
302 }\r
303 \r
304 \r
305 \r
306 /* End of file Common.php */\r
307 /* Location: ./system/codeigniter/Common.php */