-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorHandler.php
217 lines (188 loc) · 4.22 KB
/
ErrorHandler.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php if ( ! defined('DENY_ACCESS')) exit('403: No direct file access allowed');
/**
* A Bright CMS
*
* Open source, lightweight, web application framework and content management
* system in PHP.
*
* @package A Bright CMS
* @author Gabriel Liwerant
*/
/**
* ErrorHandler Class
*
* This class allows us to report errors, record errors, and send users to a
* friendly error page. Intended for use with register_shutdown_function.
*
* @subpackage core
* @author Gabriel Liwerant
*/
class ErrorHandler
{
/**
* Holds an instance of the log class for error logging
*
* @var object $_log
*/
private $_log;
/**
* Stores an email object
*
* @var object $_email
*/
private $_email;
/**
* Stores error messages we wish to ignore
*
* @var array $_ignore_messages
*/
private $_ignore_messages = array();
/**
* Upon construction, pass in dependencies
*
* @param object $log
* @param object $email
* @param array|void $ignore_messages
*/
public function __construct(Logger $log, Email $email, $ignore_messages = null)
{
$this
->_setLog($log)
->_setEmail($email)
->_setIgnoreMessages($ignore_messages);
}
/**
* Setter for log
*
* @param object $log
*
* @return object ErrorHandler
*/
private function _setLog($log)
{
$this->_log = $log;
return $this;
}
/**
* Setter for email
*
* @param object $email
*
* @return object ErrorHandler
*/
private function _setEmail($email)
{
$this->_email = $email;
return $this;
}
/**
* Setter for error messages to ignore
*
* @param array $ignore_messages
*
* @return object ErrorHandler
*/
private function _setIgnoreMessages($ignore_messages)
{
$this->_ignore_messages = $ignore_messages;
return $this;
}
/**
* Allows us to send an email notification upon fatal error.
*
* @param string $subject Subject line for email
* @param string $msg Email message body
*
* @return boolean
*/
private function _sendEmail($subject, $msg)
{
$is_successful = $this->_email
->setEmailAddress(EMAIL_ADDRESS)
->setSubject($subject)
->setMessage($msg)
->setReplyTo(EMAIL_ADDRESS)
->sendMessage(EMAIL_HEADERS);
return $is_successful;
}
/**
* Allows us to log our fatal errors.
*
* @param string $msg Message to prepend to log message
*
* @return object ErrorHandler
*/
private function _makeLogMessage($msg)
{
$log_message = $msg;
foreach (error_get_last() as $key => $value)
{
$log_message .= $key . ' => ' . $value . ', ';
}
$log_message = rtrim($log_message, ', ');
//$this->_log->writeLogToFile($log_message, 'error', 'errorLog');
return $log_message;
}
/**
* Prepare an email notification for fatal error.
*
* @param string $subject Subject line for email
* @param string $msg Message to prepend to email message
*
* @return string
*/
private function _makeEmailMessage($msg)
{
$email_message = $msg . '<br />';
foreach (error_get_last() as $key => $value)
{
$email_message .= $key . ' => ' . $value . '<br />';
}
return $email_message;
}
/**
* Determines if an error should be ignored.
*
* @param array $error_last
*
* @return boolean
*/
private function _shouldIgnoreError($error_last)
{
if (empty($this->_ignore_messages))
{
return false;
}
foreach ($this->_ignore_messages as $message)
{
if ($message === $error_last['message'])
{
return true;
}
}
return false;
}
/**
* Display a friendly page upon fatal error.
*
* Useful when we are hiding normal error reporting. If we have an error, we
* log it, email it, and load our error page.
*/
public function showFatalErrorPage()
{
$error_last = error_get_last();
if ( ! empty($error_last) )
{
if ( ! $this->_shouldIgnoreError($error_last) )
{
$log_message = $this->_makeLogMessage('Encountered fatal error: ');
$this->_log->writeLogToFile($log_message, 'error', 'errorLog');
$email_message = $this->_makeEmailMessage('Encountered fatal error: ');
$this->_sendEmail(DOMAIN_NAME . ' Fatal Error', $email_message);
header('Location:' . ERROR_HANDLER_PAGE_PATH);
}
}
}
}
// End of ErrorHandler Class
/* EOF system/core/ErrorHandler.php */