<?
/**
* http request with fsockopen lib
* (c) anight 2001
* (c) Slach 2002 - added socks,https proxy support with authentification
* [@Usage:]
* http_resuest(array('url'=>'http://www.ru','ref'=>'http://referer','proxy'=>''socks4a://user@ipaddres'));
*/
function http_request($task) {
$url = $task['url'];
$cookies = $task['cookies'];
$method = $task['method'] ? $task['method'] : "GET";
$postvars = $task['postvars'];
$ref = $task['ref'];
$proxy = $task['proxy'];
$ua = $task['ua'];
$al = $task['al'];
$realm = $task['realm'];
$compress = $task['compress'];
$conn = ($proxy) ? parse_url($proxy) : parse_url($url);
$url_p = parse_url($url);
$fp = fsockopen($conn['host'], $conn['port'] ? $conn['port'] : 80, $errno, $errstr, 15);
if (!$fp) {
return array("Error: $errstr");
}
set_socket_blocking($fp, true);
socket_set_timeout ($fp, 15);
$c = "";
if (is_array($cookies)) {
reset($cookies);
while (list($k, $v) = each($cookies)) $c .= ($c ? "; " : "") . urlencode($k) . "=" . urlencode($v);
}
$p = "";
if (is_array($postvars)) {
reset($postvars);
while (list($k, $v) = each($postvars)) $p .= ($p ? "&" : "") . urlencode($k) . "=" . urlencode($v);
}
$s = $method . " " . $url . " HTTP/1.0\r\n";
$s .= "Accept: */*\r\n";
$compress ? ($s .= "Accept-Encoding: gzip, deflate\r\n") : 0;
$al ? ($s .= "Accept-Language: $al\r\n") : 0;
//ЁрсюЄр ё PROXY
if ($proxy) {
$conn['scheme']=strtolower($conn['scheme']);
switch ($conn['scheme']) {
case 'socks4':
case 'socks4a':
$str = "\04\01".($url_p['port'] ? pack('n',intval($url_p['port'])) : "\00\x50");
if ($conn['scheme']=='socks4a') $str .= "\00\00\00\01\00$url_p[host]\00";
else {
$ip = gethostbyname($url_p['host']);
if ($ip == $url_p['host']) return array('Error: cannot resolve ip: '.$url_p['host']);
foreach (explode (".", $ip) as $v) $str .= chr($v);
$str .= "\00";
}
fputs ($fp, $str);
$str = fread ($fp, 8);
if ($str == '') return array('Error: connection timeout.');
if ($str[0] != "\00") return array('Error: proxy connection error.');
$errors = array ('', 'Request rejected or failed', 'Cannot connect to identd on the client', 'identd report different user-ids');
$code = ord($str[1]);
if ($code>90) return array('Error: '.$errors[$code-90]);
break;
case 'socks5':
$str = $conn['user'] ? "\05\02\00\02" : "\05\01\00";
fputs ($fp, $str);
$str = fread ($fp, 2);
if ($str == '' || $str[0] != "\05") return array('Error: invalid proxy type '.$conn['scheme']);
if ($str == "\05\02") {
$str = "\01".chr(strlen($conn['user'])).$conn['user'].chr(strlen($conn['pass'])).$conn['pass'];
fputs ($fp, $str);
$str = fread ($fp, 2);
if ($str != "\01\00") return array('Error: authentication error '.$conn['scheme']);
} elseif ($str != "\05\00") {
return array('Error: authentication error '.$conn['scheme']);
}
$str = "\05\01\00\03".chr(strlen($url_p['host'])).$url_p['host'].($url_p['port'] ? pack('n',intval($url_p['port'])) : "\00\x50");
fputs($fp, $str);
$errors = array ('', 'general SOCKS server failure', 'connection not allowed by ruleset',
'network unreacheble', 'host unreacheble', 'connection refused', 'TTL expired',
'command not supported', 'address type not supported', "to X'FF' unassigned");
$head = fread ($fp, 5);
$code = ord($head[1]);
if ($code) return array('Error: '.$errors[$code]);
$lengths = array (0, 3, 0, ord($head[4]), 5);
$len = $lengths[ord($head[3])];
if (!$len) return array('Error: address type not supported');
$head .= $tail = fread($fp, $len+2);
break;
case 'https':
$s = 'CONNECT '.$url_p['host'].':'.($url_p['port'] ? $url_p['port'] : '80')." HTTP/1.0\r\n".$s;
if ($conn['user'] && $proxy) $s.= 'Proxy-authorization: Basic '.base64_encode($conn['user'].":".$conn['pass'])."\r\n";
break;
case 'http':
default:
$proxy ? ($s .= "Proxy-Connection: close\r\n") : 0;
if ($conn['user'] && $proxy) $s.= 'Proxy-authorization: Basic '.base64_encode($conn['user'].":".$conn['pass'])."\r\n";
break;
}
}
if ($url_p['user']) $s.= 'Authorization: '.($realm ? $realm : 'Basic').' '.base64_encode($url_p['user'].":".$url_p['pass'])."\r\n";
$method == "POST" ? ($s .= "Content-Length: " . strlen($p) . "\r\n") : 0;
$method == "POST" ? ($s .= "Content-Type: application/x-www-form-urlencoded\r\n") : 0;
$c ? ($s .= "Cookie: $c\r\n") : 0;
$s .= 'Host: '.$url_p['host'].($url_p['port'] ? ':'.$url_p['port'] : '')."\r\n";
$ref ? ($s .= "Referer: $ref\r\n") : 0;
$ua ? ($s .= "User-Agent: $ua\r\n") : 0;
$s .= "\r\n";
$method == "POST" ? $s .= $p : 0;
fputs($fp, $s);
$heads = true;
unset($h); unset($b);
while (!feof($fp)) {
$line = fgets($fp, 4098);
if ($heads && (!$line || $line == "\r\n" || $line == "\r" || $line == "\n")) { $heads=false; unset($line); }
if ($heads) $h[] = $line;
elseif (isset($line)) $b[] = $line;
// elseif ($line != "\r\n" && $line != "\r" && $line != "\n" && $line) $b[] = $line;
}
fclose($fp);
return array($h, $b);
}
function check_url($url) {
$urlArray = parse_url($url);
if ($urlArray['scheme']=='ftp') {
$host = $urlArray['host']; $user = $urlArray['user']; $pass = $urlArray['pass'];
if ($user == '') { $user='anonymous'; $pass='anonymous@'; }
$ftp = ftp_connect($host);
if ($ftp && ftp_login($ftp, $user, $pass)) {
$buff = ftp_nlist($ftp, $urlArray['path']);
if ($buff[0]!="") {
return true;
} else { return false;}
} else { return false;}
}
if ($urlArray['scheme']=='http') {
if (!$urlArray['port']) $urlArray['port'] = "80";
if (!$urlArray['path']) $urlArray['path'] = "/";
$sock = fsockopen($urlArray['host'], $urlArray['port'], &$errnum, &$errstr);
if (!$sock) return false;
else {
$dump .= "GET $urlArray[path] HTTP/1.0\r\n";
$dump .= "Host: $urlArray[host]\r\nConnection: close\r\n";
$dump .= "Connection: close\r\n\r\n";
fputs($sock, $dump);
while ($str = fgets($sock, 1024)) {
if (eregi("^http/[0-9]+.[0-9]+ ([0-9]{3}) [a-z ]*", $str))
$result[code] = trim(eregi_replace("^http/[0-9]+.[0-9]+ ([0-9]{3}) [a-z ]*", "\\1", $str));
if (eregi("^Content-Type: ", $str))
$result[contentType] = trim(eregi_replace("^Content-Type: ", "", $str));
}
fclose($sock);
flush();
}
} else $result[code] = "N/A";
return $result;
}
?>