forked from dooglus/intersango
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.php
138 lines (122 loc) · 3.23 KB
/
errors.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
function enable_errors()
{
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
}
function disable_errors_if_not_me()
{
if (isset($_SERVER["REMOTE_ADDR"]) && $_SERVER["REMOTE_ADDR"] != "127.0.0.1") {
error_reporting(-1);
ini_set('display_errors', '0');
}
}
class Problem extends Exception
{
// PHP sucks!
public function __construct($title, $message)
{
parent::__construct($message);
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
}
class Error extends Problem
{
}
function beginlog()
{
openlog("intersango", LOG_PID, LOG_LOCAL0);
}
function endlog()
{
closelog();
}
class SEVERITY
{
const PROBLEM = 0;
const ERROR = 1;
const BAD_PAGE = 2;
}
function report($message, $severity)
{
global $is_logged_in;
$uid = '';
if ($is_logged_in)
$uid = $is_logged_in;
$time = date('r');
$message = "$uid $time: $message";
switch ($severity) {
case SEVERITY::PROBLEM:
$filename = PROBLEM_LOGFILE;
break;
case SEVERITY::ERROR:
$filename = ERROR_LOGFILE;
break;
case SEVERITY::BAD_PAGE:
$filename = BAD_PAGE_LOGFILE;
break;
default:
report("Invalid report for $message of $severity!", SEVERITY::ERROR);
break;
}
error_log("$message\n", 3, $filename);
beginlog();
syslog(LOG_CRIT, $message);
endlog();
// do this last because it's the most risky operation, and we at least want some logs first.
if ($severity == SEVERITY::ERROR) {
// echo exec("echo 'A fatal error has occured. Time is now $time.' | mutt -s INTERSANGO_ERROR [email protected] -a $filename");
}
}
function log_badpage($page)
{
report($page, SEVERITY::BAD_PAGE);
header('Location: .');
exit();
}
function report_exception($e, $severity)
{
$title = $e->getTitle();
$message = $e->getMessage();
$file = $e->getFile();
$line = $e->getLine();
report(_("Exception") . ": $file $line\n==== $title ====\n$message\n================", $severity);
}
function reporting_error_handler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
switch ($errno) {
case E_USER_ERROR:
report("[$errno] $errstr $errline in $errfile", SEVERITY::ERROR);
exit(1);
break;
case E_USER_WARNING:
report("WARNING: [$errno] $errstr", SEVERITY::ERROR);
break;
case E_USER_NOTICE:
report("NOTICE: [$errno] $errstr", SEVERITY::ERROR);
break;
default:
report("UNKNOWN: [$errno] $errstr", SEVERITY::ERROR);
break;
}
// Don't execute PHP internal error handler
return false;
}
function reporting_shutdown() {
$error = error_get_last();
if ($error != NULL) {
$info = "[SHUTDOWN] file:".$error['file']." | ln:".$error['line']." | msg:".$error['message'] .PHP_EOL;
report($info, SEVERITY::ERROR);
}
}
set_error_handler("reporting_error_handler");
register_shutdown_function("reporting_shutdown");