<?php

class Error{
 
 	static $errorArr;
 	
    public function __construct(){
     	
     	self::$errorArr = array();

        set_error_handler(array($this,"fwindows_error_handler"));
       
    }      
    
	function fwindows_error_handler($errno, $errstr, $errfile, $errline){
	 
	    $errno = $errno & error_reporting();
	    if($errno == 0) return;
	    if(!defined('E_STRICT'))            define('E_STRICT', 2048);
	    if(!defined('E_RECOVERABLE_ERROR')) define('E_RECOVERABLE_ERROR', 4096);
	    
 		$errorStr = "";
	    switch($errno){
	        case E_ERROR:               $errorStr .=  "Error";                  break;
	        case E_WARNING:             $errorStr .=  "Warning";                break;
	        case E_PARSE:               $errorStr .=  "Parse Error";            break;
	        case E_NOTICE:              $errorStr .=  "Notice";                 break;
	        case E_CORE_ERROR:          $errorStr .=  "Core Error";             break;
	        case E_CORE_WARNING:        $errorStr .=  "Core Warning";           break;
	        case E_COMPILE_ERROR:       $errorStr .=  "Compile Error";          break;
	        case E_COMPILE_WARNING:     $errorStr .=  "Compile Warning";        break;
	        case E_USER_ERROR:          $errorStr .=  "User Error";             break;
	        case E_USER_WARNING:        $errorStr .=  "User Warning";           break;
	        case E_USER_NOTICE:         $errorStr .=  "User Notice";            break;
	        case E_STRICT:              $errorStr .=  "Strict Notice";          break;
	        case E_RECOVERABLE_ERROR:   $errorStr .=  "Recoverable Error";      break;
	        default:                    $errorStr .=  "Unknown error ($errno)"; break;
	    }
	    

		//$errorStr .=  ":</b> <i>$errstr</i> in <b>$errfile</b> on line <b>$errline</b>\n";
		$errorStr .=  ": $errstr in $errfile on line $errline\n";

	    if(function_exists('debug_backtrace')){
	        $backtrace = debug_backtrace();
	        array_shift($backtrace);
	        foreach($backtrace as $i=>$l){
	         
			   /*     $errorStr .=   "[$i] in function <b>{$l['class']}{$l['type']}{$l['function']}</b>";
		        if($l['file']) $errorStr .=   " in <b>{$l['file']}</b>";
		        if($l['line']) $errorStr .=   " on line <b>{$l['line']}</b>";
		        $errorStr .= "\n";*/
            
            
	            $errorStr .=   "[$i] in function '{$l['class']}{$l['type']}{$l['function']}' ";
	            if($l['file']) $errorStr .=   "in '{$l['file']}' ";
	            if($l['line']) $errorStr .=   " on line {$l['line']}";
	            $errorStr .=   "\n";
	        }
	    }
	   
	    $errorStr .= "";
	    
	    self::$errorArr[] =  $errorStr;
		/*
	    if(isset($GLOBALS['error_fatal'])){
	        if($GLOBALS['error_fatal'] & $errno) die('fatal');
	    }*/
	    
	}

  /*public function errorHandler($errno, $errstr, $errfile, $errline){
     	
     		if($errno == E_NOTICE) return false;
     		if($errno == E_STRICT) return false;
     		
     		$errorStr = "";
     		
     		
     		print ":</b> <i>$errstr</i> in <b>$errfile</b> on line <b>$errline</b>\n";
			if(function_exists('debug_backtrace')){
			        //print "backtrace:\n";
			        $backtrace = debug_backtrace();
			        array_shift($backtrace);
			        foreach($backtrace as $i=>$l){
			            print "[$i] in function <b>{$l['class']}{$l['type']}{$l['function']}</b>";
			            if($l['file']) print " in <b>{$l['file']}</b>";
			            if($l['line']) print " on line <b>{$l['line']}</b>";
			            print "\n";
			        }
			    }
			     		
     		
     		
     		
     		self::addError($errno,$errstr,$errfile,$errline);
     		
     		if($this->print){
	            $errtime=date("H:i:s - d/m/y");
	            $error_message="An error occured on ".$errtime."<br>";
	            $error_message .="Details are as follows<br>";
	            $error_message .="Error Number : $errno<br>";
	            $error_message .=$errstr."\nOccured in ".$errfile."<br>";
	            $error_message .="On Line $errline <br><br>";
	            $error_message .=str_repeat("-",50);
	            $error_message .="<br>";
				self::printDebug($error_message);
			}
			return true;
    }*/
    
    static function printDebug($message){
            $message="<p>Debug : ".$message."</p>";
            print $message;
    }
    
    static function getErrorArray(){
    	return self::$errorArr;
    }
    
	static function addError($errno,$errstr,$errfile,$errline){
     
     	$errorObj = (object) array();
     	$errorObj->errno = $errno;
		$errorObj->errstr = $errstr;
		$errorObj->errfile = $errfile;
		$errorObj->errline = $errline;
    	self::$errorArr[] = $errorObj;
    	
    }
}
?>