<?php 
class Logger
{

   private static $print_error = false;

   public static function customErrorMsg() {     
	   //echo  "<p>An error occured, The error has been reported to the development team and will be addresses asap.</p>"; 
	   //header("Location: ".BASE_URL."/errorPage.html");
	   exit; 	   	   
   }

   public static function exception_handler($e){
       self::newMessage($e);
       self::customErrorMsg();
   }

   public static function error_handler($number, $message, $file, $line){  
       $msg = "$message in $file on line $line";  
       
       if ( ($number !== E_NOTICE) && ($number < 2048) ) {  
           self::errorMessage($msg);
           self::customErrorMsg();
       } 
       return 0; 
   }
	
   public static function phpErrors($Error, $print_error = false, $clear = false, $error_file = 'errorlog.php') {
	   $message = $Error["message"];
	   $code = $Error["type"];
	   $file = $Error["file"];
	   $line = $Error["line"];
	   $trace ="";
	   $date = date('M d, Y G:iA');
	   
	   $errorDetails["error_date"] = $date;
	   $errorDetails["error_message"] = $message;
	   $errorDetails["error_code"] = $code;
	   $errorDetails["error_file"] = $file;
	   $errorDetails["error_lineno"] = $line;
	   $errorDetails["error_trace"] = $trace;
       $errorDetails["tdate"] = date("Y-m-d H:i:s");
	   
	    $log_message = "<h3>Exception information:</h3>\n
         <p><strong>Date:</strong> {$date}</p>\n
         <p><strong>Message:</strong> {$message}</p>\n
         <p><strong>Code:</strong> {$code}</p>\n
         <p><strong>File:</strong> {$file}</p>\n
         <p><strong>Line:</strong> {$line}</p>\n        
         <hr />\n";
	     
		 
		 if($code == 1 || $code == 2 || $code == 4 || $code == 16 || $code == 64 || $code == 256 || $code == 512 ){
			  $error_file=ERROR_LOG_FILE;
			  if( is_file($error_file) === false ) {
				 file_put_contents($error_file, '');
			  }       
			  if( $clear ) {
				 $content = '';
			  } else {
				 $content = file_get_contents($error_file);
			  }       
			  file_put_contents($error_file, $content.$log_message );
		 }

   }
	
   public static function newMessage(Exception $exception, $print_error = false, $clear = false, $error_file = 'errorlog.php') {
      
      $message = $exception->getMessage();
      $code = $exception->getCode();
      $file = $exception->getFile();
      $line = $exception->getLine();
      $trace = $exception->getTraceAsString();
      $date = date('M d, Y G:iA');
       
      $log_message = "<h3>Exception information:</h3>\n
         <p><strong>Date:</strong> {$date}</p>\n
         <p><strong>Message:</strong> {$message}</p>\n
         <p><strong>Code:</strong> {$code}</p>\n
         <p><strong>File:</strong> {$file}</p>\n
         <p><strong>Line:</strong> {$line}</p>\n
         <h3>Stack trace:</h3>\n
         <pre>{$trace}</pre>\n
         <hr />\n";
       
	   $errorDetails["error_date"] = $date;
	   $errorDetails["error_message"] = $message;
	   $errorDetails["error_code"] = $code;
	   $errorDetails["error_file"] = $file;
	   $errorDetails["error_lineno"] = $line;
	   $errorDetails["error_trace"] = $trace;
       $errorDetails["tdate"] = date("Y-m-d H:i:s");

	      $error_file=ERROR_LOG_FILE;
		  if( is_file($error_file) === false ) {
			 file_put_contents($error_file, '');
		  }       
		  if( $clear ) {
			 $content = '';
		  } else {
			 $content = file_get_contents($error_file);
		  }       
		  file_put_contents($error_file, $content.$log_message);

      if($print_error == true){
         echo $log_message;
         exit;
      }
   }
		
   public static function errorMessage($error, $print_error = false, $error_file = 'errorlog.php') {

      $date = date('M d, Y G:iA');
      $log_message = "<p>Error on $date - $error</p>";
      
	  $error_file=ERROR_LOG_FILE;
      if( is_file($error_file) === false ) {
         file_put_contents($error_file, '');
      }       
      
      $content = file_get_contents($error_file);       
      file_put_contents($error_file, $content.$log_message);

      if($print_error == true){
         echo $log_message;
         exit;
      } 
   }
   
} //  END Of Class


// Handle PHP Errors
function shutdown(){
			$a=error_get_last();
			if($a==null){
			}				
			else{
				 //print_r($a);
			     $code = $a["type"];
				  if($code == 1 || $code == 2 || $code == 4 || $code == 16 || $code == 64 || $code == 256 || $code == 512 ){
					 Logger::phpErrors($a);
					 logger::customErrorMsg();
				  }
			}		   
		}   		
register_shutdown_function('shutdown'); 
?>
