forked from prinsss/blessing-skin-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.php
296 lines (281 loc) · 9.87 KB
/
ajax.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* @Author: printempw
* @Date: 2016-01-16 23:01:33
* @Last Modified by: printempw
* @Last Modified time: 2016-04-03 10:16:00
*
* - login, register, logout
* - upload, change, delete
*
* All ajax requests will be handled here
*/
session_start();
header('Access-Control-Allow-Origin: *');
header('Content-type: application/json');
$dir = dirname(__FILE__);
require "$dir/libraries/autoloader.php";
Database\Database::checkConfig();
if (isset($_POST['uname'])) {
$uname = $_POST['uname'];
if (User::checkValidUname($uname)) {
$user = new User($_POST['uname']);
} else {
throw new E('无效的用户名。用户名只能包含数字,字母以及下划线。', 3);
}
} else {
throw new E('空用户名。', 3);
}
$action = isset($_GET['action']) ? $_GET['action'] : null;
$json = null;
/**
* Handle requests from index.php
*/
if ($action == "login") {
if (checkPost()) {
if (!$user->is_registered) {
$json['errno'] = 2;
$json['msg'] = "用户不存在哦";
} else {
if ($user->checkPasswd($_POST['passwd'])) {
$json['errno'] = 0;
$json['msg'] = '登录成功,欢迎回来~';
$json['token'] = $user->getToken();
$_SESSION['token'] = $user->getToken();
} else {
$json['errno'] = 1;
$json['msg'] = "用户名或密码不对哦";
}
}
}
} else if ($action == "register") {
if (checkPost('register')) {
if (!$user->is_registered) {
if (Option::get('user_can_register') == 1) {
if (User::checkValidPwd($_POST['passwd'])) {
// If amount of registered accounts of IP is more than allowed mounts,
// then reject the registration.
if ($user->db->getNumRows('ip', getRealIP()) < Option::get('regs_per_ip')) {
// use once md5 to encrypt password
if ($user->register($_POST['passwd'], getRealIP())) {
$json['errno'] = 0;
$json['msg'] = "注册成功~";
}
} else {
$json['errno'] = 7;
$json['msg'] = "你最多只能注册 ".Option::get('regs_per_ip')." 个账户哦";
}
}
} else {
$json['errno'] = 7;
$json['msg'] = "残念。。本皮肤站已经关闭注册咯 QAQ";
}
} else {
$json['errno'] = 5;
$json['msg'] = "这个用户名已经被人注册辣,换一个吧";
}
}
}
function getRealIP() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function checkPost() {
global $json;
if (!isset($_POST['passwd'])) {
$json['errno'] = 2;
$json['msg'] = "空密码。";
return false;
}
return true;
}
/**
* Handle request from user/index.php
*/
if ($action == "upload") {
if (Utils::getValue('token', $_SESSION) == $user->getToken()) {
if (checkFile()) {
if ($file = Utils::getValue('skin_file', $_FILES)) {
$model = (isset($_GET['model']) && $_GET['model'] == "steve") ? "steve" : "alex";
if ($user->setTexture($model, $file)) {
$json['skin']['errno'] = 0;
$json['skin']['msg'] = "皮肤上传成功!";
}
}
if ($file = Utils::getValue('cape_file', $_FILES)) {
if ($user->setTexture('cape', $file)) {
$json['cape']['errno'] = 0;
$json['cape']['msg'] = "披风上传成功!";
}
}
}
} else {
$json['errno'] = 1;
$json['msg'] = "无效的 token,请先登录。";
}
} else if ($action == "model") {
if (Utils::getValue('token', $_SESSION) == $user->getToken()) {
$new_model = ($user->getPreference() == "default") ? "slim" : "default";
$user->setPreference($new_model);
$json['errno'] = 0;
$json['msg'] = "优先模型已经更改为 ".$user->getPreference()."。";
} else {
$json['errno'] = 1;
$json['msg'] = "无效的 token,请先登录。";
}
}
function checkFile() {
global $json;
if (!(Utils::getValue('skin_file', $_FILES) || Utils::getValue('cape_file', $_FILES))) {
$json['errno'] = 1;
$json['msg'] = "什么文件都没有诶?";
return false;
}
/**
* Check for skin_file
*/
if (isset($_FILES['skin_file']) && ($_FILES['skin_file']['type'] == "image/png" ||
$_FILES['skin_file']['type'] == "image/x-png"))
{
// if error occured while uploading file
if ($_FILES['skin_file']["error"] > 0) {
$json['skin']['errno'] = 1;
$json['skin']['msg'] = $_FILES['skin_file']["error"];
return false;
}
if ($_FILES['skin_file']['size'] > (Option::get('upload_max_size')) * 1024) {
$json['skin']['errno'] = 1;
$json['skin']['msg'] = "本站最大只允许上传 ".Option::get('upload_max_size')." KB 的材质。";
return false;
}
$size = getimagesize($_FILES['skin_file']["tmp_name"]);
$ratio = $size[0] / $size[1];
if ($ratio != 2 && $ratio != 1) {
$json['skin']['errno'] = 1;
$json['skin']['msg'] = "不是有效的皮肤文件(宽 {$size[0]},高 {$size[1]})";
return false;
}
} else {
if (Utils::getValue('skin_file', $_FILES)) {
$json['skin']['errno'] = 1;
$json['skin']['msg'] = '错误的皮肤文件类型。';
return false;
} else {
$json['skin']['errno'] = 0;
$json['skin']['msg'] = '什么文件都没有诶?';
}
}
/**
* Check for cape_file
*/
if (isset($_FILES['cape_file']) && ($_FILES['cape_file']['type'] == "image/png" ||
$_FILES['cape_file']['type'] == "image/x-png"))
{
// if error occured while uploading file
if ($_FILES['cape_file']["error"] > 0) {
$json['cape']['errno'] = 1;
$json['cape']['msg'] = $_FILES['cape_file']["error"];
return false;
}
if ($_FILES['cape_file']['size'] > (Option::get('upload_max_size')) * 1024) {
$json['cape']['errno'] = 1;
$json['cape']['msg'] = "本站最大只允许上传 ".(Option::get('upload_max_size') * 1024)." KB 的材质。";
return false;
}
$size = getimagesize($_FILES['cape_file']["tmp_name"]);
$ratio = $size[0] / $size[1];
if ($ratio != 2) {
$json['cape']['errno'] = 1;
$json['cape']['msg'] = "不是有效的披风文件(宽 {$size[0]},高 {$size[1]})";
return false;
}
} else {
if (Utils::getValue('cape_file', $_FILES)) {
$json['cape']['errno'] = 1;
$json['cape']['msg'] = '错误的披风文件类型。';
return false;
} else {
$json['cape']['errno'] = 0;
$json['cape']['msg'] = '什么文件都没有诶?';
}
}
return true;
}
/**
* Handle requests from user/profile.php
*/
if ($action == "change") {
if (checkPost()) {
if (isset($_POST['new_passwd'])) {
if ($user->checkPasswd($_POST['passwd'])) {
$user->changePasswd($_POST['new_passwd']);
$json['errno'] = 0;
$json['msg'] = "密码更改成功。请重新登录。";
} else {
$json['errno'] = 2;
$json['msg'] = "原密码不对哦?";
}
} else {
$json['errno'] = 1;
$json['msg'] = "新密码呢?";
}
}
} else if ($action == "delete") {
if (isset($_SESSION['token']) && $_SESSION['token'] == $user->getToken()) {
if (checkPost()) {
if (!$user->is_admin) {
if ($user->checkPasswd($_POST['passwd'])) {
session_destroy();
$user->unRegister();
$json['errno'] = 0;
$json['msg'] = "账号已经成功删除,再见~";
} else {
$json['errno'] = 2;
$json['msg'] = "错误的密码。";
}
} else {
$json['errno'] = 1;
$json['msg'] = "管理员账号不能被删除哟~";
}
}
} else {
$json['errno'] = 1;
$json['msg'] = "无效的 token,请先登录。";
}
} else if ($action == "reset") {
if (isset($_SESSION['token']) && $_SESSION['token'] == $user->getToken()) {
if (checkPost()) {
if ($user->checkPasswd($_POST['passwd'])) {
$user->reset();
$json['errno'] = 0;
$json['msg'] = "重置成功。";
} else {
$json['errno'] = 2;
$json['msg'] = "错误的密码。";
}
}
} else {
$json['errno'] = 1;
$json['msg'] = "无效的 token,请先登录。";
}
} else if ($action == "logout") {
if (Utils::getValue('token', $_SESSION)) {
session_destroy();
$json['errno'] = 0;
$json['msg'] = '成功登出 | ゚ ∀゚)ノ';
} else {
$json['errno'] = 1;
$json['msg'] = '并没有任何有效的 session。';
}
}
if (!$action) {
$json['errno'] = 6;
$json['msg'] = "无效的参数。不要乱 POST 玩哦。";
}
echo json_encode($json);