-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathconverter.php
More file actions
238 lines (187 loc) · 6.69 KB
/
Copy pathconverter.php
File metadata and controls
238 lines (187 loc) · 6.69 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/** SETTINGS *************************************************************/
// Parse CLI arguments into the $_GET global.
if ( isset( $argv ) ) {
parse_str( implode( '&', array_slice( $argv, 1 ) ), $_GET );
}
// Parse list from CLI parameters if available.
if ( ! empty( $_GET['url'] ) && ! empty( $_GET['name'] ) ) {
$lists = [
$_GET['name'] => $_GET['url']
];
// Default lists.
} else {
$lists = array(
// Mobile Ads
'AdguardMobileAds' => 'https://raw.githubusercontent.com/AdguardTeam/FiltersRegistry/master/filters/filter_11_Mobile/filter.txt',
// Mobile Tracking + Spyware
'AdguardMobileSpyware' => 'https://raw.githubusercontent.com/AdguardTeam/AdguardFilters/master/SpywareFilter/sections/mobile.txt',
// Adguard DNS
'AdguardDNS' => 'https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt',
// Adguard CNAME Ads
'AdguardCNAMEAds' => 'https://raw.githubusercontent.com/AdguardTeam/cname-trackers/master/data/combined_disguised_ads.txt',
// Adguard CNAME Clickthroughs
'AdguardCNAMEClickthroughs' => 'https://raw.githubusercontent.com/AdguardTeam/cname-trackers/master/data/combined_disguised_clickthroughs.txt',
// Adguard CNAME Microsites
'AdguardCNAMEMicrosites' => 'https://raw.githubusercontent.com/AdguardTeam/cname-trackers/master/data/combined_disguised_microsites.txt',
// Adguard CNAME Trackers
'AdguardCNAME' => 'https://raw.githubusercontent.com/AdguardTeam/cname-trackers/master/data/combined_disguised_trackers.txt',
// Adguard Tracking
'AdguardTracking' => 'https://raw.githubusercontent.com/AdguardTeam/FiltersRegistry/master/filters/filter_3_Spyware/filter.txt',
// EasyPrivacy Specific
'EasyPrivacySpecific' => 'https://raw.githubusercontent.com/easylist/easylist/master/easyprivacy/easyprivacy_specific.txt',
// EasyPrivacy Third-Party
'EasyPrivacy3rdParty' => 'https://raw.githubusercontent.com/easylist/easylist/master/easyprivacy/easyprivacy_thirdparty.txt',
);
}
/** PARSER ***************************************************************/
$idn_to_ascii = function_exists( 'idn_to_ascii' );
foreach ( $lists as $name => $list ) {
echo "Converting {$name}...\n";
// Fetch filter list and explode into an array.
$lines = file_get_contents( $list );
$lines = explode( "\n", $lines );
// HOSTS header.
$hosts = "# {$name}\n";
$hosts .= "#\n";
$hosts .= "# Converted from - {$list}\n";
$hosts .= "# Last converted - " . date( 'r' ) . "\n";
$hosts .= "#\n\n";
$domains = $exceptions = array();
// Loop through each ad filter.
foreach ( $lines as $filter ) {
// Skip filter if matches the following:
if ( false === strpos( $filter, '.' ) ) {
continue;
}
if ( false !== strpos( $filter, '*' ) ) {
continue;
}
if ( false !== strpos( $filter, '/' ) ) {
continue;
}
if ( false !== strpos( $filter, '#' ) ) {
continue;
}
if ( false !== strpos( $filter, ' ' ) ) {
continue;
}
if ( false !== strpos( $filter, 'abp?' ) ) {
continue;
}
// Skip Adguard HTML filtering syntax.
if ( false !== strpos( $filter, '$$' ) || false !== strpos( $filter, '$@$' ) ) {
continue;
}
// Skip domain rules.
if ( false !== strpos( $filter, '$domain' ) || false !== strpos( $filter, ',domain' ) ) {
continue;
}
// Skip third-party rules.
if ( false !== strpos( $filter, '$third-party' ) || false !== strpos( $filter, ',third-party' ) ) {
continue;
}
// Skip ping rules.
if ( false !== strpos( $filter, '$ping' ) || false !== strpos( $filter, ',ping' ) ) {
continue;
}
// Skip redirect rules.
if ( false !== strpos( $filter, '$redirect' ) || false !== strpos( $filter, ',redirect' ) ) {
continue;
}
// Skip Adguard $app rules. See #43.
if ( false !== strpos( $filter, '$app' ) || false !== strpos( $filter, ',app' ) ) {
continue;
}
// Skip Adguard $cookie rules. See #43.
if ( false !== strpos( $filter, '$cookie' ) || false !== strpos( $filter, ',cookie' ) ) {
continue;
}
// Skip Adguard $referrerpolicy rules.
if ( false !== strpos( $filter, '$referrerpolicy' ) || false !== strpos( $filter, ',referrerpolicy' ) ) {
continue;
}
// Replace filter syntax with HOSTS syntax.
// @todo Perhaps skip $image and $popup?
$filter = str_replace( array( '||', '^', '$all', ',all', '$image', ',image', ',important', '$script', ',script', '$object', ',object', '$popup', ',popup', '$empty', '$object-subrequest', '$document', '$subdocument', ',subdocument', '$important', '$badfilter', ',badfilter', '$websocket', '$other' ), '', $filter );
// Skip rules matching 'xmlhttprequest' for now.
if ( false !== strpos( $filter, 'xmlhttprequest' ) ) {
continue;
}
// Skip exclusion rules.
if ( false !== strpos( $filter, '~' ) ) {
continue;
}
// Trim whitespace.
$filter = trim( $filter );
// If starting or ending with '.', skip.
if ( '.' === substr( $filter, 0, 1 ) || '.' === substr( $filter, -1 ) ) {
continue;
}
// If starting with '-', skip.
// https://github.com/r-a-y/mobile-hosts/issues/5
if ( '-' === substr( $filter, 0, 1 ) || '_' === substr( $filter, 0, 1 ) ) {
continue;
}
// If starting with '!', skip.
if ( '!' === substr( $filter, 0, 1 ) ) {
continue;
}
// If starting with '|', skip.
if ( '|' === substr( $filter, 0, 1 ) ) {
continue;
}
// If ending with '=', skip.
if ( '=' === substr( $filter, -1 ) ) {
continue;
}
// If contains a comma, skip.
if ( false !== strpos( $filter, ',' ) ) {
continue;
}
// Strip trailing |.
if ( '|' === substr( $filter, -1 ) ) {
$filter = str_replace( '|', '', $filter );
}
// Skip file extensions
if ( '.jpg' === substr( $filter, -4 ) || '.gif' === substr( $filter, -4 ) ) {
continue;
}
// Skip email tracker Adguard syntax. See #34.
if ( false !== strpos( $filter, '%' ) ) {
continue;
}
// Strip port numbers.
if ( false !== strpos( $filter, ':' ) ) {
$filter = substr( $filter, 0, strpos( $filter, ':' ) );
}
// Convert internationalized domain names to punycode.
if ( $idn_to_ascii && preg_match( "//u", $filter ) ) {
$filter = idn_to_ascii( $filter );
}
// If empty, skip.
if( empty( $filter ) ) {
continue;
}
// Save exception to parse later.
if ( 0 === strpos( $filter, '@@' ) ) {
$exceptions[] = '0.0.0.0 ' . str_replace( '@@', '', $filter );
continue;
}
$domains[] = "0.0.0.0 {$filter}";
}
// Generate the hosts list.
if ( ! empty( $domains ) ) {
// Filter out duplicates.
$domains = array_unique( $domains );
// Remove exceptions.
if ( ! empty( $exceptions ) ) {
$domains = array_diff( $domains, $exceptions );
}
$hosts .= implode( "\n", $domains );
unset( $domains );
}
// Output the file.
file_put_contents( "{$name}.txt", $hosts );
echo "{$name} converted to HOSTS file - see {$name}.txt\n";
}