Skip to content
This repository has been archived by the owner on Mar 2, 2021. It is now read-only.

Commit

Permalink
Fixed up error handler so it suppresses the same messages within x mi…
Browse files Browse the repository at this point in the history
…nutes

git-svn-id: https://seedframework.svn.sourceforge.net/svnroot/seedframework/trunk@522 e74265ba-5712-0410-b544-ed03b9d7f503
  • Loading branch information
mamihod committed May 19, 2009
1 parent f5085a9 commit 5a839b2
Showing 1 changed file with 69 additions and 3 deletions.
72 changes: 69 additions & 3 deletions error/lib/error/live.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
print '<p>The server encountered an unexpected condition which prevented it from fulfilling the request.</p>\n';
print "<p>An email has been generated and sent to our technical staff</p>";
}

// send email to admin
if (defined('ADMIN_EMAIL')) {

// send email to admin?
if (defined('ADMIN_EMAIL') && _send_error_email($errno.$errstr)) {
print 'sending email';
$error_message = ucfirst(error_string($errno))."\n".$errstr."\n$errfile in line $errline\n\n";
$error_message .= "-- backtrace --\n\n".backtrace(2)."\n";
$error_message .= "-- get --\n\n".print_r($_GET, true)."\n\n";
Expand All @@ -43,9 +44,74 @@ function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
//$error_message .= "-- context --\n\n".print_r($errcontext, true);
//print($error_message);
mail(ADMIN_EMAIL, 'PHP '.ucfirst(error_string($errno)).': '.$errstr, $error_message);

} else {
print 'dont send email';
}

die();
}

/**
* Used to determine whether or not to send an error email
* To solve problem of N emails all exactly the same when some script kiddie hits one of our sites
* Basic idea is:
* Make hash of error info,
* Write a file with that name, and put timestamp for when the error suppression should be good till.
*
* If older, then send the mail, and write new timestamp.
*
* Needs to be php4 + 5 compat. Idea is it can be dropped into any old sites based on seed.
*
* @doc Make sure you have the LOG PATH setup (usually /cms/logs)
*
* @param $err Used to make a hash filename. Combo between error no + error string
* @return bool
**/
function _send_error_email($err) {

$filename = md5($err);
$filepath = LOG_PATH.$filename;

if(file_exists($filepath)){

$error_expires = file_get_contents($filepath);

if(mktime() > $error_expires) {
_create_error_tracker($filepath);
} else {
return false;
}
} else {
_create_error_tracker($filepath);
}

//Default to true - in case of mistake, would rather err on side of send me error message
return true;

}

/**
* Creates the little error tracker file
*
* @return void
**/
function _create_error_tracker($filepath) {

//In minutes
if(!defined('ERROR_EXPIRY')) {
define('ERROR_EXPIRY', 5);
}

//@change to file_put_contents?
$handle = fopen($filepath, 'w');
//Minutes till expires
$error_expires = mktime() + (ERROR_EXPIRY * 60);
fwrite($handle, $error_expires);
fclose($handle);


}


?>

0 comments on commit 5a839b2

Please sign in to comment.