forgot.php 3.26 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 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-page-forgot.php
	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
23 24 25 26
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
	header('Location: ../');
	exit;
}
Scott committed
27

Scott committed
28 29
require_once QA_INCLUDE_DIR . 'db/users.php';
require_once QA_INCLUDE_DIR . 'app/captcha.php';
Scott committed
30 31 32 33


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

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

Scott committed
37 38
if (qa_is_logged_in())
	qa_redirect('');
Scott committed
39 40 41 42


//	Start the 'I forgot my password' process, sending email if appropriate

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

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

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

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

Scott committed
53 54 55 56
	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');
Scott committed
57

Scott committed
58 59 60 61
		} else {
			$matchusers = qa_db_user_find_by_email($inemailhandle);
			$passemailhandle = true;
		}
Scott committed
62

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

Scott committed
66 67
		if (qa_opt('captcha_on_reset_password'))
			qa_captcha_validate_post($errors);
Scott committed
68

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

Scott committed
76 77
} else
	$inemailhandle = qa_get('e');
Scott committed
78 79 80 81


//	Prepare content for theme

Scott committed
82
$qa_content = qa_content_prepare();
Scott committed
83

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

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

Scott committed
90
	'style' => 'tall',
Scott committed
91

Scott committed
92 93 94 95 96 97 98
	'fields' => array(
		'email_handle' => array(
			'label' => qa_lang_html('users/email_handle_label'),
			'tags' => 'name="emailhandle" id="emailhandle"',
			'value' => qa_html(@$inemailhandle),
			'error' => qa_html(@$errors['emailhandle']),
			'note' => qa_lang_html('users/send_reset_note'),
Scott committed
99
		),
Scott committed
100
	),
Scott committed
101

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

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

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

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


Scott committed
120
return $qa_content;