Location: PHPKode > projects > Simple Way to Usenet > libs/core/err/errors.class.php
<?php
/**
 *  
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2009  Benjamin Gillissen
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */

class errors {
	
	private static $LOG;
	
	public static function broadcast($err){
		if ( !CORE::isError($err) ){ return FALSE; }
		if ( ! isset(self::$LOG) ){ self::init(); }
		self::$LOG->log($err);
		if ( self::isfatal($err) ){	self::fatalerr($err); }
		$err->__destruct();
		unset($err);
	}
	
	public static function init(){	
		if ( isset(self::$LOG) ){ return; }
		self::$LOG = new logs();
	}
	
	public static function isfatal($err){
		return ($err->getlvl() == CORE_LOG_ALERT) 
			OR ($err->getlvl() == CORE_LOG_CRIT) 
			OR ($err->getlvl() == CORE_LOG_EMERG);
	}
		
	public static function getnew($msg, $lvl=CORE_LOG_NOTICE, $context='unknow'){
		return new error($msg, $lvl, $context);
	}
	
	public static function raise($msg, $lvl, $context){
		self::broadcast(new error($msg, $lvl, $context));
	}
	
	public static function fetch_debugchan($backtrace, $color){
		$chan = configs::get('errors', 'debug_chan');
		return self::$LOG->fetch($chan, $backtrace, $color);
		
	}
	
	public static function lvl_to_dsp($lvl){
		switch($lvl){
			case CORE_LOG_NOTICE	: $o = 'notice';break;
			case CORE_LOG_DEBUG		: $o = 'debug';break;
			case CORE_LOG_WARNING	: $o = 'warning';break;
			case CORE_LOG_INFO		: $o = 'info';break;
			case CORE_LOG_ERR		: $o = 'err';break;
			case CORE_LOG_ALERT		: $o = 'alert';break;
			case CORE_LOG_CRIT 		: $o = 'crit';break;
			case CORE_LOG_EMERG		: $o = 'emerg';break;
			default 				: $o = 'unknow';break;
		}
		return $o;
	}
	
	private static function fatalerr($err){
		if ( TRUE === ajax::isAjaxReq() ){ 
			self::ajaxerr($err);
		} elseif ( TRUE === @constant('THEME_HEADER_ISSENT') ){
			self::pagederr($err);
		} else {
			self::failsaverr($err);
		}
		exit;
	}
	
	private static function failsaverr($err){
		//no pgen's header has been sent.
		if ( ! headers_sent($file, $line) ){
			//if no output started, we enforce charset.
			header("Content-type: text/html; charset=UTF-8");
		}
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml" lang="EN">
			<head>
				<title id="title">Core Error</title>
				<meta name="generator" content="CoreV5" />
				<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
			</head>
			<body>
			<div style="position:absolute;left:100px;top:100px;z-index:5;background-color:gray;color:silver;border:2px black solid">
				<div style="width:100%;background-color:white;color:black;font-size:16pt;text-align:center"><br/>'.$err->getmsg().'<br/></div>
				<div style="">Request\'s Events :<br/>
			';
		while( FALSE !== ( $event = errors::fetch_debugchan(FALSE, TRUE)) ){ echo $event['event']."<br/>\n".$event['backtrace']."<br/>\n"; }
		echo '</div></div></body></html>';
		$err->__destruct();
		return TRUE;
	}
	
	private static function pagederr($err){
		//header is sent,
		//output some html above everything, with err msg and log cache content if any.
		echo langs::recode_for_client('<div style="position:absolute;left:100px;top:100px;z-index:5;background-color:gray;color:silver;border:2px black solid">
											<div style="width:100%;background-color:white;color:black;font-size:16pt;text-align:center"><br/>'.$err->getmsg().'<br/></div>
											<div style="">Request\'s Events :<br/>');
		while(FALSE !== ($event = errors::fetch_debugchan(TRUE, TRUE)) ){
			echo langs::recode_for_client('<div>'.$event['event'].'<br/>'.$event['backtrace'].'</div>' );
		}
		echo langs::recode_for_client('</div></div></body></html>');
		$err->__destruct();
		return TRUE;
	}
	
	private static function ajaxerr($err){
		echo $err->getmsg();
		//ajax, 
		//output a valid xml reply : a js alert, with err msg
	}
}
Return current item: Simple Way to Usenet