/
detect-asciiart.php
executable file
·205 lines (154 loc) · 4.44 KB
/
detect-asciiart.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
<?php
$cacheDirName = 'cache';
$numFilesProcessed = 0;
/*
// Loop over all sub directories of "cache".
if($outerDirHandle = opendir($cacheDirName)) {
while(false !== ($innerDirName = readdir($outerDirHandle))){
if($innerDirName != '.' && $innerDirName != '..' && is_dir($cacheDirName.'/'.$innerDirName)){
// In each subdirectory, loop over all files (pages).
if($innerDirHandle = opendir($cacheDirName.'/'.$innerDirName)) {
while(false !== ($fileName = readdir($innerDirHandle))){
if($fileName != '.' && $fileName != '..' && !is_dir($cacheDirName.'/'.$innerDirName.'/'.$fileName)){
// Process the page.
++$numFilesProcessed;
handlePage($fileName, gzinflate(file_get_contents($cacheDirName.'/'.$innerDirName.'/'.$fileName)));
}
}
closedir($innerDirHandle);
}
}
}
closedir($outerDirHandle);
}
*/
$inFile = @fopen('domains.txt', 'r');
$outFile = @fopen('ascii_art.txt', 'a');
if ($inFile && $outFile) {
// Read line by line until failing.
while(($line = fgets($inFile)) !== false){
// Extract the domain from the line.
$domain = trim($line);
$innerDirName = substr($domain, 0, min(2, strpos($domain, '.')));
$innerFileName = $cacheDirName.'/'.$innerDirName.'/'.$domain;
if(file_exists($innerFileName)){
$pageContent = gzinflate(file_get_contents($innerFileName));
// We only bother to look at the first comment of the page.
$firstComment = extractFirstComment($pageContent);
if($firstComment && isAsciiArt($firstComment)){
// Log the comment and the domain name to a file.
$domainAndArt = "\n\n\n".$domain."\n\n".$firstComment;
fwrite($outFile, $domainAndArt);
print("\n".$domainAndArt);
print("\n\n".'processed '.$numFilesProcessed.' files');
}
++$numFilesProcessed;
}
}
fclose($outFile);
fclose($inFile);
}
print('done');
function handlePage($domain, $pageContent){
global $numFilesProcessed;
// We only bother to look at the first comment of the page.
$firstComment = extractFirstComment($pageContent);
if($firstComment && isAsciiArt($firstComment)){
// Log the comment and the domain name to a file.
$domainAndArt = "\n\n\n".$domain."\n\n".$firstComment;
file_put_contents('ascii_art.txt', $domainAndArt, FILE_APPEND);
print("\n".$domainAndArt);
print("\n\n".'processed '.$numFilesProcessed.' files');
}
}
function extractFirstComment($pageContent){
$tidy = tidy_parse_string($pageContent, array(), 'ascii');
return firstTidyComment($tidy->root());
}
function firstTidyComment($tinyNode){
// Found value.
if($tinyNode->isComment())
return $tinyNode->value;
// Recurse.
if($tinyNode->hasChildren())
foreach($tinyNode->child as $child){
$possiblyComent = firstTidyComment($child);
if($possiblyComent)
// Got one!
return $possiblyComent;
}
// No comment found.
return false;
}
function isAsciiArt($comment){
$numChars = strlen($comment);
// No huge chunks of text.
if($numChars > 2000)
return false;
$numLines = count(explode("\n", $comment));
// Must be at least 5 lines.
if($numLines < 5)
return false;
// Must be less than a page.
if($numLines > 40)
return false;
// Try to block commented-out HTML. Roughly sorted by frequency of occurence as anecdotally spotted in the wild.
foreach(array(
// Head element stuff
'<style',
'<meta',
'<![endif]',
'[if IE',
'[if lt IE ',
'[if lte IE ',
'[if gte IE',
// Some end-tags
'</td>',
'</tr>',
'</script>',
'</option>',
'</div>',
'</a>',
'</li>',
'</ul>',
'</object>',
'</p>',
'</form>',
'</body>',
// Some CMS signatures
'TYPO3',
'START DEBUG OUTPUT',
'generated',
'XT-Commerce',
'TYPOlight',
'Contao Open Source CMS',
'W3 Total Cache',
'Free CSS Templates',
'DYNAMIC PAGE-SPECIFIC META TAGS WILL BE PLACED HERE',
'vBulletin',
'phpBB',
'Shopsoftware by Gambio',
'BLOX CMS',
'Shopsystem powered by',
'phpwcms',
// Misc. garbage
'<rdf:RDF',
'src="',
'Exception]:',
'DoubleClick',
'ct=WEBSITE',
'Unfortunately, Microsoft has added a clever new',
'skype.com/go/skypebuttons',
'eXTReMe Non Public Tracker Code',
'CURRENCY SETTING:',
'This page is valid XHTML 1.0 Transitional',
'Be inspired, but please don\'t steal...',
'This credit note should not be removed',
'chCounter',
'These paths are pathed fo veiwing by a browser',
'MyFonts Webfont Build',
) as $codeFragment)
if(strpos($comment, $codeFragment) !== false)
return false;
return true;
}