<?php
namespace plugin\sms;

class Sms extends \app\index\behavior\Plugin{

    public $info = array(
        'name'=>'sms',
        'title'=>'短信插件',
        'description'=>'统计sms的基础信息',
        'status'=>1,
        'author'=>'bymall',
        'version'=>'0.1',
		'admin'=>'0',
		'index'=>'0',
		'ids'=>''
    );
  public function sd()
    {
		 return $this->fetch('info');
        //echo 'demo hook!page issasa ' ;
    }
    public function install(){
        return true;
    }

    public function uninstall(){
        return true;
    }
 /**
     * 实现demo钩子
     * @param array $params
     */
    public function send($params = [])
    {
		
   
	if(!$params){
		die('手机号不存在');	
		
	}  
	$config=$this->getConfig();
	
	  $appid= $config['appid']; 
      //应用的APPKEY 
     $secret = $config['appSecret'];   
	 $length= $config['length']; 
	if(!$appid||!$secret||!(int)$length){		       
		$res["msg"]='插件配置不能为空';
		$res["code"]=0;	
		header('Content-Type:application/json; charset=utf-8');
	    print_r( json_encode($res,JSON_UNESCAPED_UNICODE));
		exit();		 
	 }   
	$mobile=safe_replace($params['mobile']);
	$map["mobile"]=$mobile;	
	$target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
	$length=$length?$length:4;
	$mobile_code =$this->random($length,1);//获取验证码		
    //$u=api('HUYIUSERNAME');
	//$p=api('HUYIPASSWORD');
	$post_data = "account={$appid}&password={$secret}&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$mobile_code."。请不要把验证码泄露给其他人。");
	//密码可以使用明文密码或使用32位MD5加密
	$gets = $this->xml_to_array($this->httpPost($post_data, $target));
	if($gets&&$gets['SubmitResult']['code']==2){		
		$_SESSION['mobile'] = $mobile;
		$_SESSION['mobile_code'] = $mobile_code;
		
		//发送成功加入记录
		 $data["content"]=$mobile_code;//验证码
	     $data["mobile"]=$mobile;//验证码
	     $data["create_time"]=time();//验证码
	     db("sms")->insert($data);	
		 
		$res["msg"]=$gets['SubmitResult']['msg'];
		$res["code"]=1;	
		header('Content-Type:application/json; charset=utf-8');
	    print_r( json_encode($res,JSON_UNESCAPED_UNICODE));
		exit();
	}else{
		$res["msg"]=$gets?$gets['SubmitResult']['msg']:'未连接网络';
		$res["code"]=0;	
		header('Content-Type:application/json; charset=utf-8');
	    print_r( json_encode($res,JSON_UNESCAPED_UNICODE));
		exit();
    }
      
    }
function httpPost($curlPost,$url){
	    $url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_NOBODY, true);
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
		$return_str = curl_exec($curl);
		curl_close($curl);
		return $return_str;
}
function random($length = 6 , $numeric = 0) {
	
	PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
	if($numeric) {
		$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
	} else {
		$hash = '';
		$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++) {
			$hash .= $chars[mt_rand(0, $max)];
		}
	}
	return $hash;
}
 function xml_to_array($xml){
		$arr=array();
		$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
		if(preg_match_all($reg, $xml, $matches)){
			$count = count($matches[0]);
			for($i = 0; $i < $count; $i++){
			$subxml= $matches[2][$i];
			$key = $matches[1][$i];
				if(preg_match( $reg, $subxml )){
					$arr[$key] = xml_to_array( $subxml );
				}else{
					$arr[$key] = $subxml;
				}
			}
		}
		return $arr;
   }
}