/
host.php
72 lines (49 loc) · 1.89 KB
/
host.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
header('content-type:application/json');
error_reporting(E_ALL);
ini_set('display_errors',1);
$HostFiles = includes('config.php');
function remove_hashtags($string){
return str_replace('#', '',
preg_replace('/(?:#[\w-]+\s*)+$/', '', $string));
}
function PurgeHostFiles($files){
$combinedFiles = '';
#array of item to search for and remove in string
$srchAdtrj = array('localhost', '::1', '#[IPv6]', ' ');
#Regex strings to use to search through the files and remove unnecessary information
$find = array(
'!/\*.*?\*/!s',
'/\n\s*\n/', #Remove comment blockes
'/\#(.*)$/m', #/Remove comment blockes
'/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/', #Remove linebreaks that are left behind after removing comments
'~^[ \t]++(?=(?:[^<]++|<(?!/?+pre\b))*+(?:\z|<pre\b))~im',
'/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', #Remove linebreakes and space on top of file
);
#Replace values to combine with the find array. The quotation marks must be double once for some reason, else the \n will not work
$replace = array(
"",
"\n",
"",
"\n",
"",
"",
);
if (!sizeof($find) === sizeof($replace)) die("$find and $replace arrays do not match, make sure there are equel mounts of items in both arrays");
foreach ($files as $value) {
$value = file_get_contents($value);
#regex values, see more info in the arrays above
$value = preg_replace($find, $replace, $value);
#plain text values to replace direct examples, se more in the array above
$value = str_replace($srchAdtrj, "", $value);
$combinedFiles .= $value;
}
//remove any space left in the top of the file
return ltrim($combinedFiles);
}
$hostArray = explode("\n", PurgeHostFiles($HostFiles));
echo json_encode($hostArray)
//$myfile = fopen("hosts.txt", "w") or die("Unable to open file!");
//fwrite($myfile, PurgeHostFiles($HostFiles));
//fclose($myfile);
?>