blob: a4531a7d08d50be530e5ecaed99bf873e5761915 [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*
35*
36* @access public
37* @return object
38*/
39function &_load_class($class, $instantiate = TRUE)
40{
admin349b09c2006-09-15 20:07:00 +000041 static $objects = array();
adminb0dd10f2006-08-25 17:25:49 +000042
43 if ( ! isset($objects[$class]))
44 {
45 if (FALSE !== strpos($class, 'CI_'))
46 {
47 if (file_exists(APPPATH.'libraries/'.str_replace('CI_', '', $class).EXT))
48 {
49 require(APPPATH.'libraries/'.str_replace('CI_', '', $class).EXT);
50 }
51 else
52 {
53 require(BASEPATH.'libraries/'.str_replace('CI_', '', $class).EXT);
54 }
55 }
56
57 if ($instantiate == TRUE)
58 {
59 if ($class == 'CI_Controller')
60 $class = 'Controller';
61
62 $objects[$class] =& new $class();
63 }
64 else
65 {
admin349b09c2006-09-15 20:07:00 +000066 $objects[$class] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +000067 }
68 }
69
adminb0dd10f2006-08-25 17:25:49 +000070 return $objects[$class];
71}
72
73/**
74* Loads the main config.php file
75*
76*
77* @access private
78* @return array
79*/
80function &_get_config()
81{
82 static $conf;
83
84 if ( ! isset($conf))
85 {
86 require(APPPATH.'config/config'.EXT);
87
88 if ( ! isset($config) OR ! is_array($config))
89 {
90 show_error('Your config file does not appear to be formatted correctly.');
91 }
92
93 $conf[0] =& $config;
94 }
95 return $conf[0];
96}
97
98
99/**
100* Error Handler
101*
102* This function lets us invoke the exception class and
103* display errors using the standard error template located
104* in application/errors/errors.php
105* This function will send the error page directly to the
106* browser and exit.
107*
108* @access public
109* @return void
110*/
111function show_error($message)
112{
admin43a46cc2006-09-15 20:44:52 +0000113 $error =& _load_class('CI_Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000114 echo $error->show_error('An Error Was Encountered', $message);
115 exit;
116}
117
118
119/**
120* 404 Page Handler
121*
122* This function is similar to the show_error() function above
123* However, instead of the standard error template it displays
124* 404 errors.
125*
126* @access public
127* @return void
128*/
129function show_404($page = '')
130{
admin43a46cc2006-09-15 20:44:52 +0000131 $error =& _load_class('CI_Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000132 $error->show_404($page);
133 exit;
134}
135
136
137/**
138* Error Logging Interface
139*
140* We use this as a simple mechanism to access the logging
141* class and send messages to be logged.
142*
143* @access public
144* @return void
145*/
admin57b3d392006-08-27 15:28:31 +0000146function log_message($level = 'error', $message, $php_error = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000147{
admin57b3d392006-08-27 15:28:31 +0000148 static $LOG;
149
adminb0dd10f2006-08-25 17:25:49 +0000150 $config =& _get_config();
151 if ($config['log_errors'] === FALSE)
152 {
153 return;
154 }
155
admin43a46cc2006-09-15 20:44:52 +0000156 $LOG =& _load_class('CI_Log');
adminb0dd10f2006-08-25 17:25:49 +0000157 $LOG->write_log($level, $message, $php_error);
158}
159
160
161/**
162* Exception Handler
163*
164* This is the custom exception handler we defined at the
165* top of this file. The main reason we use this is permit
166* PHP errors to be logged in our own log files since we may
167* not have access to server logs. Since this function
168* effectively intercepts PHP errors, however, we also need
169* to display errors based on the current error_reporting level.
170* We do that with the use of a PHP error template.
171*
172* @access private
173* @return void
174*/
175function _exception_handler($severity, $message, $filepath, $line)
176{
177 // We don't bother with "strict" notices since they will fill up
178 // the log file with information that isn't normally very
179 // helpful. For example, if you are running PHP 5 and you
180 // use version 4 style class functions (without prefixes
181 // like "public", "private", etc.) you'll get notices telling
182 // you that these have been deprecated.
183
184 if ($severity == E_STRICT)
185 {
186 return;
187 }
188
admin43a46cc2006-09-15 20:44:52 +0000189 $error =& _load_class('CI_Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000190
191 // Should we display the error?
192 // We'll get the current error_reporting level and add its bits
193 // with the severity bits to find out.
194
195 if (($severity & error_reporting()) == $severity)
196 {
197 $error->show_php_error($severity, $message, $filepath, $line);
198 }
199
200 // Should we log the error? No? We're done...
201 $config =& _get_config();
202 if ($config['log_errors'] === FALSE)
203 {
204 return;
205 }
206
207 $error->log_exception($severity, $message, $filepath, $line);
208}
209
210
211?>