<?php

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

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

class tj_sendsms_sunnyh 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'];

        $http = 'http://121.10.141.28:8090/cgi-bin/sendsms';
		$data = array
		(
		'username'=>$post['uid'],							//用户账号
		'password'=>strtolower($post['pwd']),			//MD5位32密码
		'to'=>str_replace(',',' ',$post['mobile']),						//号码
		'text'=>$post['content'],			//内容
		'msgtype'=>4,							//定时发送
		'charset'=>1,								//字符编码
		'dtime'=>$post['time']										//子扩展号
		);
		$re= $this->getSMS($http,$data);				//POST方式提交
		
		if( trim($re) == '0' )
		{
		return 100;
		}
		else 
		{
			switch ($re){
				
			case "-1":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "不是GET方式！";
				outputJson($json);
			}
			Note('不是GET方式！');
			break;

			case "-2":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "内容超长！";
				outputJson($json);
			}
			Note('内容超长！');
			break;
			
			case "-3":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "手机号码可能为空！";
				outputJson($json);
			}
			Note('手机号码可能为空！');
			break;
			
			case "-4":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "手机号码可能存在问题！";
				outputJson($json);
			}
			Note("手机号码可能存在问题！");
			break;
			
			case "-5":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "帐号和密码参数有问题！";
				outputJson($json);
			}
			Note('帐号和密码参数有问题！');
			break;
			
			case "-6":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "余额不足！";
				outputJson($json);
			}
			Note('余额不足！');
			break;
			
			case "-7":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "发送内容有问题！";
				outputJson($json);
			}
			Note('发送内容有问题！');
			break;
			
			case "-8":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "号码有问题！";
				outputJson($json);
			}
			Note('号码有问题！');
			break;
			
			case "-9":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "号码超出100个！";
				outputJson($json);
			}
			Note('号码超出100个！');
			break;
			
			case "-10":
			if($ajax){
				$json['status'] = 0;
				$json['msg'] = "平台数据入库有问题！";
				outputJson($json);
			}
			Note('平台数据入库有问题！');
			break;
			
			case "-11":
			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;
			
			default:
  				if($ajax){
				$json['status'] = 0;
				$json['msg'] = "未知错误！";
				outputJson($json);
			}
			Note('未知错误！');
	
			
			}
		}
    }
	
	//获取剩余&已发短信条数
	function remainderSMS($post,$send=null) {
		
			return 0;
    }
	function getSMS($url,$data='')
	{
		
		while (list($k,$v) = each($data)) 
	{
		$post .= rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
	}
	$post = substr( $post , 0 , -1 );
	$sendurl = $url.'?'.$post;
	return file_get_contents($sendurl);
	}
	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');
    }
}
?>