-
Notifications
You must be signed in to change notification settings - Fork 0
/
wxmsg.php
105 lines (89 loc) · 1.91 KB
/
wxmsg.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
<?php
require_once("xic.php");
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)
{
if (substr_compare($k, '__x4fcgi_', 0, 9) == 0)
{
continue;
}
xmlwriter_start_element($xw, $k);
xmlwriter_text($xw, $v);
xmlwriter_end_element($xw);
}
xmlwriter_end_element($xw);
return xmlwriter_output_memory($xw);
}
function main()
{
$timestamp = @$_GET['timestamp'];
$nonce = @$_GET['nonce'];
$signature = @$_GET['signature'];
if ($signature == '' || $nonce == '' || $timestamp == '')
{
dlog("", "AUTH", "NO_PARAMS");
print "invalid request\n";
exit();
}
$diff = intval($timestamp) - time();
if ($diff < -30 || $diff > 30)
{
dlog("", "AUTH", "INVALID_TIMESTAMP");
print "invalid timestamp\n";
exit();
}
$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)
{
dlog("", "AUTH", "REPLAY_ATTACK");
print "replay attack?\n";
exit();
}
$echostr = @$_GET['echostr'];
if ($echostr != "")
{
dlog("", "CONF", "SETUP");
print $echostr;
exit();
}
// Handle the msg
$input_fp = fopen("php://input", "rb");
$input_bytes = stream_get_contents($input_fp);
fclose($input_fp);
dlog("", "MSG", $input_bytes);
$xml = simplexml_load_string($input_bytes);
if ($xml === FALSE)
{
print "invalid xml\n";
exit();
}
$msg = xml2array($xml);
$prx = xic_createProxy("WxMsg");
$answer = $prx->invoke("msg", array("auth"=>$_GET, "msg"=>$msg));
$output_bytes = array2xml($answer['msg']);
dlog("", "OUT", $output_bytes);
print $output_bytes;
}
try
{
main();
}
catch (Throwable $ex)
{
dlog("", "EXCEPTION", $ex);
print "exception occurs\n";
}