diff --git a/web/app/controllers/image_hosting/index.php b/web/app/controllers/image_hosting/index.php index 4e45f2e..2237a86 100644 --- a/web/app/controllers/image_hosting/index.php +++ b/web/app/controllers/image_hosting/index.php @@ -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(); diff --git a/web/app/controllers/super_manage.php b/web/app/controllers/super_manage.php index 45911c8..59687b1 100644 --- a/web/app/controllers/super_manage.php +++ b/web/app/controllers/super_manage.php @@ -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(<< '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(<< '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' => '权限修改成功']); } } diff --git a/web/app/libs/uoj-form-lib.php b/web/app/libs/uoj-form-lib.php index 18560c2..f91aae2 100644 --- a/web/app/libs/uoj-form-lib.php +++ b/web/app/libs/uoj-form-lib.php @@ -861,4 +861,9 @@ EOD; }; return $form; } + + function returnJSONData($data) { + header('Content-Type: application/json'); + die(json_encode($data)); + } ?>