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