<?php
defined('IN_YUNYECMS') or exit('No permission.');
core::load_class('base',false);
class sendsms extends base {
	    function __construct() {
            parent::__construct();
        }
        //获取短信验证码
       public function getcode() {
		$mobile=$_POST['mobile'];
		$smstype=$_POST['smstype'];
        if(empty($mobile)||!is_numeric($mobile)||!check_mobile($mobile)){
           output_json(array("code"=>101,"msg"=>"手机号码错误！"));	
        }
		$yanzhengma=mt_rand(100000,999999);
        $str_smstype=get_smstype($smstype);
        if(empty($str_smstype)){
            output_json(array("code"=>102,"msg"=>"验证码类型不能为空！"));	
        }
		$_SESSION[$str_smstype]=$yanzhengma;
		$templateCode=get_sms_tplcode($smstype);
        if(empty($templateCode)){
            output_json(array("code"=>103,"msg"=>"短信模板不能为空！"));	
        }
		$templateParam=Array( // 短信模板中字段的值
          "code"=>$_SESSION[$str_smstype]
         );	
		 $retrundata= sendsms($templateCode,$mobile,$templateParam);
         if($retrundata['Code']=="OK"){
             output_json(array("code"=>100,"msg"=>"发送成功",$retrundata));	
         }else{
            output_json(array("code"=>199,"msg"=>"短信发送失败",$retrundata));	
         }
       }
       //获取邮件验证码
       public function getEmailCode() {
        $cfg=$this->cfg;
		$lang=$this->lang; 
		$email=$_POST['email'];
		$emailtype=$_POST['emailtype'];
        if(empty($email)||!is_email($email)){
           output_json(array("code"=>101,"msg"=>"邮箱地址输入错误！"));	
        }
		$yanzhengma=mt_rand(100000,999999);
        $str_smstype=get_smstype($emailtype); 
        if(empty($str_smstype)){
            output_json(array("code"=>102,"msg"=>"验证码类型不能为空！"));	
        }
        if(empty($str_smstype)){
            output_json(array("code"=>102,"msg"=>"获取邮件内容失败！"));	
        }
		 $_SESSION[$str_smstype]=$yanzhengma;
		 $msgContent=getEmailMsg($emailtype,$yanzhengma);
         $to=$email;
         $name= $cfg['sitename'];
         $subject=sprintf(lan($str_smstype.'_email_msg'),$cfg['email']['fromname']);
         $retrundata=send_mail($cfg['email'],$to, $name, $subject, $msgContent);	
         if($retrundata){
             output_json(array("code"=>100,"msg"=>"发送成功",$retrundata));	
         }else{
            output_json(array("code"=>199,"msg"=>"短信发送失败",$retrundata));	
         }
       }
}
?>