-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
247 lines (196 loc) · 7.51 KB
/
index.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
<?php
/***********************************************************************************
* This script is distributed under the zlib/libpng License:
*
* Copyright (c) 2013 Kevin VUILLEUMIER (kevinvuilleumier.net)
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would
* be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must
* not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
***********************************************************************************/
$begin = microtime(true);
define(USER_AGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0');
define(ADMIN_EMAIL, '[email protected]');
//error_reporting(E_ALL^E_NOTICE);
set_error_handler('notify_error_handler');
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Thu, 01 Jan 1970 01:00:00 +0100');
function explodeHeaders ($headers) {
$result = array();
$headers = explode("\r\n", $headers);
$i = 0;
foreach ($headers as $header) {
$fragments = explode(": ", $header);
$result[$i++] = array('name' => $fragments[0], 'value' => $fragments[1]);
}
return $result;
}
function addhttp($url) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}
function fetchUrl($uri, &$hlength, $type = 'GET', $timeout = 5) {
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $uri);
curl_setopt($handle, CURLOPT_BINARYTRANSFER, false);
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_ENCODING, "");
curl_setopt($handle, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($handle, CURLOPT_AUTOREFERER, true);
if ($type == 'HEAD') {
curl_setopt($handle, CURLOPT_NOBODY, true);
} else if ($type == 'GET') {
curl_setopt($handle, CURLOPT_POST, false);
}
$response = curl_exec($handle);
if ($response === false) {
return false;
}
$hlength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
// If HTTP response is greater than 400, so it has been an error
if ($httpCode >= 400) {
return false;
}
return $response;
}
function notify_error_handler($number, $message, $file, $line, $vars){
if ($number == E_ERROR || $number == E_STRICT) {
$email = "
<p>An error ($number) occurred on line
<strong>$line</strong> and in the file :<strong>$file.</strong>
<p>$message</p>";
$email .= "<pre>".print_r($vars, 1)."</pre>";
$headers = 'Content-type: text/html; charset=iso-8859-1'."\r\n";
error_log($email, 1, ADMIN_EMAIL, $headers);
echo "<p class=\"error\">There was an error : \"$message\". Don't worry : the admin has been notified !</p>\n";
}
return true;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Redirections Tracer</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en" />
<meta name="Author" content="Kevin Vuilleumier" />
<meta name="Robots" content="index, nofollow" />
<meta name="keywords" content="redirects, tracer, redirections, http, location" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript">
/* <![CDATA[ */
(function() {
var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
t.parentNode.insertBefore(s, t);
})();
/* ]]> */
</script>
</head>
<body>
<h1><a href="./index.php">Redirections Tracer</a></h1>
<?php
$url = "";
$type = 'GET';
if (isset($_GET['link'])) {
$url = htmlentities(strtolower($_GET['link']));
}
if (isset($_GET['type']) && $_GET['type'] == 'GET' || $_GET['type'] == 'HEAD') {
$type = $_GET['type'];
}
echo "<div class=\"box\">\n";
echo "<p>Trace all HTTP redirections of an URL !</p>
<form action=\"./index.php\" method=\"get\">
<table align=\"center\">
<tr>
<td>URL : </td>
<td><input type=\"text\" name=\"link\" value=\"$url\" size=\"40\" /></td>
<td><input type=\"submit\" value=\"Go !\" /></td>
</tr>
</table>
<p>Request type:
<input type=\"radio\" name=\"type\" value=\"GET\"". (($type=='GET')?"checked=\"checked\"":"").">GET
<input type=\"radio\" name=\"type\" value=\"HEAD\"". (($type=='HEAD')?"checked=\"checked\"":"").">HEAD</p>
</form>
</div>";
if (isset($_GET['link']) && (strlen($_GET['link']) > 0)) {
$size = 0;
$url = addhttp(strtolower(trim($_GET['link'])));
$result = fetchUrl($url, $size, $type);
if ($result != false) {
$headers = substr($result, 0, $size);
$headers = explodeHeaders($headers);
$results = array();
$i = 0;
foreach ($headers as $header) {
$name = strtolower($header['name']);
$value = strtolower($header['value']);
if ($value == "" && $name != "") {
$pos = strpos($name, ' ') + 1;
$code = substr($name, $pos, 3);
$results[$i]['code'] = $code;
if ($code < 300 && $i == 0) {
$results[$i++]['location'] = $url;
}
}
if ($name == "location") {
if ($i == 0) {
$results[$i]['location'] = $url;
$results[$i + 1]['location'] = $value;
} else {
$results[$i + 1]['location'] = $value;
}
$i++;
}
}
echo "<div class=\"box\">\n";
foreach ($results as $result) {
$code = $result['code'];
echo "<p><a href=\"{$result['location']}\"><strong>{$result['location']}</strong></a></p>\n";
if ($code == 301) {
echo '<img src="301.png" alt="Redirection 301 (Moved Permanently)" /><br/>'."\n";
} else if ($code == 302) {
echo '<img src="302.png" alt="Redirection 302 (Moved Temporarily)" /><br/>'."\n";
}
}
echo "</div>\n";
} else {
echo "<div class=\"box\">\n";
echo "<p class=\"error\">The given URL is invalid or the website is not reachable!</p>\n";
echo "</div>\n";
}
}
?>
<div id="footer">
<p><a class="FlattrButton" style="display:none;" rev="flattr;button:compact;" href="http://tracer.vuilleumier.tv/"></a></p>
<p>Created by <a href="http://kevinvuilleumier.net">Kevin Vuilleumier</a>. Source code available <a href="https://github.com/vekin03/redirections-tracer">on GitHub</a>.</p>
<p><?php echo 'Generated in '.round((microtime(true) - $begin), 6).' seconds.'; ?></p>
</div>
</body>
</html>