blob: b0df529217bafcb09d924bb2189ba17071b0b654 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000026 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000027 * @link http://codeigniter.com/user_guide/
Derek Allardd2df9bc2007-04-15 17:41:17 +000028 */
29
30// ------------------------------------------------------------------------
31
32/**
Derek Jonesa25530f2008-01-28 17:11:02 +000033 * Tests for file writability
34 *
35 * is_writable() returns TRUE on Windows servers
36 * when you really can't write to the file
37 * as the OS reports to PHP as FALSE only if the
38 * read-only attribute is marked. Ugh?
39 *
40 * @access private
41 * @return void
Derek Allard0fd8f022008-05-13 02:01:47 +000042 */
Derek Jonesa25530f2008-01-28 17:11:02 +000043function is_really_writable($file)
44{
45 if (is_dir($file))
46 {
47 $file = rtrim($file, '/').'/'.md5(rand(1,100));
Derek Allard0fd8f022008-05-13 02:01:47 +000048
Derek Jones3be20e22008-05-05 20:07:09 +000049 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
Derek Jonesa25530f2008-01-28 17:11:02 +000050 {
51 return FALSE;
52 }
Derek Allard0fd8f022008-05-13 02:01:47 +000053
Derek Jonesa25530f2008-01-28 17:11:02 +000054 fclose($fp);
Derek Jones3ad8efe2008-04-04 18:56:04 +000055 @chmod($file, DIR_WRITE_MODE);
Derek Jonesa25530f2008-01-28 17:11:02 +000056 @unlink($file);
57 return TRUE;
58 }
Derek Jones3be20e22008-05-05 20:07:09 +000059 elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
Derek Jonesa25530f2008-01-28 17:11:02 +000060 {
61 return FALSE;
62 }
63
64 fclose($fp);
65 return TRUE;
66}
67
68// ------------------------------------------------------------------------
69
70/**
Derek Allardd2df9bc2007-04-15 17:41:17 +000071* Class registry
72*
73* This function acts as a singleton. If the requested class does not
74* exist it is instantiated and set to a static variable. If it has
75* previously been instantiated the variable is returned.
76*
77* @access public
78* @param string the class name being requested
79* @param bool optional flag that lets classes get loaded but not instantiated
80* @return object
81*/
82function &load_class($class, $instantiate = TRUE)
83{
84 static $objects = array();
85
86 // Does the class exist? If so, we're done...
87 if (isset($objects[$class]))
88 {
89 return $objects[$class];
90 }
Derek Allard0fd8f022008-05-13 02:01:47 +000091
Derek Allardd2df9bc2007-04-15 17:41:17 +000092 // If the requested class does not exist in the application/libraries
93 // folder we'll load the native class from the system/libraries folder.
94 if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
95 {
96 require(BASEPATH.'libraries/'.$class.EXT);
97 require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
98 $is_subclass = TRUE;
99 }
100 else
101 {
102 if (file_exists(APPPATH.'libraries/'.$class.EXT))
103 {
104 require(APPPATH.'libraries/'.$class.EXT);
105 $is_subclass = FALSE;
106 }
107 else
108 {
109 require(BASEPATH.'libraries/'.$class.EXT);
110 $is_subclass = FALSE;
111 }
112 }
113
114 if ($instantiate == FALSE)
115 {
116 $objects[$class] = TRUE;
117 return $objects[$class];
118 }
Derek Allard0fd8f022008-05-13 02:01:47 +0000119
Derek Allardd2df9bc2007-04-15 17:41:17 +0000120 if ($is_subclass == TRUE)
121 {
122 $name = config_item('subclass_prefix').$class;
123 $objects[$class] =& new $name();
124 return $objects[$class];
125 }
126
127 $name = ($class != 'Controller') ? 'CI_'.$class : $class;
128
129 $objects[$class] =& new $name();
130 return $objects[$class];
131}
132
133/**
134* Loads the main config.php file
135*
136* @access private
137* @return array
138*/
139function &get_config()
140{
141 static $main_conf;
Derek Allard0fd8f022008-05-13 02:01:47 +0000142
Derek Jones0b59f272008-05-13 04:22:33 +0000143 if ( ! isset($main_conf))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000144 {
Derek Jones0b59f272008-05-13 04:22:33 +0000145 if ( ! file_exists(APPPATH.'config/config'.EXT))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000146 {
147 exit('The configuration file config'.EXT.' does not exist.');
148 }
Derek Allard0fd8f022008-05-13 02:01:47 +0000149
Derek Allardd2df9bc2007-04-15 17:41:17 +0000150 require(APPPATH.'config/config'.EXT);
Derek Allard0fd8f022008-05-13 02:01:47 +0000151
Derek Jones0b59f272008-05-13 04:22:33 +0000152 if ( ! isset($config) OR ! is_array($config))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000153 {
154 exit('Your config file does not appear to be formatted correctly.');
155 }
156
157 $main_conf[0] =& $config;
158 }
159 return $main_conf[0];
160}
161
162/**
163* Gets a config item
164*
165* @access public
166* @return mixed
167*/
168function config_item($item)
169{
170 static $config_item = array();
171
Derek Jones0b59f272008-05-13 04:22:33 +0000172 if ( ! isset($config_item[$item]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000173 {
174 $config =& get_config();
Derek Allard0fd8f022008-05-13 02:01:47 +0000175
Derek Jones0b59f272008-05-13 04:22:33 +0000176 if ( ! isset($config[$item]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000177 {
178 return FALSE;
179 }
180 $config_item[$item] = $config[$item];
181 }
182
183 return $config_item[$item];
184}
185
186
187/**
188* Error Handler
189*
190* This function lets us invoke the exception class and
191* display errors using the standard error template located
192* in application/errors/errors.php
193* This function will send the error page directly to the
194* browser and exit.
195*
196* @access public
197* @return void
198*/
199function show_error($message)
200{
201 $error =& load_class('Exceptions');
202 echo $error->show_error('An Error Was Encountered', $message);
203 exit;
204}
205
206
207/**
208* 404 Page Handler
209*
210* This function is similar to the show_error() function above
211* However, instead of the standard error template it displays
212* 404 errors.
213*
214* @access public
215* @return void
216*/
217function show_404($page = '')
218{
219 $error =& load_class('Exceptions');
220 $error->show_404($page);
221 exit;
222}
223
224
225/**
226* Error Logging Interface
227*
228* We use this as a simple mechanism to access the logging
229* class and send messages to be logged.
230*
231* @access public
232* @return void
233*/
234function log_message($level = 'error', $message, $php_error = FALSE)
235{
236 static $LOG;
237
238 $config =& get_config();
239 if ($config['log_threshold'] == 0)
240 {
241 return;
242 }
243
244 $LOG =& load_class('Log');
245 $LOG->write_log($level, $message, $php_error);
246}
247
248/**
249* Exception Handler
250*
251* This is the custom exception handler that is declaired at the top
252* of Codeigniter.php. The main reason we use this is permit
253* PHP errors to be logged in our own log files since we may
254* not have access to server logs. Since this function
255* effectively intercepts PHP errors, however, we also need
256* to display errors based on the current error_reporting level.
257* We do that with the use of a PHP error template.
258*
259* @access private
260* @return void
261*/
262function _exception_handler($severity, $message, $filepath, $line)
263{
264 // We don't bother with "strict" notices since they will fill up
265 // the log file with information that isn't normally very
266 // helpful. For example, if you are running PHP 5 and you
267 // use version 4 style class functions (without prefixes
268 // like "public", "private", etc.) you'll get notices telling
269 // you that these have been deprecated.
270
271 if ($severity == E_STRICT)
272 {
273 return;
274 }
275
276 $error =& load_class('Exceptions');
277
278 // Should we display the error?
279 // We'll get the current error_reporting level and add its bits
280 // with the severity bits to find out.
281
282 if (($severity & error_reporting()) == $severity)
283 {
284 $error->show_php_error($severity, $message, $filepath, $line);
285 }
286
287 // Should we log the error? No? We're done...
288 $config =& get_config();
289 if ($config['log_threshold'] == 0)
290 {
291 return;
292 }
293
294 $error->log_exception($severity, $message, $filepath, $line);
295}
296
297
Derek Allard0fd8f022008-05-13 02:01:47 +0000298
299/* End of file Common.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000300/* Location: ./system/codeigniter/Common.php */