<html>
<head><title>Get File</title></head>
<body>
<form action="getfile.php" method="POST">
URL: <input type="text" maxlength="4096" name="url"><br>
<input type="submit" value="submit"> <input type="reset" value="reset">
</form>
</body>
</html>
<?php
class download_url
{
var $page, $type;
function get_url($host, $port, $url)
{
$this->page = "";
$this->type = "text/html";
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp)
{
die("ERROR - $errno: $errstr");
}
else
{
$msg = "GET $url HTTP/1.1\r\n";
$msg .= "Host: $host\r\n";
preg_match("/[^\.\/]+$/", $url, $match);
if ($match[0] == 'pdf')
{
$msg .= "Accept: application/pdf";
$this->type = 'application/pdf';
}
else
{
$msg .= "Accept: text/html";
}
$msg .= "\r\n\r\n";
fputs($fp, $msg);
}
$chunked = 0;
$firstline = 1;
while (($buffer = fgets($fp, 4096)) && ($buffer != "\r\n"))
{
if ($firstline)
{
preg_match("/^HTTP\/\S+\s+(\d{3})\s+\w*/", $buffer, $match);
if ($match[1] == "300")
{
$this->follow($host, $port, $url);
}
$firstline = 0;
}
else
{
if (preg_match("/^Transfer-Encoding:\s+(.+)\r\n/U", $buffer, $match))
{
if (strtoupper($match[1] == "CHUNKED"))
{
$chunked = 1;
}
}
}
}
if (!$chunked)
{
while ($buffer = fgets($fp, 4096))
{
$this->page .= $buffer;
}
}
else
{
while ($buffer = fgets($fp, 4096))
{
if (!preg_match("/\s*([\dabcdef]+)\s*\r\n$/i", $buffer, $match))
{
die("ERROR: Chunk size not found");
}
$size = (int)base_convert(strtoupper($match[1]), 16, 10);
if ($size == 0) die("ERROR: Chunk size == 0");
$buffer = fgets($fp, 4096);
if (strlen($buffer) != $size)
{
die("Can only read: ".strlen($buffer)." bytes\r\n");
}
$this->page .= $buffer;
$buffer = fgets($fp, 4096);
if ($buffer != "\r\n")
{
die("ERROR: CRLF not found");
}
}
}
fclose($fp);
}
function follow($host, $port, $url)
{
$location = $host.":".$port.$url;
preg_match("/^([^:\/]+)(:(\d+))?(\/.*)$/", $location, $match);
$this->get_url($match[1], intval($match[3]), $match[4]);
}
function send_mail($address)
{
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"abcdefghijklmnopqrstuvwxyz\"\r\n";
$header .= "Content-Transfer-Encoding: 7bit";
$msg = "--abcdefghijklmnopqrstuvwxyz\n";
$msg .= "Content-Type: text/plain\n";
$msg .= "Content-Transfer-Encoding: 7bit\n\n";
$msg .= "Xem file gui kem\n\n";
$msg .= "--abcdefghijklmnopqrstuvwxyz\n";
$msg .= "Content-Type: ";
$msg .= ($this->type == 'text/html') ? "text/html\n" : "application/pdf\n";
$msg .= "Content-Transfer-Encoding: base64\n";
$msg .= "Content-Disposition: attachment\n\n";
$msg .= chunk_split(base64_encode($this->page));
$msg .= "\n--abcdefghijklmnopqrstuvwxyz--";
mail($address, "URL", $msg, $header);
}
}
if (isset($_POST['url']) && !empty($_POST['url']))
{
$download = new download_url;
if (!eregi("^http", $_POST['url']))
{
$_POST['url'] = "http://".$_POST['url'];
}
if (!ereg("/$", $_POST['url']))
{
$_POST['url'] .= '/';
}
while (!preg_match("/^http:\/\/([^:\/]+)(:(\d+))(\/.*)$/", $_POST['url'], $match))
{
preg_match("/([^\/\/]*)$/", $_POST['url'], $match);
$url = "/".$match[1];
preg_match("/^http:\/\/([^\/\/]+).*$/", $_POST['url'], $match);
$_POST['url'] = "http://".$match[1].":80".$url;
}
$host = $match[1];
$port = intval($match[3]);
$url = $match[4];
$download->get_url($host, $port, $url);
$download->send_mail('[email protected]');
}
?>