-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
228 lines (132 loc) · 5.03 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
require_once ('libs/autoloader.php');
require_once ('init.php');
// HTTPS json
// 所有軟體一開啟都要先傳送一次這串給BIP Server
// {"getToken":"1" ,"cardid":"34234341234134", "program":"actoma", "os":"ios", "polling": 0}
// 之後所傳送的資料格式
// {"cardid":"2392932992376589", "program":"slimeduet", "cmd":"modify_pin", "value":"123456", "token": "adf34fsfsfa"}
// 會得到的回傳格式
// {"result":"ok", "value":"a32323"}
// Content-Type: application/json
$funcs -> Content_type_check("application/json");
main();
function main(){
global $global;
if(!empty($global -> input)){
$jsonObj = json_decode($global -> input);
if(!empty($jsonObj)){
if(!redis_connect($global -> redis, $global -> conf['REDIS_SERVER'], $global -> conf['REDIS_PASSWD'])){
return false;
}
$apitoken = $global -> redis -> get($jsonObj->token);
//if($jsonObj->token == "KNVrtfNIkRk53TuLJriZrtS2WWMCp3yn5mAI89DHVZbx2vVMS25AY2aH4dndis9u"){
if(!empty($apitoken)){
if(!empty($jsonObj->cardid)){
if($jsonObj->getToken){
$token = $global -> funcs -> getRand_str(64);
if(info_handle($jsonObj, $token)){
$timestamp = time();
$reponse = "";
$index = $jsonObj->cardid."_".$jsonObj->program."_".$token."_reponse";
while(time() - $timestamp < 60){
$queue = $global -> redis -> get($index);
if(!empty($queue)){
echo '{"result":"ok", "token": "'.$token.'"}';
exit;
}
}
echo '{"result":"timeout"}';
} else {
echo '{"result":"error : token not set"}';
}
} else {
cmd_handle($jsonObj);
}
} else { echo '{"result":"cardid empty"}'; }
} else {
echo '{"result":"invalid token"}';
}
} else { echo '{"result":"wrong format"}'; }
}
}
function info_handle($jsonObj, $token){
global $global;
$index = $jsonObj->cardid."_".$jsonObj->program."_".$token;
$infoData = $jsonObj->os.",".$jsonObj->polling;
$global -> redis -> set($index, $infoData);
return true;
}
function cmd_handle($jsonObj){
global $global;
$rand = rand(10, 65535);
if(!redis_connect($global -> redis, $global -> conf['REDIS_SERVER'], $global -> conf['REDIS_PASSWD'])){
exit;
}
switch($jsonObj->cmd){
case "device_info": // 取得設備資訊
$index = $jsonObj->cardid."_".$jsonObj->program."_".$rand;
//echo "index : $index\n";
array_push($Instruction, $rand);
$global -> redis -> set($index, "device_info");
$global -> redis -> set($index."_waitting", "1");
$index_response = $jsonObj->cardid."_".$jsonObj->program."_".$rand."_response";
$timestamp = time();
$response_queue = "";
while(time() - $timestamp < 60){
$response_queue = $global -> redis -> get($index_response);
if(!empty($response_queue)){
break;
}
}
if(!empty($response_queue)){
$global -> redis -> del($index."_waitting");
$global -> redis -> del($index_response);
echo '{"result":"ok", "value":"'.$response_queue.'"}';
} else {
echo '{"result":"timeout", "value":""}';
}
break;
case "change_pin": // 變更 pin 碼
break;
case "verify_pin": // 校驗 pin 碼
break;
case "unlock_pin": // 解鎖 pin 碼
break;
case "create_doc": // 建立文件
break;
case "select_doc": // 選擇文件
break;
case "delete_doc": // 刪除文件
break;
case "doc_info": // 取得文件屬性
break;
case "get_doc_read_id": // 取得 讀取文件的 id
break;
case "read_doc": // 讀取文件內容
break;
case "get_doc_write_id": // 取得 寫文件的 id
break;
case "write_doc": // 將資料寫入文件
break;
case "get_rand": // 取得卡片隨機數
break;
case "sm4_encrypt": // SM4 加密
break;
case "create_key_pair": // 卡片產生密鑰對
break;
case "summary_d1": // 摘要 消息數據
break;
case "summary_d2": // 摘要 摘要數據
break;
case "rsa_d1": // RSA 待運算
break;
case "rsa_d2": // RSA 運算結果
break;
case "hash_set":
break;
case "":
break;
default:
}
}