PageRenderTime 52ms CodeModel.GetById 35ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/halogy/codeigniter/Common.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 421 lines | 220 code | 58 blank | 143 comment | 38 complexity | 03d6e454dc152f9503b2a4f7316cb080 MD5 | raw file
  1<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2/**
  3 * CodeIgniter
  4 *
  5 * An open source application development framework for PHP 4.3.2 or newer
  6 *
  7 * @package		CodeIgniter
  8 * @author		ExpressionEngine Dev Team
  9 * @copyright	Copyright (c) 2008 - 2009, EllisLab, Inc.
 10 * @license		http://codeigniter.com/user_guide/license.html
 11 * @link		http://codeigniter.com
 12 * @since		Version 1.0
 13 * @filesource
 14 */
 15
 16// ------------------------------------------------------------------------
 17
 18/**
 19* Determines if the current version of PHP is greater then the supplied value
 20*
 21* Since there are a few places where we conditionally test for PHP > 5
 22* we'll set a static variable.
 23*
 24* @access	public
 25* @param	string
 26* @return	bool
 27*/
 28function is_php($version = '5.0.0')
 29{
 30	static $_is_php;
 31	$version = (string)$version;
 32	
 33	if ( ! isset($_is_php[$version]))
 34	{
 35		$_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
 36	}
 37
 38	return $_is_php[$version];
 39}
 40
 41// ------------------------------------------------------------------------
 42
 43/**
 44 * Tests for file writability
 45 *
 46 * is_writable() returns TRUE on Windows servers when you really can't write to 
 47 * the file, based on the read-only attribute.  is_writable() is also unreliable
 48 * on Unix servers if safe_mode is on. 
 49 *
 50 * @access	private
 51 * @return	void
 52 */
 53function is_really_writable($file)
 54{	
 55	// If we're on a Unix server with safe_mode off we call is_writable
 56	if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
 57	{
 58		return is_writable($file);
 59	}
 60
 61	// For windows servers and safe_mode "on" installations we'll actually
 62	// write a file then read it.  Bah...
 63	if (is_dir($file))
 64	{
 65		$file = rtrim($file, '/').'/'.md5(rand(1,100));
 66
 67		if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
 68		{
 69			return FALSE;
 70		}
 71
 72		fclose($fp);
 73		@chmod($file, DIR_WRITE_MODE);
 74		@unlink($file);
 75		return TRUE;
 76	}
 77	elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
 78	{
 79		return FALSE;
 80	}
 81
 82	fclose($fp);
 83	return TRUE;
 84}
 85
 86// ------------------------------------------------------------------------
 87
 88/**
 89* Class registry
 90*
 91* This function acts as a singleton.  If the requested class does not
 92* exist it is instantiated and set to a static variable.  If it has
 93* previously been instantiated the variable is returned.
 94*
 95* @access	public
 96* @param	string	the class name being requested
 97* @param	bool	optional flag that lets classes get loaded but not instantiated
 98* @return	object
 99*/
