forked from avdg/ppi-framework-old
/
Exception.php
273 lines (240 loc) · 6.63 KB
/
Exception.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
*
* @version 1.0
* @author Paul Dragoonis <dragoonis@php.net>
* @license http://opensource.org/licenses/mit-license.php MIT
* @copyright Digiflex Development
* @package Core
* @link www.ppiframework.com
*/
class PPI_Exception extends Exception {
/**
* The backtrace
*
* @var string
*/
public $_traceString = '';
/**
* The error line
*
* @var int|string
*/
public $_line = '';
/**
* The error code
*
* @var int|string
*/
public $_code = '';
/**
* The error message
*
* @var string
*/
public $_message = '';
/**
* The error filename
*
* @var string
*/
public $_file = '';
/**
* The backtrace as an array
*
* @var array
*/
public $_traceArray = array();
/**
* Any SQL queries that have been ran
*
* @var array
*/
public $_queries = array();
/**
* Singleton instance
*
* @var null
*/
private static $_instance = null;
/**
* Initialize the default registry instance.
* @return void
*/
protected static function init() {
self::setInstance(new PPI_Exception());
}
/**
* Set the default registry instance to a specified instance.
*
* @return void
*/
public static function setInstance(PPI_Exception $instance) {
self::$_instance = $instance;
}
/**
* Retrieves the default exception instance.
*/
public static function getInstance() {
if (self::$_instance === null) {
self::init();
}
return self::$_instance;
}
/**
* Initialise the PPI_Exception object and start setting up debug info such as backtraces.
*
* @param string $message
* @param null|array $sqlQueries
*/
function __construct($message = '', $sqlQueries = null) {
parent::__construct($message);
$this->_traceString = $this->getTraceAsString();
$this->_traceArray = $this->getTrace();
$this->_line = $this->getLine();
$this->_code = $this->getCode();
$this->_message = $this->getMessage();
$this->_file = $this->getFile();
$this->_queries = PPI_Helper::getRegistry()->get('PPI_Model_Queries', array());
}
/**
* This function shows a 403 error
*
* @access public
* @return void
*/
function show_403($p_sMessage = "") {
$heading = "403 Forbidden";
$message = (!empty ($p_sMessage) ) ? $p_sMessage : "You are not allowed to access the requested location";
PPI_Helper::getRegistry()->set('PPI_View::httpResponseCode', 403);
require SYSTEMPATH.'errors/403.php';
}
/**
* This function shows a 404 error
*
* @access public
* @todo change this to a new function name
* @todo make this do 404 on the HTTP status line
* @param string argument name
* @return void
*/
static function show_404($p_sLocation = "", $p_bUseImage = false) {
$oConfig = PPI_Helper::getConfig();
$heading = "Page cannot be found";
$message = !empty($p_sLocation) ? $p_sLocation : "The page you requested was not found.";
PPI_Helper::getRegistry()->set('PPI_View::httpResponseCode', 404);
header('Status: 404 Not Found');
$oView = new PPI_View();
$oView->load('framework/404', array(
'heading' => 'Page cannot be found', 'message' => $message,
'errorPageType' => '404'
));
exit;
}
/**
* Show this exception
*
* @param string $p_aError Error information from the custom error log
* @return void
*/
function show_exceptioned_error($p_aError = "") {
$p_aError['sql'] = PPI_Helper::getRegistry()->get('PPI_Model::Query_Backtrace', array());
if(!empty($p_aError)) {
try {
$logInfo = $p_aError;
unset($logInfo['code']);
$logInfo['sql'] = serialize($logInfo['sql']);
$oModel = new PPI_Model_Shared('ppi_exception', 'id');
$oModel->insert($logInfo);
} catch(PPI_Exception $e) {} catch(Exception $e) {}
try {
$oEmail = new PPI_Email_PHPMailer();
$oConfig = PPI_Helper::getConfig();
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$ip = $_SERVER['REMOTE_ADDR'];
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Not Available';
if(isset($oConfig->system->error->email)) {
$emailBody = <<<EOT
Hey Support Team,
An error has occured. The following information will help you debug:
Message: {$p_aError['message']}
Line: {$p_aError['line']}
File: {$p_aError['file']}
URL: {$url}
User Agent: {$userAgent}
IP: {$ip}
Referer: {$referer}
Backtrace: {$p_aError['backtrace']}
EOT;
$aErrorConfig = $oConfig->system->error->email->toArray();
$aEmails = array_map('trim', explode(',', $aErrorConfig['to']));
foreach($aEmails as $email) {
$name = '';
if(strpos($email, ':') !== false) {
list($name, $email) = explode(':', $email, 2);
}
$oEmail->AddAddress($email, $name);
}
$fromEmail = $aErrorConfig['from'];
$fromName = '';
if(strpos($fromEmail, ':') !== false) {
list($fromName, $fromEmail) = explode(':', $fromEmail, 2);
}
$oEmail->SetFrom($fromEmail, $fromName);
$oEmail->Subject = $aErrorConfig['subject'];
$oEmail->Body = $emailBody;
$oEmail->Send();
}
} catch(PPI_Exception $e) {} catch(Exception $e) {}
}
$oApp = PPI_Helper::getRegistry()->get('PPI_App', false);
if($oApp === false) {
$sSiteMode = 'development';
$heading = "Exception";
require SYSTEMPATH.'View/fatal_code_error.php';
echo $header.$html.$footer;
exit;
}
$sSiteMode = $oApp->getSiteMode();
if($sSiteMode == 'development') {
$heading = "Exception";
$baseUrl = PPI_Helper::getConfig()->system->base_url;
require SYSTEMPATH.'View/code_error.php';
echo $header.$html.$footer;
} else {
$oView = new PPI_View();
$oView->load('framework/error', array(
'message' => $p_aError['message'],
'errorPageType' => '404'
));
}
exit;
}
/**
* Show an error message
*
* @param string $p_sMsg
* @return void
*/
function show_error($p_sMsg) {
$oConfig = PPI_Helper::getConfig();
$heading = "PHP Error";
$baseUrl = $oConfig->system->base_url;
require SYSTEMPATH.'View/code_error.php';
echo $header.$html.$footer;
}
/**
* Same as show error but return the content as a param
*
* @param array $p_aError
* @return string The HTML contents
*/
function getErrorForEmail($p_aError = "") {
$oConfig = PPI_Helper::getConfig();
$heading = "PHP Error";
$baseUrl = $oConfig->system->base_url;
require SYSTEMPATH.'View/code_error.php';
return $html;
}
}