<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &创明短信平台发送类文件&
 */

if (!defined('IS_RUN')) die('Forbsendidden');

class tj_sendsms_c123 extends tj_model {
	
	var $SMS_uid;
	var $SMS_pwd;
	var $SMS_mobile;
	var $SMS_content;
	var $SMS_time;
	
    function __construct($SMS_uid,$SMS_pwd) {
		$this->SMS_uid=$SMS_uid;
		$this->SMS_pwd=$SMS_pwd;
    }

    function tj_sendsms_c123($SMS_uid,$SMS_pwd) {
        $this->__construct($SMS_uid,$SMS_pwd);
    }
	
	//发送短信
	function sendSMS($post,$variable=null,$ajax=null) {
		
		//定时发送
		if($post['dtime']) $post['time']= $post['y'].'-'.$post['m'].'-'.$post['d'].' '.$post['h'].':'.$post['n'].':'.$post['s'];
		$this->check_post($post);
        $http = 'http://http.c123.com/tx/';
		$data = array
		(
		'uid'=>$post['uid'],							//用户账号
		'pwd'=>strtolower(md5($post['pwd'])),			//MD5位32密码
		'mobile'=>$post['mobile'],						//号码
		'content'=>urlencode($post['content']),			//内容
		'time'=>$post['time'],							//定时发送
		'encode'=>'utf8',								//字符编码
		'mid'=>$mid										//子扩展号
		);
		$re= $this->postSMS($http,$data);				//POST方式提交
		
		if( trim($re) == '100' )
		{
		return 100;
		}
		else 
		{
			switch ($re){
				
			case "101":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "验证失败！";
				outputJson($json);
			}
			Note('验证失败！');
			break;

			case "102":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "短信不足！";
				outputJson($json);
			}
			Note('短信不足！');
			break;
			
			case "103":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "操作失败！";
				outputJson($json);
			}
			Note('操作失败！');
			break;
			
			case "104":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "非法字符！";
				outputJson($json);
			}
			Note('非法字符！');
			break;
			
			case "105":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "内容过多！";
				outputJson($json);
			}
			Note('内容过多！');
			break;
			
			case "106":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "号码过多！";
				outputJson($json);
			}
			Note('号码过多！');
			break;
			
			case "107":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "频率过快！";
				outputJson($json);
			}
			Note('频率过快！');
			break;
			
			case "108":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "手机号码不能为空！";
				outputJson($json);
			}
			Note('手机号码不能为空！');
			break;
			
			case "109":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "账号冻结！";
				outputJson($json);
			}
			Note('账号冻结！');
			break;
			
			case "110":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "禁止频繁单条发送！";
				outputJson($json);
			}
			Note('禁止频繁单条发送！');
			break;
			
			case "111":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "系统暂定发送！";
				outputJson($json);
			}
			Note('系统暂定发送！');
			break;
			
			case "112":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "号码不正确！";
				outputJson($json);
			}
			Note('号码不正确！');
			break;
			
			case "120":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "系统升级！";
				outputJson($json);
			}
			Note('系统升级！');
			break;
	
			
			}
		}
    }
	
	//获取剩余&已发短信条数
	function remainderSMS($post,$send=null) {
		$this->check_post($post);
        $http = 'http://http.c123.com/mm/';
		$data = array
		(
		'uid'=>$post['uid'],							//用户账号
		'pwd'=>strtolower(md5($post['pwd'])),			//MD5位32密码
		'cmd'=>$send,
		);
		$re= $this->postSMS($http,$data);				//POST方式提交
		$reto = explode('||',$re);

		if($reto[0]=='100'){
			return $reto[1];
		}
		elseif($reto[0]=='101'){
			return 1;
		}else{
			return 0;
		}
    }
	
	function postSMS($url,$data='')
{
	$row = parse_url($url);
	$host = $row['host'];
	$port = $row['port'] ? $row['port']:80;
	$file = $row['path'];
	while (list($k,$v) = each($data)) 
	{
		$post .= rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
	}
	$post = substr( $post , 0 , -1 );
	$len = strlen($post);
	$fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
	if (!$fp) {
		return "$errstr ($errno)\n";
	} else {
		$receive = '';
		$out = "POST $file HTTP/1.1\r\n";
		$out .= "Host: $host\r\n";
		$out .= "Content-type: application/x-www-form-urlencoded\r\n";
		$out .= "Connection: Close\r\n";
		$out .= "Content-Length: $len\r\n\r\n";
		$out .= $post;		
		fwrite($fp, $out);
		while (!feof($fp)) {
			$receive .= fgets($fp, 128);
		}
		fclose($fp);
		$receive = explode("\r\n\r\n",$receive);
		unset($receive[0]);
		return implode("",$receive);
	}
}

    function check_post(& $post) {
        //if($edit && isset($post['groupname']) && !$post['groupname']) redirect('membercp_usergroup_empty_name');
      //  if(!$edit && !$post['groupname']) redirect('membercp_usergroup_empty_name');
       // if(!$edit && !in_array($post['grouptype'],array('member','special','system'))) redirect('membercp_usergroup_empty_type');
      //  if(!$edit && $post['grouptype']=='member' && !is_numeric($post['point'])) redirect('membercp_usergroup_empty_point');
    }
}
?>