-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostServer.php
More file actions
260 lines (234 loc) · 6.79 KB
/
Copy pathpostServer.php
File metadata and controls
260 lines (234 loc) · 6.79 KB
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
<?php
require_once($_SERVER['DOCUMENT_ROOT'] .'/dynamic/include.php');
header('Content-Type: text/plain');
$postValues = [
'ServerName',
'Port',
'Players',
'MaxPlayers',
'Map',
'Passworded',
'Dedicated',
'BrickCount',
'blid',
'ver'
];
$valuesCount = count($postValues);
$currentValuesFound = 0;
foreach ($postValues as $value)
{
if (in_array($value, array_keys($_POST)))
{
++$currentValuesFound;
}
}
if ($currentValuesFound !== $valuesCount)
{
$GLOBALS['database'] = null;
exit('FAIL Invalid POST params');
}
$ip = IP;
$uptime = time();
$username = '';
$serverName = $_POST['ServerName'];
$serverPort = $_POST['Port'];
$activePlayers = $_POST['Players'];
$maxPlayers = $_POST['MaxPlayers'];
$gameMode = $_POST['Map'];
$isPassworded = $_POST['Passworded'];
$isDedicated = $_POST['Dedicated'];
$brickCount = $_POST['BrickCount'];
$bl_id = $_POST['blid'];
$key_id = $_POST['blid'];
$version = $_POST['ver'];
$intArray = [
$brickCount,
$serverPort,
$activePlayers,
$maxPlayers,
$isPassworded,
$isDedicated,
$version
];
if ($version == '21')
{
$intArray[] = $bl_id;
}
foreach ($intArray as $testInt)
{
if (is_numeric($testInt))
{
$testInt = (int)$testInt; // Cast as int
}
else
{
$GLOBALS['database'] = null;
exit('FAIL One of the POST values specified is not an integer');
}
}
if ($version < 21)
{
if (keyToID($bl_id))
{
$bl_id = keyToID($bl_id);
}
else
{
$GLOBALS['database'] = null;
exit('FAIL Failed to convert BL_ID to key ID');
}
}
else
{
$key_id = idToKey($bl_id);
}
if ($serverPort < 0)
{
$serverPort = 28000;
}
if (empty($ip))
{
$ip = '127.0.0.1';
}
if ($bl_id < 0)
{
$GLOBALS['database'] = null;
exit('FAIL Invalid BL_ID ' . $bl_id);
}
if ($maxPlayers < 0)
{
$GLOBALS['database'] = null;
exit('FAIL Invalid player cap');
}
if (empty($gameMode))
{
$gameMode = 'Custom';
}
// Get creator's name
$check = trim(file_get_contents('http://mods.greek2me.us/statistics/user-lookup.php?blid='. $bl_id));
if (empty($check))
{
$GLOBALS['database'] = null;
exit('FAIL No user');
}
$entries = explode("\n", $check);
$namesAndDates = [];
$interval = [];
foreach ($entries as $entry)
{
$entry = explode("\t", $entry);
array_shift($entry);
$namesAndDates[$entry[0]] = $entry[1];
}
foreach ($namesAndDates as $name => $date)
{
$interval[$name] = abs(strtotime(date('Y-m-d H:m:s')) - strtotime($date));
}
asort($interval);
$username = trim(key($interval));
if (empty($username))
{
$GLOBALS['database'] = null;
exit('FAIL Failed to get username');
}
// Do auth
if ($ip !== '127.0.0.1' || $ip !== '::1')
{
$data = [
'NAME' => urlencode($username),
'IP' => urlencode($ip)
];
$options = [
'http' => [
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
]
];
$context = stream_context_create($options);
$result = str_replace(trim(strtolower(file_get_contents('https://auth.blockland.us/authQuery.php', false, $context))), "\n", '');
if (strpos($result, 'yes') !== false)
{
$parts = explode(' ', $result);
if ($parts[0] != $bl_id)
{
$GLOBALS['database'] = null;
exit('FAIL Authserver returned different BL_ID than one sent');
}
// Successfully authed! Yay
}
elseif (strpos($result, 'error') !== false)
{
$GLOBALS['database'] = null;
exit('FAIL Authserver had error');
}
elseif (strpos($result, 'no') !== false)
{
$GLOBALS['database'] = null;
exit('FAIL Failed to auth');
}
}
// Make server name
if (substr($username, -1) == 's')
{
$serverName = $username . '\'' . $serverName;
}
else
{
$serverName = $username . '\'s ' . $serverName;
}
if (SERVERNAME_VERSION)
{
$serverName = '[v' . $version . '] ' . $serverName;
}
// finish checks, post the server
$updating = false;
$query = 'SELECT * FROM servers WHERE ip = :ip AND port = :port AND version = :version AND key_id = :key_id;';
$statement = $GLOBALS['database']->prepare($query);
$statement->bindParam(':ip', $ip, PDO::PARAM_STR);
$statement->bindParam(':port', $serverPort, PDO::PARAM_INT);
$statement->bindParam(':version', $version, PDO::PARAM_INT);
$statement->bindParam(':key_id', $key_id, PDO::PARAM_STR);
$statement->execute();
if ($statement->rowCount() > 0)
{
$updating = true;
$query = 'UPDATE servers SET port = :newPort, passworded = :newPasswordDefinition, dedicated = :newDedicatedDefinition, serverName = :newServerName, players = :newPlayerCount, maxPlayers = :newMaxPlayers, gamemode = :newGamemode, brickCount = :newBrickCount, version = :newVersion, uptime = :newUptime, bl_id = :newBLID, key_id = :newKeyID WHERE ip = :currentIp AND port = :currentPort AND key_id = :newKeyID;';
}
elseif ($statement->rowCount() >= SERVER_LIMIT)
{
$GLOBALS['database'] = null;
exit('FAIL You can only have ' . SERVER_LIMIT . 'servers per version online at a time.');
}
else
{
$query = 'INSERT INTO servers(ip, port, passworded, dedicated, serverName, players, maxPlayers, gamemode, brickCount, version, uptime, bl_id, key_id) VALUES(:newIp, :newPort, :newPasswordDefinition, :newDedicatedDefinition, :newServerName, :newPlayerCount, :newMaxPlayers, :newGamemode, :newBrickCount, :newVersion, :newUptime, :newBLID, :newKeyID);';
}
$statement = null;
$statement = $GLOBALS['database']->prepare($query);
$statement->bindParam(':newPort', $serverPort, PDO::PARAM_INT);
$statement->bindParam(':newPasswordDefinition', $isPassworded, PDO::PARAM_INT);
$statement->bindParam(':newDedicatedDefinition', $isDedicated, PDO::PARAM_INT);
$statement->bindParam(':newServerName', $serverName, PDO::PARAM_STR);
$statement->bindParam(':newPlayerCount', $activePlayers, PDO::PARAM_INT);
$statement->bindParam(':newMaxPlayers', $maxPlayers, PDO::PARAM_INT);
$statement->bindParam(':newGamemode', $gameMode, PDO::PARAM_STR);
$statement->bindParam(':newBrickCount', $brickCount, PDO::PARAM_INT);
$statement->bindParam(':newVersion', $version, PDO::PARAM_INT);
$statement->bindParam(':newUptime', $uptime, PDO::PARAM_STR);
$statement->bindParam(':newBLID', $bl_id, PDO::PARAM_INT);
$statement->bindParam(':newKeyID', $key_id, PDO::PARAM_STR);
if ($updating)
{
$statement->bindParam(':currentIp', $ip, PDO::PARAM_STR);
$statement->bindParam(':currentPort', $serverPort, PDO::PARAM_INT);
}
else
{
$statement->bindParam(':newIp', $ip, PDO::PARAM_STR);
}
$statement->execute();
$statement = null;
$GLOBALS['database'] = null;
exit('SUCCESS ' . ($updating ? 'Successfully updated server!' : 'Successfully posted server!'));
?>