-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserverstatus.php
More file actions
64 lines (54 loc) · 1.69 KB
/
serverstatus.php
File metadata and controls
64 lines (54 loc) · 1.69 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
<?php
chdir(dirname(__FILE__));
require __DIR__ . '/library/source/cs_functions.php';
/*-------SETTINGS-------*/
$input_file = 'serverlist.txt';
$output_file = 'serverstatus.json';
/*-------DON'T EDIT------*/
if (file_exists($input_file) & filesize($input_file) !== 0) {
pingServers($input_file, $output_file);
} else {
echo "Error: serverlist.txt either missing or empty!";
}
function pingServers($input, $output) {
try {
$servers_array = getServers($input);
$counter = 0;
foreach ($servers_array as $value) {
if (serverStatus($value['ip'], $value['port']) == true) {
$status = "Online";
} else {
$status = "Offline";
}
$servers_array[$counter]['status'] = $status;
$counter++;
}
echo "Success: ".$counter." servers pinged!";
file_put_contents($output, json_encode($servers_array));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
function serverStatus($ip, $port) {
$fp = @fsockopen($ip, $port, $errno, $errstr, 10);
if (!$fp){
return false;
}
else {
return true;
}
}
function getServers($file) {
$index = 0;
$data = file($file, FILE_IGNORE_NEW_LINES);
foreach ($data as $value) {
$servers[$index] = explode(':', $value);
$server_info_array = GetInfo($servers[$index][2], $servers[$index][3]);
//Build primary array
$players = $server_info_array['Players'].'/'.$server_info_array['MaxPlayers'];
$array[$index] = array('game' => $servers[$index][0], 'name' => $servers[$index][1], 'ip' => $servers[$index][2], 'port' => $servers[$index][3], 'map' => $server_info_array['Map'], 'players' => $players, 'version' => $server_info_array['Version'], 'status' => '');
$index++;
}
return $array;
}
?>