<?php
class weixinControl extends skymvc{
	public $weixin;
	public function __construct(){
		parent::__construct();
		$this->loadModel(array("weixin","weixin_command"));
		$this->weixin=$this->weixin->selectRow(array("order"=>"id DESC"));
		define("WXTOKEN",$this->weixin['token']);
	}
	
	
	public function onDefault(){
 		$this->responseMsg();
	}
	
	public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;
        }
    }
	
	public function post(){
		$textTpl="<xml>
 <ToUserName><![CDATA[%s]]></ToUserName>
 <FromUserName><![CDATA[%s]]></FromUserName> 
 <CreateTime>%s</CreateTime>
 <MsgType><![CDATA[%s]]></MsgType>
 <Content><![CDATA[%s]]></Content>
 <MsgId>0</MsgId>
 </xml>";
	}

    public function responseMsg()
    {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		file_put_contents("log.txt",$postStr);
      	//extract post data
		if (!empty($postStr)){
                
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
				$MsgId=$postObj->MsgId;
				$MsgType=$postObj->MsgType;
				file_put_contents("log.txt",$postStr."$fromUsername $toUsername $keyword $MsgId $MsgType");
			 
                $time = time();
                $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>";             
				if(!empty( $keyword ))
                {
              		$msgType = "text";
                	$contentStr = $this->getContent($keyword);
			
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
					//file_put_contents("log.txt",$resultStr);
                	echo $resultStr;
                }else{
					$row=$this->weixin_command->selectRow(array("where"=>" type_id=8 "));
                	echo $row['content'];
                }

        }else {
        	echo "";
        	exit;
        }
    }
	
	public function getContent($keyword){
		$s=explode(":",$keyword);
		$row=$this->weixin_command->selectRow(array("where"=>" command='".$s[0]."' "));
		switch($row['type_id']){
			case 1:
					return $row['content'];
				break;
			default:
					$row=$this->weixin_command->selectRow(array("where"=>" type_id=7 "));
					return $row['content'];
				break;
		}
		 
		
	}
		
	private function checkSignature()
	{
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        		
		$token = WXTOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
	
}
?>