-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·114 lines (95 loc) · 3.59 KB
/
index.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
<?php
define('ROOT', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
define("TOKEN", "leaf");
ValidUtils::valid();
require_once(ROOT.DS."Teacher.php");
// 接收并且回复信息
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)) {
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$content = $postObj->Content;
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$msgType = "text";
$contentStr = "";
$arr = HTMLCatcher::getTeacherMsg($content);
foreach ($arr as $teacher) {
$contentStr =$contentStr
."姓名:".$teacher->name."\n\r"
."办公室:". $teacher->office."\n\r"
."电话:".$teacher->phone."\n\r"
."职位:".$teacher->position."\n\r"
."邮箱:".$teacher->email."\n\r\n\r";
}
if (empty($contentStr)) {
$contentStr = "找不到该老师";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
// 抓取信息
class HTMLCatcher {
public static function getTeacherMsg($keyWord) {
$url = "http://info.scau.edu.cn/nav-contact.asp";
$htmlDoc = new DOMDocument;
$htmlDoc->loadHTMLFile($url);
$htmlDoc->normalizeDocument();
$tables_list = $htmlDoc->getElementsByTagName('table');
$arr = Array();
$table = $tables_list->item(0);
$rows_list = $table->getElementsByTagName('tr');
foreach ($rows_list as $row) {
$teacher = Teacher::parse($row);
if(!is_null($teacher)) {
//$teacherName = $_GET['name'];
$teacherName = $keyWord;
if (is_null($teacherName)) {
array_push($arr, $teacher);
}
else {
if ($teacher->name == $teacherName) {
array_push($arr, $teacher);
break;
}
}
}
}
return $arr;
}
}
// 链接微信
class ValidUtils {
public static function valid() {
$echoStr = $_GET["echostr"];
//valid signature , option
if(!is_null($echoStr)&&checkSignature()){
echo $echoStr;
exit;
}
}
private static function checkSignature() {
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>