forked from riverstore/NexusPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc98logoncallback.php
258 lines (200 loc) · 5.66 KB
/
cc98logoncallback.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/*
* CC98 登录回调页面
*
* 版本:1.0
* 作者:樱桃
* 时间:2014-10-13
*
* 修订记录:
* 无
*
*/
require_once("include/bittorrent.php");
header("Content-Type: text/html; charset=utf-8");
dbconn();
// 资源对象。
$res = get_current_user_resource()['cc98_logon'];
// CC98 登录 APP 的客户端 ID 和机密。
global $cc98_client_id;
global $cc98_client_secret;
global $redirect_uri;
$code = $_GET['code'];
$state_str = $_GET['state'];
// 解密数据,如果解密失败则立即返回错误。
$state = unprotect_data($state_str);
if(!$state){
show_system_error();
}
// 参数检查。
if(empty($code)) {
show_system_error();
}
//// 发送到 Token 验证程序的字典。
// var postDic = new Dictionary<string, string>
// {
// {"client_id", Options.ClientId},
// {"client_secret", Options.ClientSecret},
// {"redirect_uri", RedirectUriBase},
// {"code", code},
// {"grant_type", "authorization_code"}
// };
// 请求主体
$body = MessageFormatter::formatMessage('', 'client_id={0}&client_secret={1}&code={2}&grant_type=authorization_code&redirect_uri={3}', array (urlencode($cc98_client_id), urlencode($cc98_client_secret), urlencode($code), urlencode($cc98_redirect_uri)));
// URL 请求对象。
$request = curl_init('https://login.cc98.org/OAuth/Token');
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_POSTFIELDS, $body);
// 执行 URL 并返回结果。
$data = json_decode(curl_exec($request), true);
// 关闭连接。
curl_close($request);
// 获得授权令牌和授权类型。
$access_token = $data['access_token'];
$token_type = $data['token_type'];
// 如果令牌获取失败,则结束进程。
if(!$access_token) {
show_system_error();
}
//////////// 根据访问令牌获得用户信息。
$request = curl_init("http://api.cc98.org/Me/Basic");
$headers = array(
// Authorization 标头
MessageFormatter::formatMessage('', 'Authorization: {0} {1}', array($token_type, $access_token)),
);
curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
// 执行并获得结果。
$data = json_decode(curl_exec($request), true);
// 关闭请求。
curl_close($request);
// CC98 ID。
$cc98_id = $data['name'];
// 如果无法获得 CC98 ID,则立即结束请求。
if(!$cc98_id) {
show_system_error();
}
// 判断操作并执行对应的工作。
switch($state['action']){
case "logon":
dologon($cc98_id);
break;
case "new":
case "edit":
doedit($state, $cc98_id);
break;
default:
break;
}
/**
* 显示操作结果。
*/
function show_result(){
}
/**
* 回到之前的页面地址。
* @param string $returnto 要返回的页面。
*/
function goback($returnto) {
$returnto = $returnto ? $returnto : "/";
header("Location: " . $returnto);
die();
}
/**
* 追加返回到指定 URL 的 Meta 元素。
* @param string $returnto 要返回的地址。
*/
function goback_meta($returnto) {
$returnto = $returnto ? $returnto : "/";
?>
<meta http-equiv="refresh" content="3; url=<?= $returnto ?>" />
<?php
}
/**
* 编辑相关操作
*/
function doedit($state, $username){
global $CURUSER;
global $res;
$user_lang = get_current_user_lang();
// 防伪 ID。
$xrsfid = $state['id'];
// 如果防伪 ID 变化则失败。
if($xrsfid != $CURUSER['id']) {
show_system_error();
}
// 检查重复
$sql = new_mysqli();
$query = $sql->prepare('SELECT count(*) AS `total` FROM `users` WHERE `cc98id` = ? AND `id` <> ?');
$query->bind_param("si", $username, $CURUSER['id']);
$query->execute();
$query->bind_result($count);
$query->fetch();
// 已经有账户关联。
if($count <> 0) {
stderr($res['msg_duplicated_association_title'], $res['msg_duplicated_association_text']);
die();
}
$sql->close();
// 执行数据库更新。
$sql = new_mysqli();
$query = $sql->prepare('UPDATE `users` SET `cc98id` = ? WHERE `id` = ?');
$query->bind_param("si", $username, $CURUSER['id']);
$query->execute();
$sql->close();
switch($state['action']){
case "new":
$title = $res['msg_create_association_success_title'];
$msg = MessageFormatter::formatMessage($user_lang, $res['msg_create_association_success_text'], array($username));
break;
case "edit":
$title = $res['msg_edit_association_success_title'];
$msg = MessageFormatter::formatMessage($user_lang, $res['msg_edit_association_success_text'], array($username));
break;
}
goback_meta($state['returnto']);
stdhead($title);
stdmsg($title, $msg);
stdfoot();
die();
}
/**
* 登录相关操作。
*/
function dologon($cc98_id) {
global $res;
$user_lang = get_current_user_lang();
$sql = new_mysqli();
$query = $sql->prepare('SELECT `id`, `passhash`, `username` FROM `users` WHERE `cc98id` = ?');
$query->bind_param('s', $cc98_id);
$query->execute();
$query->bind_result($id, $passhash, $username);
// 是否匹配到结果。
if ($query->fetch()){
logincookie($id, md5($passhash));
?>
<meta http-equiv="refresh" content="3; url=/" />
<?php
$title = $res['msg_logon_success_title'];
$msg = MessageFormatter::formatMessage($user_lang, $res['msg_logon_success_text'], array($username));
stdhead($title);
stdmsg($title, $msg);
stdfoot();
die();
break;
// 没有关联到账户
} else {
stderr($res['msg_no_associated_account_title'], $res['msg_no_associated_account_text']);
die();
}
}
/**
* 显示错误并终止执行。
*/
function show_system_error(){
global $res;
stderr($res['msg_system_error_title'], $res['msg_system_error_text']);
die();
}
?>