forgot.php 3.39 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 'forgot my password' 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 28
require_once QA_INCLUDE_DIR . 'db/users.php';
require_once QA_INCLUDE_DIR . 'app/captcha.php';
Scott committed
29 30


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

Scott committed
33 34
if (QA_FINAL_EXTERNAL_USERS)
	qa_fatal_error('User login is handled by external code');
Scott committed
35

Scott committed
36 37
if (qa_is_logged_in())
	qa_redirect('');
Scott committed
38 39


Scott committed
40
// Start the 'I forgot my password' process, sending email if appropriate
Scott committed
41

Scott committed
42 43
if (qa_clicked('doforgot')) {
	require_once QA_INCLUDE_DIR . 'app/users-edit.php';
Scott committed
44

Scott committed
45
	$inemailhandle = qa_post_text('emailhandle');
Scott committed
46

Scott committed
47
	$errors = array();
Scott committed
48

Scott committed
49 50
	if (!qa_check_form_security_code('forgot', qa_post_text('code')))
		$errors['page'] = qa_lang_html('misc/form_security_again');
Scott committed
51

Scott committed
52 53 54 55 56 57 58 59
	else {
		if (strpos($inemailhandle, '@') === false) { // handles can't contain @ symbols
			$matchusers = qa_db_user_find_by_handle($inemailhandle);
			$passemailhandle = !qa_opt('allow_login_email_only');
		} else {
			$matchusers = qa_db_user_find_by_email($inemailhandle);
			$passemailhandle = true;
		}
Scott committed
60

61
		if (count($matchusers) != 1 || !$passemailhandle) // if we get more than one match (should be impossible) also give an error
Scott committed
62
			$errors['emailhandle'] = qa_lang('users/user_not_found');
Scott committed
63

Scott committed
64 65
		if (qa_opt('captcha_on_reset_password'))
			qa_captcha_validate_post($errors);
Scott committed
66

Scott committed
67 68 69
		if (empty($errors)) {
			$inuserid = $matchusers[0];
			qa_start_reset_user($inuserid);
70
			qa_redirect('reset', $passemailhandle ? array('e' => $inemailhandle, 's' => '1') : null); // redirect to page where code is entered
Scott committed
71
		}
Scott committed
72
	}
Scott committed
73

Scott committed
74 75
} else
	$inemailhandle = qa_get('e');
Scott committed
76 77


Scott committed
78
// Prepare content for theme
Scott committed
79

Scott committed
80
$qa_content = qa_content_prepare();
Scott committed
81

Scott committed
82 83
$qa_content['title'] = qa_lang_html('users/reset_title');
$qa_content['error'] = @$errors['page'];
Scott committed
84

Scott committed
85 86
$qa_content['form'] = array(
	'tags' => 'method="post" action="' . qa_self_html() . '"',
Scott committed
87

Scott committed
88
	'style' => 'tall',
Scott committed
89

Scott committed
90 91
	'fields' => array(
		'email_handle' => array(
92
			'type' => qa_opt('allow_login_email_only') ? 'email' : 'text',
93
			'label' => qa_opt('allow_login_email_only') ? qa_lang_html('users/email_label') : qa_lang_html('users/email_handle_label'),
Scott committed
94 95 96 97
			'tags' => 'name="emailhandle" id="emailhandle"',
			'value' => qa_html(@$inemailhandle),
			'error' => qa_html(@$errors['emailhandle']),
			'note' => qa_lang_html('users/send_reset_note'),
Scott committed
98
		),
Scott committed
99
	),
Scott committed
100

Scott committed
101 102 103
	'buttons' => array(
		'send' => array(
			'label' => qa_lang_html('users/send_reset_button'),
Scott committed
104
		),
Scott committed
105
	),
Scott committed
106

Scott committed
107 108 109 110 111
	'hidden' => array(
		'doforgot' => '1',
		'code' => qa_get_form_security_code('forgot'),
	),
);
Scott committed
112

Scott committed
113 114
if (qa_opt('captcha_on_reset_password'))
	qa_set_up_captcha_field($qa_content, $qa_content['form']['fields'], @$errors);
Scott committed
115

Scott committed
116
$qa_content['focusid'] = 'emailhandle';
Scott committed
117 118


Scott committed
119
return $qa_content;