blob: d2342de42aa0845995f818b86783b8f533681131 [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*/
44function &_load_class($class, $instantiate = TRUE)
45{
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*
124*
125* @access private
126* @return array
127*/
128function &_get_config()
129{
adminbc042dd2006-09-21 02:46:59 +0000130 static $main_conf;
131
132 if ( ! isset($main_conf))
adminb0dd10f2006-08-25 17:25:49 +0000133 {
adminbc042dd2006-09-21 02:46:59 +0000134 if ( ! file_exists(APPPATH.'config/config'.EXT))
135 {
136 show_error('The configuration file config'.EXT.' does not exist.');
137 }
138
adminb0dd10f2006-08-25 17:25:49 +0000139 require(APPPATH.'config/config'.EXT);
140
141 if ( ! isset($config) OR ! is_array($config))
142 {
143 show_error('Your config file does not appear to be formatted correctly.');
144 }
145
adminbc042dd2006-09-21 02:46:59 +0000146 $main_conf[0] =& $config;
adminb0dd10f2006-08-25 17:25:49 +0000147 }
adminbc042dd2006-09-21 02:46:59 +0000148 return $main_conf[0];
adminb0dd10f2006-08-25 17:25:49 +0000149}
150
151
152/**
153* Error Handler
154*
155* This function lets us invoke the exception class and
156* display errors using the standard error template located
157* in application/errors/errors.php
158* This function will send the error page directly to the
159* browser and exit.
160*
161* @access public
162* @return void
163*/
164function show_error($message)
165{
admin33de9a12006-09-28 06:50:16 +0000166 $error =& _load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000167 echo $error->show_error('An Error Was Encountered', $message);
168 exit;
169}
170
171
172/**
173* 404 Page Handler
174*
175* This function is similar to the show_error() function above
176* However, instead of the standard error template it displays
177* 404 errors.
178*
179* @access public
180* @return void
181*/
182function show_404($page = '')
183{
admin33de9a12006-09-28 06:50:16 +0000184 $error =& _load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000185 $error->show_404($page);
186 exit;
187}
188
189
190/**
191* Error Logging Interface
192*
193* We use this as a simple mechanism to access the logging
194* class and send messages to be logged.
195*
196* @access public
197* @return void
198*/
admin57b3d392006-08-27 15:28:31 +0000199function log_message($level = 'error', $message, $php_error = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000200{
admin57b3d392006-08-27 15:28:31 +0000201 static $LOG;
202
adminb0dd10f2006-08-25 17:25:49 +0000203 $config =& _get_config();
204 if ($config['log_errors'] === FALSE)
205 {
206 return;
207 }
208
admin33de9a12006-09-28 06:50:16 +0000209 $LOG =& _load_class('Log');
adminb0dd10f2006-08-25 17:25:49 +0000210 $LOG->write_log($level, $message, $php_error);
211}
212
213
214/**
215* Exception Handler
216*
217* This is the custom exception handler we defined at the
218* top of this file. The main reason we use this is permit
219* PHP errors to be logged in our own log files since we may
220* not have access to server logs. Since this function
221* effectively intercepts PHP errors, however, we also need
222* to display errors based on the current error_reporting level.
223* We do that with the use of a PHP error template.
224*
225* @access private
226* @return void
227*/
228function _exception_handler($severity, $message, $filepath, $line)
229{
230 // We don't bother with "strict" notices since they will fill up
231 // the log file with information that isn't normally very
232 // helpful. For example, if you are running PHP 5 and you
233 // use version 4 style class functions (without prefixes
234 // like "public", "private", etc.) you'll get notices telling
235 // you that these have been deprecated.
236
237 if ($severity == E_STRICT)
238 {
239 return;
240 }
241
admin33de9a12006-09-28 06:50:16 +0000242 $error =& _load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000243
244 // Should we display the error?
245 // We'll get the current error_reporting level and add its bits
246 // with the severity bits to find out.
247
248 if (($severity & error_reporting()) == $severity)
249 {
250 $error->show_php_error($severity, $message, $filepath, $line);
251 }
252
253 // Should we log the error? No? We're done...
254 $config =& _get_config();
255 if ($config['log_errors'] === FALSE)
256 {
257 return;
258 }
259
260 $error->log_exception($severity, $message, $filepath, $line);
261}
262
263
264?>