<?php
class netsMobile extends netsError {
	private $sysErrorClassID='1202';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;

	protected $sUName='9999';
	protected $sUPxd='9999';
	protected $sURL_SEND='http://http.c123.com/tx/';
	protected $sURL_RECEIVE='http://http.c123.com/rx/';
	protected $sURL_COUNT='http://http.c123.com/mm/';
	public $sContent=array();
	
	public function netsMobile($pConfig){
		$this->netsError();
		//-- ------ # ------ --
		if(count($pConfig)>0){
			$this->sUName=$pConfig[0];
			$this->sUPxd=$pConfig[1];
			$this->sURL_SEND=$pConfig[2];
			$this->sURL_RECEIVE=$pConfig[3];
			$this->sURL_COUNT=$pConfig[4];
		}
	}

	public function sendCSMS($pMobiles,$pContent){
		$tmpErrorFuncID=$this->sysErrorClassID.'01';
		
		$tmpAR0=parse_url($this->sURL_SEND);
		$tmpHost=$tmpAR0['host'];
		$tmpPort=80;
		if(isset($tmpAR0['port'])){
			$tmpPort=$tmpAR0['port'];
		}
		$tmpFile=$tmpAR0['path'];
		$tmpAR0=array(
			'uid'=>$this->sUName,//用户账号
			'pwd'=>strtolower(md5($this->sUPxd)),//MD5位32密码
			'mobile'=>$pMobiles,//号码
			'content'=>$pContent,//内容
			'time'=>date($this->sysTimeFormat),	//定时发送
			'mid'=>'',//子扩展号
			);
		$tmpPost='';
		while (list($k,$v) = each($tmpAR0)) {
			$tmpPost.=rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
		}
		$tmpPost = substr($tmpPost,0,-1);
		$tmpPostLength=strlen($tmpPost);
		$tmpHandle=@fsockopen($tmpHost,$tmpPort,$Errno,$Errstr,10);
		if (!$tmpHandle) {
			fclose($tmpHandle);
			$this->sysLastError='服务器响应错误';
			return 1;
		}
		$tmpReceive='';
		$tmpPut='POST '.$tmpFile.' HTTP/1.1'.$this->sysSPLIT4;
		$tmpPut.='Host:'.$tmpHost.$this->sysSPLIT4;
		$tmpPut.='Content-type: application/x-www-form-urlencoded'.$this->sysSPLIT4;
		$tmpPut.='Connection: Close'.$this->sysSPLIT4;
		$tmpPut.='Content-Length: '.$tmpPostLength.$this->sysSPLIT4.$this->sysSPLIT4;
		$tmpPut.=$tmpPost;		
		fwrite($tmpHandle, $tmpPut);
		while (!feof($tmpHandle)) {
			$tmpReceive.=fgets($tmpHandle, 128);
		}
		fclose($tmpHandle);
		$tmpReceive=explode($this->sysSPLIT4.$this->sysSPLIT4,$tmpReceive);
		unset($tmpReceive[0]);
		$tmpSTR=implode("",$tmpReceive);
		//-- ------ #Errors ------ --
		if($tmpSTR=='101'){
			$this->sysLastError='验证失败';
			return 1;
		}else if($tmpSTR=='102'){
			$this->sysLastError='短信不足';
			return 1;
		}else if($tmpSTR=='103'){
			$this->sysLastError='操作失败';
			return 1;
		}else if($tmpSTR=='104'){
			$this->sysLastError='非法字符';
			return 1;
		}else if($tmpSTR=='105'){
			$this->sysLastError='内容过多';
			return 1;
		}else if($tmpSTR=='106'){
			$this->sysLastError='号码过多';
			return 1;
		}else if($tmpSTR=='107'){
			$this->sysLastError='频率过快';
			return 1;
		}else if($tmpSTR=='108'){
			$this->sysLastError='号码内容空';
			return 1;
		}else if($tmpSTR=='109'){
			$this->sysLastError='账号冻结';
			return 1;
		}else if($tmpSTR=='110'){
			$this->sysLastError='禁止频繁单条发送';
			return 1;
		}else if($tmpSTR=='111'){
			$this->sysLastError='系统暂定发送';
			return 1;
		}else if($tmpSTR=='112'){
			$this->sysLastError='号码不正确';
			return 1;
		}else if($tmpSTR=='120'){
			$this->sysLastError='系统升级';
			return 1;
		}
		
		return 0;
	}
	public function receiveCSMS(){
		$tmpErrorFuncID=$this->sysErrorClassID.'01';
		
		$tmpAR0=parse_url($this->sURL_RECEIVE);
		$tmpHost=$tmpAR0['host'];
		$tmpPort=80;
		if(isset($tmpAR0['port'])){
			$tmpPort=$tmpAR0['port'];
		}
		$tmpFile=$tmpAR0['path'];
		$tmpAR0=array(
			'uid'=>$this->sUName,//用户账号
			'pwd'=>strtolower(md5($this->sUPxd)),//MD5位32密码
			);
		$tmpPost='';
		while (list($k,$v) = each($tmpAR0)) {
			$tmpPost.=rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
		}
		$tmpPost = substr($tmpPost,0,-1);
		$tmpPostLength=strlen($tmpPost);
		$tmpHandle=@fsockopen($tmpHost,$tmpPort,$Errno,$Errstr,10);
		if (!$tmpHandle) {
			fclose($tmpHandle);
			$this->sysLastError='服务器响应错误';
			return 1;
		}
		
		$tmpReceive='';
		$tmpPut='POST '.$tmpFile.' HTTP/1.1'.$this->sysSPLIT4;
		$tmpPut.='Host:'.$tmpHost.$this->sysSPLIT4;
		$tmpPut.='Content-type: application/x-www-form-urlencoded'.$this->sysSPLIT4;
		$tmpPut.='Connection: Close'.$this->sysSPLIT4;
		$tmpPut.='Content-Length: '.$tmpPostLength.$this->sysSPLIT4.$this->sysSPLIT4;
		$tmpPut.=$tmpPost;		
		fwrite($tmpHandle, $tmpPut);
		while (!feof($tmpHandle)) {
			$tmpReceive.=fgets($tmpHandle, 128);
		}
		fclose($tmpHandle);
		$tmpReceive=explode($this->sysSPLIT4.$this->sysSPLIT4,$tmpReceive);
		unset($tmpReceive[0]);
		$tmpSTR=implode("",$tmpReceive);
		//-- ------ #Content ------ --
		$tmpSPLIT1='{&}';
		$tmpSPLIT2='||';
		$this->sContent=array();
		$tmpAR0=explode($tmpSPLIT1,$tmpSTR);
		$Coun0=0;
		foreach($tmpAR0 as $Cell0){
			if($Coun0==0){
				if($Cell0=='101'){
					$this->sysLastError='验证失败';
					return 1;
				}
			}else{
				$tmpAR1=explode($tmpSPLIT2,$Cell0);
				$this->sContent[]=$tmpAR1;
			}
			$Coun0++;
		}
		
		return 0;
	}
	public function countCSMS(){
		$tmpErrorFuncID=$this->sysErrorClassID.'01';
		
		$tmpAR0=parse_url($this->sURL_RECEIVE);
		$tmpHost=$tmpAR0['host'];
		$tmpPort=80;
		if(isset($tmpAR0['port'])){
			$tmpPort=$tmpAR0['port'];
		}
		$tmpFile=$tmpAR0['path'];
		$tmpAR0=array(
			'uid'=>$this->sUName,//用户账号
			'pwd'=>strtolower(md5($this->sUPxd)),//MD5位32密码
			);
		$tmpPost='';
		while (list($k,$v) = each($tmpAR0)) {
			$tmpPost.=rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
		}
		$tmpPost = substr($tmpPost,0,-1);
		$tmpPostLength=strlen($tmpPost);
		$tmpHandle=@fsockopen($tmpHost,$tmpPort,$Errno,$Errstr,10);
		if (!$tmpHandle) {
			fclose($tmpHandle);
			$this->sysLastError='服务器响应错误';
			return 1;
		}
		
		$tmpReceive='';
		$tmpPut='POST '.$tmpFile.' HTTP/1.1'.$this->sysSPLIT4;
		$tmpPut.='Host:'.$tmpHost.$this->sysSPLIT4;
		$tmpPut.='Content-type: application/x-www-form-urlencoded'.$this->sysSPLIT4;
		$tmpPut.='Connection: Close'.$this->sysSPLIT4;
		$tmpPut.='Content-Length: '.$tmpPostLength.$this->sysSPLIT4.$this->sysSPLIT4;
		$tmpPut.=$tmpPost;		
		fwrite($tmpHandle, $tmpPut);
		while (!feof($tmpHandle)) {
			$tmpReceive.=fgets($tmpHandle, 128);
		}
		fclose($tmpHandle);
		$tmpReceive=explode($this->sysSPLIT4.$this->sysSPLIT4,$tmpReceive);
		unset($tmpReceive[0]);
		$tmpSTR=implode("",$tmpReceive);
		//-- ------ #Content ------ --
		$tmpSPLIT1='||';
		$this->sContent=array();
		$tmpAR0=explode($tmpSPLIT1,$tmpSTR);
		$Coun0=0;
		foreach($tmpAR0 as $Cell0){
			if($Coun0==0){
				if($Cell0=='101'){
					$this->sysLastError='验证失败';
					return 1;
				}
			}else{
				$this->sContent[]=$Cell0;
			}
			$Coun0++;
		}
		
		return 0;
	}
}
?>