-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
350 lines (314 loc) · 10.4 KB
/
index.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
<?php
// Urban Terror PHP Stats
//
// https://github.com/firefly2442/phpurbanterror
// ---------------------------------------------------------------------
// Portions of code adapted from "systates"
// http://systates.sourceforge.net
// ---------------------------------------------------------------------
// Make any config changes in config.ini, not in this file
$config = parse_ini_file( 'config.ini' );
$version = 0.7;
$timeout = 15; // Default timeout for the php socket (seconds)
$length = 2048; // Packet length (should this be larger?)
$protocol = 'udp'; // Default protocol for sending query
$magic = "\377\377\377\377"; // Magic string to send via UDP
$pattern = "/$magic" . "print\n/";
$pattern2 = "/$magic" . "statusResponse\n/";
$players = array(); // List of players
$params = array(); // Game parameters
// color parser (^0 to ^9)
function colorParse($colorize) {
//set color based on Quake color alias
//http://www.computerhope.com/issues/ch000658.htm
//http://wolfwiki.anime.net/index.php/Color_Codes
static $colors = array('black', '#DD2020', '#00CC00', '#DDCC00', '#3377EE', '#00EEEE', '#DD55DD', 'white', 'orange', '#888888');
return "<span style='color:{$colors[$colorize[1]]}'>{$colorize[2]}</span>";
}
//Add ?devmode=1 to the URL to see warnings
//e.g.: http://yourwebsite.com/phpurbanterror/index.php?devmode=1
isset($_GET['devmode']) ? error_reporting(E_ALL) : error_reporting(!E_WARNING);
if(!function_exists("socket_create")) die("<font color=red>socket support missing!</font>");
// Create the UDP socket
$socket = socket_create (AF_INET, SOCK_DGRAM, getprotobyname ($protocol));
if ($socket)
{
if (socket_set_nonblock ($socket))
{
$time = time();
$error = "";
while (!@socket_connect ($socket, $config['host'], $config['port'] ))
{
$err = socket_last_error ($socket);
if ($err == 115 || $err == 114)
{
if ((time () - $time) >= $timeout)
{
socket_close ($socket);
echo "Error! Connection timed out.";
}
sleep(1);
continue;
}
}
// Verify if an error occured
if( strlen($error) == 0 )
{
socket_write ($socket, $magic . "getstatus\n");
$read = array ($socket);
$out = "";
$write = $except = null;
while (socket_select ($read, $write, $except, 1))
{
$out .= socket_read ($socket, $length, PHP_BINARY_READ);
}
if ($out == "")
echo "<font color=red><h2>Unable to connect to server...</h2></font>\n";
socket_close ($socket);
$out = preg_replace ($pattern, "", $out);
$out = preg_replace ($pattern2, "", $out);
$all = explode( "\n", $out );
$params = explode( "\\", $all[0] );
array_shift( $params );
$temp = count($params);
for( $i = 0; $i < $temp; $i++ )
{
$params[ strtolower($params[$i]) ] = $params[++$i];
}
for( $i = 1; $i < count($all) - 1; $i++ )
{
$pos = strpos( $all[$i], " " );
$score = substr( $all[$i], 0, $pos );
$pos2 = strpos( $all[$i], " ", $pos + 1 );
$ping = substr( $all[$i], $pos + 1, $pos2 - $pos - 1 );
$name = substr( $all[$i], $pos2 + 2 );
$name = substr( $name, 0, strlen( $name ) - 1);
$player = array( $name, $score, $ping );
$players[] = $player;
}
//sort by player score
foreach ($players as $key => $row) {
$scores[$key] = $row[1];
}
array_multisort($scores, SORT_DESC, $players);
}
else
{
echo "Unable to connect to server.";
}
}
else
{
echo "Error! Unable to set nonblock on socket.";
}
}
else
{
echo "The server is DOWN!";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link rel="stylesheet" type="text/css" href="./stylesheets/default.css">
<link rel="shortcut icon" href="favicon.ico" />
<script type="text/javascript">
function changeCSS(cssFile, cssLinkIndex) {
var oldlink = document.getElementsByTagName("link").item(cssLinkIndex);
var newlink = document.createElement("link");
newlink.setAttribute("rel", "stylesheet");
newlink.setAttribute("type", "text/css");
newlink.setAttribute("href", cssFile);
document.getElementsByTagName("head").item(0).replaceChild(newlink, oldlink);
}
</script>
<title>Urban Terror Server Status</title>
</head>
<body>
<div class="flex-center">
<img src="urbanterror.jpg" alt="Server Status" title="Server Status">
</div>
<hr>
<br>
<div class="flex-center">
<table>
<tr class="box_titles">
<td><b>
<?php echo preg_replace_callback('~\^(\d)(.*?)(?=\^|$)~', 'colorParse', $params['sv_hostname']) . " - " . $config['host'] . ":" . $config['port']; ?>
</b></td>
<td><b>Players</b></td>
</tr>
<tr>
<td>
<?php
//map information
echo "<b>Map: </b>" . $params['mapname'] . "<br>";
if ($config['location'] != "")
echo "<b>Location: </b>" . $config['location'] . "<br>";
else if (isset($params[' location'])) //<-- note the extra space, bug in default Urban Terror server config file?
echo "<b>Location: </b>" . $params[' location'] . "<br>";
echo count($players) . " / " . $params['sv_maxclients'] . " currently playing<br><br>\n";
if (file_exists("./levelshots/" . $params['mapname'] . ".jpg"))
{
echo "<img src='./levelshots/" . $params['mapname'] . ".jpg' alt='Map: " . $params['mapname'] . "' title='Map: " . $params['mapname'] . "'>\n";
}
else
{
echo "<img src='./levelshots/no_image.jpg' alt='Map: " . $params['mapname'] . " (no image)' title='Map: " . $params['mapname'] . " (no image)'>\n";
}
?>
</td>
<td class="align-top">
<table>
<tr class="box_titles">
<td><b>Player</b></td>
<td><b>Score</b></td>
<td><b>Ping</b></td>
</tr>
<?php //players information
for ($j = 0; $j < count($players); $j++)
{
echo "<tr class='general_row'>\n";
echo "<td>" . preg_replace_callback('~\^(\d)(.*?)(?=\^|$)~', 'colorParse', $players[$j][0]) . "</td>\n";
echo "<td>" . $players[$j][1] . "</td>\n";
if ($players[$j][2] == 999)
echo "<td>Connecting...</td>\n";
else
echo "<td>" . $players[$j][2] . "</td>\n";
echo "</tr>";
}
echo "</table><br>\n";
?>
</td>
</tr>
</table>
</div>
<br>
<div class="flex-center">
<table>
<tr>
<td>
<table>
<tr class="box_titles">
<td><b>Rule</b></td>
<td><b>Setting</b></td>
<?php //server information
echo "<tr class='general_row'>\n";
echo "<td>Urban Terror Version</td>";
echo "<td>" . $params['g_modversion'] . "</td>";
echo "</tr>";
echo "<tr class='general_row'>\n";
echo "<td>Urban Terror Server Version</td>";
echo "<td>" . preg_replace_callback('~\^(\d)(.*?)(?=\^|$)~', 'colorParse', $params['version']) . "</td>";
echo "</tr>";
echo "<tr class='general_row'>\n";
echo "<td>GameType</td>";
if ($params['g_gametype'] == 0 || $params['g_gametype'] == 1 || $params['g_gametype'] == 2)
echo "<td>FreeForAll</td>\n";
if($params['g_gametype'] == 1)
echo "<td>Last Man Standing</td>\n";
if($params['g_gametype'] == 3)
echo "<td>Team Deathmatch</td>\n";
if($params['g_gametype'] == 4)
echo "<td>Team Survivor</td>\n";
if($params['g_gametype'] == 5)
echo "<td>Follow the Leader</td>\n";
if($params['g_gametype'] == 6)
echo "<td>Capture and Hold</td>\n";
if($params['g_gametype'] == 7)
echo "<td>Capture the Flag</td>\n";
if($params['g_gametype'] == 8)
echo "<td>Bomb and Defuse</td>\n";
if($params['g_gametype'] == 9)
echo "<td>Jump</td>\n";
if($params['g_gametype'] == 10)
echo "<td>Freeze Tag</td>\n";
if($params['g_gametype'] == 11)
echo "<td>Gun Game</td>\n";
echo "</tr>";
echo "<tr class='general_row'>\n";
echo "<td>Friendly Fire</td>";
echo "<td>" . $params['g_friendlyfire'] . "</td>";
echo "</tr>";
echo "<tr class='general_row'>\n";
echo "<td>Password Protected</td>";
echo "<td>" . $params['g_needpass'] . "</td>";
echo "</tr>";
if (isset($params['g_warmup'])) {
echo "<tr class='general_row'>\n";
echo "<td>Warmup Time</td>";
echo "<td>" . $params['g_warmup'] . " seconds </td>";
echo "</tr>";
}
if (isset($params['g_swaproles'])) {
echo "<tr class='general_row'>\n";
echo "<td>Swap Roles</td>";
echo "<td>" . $params['g_swaproles'] . "</td>";
echo "</tr>";
}
// calculate voting options based on g_allowvote
echo "<tr class='general_row'>\n";
echo "<td>Voting Allowed On<br></td>";
echo "<td>";
$base_convert = base_convert($params['g_allowvote'], 10, 2);
$vote_values = array ("reload", "restart", "map", "nextmap", "kick/clientKick",
"swapTeams", "shuffleTeams", "g_friendlyFire", "g_followStrict",
"g_gameType", "g_waveRespawns", "timelimit", "fragLimit",
"captureLimit", "g_respawnDelay", "g_redWaveRespawnDelay",
"g_blueWaveRespawnDelay", "g_bombExplodeTime", "g_bombDefuseTime",
"g_survivorRoundTime", "g_caputureScoreTime", "g_warmup",
"g_matchMode", "g_timeouts", "g_timeoutLength", "exec",
"g_swapRoles", "g_maxRounds", "g_gear", "cyclemap");
$index_value = 0;
for ($i = count($vote_values)-1; $i >= 0; $i--)
{
if (substr($base_convert, $i, 1) == "1")
echo $vote_values[$index_value] . "<br>";
$index_value++;
}
if ($params['g_allowvote'] == "0")
echo "No voting allowed";
echo "</td>";
echo "</tr>";
echo "<tr class='general_row'>\n";
echo "<td>Website</td>";
echo "<td>";
if ($config['website'] == "" && !isset($params[' website'])) //<-- note the extra space, bug in default Urban Terror server config file?
echo "None";
else if ($config['website'] != "")
echo "<a class=\"general_row_link\" href=" . $config['website'] . " target=_blank>" . $config['website'] . "</a>\n";
else if (isset($params[' website']))
echo "<a class=\"general_row_link\" href=http://" . $params[' website'] . " target=_blank>" . $params[' website'] . "</a>\n";
echo "</td></tr>";
?>
</table>
</td>
<td class="align-top">
<?php
if (substr_count($params['version'], "win") > 0)
echo "<img class=\"img-circle\" src=\"./images/windows_logo.jpg\" alt=\"Server Runs Windows\" title=\"Server Runs Windows\">\n";
if (substr_count($params['version'], "linux") > 0)
echo "<img class=\"img-circle\" src=\"./images/linux_logo.jpg\" alt=\"Server Runs Linux\" title=\"Server Runs Linux\">\n";
?>
</td>
</tr>
</table>
</div>
<br>
<div class="flex-center">
<p>
<?php echo "<a href='https://github.com/firefly2442/phpurbanterror' target='_blank'>Version: " . $version . " - phpUrbanTerror</a>"; ?>
<br>
Theme:
<a href="#" onclick="changeCSS('stylesheets/default.css', 0);">Default</a> <span class="bull">•</span>
<a href="#" onclick="changeCSS('stylesheets/dark.css', 0);">Grayscale</a>
</p>
</div>
</body>
</html>
<?php
//uncomment to show ALL server variables
//for( $i = 0; $i < count($params); $i++ )
// echo $params[$i] . "<br>";
//?>