blob: 340be24543ded211a2cffc8eea4295177b1fba58 [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/**
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 */
42function 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*/
89function &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*/
146function &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*/
175function 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*/
Derek Jones817163a2009-07-11 17:05:58 +0000206function show_error($message, $status_code = 500)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207{
208 $error =& load_class('Exceptions');
Derek Jones817163a2009-07-11 17:05:58 +0000209 echo $error->show_error('An Error Was Encountered', $message, 'error_general', $status_code);
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 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*/
224function 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*/
241function 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
Derek Jones817163a2009-07-11 17:05:58 +0000255
256/**
257 * Set HTTP Status Header
258 *
259 * @access public
260 * @param int the status code
261 * @param string
262 * @return void
263 */
264function set_status_header($code = 200, $text = '')
265{
266 $stati = array(
267 200 => 'OK',
268 201 => 'Created',
269 202 => 'Accepted',
270 203 => 'Non-Authoritative Information',
271 204 => 'No Content',
272 205 => 'Reset Content',
273 206 => 'Partial Content',
274
275 300 => 'Multiple Choices',
276 301 => 'Moved Permanently',
277 302 => 'Found',
278 304 => 'Not Modified',
279 305 => 'Use Proxy',
280 307 => 'Temporary Redirect',
281
282 400 => 'Bad Request',
283 401 => 'Unauthorized',
284 403 => 'Forbidden',
285 404 => 'Not Found',
286 405 => 'Method Not Allowed',
287 406 => 'Not Acceptable',
288 407 => 'Proxy Authentication Required',
289 408 => 'Request Timeout',
290 409 => 'Conflict',
291 410 => 'Gone',
292 411 => 'Length Required',
293 412 => 'Precondition Failed',
294 413 => 'Request Entity Too Large',
295 414 => 'Request-URI Too Long',
296 415 => 'Unsupported Media Type',
297 416 => 'Requested Range Not Satisfiable',
298 417 => 'Expectation Failed',
299
300 500 => 'Internal Server Error',
301 501 => 'Not Implemented',
302 502 => 'Bad Gateway',
303 503 => 'Service Unavailable',
304 504 => 'Gateway Timeout',
305 505 => 'HTTP Version Not Supported'
306 );
307
308 if ($code == '' OR ! is_numeric($code))
309 {
310 show_error('Status codes must be numeric', 500);
311 }
312
313 if (isset($stati[$code]) AND $text == '')
314 {
315 $text = $stati[$code];
316 }
317
318 if ($text == '')
319 {
320 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
321 }
322
323 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
324
325 if (substr(php_sapi_name(), 0, 3) == 'cgi')
326 {
327 header("Status: {$code} {$text}", TRUE);
328 }
329 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
330 {
331 header($server_protocol." {$code} {$text}", TRUE, $code);
332 }
333 else
334 {
335 header("HTTP/1.1 {$code} {$text}", TRUE, $code);
336 }
337}
338
339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340/**
341* Exception Handler
342*
343* This is the custom exception handler that is declaired at the top
344* of Codeigniter.php. The main reason we use this is permit
345* PHP errors to be logged in our own log files since we may
346* not have access to server logs. Since this function
347* effectively intercepts PHP errors, however, we also need
348* to display errors based on the current error_reporting level.
349* We do that with the use of a PHP error template.
350*
351* @access private
352* @return void
353*/
354function _exception_handler($severity, $message, $filepath, $line)
355{
356 // We don't bother with "strict" notices since they will fill up
357 // the log file with information that isn't normally very
358 // helpful. For example, if you are running PHP 5 and you
359 // use version 4 style class functions (without prefixes
360 // like "public", "private", etc.) you'll get notices telling
361 // you that these have been deprecated.
362
363 if ($severity == E_STRICT)
364 {
365 return;
366 }
367
368 $error =& load_class('Exceptions');
369
370 // Should we display the error?
371 // We'll get the current error_reporting level and add its bits
372 // with the severity bits to find out.
373
374 if (($severity & error_reporting()) == $severity)
375 {
376 $error->show_php_error($severity, $message, $filepath, $line);
377 }
378
379 // Should we log the error? No? We're done...
380 $config =& get_config();
381 if ($config['log_threshold'] == 0)
382 {
383 return;
384 }
385
386 $error->log_exception($severity, $message, $filepath, $line);
387}
388
389
390
391/* End of file Common.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000392/* Location: ./system/codeigniter/Common.php */