reset.php 5.94 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Controller for password reset page (comes after forgot page)


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
22
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../');
Scott committed
24 25
	exit;
}
Scott committed
26

Scott committed
27
// Check we're not using single-sign on integration and that we're not logged in
Scott committed
28

29
if (QA_FINAL_EXTERNAL_USERS) {
Scott committed
30
	qa_fatal_error('User login is handled by external code');
31
}
Scott committed
32

33
if (qa_is_logged_in()) {
Scott committed
34
	qa_redirect('');
35
}
Scott committed
36

37 38 39 40 41 42
// Fetch the email or handle from POST or GET
$emailHandle = qa_post_text('emailhandle');
if (!isset($emailHandle)) {
	$emailHandle = qa_get('e');
}
$emailHandle = trim($emailHandle); // if $emailHandle is null, trim returns an empty string
Scott committed
43

44 45 46 47 48 49
// Fetch the code from POST or GET
$code = qa_post_text('code');
if (!isset($code)) {
	$code = qa_get('c');
}
$code = trim($code); // if $code is null, trim returns an empty string
Scott committed
50

51
$forgotPath = strlen($emailHandle) > 0 ? qa_path('forgot', array('e' => $emailHandle)) : qa_path('forgot');
Scott committed
52

53
$focusId = 'code';
Scott committed
54

55 56 57 58
$errors = array();
$fields = array(
	'email_handle' => array(
		'type' => 'static',
59
		'label' => qa_lang_html(qa_opt('allow_login_email_only') ? 'users/email_label' : 'users/email_handle_label'),
60 61 62
		'value' => qa_html($emailHandle),
	),
	'code' => array(
63
		'label' => qa_lang_html('users/email_code_label'),
64 65 66
		'tags' => 'name="code" id="code"',
		'value' => isset($code) ? qa_html($code) : null,
		'note_force' => true,
67 68
		'note' => qa_lang_html('users/email_code_emailed') . ' - ' .
			'<a href="' . qa_html($forgotPath) . '">' . qa_lang_html('users/email_code_another') . '</a>',
69 70 71 72 73 74 75 76 77 78 79
	),
);
$buttons = array(
	'next' => array(
		'tags' => 'name="donext"',
		'label' => qa_lang_html('misc/next_step'),
	),
);
$hidden = array(
	'formcode' => qa_get_form_security_code('reset'),
);
Scott committed
80

81 82 83
if (strlen($emailHandle) > 0) {
	require_once QA_INCLUDE_DIR . 'app/users-edit.php';
	require_once QA_INCLUDE_DIR . 'db/users.php';
Scott committed
84

85 86 87 88 89 90 91 92 93 94 95 96
	$hidden['emailhandle'] = $emailHandle;

	$matchingUsers = qa_opt('allow_login_email_only') || strpos($emailHandle, '@') !== false // handles can't contain @ symbols
		? qa_db_user_find_by_email($emailHandle)
		: qa_db_user_find_by_handle($emailHandle);

	// Make sure there is only one match
	if (count($matchingUsers) == 1) {
		require_once QA_INCLUDE_DIR . 'db/selects.php';

		// strlen() check is vital otherwise we can reset code for most users by entering the empty string
		if (strlen($code) > 0) {
97 98 99
			$userId = $matchingUsers[0];
			$userInfo = qa_db_select_with_pending(qa_db_user_account_selectspec($userId, true));

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
			if (strtolower(trim($userInfo['emailcode'])) == strtolower($code)) {
				// User input a valid code so no need to ask for it but pass it to the next step
				unset($fields['code']);
				$hidden['code'] = $code;

				$buttons = array(
					'change' => array(
						'tags' => 'name="dochangepassword"',
						'label' => qa_lang_html('users/change_password'),
					),
				);

				$focusId = 'newpassword1';

				if (qa_clicked('dochangepassword')) {
					$newPassword = qa_post_text('newpassword1');
					$repeatPassword = qa_post_text('newpassword2');

					if (!qa_check_form_security_code('reset', qa_post_text('formcode'))) {
						$errors['page'] = qa_lang_html('misc/form_security_again');
					} else {
						$passwordError = qa_password_validate($newPassword, $userInfo);
						if (!empty($passwordError)) {
							$errors['new_1'] = $passwordError['password'];
						}

						if ($newPassword != $repeatPassword) {
							$errors['new_2'] = qa_lang('users/password_mismatch');
						}

						if (empty($errors)) {
							// Update password, login user, fire events and redirect to home page
							qa_finish_reset_user($userId, $newPassword);
							qa_redirect('');
						}
					}
				}

				$fields['new_1'] = array(
					'label' => qa_lang_html('users/new_password_1'),
					'tags' => 'name="newpassword1" id="newpassword1"',
					'type' => 'password',
					'error' => qa_html(isset($errors['new_1']) ? $errors['new_1'] : null),
				);

				$fields['new_2'] = array(
					'label' => qa_lang_html('users/new_password_2'),
					'tags' => 'name="newpassword2"',
					'type' => 'password',
					'error' => qa_html(isset($errors['new_2']) ? $errors['new_2'] : null),
				);
Scott committed
151
			} else {
152
				// User input wrong code so show field with error
153
				$fields['code']['error'] = qa_lang('users/email_code_wrong');
Scott committed
154
			}
155
		} elseif (qa_clicked('donext')) {
156
			// If user submitted the form with an empty code
157
			$fields['code']['error'] = qa_lang('users/email_code_wrong');
Scott committed
158
		}
159 160 161
	} else {
		// If match more than one (should be impossible), consider it a non-match
		$errors['page'] = qa_lang_html('users/user_not_found');
Scott committed
162
	}
Scott committed
163
} else {
164 165
	// If there is no handle notify the user
	$errors['page'] = qa_lang_html('users/user_not_found');
Scott committed
166
}
Scott committed
167 168


Scott committed
169
// Prepare content for theme
Scott committed
170

Scott committed
171
$qa_content = qa_content_prepare();
Scott committed
172

Scott committed
173
$qa_content['title'] = qa_lang_html('users/reset_title');
174
$qa_content['error'] = isset($errors['page']) ? $errors['page'] : null;
Scott committed
175

176 177 178
if (!isset($errors['page'])) {
	// Using this form action instead of qa_self_html() to get rid of the 's' (success) GET parameter from forgot.php
	$qa_content['form'] = array(
179
		'tags' => 'method="post" action="' . qa_path_html('reset') . '"',
Scott committed
180

181 182
		'style' => 'tall',

183
		'ok' => qa_get('s') ? qa_lang_html('users/email_code_emailed') : null,
Scott committed
184

185 186 187 188 189 190 191
		'fields' => $fields,

		'buttons' => $buttons,

		'hidden' => $hidden,
	);
}
Scott committed
192

193
$qa_content['focusid'] = $focusId;
Scott committed
194

Scott committed
195
return $qa_content;