change_user_info.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. if (!Auth::check()) {
  3. redirectToLogin();
  4. }
  5. function handlePost() {
  6. global $myUser;
  7. if (!isset($_POST['old_password'])) {
  8. return '无效表单';
  9. }
  10. $old_password = $_POST['old_password'];
  11. if (!validatePassword($old_password) || !checkPassword($myUser, $old_password)) {
  12. return "失败:密码错误。";
  13. }
  14. if ($_POST['ptag']) {
  15. $password = $_POST['password'];
  16. if (!validatePassword($password)) {
  17. return "失败:无效密码。";
  18. }
  19. $password = getPasswordToStore($password, $myUser['username']);
  20. DB::update("update user_info set password = '$password' where username = '{$myUser['username']}'");
  21. }
  22. $email = $_POST['email'];
  23. if (!validateEmail($email)) {
  24. return "失败:无效电子邮箱。";
  25. }
  26. $esc_email = DB::escape($email);
  27. DB::update("update user_info set email = '$esc_email' where username = '{$myUser['username']}'");
  28. if ($_POST['Qtag']) {
  29. $qq = $_POST['qq'];
  30. if ($qq != $myUser['qq'])
  31. return "失败:不能更改 QQ。";
  32. if (!validateQQ($qq)) {
  33. return "失败:无效QQ。";
  34. }
  35. $esc_qq = DB::escape($qq);
  36. DB::update("update user_info set qq = '$esc_qq' where username = '{$myUser['username']}'");
  37. } else {
  38. DB::update("update user_info set QQ = NULL where username = '{$myUser['username']}'");
  39. }
  40. if ($_POST['sex'] == "U" || $_POST['sex'] == 'M' || $_POST['sex'] == 'F') {
  41. $sex = $_POST['sex'];
  42. $esc_sex = DB::escape($sex);
  43. DB::update("update user_info set sex = '$esc_sex' where username = '{$myUser['username']}'");
  44. }
  45. if (validateMotto($_POST['motto'])) {
  46. $esc_motto = DB::escape($_POST['motto']);
  47. DB::update("update user_info set motto = '$esc_motto' where username = '{$myUser['username']}'");
  48. }
  49. return "ok";
  50. }
  51. if (isset($_POST['change'])) {
  52. die(handlePost());
  53. }
  54. ?>
  55. <?php
  56. $REQUIRE_LIB['dialog'] = '';
  57. $REQUIRE_LIB['md5'] = '';
  58. ?>
  59. <?php echoUOJPageHeader(UOJLocale::get('modify my profile')) ?>
  60. <h2 class="page-header"><?= UOJLocale::get('modify my profile') ?></h2>
  61. <form id="form-update" class="form-horizontal">
  62. <h4><?= UOJLocale::get('please enter your password for authorization') ?></h4>
  63. <div id="div-old_password" class="form-group">
  64. <label for="input-old_password" class="col-sm-2 control-label"><?= UOJLocale::get('password') ?></label>
  65. <div class="col-sm-3">
  66. <input type="password" class="form-control" name="old_password" id="input-old_password" placeholder="<?= UOJLocale::get('enter your password') ?>" maxlength="20" />
  67. <span class="help-block" id="help-old_password"></span>
  68. </div>
  69. </div>
  70. <h4><?= UOJLocale::get('please enter your new profile') ?></h4>
  71. <div id="div-password" class="form-group">
  72. <label for="input-password" class="col-sm-2 control-label"><?= UOJLocale::get('new password') ?></label>
  73. <div class="col-sm-3">
  74. <input type="password" class="form-control" id="input-password" name="password" placeholder="<?= UOJLocale::get('enter your new password') ?>" maxlength="20" />
  75. <input type="password" class="form-control top-buffer-sm" id="input-confirm_password" placeholder="<?= UOJLocale::get('re-enter your new password') ?>" maxlength="20" />
  76. <span class="help-block" id="help-password"><?= UOJLocale::get('leave it blank if you do not want to change the password') ?></span>
  77. </div>
  78. </div>
  79. <div id="div-email" class="form-group">
  80. <label for="input-email" class="col-sm-2 control-label"><?= UOJLocale::get('email') ?></label>
  81. <div class="col-sm-3">
  82. <input type="email" class="form-control" name="email" id="input-email" value="<?=$myUser['email']?>" placeholder="<?= UOJLocale::get('enter your email') ?>" maxlength="50" />
  83. <span class="help-block" id="help-email"></span>
  84. </div>
  85. </div>
  86. <div id="div-qq" class="form-group">
  87. <label for="input-qq" class="col-sm-2 control-label"><?= UOJLocale::get('QQ') ?></label>
  88. <div class="col-sm-3">
  89. <input type="text" class="form-control" name="qq" id="input-qq" value="<?= $myUser['qq'] != 0 ? $myUser['qq'] : '' ?>" placeholder="<?= UOJLocale::get('enter your QQ') ?>" maxlength="50" />
  90. <span class="help-block" id="help-qq"></span>
  91. </div>
  92. </div>
  93. <div id="div-sex" class="form-group">
  94. <label for="input-sex" class="col-sm-2 control-label"><?= UOJLocale::get('sex') ?></label>
  95. <div class="col-sm-3">
  96. <select class="form-control" id="input-sex" name="sex">
  97. <option value="U"<?= Auth::user()['sex'] == 'U' ? ' selected="selected"' : ''?>><?= UOJLocale::get('refuse to answer') ?></option>
  98. <option value="M"<?= Auth::user()['sex'] == 'M' ? ' selected="selected"' : ''?>><?= UOJLocale::get('male') ?></option>
  99. <option value="F"<?= Auth::user()['sex'] == 'F' ? ' selected="selected"' : ''?>><?= UOJLocale::get('female') ?></option>
  100. </select>
  101. </div>
  102. </div>
  103. <div id="div-motto" class="form-group">
  104. <label for="input-motto" class="col-sm-2 control-label"><?= UOJLocale::get('motto') ?></label>
  105. <div class="col-sm-3">
  106. <textarea class="form-control" id="input-motto" name="motto"><?=HTML::escape($myUser['motto'])?></textarea>
  107. <span class="help-block" id="help-motto"></span>
  108. </div>
  109. </div>
  110. <div class="form-group">
  111. <div class="col-sm-offset-2 col-sm-3">
  112. <p class="form-control-static"><strong><?= UOJLocale::get('change avatar help') ?></strong></p>
  113. </div>
  114. </div>
  115. <div class="form-group">
  116. <div class="col-sm-offset-2 col-sm-3">
  117. <button type="submit" id="button-submit" class="btn btn-secondary"><?= UOJLocale::get('submit') ?></button>
  118. </div>
  119. </div>
  120. </form>
  121. <script type="text/javascript">
  122. function validateUpdatePost() {
  123. var ok = true;
  124. ok &= getFormErrorAndShowHelp('email', validateEmail);
  125. ok &= getFormErrorAndShowHelp('old_password', validatePassword);
  126. if ($('#input-password').val().length > 0)
  127. ok &= getFormErrorAndShowHelp('password', validateSettingPassword);
  128. if ($('#input-qq').val().length > 0)
  129. ok &= getFormErrorAndShowHelp('qq', validateQQ);
  130. ok &= getFormErrorAndShowHelp('motto', validateMotto);
  131. return ok;
  132. }
  133. function submitUpdatePost() {
  134. if (!validateUpdatePost())
  135. return;
  136. $.post('/user/modify-profile', {
  137. change : '',
  138. etag : $('#input-email').val().length,
  139. ptag : $('#input-password').val().length,
  140. Qtag : $('#input-qq').val().length,
  141. email : $('#input-email').val(),
  142. password : md5($('#input-password').val(), "<?= getPasswordClientSalt() ?>"),
  143. old_password : md5($('#input-old_password').val(), "<?= getPasswordClientSalt() ?>"),
  144. qq : $('#input-qq').val(),
  145. sex : $('#input-sex').val(),
  146. motto : $('#input-motto').val()
  147. }, function(msg) {
  148. if (msg == 'ok') {
  149. BootstrapDialog.show({
  150. title : '修改成功',
  151. message : '用户信息修改成功',
  152. type : BootstrapDialog.TYPE_SUCCESS,
  153. buttons : [{
  154. label: '好的',
  155. action: function(dialog) {
  156. dialog.close();
  157. }
  158. }],
  159. onhidden : function(dialog) {
  160. window.location.href = '/user/profile/<?=$myUser['username']?>';
  161. }
  162. });
  163. } else {
  164. BootstrapDialog.show({
  165. title : '修改失败',
  166. message : msg,
  167. type : BootstrapDialog.TYPE_DANGER,
  168. buttons: [{
  169. label: '好的',
  170. action: function(dialog) {
  171. dialog.close();
  172. }
  173. }],
  174. });
  175. }
  176. });
  177. }
  178. $(document).ready(function(){$('#form-update').submit(function(e) {submitUpdatePost();e.preventDefault();});
  179. });
  180. </script>
  181. <?php echoUOJPageFooter() ?>