blob: 15204492ef26a000cc240750d05ac6ddf4d5e53e [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
Derek Jonesfc395a12009-04-22 14:15:09 +00009 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @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/**
Derek Jones086ee5a2009-07-28 14:42:12 +000033* Determines if the current version of PHP is greater then the supplied value
34*
35* Since there are a few places where we conditionally test for PHP > 5
36* we'll set a static variable.
37*
38* @access public
Derek Jones77b513b2009-08-06 14:39:25 +000039* @param string
Derek Jones6d3c2c32009-08-06 14:43:16 +000040* @return bool
Derek Jones086ee5a2009-07-28 14:42:12 +000041*/
Derek Jones5bcfd2e2009-07-28 14:42:42 +000042function is_php($version = '5.0.0')
43{
44 static $_is_php;
Derek Jones77b513b2009-08-06 14:39:25 +000045 $version = (string)$version;
46
Derek Jones5bcfd2e2009-07-28 14:42:42 +000047 if ( ! isset($_is_php[$version]))
Derek Jones086ee5a2009-07-28 14:42:12 +000048 {
Derek Jones5bcfd2e2009-07-28 14:42:42 +000049 $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
Derek Jones086ee5a2009-07-28 14:42:12 +000050 }
51
Derek Jones5bcfd2e2009-07-28 14:42:42 +000052 return $_is_php[$version];
53}
54
Derek Jones086ee5a2009-07-28 14:42:12 +000055// ------------------------------------------------------------------------
56
57/**
Derek Allard2067d1a2008-11-13 22:59:24 +000058 * Tests for file writability
59 *
60 * is_writable() returns TRUE on Windows servers when you really can't write to
61 * the file, based on the read-only attribute. is_writable() is also unreliable
62 * on Unix servers if safe_mode is on.
63 *
64 * @access private
65 * @return void
66 */
67function is_really_writable($file)
68{
69 // If we're on a Unix server with safe_mode off we call is_writable
70 if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
71 {
72 return is_writable($file);
73 }
74
75 // For windows servers and safe_mode "on" installations we'll actually
76 // write a file then read it. Bah...
77 if (is_dir($file))
78 {
79 $file = rtrim($file, '/').'/'.md5(rand(1,100));
80
81 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
82 {
83 return FALSE;
84 }
85
86 fclose($fp);
87 @chmod($file, DIR_WRITE_MODE);
88 @unlink($file);
89 return TRUE;
90 }
91 elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
92 {
93 return FALSE;
94 }
95
96 fclose($fp);
97 return TRUE;
98}
99
100// ------------------------------------------------------------------------
101
102/**
103* Class registry
104*
105* This function acts as a singleton. If the requested class does not
106* exist it is instantiated and set to a static variable. If it has
107* previously been instantiated the variable is returned.
108*
109* @access public
110* @param string the class name being requested
111* @param bool optional flag that lets classes get loaded but not instantiated
112* @return object
113*/
114function &load_class($class, $instantiate = TRUE)
115{
116 static $objects = array();
117
118 // Does the class exist? If so, we're done...
119 if (isset($objects[$class]))
120 {
121 return $objects[$class];
122 }
123
124 // If the requested class does not exist in the application/libraries
125 // folder we'll load the native class from the system/libraries folder.
126 if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
127 {
128 require(BASEPATH.'libraries/'.$class.EXT);
129 require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
130 $is_subclass = TRUE;
131 }
132 else
133 {
134 if (file_exists(APPPATH.'libraries/'.$class.EXT))
135 {
136 require(APPPATH.'libraries/'.$class.EXT);
137 $is_subclass = FALSE;
138 }
139 else
140 {
141 require(BASEPATH.'libraries/'.$class.EXT);
142 $is_subclass = FALSE;
143 }
144 }
145
146 if ($instantiate == FALSE)
147 {
148 $objects[$class] = TRUE;
149 return $objects[$class];
150 }
151
152 if ($is_subclass == TRUE)
153 {
154 $name = config_item('subclass_prefix').$class;
Derek Jonesf0a9b332009-07-29 14:19:18 +0000155
156 $objects[$class] =& instantiate_class(new $name());
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 return $objects[$class];
158 }
159
160 $name = ($class != 'Controller') ? 'CI_'.$class : $class;
Derek Jonesf0a9b332009-07-29 14:19:18 +0000161
162 $objects[$class] =& instantiate_class(new $name());
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 return $objects[$class];
164}
165
166/**
Derek Jonesf0a9b332009-07-29 14:19:18 +0000167 * Instantiate Class
168 *
169 * Returns a new class object by reference, used by load_class() and the DB class.
170 * Required to retain PHP 4 compatibility and also not make PHP 5.3 cry.
171 *
172 * Use: $obj =& instantiate_class(new Foo());
173 *
174 * @access public
175 * @param object
176 * @return object
177 */
178function &instantiate_class(&$class_object)
179{
180 return $class_object;
181}
182
183/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000184* Loads the main config.php file
185*
186* @access private
187* @return array
188*/
189function &get_config()
190{
191 static $main_conf;
192
193 if ( ! isset($main_conf))
194 {
195 if ( ! file_exists(APPPATH.'config/config'.EXT))
196 {
197 exit('The configuration file config'.EXT.' does not exist.');
198 }
199
200 require(APPPATH.'config/config'.EXT);
201
202 if ( ! isset($config) OR ! is_array($config))
203 {
204 exit('Your config file does not appear to be formatted correctly.');
205 }
206
207 $main_conf[0] =& $config;
208 }
209 return $main_conf[0];
210}
211
212/**
213* Gets a config item
214*
215* @access public
216* @return mixed
217*/
218function config_item($item)
219{
220 static $config_item = array();
221
222 if ( ! isset($config_item[$item]))
223 {
224 $config =& get_config();
225
226 if ( ! isset($config[$item]))
227 {
228 return FALSE;
229 }
230 $config_item[$item] = $config[$item];
231 }
232
233 return $config_item[$item];
234}
235
236
237/**
238* Error Handler
239*
240* This function lets us invoke the exception class and
241* display errors using the standard error template located
242* in application/errors/errors.php
243* This function will send the error page directly to the
244* browser and exit.
245*
246* @access public
247* @return void
248*/
Derek Jones817163a2009-07-11 17:05:58 +0000249function show_error($message, $status_code = 500)
Derek Allard2067d1a2008-11-13 22:59:24 +0000250{
251 $error =& load_class('Exceptions');
Derek Jones817163a2009-07-11 17:05:58 +0000252 echo $error->show_error('An Error Was Encountered', $message, 'error_general', $status_code);
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 exit;
254}
255
256
257/**
258* 404 Page Handler
259*
260* This function is similar to the show_error() function above
261* However, instead of the standard error template it displays
262* 404 errors.
263*
264* @access public
265* @return void
266*/
267function show_404($page = '')
268{
269 $error =& load_class('Exceptions');
270 $error->show_404($page);
271 exit;
272}
273
274
275/**
276* Error Logging Interface
277*
278* We use this as a simple mechanism to access the logging
279* class and send messages to be logged.
280*
281* @access public
282* @return void
283*/
284function log_message($level = 'error', $message, $php_error = FALSE)
285{
286 static $LOG;
287
288 $config =& get_config();
289 if ($config['log_threshold'] == 0)
290 {
291 return;
292 }
293
294 $LOG =& load_class('Log');
295 $LOG->write_log($level, $message, $php_error);
296}
297
Derek Jones817163a2009-07-11 17:05:58 +0000298
299/**
300 * Set HTTP Status Header
301 *
302 * @access public
303 * @param int the status code
304 * @param string
305 * @return void
306 */
307function set_status_header($code = 200, $text = '')
308{
309 $stati = array(
310 200 => 'OK',
311 201 => 'Created',
312 202 => 'Accepted',
313 203 => 'Non-Authoritative Information',
314 204 => 'No Content',
315 205 => 'Reset Content',
316 206 => 'Partial Content',
317
318 300 => 'Multiple Choices',
319 301 => 'Moved Permanently',
320 302 => 'Found',
321 304 => 'Not Modified',
322 305 => 'Use Proxy',
323 307 => 'Temporary Redirect',
324
325 400 => 'Bad Request',
326 401 => 'Unauthorized',
327 403 => 'Forbidden',
328 404 => 'Not Found',
329 405 => 'Method Not Allowed',
330 406 => 'Not Acceptable',
331 407 => 'Proxy Authentication Required',
332 408 => 'Request Timeout',
333 409 => 'Conflict',
334 410 => 'Gone',
335 411 => 'Length Required',
336 412 => 'Precondition Failed',
337 413 => 'Request Entity Too Large',
338 414 => 'Request-URI Too Long',
339 415 => 'Unsupported Media Type',
340 416 => 'Requested Range Not Satisfiable',
341 417 => 'Expectation Failed',
342
343 500 => 'Internal Server Error',
344 501 => 'Not Implemented',
345 502 => 'Bad Gateway',
346 503 => 'Service Unavailable',
347 504 => 'Gateway Timeout',
348 505 => 'HTTP Version Not Supported'
349 );
350
351 if ($code == '' OR ! is_numeric($code))
352 {
353 show_error('Status codes must be numeric', 500);
354 }
355
356 if (isset($stati[$code]) AND $text == '')
357 {
358 $text = $stati[$code];
359 }
360
361 if ($text == '')
362 {
363 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
364 }
365
366 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
367
368 if (substr(php_sapi_name(), 0, 3) == 'cgi')
369 {
370 header("Status: {$code} {$text}", TRUE);
371 }
372 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
373 {
374 header($server_protocol." {$code} {$text}", TRUE, $code);
375 }
376 else
377 {
378 header("HTTP/1.1 {$code} {$text}", TRUE, $code);
379 }
380}
381
382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383/**
384* Exception Handler
385*
386* This is the custom exception handler that is declaired at the top
387* of Codeigniter.php. The main reason we use this is permit
388* PHP errors to be logged in our own log files since we may
389* not have access to server logs. Since this function
390* effectively intercepts PHP errors, however, we also need
391* to display errors based on the current error_reporting level.
392* We do that with the use of a PHP error template.
393*
394* @access private
395* @return void
396*/
397function _exception_handler($severity, $message, $filepath, $line)
398{
399 // We don't bother with "strict" notices since they will fill up
400 // the log file with information that isn't normally very
401 // helpful. For example, if you are running PHP 5 and you
402 // use version 4 style class functions (without prefixes
403 // like "public", "private", etc.) you'll get notices telling
404 // you that these have been deprecated.
405
406 if ($severity == E_STRICT)
407 {
408 return;
409 }
410
411 $error =& load_class('Exceptions');
412
413 // Should we display the error?
414 // We'll get the current error_reporting level and add its bits
415 // with the severity bits to find out.
416
417 if (($severity & error_reporting()) == $severity)
418 {
419 $error->show_php_error($severity, $message, $filepath, $line);
420 }
421
422 // Should we log the error? No? We're done...
423 $config =& get_config();
424 if ($config['log_threshold'] == 0)
425 {
426 return;
427 }
428
429 $error->log_exception($severity, $message, $filepath, $line);
430}
431
432
433
434/* End of file Common.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000435/* Location: ./system/codeigniter/Common.php */