blob: 8ac80d634a2cb2789862baa25cb3540e085eb7e8 [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*/
admin7099a582006-10-10 17:47:59 +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 {
admin7099a582006-10-10 17:47:59 +000058 $objects[$class] =& new $class();
59 return $objects[$class];
admin33de9a12006-09-28 06:50:16 +000060 }
61
62 // If the requested class does not exist in the application/libraries
63 // folder we'll load the native class from the system/libraries folder.
admin4a2ed692006-09-29 01:14:52 +000064
admin33de9a12006-09-28 06:50:16 +000065 $is_subclass = FALSE;
66 if ( ! file_exists(APPPATH.'libraries/'.$class.EXT))
67 {
68 require(BASEPATH.'libraries/'.$class.EXT);
69 }
70 else
71 {
72 // A core class can either be extended or replaced by putting an
admin4a2ed692006-09-29 01:14:52 +000073 // identically named file in the application/libraries folder. 
74 // We need to determine, however, if the class being requested is
75 // a sub-class of an existing library or an independent instance
adminb2a9cec2006-10-01 03:38:04 +000076 // since each needs to be handled slightly different. 
admin4a2ed692006-09-29 01:14:52 +000077 // To do this we'll open the requested class and read the top portion
78 // of it. If the class extends a base class we will load the base first.
79 // If it doesn't extend the base we'll only load the requested class.
admin33de9a12006-09-28 06:50:16 +000080
81 // Note: I'm not thrilled with this approach since it requires us to
admin4a2ed692006-09-29 01:14:52 +000082 // read the top part of the file (I set a character limit of 5000 bytes,
83 // which correlates to roughly the first 100 lines of code), but
84 // I can't think of a better way to allow classes to be extended or
85 // replaced on-the-fly with nothing required for the user to do
86 // except write the declaration.  Fortunately PHP is ridiculously fast
87 // at file reading operations so I'm not able to discern a performance
adminb2a9cec2006-10-01 03:38:04 +000088 // hit based on my benchmarks, assuming only a small number of core
admin4a2ed692006-09-29 01:14:52 +000089 // files are being extended, which will usually be the case.
admin33de9a12006-09-28 06:50:16 +000090
91 $fp = fopen(APPPATH.'libraries/'.$class.EXT, "rb");
admin4a2ed692006-09-29 01:14:52 +000092
adminb2a9cec2006-10-01 03:38:04 +000093 if (preg_match("/MY_".$class."\s+extends\s+CI_".$class."/i", fread($fp, '6000')))
adminb0dd10f2006-08-25 17:25:49 +000094 {
admin33de9a12006-09-28 06:50:16 +000095 require(BASEPATH.'libraries/'.$class.EXT);
96 require(APPPATH.'libraries/'.$class.EXT);
97 $is_subclass = TRUE;
adminb0dd10f2006-08-25 17:25:49 +000098 }
99 else
100 {
admin33de9a12006-09-28 06:50:16 +0000101 require(APPPATH.'libraries/'.$class.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000102 }
admin33de9a12006-09-28 06:50:16 +0000103 fclose($fp);
adminb0dd10f2006-08-25 17:25:49 +0000104 }
admin33de9a12006-09-28 06:50:16 +0000105
106 if ($instantiate == FALSE)
107 {
admin7099a582006-10-10 17:47:59 +0000108 $objects[$class] = TRUE;
109 return $objects[$class];
admin33de9a12006-09-28 06:50:16 +0000110 }
111
112 if ($is_subclass == TRUE)
113 {
114 $name = 'MY_'.$class;
admin7099a582006-10-10 17:47:59 +0000115 $objects[$class] =& new $name();
116 return $objects[$class];
admin33de9a12006-09-28 06:50:16 +0000117 }
118
119 $name = ($class != 'Controller') ? 'CI_'.$class : $class;
adminb0dd10f2006-08-25 17:25:49 +0000120
admin7099a582006-10-10 17:47:59 +0000121 $objects[$class] =& new $name();
122 return $objects[$class];
adminb0dd10f2006-08-25 17:25:49 +0000123}
124
125/**
126* Loads the main config.php file
127*
adminb0dd10f2006-08-25 17:25:49 +0000128* @access private
129* @return array
130*/
admin7099a582006-10-10 17:47:59 +0000131function &get_config()
adminb0dd10f2006-08-25 17:25:49 +0000132{
adminbc042dd2006-09-21 02:46:59 +0000133 static $main_conf;
134
135 if ( ! isset($main_conf))
adminb0dd10f2006-08-25 17:25:49 +0000136 {
adminbc042dd2006-09-21 02:46:59 +0000137 if ( ! file_exists(APPPATH.'config/config'.EXT))
138 {
139 show_error('The configuration file config'.EXT.' does not exist.');
140 }
141
adminb0dd10f2006-08-25 17:25:49 +0000142 require(APPPATH.'config/config'.EXT);
143
144 if ( ! isset($config) OR ! is_array($config))
145 {
146 show_error('Your config file does not appear to be formatted correctly.');
147 }
148
adminbc042dd2006-09-21 02:46:59 +0000149 $main_conf[0] =& $config;
adminb0dd10f2006-08-25 17:25:49 +0000150 }
adminbc042dd2006-09-21 02:46:59 +0000151 return $main_conf[0];
adminb0dd10f2006-08-25 17:25:49 +0000152}
153
154
155/**
156* Error Handler
157*
158* This function lets us invoke the exception class and
159* display errors using the standard error template located
160* in application/errors/errors.php
161* This function will send the error page directly to the
162* browser and exit.
163*
164* @access public
165* @return void
166*/
167function show_error($message)
168{
admin7099a582006-10-10 17:47:59 +0000169 $error =& load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000170 echo $error->show_error('An Error Was Encountered', $message);
171 exit;
172}
173
174
175/**
176* 404 Page Handler
177*
178* This function is similar to the show_error() function above
179* However, instead of the standard error template it displays
180* 404 errors.
181*
182* @access public
183* @return void
184*/
185function show_404($page = '')
186{
admin7099a582006-10-10 17:47:59 +0000187 $error =& load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000188 $error->show_404($page);
189 exit;
190}
191
192
193/**
194* Error Logging Interface
195*
196* We use this as a simple mechanism to access the logging
197* class and send messages to be logged.
198*
199* @access public
200* @return void
201*/
admin57b3d392006-08-27 15:28:31 +0000202function log_message($level = 'error', $message, $php_error = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000203{
admin57b3d392006-08-27 15:28:31 +0000204 static $LOG;
205
admin7099a582006-10-10 17:47:59 +0000206 $config = get_config();
adminb0dd10f2006-08-25 17:25:49 +0000207 if ($config['log_errors'] === FALSE)
208 {
209 return;
210 }
211
admin7099a582006-10-10 17:47:59 +0000212 $LOG =& load_class('Log');
adminb0dd10f2006-08-25 17:25:49 +0000213 $LOG->write_log($level, $message, $php_error);
214}
215
216
217/**
218* Exception Handler
219*
220* This is the custom exception handler we defined at the
221* top of this file. The main reason we use this is permit
222* PHP errors to be logged in our own log files since we may
223* not have access to server logs. Since this function
224* effectively intercepts PHP errors, however, we also need
225* to display errors based on the current error_reporting level.
226* We do that with the use of a PHP error template.
227*
228* @access private
229* @return void
230*/
231function _exception_handler($severity, $message, $filepath, $line)
232{
233 // We don't bother with "strict" notices since they will fill up
234 // the log file with information that isn't normally very
235 // helpful. For example, if you are running PHP 5 and you
236 // use version 4 style class functions (without prefixes
237 // like "public", "private", etc.) you'll get notices telling
238 // you that these have been deprecated.
239
240 if ($severity == E_STRICT)
241 {
242 return;
243 }
244
admin7099a582006-10-10 17:47:59 +0000245 $error =& load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000246
247 // Should we display the error?
248 // We'll get the current error_reporting level and add its bits
249 // with the severity bits to find out.
250
251 if (($severity & error_reporting()) == $severity)
252 {
253 $error->show_php_error($severity, $message, $filepath, $line);
254 }
255
256 // Should we log the error? No? We're done...
admin7099a582006-10-10 17:47:59 +0000257 $config = get_config();
adminb0dd10f2006-08-25 17:25:49 +0000258 if ($config['log_errors'] === FALSE)
259 {
260 return;
261 }
262
263 $error->log_exception($severity, $message, $filepath, $line);
264}
265
266
267?>