Technology Temerity

Class – Error

<?php

class class_error
{    

	/*
	class_error
	Damon Vaughn Caskey
	2012_12_28
	
	Error handler.
	*/	
	
	const 	c_cDBEHost		= "box406.bluehost.com";	//Error log DB host.
	const 	c_cDBELName		= "caskeysc_uk";			//Error log DB logical name.
	const 	c_cDBEUser		= "caskeysc_ehsinfo";		//Error log DB user.
	const 	c_cDBEPword		= "caskeysc_ehsinfo_user";	//Error log DB password.
	const	c_iETScript		= 0;						//Error type; general script errors.
	const	c_iETDB			= 1;						//Error type; datbase error.
	
	private $cIP			= NULL;						//$_SERVER['REMOTE_ADDR']
	private $cSource		= NULL; 					//$_SERVER['PHP_SELF']
	private $debug			= 0;
	private $oMail			= NULL;						//Class mail hanlder.
	private $oUtl			= NULL;						//Utility functions.
	
	public	$cErrType		= NULL;						//Error number or user type.
	public 	$cErrCode		= NULL;						//Error code.
	public 	$cErrDetail		= NULL;						//Error detail (SQL string, parameters, user defined data...).
	public 	$cErrFile		= NULL;						//File running at error time.
	public	$cErrLine		= NULL; 					//Error line.
	public 	$cErrMsg		= NULL;						//Error message.
	public	$cErrState		= NULL;						//State of server (ex. SQL State).
	public	$cErrTOE		= NULL;						//Time of error.
	public	$cErrVars		= NULL;						//String dump of variables.
	
	public function __construct($oDep, $debug = 0)
    {
        /* Import object dependencies. */
		$this->oMail = $oDep['Mail'];
		$this->oUtl = $oDep['Utl'];
				
		/* Verify object dependencies. */
		if(!$this->oMail)	trigger_error("Missing object dependency: Mail.", E_USER_ERROR);
		if(!$this->oUtl)	trigger_error("Missing object dependency: Utility.", E_USER_ERROR);
		
		$this->debug = $debug;
        set_error_handler(array($this, 'error_handle_start'));
	
		register_shutdown_function(array(&$this, 'error_shutdown'));
	}
	
	public function error_fatal()
	{	
		/*
		error_fatal
		Damon Vaughn Caskey
		2012_12_30
		
		Run final actions before exit on a fatal error.
		*/
		
		/*
		If headers haven't been sent, redirect user to an error page. Otherwise we'll just have to die and settle for a plain text message.
		*/
		if($this->oUtl->utl_redirect("/a_errors/php.php")===FALSE)
		{ 
			die("I'm sorry; it appears an internal error has occurred while processing your request. The webmaster has been alerted and will resolve this issue as soon as possible.");
		}
		
		exit;
			
	}
	
	public function error_handle_start($cCode=NULL, $cMsg=NULL, $cFile=NULL, $cLine=NULL)
	{
		$this->error_handle($cCode, $cMsg, $cFile, $cLine, self::c_iETScript, NULL, NULL);
		
		return true;		
	}
	
