<?php
/*
* Light Environment Framework (c) NTF
* Author: ntf
* Version: Pre-Alpha 1.0
* Last modified: 23/7/2008 17:51
* Http socket Tool
*
*/
class HTTPSocket {
private $resource;
private $headers;
private $content;
function HTTPSocket($Socketdata=array()){
$this->resource['url']=$Socketdata['url'];
$this->resource['timeout']=isset($Socketdata['timeout'])?$Socketdata['timeout']:60;
$this->resource['get']=array();
$this->resource['cookie']=array();
$this->resource['post']=array();
}
function url($url){
$this->resource['url']=$url;
}
function timeout($timeout){
$this->resource['timeout']=($timeout>5)?$timeout:60;
}
function add_header($add_header_str) {
$this->headers[]= $add_header_str;;
}
function set_header($set_header_str) {
$this->headers=array($set_header_str);
}
function add_get($arrParam) {
$this->resource['get'][]=http_build_query($arrParam); // $this->url .= (strpos($this->url,'?') === false?'?':'&').
}
function add_post($arrParam) {
$this->resource['post'][]= http_build_query($arrParam);
}
function set_useragent($Useragent){
$this->headers['useragent']="User-Agent:{$Useragent}";
}
function set_referer($Referer){
$this->headers['referer']="Referer:{$Referer}";
}
function set_cookie($set_cookiename,$set_cookievalue){
$this->resource['cookie'][$set_cookiename]=$set_cookievalue;
}
function del_cookie($set_cookiename){
unset($this->resource['cookie'][$set_cookiename]);
}
function connect(){
$errno=$errstr='';
$limit=0;
$matches = parse_url($this->resource['url'].(strpos($this->resource['url'],'?') === false?'?':'&').implode('&',$this->resource['get']));
$host = $matches['host'];
$path = $matches['path'] ? $matches['path'].'?'.$matches['query'].'#'.$matches['fragment'] : '/';
$port = !empty($matches['port']) ? $matches['port'] : 80;
$this->headers['useragent']=(!empty($this->headers['useragent']))?$this->headers['useragent']: "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$fp = fsockopen($host,$port, $errno, $errstr,$this->resource['timeout']);
if(!$fp) {
return $errstr.'|'.$errno;
}else{
if(empty($this->resource['post'])){
$out = "GET $path HTTP/1.0\r\n";
}else{
$out = "POST $path HTTP/1.0\r\n";
}
$out .= "Accept: */*\r\n";
$out .= "Accept-Language: zh-hk\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Host: $host\r\n";
if(!empty($this->resource['post'])){
$out .= 'Content-Length: '.strlen(implode('',$this->resource['post']))."\r\n";
}
$out.=implode("\r\n",$this->headers);
$out .= "Connection: Close\r\n";
$out .= "Cache-Control: no-cache\r\n";
$outcookie=array();
if(is_array($this->resource['cookie'])){
foreach($this->resource['cookie'] as $key => $value){
$outcookie[]="{$key}={$value}";
}
}
$out .= "Cookie: ".implode('&',$outcookie)."\r\n\r\n";
$out .= is_array($this->resource['post'])?implode("&",$this->resource['post']):'';
stream_set_blocking($fp,true);
stream_set_timeout($fp, $this->extra['timeout']);
@fwrite($fp, $out);
$status = stream_get_meta_data($fp);
if(!$status['timed_out']) {
while (!feof($fp)) {
if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
break;
}
}
$stop = false;
while(!feof($fp) && !$stop) {
$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
$return .= $data;
if($limit) {
$limit -= strlen($data);
$stop = $limit <= 0;
}
}
}
@fclose($fp);
$this->content=$return;
return$this->content;
}
}
function insert($inserttype){
switch($inserttype){
case'baseurl':
$matches = parse_url($this->resource['url']);
$matches['port'] = !empty($matches['port']) ? $matches['port'] : 80;
$insert_find='/<head>(.*)<\/head>/s';
$insert_replace='<head>\1<base href="http://'.$matches['host'].':'.$matches['port']. $matches['path'].'"/></head>';
break;
}
$this->content=preg_replace($insert_find,$insert_replace,$this->content);
return $this->content;
}
function content(){
return $this->content;
}
function reset(){
$this->HTTPSocket();
$this->content='';
}
}
?>