blob: 24e6042cc0db57c8b9737cdcd24d56341ae0a522 [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
40* @return object
41*/
42function &_load_class($class, $instantiate = TRUE)
43{
admin349b09c2006-09-15 20:07:00 +000044 static $objects = array();
admin33de9a12006-09-28 06:50:16 +000045
46 // Does the class exist? If so, we're done...
47 if (isset($objects[$class]))
adminb0dd10f2006-08-25 17:25:49 +000048 {
admin33de9a12006-09-28 06:50:16 +000049 return $objects[$class];
50 }
adminb0dd10f2006-08-25 17:25:49 +000051
admin33de9a12006-09-28 06:50:16 +000052 // This is a special case. It's a class in the Base5.php file
53 // which we don't need to load. We only instantiate it.
54 if ($class == 'Instance')
55 {
56 return $objects[$class] =& new $class();
57 }
58
59 // If the requested class does not exist in the application/libraries
60 // folder we'll load the native class from the system/libraries folder.
61
62 $is_subclass = FALSE;
63 if ( ! file_exists(APPPATH.'libraries/'.$class.EXT))
64 {
65 require(BASEPATH.'libraries/'.$class.EXT);
66 }
67 else
68 {
69 // A core class can either be extended or replaced by putting an
70 // identially named file in the application/libraries folder.
71 // We need to need to determine if the class being requested is
72 // a sub-class or an independent instance so we'll open the file,
73 // read the top portion of it. If the class extends the base class
74 // we need to load it's parent. If it doesn't extend the base we'll
75 // only load the requested class.
76
77 // Note: I'm not thrilled with this approach since it requires us to
78 // read the file, but I can't think of any other way to allow classes
79 // to be extended on-the-fly. I did benchmark the difference with and
80 // without the file reading and I'm not seeing a perceptable difference.
81
82 $fp = fopen(APPPATH.'libraries/'.$class.EXT, "rb");
83 if (preg_match("/MY_".$class."\s+extends\s+CI_".$class."/", fread($fp, '8000')))
adminb0dd10f2006-08-25 17:25:49 +000084 {
admin33de9a12006-09-28 06:50:16 +000085 require(BASEPATH.'libraries/'.$class.EXT);
86 require(APPPATH.'libraries/'.$class.EXT);
87 $is_subclass = TRUE;
adminb0dd10f2006-08-25 17:25:49 +000088 }
89 else
90 {
admin33de9a12006-09-28 06:50:16 +000091 require(APPPATH.'libraries/'.$class.EXT);
adminb0dd10f2006-08-25 17:25:49 +000092 }
admin33de9a12006-09-28 06:50:16 +000093 fclose($fp);
adminb0dd10f2006-08-25 17:25:49 +000094 }
admin33de9a12006-09-28 06:50:16 +000095
96 if ($instantiate == FALSE)
97 {
98 return $objects[$class] = TRUE;
99 }
100
101 if ($is_subclass == TRUE)
102 {
103 $name = 'MY_'.$class;
104 return $objects[$class] =& new $name();
105 }
106
107 $name = ($class != 'Controller') ? 'CI_'.$class : $class;
adminb0dd10f2006-08-25 17:25:49 +0000108
admin33de9a12006-09-28 06:50:16 +0000109 return $objects[$class] =& new $name();
adminb0dd10f2006-08-25 17:25:49 +0000110}
111
112/**
113* Loads the main config.php file
114*
115*
116* @access private
117* @return array
118*/
119function &_get_config()
120{
adminbc042dd2006-09-21 02:46:59 +0000121 static $main_conf;
122
123 if ( ! isset($main_conf))
adminb0dd10f2006-08-25 17:25:49 +0000124 {
adminbc042dd2006-09-21 02:46:59 +0000125 if ( ! file_exists(APPPATH.'config/config'.EXT))
126 {
127 show_error('The configuration file config'.EXT.' does not exist.');
128 }
129
adminb0dd10f2006-08-25 17:25:49 +0000130 require(APPPATH.'config/config'.EXT);
131
132 if ( ! isset($config) OR ! is_array($config))
133 {
134 show_error('Your config file does not appear to be formatted correctly.');
135 }
136
adminbc042dd2006-09-21 02:46:59 +0000137 $main_conf[0] =& $config;
adminb0dd10f2006-08-25 17:25:49 +0000138 }
adminbc042dd2006-09-21 02:46:59 +0000139 return $main_conf[0];
adminb0dd10f2006-08-25 17:25:49 +0000140}
141
142
143/**
144* Error Handler
145*
146* This function lets us invoke the exception class and
147* display errors using the standard error template located
148* in application/errors/errors.php
149* This function will send the error page directly to the
150* browser and exit.
151*
152* @access public
153* @return void
154*/
155function show_error($message)
156{
admin33de9a12006-09-28 06:50:16 +0000157 $error =& _load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000158 echo $error->show_error('An Error Was Encountered', $message);
159 exit;
160}
161
162
163/**
164* 404 Page Handler
165*
166* This function is similar to the show_error() function above
167* However, instead of the standard error template it displays
168* 404 errors.
169*
170* @access public
171* @return void
172*/
173function show_404($page = '')
174{
admin33de9a12006-09-28 06:50:16 +0000175 $error =& _load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000176 $error->show_404($page);
177 exit;
178}
179
180
181/**
182* Error Logging Interface
183*
184* We use this as a simple mechanism to access the logging
185* class and send messages to be logged.
186*
187* @access public
188* @return void
189*/
admin57b3d392006-08-27 15:28:31 +0000190function log_message($level = 'error', $message, $php_error = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000191{
admin57b3d392006-08-27 15:28:31 +0000192 static $LOG;
193
adminb0dd10f2006-08-25 17:25:49 +0000194 $config =& _get_config();
195 if ($config['log_errors'] === FALSE)
196 {
197 return;
198 }
199
admin33de9a12006-09-28 06:50:16 +0000200 $LOG =& _load_class('Log');
adminb0dd10f2006-08-25 17:25:49 +0000201 $LOG->write_log($level, $message, $php_error);
202}
203
204
205/**
206* Exception Handler
207*
208* This is the custom exception handler we defined at the
209* top of this file. The main reason we use this is permit
210* PHP errors to be logged in our own log files since we may
211* not have access to server logs. Since this function
212* effectively intercepts PHP errors, however, we also need
213* to display errors based on the current error_reporting level.
214* We do that with the use of a PHP error template.
215*
216* @access private
217* @return void
218*/
219function _exception_handler($severity, $message, $filepath, $line)
220{
221 // We don't bother with "strict" notices since they will fill up
222 // the log file with information that isn't normally very
223 // helpful. For example, if you are running PHP 5 and you
224 // use version 4 style class functions (without prefixes
225 // like "public", "private", etc.) you'll get notices telling
226 // you that these have been deprecated.
227
228 if ($severity == E_STRICT)
229 {
230 return;
231 }
232
admin33de9a12006-09-28 06:50:16 +0000233 $error =& _load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000234
235 // Should we display the error?
236 // We'll get the current error_reporting level and add its bits
237 // with the severity bits to find out.
238
239 if (($severity & error_reporting()) == $severity)
240 {
241 $error->show_php_error($severity, $message, $filepath, $line);
242 }
243
244 // Should we log the error? No? We're done...
245 $config =& _get_config();
246 if ($config['log_errors'] === FALSE)
247 {
248 return;
249 }
250
251 $error->log_exception($severity, $message, $filepath, $line);
252}
253
254
255?>