<?php
/**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class SmsModel extends BaseModel{
	public $errString = '';
	public $msg='';
	function getInfo($id){
		if(!$id || !is_numeric($id)){return false;}
		$sql = "select * from `qk_sms` where id='$id'";
		$info = $this->db->getRow($sql);
			$info['productInfo'] = $this->db->getRow("select * from `qk_product` where id='".$info['productId']."'");
			$info['userInfo'] = $this->db->getRow("select loginName,id from `qk_user` where id='".$info['userId']."'");
			$info['apiInfo'] = $this->db->getRow("select name from `qk_api` where id='".$info['apiId']."'");
			$status = $this->db->getRow("select codeName,color from `qk_codemap` where id='".$info['iStatus']."'");
			$info['status'] = $status['codeName'];
			$info['colorstatus'] = '<font color="'.$status['color'].'">'.$info['status'].'</font>';

		return $info;
	}
	function del($id){
		if(!$id || !is_numeric($id)){return false;}
		$sql = "delete from `qk_sms` where id='$id'";
		if($this->db->query($sql)){
			return true;
		}
		return false;
	}

	function edit($param){
		return $this->query($param);
	}
	function add($param){
		return $this->query($param);
	}
	function query($param){
		$id = $param['id'];
		$act = $param['act'];
		$loginName = $param['loginName'];
		$productId = $param['productId'];
		$apiId = $param['apiId'];
		$login = $param['login'];
		$password = $param['password'];
		$intNum = $param['intNum'];
		$signature = $param['signature'];
		$starttime = $param['starttime'];
		$endtime = $param['endtime'];
		$iStatus = $param['iStatus'];
		if(!$starttime || ($starttime=strtotime($starttime))===false){$starttime=time();}
		if(!$endtime || ($endtime=strtotime($endtime))===false){$endtime=0;}
		settype($apiId,"integer");
		settype($productId,"integer");
		settype($iStatus,"integer");
		settype($intNum,"integer");
		load_class("user");
		if(($userId=UserModel::getId($loginName))==false){return false;}
		if($act=="edit" && $id && is_numeric($id)){
			$sql = "update `qk_sms` set ";
			$sqlEnd = " where id='$id'";
		}else if($act=="add"){
			$sql = "select count(id) from `qk_sms` where login='$login'";
			if($this->db->getOne($sql)){
				$this->errString = "短信管理账号重复";
				return false;
			}
			$sql = "insert into `qk_sms` set ";
			$sqlEnd = ",posttime=UNIX_TIMESTAMP()";
		}
		$sql .="productId='$productId',userId='$userId',apiId='$apiId',login='$login',password='$password',intNum='$intNum',signature='$signature',starttime='$starttime',endtime='$endtime',iStatus='$iStatus'";
		$sql .= $sqlEnd;
		if($this->db->query($sql)){
			return true;
		}
		return false;
	}
	function ajaxEdit($param){
		$id = $param['id'];
		if(!$id || !is_numeric($id)){$this->errString='缺少标识';return false;}
		$col = $param['col'];
		$val = $param['val'];
		$oldval = $param['oldval'];
		$sql = "update `qk_sms` set `$col`='$val' where id='$id'";
		$this->db->query($sql);
		return true;
	}

	function delLog($id=0){
		if($id && is_numeric($id)){
			return $this->db->query("delete from `qk_sms_log` where id='$id'");
		}
		return false;
	}
	function sendMsg($mobile,$content,$userId=0){
		$smsApi = getConfig('smsApi');
		if(!$mobile || strlen($mobile)!=11 || !is_numeric($mobile)){$this->msg='手机号码为空或不为11位数字';return false;}
		if(!$content){$this->msg='短信内空为空';return false;}
		if(!$userId || !is_numeric($userId)){$userId=0;}
				if($smsApi['url'] && $smsApi['login'] && $smsApi['password']){
					$success = 'false';
					$http = new http($smsApi['url']);
					$param = "category=sms&action=send&mobile=".$mobile."&content=".$content."&login=".$smsApi['login']."&password=".$smsApi['password'];
					//saveLog($smsApi['url']."?".$param);	//尊敬的用户，您的校验码：773584【工作人员不会索取，请勿泄漏】
					$http->setPost($param);
					$http->createCurl();
					$return = $http->responseText();
					//saveLog($return);
					if(strpos($return,"<returncode>200</returncode>")){
						$this->msg='短信发送成功';
						return true;
					}else{
						$code = 400;
						$msg = '短信网关发送失败';
					}
				}else{
					$code = 400;
					$msg = '短信接口未启用';
				}
				$this->msg = $msg;
		return false;
	}
	function sendReg($mobile,$code,$userId=0){
		$smsApi = getConfig('smsApi');
		if(!$mobile || strlen($mobile)!=11 || !is_numeric($mobile)){$this->msg='手机号码为空或不为11位数字';return false;}
		if(!$code){$this->msg='验证码不能为空';return false;}
		if(!$userId || !is_numeric($userId)){$userId=0;}
				if($smsApi['url'] && $smsApi['login'] && $smsApi['password']){
					$success = 'false';
					$http = new http($smsApi['url']);
					$param = "category=sms&action=send&mobile=".$mobile."&tid=".($smsApi['regTid']?$smsApi['regTid']:$smsApi['tid'])."&randcode=".$code."&login=".$smsApi['login']."&password=".$smsApi['password'];//echo $smsApi['url'];echo $param;exit;
					$http->setPost($param);
					$http->createCurl();
					$return = $http->responseText();
					saveLog($return);
					if(strpos($return,"<returncode>200</returncode>")){
						$this->msg='短信发送成功';
						return true;
					}else{
						$code = 400;
						$msg = '短信网关发送失败';
					}
				}else{
					$code = 400;
					$msg = '短信接口未启用';
				}
				$this->msg = $msg;
		return false;
	}
}
?>
