|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
function logException(Throwable $e) { |
|
3
|
|
|
global $account, $var, $player; |
|
4
|
|
|
$errorType = 'Unexpected Game Error!'; |
|
5
|
|
|
$message = ''; |
|
6
|
|
|
$delim = "\n\n-----------\n\n"; |
|
7
|
|
|
|
|
8
|
|
|
if (is_object($account)) { |
|
9
|
|
|
$message .= 'Login: ' . $account->getLogin() . "\n" . |
|
10
|
|
|
'Account ID: ' . $account->getAccountID() . "\n" . |
|
11
|
|
|
'E-Mail: ' . $account->getEmail() . $delim; |
|
12
|
|
|
} |
|
13
|
|
|
$message .= 'Error Message: ' . $e . $delim; |
|
14
|
|
|
|
|
15
|
|
|
$message .= '$var: ' . var_export($var, true); |
|
16
|
|
|
|
|
17
|
|
|
// Don't display passwords input by users in the log message! |
|
18
|
|
|
if (isset($_REQUEST['password'])) { |
|
19
|
|
|
$_REQUEST['password'] = '*****'; |
|
20
|
|
|
} |
|
21
|
|
|
$message .= "\n\n" . '$_REQUEST: ' . var_export($_REQUEST, true); |
|
22
|
|
|
$message .= $delim; |
|
23
|
|
|
|
|
24
|
|
|
$message .= |
|
25
|
|
|
'User IP: ' . getIpAddress() . "\n" . |
|
26
|
|
|
'USING_AJAX: ' . (defined('USING_AJAX') ? var_export(USING_AJAX, true) : 'undefined') . "\n" . |
|
27
|
|
|
'URL: ' . (defined('URL') ? URL : 'undefined'); |
|
28
|
|
|
|
|
29
|
|
|
try { |
|
30
|
|
|
if (function_exists('release_lock')) { |
|
31
|
|
|
release_lock(); //Try to release lock so they can carry on normally |
|
32
|
|
|
} |
|
33
|
|
|
} catch (Throwable $ee) { |
|
34
|
|
|
$message .= $delim . |
|
35
|
|
|
'Releasing Lock Failed' . "\n" . |
|
36
|
|
|
'Message: ' . $ee . "\n"; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (defined('SCRIPT_ID')) { |
|
40
|
|
|
$message = 'Script: ' . SCRIPT_ID . $delim . $message . "\n\n"; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Unconditionally send error message to the log |
|
44
|
|
|
error_log($message); |
|
45
|
|
|
|
|
46
|
|
|
if (defined('NPC_SCRIPT') && ENABLE_DEBUG) { |
|
47
|
|
|
// In debug mode, we normally exit, but NPCs must cleanup after an error |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (ENABLE_DEBUG) { |
|
52
|
|
|
// Display error message on the page and then exit |
|
53
|
|
|
echo nl2br($message); |
|
54
|
|
|
exit; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Send error message to the in-game auto bugs mailbox |
|
58
|
|
|
if (is_object($player) && method_exists($player, 'sendMessageToBox')) { |
|
59
|
|
|
$player->sendMessageToBox(BOX_BUGS_AUTO, $message); |
|
60
|
|
|
} elseif (is_object($account) && method_exists($account, 'sendMessageToBox')) { |
|
61
|
|
|
// Will be logged without a game_id |
|
62
|
|
|
$account->sendMessageToBox(BOX_BUGS_AUTO, $message); |
|
63
|
|
|
} else { |
|
64
|
|
|
// Will be logged without a game_id or sender_id |
|
65
|
|
|
SmrAccount::doMessageSendingToBox(0, BOX_BUGS_AUTO, $message, 0); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Send error message to e-mail so that we have a permanent record |
|
69
|
|
|
if (!empty(BUG_REPORT_TO_ADDRESSES)) { |
|
|
|
|
|
|
70
|
|
|
$mail = setupMailer(); |
|
71
|
|
|
$mail->Subject = (defined('PAGE_PREFIX') ? PAGE_PREFIX : '??? ') . |
|
72
|
|
|
'Automatic Bug Report'; |
|
73
|
|
|
$mail->setFrom('[email protected]'); |
|
74
|
|
|
$mail->Body = $message; |
|
75
|
|
|
foreach (BUG_REPORT_TO_ADDRESSES as $toAddress) { |
|
76
|
|
|
$mail->addAddress($toAddress); |
|
77
|
|
|
} |
|
78
|
|
|
$mail->send(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $errorType; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
function handleException(Throwable $e) { |
|
85
|
|
|
// The real error message may display sensitive information, so we |
|
86
|
|
|
// need to catch any exceptions that are thrown while logging the error. |
|
87
|
|
|
try { |
|
88
|
|
|
$errorType = logException($e); |
|
89
|
|
|
} catch (Throwable $e) { |
|
90
|
|
|
error_log($e); |
|
91
|
|
|
$errorType = 'This error cannot be automatically reported. Please notify an admin!'; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// If this is an ajax update, we don't really have a way to redirect |
|
95
|
|
|
// to an error page at this time, so we just quit. |
|
96
|
|
|
if (!defined('USING_AJAX') || !USING_AJAX) { |
|
97
|
|
|
header('location: /error.php?msg=' . urlencode($errorType)); |
|
98
|
|
|
} |
|
99
|
|
|
exit; |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Can be used to convert any type of notice into an exception. |
|
104
|
|
|
*/ |
|
105
|
|
|
function exception_error_handler($errno, $errstr, $errfile, $errline) { |
|
106
|
|
|
throw new ErrorException($errstr, $errno, E_ERROR, $errfile, $errline); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
function setupMailer() { |
|
110
|
|
|
$mail = new \PHPMailer\PHPMailer\PHPMailer(true); |
|
111
|
|
|
if (!empty(SMTP_HOSTNAME)) { |
|
|
|
|
|
|
112
|
|
|
$mail->isSMTP(); |
|
113
|
|
|
$mail->Host = SMTP_HOSTNAME; |
|
114
|
|
|
} |
|
115
|
|
|
return $mail; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
function getIpAddress() { |
|
119
|
|
|
foreach (['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'] as $key) { |
|
120
|
|
|
if (array_key_exists($key, $_SERVER) === true) { |
|
121
|
|
|
foreach (explode(',', $_SERVER[$key]) as $ip) { |
|
122
|
|
|
if (filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
|
123
|
|
|
return $ip; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
return 'unknown'; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
function dumpMemDiff($msg) { |
|
132
|
|
|
static $memory; |
|
133
|
|
|
@ob_end_clean(); |
|
|
|
|
|
|
134
|
|
|
var_dump($msg); |
|
|
|
|
|
|
135
|
|
|
var_dump(($memory2 = memory_get_usage()) - $memory); |
|
136
|
|
|
$memory = $memory2; |
|
137
|
|
|
ob_start(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Wrapper around the floor() builtin for returning an integer type. |
|
142
|
|
|
*/ |
|
143
|
|
|
function IFloor(float $val) : int { |
|
144
|
|
|
return (int)floor($val); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Wrapper around the ceil() builtin for returning an integer type. |
|
149
|
|
|
*/ |
|
150
|
|
|
function ICeil(float $val) : int { |
|
151
|
|
|
return (int)ceil($val); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Wrapper around the round() builtin for returning an integer type. |
|
156
|
|
|
*/ |
|
157
|
|
|
function IRound(float $val) : int { |
|
158
|
|
|
return (int)round($val); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Convert a numeric string to an int with input validation. |
|
163
|
|
|
*/ |
|
164
|
|
|
function str2int(string $val) : int { |
|
165
|
|
|
$result = filter_var($val, FILTER_VALIDATE_INT); |
|
166
|
|
|
if ($result === false) { |
|
167
|
|
|
throw new Exception('Input value is not an integer: ' . $val); |
|
168
|
|
|
} |
|
169
|
|
|
return $result; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Generate a cryptographically strong random hexadecimal string. |
|
174
|
|
|
* The requested length must be a multiple of 2. |
|
175
|
|
|
*/ |
|
176
|
|
|
function random_string(int $length) : string { |
|
177
|
|
|
if ($length % 2 != 0) { |
|
178
|
|
|
throw new Exception('Length must be a multiple of 2!'); |
|
179
|
|
|
} |
|
180
|
|
|
return bin2hex(random_bytes($length / 2)); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// Defines all constants |
|
184
|
|
|
require_once('config.php'); |
|
185
|
|
|
|
|
186
|
|
|
// Set up vendor and class autoloaders |
|
187
|
|
|
require_once(ROOT . 'vendor/autoload.php'); |
|
188
|
|
|
require_once(LIB . 'autoload.inc'); |
|
189
|
|
|
spl_autoload_register('get_class_loc'); |
|
190
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.