blob: a801c08212c1135e8ee08906955ffd1e7b12c6c0 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.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 Rick Ellis
27 * @link http://www.codeigniter.com/user_guide/
28 */
29
30// ------------------------------------------------------------------------
31
32/**
33* Class registry
34*
admin33de9a12006-09-28 06:50:16 +000035* This function acts as a singleton. If the requested class does not
36* exist it is instantiated and set to a static variable. If it has
37* previously been instantiated the variable is returned.
38*
adminb0dd10f2006-08-25 17:25:49 +000039* @access public
admin4a2ed692006-09-29 01:14:52 +000040* @param string the class name being requested
41* @param bool optional flag that lets classes get loaded but not instantiated
adminb0dd10f2006-08-25 17:25:49 +000042* @return object
43*/
adminb87cf232006-10-04 07:46:05 +000044function _load_class($class, $instantiate = TRUE)
adminb0dd10f2006-08-25 17:25:49 +000045{
admin349b09c2006-09-15 20:07:00 +000046 static $objects = array();
admin33de9a12006-09-28 06:50:16 +000047
48 // Does the class exist? If so, we're done...
49 if (isset($objects[$class]))
adminb0dd10f2006-08-25 17:25:49 +000050 {
admin33de9a12006-09-28 06:50:16 +000051 return $objects[$class];
52 }
adminb0dd10f2006-08-25 17:25:49 +000053
admin33de9a12006-09-28 06:50:16 +000054 // This is a special case. It's a class in the Base5.php file
55 // which we don't need to load. We only instantiate it.
56 if ($class == 'Instance')
57 {
58 return $objects[$class] =& new $class();
59 }
60
61 // If the requested class does not exist in the application/libraries
62 // folder we'll load the native class from the system/libraries folder.
admin4a2ed692006-09-29 01:14:52 +000063
admin33de9a12006-09-28 06:50:16 +000064 $is_subclass = FALSE;
65 if ( ! file_exists(APPPATH.'libraries/'.$class.EXT))
66 {
67 require(BASEPATH.'libraries/'.$class.EXT);
68 }
69 else
70 {
71 // A core class can either be extended or replaced by putting an
admin4a2ed692006-09-29 01:14:52 +000072 // identically named file in the application/libraries folder. 
73 // We need to determine, however, if the class being requested is
74 // a sub-class of an existing library or an independent instance
adminb2a9cec2006-10-01 03:38:04 +000075 // since each needs to be handled slightly different. 
admin4a2ed692006-09-29 01:14:52 +000076 // To do this we'll open the requested class and read the top portion
77 // of it. If the class extends a base class we will load the base first.
78 // If it doesn't extend the base we'll only load the requested class.
admin33de9a12006-09-28 06:50:16 +000079
80 // Note: I'm not thrilled with this approach since it requires us to
admin4a2ed692006-09-29 01:14:52 +000081 // read the top part of the file (I set a character limit of 5000 bytes,
82 // which correlates to roughly the first 100 lines of code), but
83 // I can't think of a better way to allow classes to be extended or
84 // replaced on-the-fly with nothing required for the user to do
85 // except write the declaration.  Fortunately PHP is ridiculously fast
86 // at file reading operations so I'm not able to discern a performance
adminb2a9cec2006-10-01 03:38:04 +000087 // hit based on my benchmarks, assuming only a small number of core
admin4a2ed692006-09-29 01:14:52 +000088 // files are being extended, which will usually be the case.
admin33de9a12006-09-28 06:50:16 +000089
90 $fp = fopen(APPPATH.'libraries/'.$class.EXT, "rb");
admin4a2ed692006-09-29 01:14:52 +000091
adminb2a9cec2006-10-01 03:38:04 +000092 if (preg_match("/MY_".$class."\s+extends\s+CI_".$class."/i", fread($fp, '6000')))
adminb0dd10f2006-08-25 17:25:49 +000093 {
admin33de9a12006-09-28 06:50:16 +000094 require(BASEPATH.'libraries/'.$class.EXT);
95 require(APPPATH.'libraries/'.$class.EXT);
96 $is_subclass = TRUE;
adminb0dd10f2006-08-25 17:25:49 +000097 }
98 else
99 {
admin33de9a12006-09-28 06:50:16 +0000100 require(APPPATH.'libraries/'.$class.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000101 }
admin33de9a12006-09-28 06:50:16 +0000102 fclose($fp);
adminb0dd10f2006-08-25 17:25:49 +0000103 }
admin33de9a12006-09-28 06:50:16 +0000104
105 if ($instantiate == FALSE)
106 {
107 return $objects[$class] = TRUE;
108 }
109
110 if ($is_subclass == TRUE)
111 {
112 $name = 'MY_'.$class;
113 return $objects[$class] =& new $name();
114 }
115
116 $name = ($class != 'Controller') ? 'CI_'.$class : $class;
adminb0dd10f2006-08-25 17:25:49 +0000117
admin33de9a12006-09-28 06:50:16 +0000118 return $objects[$class] =& new $name();
adminb0dd10f2006-08-25 17:25:49 +0000119}
120
121/**
122* Loads the main config.php file
123*
adminb0dd10f2006-08-25 17:25:49 +0000124* @access private
125* @return array
126*/
127function &_get_config()
128{
adminbc042dd2006-09-21 02:46:59 +0000129 static $main_conf;
130
131 if ( ! isset($main_conf))
adminb0dd10f2006-08-25 17:25:49 +0000132 {
adminbc042dd2006-09-21 02:46:59 +0000133 if ( ! file_exists(APPPATH.'config/config'.EXT))
134 {
135 show_error('The configuration file config'.EXT.' does not exist.');
136 }
137
adminb0dd10f2006-08-25 17:25:49 +0000138 require(APPPATH.'config/config'.EXT);
139
140 if ( ! isset($config) OR ! is_array($config))
141 {
142 show_error('Your config file does not appear to be formatted correctly.');
143 }
144
adminbc042dd2006-09-21 02:46:59 +0000145 $main_conf[0] =& $config;
adminb0dd10f2006-08-25 17:25:49 +0000146 }
adminbc042dd2006-09-21 02:46:59 +0000147 return $main_conf[0];
adminb0dd10f2006-08-25 17:25:49 +0000148}
149
150
151/**
152* Error Handler
153*
154* This function lets us invoke the exception class and
155* display errors using the standard error template located
156* in application/errors/errors.php
157* This function will send the error page directly to the
158* browser and exit.
159*
160* @access public
161* @return void
162*/
163function show_error($message)
164{
admin33de9a12006-09-28 06:50:16 +0000165 $error =& _load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000166 echo $error->show_error('An Error Was Encountered', $message);
167 exit;
168}
169
170
171/**
172* 404 Page Handler
173*
174* This function is similar to the show_error() function above
175* However, instead of the standard error template it displays
176* 404 errors.
177*
178* @access public
179* @return void
180*/
181function show_404($page = '')
182{
admin33de9a12006-09-28 06:50:16 +0000183 $error =& _load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000184 $error->show_404($page);
185 exit;
186}
187
188
189/**
190* Error Logging Interface
191*
192* We use this as a simple mechanism to access the logging
193* class and send messages to be logged.
194*
195* @access public
196* @return void
197*/
admin57b3d392006-08-27 15:28:31 +0000198function log_message($level = 'error', $message, $php_error = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000199{
admin57b3d392006-08-27 15:28:31 +0000200 static $LOG;
201
adminb0dd10f2006-08-25 17:25:49 +0000202 $config =& _get_config();
203 if ($config['log_errors'] === FALSE)
204 {
205 return;
206 }
207
admin33de9a12006-09-28 06:50:16 +0000208 $LOG =& _load_class('Log');
adminb0dd10f2006-08-25 17:25:49 +0000209 $LOG->write_log($level, $message, $php_error);
210}
211
212
213/**
214* Exception Handler
215*
216* This is the custom exception handler we defined at the
217* top of this file. The main reason we use this is permit
218* PHP errors to be logged in our own log files since we may
219* not have access to server logs. Since this function
220* effectively intercepts PHP errors, however, we also need
221* to display errors based on the current error_reporting level.
222* We do that with the use of a PHP error template.
223*
224* @access private
225* @return void
226*/
227function _exception_handler($severity, $message, $filepath, $line)
228{
229 // We don't bother with "strict" notices since they will fill up
230 // the log file with information that isn't normally very
231 // helpful. For example, if you are running PHP 5 and you
232 // use version 4 style class functions (without prefixes
233 // like "public", "private", etc.) you'll get notices telling
234 // you that these have been deprecated.
235
236 if ($severity == E_STRICT)
237 {
238 return;
239 }
240
admin33de9a12006-09-28 06:50:16 +0000241 $error =& _load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000242
243 // Should we display the error?
244 // We'll get the current error_reporting level and add its bits
245 // with the severity bits to find out.
246
247 if (($severity & error_reporting()) == $severity)
248 {
249 $error->show_php_error($severity, $message, $filepath, $line);
250 }
251
252 // Should we log the error? No? We're done...
253 $config =& _get_config();
254 if ($config['log_errors'] === FALSE)
255 {
256 return;
257 }
258
259 $error->log_exception($severity, $message, $filepath, $line);
260}
261
262
263?>