1 | <?php |
||||
2 | function ping($ip, $port) |
||||
3 | { |
||||
4 | $it = microtime(true); |
||||
5 | $check = @fsockopen($ip, $port, $errno, $errstr, 0.2); |
||||
6 | $ft = microtime(true); |
||||
7 | $militime = round(($ft - $it) * 1e3, 2); |
||||
8 | if ($check) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
9 | fclose($check); |
||||
10 | return $militime; |
||||
11 | } else { |
||||
12 | return "unavailable"; |
||||
13 | } |
||||
14 | } |
||||
15 | |||||
16 | function check_the_host($host) |
||||
17 | { |
||||
18 | if (empty($host)) { |
||||
19 | return null; |
||||
20 | } |
||||
21 | |||||
22 | $url = "https://check-host.net/check-ping"; |
||||
23 | $nodes = ["ir3.node.check-host.net", "ir4.node.check-host.net", "ir1.node.check-host.net"]; |
||||
24 | $params = http_build_query([ |
||||
25 | 'host' => $host, |
||||
26 | 'node' => $nodes, |
||||
27 | ]); |
||||
28 | |||||
29 | $ch = curl_init(); |
||||
30 | curl_setopt_array($ch, [ |
||||
31 | CURLOPT_URL => $url . '?' . $params, |
||||
32 | CURLOPT_RETURNTRANSFER => true, |
||||
33 | CURLOPT_HTTPHEADER => ["Accept: application/json"], |
||||
34 | ]); |
||||
35 | |||||
36 | $response = curl_exec($ch); |
||||
37 | |||||
38 | if (curl_errno($ch)) { |
||||
39 | echo "cURL error: " . curl_error($ch); |
||||
40 | return null; |
||||
41 | } |
||||
42 | |||||
43 | $request_id = json_decode($response, true)["request_id"]; |
||||
0 ignored issues
–
show
It seems like
$response can also be of type true ; however, parameter $json of json_decode() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
44 | curl_close($ch); |
||||
45 | |||||
46 | return $request_id; |
||||
47 | } |
||||
48 | |||||
49 | function check_the_ping($request_id) |
||||
50 | { |
||||
51 | if (empty($request_id)) { |
||||
52 | return "Request ID not found!"; |
||||
53 | } |
||||
54 | |||||
55 | $url = "https://check-host.net/check-result/" . $request_id; |
||||
56 | |||||
57 | $ch = curl_init(); |
||||
58 | curl_setopt_array($ch, [ |
||||
59 | CURLOPT_URL => $url, |
||||
60 | CURLOPT_RETURNTRANSFER => true, |
||||
61 | CURLOPT_HTTPHEADER => ["Accept: application/json"], |
||||
62 | ]); |
||||
63 | |||||
64 | $response = curl_exec($ch); |
||||
65 | |||||
66 | if (curl_errno($ch)) { |
||||
67 | echo "cURL error: " . curl_error($ch); |
||||
68 | return null; |
||||
69 | } |
||||
70 | |||||
71 | $decoded_response = json_decode($response, true); |
||||
0 ignored issues
–
show
It seems like
$response can also be of type true ; however, parameter $json of json_decode() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
72 | |||||
73 | $pings = []; |
||||
74 | $nodes = ["ir1.node.check-host.net", "ir3.node.check-host.net", "ir4.node.check-host.net"]; |
||||
75 | |||||
76 | foreach ($nodes as $node) { |
||||
77 | if (empty($decoded_response[$node])) { |
||||
78 | continue; |
||||
79 | } |
||||
80 | |||||
81 | $count = 0; |
||||
82 | $ping_sum = 0; |
||||
83 | foreach ($decoded_response[$node][0] as $value) { |
||||
84 | if (@$value[0] == "OK") { |
||||
85 | $count++; |
||||
86 | $ping_sum += $value[1]; |
||||
87 | } |
||||
88 | } |
||||
89 | |||||
90 | if ($count !== 0) { |
||||
91 | $ping_avg = (@$ping_sum / $count) * 1000; |
||||
92 | $pings[$node] = $ping_avg; |
||||
93 | } |
||||
94 | } |
||||
95 | |||||
96 | $json_pings = json_encode($pings, JSON_PRETTY_PRINT); |
||||
97 | |||||
98 | curl_close($ch); |
||||
99 | |||||
100 | return $json_pings; |
||||
101 | } |
||||
102 | |||||
103 | function accuracy($accuracy) |
||||
104 | { |
||||
105 | $accuracy = (int) $accuracy; |
||||
106 | |||||
107 | if ($accuracy < 0) { |
||||
108 | $accuracy = 0; |
||||
109 | } elseif ($accuracy > 10) { |
||||
110 | $accuracy = 10; |
||||
111 | } |
||||
112 | |||||
113 | sleep($accuracy); |
||||
114 | } |
||||
115 | |||||
116 | function filtered_or_not($input, $accuracy = 3){ |
||||
117 | $request_id = check_the_host($input); |
||||
118 | accuracy($accuracy); |
||||
119 | $pings = check_the_ping($request_id); |
||||
120 | $check_host_data = json_decode($pings, true); |
||||
0 ignored issues
–
show
It seems like
$pings can also be of type null ; however, parameter $json of json_decode() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
121 | $ping_count = 0; |
||||
0 ignored issues
–
show
|
|||||
122 | $precent = [100, 66, 33, 0]; |
||||
123 | if (!is_null($check_host_data)){ |
||||
124 | $ping_count = count($check_host_data); |
||||
125 | $output = $precent[$ping_count] >= 66 ? true : false ; |
||||
126 | } |
||||
127 | return $output; |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
128 | } |
||||
129 | |||||
130 |