blob: 2ec1c3d38ba83087b9c4169a3693c9a1e3dd6cdf [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{
41 static $objects;
42
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 {
66 $objects[$class] = TRUE;
67 }
68 }
69
70
71 return $objects[$class];
72}
73
74/**
75* Loads the main config.php file
76*
77*
78* @access private
79* @return array
80*/
81function &_get_config()
82{
83 static $conf;
84
85 if ( ! isset($conf))
86 {
87 require(APPPATH.'config/config'.EXT);
88
89 if ( ! isset($config) OR ! is_array($config))
90 {
91 show_error('Your config file does not appear to be formatted correctly.');
92 }
93
94 $conf[0] =& $config;
95 }
96 return $conf[0];
97}
98
99
100/**
101* Error Handler
102*
103* This function lets us invoke the exception class and
104* display errors using the standard error template located
105* in application/errors/errors.php
106* This function will send the error page directly to the
107* browser and exit.
108*
109* @access public
110* @return void
111*/
112function show_error($message)
113{
114 if ( ! class_exists('CI_Exceptions'))
115 {
116 include_once(BASEPATH.'libraries/Exceptions.php');
117 }
118
119 $error = new CI_Exceptions();
120 echo $error->show_error('An Error Was Encountered', $message);
121 exit;
122}
123
124
125/**
126* 404 Page Handler
127*
128* This function is similar to the show_error() function above
129* However, instead of the standard error template it displays
130* 404 errors.
131*
132* @access public
133* @return void
134*/
135function show_404($page = '')
136{
137 if ( ! class_exists('CI_Exceptions'))
138 {
139 include_once(BASEPATH.'libraries/Exceptions.php');
140 }
141
142 $error = new CI_Exceptions();
143 $error->show_404($page);
144 exit;
145}
146
147
148/**
149* Error Logging Interface
150*
151* We use this as a simple mechanism to access the logging
152* class and send messages to be logged.
153*
154* @access public
155* @return void
156*/
157function log_message($level = 2, $message, $php_error = FALSE)
158{
159 $config =& _get_config();
160 if ($config['log_errors'] === FALSE)
161 {
162 return;
163 }
164
165 if ( ! class_exists('CI_Log'))
166 {
167 include_once(BASEPATH.'libraries/Log.php');
168 }
169
170 if ( ! isset($LOG))
171 {
172 $LOG = new CI_Log(
173 $config['log_path'],
174 $config['log_threshold'],
175 $config['log_date_format']
176 );
177 }
178
179 $LOG->write_log($level, $message, $php_error);
180}
181
182
183/**
184* Exception Handler
185*
186* This is the custom exception handler we defined at the
187* top of this file. The main reason we use this is permit
188* PHP errors to be logged in our own log files since we may
189* not have access to server logs. Since this function
190* effectively intercepts PHP errors, however, we also need
191* to display errors based on the current error_reporting level.
192* We do that with the use of a PHP error template.
193*
194* @access private
195* @return void
196*/
197function _exception_handler($severity, $message, $filepath, $line)
198{
199 // We don't bother with "strict" notices since they will fill up
200 // the log file with information that isn't normally very
201 // helpful. For example, if you are running PHP 5 and you
202 // use version 4 style class functions (without prefixes
203 // like "public", "private", etc.) you'll get notices telling
204 // you that these have been deprecated.
205
206 if ($severity == E_STRICT)
207 {
208 return;
209 }
210
211 // Send the PHP error to the log file...
212 if ( ! class_exists('CI_Exceptions'))
213 {
214 include_once(BASEPATH.'libraries/Exceptions.php');
215 }
216 $error = new CI_Exceptions();
217
218 // Should we display the error?
219 // We'll get the current error_reporting level and add its bits
220 // with the severity bits to find out.
221
222 if (($severity & error_reporting()) == $severity)
223 {
224 $error->show_php_error($severity, $message, $filepath, $line);
225 }
226
227 // Should we log the error? No? We're done...
228 $config =& _get_config();
229 if ($config['log_errors'] === FALSE)
230 {
231 return;
232 }
233
234 $error->log_exception($severity, $message, $filepath, $line);
235}
236
237
238?>