PageRenderTime 33ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/qa-include/pages/reset.php

http://github.com/q2a/question2answer
PHP | 195 lines | 128 code | 35 blank | 32 comment | 23 complexity | e2bbfc87c100b56af17567a98ca0cd9a MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. Question2Answer by Gideon Greenspan and contributors
  4. http://www.question2answer.org/
  5. Description: Controller for password reset page (comes after forgot page)
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. More about this license: http://www.question2answer.org/license.php
  15. */
  16. if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
  17. header('Location: ../../');
  18. exit;
  19. }
  20. // Check we're not using single-sign on integration and that we're not logged in
  21. if (QA_FINAL_EXTERNAL_USERS) {
  22. qa_fatal_error('User login is handled by external code');
  23. }
  24. if (qa_is_logged_in()) {
  25. qa_redirect('');
  26. }
  27. // Fetch the email or handle from POST or GET
  28. $emailHandle = qa_post_text('emailhandle');
  29. if (!isset($emailHandle)) {
  30. $emailHandle = qa_get('e');
  31. }
  32. $emailHandle = trim($emailHandle); // if $emailHandle is null, trim returns an empty string
  33. // Fetch the code from POST or GET
  34. $code = qa_post_text('code');
  35. if (!isset($code)) {
  36. $code = qa_get('c');
  37. }
  38. $code = trim($code); // if $code is null, trim returns an empty string
  39. $forgotPath = strlen($emailHandle) > 0 ? qa_path('forgot', array('e' => $emailHandle)) : qa_path('forgot');
  40. $focusId = 'code';
  41. $errors = array();
  42. $fields = array(
  43. 'email_handle' => array(
  44. 'type' => 'static',
  45. 'label' => qa_lang_html(qa_opt('allow_login_email_only') ? 'users/email_label' : 'users/email_handle_label'),
  46. 'value' => qa_html($emailHandle),
  47. ),
  48. 'code' => array(
  49. 'label' => qa_lang_html('users/email_code_label'),
  50. 'tags' => 'name="code" id="code"',
  51. 'value' => isset($code) ? qa_html($code) : null,
  52. 'note_force' => true,
  53. 'note' => qa_lang_html('users/email_code_emailed') . ' - ' .
  54. '<a href="' . qa_html($forgotPath) . '">' . qa_lang_html('users/email_code_another') . '</a>',
  55. ),
  56. );
  57. $buttons = array(
  58. 'next' => array(
  59. 'tags' => 'name="donext"',
  60. 'label' => qa_lang_html('misc/next_step'),
  61. ),
  62. );
  63. $hidden = array(
  64. 'formcode' => qa_get_form_security_code('reset'),
  65. );
  66. if (strlen($emailHandle) > 0) {
  67. require_once QA_INCLUDE_DIR . 'app/users-edit.php';
  68. require_once QA_INCLUDE_DIR . 'db/users.php';
  69. $hidden['emailhandle'] = $emailHandle;
  70. $matchingUsers = qa_opt('allow_login_email_only') || strpos($emailHandle, '@') !== false // handles can't contain @ symbols
  71. ? qa_db_user_find_by_email($emailHandle)
  72. : qa_db_user_find_by_handle($emailHandle);
  73. // Make sure there is only one match
  74. if (count($matchingUsers) == 1) {
  75. require_once QA_INCLUDE_DIR . 'db/selects.php';
  76. // strlen() check is vital otherwise we can reset code for most users by entering the empty string
  77. if (strlen($code) > 0) {
  78. $userId = $matchingUsers[0];
  79. $userInfo = qa_db_select_with_pending(qa_db_user_account_selectspec($userId, true));
  80. if (strtolower(trim($userInfo['emailcode'])) == strtolower($code)) {
  81. // User input a valid code so no need to ask for it but pass it to the next step
  82. unset($fields['code']);
  83. $hidden['code'] = $code;
  84. $buttons = array(
  85. 'change' => array(
  86. 'tags' => 'name="dochangepassword"',
  87. 'label' => qa_lang_html('users/change_password'),
  88. ),
  89. );
  90. $focusId = 'newpassword1';
  91. if (qa_clicked('dochangepassword')) {
  92. $newPassword = qa_post_text('newpassword1');
  93. $repeatPassword = qa_post_text('newpassword2');
  94. if (!qa_check_form_security_code('reset', qa_post_text('formcode'))) {
  95. $errors['page'] = qa_lang_html('misc/form_security_again');
  96. } else {
  97. $passwordError = qa_password_validate($newPassword, $userInfo);
  98. if (!empty($passwordError)) {
  99. $errors['new_1'] = $passwordError['password'];
  100. }
  101. if ($newPassword != $repeatPassword) {
  102. $errors['new_2'] = qa_lang('users/password_mismatch');
  103. }
  104. if (empty($errors)) {
  105. // Update password, login user, fire events and redirect to home page
  106. qa_finish_reset_user($userId, $newPassword);
  107. qa_redirect('');
  108. }
  109. }
  110. }
  111. $fields['new_1'] = array(
  112. 'label' => qa_lang_html('users/new_password_1'),
  113. 'tags' => 'name="newpassword1" id="newpassword1"',
  114. 'type' => 'password',
  115. 'error' => qa_html(isset($errors['new_1']) ? $errors['new_1'] : null),
  116. );
  117. $fields['new_2'] = array(
  118. 'label' => qa_lang_html('users/new_password_2'),
  119. 'tags' => 'name="newpassword2"',
  120. 'type' => 'password',
  121. 'error' => qa_html(isset($errors['new_2']) ? $errors['new_2'] : null),
  122. );
  123. } else {
  124. // User input wrong code so show field with error
  125. $fields['code']['error'] = qa_lang('users/email_code_wrong');
  126. }
  127. } elseif (qa_clicked('donext')) {
  128. // If user submitted the form with an empty code
  129. $fields['code']['error'] = qa_lang('users/email_code_wrong');
  130. }
  131. } else {
  132. // If match more than one (should be impossible), consider it a non-match
  133. $errors['page'] = qa_lang_html('users/user_not_found');
  134. }
  135. } else {
  136. // If there is no handle notify the user
  137. $errors['page'] = qa_lang_html('users/user_not_found');
  138. }
  139. // Prepare content for theme
  140. $qa_content = qa_content_prepare();
  141. $qa_content['title'] = qa_lang_html('users/reset_title');
  142. $qa_content['error'] = isset($errors['page']) ? $errors['page'] : null;
  143. if (!isset($errors['page'])) {
  144. // Using this form action instead of qa_self_html() to get rid of the 's' (success) GET parameter from forgot.php
  145. $qa_content['form'] = array(
  146. 'tags' => 'method="post" action="' . qa_path_html('reset') . '"',
  147. 'style' => 'tall',
  148. 'ok' => qa_get('s') ? qa_lang_html('users/email_code_emailed') : null,
  149. 'fields' => $fields,
  150. 'buttons' => $buttons,
  151. 'hidden' => $hidden,
  152. );
  153. }
  154. $qa_content['focusid'] = $focusId;
  155. return $qa_content;