DreadyCrig
12/17/2014 - 5:51 PM

Simple error handler to email fatal errors

Simple error handler to email fatal errors

<?php
/**
 * Simple error handler that email's when a error happens
 *
 * @author  Shawn Crigger
 */


define( 'SEND_ERROR_EMAIL', TRUE );




set_error_handler( 'ewd_error_handler', E_ALL );
register_shutdown_function( 'fatal_error_shutdown_handler' );


// ------------------------------------------------------------------------
// Log
// ------------------------------------------------------------------------
if ( ! is_object( $log ) )
{
	$log = new Log( $config->get( 'config_error_filename' ) );
	$registry->set( 'log', $log );
}

// ------------------------------------------------------------------------

/**
 * Handles PHP errors
 * @param  integer $code       PHP Error code constant
 * @param  string  $message    PHP Error message
 * @param  string  $file       File name that caused error
 * @param  integer $line       Line number of file that caused error
 * @param  array   [$context]  Context of error
 * @return void
 */
function ewd_error_handler( $code, $message, $file, $line, $context = array() )
{
	global $log, $config;

	error_handler( $code, $message, $file, $line );

	// if SEND_ERROR_EMAIL is FALSE then exit
	if ( FALSE === SEND_ERROR_EMAIL ) return;

	$error = NULL;

	switch ( $code )
	{
		case E_NOTICE:
		case E_USER_NOTICE:
			$error = 'Notice';
			break;
		case E_WARNING:
		case E_USER_WARNING:
			$error = 'Warning';
			break;
		case E_ERROR:
		case E_USER_ERROR:
			$error = 'Fatal Error';
			break;
		default:
			$error = 'Unknown';
			break;
	}
	$date = ' [' . date( 'Y-m-d H:i:s' ) . '] ';
	$msg  = $date . 'PHP ' . $error . ':  ' . $message . ' in ' . $file . ' on line ' . $line;

	if ( ! empty( $context ) )
	{
		$msg .= ' - Context [ ' . print_r( $context, TRUE ) . '] ';
	}

	$msg .= PHP_EOL;

	$to_email   = 'shawn@eaglewebdesigns.com';
	$to_name    = 'Shawn';

	$protocol   = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === FALSE ? 'http' : 'https';            // Get protocol HTTP/HTTPS

	$host     = $_SERVER['HTTP_HOST'];   // Get  www.domain.com
	$script   = $_SERVER['SCRIPT_NAME']; // Get folder/file.php
	$params   = $_SERVER['QUERY_STRING'];// Get Parameters occupation=odesk&name=ashik

	$url     = $protocol . '://' . $host . $script . '?' . $params; // Adding all

	$domain     = parse_url( $url, PHP_URL_HOST );

	$from_name  = 'Error Reporting';
	$from_email = "error_handler@{$domain}";

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
    $headers .= 'To: '.$to_name.' <'.$to_email.'>' . "\r\n";
    $headers .= 'From: '.$from_name.' <'.$from_email.'>' . "\r\n";

	@mail( $to_email , 'PHP Error on Isle of Candles! ' . $date , $msg );

}

// ------------------------------------------------------------------------

/**
 * Handles fatal errors by binding to the PHP shutdown method.
 * @link   http://insomanic.me.uk/post/229851073/php-trick-catching-fatal-errors-e-error-with-a
 * @uses   ewd_error_handler()
 * @return void
 */
function fatal_error_shutdown_handler()
{
	$last_error = error_get_last();
	$error_type = isset( $last_error['type'] ) ? $last_error['type'] : NULL;

	switch( $error_type )
	{
		case E_ERROR:
        case E_CORE_ERROR:
        case E_COMPILE_ERROR:
        case E_USER_ERROR:
            ewd_error_handler( $error_type, $last_error['message'], $last_error['file'], $last_error['line'] );
            break;
	}

}