/
hosts.php
72 lines (58 loc) · 1.14 KB
/
hosts.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
<?php
$hosts = file("/etc/hosts");
$i=0;
foreach ($hosts as $line) {
$line = trim($line);
if(empty($line)) continue;
if(substr($line, 0,1)=="#") {
$line = trim($line,"#");
$line = preg_split("/[\s\t]+/", $line);
$ip = array_shift($line);
if(is_ipv4($ip)) {
$disabled['ipv4'][$ip] = $line;
continue;
}
if(is_ipv6($ip)) {
$disabled['ipv6'][$ip] = $line;
continue;
}
$comment_line[] = "# ".implode(" ",$line);
continue;
}
$line = preg_split("/[\s\t]+/", $line);
$ip = array_shift($line);
// preveri če ip obstaja, da ne prepiše
if(is_ipv4($ip)) {
foreach($line as $l) {
$entry['ipv4'][$ip][] = $l;
}
continue;
}
if(is_ipv6($ip)) {
foreach($line as $l) {
$entry['ipv6'][$ip][] = $l;
}
continue;
}
$i++;
}
$output['disabled'] = $disabled;
$output['comment'] = $comment_line;
$output['entry'] = $entry;
print_r($output);
function is_ipv6($ip) {
if( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ){
return true;
}
else {
return false;
}
}
function is_ipv4($ip) {
if( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ){
return true;
}
else {
return false;
}
}