1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
use Smr\Container\DiContainer; |
4
|
|
|
|
5
|
|
|
function logException(Throwable $e) : void { |
6
|
|
|
global $account, $var, $player; |
7
|
|
|
$message = ''; |
8
|
|
|
$delim = "\n\n-----------\n\n"; |
9
|
|
|
|
10
|
|
|
if (is_object($account)) { |
11
|
|
|
$message .= 'Login: ' . $account->getLogin() . "\n" . |
12
|
|
|
'Account ID: ' . $account->getAccountID() . "\n" . |
13
|
|
|
'E-Mail: ' . $account->getEmail() . $delim; |
14
|
|
|
} |
15
|
|
|
$message .= 'Error Message: ' . $e . $delim; |
16
|
|
|
|
17
|
|
|
$message .= '$var: ' . var_export($var, true); |
18
|
|
|
|
19
|
|
|
// Don't display passwords input by users in the log message! |
20
|
|
|
if (isset($_REQUEST['password'])) { |
21
|
|
|
$_REQUEST['password'] = '*****'; |
22
|
|
|
} |
23
|
|
|
$message .= "\n\n" . '$_REQUEST: ' . var_export($_REQUEST, true); |
24
|
|
|
$message .= $delim; |
25
|
|
|
|
26
|
|
|
$message .= |
27
|
|
|
'User IP: ' . getIpAddress() . "\n" . |
28
|
|
|
'USING_AJAX: ' . (defined('USING_AJAX') ? var_export(USING_AJAX, true) : 'undefined') . "\n" . |
29
|
|
|
'URL: ' . (defined('URL') ? URL : 'undefined'); |
30
|
|
|
|
31
|
|
|
try { |
32
|
|
|
if (function_exists('release_lock')) { |
33
|
|
|
release_lock(); //Try to release lock so they can carry on normally |
34
|
|
|
} |
35
|
|
|
} catch (Throwable $ee) { |
36
|
|
|
$message .= $delim . |
37
|
|
|
'Releasing Lock Failed' . "\n" . |
38
|
|
|
'Message: ' . $ee . "\n"; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (defined('SCRIPT_ID')) { |
42
|
|
|
$message = 'Script: ' . SCRIPT_ID . $delim . $message . "\n\n"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Unconditionally send error message to the log |
46
|
|
|
error_log($message); |
47
|
|
|
|
48
|
|
|
if (ENABLE_DEBUG) { |
49
|
|
|
// Display error message on the page (redundant with error_log for CLI) |
50
|
|
|
if (php_sapi_name() !== 'cli') { |
51
|
|
|
echo nl2br($message); |
52
|
|
|
} |
53
|
|
|
// Skip remaining log methods (too disruptive during development) |
54
|
|
|
return; |
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
|
|
|
|
82
|
|
|
function handleException(Throwable $e) { |
83
|
|
|
// The real error message may display sensitive information, so we |
84
|
|
|
// need to catch any exceptions that are thrown while logging the error. |
85
|
|
|
try { |
86
|
|
|
logException($e); |
87
|
|
|
$errorType = 'Unexpected Error!'; |
88
|
|
|
} catch (Throwable $e) { |
89
|
|
|
error_log('Exception during logException: ' . $e); |
90
|
|
|
$errorType = 'This error cannot be automatically reported. Please notify an admin!'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// If this is an ajax update, we don't really have a way to redirect |
94
|
|
|
// to an error page at this time. |
95
|
|
|
if (!ENABLE_DEBUG && (!defined('USING_AJAX') || !USING_AJAX)) { |
96
|
|
|
header('location: /error.php?msg=' . urlencode($errorType)); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Can be used to convert any type of notice into an exception. |
102
|
|
|
*/ |
103
|
|
|
function exception_error_handler($errno, $errstr, $errfile, $errline) { |
104
|
|
|
throw new ErrorException($errstr, $errno, E_ERROR, $errfile, $errline); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
function setupMailer() { |
108
|
|
|
$mail = new \PHPMailer\PHPMailer\PHPMailer(true); |
109
|
|
|
if (!empty(SMTP_HOSTNAME)) { |
|
|
|
|
110
|
|
|
$mail->isSMTP(); |
111
|
|
|
$mail->Host = SMTP_HOSTNAME; |
112
|
|
|
} |
113
|
|
|
return $mail; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
function getIpAddress() { |
117
|
|
|
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) { |
118
|
|
|
if (array_key_exists($key, $_SERVER) === true) { |
119
|
|
|
foreach (explode(',', $_SERVER[$key]) as $ip) { |
120
|
|
|
if (filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
121
|
|
|
return $ip; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
return 'unknown'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Wrapper around the floor() builtin for returning an integer type. |
131
|
|
|
*/ |
132
|
|
|
function IFloor(float $val) : int { |
133
|
|
|
return (int)floor($val); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Wrapper around the ceil() builtin for returning an integer type. |
138
|
|
|
*/ |
139
|
|
|
function ICeil(float $val) : int { |
140
|
|
|
return (int)ceil($val); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Wrapper around the round() builtin for returning an integer type. |
145
|
|
|
*/ |
146
|
|
|
function IRound(float $val) : int { |
147
|
|
|
return (int)round($val); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Convert a numeric string to an int with input validation. |
152
|
|
|
*/ |
153
|
|
|
function str2int(string $val) : int { |
154
|
|
|
$result = filter_var($val, FILTER_VALIDATE_INT); |
155
|
|
|
if ($result === false) { |
156
|
|
|
throw new Exception('Input value is not an integer: ' . $val); |
157
|
|
|
} |
158
|
|
|
return $result; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Generate a cryptographically strong random hexadecimal string. |
163
|
|
|
* The requested length must be a multiple of 2. |
164
|
|
|
*/ |
165
|
|
|
function random_string(int $length) : string { |
166
|
|
|
if ($length % 2 != 0) { |
167
|
|
|
throw new Exception('Length must be a multiple of 2!'); |
168
|
|
|
} |
169
|
|
|
return bin2hex(random_bytes($length / 2)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Return the value of a random key from an array. |
174
|
|
|
* @return mixed |
175
|
|
|
*/ |
176
|
|
|
function array_rand_value(array $arr) { |
177
|
|
|
if (empty($arr)) { |
178
|
|
|
throw new Exception('Cannot pick random value from empty array!'); |
179
|
|
|
} |
180
|
|
|
return $arr[array_rand($arr)]; |
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.php'); |
189
|
|
|
spl_autoload_register('get_class_loc'); |
190
|
|
|
|
191
|
|
|
// Set up dependency injection container |
192
|
|
|
DiContainer::initializeContainer(); |
193
|
|
|
|