class HTTP {
private $cookies;
private $useproxy;
public function HTTP($useproxy=true) {
$this->cookies = array();
$this->useproxy = $useproxy;
}
public function POST($url, $postdata, $ref=null) {
$postdata = http_build_query($postdata);
$s = $this->prepareCurl($ref);
curl_setopt($s,CURLOPT_URL,$url);
curl_setopt($s,CURLOPT_POST, true);
curl_setopt($s,CURLOPT_POSTFIELDS, $postdata);
//Logger::log($url);
return $this->filterCurlHeader($s);
}
public function POSTFILE($url, $postdata, $ref=null) {
//$postdata = http_build_query($postdata);
$s = $this->prepareCurl($ref);
curl_setopt($s,CURLOPT_URL,$url);
curl_setopt($s,CURLOPT_BINARYTRANSFER, true);
curl_setopt($s,CURLOPT_POST, true);
curl_setopt($s,CURLOPT_POSTFIELDS, $postdata);
return $this->filterCurlHeader($s);
}
public function GET($url, $ref=null) {
$s = $this->prepareCurl($ref);
//Logger::log($url);
curl_setopt($s,CURLOPT_URL,$url);
return $this->filterCurlHeader($s);
}
public function GETFILE($url, $filename, $ref=null) {
$s = $this->prepareCurl($ref);
$fp = fopen($filename, "wb");
curl_setopt($s, CURLOPT_HEADER, FALSE);
curl_setopt($s, CURLOPT_URL, $url);
curl_setopt($s, CURLOPT_FILE, $fp);
curl_exec($s);
curl_close($s);
fclose($fp);
}
private function prepareCurl($ref) {
$useragent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; nl; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7";
$s = curl_init();
$this->setcookies($s);
curl_setopt($s,CURLOPT_HTTPHEADER,array('Expect:'));
curl_setopt($s,CURLOPT_USERAGENT,$useragent);
if ($this->useproxy) curl_setopt($s,CURLOPT_PROXY,"localhost:8118");
if (isset($ref)) curl_setopt($s,CURLOPT_REFERER, $ref);
//curl_setopt($s,CURL_COOKIEJAR, dirname(__FILE__)."/cookie.txt");
curl_setopt($s,CURLOPT_AUTOREFERER, true);
curl_setopt($s,CURLOPT_FOLLOWLOCATION, true);
curl_setopt($s,CURLOPT_MAXREDIRS, 5);
curl_setopt($s,CURLOPT_RETURNTRANSFER, true);
curl_setopt($s, CURLOPT_HEADER, true);
return $s;
}
private function setcookies($s) {
if (sizeof($this->cookies) == 0) return;
foreach($this->cookies as $var => $val) {
$tmp[] = "$var=$val";
}
$cookiestring = implode(";", $tmp);
curl_setopt($s, CURLOPT_COOKIE, $cookiestring);
}
private function filterCurlHeader($s) {
$c = curl_exec($s);
curl_close($s);
$tmp = explode("\r\n\r\n", $c);
$header = $tmp[0];
array_shift($tmp);
$content = implode("\r\n\r\n", $tmp);
$lines = explode("\n", $header);
foreach ($lines as $line) {
if (is_int(strpos($line, "Set-Cookie:"))) {
$tmp = explode(":", $line);
$tmp = explode(";", $tmp[1]);
list($var,$val) = explode("=", trim($tmp[0]));
$this->cookies[$var] = $val;
}
}
return $content;
}
public function getCleanHTMLDOM($t) {
$tidy = tidy_parse_string($t);
$tidy->cleanRepair();
$dom = DOMDocument::loadHTML($tidy);
}
}