chore(web): returnJSONData util

This commit is contained in:
Baoshuo Ren 2022-10-21 13:23:36 +08:00
parent e98978d6d8
commit c222f736fe
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
4 changed files with 18 additions and 22 deletions

View File

@ -19,13 +19,11 @@
$count = $_result["count(*)"];
function throwError($msg) {
die(json_encode(['status' => 'error', 'message' => $msg]));
returnJSONData(['status' => 'error', 'message' => $msg]);
}
$allowedTypes = [IMAGETYPE_PNG, IMAGETYPE_JPEG];
if ($_POST['image_upload_file_submit'] == 'submit') {
header('Content-Type: application/json');
if (!crsf_check()) {
throwError('expired');
}
@ -68,7 +66,7 @@
$existing_image = DB::selectFirst("SELECT * FROM users_images WHERE `hash` = '$hash'");
if ($existing_image) {
die(json_encode(['status' => 'success', 'path' => $existing_image['path']]));
returnJSONData(['status' => 'success', 'path' => $existing_image['path']]);
}
$image = new Imagick($_FILES["image_upload_file"]["tmp_name"]);
@ -94,7 +92,7 @@
DB::insert("INSERT INTO users_images (`path`, uploader, width, height, upload_time, size, `hash`) VALUES ('$filename', '{$myUser['username']}', $width, $height, now(), {$_FILES["image_upload_file"]["size"]}, '$hash')");
die(json_encode(['status' => 'success', 'path' => $filename]));
returnJSONData(['status' => 'success', 'path' => $filename]);
} elseif ($_POST['image_delete_submit'] == 'submit') {
crsf_defend();

View File

@ -315,8 +315,7 @@
DB::query("insert into user_info (username, realname, email, school, password, svn_password, register_time, usergroup) values ('$username', '$realname', '$email', '$school', '$password', '$svn_password', now(), 'U')");
header('Content-Type: application/json');
die(json_encode(['status' => 'success', 'message' => '']));
returnJSONData(['status' => 'success', 'message' => '']);
};
$register_form->setAjaxSubmit(<<<EOD
function(res) {
@ -371,8 +370,7 @@ EOD);
DB::query("update user_info set password = '$esc_password' where username = '$esc_username'");
header('Content-Type: application/json');
die(json_encode(['status' => 'success', 'message' => '用户 ' . $vdata['username'] . ' 的密码已经被成功重置。']));
returnJSONData(['status' => 'success', 'message' => '用户 ' . $vdata['username'] . ' 的密码已经被成功重置。']);
};
$change_password_form->submit_button_config['margin_class'] = 'mt-3';
$change_password_form->submit_button_config['text'] = '重置';
@ -438,8 +436,7 @@ EOD);
break;
}
header('Content-Type: application/json');
die(json_encode(['status' => 'success', 'message' => '用户 ' . $username . ' 现在是 ' . $usergroup . '。']));
returnJSONData(['status' => 'success', 'message' => '用户 ' . $username . ' 现在是 ' . $usergroup . '。']);
};
$change_usergroup_form->setAjaxSubmit(<<<EOD
function(res) {

View File

@ -179,8 +179,7 @@ EOD);
DB::update("UPDATE user_info SET email = '$esc_email', qq = '$esc_qq', sex = '$esc_sex', motto = '$esc_motto', codeforces_handle = '$esc_codeforces_handle', github = '$esc_github', website = '$esc_website', avatar_source = '$esc_avatar_source' WHERE username = '{$user['username']}'");
header('Content-Type: application/json');
die(json_encode(['status' => 'success']));
returnJSONData(['status' => 'success']);
};
$update_profile_form->submit_button_config['margin_class'] = 'mt-3';
$update_profile_form->submit_button_config['text'] = '更新';
@ -206,31 +205,28 @@ EOD);
$update_profile_form->runAtServer();
} elseif ($cur_tab == 'password') {
if (isset($_POST['submit-change_password']) && $_POST['submit-change_password'] == 'change_password') {
header('Content-Type: application/json');
$old_password = $_POST['current_password'];
$new_password = $_POST['new_password'];
if (!validatePassword($old_password) || !checkPassword($user, $old_password)) {
die(json_encode(['status' => 'error', 'message' => '旧密码错误']));
returnJSONData(['status' => 'error', 'message' => '旧密码错误']);
}
if (!validatePassword($new_password)) {
die(json_encode(['status' => 'error', 'message' => '新密码不合法']));
returnJSONData(['status' => 'error', 'message' => '新密码不合法']);
}
if ($old_password == $new_password) {
die(json_encode(['status' => 'error', 'message' => '新密码不能与旧密码相同']));
returnJSONData(['status' => 'error', 'message' => '新密码不能与旧密码相同']);
}
$password = getPasswordToStore($new_password, $user['username']);
DB::update("UPDATE `user_info` SET `password` = '$password' where `username` = '{$user['username']}'");
die(json_encode(['status' => 'success', 'message' => '密码修改成功']));
returnJSONData(['status' => 'success', 'message' => '密码修改成功']);
}
} elseif ($cur_tab == 'privilege') {
if (isset($_POST['submit-privilege']) && $_POST['submit-privilege'] == 'privilege' && isSuperUser($myUser)) {
header('Content-Type: application/json');
$user['usertype'] = 'student';
if ($_POST['user_type'] == 'teacher') {
@ -258,7 +254,7 @@ EOD);
DB::update("UPDATE `user_info` SET `usertype` = '{$user['usertype']}' where `username` = '{$user['username']}'");
die(json_encode(['status' => 'success', 'message' => '权限修改成功']));
returnJSONData(['status' => 'success', 'message' => '权限修改成功']);
}
}

View File

@ -861,4 +861,9 @@ EOD;
};
return $form;
}
function returnJSONData($data) {
header('Content-Type: application/json');
die(json_encode($data));
}
?>