	public function error_handle($cCode=NULL, $cMsg=NULL, $cFile=NULL, $cLine=NULL, $cType=self::c_iETScript, $cState=NULL, $cDetail=NULL)
	{
		/*
		error_run
		Damon Vaughn Caskey
		2012_12_28
		
		Run error mail and and log in single call.
				
		$cCode:		Error code/number.
		$cMsg:		Error message.
		$cFile:		PHP generated file name.
		$cLine:		Code line location.
		$cType:		User defined error type.
		$cState:	Server state (mostly for SQL errors).
		$cDetail:	User added detail.
		*/
		
		$iLevel = NULL;
		$value	= NULL;
		$key	= NULL;
		$i		= 0;
				
		$this->cErrTOE		= date(constants::c_cDateF);
		$this->cIP			= $_SERVER['REMOTE_ADDR'];
		$this->cSource		= $_SERVER['PHP_SELF'];
		$this->cErrType		= $cType;
		$this->cErrFile		= $cFile;
		$this->cErrLine		= $cLine;	
		$this->cErrState	= $cState;
		$this->cErrCode		= $cCode;
		$this->cErrMsg		= $cMsg;
		$this->cErrDetail	= $cDetail;
		$this->cErrVars		= NULL;
						
		/*
		If logging in (/authenticate_0001.php) and error is suppressed then exit and do nothing.
		
		LDAP libraries are bugged. EX: ldap_bind throws error 49 on bad password instead of returning FALSE as documented. 
		In PHP this can only be worked around by suppressing the error. Otherwise suppressing errors with @ is bad practice 
		that should be avoided at all costs. Its use will be ignored within any other file.
		*/
		$iLevel = error_reporting();
		
		if (($iLevel == 0 || ($iLevel & $cCode) == 0) && $this->cSource == "/authenticate_0001.php")
		{
			return true;
		}
				
		if($this->cErrCode)
		{
			/*
			Log error to database.
			*/
			//$this->error_log_db();
			
			/*
			If error is any type other than a notice then immediately end script and send an email alert to webmaster.
			*/
			switch ($this->cErrCode)
			{
				case E_USER_ERROR:
				case E_USER_WARNING:
				case E_ERROR:
				case E_CORE_ERROR:
				case E_COMPILE_ERROR:
				default:
					
					if(isset($_GET))
					{
						foreach($_GET as $key => $value)
						{
							$this->cErrVars .= "GET[".$key."]: ".$value." || ";
						}
					}
					
					if(isset($_POST))
					{
						foreach($_POST as $key => $value)
						{
							$this->cErrVars .= "POST[".$key."]: ".$value." || ";
						}
					}
					
					if(isset($_SESSION))
					{
						foreach($_SESSION as $key => $value)
						{
							$this->cErrVars .= "SESSION[".$key."]: ".$value." || ";
						}
					}
		
					$this->error_mail();
					$this->error_fatal();
					break;
				case E_USER_NOTICE:
				case E_NOTICE:
					break;				
			}		
		}
	}
		
   	public function error_log_db()
	{
		/*
		error_db_log
		Damon Vaughn Caskey
		2012_12_28
				
		Attempt to log error detail to database. Self contained to avoid recursive calls to database class.
		*/
				
		$rDBConn		= NULL;	//Connection reference to DB error log.
		$cQuery			= NULL; //Error query string.
		$rDBStatement	= NULL;	//Prepared query reference.
		
		$rDBConn = new mysqli(self::c_cDBEHost, self::c_cDBEUser, self::c_cDBEPword, self::c_cDBELName);
		
		/* If the error log database connection was successful, insert each error to table. */
		if (!$rDBConn->connect_error) 
		{				
			/* Build query string. */ 		
			$cQuery = "INSERT INTO tbl_gen_errors (toe, ip, type, source, file, line, state, code, vars, msg, details) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
			$rDBStatement = $rDBConn->prepare($cQuery);
			
			/* Bind parameters. */
			$rDBStatement->bind_param("sssssssssss", $this->cErrTOE, $this->cIP, $this->cErrType, $this->cSource, $this->cErrFile, $this->cErrLine, $this->cErrState, $this->cErrCode, $this->cErrVars, $this->cErrMsg, $this->cErrDetail);
			
			/* Execute and close query. */ 
			if($rDBStatement != false)
			{							
				$rDBStatement->execute();
				$rDBStatement->close();
			}
			
			/* Close DB connection. */
			$rDBConn->close();			
		}						
		
	}
	
	public function error_mail()
	{
		/*
		error_mail
		Damon Vaughn Caskey
		2012_12_31
		~2012_01_02: Array list upgrade.
		
		Prepare and send an email error alert.
		*/
		
		$cMsg 	= NULL;
		
		$cMsg = array(
			"Time"				=>	$this->cErrTOE,
			"Type"				=>	$this->cErrType,
			"IP"				=>	$this->cIP,
			"Def. Source File"	=>	$this->cSource,
			"Source File"		=>	$this->cErrFile,
			"Line"				=>	$this->cErrLine,
			"State"				=>	$this->cErrState,
			"Code"				=>	$this->cErrCode,
			"Message"			=>	$this->cErrMsg,
			"Variables"			=>	$this->cErrVars,
			"Details"			=>	$this->cErrDetail
		);
				
		$this->oMail->mail_send($cMsg, "Error Report");
	}
			
	public function error_shutdown()
	{
		/*
		error_shutdown
		Damon Vaughn Caskey
		2012_12_31
		
		Shutdown function to capture error types PHP will not normally allow custom error handlers to deal with.
		*/
		
		$cError	= NULL;		//Last error status.
		
		/*
		Get last error status.
		*/
        $cError = error_get_last();
		
		$this->error_handle($cError['type'], $cError['message'], $cError['file'], $cError['line']);
    }	
}


Author: Damon Caskey

Hello all, Damon Caskey here - the esteemed owner of this little slice of cyberspace. Welcome!

Leave a Reply