<?php

/**
 * @class myException
 * @brief Manage Exceptions
 * 
 * @author Benjamin Mercier
 * @data 01/03/2003
 * @version 0.1
 */
class myException extends exception {

	/**
	 * @brief Receive exceptions and manage it
	 * @param $exception_message -> Text generated by exception
	*/
	public function __construct($exception_message, $error_code = 0)
	{
		parent::__construct($exception_message);
		$this->insertInLogFile($exception_message, $error_code);
	}

	/**
	 * @brief Insert data from exceptions in logfile
	*/
	public function insertInLogFile($exception_message, $error_code)
	{
		$get = null;
		$post = null;
		$session = null;
		$included_files = null;
		foreach ( $_GET as $key => $value ) {
			$get .= "\n  -> ".escapeshellcmd($key).' : '.$value;
		}
		if ( is_null($get) ) $get = 'none';

		foreach ( $_POST as $key => $value ) {
			$post .= "\n  -> ".escapeshellcmd($key).' : '.$value;
		}
		if ( is_null($post) ) $post = 'none';

		$files  = get_included_files();
		foreach ( $files as $key => $value ) {
			$included_files .= "\n  -> ".$key.' : '.$value;
		}
		if ( is_null($included_files) ) $included_files = 'none';

		$message = "An error has been generated : \n";
		$message .= "Warning : The content is protected, is not the reel content (POST,GET) !\n";
		$message .= 'Date : '.date('Y-m-d H:i:s')."\n";
		$message .= 'Error : '.$exception_message."\n";
		$message .= 'File : '.$this->getFile()."\n";
		$message .= 'Line : '.$this->getLine()."\n";
		$message .= 'Code : '.$this->getCode()."\n";
		if ( isset($_SERVER['HTTP_USER_AGENT']) ) $message .= 'Browser : '.@escapeshellcmd($_SERVER['HTTP_USER_AGENT'])."\n";
		$message .= 'IP Address : '.$_SERVER['REMOTE_ADDR']."\n";
		$message .= 'Call Page : '.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.@escapeshellcmd($_SERVER['QUERY_STRING'])."\n";
		$message .= 'Request method : '.$_SERVER['REQUEST_METHOD']."\n";
		$message .= 'Request timer : '.$_SERVER['REQUEST_TIME']."\n";
		if ( isset($_SERVER['HTTP_REFERER']) ) $message .= 'Referer : '.@escapeshellcmd($_SERVER['HTTP_REFERER'])."\n";
		$message .= 'Script path : '.$_SERVER['SCRIPT_FILENAME']."\n";
		$message .= 'GET value : '.$get."\n";
		$message .= 'POST value : '.$post."\n";
		$message .= 'Included files : '.$included_files."\n";
		$message .= "Execution traces : \n".$this->getTraceAsString()."\n";
		$message .= "---------------------------------------------------\n";
		$message .= "\n\n";


			$file_handle = fopen (PATH_LOGS.date('Y-m-d').'.log', 'a+');
		if ( !$file_handle ) {
			mail('root@kelio.org', 'CRITICAL ERROR !!', 'The logfile of exceptions can\'t be open !');
		}

		$file_writen = fwrite ( $file_handle, $message ) ;
		if ( !$file_writen ) {
			mail('root@kelio.org', 'CRITICAL ERROR !!', 'The logfile of exception can\'t be written !');
		}

		fclose($file_handle);	
	
		if ( defined('DEBUG') and (preg_match('`ajax.php`i',$_SERVER['PHP_SELF']) == 0) ) {
			$unique_id = mt_rand(0,2500);
			$echo = "<div style='font-color:red;width:90%;background-color:#91020b;margin:auto;margin-bottom:20px;padding:20px;color:#fff;' id='".$unique_id."'>";
			$echo .= "An error has been generated : <br />\n";
			$echo .= 'Error : <strong>'.nl2br($exception_message)."</strong><br />\n";
			$echo .= 'File  : <strong>'.$this->getFile()."</strong><br />\n";
			$echo .= 'Line  : <strong>'.$this->getLine()."</strong><br />\n";
			$echo .= 'Code  : <strong>'.$error_code."</strong><br />\n";
			$echo .= "Trace :\n".nl2br($this->getTraceAsString())."<br /><br />\n";
			$echo .= "<a href='javascript:;' style='color:#fff' onclick=\"$('#".$unique_id."-2').css('display', '');\">For see complete rapport, clic here</a><br />\n";
			$echo .= "<a href='javascript:;' style='color:#fff' onclick=\"$('#".$unique_id."').css('display', 'none');\">For quit this error, clic here</a><br />\n";
			$echo .= "<div style='display:none;color:#fff;margin-top:20px;margin-bottom:20px;width:100%;' id='".$unique_id."-2'>".nl2br($message)."</div><br />\n";
			$echo .= "</div>\n\n\n\n";
			echo $echo;
		}
	} // End of insertInLogFile

	/**
	 * @brief Display HTML error
	*/
	public function displayErrorMessage()
	{
		@$page = file_get_contents(PAGE_CRITICAL_ERROR);
		if ( !$page ) {
			die(NOLOG_ERROR);
		} else {
			echo $page;
		}
		die();
	} // End of displayErrorMessage
	
	/**
	 * @brief Display 'critical_error' for ajax
	*/
	public function displayCriticalError()
	{
		echo 'critical_error';
		die();
	}

}
/**
 * @brief Manage PHP error
 */
function myErrorHandler($type, $string, $file, $line, $context){
	try {
		switch ($type) {
			case E_USER_WARNING:
				throw new MyException("An PHP error has been generated :\n  -> Error : ".$string."\n  -> File : ".$file."\n  -> Line : ".$line."\n  -> Context : ".$context, -999);
			break;
			case E_USER_NOTICE:
				throw new MyException("An PHP error has been generated :\n  -> Error : ".$string."\n  -> File : ".$file."\n  -> Line : ".$line."\n  -> Context : ".$context, -998);
			break;
			case E_WARNING:
				throw new MyException("An PHP error has been generated :\n  -> Error : ".$string."\n  -> File : ".$file."\n  -> Line : ".$line."\n  -> Context : ".$context, -899);
			break;
			case E_NOTICE:
				throw new MyException("An PHP error has been generated :\n  -> Error : ".$string."\n  -> File : ".$file."\n  -> Line : ".$line."\n  -> Context : ".$context, -898);
			break;
			case E_ERROR:
				throw new MyException("An PHP error has been generated :\n  -> Error : ".$string."\n  -> File : ".$file."\n  -> Line : ".$line."\n  -> Context : ".$context, -797);
			break;
			default:
				throw new MyException("An PHP error has been generated :\n  -> Error : ".$string."\n  -> File : ".$file."\n  -> Line : ".$line."\n  -> Context : ".$context, -797);
		}
	} catch ( MyException $oException ) {
	}
}
set_error_handler("myErrorHandler");

?>
