-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWxServant.php
144 lines (126 loc) · 3.04 KB
/
WxServant.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace wx;
require_once("xic.php");
require_once("x4fcgi.php");
require_once("wxmsgcrypt.php");
$wxkeyfile = "/xio/private/wxmsgkeys.json";
function xml2array($xml)
{
$arr = array();
foreach ($xml->children() as $k => $v)
{
$arr[$k] = strval($v);
}
return $arr;
}
function array2xml($arr)
{
$xw = xmlwriter_open_memory();
xmlwriter_start_element($xw, 'xml');
foreach ($arr as $k => $v)
{
xmlwriter_start_element($xw, $k);
xmlwriter_text($xw, $v);
xmlwriter_end_element($xw);
}
xmlwriter_end_element($xw);
return xmlwriter_output_memory($xw);
}
class WxServant extends \xic_Servant
{
protected function _xic_msg($quest)
{
global $wxkeyfile;
$keys = json_decode(file_get_contents($wxkeyfile), TRUE);
$appId = $keys["appId"];
$token = $keys["token"];
$cryptKey = $keys["cryptKey"];
$cryptor = new \WxMsgCrypt($appId, $token, $cryptKey);
$args = $quest->args;
$auth = $args['auth'];
$timestamp = $auth['timestamp'];
$nonce = $auth['nonce'];
$signature = $auth['signature'];
$lcache = \xic_createProxy("LCache");
$tnkey = $timestamp . '+' . $nonce . '+' . $signature;
$answer = $lcache->invoke("get_and_set", array('key'=>$tnkey, 'value'=>1, 'maxage'=>60));
if ($answer['value'] != NULL)
{
throw new Exception("Replay Attack?");
}
$arr = array($token, $timestamp, $nonce);
sort($arr, SORT_STRING);
$hash = sha1(implode('', $arr));
if ($hash != $signature)
{
throw new Exception("Auth failed");
}
$msg = $args['msg'];
$encrypt_type = $auth['encrypt_type'];
if ($encrypt_type == 'aes')
{
$cryptor->decryptIncoming($timestamp, $nonce, $msg['Encrypt'], $auth['msg_signature'], $plain);
$xml = simplexml_load_string($plain);
$msg = xml2array($xml);
}
dlog("", "RECV", $msg);
$msgType = $msg["MsgType"];
$text = "OK";
if ($msgType == 'event')
{
$event = $msg['Event'];
$eventKey = $msg['EventKey'];
if ($event == 'subscribe')
{
$text = "Welcome";
}
else if ($event == 'unsubscribe')
{
$text = "Bye bye";
}
}
else
{
$msgId = $msg["MsgId"];
if ($msgType == 'text')
{
$content = $msg["Content"];
$text = $content;
}
else if ($msgType == 'image')
{
$picUrl = $msg["PicUrl"];
$mediaId = $msg["MediaId"];
}
else if ($msgType == 'file')
{
$title = $msg["Title"];
$description = $msg["Description"];
$fileKey = $msg["FileKey"];
$fileMd5 = $msg["FileMd5"];
$fileLen = $msg["FileTotalLen"];
}
}
$reply = array(
"FromUserName"=>$msg["ToUserName"],
"ToUserName"=>$msg["FromUserName"],
"MsgType"=>"text",
"Content"=>$text,
"CreateTime"=>time(),
);
dlog("", "SEND", $reply);
if ($encrypt_type == 'aes')
{
$plain = array2xml($reply);
$o_timestamp = time();
$o_nonce = mt_rand();
$cryptor->encryptOutgoing($o_timestamp, $o_nonce, $plain, $o_cipher, $o_sig);
$reply = array("TimeStamp"=>$o_timestamp,
"Nonce"=>$o_nonce,
"Encrypt"=>$o_cipher,
"MsgSignature"=>$o_sig,
);
}
return array("msg"=>$reply);
}
}