100function &load_class($class, $instantiate = TRUE)
101{
102	static $objects = array();
103
104	// Does the class exist?  If so, we're done...
105	if (isset($objects[$class]))
106	{
107		return $objects[$class];
108	}
109
110	// If the requested class does not exist in the application/libraries
111	// folder we'll load the native class from the system/libraries folder.	
112	if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
113	{
114		require(BASEPATH.'libraries/'.$class.EXT);
115		require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
116		$is_subclass = TRUE;
117	}
118	else
119	{
120		if (file_exists(APPPATH.'libraries/'.$class.EXT))
121		{
122			require(APPPATH.'libraries/'.$class.EXT);
123			$is_subclass = FALSE;
124		}
125		else
126		{
127			require(BASEPATH.'libraries/'.$class.EXT);
128			$is_subclass = FALSE;
129		}
130	}
131
132	if ($instantiate == FALSE)
133	{
134		$objects[$class] = TRUE;
135		return $objects[$class];
136	}
137
138	if ($is_subclass == TRUE)
139	{
140		$name = config_item('subclass_prefix').$class;
141
142		$objects[$class] =& instantiate_class(new $name());
143		return $objects[$class];
144	}
145
146	$name = ($class != 'Controller') ? 'CI_'.$class : $class;
147
148	$objects[$class] =& instantiate_class(new $name());
149	return $objects[$class];
150}
151
152/**
153 * Instantiate Class
154 *
155 * Returns a new class object by reference, used by load_class() and the DB class.
156 * Required to retain PHP 4 compatibility and also not make PHP 5.3 cry.
157 *
158 * Use: $obj =& instantiate_class(new Foo());
159 * 
160 * @access	public
161 * @param	object
162 * @return	object
163 */
164function &instantiate_class(&$class_object)
165{
166	return $class_object;
167}
168
169/**
170* Loads the main config.php file
171*
172* @access	private
173* @return	array
174*/
175function &get_config()
176{
177	static $main_conf;
178
179	if ( ! isset($main_conf))
180	{
181		if ( ! file_exists(APPPATH.'config/config'.EXT))
182		{
183			exit('The configuration file config'.EXT.' does not exist.');
184		}
185
186		require(APPPATH.'config/config'.EXT);
187
188		if ( ! isset($config) OR ! is_array($config))
189		{
190			exit('Your config file does not appear to be formatted correctly.');
191		}
192
193		$main_conf[0] =& $config;
194	}
195	return $main_conf[0];
196}
197
198/**
199* Gets a config item
200*
201* @access	public
202* @return	mixed
203*/
204function config_item($item)
205{
206	static $config_item = array();
207
208	if ( ! isset($config_item[$item]))
209	{
210		$config =& get_config();
211
212		if ( ! isset($config[$item]))
213		{
214			return FALSE;
215		}
216		$config_item[$item] = $config[$item];
217	}
218
219	return $config_item[$item];
220}
221
222
223/**
224* Error Handler
225*
226* This function lets us invoke the exception class and
227* display errors using the standard error template located
228* in application/errors/errors.php
229* This function will send the error page directly to the
230* browser and exit.
231*
232* @access	public
233* @return	void
234*/
235function show_error($message, $status_code = 500)
236{
237	$error =& load_class('Exceptions');
238	echo $error->show_error('An Error Was Encountered', $message, 'error_general', $status_code);
239	exit;
240}
241
242
243/**
244* 404 Page Handler
245*
246* This function is similar to the show_error() function above
247* However, instead of the standard error template it displays
248* 404 errors.
249*
250* @access	public
251* @return	void
252*/
253function show_404($page = '')
254{
255	$error =& load_class('Exceptions');
256	$error->show_404($page);
257	exit;
258}
259
260
261/**
262* Error Logging Interface
263*
264* We use this as a simple mechanism to access the logging
265* class and send messages to be logged.
266*
267* @access	public
268* @return	void
269*/
270function log_message($level = 'error', $message, $php_error = FALSE)
271{
272	static $LOG;
273	
274	$config =& get_config();
275	if ($config['log_threshold'] == 0)
276	{
277		return;
278	}
279
280	$LOG =& load_class('Log');
281	$LOG->write_log($level, $message, $php_error);
282}
283
284
285/**
286 * Set HTTP Status Header
287 *
288 * @access	public
289 * @param	int 	the status code
290 * @param	string	
291 * @return	void
292 */
293function set_status_header($code = 200, $text = '')
294{
295	$stati = array(
296						200	=> 'OK',
297						201	=> 'Created',
298						202	=> 'Accepted',
299						203	=> 'Non-Authoritative Information',
300						204	=> 'No Content',
301						205	=> 'Reset Content',
302						206	=> 'Partial Content',
303
304						300	=> 'Multiple Choices',
305						301	=> 'Moved Permanently',
306						302	=> 'Found',
307						304	=> 'Not Modified',
308						305	=> 'Use Proxy',
309						307	=> 'Temporary Redirect',
310
311						400	=> 'Bad Request',
312						401	=> 'Unauthorized',
313						403	=> 'Forbidden',
314						404	=> 'Not Found',
315						405	=> 'Method Not Allowed',
316						406	=> 'Not Acceptable',
317						407	=> 'Proxy Authentication Required',
318						408	=> 'Request Timeout',
319						409	=> 'Conflict',
320						410	=> 'Gone',
321						411	=> 'Length Required',
322						412	=> 'Precondition Failed',
323						413	=> 'Request Entity Too Large',
324						414	=> 'Request-URI Too Long',
325						415	=> 'Unsupported Media Type',
326						416	=> 'Requested Range Not Satisfiable',
327						417	=> 'Expectation Failed',
328
329						500	=> 'Internal Server Error',
330						501	=> 'Not Implemented',
331						502	=> 'Bad Gateway',
332						503	=> 'Service Unavailable',
333						504	=> 'Gateway Timeout',
334						505	=> 'HTTP Version Not Supported'
335					);
336
337	if ($code == '' OR ! is_numeric($code))
338	{
339		show_error('Status codes must be numeric', 500);
340	}
341
342	if (isset($stati[$code]) AND $text == '')
343	{				
344		$text = $stati[$code];
345	}
346	
347	if ($text == '')
348	{
349		show_error('No status text available.  Please check your status code number or supply your own message text.', 500);
350	}
351	
352	$server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
353
354	if (substr(php_sapi_name(), 0, 3) == 'cgi')
355	{
356		header("Status: {$code} {$text}", TRUE);
357	}
358	elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
359	{
360		header($server_protocol." {$code} {$text}", TRUE, $code);
361	}
362	else
363	{
364		header("HTTP/1.1 {$code} {$text}", TRUE, $code);
365	}
366}
367
368
369/**
370* Exception Handler
371*
372* This is the custom exception handler that is declaired at the top
373* of Codeigniter.php.  The main reason we use this is permit
374* PHP errors to be logged in our own log files since we may
375* not have access to server logs. Since this function
376* effectively intercepts PHP errors, however, we also need
377* to display errors based on the current error_reporting level.
378* We do that with the use of a PHP error template.
379*
380* @access	private
381* @return	void
382*/
383function _exception_handler($severity, $message, $filepath, $line)
384{	
385	 // We don't bother with "strict" notices since they will fill up
386	 // the log file with information that isn't normally very
387	 // helpful.  For example, if you are running PHP 5 and you
388	 // use version 4 style class functions (without prefixes
389	 // like "public", "private", etc.) you'll get notices telling
390	 // you that these have been deprecated.
391	
392	if ($severity == E_STRICT)
393	{
394		return;
395	}
396
397	$error =& load_class('Exceptions');
398
399	// Should we display the error?
400	// We'll get the current error_reporting level and add its bits
401	// with the severity bits to find out.
402	
403	if (($severity & error_reporting()) == $severity)
404	{
405		$error->show_php_error($severity, $message, $filepath, $line);
406	}
407	
408	// Should we log the error?  No?  We're done...
409	$config =& get_config();
410	if ($config['log_threshold'] == 0)
411	{
412		return;
413	}
414
415	$error->log_exception($severity, $message, $filepath, $line);
416}
417
418
419
420/* End of file Common.php */
421/* Location: ./system/codeigniter/Common.php */