<?php
set_time_limit(1000000);
function tach_string($str)
{
for($i = 0; $i < strlen($str); $i++)
{
$astr[] = substr($str, $i, 1);
}
return $astr;
}
function debug($data)
{
$handle = fopen("./http_response.txt", "w+");
fwrite($handle, $data);
fclose($handle);
}
function gen_cookie($cookies)
{
$cstring = "Cookie: ";
foreach ($cookies as $cookie)
{
$key = array_search($cookie, $cookies);
$cstring .= $key."=".$cookie."; ";
}
return $cstring;
}
function http_header($method, $location, $host, $data, $cookies)
{
$clength = strlen($data);
$in = $method." ".$location." HTTP/1.1\r\n";
$in .= "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";
$in .= "Accept-Language: en-us\r\n";
if ($method == "POST")
{
$in .= "Content-Type: application/x-www-form-urlencoded\r\n";
$in .= "Content-Length: ".$clength."\r\n";
$in .= "Cache-Control: no-cache\r\n";
}
$in .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)\r\n";
$in .= "Host: ".$host."\r\n";
$in .= "Connection: Keep-Alive\r\n";
$in .= gen_cookie($cookies)."\r\n\r\n";
$in .= $data;
return $in;
}
function httpconnect($hostname)
{
$service_port = getservbyname('www', 'tcp');
$address = gethostbyname($hostname);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
}
$result = socket_connect($socket, $address, $service_port);
if ($result < 0) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
}
return $socket;
}
function http_response($method, $socket, $location, $data, $cookie)
{
$in = http_header($method, $location, $host, $data, $cookie);
socket_write($socket, $in, strlen($in)) or die("could not send data!");
$response = "";
while ($out = socket_read($socket, 2048))
{
$response .= $out;
}
debug($response);
return $response;
}
function get_cookies($response)
{
$adata = explode("\n", $response);
foreach ($adata as $cookieline)
{
if (strpos($cookieline, "Set-Cookie:") !== false)
{
$ldata = explode(" ", $cookieline);
$cookiedata = substr($ldata[1], 0, -1);
list ($key, $value) = explode("=", $cookiedata);
$cookie[$key] = $value;
}
}
return $cookie;
}
function link_filter($host, $link)
{
$link1 = str_replace("\"", "", $link);
$link2 = str_replace("'", "", $link);
$msg1 = strtolower($link);
if ((($pos = strpos($msg1, "http://", 0)) === false) && (($pos = strpos($msg1, "HTTP://", 0)) === false))
{
$link2 = "http://".$host."/".$link2;
}
return $link2;
}
function get_pics($host, $location)
{
$socket = httpconnect($host);
$msg1 = http_response("GET", $socket, $location, "", "");
$msg = strtolower($msg1);
$pos = 0;
while(($pos = strpos($msg, "<img", $pos)) !== false)
{
$pos2 = strpos($msg, ">", $pos);
$msg2 = substr($msg1, $pos, $pos2 - $pos + 1);
if (($pos100 = strpos($msg2, "src=", 0)) === false)
{
$strtim = "SRC=";
}
else
{
$strtim = "src=";
}
$m1 = explode($strtim, $msg2);
$m2 = explode(" ", $m1[1]);
if (($pos100 = strpos($m2[0], "\"", 0)) === false)
{
$strtim = "'";
}
else
{
$strtim = "\"";
}
$m3 = explode($strtim, $m2[0]);
$imglinks[] = link_filter($host, $m3[1]);
$pos = $pos2;
}
return $imglinks;
}
function get_piclist($host, $location)
{
$socket = httpconnect($host);
$msg1 = http_response("GET", $socket, $location, "", "");
$msg = strtolower($msg1);
$pos = 0;
while(($pos = strpos($msg, "<a", $pos)) !== false)
{
$pos2 = strpos($msg, ">", $pos);
if (($pos3 = strpos($msg, "><img", $pos)) == $pos2)
{
$pos4 = strpos($msg, ">", $pos3 + 2);
$msg3 = substr($msg1, $pos3 + 1, $pos4 - $pos3);
$msg4 = substr($msg1, $pos, $pos2 - $pos1);
if (($pos100 = strpos($msg3, "src=", 0)) === false)
{
$strtim = "SRC=";
}
else
{
$strtim = "src=";
}
$m1 = explode($strtim, $msg3);
$m2 = explode(" ", $m1[1]);
if (($pos100 = strpos($m2[0], "\"", 0)) === false)
{
$strtim = "'";
}
else
{
$strtim = "\"";
}
$m3 = explode($strtim, $m2[0]);
$th = link_filter($host, $m3[1]);
if (($pos100 = strpos($msg4, "href=", 0)) === false)
{
$strtim = "HREF=";
}
else
{
$strtim = "href=";
}
$m1 = explode($strtim, $msg4);
$m2 = explode(" ", $m1[1]);
if (($pos100 = strpos($m2[0], "\"", 0)) === false)
{
$strtim = "'";
}
else
{
$strtim = "\"";
}
$m3 = explode($strtim, $m2[0]);
$or = link_filter($host, $m3[1]);
if((($pos300 = strpos($or, ".jpg", 0)) !== false) || (($pos300 = strpos($or, ".JPG", 0)) !== false) || (($pos300 = strpos($or, ".bmp", 0)) !== false) || (($pos300 = strpos($or, ".BMP", 0)) !== false) || (($pos300 = strpos($or, ".gif", 0)) !== false) || (($pos300 = strpos($or, ".GIF", 0)) !== false) || (($pos300 = strpos($or, ".pcx", 0)) !== false) || (($pos300 = strpos($or, ".pcx", 0)) !== false))
{
$imglinks['th'][] = $th;
$imglinks['or'][$th] = $or;
}
$pos = $pos4;
}
$pos++;
}
return $imglinks;
}
$host = "www.superiorpics.com";
$location = "/ubbthreads/showflat.php/Cat/0/Number/917395/page/0/fpart/all/vc/1";
$imglinks = get_piclist($host, $location);
mkdir($host, 0777) or die("could not create dir $host");
mkdir("./".$host."/th", 0777) or die("could not create dir th");
mkdir("./".$host."/or", 0777) or die("could not create dir or");
$i = 1;
foreach($imglinks['th'] as $imglink)
{
if (($pos400 = strpos($imglinks['or'][$imglink], "http://", 0)) !== false)
{
$a1 = explode("http://", $imglinks['or'][$imglink]);
$a3 = $a1[1];
}
else
{
$a3 = $imglinks['or'][$imglink];
}
$a2 = explode("/", $a3);
$host2 = $a2[0];
$a5 = explode($host2, $imglinks['or'][$imglink]);
if (($pos400 = strpos($imglinks['or'][$imglink], "http://", 0)) !== false)
{
$location2 = $a5[1];
}
else
{
$location2 = $a5[0];
}
$file = $imglink;
$orlinks = get_pics($host2, $location2);
$newfile1 = "./".$host."/th/pic".$i.".jpg";
copy($file, $newfile1);
foreach($orlinks as $orlink)
{
$newfile2 = "./".$host."/or/pic".$i.".jpg";
copy($orlink, $newfile2);
if (filesize($newfile2) < 10000)
{
unlink($newfile2);
}
}
$i++;
}
printf("Done!");
?>