-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPing.php
More file actions
87 lines (87 loc) · 2.9 KB
/
Ping.php
File metadata and controls
87 lines (87 loc) · 2.9 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
<?php
require_once '../app/require.php';
$api = new APIController;
function pingDomain($host, $count = 4) {
$isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
$pingCommand = $isWindows ? "ping -n {$count} {$host}" : "ping -c {$count} {$host}";
exec($pingCommand, $output, $status);
if ($status !== 0) {
return ['error' => "An error occurred while trying to ping {$host}."];
}
return $output;
// return ['host' => $host, 'result' => $output];
}
function pingDomainString($host, $count = 4) {
$isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
$pingCommand = $isWindows ? "ping -n {$count} {$host}" : "ping -c {$count} {$host}";
exec($pingCommand, $output, $status);
if ($status !== 0) {
return "An error occurred while trying to ping {$host}.";
}
return implode("\n", $output);
}
function pingDomainv2($host, $count = 4) {
$isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
$pingCommand = $isWindows ? "ping -n {$count} {$host}" : "ping -c {$count} {$host}";
exec($pingCommand, $output, $status);
if ($status !== 0) {
return ["An error occurred while trying to ping {$host}."];
}
return $output;
}
// start key
if (isset($_GET['key']))
{
$statusCheck = $api->CheckKey($_GET['key'], $_SERVER['REQUEST_URI']);
// start statusCheck
if ($statusCheck === "valid")
{
// start host check
if (isset($_GET['host']))
{
$host = $_GET['host'];
$returnData = pingDomain($host);
$returnDataString = pingDomainString($host);
$returnDataStringv2 = pingDomainv2($host);
// start data check
if ($returnData !== null)
{
if (isset($_GET['json']))
{
header("Content-Type: application/json");
$api->QueryLog($_GET['host'], "Ping", $_GET['key']);
echo json_encode($returnData, JSON_PRETTY_PRINT);
}
else {
foreach ($returnDataStringv2 as $result) {
echo $result . "<br>";
}
$api->QueryLog($_GET['host'], "Ping", $_GET['key']);
}
}
else {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Content-Type: application/json");
echo json_encode(["error" => "Failed to get location data for host: $host"]);
}
// end data check
}
else {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Content-Type: application/json");
echo json_encode(["error" => "Missing 'host' parameter"]);
}
// end host check
}
else {
echo $statusCheck;
}
}
else {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Content-Type: application/json");
echo json_encode(["error" => "Missing 'key' parameter"]);
}