This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shalla2hosts.php
217 lines (176 loc) · 5.44 KB
/
shalla2hosts.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
//set default list, so simply adding an URL-parameter "?download" will suffice
$preSelection=array("ads","hacking","music","porn","sex","spyware","violence","adv","audio-video","tracker","warez","webradio","aggressive","drugs","podcasts","radiotv","updatesites","webtv","movies");
//use selected categories or use defaults.
if(isset($_GET["c"]))
$selectedCategories=$_GET["c"];
else
$selectedCategories=$preSelection;
//get update time of last update.
$lastDownloadTime=getLastUpdateTime();
//if local list is older than 1 week
if($lastDownloadTime<strtotime("-1 week"))
{
@unlink("shallalist.tar.gz");
@unlink("shallalist.tar");
downloadShallaList("http://www.shallalist.de/Downloads/shallalist.tar.gz");
unpackList();
$lastDownloadTime=saveDownloadTimeToFile();
}
//if downloadbutton is hit
if(isset($_GET["download"]))
{
//generate random file
$tempFileName="hosts".mt_rand();
//add each selected category to hosts-file
foreach ($selectedCategories as $category)
{
assembleHostsFile($category,$tempFileName);
}
//deliver it as download
header('Content-Disposition: attachment; filename="hosts"');
//add some header information.
echo "#######################################################################################\n";
echo "#This file is derived from URL blacklist maintained by http://www.shallalist.de/\n";
echo "#using an automatic conversion tool: https://github.com/derStephan/shallalistToHosts\n";
echo "#Note: The author of this tool is in no way connected to shalla.\n";
echo "#\n";
echo "#creation date: ".date("c")."\n";
echo "#last list update: ".date("c",$lastDownloadTime)."\n";
echo "#For automated download use: wget \"http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]."\" -O hosts\n";
echo "#######################################################################################\n";
readfile($tempFileName);
//delete temp file
unlink($tempFileName);
}
else
{
//if downloadbutton is not pressed, show list.
showAvailableCategorySelection($lastDownloadTime, $selectedCategories);
}
function showAvailableCategorySelection($lastDownloadTime, $selectedCategories)
{
//get main categories from list
$categoryDirectories=glob("list/BL/*");
?>
<!DOCTYPE html>
<head>
<title>Shalla to hosts</title>
<script type="text/javascript">
function toggle(source)
{
checkboxes = document.getElementsByName('c[]');
for(var i=0, n=checkboxes.length;i<n;i++)
{
checkboxes[i].checked = source.checked;
}
}
</script>
</head>
<body>
last list update: <?=date("c",$lastDownloadTime);?>
<form method="GET">
<input type="checkbox" value="select all" onClick="toggle(this)" id="selectAll"><label for="selectAll">toggle all</label><br/><br />
<input type="submit" value="download hosts" name="download"><br />
<?php
foreach ($categoryDirectories as $categoryDirectory)
{
if(is_dir($categoryDirectory))
{
$category=explode("/",$categoryDirectory);
$category=$category[2];
?>
<input type="checkbox" name="c[]" value="<?=$category?>" id="<?=$category?>" <?php if(in_array($category,@$selectedCategories)) echo "checked"; ?>><label for="<?=$category?>"><?=$category?></label><br />
<?php
}
}
?>
<input type="submit" value="download hosts" name="download">
</form>
</body>
</html>
<?php
}
function getLastUpdateTime()
{
if (!file_exists("lastDownload"))
return 0;
$fp=fopen("lastDownload","r");
$lastDownloadTime=fgets($fp);
fclose($fp);
return $lastDownloadTime;
}
function downloadShallaList($url)
{
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $url);
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds, pretty high!
curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
// Download the given URL, and save output
$contents = curl_exec($ch);
//write output to file
$fp=fopen("shallalist.tar.gz","w");
fwrite($fp, $contents);
fclose($fp);
// Close the cURL resource, and free system resources
curl_close($ch);
}
function unpackList()
{
$p = new PharData('shallalist.tar.gz');
$p->decompress(); // creates files.tar
// unarchive from the tar
$phar = new PharData('shallalist.tar');
$phar->extractTo('list',null,true);
}
function saveDownloadTimeToFile()
{
//save downloadtime to file.
$fp=fopen("lastDownload","w");
fwrite($fp, time());
fclose($fp);
return time();
}
function assembleHostsFile($category,$hostsFilename)
{
$directory=glob("list/BL/$category");
@$directory=$directory[0];
if($directory[0]=="")
return false;
if(file_exists ("$directory/domains"))
writeDomainsToHosts("$directory/domains",$hostsFilename);
else
{
$subCategories=glob("$directory/*");
foreach($subCategories as $subCategory)
{
writeDomainsToHosts("$subCategory/domains",$hostsFilename);
}
}
}
function writeDomainsToHosts($filePath,$hostsFilename)
{
$domains=fopen($filePath,"r");
$hosts=fopen($hostsFilename,"a");
//add heading for upcoming list.
fwrite($hosts,"#category: $filePath \n");
while($line = fgets($domains))
{
if(!filter_var(trim($line), FILTER_VALIDATE_IP))
fwrite($hosts,"127.0.0.1 $line");
}
fclose($hosts);
fclose($domains);
}
?>