<?php

function sendsms1($mobile, $content,$type="sms"){

	$log = date('Y-m-d H:i:s',time())." | ".real_ip()." | ".$mobile." | ".$content."\r\n";
	file_put_contents(dirname(dirname(__FILE__))."/temp/sms_log_".$GLOBALS['_CFG']['ihuyi_sms_user_name'].".log",$log,FILE_APPEND);
	$content=urlencode($content);
	//配置信息
	if($type=="sms"){
		$target = "http://api.message.net.tw/send.php?";	
		$post_data = "mtype=G&encoding=utf8&id=".$GLOBALS['_CFG']['ihuyi_sms_user_name']."&password=".$GLOBALS['_CFG']['ihuyi_sms_pass_word']."&tel=".$mobile."&msg=".$content;
		$target.=$post_data;
	}else{
		$target = "http://api.message.net.tw/send.php?mtype=G";	
		$post_data = "id=".$GLOBALS['_CFG']['ihuyi_sms_user_name']."&password=".md5($GLOBALS['_CFG']['ihuyi_sms_pass_word'])."&tel=".$mobile."&msg=".urlencode($content);
	}
	
	$gets = '<?xml version="1.0" encoding="utf-8"?>
	<SubmitResult xmlns="http://106.ihuyi.cn/">
	<code>0</code>
	<msg>提交成功</msg>
	<smsid>67472311</smsid>
	</SubmitResult>';


//	if($GLOBALS['_CFG']['ihuyi_sms_debug']==0){
		$gets = ihuyi_Post($post_data,$target);
//	}	
	$t_gets_arr=preg_split('/\R/', $gets);

	foreach($t_gets_arr as $value){
		if(strlen(trim($value))>0){
			$t_return=explode('=',$value);
			switch($t_return[0]){
				case 'ErrorCode':
					$gets_arr['code']=$t_return[1];			
				break;
				case 'LCount':
					$gets_arr['msg']=$t_return[1];			
				break;
				case 'MsgID000':
					$gets_arr['smsid']=$t_return[1];			
				break;			
			}
		}
	}
	//$gets_arr = xml_to_array($gets);
	
	file_put_contents(dirname(dirname(__FILE__))."/temp/sms_log_".$GLOBALS['_CFG']['ihuyi_sms_user_name'].".log",'发送状态：'.$gets_arr['msg']."\r\n",FILE_APPEND);
	
	if ($gets_arr['code'] == 0){
		return true;
	}else{
		return $gets_arr['code']; 
	}
}

function sendsms($mobile, $content,$type="sms"){

	include(ROOT_PATH . 'vendor/autoload.php');

	$log = date('Y-m-d H:i:s',time())." | ".real_ip()." | ".$mobile." | ".$content."\r\n";
	file_put_contents(dirname(dirname(__FILE__))."/temp/sms2_log_".$GLOBALS['_CFG']['ihuyi_sms_user_name'].".log",$log,FILE_APPEND);
	$content=$content;

	$basic  = new \Nexmo\Client\Credentials\Basic('21920a29', '7l2byV8Pp5ZLb4L3');
	$client = new \Nexmo\Client($basic);

	$message = $client->message()->send([
	    'to' => '886'.substr($mobile,1),
	    'from' => $GLOBALS['_CFG']['ihuyi_sms_user_name'],
	    'text' => $content,
	    'type' => 'unicode'
	]);

 	$response = $message->getResponseData();

    if($response['messages'][0]['status'] == 0) {
        $result= "The message was sent successfully\n";
    } else {
        $result= "The message failed with status: " . $response['messages'][0]['status'] . "\n";
    }
	
	file_put_contents(dirname(dirname(__FILE__))."/temp/sms_log_".$GLOBALS['_CFG']['ihuyi_sms_user_name'].".log",'发送状态：'.$result."\r\n",FILE_APPEND);
	
//	if ($gets_arr['code'] == 0){
		return true;
//	}else{
//		return $gets_arr['code']; 
//	}
}

function sendline($content){

//	$log = date('Y-m-d H:i:s',time())." | ".real_ip()." | ".$mobile." | ".$content."\r\n";
//	file_put_contents(dirname(dirname(__FILE__))."/temp/line_log.log",$log,FILE_APPEND);
	


	$token = $GLOBALS['_CFG']['asc_line_token'];
	
	$message = $content;
	$query = http_build_query(['message' => $message]);
	$header = [
	        'Content-Type: application/x-www-form-urlencoded',
	        'Authorization: Bearer ' . $token,
	        'Content-Length: ' . strlen($query)
	];
	
	$ch = curl_init('https://notify-api.line.me/api/notify');
	$options = [
	    CURLOPT_RETURNTRANSFER  => true,
	    CURLOPT_POST            => true,
	    CURLOPT_HTTPHEADER      => $header,
	    CURLOPT_POSTFIELDS      => $query
	];
	
	curl_setopt_array($ch, $options);
	$result=curl_exec($ch);
	curl_close($ch);
//	print_r($result);
//	file_put_contents(dirname(dirname(__FILE__))."/temp/sms_log_".$GLOBALS['_CFG']['ihuyi_sms_user_name'].".log",'发送状态：'.$gets_arr['msg']."\r\n",FILE_APPEND);
	
	return true;
}


function ismobile($mobile){
//	return true;
	return (strlen($mobile) == 10 || strlen($mobile) == 10) && (preg_match("/^09\d{8}$/", $mobile));
}

function getverifycode() {
	$length = 6;
	PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
	$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
	return $hash;
}

function ihuyi_Post($curlPost,$url){
		$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, false);
//		curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
		$return_str = curl_exec($curl);
		curl_close($curl);
		return $return_str;
}

function xml_to_array($xml){
	$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;
}
?>
