<?php
class netsSocket extends netsError {
	private $sysErrorClassID='1202';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public $serverAddress='mail.unionnets.com';//服务器
	public $serverServerport=25;//服务器端口
	public $serverUser='vir@unionnets.com';//服务器的用户帐号
	public $serverPxd='1234567abc';//服务器的用户密码
	public $serverCharset='GB2312';
	
	public $socketHandle=0;//Socket 连接句柄
	public $mustCheck=true;//服务器是否要认证，一般都要
	public $mailType='HTML';//邮件格式（HTML/TXT）,TXT为文本邮件
	public $message='';
	public $messageAR0=array();
	public $timeOut=30;
	public $getLength=512;
	
	public function netsSocket($pConfig){
		$this->netsError();
		//-- ------ # ------ --
		if(count($pConfig)>0){
			$this->serverAddress=$pConfig[0];
			$this->serverServerport=$pConfig[1];
			$this->serverUser=$pConfig[2];
			$this->serverPxd=$pConfig[3];
			$this->serverCharset=$pConfig[4];
		}
		
	}
	public function connectionSock(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$errorNo=0;
		$errorSTR='';
		$this->socketHandle=fsockopen($this->serverAddress, $this->serverServerport, $errorNo, $errorSTR, $this->timeOut);
        if($this->socketHandle==NULL){
        	return $this->sysErrorFuncID.'001';
        }
		return 0;
	}
	public function closeSock(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'03';
		if(fclose($this->socketHandle)){
			return $this->sysErrorFuncID.'001';
		}
		return 0;
	}
	public function putSock($pPara){
		$this->sysErrorFuncID=$this->sysErrorClassID.'04';
		$tmpINT=fputs($this->socketHandle,$pPara);
		if(!$tmpINT){
			return $this->sysErrorFuncID.'001';
		}
		return 0;
	}
	public function postSock($pURL,$pPara){
		$this->sysErrorFuncID=$this->sysErrorClassID.'04';
		$tmpCode=new netsCode();
		
		$tmpAR0=$tmpCode->URL_Analyer($pURL);
		$tmpHost=$tmpAR0[0];
		$tmpPort=$tmpAR0[1];
		$tmpPath=$tmpAR0[2];
		
		$tmpSTR='POST {Nets.Path} HTTP/1.0'.$this->sysSPLIT4.
			'Host: {Nets.Host}'.$this->sysSPLIT4.
			'Referer: {Nets.Referer}'.$this->sysSPLIT4.
			'User-Agent: Incutio HttpClient v0.9'.$this->sysSPLIT4.
			'Accept: text/xml,application/xml,application/xhtml+xml,text/html,text/plain,*/*'.$this->sysSPLIT4.
			'Accept-encoding: gzip'.$this->sysSPLIT4.
			'Accept-Language: zh-cn'.$this->sysSPLIT4.
			'Content-Type: application/x-www-form-urlencoded'.$this->sysSPLIT4.
			'Content-length: {Nets.StringLength}'.$this->sysSPLIT4.
			'Cache-Control: no-cache'.$this->sysSPLIT4.
			'Connection: Keep-Alive'.$this->sysSPLIT4.$this->sysSPLIT4.
			'{Nets.Content}'.$this->sysSPLIT4.$this->sysSPLIT4.$this->sysSPLIT4;
		
		$tmpSTR=str_replace('{Nets.Host}',$tmpHost,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Path}','/'.$tmpPath,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Referer}',$pURL,$tmpSTR);
		$tmpSTR=str_replace('{Nets.StringLength}',strlen($pPara),$tmpSTR);
		$tmpSTR=str_replace('{Nets.Content}',$pPara,$tmpSTR);

		$tmpINT=fputs($this->socketHandle,$tmpSTR);
		if(!$tmpINT){
			return $this->sysErrorFuncID.'001';
		}
		return 0;
	}
	public function getGETS(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'05';
		while (!feof($this->socketHandle)) {
			$this->message.=fgets($this->socketHandle,$this->getLength);
		}
		
		return 0;
	}
	public function getSock(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'05';
		$this->message=fgets($this->socketHandle,$this->getLength);
		if(!$this->message){
			return $this->sysErrorFuncID.'001';
		}
		//#
		$tmpAR0=explode(' ',$this->message);
		if(count($tmpAR0)>0){
			$this->messageAR0[0]=$tmpAR0[0];
		}else {
			return $this->sysErrorFuncID.'002';
		}
		if(count($tmpAR0)>1){
			$this->messageAR0[1]=str_replace($this->messageAR0[0].' ','',$this->message);
		}else {
			$this->messageAR0[1]='Non';
		}
		return 0;
	}
	public function getSockContent(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'05';
		$this->message='';
		$this->messageAR0=array();
		$Coun0=0;
		$tmpFlag=0;
		while (!feof($this->socketHandle) && $tmpFlag==0) {
			$tmpSTR=fgets($this->socketHandle, $this->getLength);
			if ($Coun0==0) {
				$this->messageAR0=explode(' ',$tmpSTR);
			}else if($tmpSTR=='.'.$this->sysSPLIT4){
				$tmpFlag=1;
			}else {
				$this->message.=$tmpSTR;
			}
			$Coun0++;
		}

		return 0;
	}
}
?>