diff options
-rw-r--r-- | it.class | 76 |
1 files changed, 45 insertions, 31 deletions
@@ -116,14 +116,15 @@ function timerlog($label = '') /** - * Send error report either to browser (on devel/twin machines) or by email to .diffnotice guys - * @parma $title (optional) error title, one line - * @param $body (optional) error body, multiline - * @param $to (optional) comma separated recipient list - * @param named parameter id identifier of error. if given, only subsequent errors of same id will trigger message - * @param named parameter graceperiod number of seconds within which additional errors are ignored if id is set - * @param named parameter timewindow number of seconds after graceperiod within which the second error must occur if id is set - * @param named parameter backtraceskip number of stack levels to drop + * Send verbose error report to browser or (if display_errors is off) by email to .diffnotice guys. All params optional. + * @parma $p['title'] error title, one line + * @param $p['body'] error body, multiline + * @param $p['to'] comma separated recipient list + * @param $p['id'] identifier of error. if given, only subsequent errors of same id will trigger message + * @param $p['graceperiod'] number of seconds within which additional errors are ignored if id is set + * @param $p['timewindow'] number of seconds after graceperiod within which the second error must occur if id is set + * @param $p['backtraceskip'] number of stack levels to drop + * @param $p['blockmail'] number of seconds to block mails after having sent a mail [3600] */ function error($p = array(), $body = "", $to = "") { @@ -131,60 +132,73 @@ function error($p = array(), $body = "", $to = "") $p = array('title' => $p, 'body' => $body, 'to' => $to); if ($_SERVER['REMOTE_ADDR']) - $url = ($_SERVER['HTTPS'] ? "https://" : "http://") . $_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']; + $url = ($_SERVER['HTTPS'] ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + else + $url = $_SERVER['SCRIPT_NAME']; + $gods = strtr(trim(@file_get_contents($GLOBALS['ULTRAHOME'] . "/.diffnotice")), array("\n"=>', ')); $p += array( - 'timewindow' => 25*3600, + 'title' => "it::error", + 'to' => $gods ? $gods : ($_SERVER['SERVER_ADMIN'] ? $_SERVER['SERVER_ADMIN'] : get_current_user()), 'graceperiod' => 60, + 'timewindow' => 25*3600, 'backtraceskip' => 0, - 'to' => strtr(trim(@file_get_contents($GLOBALS['ULTRAHOME'] . "/.diffnotice")), array("\n"=>', ')), - 'title' => "Error in $url", + 'blockmail' => 3600, ); - $toscreen = !ereg('live', $GLOBALS['ULTRASERVERTYPE']); + $toscreen = ini_get('display_errors') || EDC('astwin') || EDC('asdevel'); if (!$toscreen) # this error can only be sent by mail: find out if we want to suppress it { @mkdir("/tmp/alertdata"); @chmod("/tmp/alertdata", 0777); - if ($p['id']) - { - $stampfn = "/tmp/alertdata/errstamp_" . urlencode($p['id']); - $stampage = time() - @filemtime($stampfn); - $sendmail = $stampage >= $p['graceperiod'] && $stampage <= $p['graceperiod'] + $p['timewindow']; - } + if (!$p['id']) + $sendmail = true; else { - $stampfn = "/tmp/alertdata/lastsent_" . getmyuid() . "." . md5($p['to']); - $sendmail = time() - @filemtime($stampfn) > 3600; + $errstampfn = "/tmp/alertdata/errstamp_" . urlencode($p['id']); + $errstampage = time() - @filemtime($errstampfn); + $sendmail = $errstampage >= $p['graceperiod'] && $errstampage <= $p['graceperiod'] + $p['timewindow']; + if ($errstampage > $p['graceperiod']) + { + @unlink($errstampfn); + @touch($errstampfn); + } } - if ($sendmail || $stampage > $p['graceperiod']) # mark time of sending mail or of encountering initial $p['id'] error + if ($sendmail) { - @unlink($stampfn); - touch($stampfn); + $lastsentfn = "/tmp/alertdata/lastsent_" . getmyuid() . "." . md5($p['to']); + if (($sendmail = time() - @filemtime($lastsentfn) > $p['blockmail'])) + { + @unlink($lastsentfn); + @touch($lastsentfn); + } } } if ($toscreen || $sendmail) { - $trace = it_debug::backtrace($p['backtraceskip']); # in here for performance + $trace = it_debug::backtrace($p['backtraceskip']); # moved in here for performance in mass error case $body = ($body ? trim($body)."\n\n" : "") . ($url && !$toscreen? "Url: $url\n\n" : "") . ($trace ? ($sendmail?"" :" ")."Trace: $trace\n\n" : ""); if ($sendmail) # we're mailing: send maximum info { - $body .= "Locals: " . print_r($p['locals'], true) . "\n\n\$_REQUEST: ".print_r($_REQUEST, true) . "\n\$_SERVER: " . print_r($_SERVER, true) . "\n\n"; - if ($trace) # this is only set if no excessive memory usage - $body .= "Stack: " . print_r(debug_backtrace(), true) . "\n\n"; - - mail($p['to'], $p['title'], $body); + $body .= $p['locals'] ? "Locals: " . print_r($p['locals'], true) . "\n\n" : ""; + $body .= $_GET ? "\$_GET: " . print_r($_GET, true) . "\n\n" : ""; + $body .= $_POST ? "\$_POST: " . print_r($_POST, true) . "\n\n" : ""; + $body .= $_COOKIE ? "\$_COOKIE: " . print_r($_COOKIE, true) . "\n\n" : ""; + $body .= $_SERVER ? "\$_SERVER: " . print_r($_SERVER, true) . "\n\n" : ""; + $body .= $trace ? "Stack: " . print_r(debug_backtrace(), true) . "\n\n" : ""; + + mail($p['to'], $p['title'], $body, "From: " . get_current_user()); } else if ($_SERVER['REMOTE_ADDR']) # in shell mode we rely on error_log below echo "<pre>{$p['title']}\n$body</pre>"; } - error_log("it::error: " . $p['title'] . " in " . ($trace ? $trace : $p['line']) . (EDC('verbose') ? D($p['locals']) : "")); + error_log("it::error: " . $p['title'] . " in " . ($trace ? $trace : $p['line']) . " Url: $url " . (EDC('verbose') ? D($p['locals']) : "")); } |