1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: class WxRobot_Robot{
12:
13: 14: 15:
16: public static $_instance = null;
17:
18: 19: 20:
21: public function __construct(){
22: include_once('class-wx-functions.php');
23: include_once('class-wx-cmd.php');
24: }
25:
26: 27: 28: 29: 30:
31: public static function instance(){
32: if( is_null (self::$_instance)){
33: self::$_instance = new self();
34: }
35: return self::$_instance;
36: }
37:
38: 39: 40: 41: 42:
43: public function valid(){
44: if(isset($_GET['debug'])){
45: header('Content-type: text/html;charset=utf-8');
46: $this->responseMsg();
47: }else{
48: if($this->checkSignature()){
49: $echoStr = (isset($_GET['echostr']))?$_GET['echostr']:'';
50: if(!empty($echoStr)){
51: echo $echoStr;
52: }else{
53: $this->responseMsg();
54: }
55: }else{
56:
57: $this->responseMsg();
58: }
59: }
60: }
61:
62: 63: 64: 65: 66:
67: private function checkSignature(){
68: $signature = $_GET['signature'];
69: $timestamp = $_GET['timestamp'];
70: $nonce = $_GET['nonce'];
71:
72: $token = WEIXIN_TOKEN;
73: $tmpArr = array($token, $timestamp, $nonce);
74: sort($tmpArr, SORT_STRING);
75: $tmpStr = implode( $tmpArr );
76: $tmpStr = sha1( $tmpStr );
77:
78: if($tmpStr == $signature){
79: return true;
80: }else{
81: return false;
82: }
83: }
84:
85: 86: 87: 88: 89:
90: public function responseMsg(){
91: echo WxRobot_Cmd::instance()->cmd();exit;
92: }
93:
94: }
95: ?>
96: