From e33e3ef413bbaa6bed12e112ba93554f3f8ab4fc Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 19 Oct 2022 21:59:16 +0800 Subject: [PATCH] refactor(web/user/edit): user_edit_v2 --- db/app_uoj233.sql | 1 + web/app/controllers/user_info_edit.php | 447 ++++++++++++++--------- web/app/libs/uoj-form-lib.php | 15 +- web/app/locale/basic/en.php | 10 +- web/app/locale/basic/zh-cn.php | 10 +- web/app/models/HTML.php | 8 +- web/app/models/Upgrader.php | 2 +- web/app/route.php | 2 +- web/app/upgrade/6_user_info_v2/README.md | 1 + web/app/upgrade/6_user_info_v2/down.sql | 1 + web/app/upgrade/6_user_info_v2/up.sql | 1 + 11 files changed, 302 insertions(+), 196 deletions(-) create mode 100644 web/app/upgrade/6_user_info_v2/README.md create mode 100644 web/app/upgrade/6_user_info_v2/down.sql create mode 100644 web/app/upgrade/6_user_info_v2/up.sql diff --git a/db/app_uoj233.sql b/db/app_uoj233.sql index 397ad22..7983cdf 100644 --- a/db/app_uoj233.sql +++ b/db/app_uoj233.sql @@ -816,6 +816,7 @@ CREATE TABLE `user_info` ( `last_login` timestamp NOT NULL DEFAULT 0, `last_visited` timestamp NOT NULL DEFAULT 0, `images_size_limit` int(11) UNSIGNED NOT NULL DEFAULT 104857600, /* 100 MiB */ + `codeforces_handle` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', PRIMARY KEY (`username`), KEY `ac_num` (`ac_num`,`username`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4; diff --git a/web/app/controllers/user_info_edit.php b/web/app/controllers/user_info_edit.php index 853381c..75efaec 100644 --- a/web/app/controllers/user_info_edit.php +++ b/web/app/controllers/user_info_edit.php @@ -1,4 +1,8 @@ [ + 'name' => ' 个人资料', + 'url' => "/user/{$user['username']}/edit/profile", + ], + 'password' => [ + 'name' => ' 修改密码', + 'url' => "/user/{$user['username']}/edit/password", + ], + ]; + + if (!isset($tabs_info[$cur_tab])) { + become404Page(); + } - $email = $_POST['email']; - if (!validateEmail($email)) { - return "失败:无效电子邮箱。"; - } - $esc_email = DB::escape($email); - DB::update("update user_info set email = '$esc_email' where username = '{$user['username']}'"); + if ($cur_tab == 'profile') { + $update_profile_form = new UOJForm('update_profile'); + $username = UOJLocale::get('username'); + $avatar = UOJLocale::get('avatar'); + $change_avatar_help = UOJLocale::get('change avatar help'); + $update_profile_form->appendHTML(<< + + +
用户名不能被修改。
+ +
+
$avatar
+
$change_avatar_help
+
+EOD); + $update_profile_form->addVInput('email', 'email', UOJLocale::get('email'), $user['email'], + function($email, &$vdata) { + if (!validateEmail($email)) { + return 'Email 格式不合法。'; + } - if ($_POST['Qtag']) { - $qq = $_POST['qq']; - if (!validateQQ($qq)) { - return "失败:无效QQ。"; - } - $esc_qq = DB::escape($qq); - DB::update("update user_info set qq = '$esc_qq' where username = '{$user['username']}'"); + $vdata['email'] = $email; + + return ''; + }, null); + $update_profile_form->addVInput('qq', 'text', UOJLocale::get('QQ'), $user['qq'] == 0 ? '' : $user['qq'], + function($qq, &$vdata) { + if ($qq && !validateQQ($qq)) { + return 'QQ 格式不合法。'; + } + + $vdata['qq'] = $qq; + + return ''; + }, null); + if (isSuperUser($myUser)) { + $update_profile_form->addVInput('school', 'text', UOJLocale::get('school'), $user['school'], + function($school, &$vdata) { + $vdata['school'] = $school; + + return ''; + }, null); } else { - DB::update("update user_info set QQ = NULL where username = '{$user['username']}'"); + $school = UOJLocale::get('school'); + $update_profile_form->appendHTML(<< + + +
只有管理员才能修改用户所属学校。
+ +EOD); } - if ($_POST['sex'] == "U" || $_POST['sex'] == 'M' || $_POST['sex'] == 'F') { - $sex = $_POST['sex']; - $esc_sex = DB::escape($sex); - DB::update("update user_info set sex = '$esc_sex' where username = '{$user['username']}'"); + $update_profile_form->addVSelect('sex', [ + 'U' => UOJLocale::get('refuse to answer'), + 'M' => UOJLocale::get('male'), + 'F' => UOJLocale::get('female'), + ], UOJLocale::get('sex'), $user['sex']); + $update_profile_form->addVInput('motto', 'text', UOJLocale::get('motto'), $user['motto'], + function($motto, &$vdata) { + if (!validateMotto($motto)) { + return '格言格式不合法'; + } + + $vdata['motto'] = $motto; + + return ''; + }, null); + $update_profile_form->addVInput('codeforces_handle', 'text', UOJLocale::get('codeforces handle'), $user['codeforces_handle'], + function($codeforces_handle, &$vdata) { + if ($codeforces_handle && !validateUsername($codeforces_handle)) { + return 'Codeforces 用户名格式不合法。'; + } + + $vdata['codeforces_handle'] = $codeforces_handle; + + return ''; + }, null); + $update_profile_form->handle = function(&$vdata) use ($user, $myUser) { + $esc_email = DB::escape($vdata['email']); + $esc_qq = DB::escape($vdata['qq']); + $esc_sex = DB::escape($vdata['sex']); + $esc_motto = DB::escape($vdata['motto']); + $esc_codeforces_handle = DB::escape($vdata['codeforces_handle']); + + if (isSuperUser($myUser)) { + $esc_school = DB::escape($vdata['school']); + + DB::update("UPDATE user_info SET school = '$esc_school' WHERE username = '{$user['username']}'"); + } + + DB::update("UPDATE user_info SET email = '$esc_email', qq = '$esc_qq', sex = '$esc_sex', motto = '$esc_motto', codeforces_handle = '$esc_codeforces_handle' WHERE username = '{$user['username']}'"); + }; + $update_profile_form->submit_button_config['margin_class'] = 'mt-3'; + $update_profile_form->submit_button_config['text'] = '更新'; + $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' => '旧密码错误'])); + } + + if (!validatePassword($new_password)) { + die(json_encode(['status' => 'error', 'message' => '新密码不合法'])); + } + + if ($old_password == $new_password) { + die(json_encode(['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' => '密码修改成功'])); } - - if (validateMotto($_POST['motto'])) { - $esc_motto = DB::escape($_POST['motto']); - DB::update("update user_info set motto = '$esc_motto' where username = '{$user['username']}'"); - } - - return "ok"; - } - if (isset($_POST['change'])) { - die(handlePost()); } + + $pageTitle = $user['username'] == $myUser['username'] + ? UOJLocale::get('modify my profile') + : UOJLocale::get('modify his profile', $user['username']) ?> - - - - -

您正在使用管理特权修改 的个人信息。

+ + + +

+ +

+ +
+ +
+ +
+ $tab): ?> + + + + +
+ +"> + 返回 + + + + -
- -

-
- -
- - -
-
- -

-
- -
- - - -
-
-
- -
- - -
-
-
- -
- - -
-
-
- -
- -
-
-
- -
- - 格言支持 Markdown 语法。 -
-
-
-
-

-
-
-
-
- -
-
-
- + + + +
+
+ diff --git a/web/app/libs/uoj-form-lib.php b/web/app/libs/uoj-form-lib.php index b44bb54..16d272a 100644 --- a/web/app/libs/uoj-form-lib.php +++ b/web/app/libs/uoj-form-lib.php @@ -159,7 +159,7 @@ EOD; public function addVSelect($name, $options, $label_text, $default_value) { $default_value = htmlspecialchars($default_value); $html = << +
'; } public static function div_vinput($name, $type, $label_text, $default_value) { - return '
' - . '' + return '
' + . '' . '' - . '' + . '' . '
'; } public static function div_vtextarea($name, $label_text, $default_value) { - return '
' + return '
' . '' . '' . '' diff --git a/web/app/models/Upgrader.php b/web/app/models/Upgrader.php index 05d83f7..3a98771 100644 --- a/web/app/models/Upgrader.php +++ b/web/app/models/Upgrader.php @@ -105,7 +105,7 @@ class Upgrader { public static function upgradeToLatest() { $names = array_filter(scandir(self::upgraderRoot()), function ($name) { - return is_dir(self::upgraderRoot().'/'.$name) && preg_match('/^\d+_[a-zA-Z_]+$/', $name); + return is_dir(self::upgraderRoot().'/'.$name) && preg_match('/^\d+_[0-9a-zA-Z_]+$/', $name); }); natsort($names); diff --git a/web/app/route.php b/web/app/route.php index dd77b1e..f4f2789 100644 --- a/web/app/route.php +++ b/web/app/route.php @@ -72,7 +72,7 @@ Route::group([ Route::any('/reset-password', '/reset_pw.php'); Route::any('/user/{username}', '/user_info.php'); - Route::any('/user/{username}/edit', '/user_info_edit.php'); + Route::any('/user/{username}/edit(?:/{tab})?', '/user_info_edit.php'); Route::any('/user_msg', '/user_msg.php'); Route::any('/user/{username}/system_msg', '/user_system_msg.php'); diff --git a/web/app/upgrade/6_user_info_v2/README.md b/web/app/upgrade/6_user_info_v2/README.md new file mode 100644 index 0000000..e0c36ee --- /dev/null +++ b/web/app/upgrade/6_user_info_v2/README.md @@ -0,0 +1 @@ +ref: https://github.com/renbaoshuo/S2OJ/pull/6 diff --git a/web/app/upgrade/6_user_info_v2/down.sql b/web/app/upgrade/6_user_info_v2/down.sql new file mode 100644 index 0000000..2609831 --- /dev/null +++ b/web/app/upgrade/6_user_info_v2/down.sql @@ -0,0 +1 @@ +ALTER TABLE `user_info` DROP COLUMN IF EXISTS `codeforces_handle`; diff --git a/web/app/upgrade/6_user_info_v2/up.sql b/web/app/upgrade/6_user_info_v2/up.sql new file mode 100644 index 0000000..fc94742 --- /dev/null +++ b/web/app/upgrade/6_user_info_v2/up.sql @@ -0,0 +1 @@ +ALTER TABLE `user_info` ADD COLUMN `codeforces_handle` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '';