blob: a82d486e6864ce2d06ce0ca948e7b39b1213d419 [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 }
admin0625e192006-10-20 22:16:54 +000053
admin33de9a12006-09-28 06:50:16 +000054 // If the requested class does not exist in the application/libraries
admin0625e192006-10-20 22:16:54 +000055 // folder we'll load the native class from the system/libraries folder.
56 if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
admin33de9a12006-09-28 06:50:16 +000057 {
admin0625e192006-10-20 22:16:54 +000058 require(BASEPATH.'libraries/'.$class.EXT);
59 require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
60 $is_subclass = TRUE;
admin33de9a12006-09-28 06:50:16 +000061 }
62 else
63 {
admin0625e192006-10-20 22:16:54 +000064 if (file_exists(APPPATH.'libraries/'.$class.EXT))
adminb0dd10f2006-08-25 17:25:49 +000065 {
admin0625e192006-10-20 22:16:54 +000066 require(APPPATH.'libraries/'.$class.EXT);
67 $is_subclass = FALSE;
adminb0dd10f2006-08-25 17:25:49 +000068 }
69 else
70 {
admin0625e192006-10-20 22:16:54 +000071 require(BASEPATH.'libraries/'.$class.EXT);
72 $is_subclass = FALSE;
adminb0dd10f2006-08-25 17:25:49 +000073 }
74 }
admin33de9a12006-09-28 06:50:16 +000075
76 if ($instantiate == FALSE)
77 {
admin7099a582006-10-10 17:47:59 +000078 $objects[$class] = TRUE;
79 return $objects[$class];
admin33de9a12006-09-28 06:50:16 +000080 }
81
82 if ($is_subclass == TRUE)
83 {
admin0625e192006-10-20 22:16:54 +000084 $name = config_item('subclass_prefix').$class;
admin7099a582006-10-10 17:47:59 +000085 $objects[$class] =& new $name();
86 return $objects[$class];
admin33de9a12006-09-28 06:50:16 +000087 }
88
89 $name = ($class != 'Controller') ? 'CI_'.$class : $class;
adminb0dd10f2006-08-25 17:25:49 +000090
admin7099a582006-10-10 17:47:59 +000091 $objects[$class] =& new $name();
92 return $objects[$class];
adminb0dd10f2006-08-25 17:25:49 +000093}
94
95/**
96* Loads the main config.php file
97*
adminb0dd10f2006-08-25 17:25:49 +000098* @access private
99* @return array
100*/
admin7099a582006-10-10 17:47:59 +0000101function &get_config()
adminb0dd10f2006-08-25 17:25:49 +0000102{
adminbc042dd2006-09-21 02:46:59 +0000103 static $main_conf;
104
105 if ( ! isset($main_conf))
adminb0dd10f2006-08-25 17:25:49 +0000106 {
adminbc042dd2006-09-21 02:46:59 +0000107 if ( ! file_exists(APPPATH.'config/config'.EXT))
108 {
109 show_error('The configuration file config'.EXT.' does not exist.');
110 }
111
adminb0dd10f2006-08-25 17:25:49 +0000112 require(APPPATH.'config/config'.EXT);
113
114 if ( ! isset($config) OR ! is_array($config))
115 {
116 show_error('Your config file does not appear to be formatted correctly.');
117 }
118
adminbc042dd2006-09-21 02:46:59 +0000119 $main_conf[0] =& $config;
adminb0dd10f2006-08-25 17:25:49 +0000120 }
adminbc042dd2006-09-21 02:46:59 +0000121 return $main_conf[0];
adminb0dd10f2006-08-25 17:25:49 +0000122}
123
admin0625e192006-10-20 22:16:54 +0000124/**
125* Gets a config item
126*
127* @access public
128* @return mixed
129*/
130function &config_item($item)
131{
132 static $config_item = array();
133
134 if ( ! isset($config_item[$item]))
135 {
136 $config =& get_config();
137
138 if ( ! isset($config[$item]))
139 {
140 return FALSE;
141 }
142 $config_item[$item] = $config[$item];
143 }
144
145 return $config_item[$item];
146}
147
adminb0dd10f2006-08-25 17:25:49 +0000148
149/**
150* Error Handler
151*
152* This function lets us invoke the exception class and
153* display errors using the standard error template located
154* in application/errors/errors.php
155* This function will send the error page directly to the
156* browser and exit.
157*
158* @access public
159* @return void
160*/
161function show_error($message)
162{
admin7099a582006-10-10 17:47:59 +0000163 $error =& load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000164 echo $error->show_error('An Error Was Encountered', $message);
165 exit;
166}
167
168
169/**
170* 404 Page Handler
171*
172* This function is similar to the show_error() function above
173* However, instead of the standard error template it displays
174* 404 errors.
175*
176* @access public
177* @return void
178*/
179function show_404($page = '')
180{
admin7099a582006-10-10 17:47:59 +0000181 $error =& load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000182 $error->show_404($page);
183 exit;
184}
185
186
187/**
188* Error Logging Interface
189*
190* We use this as a simple mechanism to access the logging
191* class and send messages to be logged.
192*
193* @access public
194* @return void
195*/
admin57b3d392006-08-27 15:28:31 +0000196function log_message($level = 'error', $message, $php_error = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000197{
admin57b3d392006-08-27 15:28:31 +0000198 static $LOG;
199
admin7099a582006-10-10 17:47:59 +0000200 $config = get_config();
adminb06d69f2006-10-13 21:44:17 +0000201 if ($config['log_threshold'] == 0)
adminb0dd10f2006-08-25 17:25:49 +0000202 {
203 return;
204 }
205
admin7099a582006-10-10 17:47:59 +0000206 $LOG =& load_class('Log');
adminb0dd10f2006-08-25 17:25:49 +0000207 $LOG->write_log($level, $message, $php_error);
208}
209
210
211/**
212* Exception Handler
213*
214* This is the custom exception handler we defined at the
215* top of this file. The main reason we use this is permit
216* PHP errors to be logged in our own log files since we may
217* not have access to server logs. Since this function
218* effectively intercepts PHP errors, however, we also need
219* to display errors based on the current error_reporting level.
220* We do that with the use of a PHP error template.
221*
222* @access private
223* @return void
224*/
225function _exception_handler($severity, $message, $filepath, $line)
226{
227 // We don't bother with "strict" notices since they will fill up
228 // the log file with information that isn't normally very
229 // helpful. For example, if you are running PHP 5 and you
230 // use version 4 style class functions (without prefixes
231 // like "public", "private", etc.) you'll get notices telling
232 // you that these have been deprecated.
233
234 if ($severity == E_STRICT)
235 {
236 return;
237 }
238
admin7099a582006-10-10 17:47:59 +0000239 $error =& load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000240
241 // Should we display the error?
242 // We'll get the current error_reporting level and add its bits
243 // with the severity bits to find out.
244
245 if (($severity & error_reporting()) == $severity)
246 {
247 $error->show_php_error($severity, $message, $filepath, $line);
248 }
249
250 // Should we log the error? No? We're done...
admin7099a582006-10-10 17:47:59 +0000251 $config = get_config();
adminb06d69f2006-10-13 21:44:17 +0000252 if ($config['log_threshold'] == 0)
adminb0dd10f2006-08-25 17:25:49 +0000253 {
254 return;
255 }
256
257 $error->log_exception($severity, $message, $filepath, $line);
258}
259
260
261?>