1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: 18: 19: 20: 21: 22:
23: function wx_parse_xml($xml){
24: $array = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
25: return (Array)$array;
26: }
27:
28: 29: 30: 31: 32:
33: function wx_random_big_pic(){
34: return WEIXIN_ROBOT_URL.'/assets/img/640_320/'.mt_rand(1,5).'.jpg';
35: }
36:
37: 38: 39: 40: 41:
42: function wx_random_small_pic(){
43: return WEIXIN_ROBOT_URL.'/assets/img/80_80/'.mt_rand(1,10).'.jpg';
44: }
45:
46:
47: 48: 49: 50: 51: 52:
53: function wx_is_xml($xml){
54: if(substr($xml, 0, strlen('<xml>')) == '<xml>'){
55: return true;
56: }
57: return false;
58: }
59:
60: 61: 62: 63: 64:
65: function wx_request_xml(){
66:
67: if(!empty($GLOBALS["HTTP_RAW_POST_DATA"])){
68: return $GLOBALS["HTTP_RAW_POST_DATA"];
69: }else{
70: return file_get_contents('php://input');
71: }
72: }
73:
74:
75: 76: 77: 78: 79:
80: function wx_request_is_encode(){
81: $result_xml = wx_request_xml();
82: $result = wx_parse_xml($result_xml);
83: if(isset($result['Encrypt'])){
84: return true;
85: }
86: return false;
87: }
88:
89:
90: 91: 92: 93: 94:
95: function wx_request_array(){
96:
97: $options = get_option(WEIXIN_ROBOT_OPTIONS);
98:
99:
100: if($options['weixin_robot_debug'] == 'true' && (isset($_GET['debug']) || $_GET['debug'] == '1')){
101: $info['MsgType'] = 'text';
102: $info['FromUserName'] = 'userid';
103: $info['ToUserName'] = 'openid';
104: $info['CreateTime'] = time();
105: $info['Content'] = (isset($_GET['kw']))?$_GET['kw']:'?';
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116: return $info;
117: }
118:
119: $result_xml = wx_request_xml();
120:
121: if(!wx_is_xml($result_xml)){
122: return false;
123: }
124:
125:
126: if(!empty($result_xml)){
127: $result = wx_parse_xml($result_xml);
128: if(isset($result['Encrypt'])){
129: $result = wx_request_decode($result_xml);
130: return wx_parse_xml($result);
131: }
132: return $result;
133: }
134: return false;
135: }
136:
137:
138: 139: 140: 141: 142: 143:
144: function wx_send_encode($text){
145: $token = WEIXIN_TOKEN;
146: $options = get_option(WEIXIN_ROBOT_OPTIONS);
147:
148: $encodingAesKey = $options['EncodingAESKey'];
149: $appId = $options['ai'];
150: $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
151:
152: $timeStamp = time();
153: $encryptMsg = '';
154: $nonce = $_GET['nonce'];
155: $retCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
156: if($retCode == 0){
157: return $encryptMsg;
158: }
159: return $text;
160: }
161:
162: 163: 164: 165: 166: 167:
168: function wx_request_decode($text){
169: $token = WEIXIN_TOKEN;
170: $options = get_option(WEIXIN_ROBOT_OPTIONS);
171: $info = wx_parse_xml($text);
172:
173: $encodingAesKey = $options['EncodingAESKey'];
174: $appId = $options['ai'];
175: $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
176:
177: $timeStamp = time();
178: $nonce = $_GET['nonce'];
179:
180: $sha1 = new SHA1;
181: $array = $sha1->getSHA1($token, $timeStamp, $nonce, $info['Encrypt']);
182: $ret = $array[0];
183: if ($ret != 0) {
184: return $ret;
185: }
186: $msg_sign = $array[1];
187:
188: $retCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $text, $msg);
189: if($retCode == 0){
190: return $msg;
191: }
192: return $text;
193: }
194:
195: ?>
196: