blob: 41e13bee674cd2958f8feb61cd0328173e481285 [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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * Common Functions
20 *
admine334c472006-10-21 19:44:22 +000021 * Loads the base classes and executes the request.
22 *
adminb0dd10f2006-08-25 17:25:49 +000023 * @package CodeIgniter
24 * @subpackage codeigniter
25 * @category Common Functions
26 * @author Rick Ellis
27 * @link http://www.codeigniter.com/user_guide/
28 */
admine334c472006-10-21 19:44:22 +000029
adminb0dd10f2006-08-25 17:25:49 +000030// ------------------------------------------------------------------------
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.
admine334c472006-10-21 19:44:22 +000038*
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*/
admin49439ff2006-10-30 04:39:12 +0000130function config_item($item)
admin0625e192006-10-20 22:16:54 +0000131{
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
admine334c472006-10-21 19:44:22 +0000153* display errors using the standard error template located
adminb0dd10f2006-08-25 17:25:49 +0000154* in application/errors/errors.php
admine334c472006-10-21 19:44:22 +0000155* This function will send the error page directly to the
adminb0dd10f2006-08-25 17:25:49 +0000156* 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/**
admine334c472006-10-21 19:44:22 +0000188* Error Logging Interface
adminb0dd10f2006-08-25 17:25:49 +0000189*
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
admin49439ff2006-10-30 04:39:12 +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
adminb0dd10f2006-08-25 17:25:49 +0000210/**
211* Exception Handler
212*
213* This is the custom exception handler we defined at the
admine334c472006-10-21 19:44:22 +0000214* top of this file. The main reason we use this is permit
215* PHP errors to be logged in our own log files since we may
adminb0dd10f2006-08-25 17:25:49 +0000216* not have access to server logs. Since this function
217* effectively intercepts PHP errors, however, we also need
218* to display errors based on the current error_reporting level.
219* We do that with the use of a PHP error template.
220*
221* @access private
222* @return void
223*/
224function _exception_handler($severity, $message, $filepath, $line)
225{
226 // We don't bother with "strict" notices since they will fill up
227 // the log file with information that isn't normally very
228 // helpful. For example, if you are running PHP 5 and you
229 // use version 4 style class functions (without prefixes
230 // like "public", "private", etc.) you'll get notices telling
231 // you that these have been deprecated.
admine334c472006-10-21 19:44:22 +0000232
adminb0dd10f2006-08-25 17:25:49 +0000233 if ($severity == E_STRICT)
234 {
235 return;
236 }
237
admin7099a582006-10-10 17:47:59 +0000238 $error =& load_class('Exceptions');
adminb0dd10f2006-08-25 17:25:49 +0000239
admine334c472006-10-21 19:44:22 +0000240 // Should we display the error?
adminb0dd10f2006-08-25 17:25:49 +0000241 // We'll get the current error_reporting level and add its bits
242 // with the severity bits to find out.
243
244 if (($severity & error_reporting()) == $severity)
245 {
246 $error->show_php_error($severity, $message, $filepath, $line);
247 }
248
249 // Should we log the error? No? We're done...
admin49439ff2006-10-30 04:39:12 +0000250 $config =& get_config();
adminb06d69f2006-10-13 21:44:17 +0000251 if ($config['log_threshold'] == 0)
adminb0dd10f2006-08-25 17:25:49 +0000252 {
253 return;
254 }
255
256 $error->log_exception($severity, $message, $filepath, $line);
257}
258
259
260?>