-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexception_notifier.php
More file actions
157 lines (136 loc) · 5.47 KB
/
Copy pathexception_notifier.php
File metadata and controls
157 lines (136 loc) · 5.47 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
require_once 'qdmail.php';
class ExceptionNotifierComponent extends Object
{
public $ERR_TYPE = array(
E_ERROR => 'FATAL',
E_WARNING => 'WARNING',
E_NOTICE => 'NOTICE',
E_STRICT => 'STRICT'
);
// Mail configuration
public $useSmtp = false;
public $smtpParams = array(
'host'=>'smtp.default.com',
'port'=>'25',
'from'=>'exception.notifier@default.com',
'protocol'=>'SMTP',
);
public $exceptionFrom = array('exception.notifier@default.com', 'Exception Notifier');// exception mail from
public $exceptionRecipients = array();// exception mail to
public $subjectPrefix = '';
// Exception error configuration
public $observeNotice = true;
public $observeWarning = true;
public $observeStrict = false;
private $_controller;
private $_exception;
public function initialize($controller)
{
$this->_controller = $controller;
}
public function handleShutdown()
{
$error = error_get_last();
switch ($error['type']) {
case E_ERROR:
case E_PARSE:
case E_CORE_ERROR:
case E_CORE_WARNING:
case E_COMPILE_ERROR:
case E_COMPILE_WARNING:
$this->handleException(new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']));
}
}
public function handleException($e)
{
$this->_exception = $e;
$mail = new Qdmail();
$mail->smtp($this->useSmtp);
$mail->smtpServer($this->smtpParams);
$mail->to($this->exceptionRecipients);
$mail->subject($this->subjectPrefix . '['. date('Ymd H:i:s') . '][' . $this->_getSeverityAsString() . '][' . $this->_getUrl() . '] ' . $this->_exception->getMessage());
$mail->text($this->_getText());
$mail->from($this->exceptionFrom);
$mail->send();
}
public function handleError($errno, $errstr, $errfile, $errline, $errcontext)
{
$cakePath = CAKE_CORE_INCLUDE_PATH . DS . CAKE;
if (error_reporting() && !preg_match('!^' . $cakePath . '!', $errfile)) {
$this->handleException(new ErrorException($errstr, 0, $errno, $errfile, $errline));
}
return false;
}
public function observe($force = false)
{
if (!$force && Configure::read('debug') > 0) return;
// error_reporting(E_ALL) and don't display errors
if (Configure::read('debug') == 0) {
error_reporting(E_ALL);
ini_set('display_errors', 0);
}
register_shutdown_function(array($this, 'handleShutdown'));
set_exception_handler(array($this, 'handleException'));
$errTypes = 0;
if ($this->observeNotice) $errTypes = $errTypes | E_NOTICE;
if ($this->observeWarning) $errTypes = $errTypes | E_WARNING;
if ($this->observeStrict) $errTypes = $errTypes | E_STRICT;
if ($errTypes) set_error_handler(array($this, 'handleError'), $errTypes);
}
private function _getText()
{
$e = $this->_exception;
$params = method_exists($this->_controller, 'params') ? $this->_controller->params : array();
$session = isset($_SESSION) ? $_SESSION : array();
$msg = array(
$e->getMessage(),
$e->getFile() . '(' . $e->getLine() . ')',
'',
'-------------------------------',
'Request:',
'-------------------------------',
'',
'* URL : ' . $this->_getUrl(),
'* IP address: ' . env('REMOTE_ADDR'),
'* Parameters: ' . trim(print_r($params, true)),
'* Cake root : ' . APP,
'',
'-------------------------------',
'Environment:',
'-------------------------------',
'',
trim(print_r($_SERVER, true)),
'',
'-------------------------------',
'Session:',
'-------------------------------',
'',
trim(print_r($session, true)),
'',
'-------------------------------',
'Cookie:',
'-------------------------------',
'',
trim(print_r($_COOKIE, true)),
'',
'-------------------------------',
'Backtrace:',
'-------------------------------',
'',
$this->_exception->getTraceAsString()
);
return join("\n", $msg);
}
private function _getSeverityAsString()
{
if (!method_exists($this->_exception, 'getSeverity')) return 'ERROR';
$errNo = $this->_exception->getSeverity();
return array_key_exists($errNo, $this->ERR_TYPE) ? $this->ERR_TYPE[$errNo] : "(errno: {$errNo})";
}
private function _getUrl()
{
$protocol = array_key_exists('HTTPS', $_SERVER) ? 'https' : 'http';
return $protocol . '://' . env('HTTP_HOST') . env('REQUEST_URI');
}
}