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