<?php
/**
 * $Id: ErrorHandler.php 959 2008-07-29 06:34:41Z pawy $
 * Obsluga bledow
 *
 * @param integer $errno
 * @param string $errstr
 * @param string $errfile
 * @param integer $errline
 * @return unknown
 */
function AppErrorHandler($errno, $errstr, $errfile, $errline) {

	
	switch ($errno) {

		case E_STRICT:
			break;

		
		case E_NOTICE:
			MFLog::Warn("Line: ".$errline." File: ".$errfile." Message: ".$errstr." Referer: ".getenv('HTTP_REFERER'));
			
			if(Config::Get('ERROR_REDIRECT') != 'true') {
				echo "<span style=\"color: red;\">Unknown error type: [$errno] $errstr<br /></span>\n";
				echo $errfile.':'.$errline;
			}
			break;


		default:
			if(!preg_match('/mysql_connect()/', $errstr)) {
			MFLog::Error("Line: ".$errline." File: ".$errfile." Message: ".$errstr." Referer: ".getenv('HTTP_REFERER'));

			if(Config::Get('ERROR_REDIRECT') == 'true') {
				if(getenv('HTTP_REFERER')==Config::Get('URL_MAIN')) {
					Header("Location: ".Config::Get('URL_MAIN')."/error.html");
				} else {
					header("HTTP/1.0 302 Moved Temporarily");
					Header("Location: ".Config::Get('URL_MAIN'));
				}
			} else {
				echo "<span style=\"color: red;\">Unknown error type: [$errno] $errstr<br /></span>\n";
				echo $errfile.':'.$errline;
				break;
			}
			}
				
	}

	/* Don't execute PHP internal error handler */
	return true;
}

set_error_handler("AppErrorHandler");


/**
 * Obsluguje wyjatki
 *
 * @param Exception $exception
 */
function ExceptionHandler($exception) {


	
	MFLog::Error("Line: ".$exception->getLine()." Message: ".$exception->getMessage()." Referer: ".getenv('HTTP_REFERER'));

	if(ERROR_REDIRECT == 'true') {
		if(getenv('HTTP_REFERER')==Config::Get('URL_MAIN')) {
			Header("Location: ".Config::Get('URL_MAIN')."/error.html");
		} else {
			header("HTTP/1.0 302 Moved Temporarily");
			Header("Location: ".Config::Get('URL_MAIN'));
		}
	} else {
		echo "<br /><span style=\"color: red;\">Nie obsluzony wyjatek: " , $exception->getMessage(), "</span>\n".getenv('HTTP_REFERER');
     echo $exception;
	}

}

set_exception_handler('ExceptionHandler');
if(Config::Get('ERROR_REDIRECT') == true) {
ini_set('html_errors',false);
ini_set('error_prepend_string','<html><head><META http-equiv="refresh" content="0;URL='.Config::Get('FATAL_ERROR_URL').'"></head></html>');
//ini_set('error_append_string','"></head></html>');
}
?>