<?php if(!defined('VIVI_CORE')) exit("duotaopu");
class collect
{
	public function strcut($start,$end,$str,$lt=false,$gt=false){
		if( $str=='' ) return '';
		$strarr=explode($start,$str);
		if( $strarr[1] ){
			$strarr2=explode($end,$strarr[1]);
			$return=$strarr2[0];
			if( $lt ) $return=$start.$return;
			if( $gt ) $return=$return.$end;
		}else{
			return '';
		}
		return $return;
	}
	public function gethtml($url,$timeout=29,$r302=true){
		$user_agent = "Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0";
		$cookie='_lang=zh_CN:GBK; t=ba6a2a7ea0ebd4f0dbef8d54d73b7a7b;';
		if(function_exists('curl_init') && function_exists('curl_exec')){
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_COOKIE,$cookie);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
			curl_setopt($ch, CURLOPT_REFERER, $url);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,$timeout);
			$data = curl_exec($ch);
			$httpCode=curl_getinfo($ch,CURLINFO_HTTP_CODE);
			curl_close($ch);
		}else if ( function_exists('fsockopen') || function_exists('pfsockopen') ){
			$arr = parse_url($url);
			$path=isset($arr['path'])?$arr['path']:"/";
			$host=$arr['host'];
			$port=isset($arr['port'])?$arr['port']:80;
			if ( isset($arr['query']) ){
				$path .= "?".$arr['query'];
			}
			if(function_exists('fsockopen')){
				$fp = fsockopen($host,$port,$errno,$errstr,$timeout);
			}else{
				$fp = pfsockopen($host,$port,$errno,$errstr,$timeout);
			}
			if (!$fp) {
				die("$errstr ($errno)");
			}
			stream_set_timeout( $fp, $timeout );
			$out = "GET {$path} HTTP/1.1\r\n";
			$out .= "Host: {$host}\r\n";
			$out .= "User-Agent: {$user_agent}\r\n";
			$out .= "Accept: */*\r\n";
			$out .= "Accept-Language: zh-cn\r\n";
			$out .= "Accept-Encoding: identity\r\n";
			$out .= "Referer: {$url}\r\n";
			$out .= "Cookie: {$cookie}\r\n";
			$out .= "Connection: Close\r\n\r\n";
			fputs( $fp, $out );
			$data = "";
			$httpCode = substr(fgets($fp, 13),9,3);
			while (($line=fgets( $fp, 1028 ))!== false){
				$data.=$line;
			}
			fclose( $fp );
			preg_match("/Content-Length:.?(\d+)/",$data, $matches);
			if($matches[1]) $data=substr($data,strlen($data)-$matches[1]);
			if(preg_match("~Content-Type: text/xml;~",$data)){
				if(preg_match("~</tmall_items_search_response>~",$data)) $data=strcut("<?xml","</tmall_items_search_response>",$data,1,1);
				if(preg_match("~</error_response>~",$data)) $data=strcut("<?xml","</error_response>",$data,1,1);
				if(preg_match("~</shop_get_response>~",$data)) $data=strcut("<?xml","</shop_get_response>",$data,1,1);
				if(preg_match("~</shoprecommend_items_get_response>~",$data)) $data=strcut("<?xml","</shoprecommend_items_get_response>",$data,1,1);
			}
		}else{
			if (ini_get('allow_url_fopen')) {
				for($i=0;$i<3;$i++){
					if(function_exists('stream_context_create')){
						$opt=array('http'=>array('timeout'=>$timeout,'header'=>"Cookie: ".$cookie));
						$context=stream_context_create($opt);
						$data = file_get_contents($url,false,$context);
					}else{
						$data = file_get_contents($url);
					}
					if($data){
						$httpCode=substr($http_response_header[0],9,3);
						break;
					}
				}
			}else{
				die('服务器未开启php采集函数，无法使用本程序！');
			}
		}
		if($httpCode==302 && $r302 ) return '{302}';
		return $data;
	}
}