-
Notifications
You must be signed in to change notification settings - Fork 0
/
spanner.php
433 lines (369 loc) · 12.3 KB
/
spanner.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
if (!function_exists("printRead")) { include HAMMERPATH . "/functions/errors.php"; } //always needed
if (!class_exists("screws")) { include "screws.php"; } //include the autoloader
if (!function_exists("visitorIP")) { include HAMMERPATH . "/functions/hammer.php"; } //to include the standard hammer functons
/**
* Spanner
*
* @package
* @author Keloran
* @copyright Copyright (c) 2008
* @version $Id: spanner.php 506 2010-01-07 15:53:59Z keloran $
* @access public
*/
class Spanner extends Exception {
private $mContact = false;
private $cMessage = false;
private $cAddress = false;
/**
* Error::__construct()
*
* @param string $cMessage
* @param int $iErrNo
* @param string $cFile
* @param int $iLine
*/
public function __construct($cMessage = null, $iErrNo = null, $oError = null) {
parent::__construct($cMessage, $iErrNo);
if ($iErrNo) { $this->code = $iErrNo; }
//previous
if ($oError) {
$this->cPrevious = get_class($oError);
} else {
$this->cPrevious = get_class($this);
}
$this->cMessage = $cMessage;
//get the config
if (function_exists("Config")) {
$this->mContact = Config("contact");
$aHead = Config("head");
$this->cAddress = $aHead['address'];
} else {
if (!class_exists("Hammer")) { include "hammer.php"; }
$this->mContact = Hammer::getConfigStat("email")['email'];
$this->cAddress = Hammer::getConfigStat("address")['address'];
}
//fix for old configs
if (is_array($this->cAddress)) {
$cAddress = $this->cAddress['address'];
$this->cAddress = $cAddress;
}
if (is_array($this->mContact)) {
$cContact = $this->mContact['email'];
$this->mContact = $cContact;
}
//Since we now need to actually do something with this
$this->catchIt();
}
/**
* Spanner::catchIt()
*
* @return null
*/
public function catchIt() {
$cReturn = false;
$bNice = false;
$cSave = false;
//Since this dictates what happens
switch($this->code) {
case 1:
$this->showSpecificMessage($this->cMessage); //this is also needed becasue im forcing the failure
$bNice = false;
break;
case 2:
if (strstr($this->cMessage, "Too many")) {
$this->showSpecificMessage("We are having scaling issues, please try again in a few hours");
$bNice = false;
break;
}
//this can spam the system
case 8:
case 200:
case 100:
$bNice = false;
$cSave = $this->saveMessage($this->code);
//is it dev, in which case tell me where the message is saved, and also show it
if (defined("DEV")) {
$cReturn = $this->showThis($cSave);
$cReturn .= $this->showMessage();
}
break;
case 1001:
$this->showNiceMessage(true);
break;
default:
if (defined("TERM")) { //Terminal
$cReturn = $this->showMessage(true); //This is still needed to solve the terminal problem
$bNice = false;
} else if (defined("DEV")) { //Development
if ($this->mContact) {
$this->sendMessage($this->code);
$cReturn = $this->showMessage(false);
} else {
$cReturn = $this->showMessage(false);
}
} else { //Live
$bBugs = false;
if (defined("bugs")) { $bBugs = true; }
if (defined("Bugs")) { $bBugs = true; }
if (defined("BUGS")) { $bBugs = true; }
if ($bBugs && function_exists("mail")) { //you want to send to the bug reporter and you have mail working
$this->sendMessage($this->code, true);
$bNice = true;
} else { //nothing so show me
$cReturn = $this->showMessage(true); //show the real message
}
}
break;
} // switch
//nice message
if ($bNice) { $cReturn = $this->showNiceMessage(); }
echo $cReturn;
//now kill it since it will loop otherwise
die();
}
/**
* Error::setMessage()
*
* @param string $message
* @return
*/
public function setMessage($cMessage) {
$this->message = $cMessage;
$this->cMessage = $cMessage;
}
/**
* Error::sendMessage()
*
* @desc This sends the message, to the user defined in the config
* @param int iErrNo Error number
* @param bool dicate the if to send to bugs
* @return
*/
private function sendMessage($iErrNo, $bBugs = false) {
$cTitle = "An error occoured in " . $this->cAddress . ":" . $iErrNo;
$cFrom = "error@" . $this->cAddress;
$mContact = $this->mContact;
$cMessageHTML = $this->showMessage(false);
$cMessageText = $this->showMessage(true);
//I really dont want to send this to bug tracker
$bNoSend = isset($_GET['nobugs']) ? $_GET['nobugs'] : false;
if (!$bNoSend) {
$oSend = new Email_Send();
if ($bBugs) {
$aSend = array(
"to" => "[email protected]",
"title" => $cTitle,
"html" => $cMessageHTML,
"text" => $cMessageText,
"from" => $cFrom,
);
$oSend->compose($aSend);
} else {
if (is_array($mContact)) {
$iContacts = count($mContact);
foreach ($mContact AS $contact) {
$cTo = addslashes($contact);
$aSend = array(
"to" => $cTo,
"title" => $cTitle,
"html" => $cMessageHTML,
"text" => $cMessageText,
"from" => $cFrom,
);
$oSend->compose($aSend);
}
} else {
$cTo = addslashes($mContact);
$aSend = array(
"to" => $cTo,
"title" => $cTitle,
"html" => $cMessageHTML,
"text" => $cMessageText,
"from" => $cFrom,
);
$oSend->compose($aSend);
}
}
}
}
/**
* Spanner::saveMessage()
*
* @param int $iErrNo
* @return null
*/
private function saveMessage($iErrNo) {
$iTime = time();
$cMessage = $this->showMessage(true);
file_put_contents("/tmp/" . $iTime . "_error.message", $cMessage);
$cReturn = "File Located at: /tmp/" . $iTime . "_error.message";
return $cReturn;
}
/**
* Error::showNiceMessage()
*
* @desc this is the message the end users see
* @param bool $bHeader This turns off the header of 404 being sent
* @return
*/
public function showNiceMessage($bOffline = false, $bSyntax = false) {
$cMessage = "<section id=\"exceptiond\">\n";
$cMessage .= "<header>A Problem happened</header>\n";
$cMessage .= "<article>\n";
$cMessage .= "<p>A problem happened, please try again in '30 Minutes'</p>\n";
$cMessage .= "<p>Site Admin has been informed</p>\n";
$cMessage .= "<p>You will be redirected to the homepage</p>\n";
$cMessage .= "<p><a href=\"#\" onclick=\"history.go(-1)\">Back</a></p>\n";
$cMessage .= "</article>\n";
$cMessage .= "</section>\n";
//clear the buffers
while(ob_get_level()) { ob_end_clean(); }
//get the site address
$cSite = "Unknown Hammer Site";
if (isset($_SERVER['HTTP_HOST'])) {
$cAddy = $_SERVER['HTTP_HOST'];
if (substr($cAddy, 0, 4) == "www.") { $cAddy = substr($cAddy, 4); }
$cSite = $cAddy;
}
//not a syntax error
if (!$bSyntax) {
//open buffer
if (!checkHeaders()) { ob_start(); }
//now put the offline page
#header("HTTP/1.1 404 File not Found");
http_response_code(404);
//is stuff offline, e.g. database, it also redirects to homepage for normal stuffs,
//hopefulyl wont go into infinite loop
if ($bOffline) {
header("refresh: 5; url=http://hammer.develbox.info/offline.html?site=" . $cSite);
} else {
header("refresh 5; url=/");
}
echo $cMessage;
//close teh buffer
if (!checkHeaders()) { ob_end_flush(); }
die();
} else {
$cMessage = "<section id=\"exceptiond\">\n";
$cMessage .= "<header>An Error Happened</header>\n";
$cMessage .= "<article>\n";
$cMessage .= $this->showSpecificMessage(true);
$cMessage .= "</article>\n";
$cMessage .= "</section>\n";
//now make the style for it
$cMessage .= "<style>\n";
$cMessage .= "body { width: 90%; }\n";
$cMessage .= "section { width: 50%; margin: 2% 10% 2% 25%; background: #000000; color: #FFFFFF; border: thin solid #FFFFFF; }\n";
$cMessage .= "header { width: 99%; text-align: center; font-weight: bold; }\n";
$cMessage .= "article { width: 99%; padding: 5%; }\n";
$cMessage .= "</style>\n";
echo $cMessage;
die();
}
}
/**
* Error::showSpecificMessage()
*
* @return
*/
private function showSpecificMessage($bReturn = false) {
$cMessage = $this->getMessage();
//return the error
if ($bReturn) { return $cMessage; }
//no return given so echo it
echo $cMessage;
}
/**
* Error::showMessage()
*
* @desc this is the message that gets sent to the developer
* @return
*/
private function showMessage($bConsole = false) {
$cMessage = false;
//since console messages cant see this
if (!$bConsole) {
$cMessage .= "<section id=\"exceptiond\">\n";
$cMessage .= "<header>" . $this->getMessage() . "</header>\n";
$cMessage .= "<article>\n";
}
$cMessage .= "<p>An exception happened in <br />" . $this->getFile() . ".</p>\n";
$cMessage .= "<p>On line <br />" . $this->getLine() . ".</p>\n";
$cMessage .= "<p>The whole message is <br />" . $this->getMessage() . "</p>\n";
$cMessage .= "<p>The exception code is <br />" . $this->getCode() . "</p>\n";
$cMessage .= "<p>A Trace is <br />" . nl2br($this->getTraceAsString()) . "</p>\n";
$cMessage .= "<p>A Printed trace is <br />" . nl2br(print_r($this->getTrace(), true)) . "</p>\n";
//the last error, incase it exists
$cMessage .= "<p>Last Error array <br />" . nl2br(print_r(error_get_last(), true)) . "</p>\n";
//memory usage, and devided by 1024
$cMessage .= "<p>Memory Usage<br />" . nl2br(print_r(memory_get_peak_usage(), true));
$cMessage .= " (" . nl2br(print_r(round(memory_get_peak_usage() / 1024), true)) . ")</p>\n";
//arguments in useS
$cMessage .= "<p>Function Arguments <br />" . nl2br(print_r(func_get_args(), true)) . "</p>\n";
//Since not all systems have the remote addr, e.g. googlebot
$cVisitor = visitorIP();
if ($cVisitor) { $cMessage .= "<p>It was triggered by " . $cVisitor. "</p>\n"; }
//class previous
if (isset($this->cPrevious)) { $cMessage .= "<p>Previous: " . $this->cPrevious . "</p>\n"; }
//Defined Vars
$cMessage .= "<p>Defined Vars " . print_r(get_defined_vars(), true) . "</p>\n";
//GPR
$cMessage .= "<p>Full REQUEST is " . print_r($_REQUEST, true) . "</p>\n";
$cMessage .= "<p>Full GET is " . print_r($_GET, true) . "</p>\n";
$cMessage .= "<p>Full POST is " . print_r($_POST, true) . "</p>\n";
if (isset($_FILES)) { $cMessage .= "<p>Full FILES is " . print_r($_FILES, true) . "</p>\n"; }
$cMessage .= "<p>Full SERVER is " . print_r($_SERVER, true) . "</p>\n";
if (isset($_SERVER['HTTP_REFERER'])) {
$cMessage .= "<p>Referer: " . print_r($_SERVER['HTTP_REFERER'], true) . "</p>\n";
} else {
$cMessage .= "<p>Direct link, god knows where they came from</p>\n";
}
//put the date
$cMessage .= "<p>Date: " . date("d/m/Y H:i", time()) . "</p>\n";
//since console cant see this
if (!$bConsole) {
$cMessage .= "</article>\n";
$cMessage .= "</section>\n";
}
//remove the brs and replace with newlines
if ($bConsole) {
$cMessage = str_replace(" ", " ", $cMessage);
$cMessage = str_replace("<br />", "\n", $cMessage);
$cMessage = str_replace("\n\n", "\n", $cMessage);
$cMessage = str_replace(array("<p>", "</p>"), array("", "\n--------\n"), $cMessage);
}
return $cMessage;
}
/**
* Spanner::showThis()
*
* @param string $cMessage
* @return string
*/
private function showThis($cMessage) {
$cReturn = "<section id=\"exceptiond\">\n";
$cReturn .= "<header>Specific Message</header>\n";
$cReturn .= "<article>" . $cMessage . "</article>\n";
$cReturn .= "</section>\n";
$cReturn .= "<hr />\n";
return $cReturn;
}
}
/**
* These are defined at the end so that they work basiclly
*/
set_exception_handler("exceptionHandler");
set_error_handler("errorHandler", E_ALL);
/** Assertions **/
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_BAIL, 0);
assert_options(ASSERT_QUIET_EVAL, 0);
assert_options(ASSERT_CALLBACK, "assertionHandler");
//if there is an noerror defined
if (defined("noError")) {
restore_error_handler();
restore_exception_handler();
assert_options(ASSERT_CALLBACK, null);
}