-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbt_prs.php
347 lines (344 loc) · 10.4 KB
/
rbt_prs.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/// copyleft 2012-2013 meklu (public domain)
// This tries to check whether a URL should be accessed
// or not.
// The latest version should be available at
// http://meklu.org/code/rbt_prs.php.bz2
// or alternatively at
// https://github.com/meklu/rbt_prs
// Just pass the url (and user agent, if you'd like) to the function.
// You can also pass your own robots.txt to it as $robots_txt and set
// $redirects to something if you'd like to allow redirects. TRUE will
// enable them and use 20 as the value, whereas FALSE disables this. 1 or
// less means that none are followed.
// If an argument is NULL, its default value will be used.
define("RBT_PRS_VER_MAJOR", "1");
define("RBT_PRS_VER_MINOR", "1");
define("RBT_PRS_VER_PATCH", "5");
define("RBT_PRS_BRANCH", "master");
define("RBT_PRS_VER", RBT_PRS_VER_MAJOR . "." . RBT_PRS_VER_MINOR . "." .
RBT_PRS_VER_PATCH . "-" . RBT_PRS_BRANCH);
define("RBT_PRS_UA", "rbt_prs/" . RBT_PRS_VER .
" (https://github.com/meklu/rbt_prs)");
function isUrlBotSafe($url, $your_useragent = RBT_PRS_UA, $robots_txt = NULL,
$redirects = FALSE, $debug = FALSE) {
if($your_useragent === NULL) $your_useragent = RBT_PRS_UA;
if($debug === NULL) $debug = FALSE;
if($redirects === NULL) $redirects = FALSE;
if($redirects === TRUE) $redirects = 20;
if($redirects !== FALSE && is_int($redirects) === TRUE) {
$redirectarray = array('http' => array('method' => 'GET',
'max_redirects' => $redirects)
);
$redirectcontext = stream_context_create($redirectarray);
}
if($debug === TRUE) {
echo "Argument initialisation finished.\n";
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
echo "PHP version: " . phpversion() . "\n";
echo "rbt_prs version: " . RBT_PRS_VER . "\n";
}
// storing the current ua
$original_ua=ini_get("user_agent");
// switching to the given ua
ini_set("user_agent", $your_useragent);
if($debug === TRUE)
echo "User agent stored and switched.\n";
// slicing up the given url
$tmp=parse_url($url);
// start re-assembling it
$baseurl=$tmp["scheme"] . "://";
if(isset($tmp["user"])) {
$baseurl=$baseurl . $tmp["user"] . ":";
if(isset($tmp["pass"])) {
$basurl=$baseurl . $tmp["pass"];
}
$baseurl=$baseurl . "@";
}
$baseurl=$baseurl . $tmp["host"];
if(isset($tmp["port"])) {
$baseurl=$baseurl . ":" . $tmp["port"];
}
$baseurl=$baseurl . "/";
if(isset($tmp["path"])) {
$checkedpath=$tmp["path"];
} else {
$checkedpath="/";
}
if(isset($tmp["query"])) {
$checkedpath=$checkedpath . "?" . $tmp["query"];
}
if($debug === TRUE)
echo "URL sliced and re-assembled.\n";
// re-assembling the url is finished
// do a bit of magic on the checked path
if(strlen($checkedpath) > 1) {
if(substr_count($checkedpath, '?') == 0) {
if(preg_match("#\w$#", $checkedpath))
$checkedpath=$checkedpath . "/";
} elseif(substr_count($checkedpath, "/?") > 0) {
$checkedpath=str_replace("/?", "/index.stuff?", $checkedpath);
}
}
unset($tmp);
if($debug === TRUE)
echo "Checked path magic done.\n";
// checking whether robots.txt can be accessed or not if the user hasn't
// supplied one.
if($robots_txt === NULL) {
if($redirects !== FALSE && is_int($redirects) === TRUE) {
if($debug === TRUE && $redirects <= 1)
echo "Warning! Setting \$redirects to 1 or less may break things!\n";
if($debug === TRUE) {
$fhandle=fopen($baseurl . "robots.txt", "rb", FALSE,
$redirectcontext);
} else {
$fhandle=@fopen($baseurl . "robots.txt", "rb", FALSE,
$redirectcontext);
}
} else {
if($debug === TRUE) {
$fhandle=fopen($baseurl . "robots.txt", "rb");
} else {
$fhandle=@fopen($baseurl . "robots.txt", "rb");
}
}
if($fhandle === FALSE) {
unset($fhandle);
ini_set("user_agent", $original_ua);
return TRUE;
} else {
if($debug === TRUE)
echo "Obtained file handle.\n";
// we were able to download something!
$raw=stream_get_contents($fhandle);
fclose($fhandle);
unset($fhandle);
// check if we ran into an html (error) page.
// we're looking for the closing tag because <html foo="bar">
if(preg_match("#</html>#i", $raw) > 0) {
if($debug === TRUE) {
echo "Ran into HTML, aborting and returning TRUE...\n";
var_dump($raw);
}
ini_set("user_agent", $original_ua);
return TRUE;
}
}
} else {
$raw=$robots_txt;
unset($robots_txt);
}
if($debug === TRUE)
echo "robots.txt loaded.\n";
// so far so good!
// fixing some newlines and removing comments on top of which we're
// escaping a few characters
// i.e. making all carriage returns newlines and removing duplicates
// and trimming the result
$orig_raw=$raw;
$raw=str_replace("\r", "\n", $raw);
// remove the comments
$raw=preg_replace(":(#).*:", "", $raw);
// make the thing regex-safe
$raw=preg_quote($raw);
// remove duplicate newlines
$raw=preg_replace("#\n+#", "\n", $raw);
// replace empty disallows with "Allow: /" since some people use that
$raw=preg_replace("#^Disallow:(\h*)$#im", "Allow: /", $raw);
// trim that
$raw=trim($raw);
if($debug === TRUE)
echo "robots.txt touched up.\n";
// explode the lines into an array
$lines=explode("\n", $raw);
// initialise our multi-dimensional rule array
$rules=array(
-1 => array(
"ua" => array("*"),
"rules" => array(
"/" => TRUE
)
)
);
// set current user agent to NULL
// this means that lines before the first declaration of a user agent will
// be ignored
$current_agent=NULL;
$ua_index=-1;
$last_is_ua=FALSE;
// process the lines individually
foreach($lines as &$line) {
// explode our lines into two segments
// since we did a preg_quote(), we need a backslash here
$rule=explode("\\:", $line, 2);
// check if we had enough elements
// this makes us silently ignore invalid entries
if(count($rule) == 2) {
$key=stripslashes(trim($rule[0]));
$value=trim($rule[1]);
} else {
if($debug === TRUE)
echo "Less than two pieces of rule.\n\t\"" . $rule[0] . "\"\n";
unset($rule);
continue;
}
// is it a user agent?
if(strcasecmp($key, "user-agent") == 0) {
// initialise ua list
if($last_is_ua === FALSE) {
$ua_index+=1;
$rules[$ua_index]=array(
"ua" => array(),
"rules" => array()
);
}
$current_agent=stripslashes($value);
$rules[$ua_index]["ua"][] = $current_agent;
if($debug === TRUE)
echo "User agent match.\n\t\"" . $value . "\"\n";
unset($rule, $key, $value);
$last_is_ua=TRUE;
continue;
}
// skip the line if we have no UA
if ($ua_index < 0) {
continue;
}
// is it an allow?
if(strcasecmp($key, "allow") == 0) {
$last_is_ua=FALSE;
if(strlen($value) > 1) {
if(substr_count($value, '?') == 0) {
if(preg_match("#\w$#", $value))
$value=$value . "/";
} else {
if(substr_count($value, "/\\?") > 0) {
$value=str_replace("/\\?", "/index\\.\w+\\?", $value);
}
}
}
$rules[$ua_index]["rules"][$value]=TRUE;
if($debug === TRUE)
echo "Allow match.\n\t\"" . $value . "\"\n";
unset($rule, $key, $value);
continue;
}
// is it a disallow?
if(strcasecmp($key, "disallow") == 0) {
$last_is_ua=FALSE;
if(strlen($value) > 1) {
if(substr_count($value, '?') == 0) {
if(preg_match("#\w$#", $value))
$value=$value . "/";
} else {
if(substr_count($value, "/\\?") > 0) {
$value=str_replace("/\\?", "/index\\.\w+\\?", $value);
}
}
}
$rules[$ua_index]["rules"][$value]=FALSE;
if($debug === TRUE)
echo "Disallow match.\n\t\"" . $value . "\"\n";
unset($rule, $key, $value);
continue;
}
if($debug === TRUE)
echo "No match.\n\t\"" . $key . ": " . $value . "\"\n";
unset($rule, $key, $value);
}
unset($line);
unset($current_agent);
if($debug === TRUE)
echo "Rules parsed.\n";
// let's see if we have a match
// $state is TRUE by default because why not
$state=TRUE;
// first checking universal rules
foreach($rules as $rule) {
$ua_match=FALSE;
foreach($rule["ua"] as $ua) {
if(strcmp("*", $ua) === 0) {
$ua_match=TRUE;
}
}
if($ua_match === FALSE) {
continue;
}
if(isset($rule["rules"]["/"])) {
$state=$rule["rules"]["/"];
}
reset($rule["rules"]);
foreach($rule["rules"] as $key => $value) {
if(preg_match("#^" . $key . "#", $checkedpath) > 0) {
$state=$value;
}
}
}
if($debug === TRUE)
echo "Universal rules checked.\n";
// checking rules specific to your user agent
// exploding it into product tags while stripping off all extra info inside
// brackets
$ua_array=explode(" ", trim(preg_replace("# +#", " ",
preg_replace("#\([^(]*?\)#", "", $your_useragent))));
// reversing the array since the first mentioned product tag should be the
// most specific (Section 14.43 of RFC 2616)
$ua_array=array_reverse($ua_array);
foreach($ua_array as $useragent) {
$tmp=preg_replace("#/(.*)#", "", $useragent);
foreach($rules as $rule) {
$ua_match=FALSE;
foreach($rule["ua"] as $ua) {
if(strcmp($tmp, $ua) === 0) {
$ua_match=TRUE;
}
}
if($ua_match === FALSE) {
continue;
}
if(isset($rule["rules"]["/"])) {
$state=$rule["rules"]["/"];
}
reset($rule["rules"]);
foreach($rule["rules"] as $key => $value) {
if(preg_match("#^" . $key . "#", $checkedpath) > 0) {
$state=$value;
}
}
}
if($debug === TRUE)
echo "Specific rules for user agent " . $tmp . " checked.\n";
unset($tmp);
}
if($debug === TRUE) {
echo "Var dumps...\n";
echo "\$checkedpath:\n";
var_dump($checkedpath);
echo "\$baseurl:\n";
var_dump($baseurl);
echo "\$redirects:\n";
var_dump($redirects);
echo "\$raw:\n";
var_dump($raw);
echo "\$orig_raw:\n";
var_dump($orig_raw);
echo "\$rules:\n";
var_dump($rules);
echo "\$your_useragent:\n";
var_dump($your_useragent);
echo "\$ua_array:\n";
var_dump($ua_array);
echo "The URL is ";
if($state === TRUE) {
echo "safe.\n";
} else {
echo "unsafe.\n";
}
}
ini_set("user_agent", $original_ua);
return $state;
}
?>