qa-page-reset.php 4.36 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-page-reset.php
	Version: See define()s at top of qa-include/qa-base.php
	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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}


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

Gideon Greenspan committed
35 36
	if (QA_FINAL_EXTERNAL_USERS)
		qa_fatal_error('User login is handled by external code');
Scott Vivian committed
37

Gideon Greenspan committed
38 39
	if (qa_is_logged_in())
		qa_redirect('');
Scott Vivian committed
40

Gideon Greenspan committed
41 42 43 44 45 46

//	Process incoming form

	if (qa_clicked('doreset')) {
		require_once QA_INCLUDE_DIR.'qa-app-users-edit.php';
		require_once QA_INCLUDE_DIR.'qa-db-users.php';
Scott Vivian committed
47

Gideon Greenspan committed
48 49
		$inemailhandle=qa_post_text('emailhandle');
		$incode=trim(qa_post_text('code')); // trim to prevent passing in blank values to match uninitiated DB rows
Scott Vivian committed
50

Gideon Greenspan committed
51
		$errors=array();
Gideon Greenspan committed
52 53 54

		if (!qa_check_form_security_code('reset', qa_post_text('formcode')))
			$errors['page']=qa_lang_html('misc/form_security_again');
Scott Vivian committed
55

Gideon Greenspan committed
56 57 58 59 60
		else {
			if (qa_opt('allow_login_email_only') || (strpos($inemailhandle, '@')!==false)) // handles can't contain @ symbols
				$matchusers=qa_db_user_find_by_email($inemailhandle);
			else
				$matchusers=qa_db_user_find_by_handle($inemailhandle);
Scott Vivian committed
61

Gideon Greenspan committed
62 63
			if (count($matchusers)==1) { // if match more than one (should be impossible), consider it a non-match
				require_once QA_INCLUDE_DIR.'qa-db-selects.php';
Scott Vivian committed
64

Gideon Greenspan committed
65 66
				$inuserid=$matchusers[0];
				$userinfo=qa_db_select_with_pending(qa_db_user_account_selectspec($inuserid, true));
Scott Vivian committed
67

Gideon Greenspan committed
68 69 70 71
				// strlen() check is vital otherwise we can reset code for most users by entering the empty string
				if (strlen($incode) && (strtolower(trim($userinfo['emailcode'])) == strtolower($incode))) {
					qa_complete_reset_user($inuserid);
					qa_redirect('login', array('e' => $inemailhandle, 'ps' => '1')); // redirect to login page
Scott Vivian committed
72

Gideon Greenspan committed
73 74
				} else
					$errors['code']=qa_lang('users/reset_code_wrong');
Scott Vivian committed
75

Gideon Greenspan committed
76
			} else
Gideon Greenspan committed
77 78
				$errors['emailhandle']=qa_lang('users/user_not_found');
		}
Gideon Greenspan committed
79 80 81 82 83

	} else {
		$inemailhandle=qa_get('e');
		$incode=qa_get('c');
	}
Scott Vivian committed
84 85


Gideon Greenspan committed
86
//	Prepare content for theme
Scott Vivian committed
87

Gideon Greenspan committed
88 89 90
	$qa_content=qa_content_prepare();

	$qa_content['title']=qa_lang_html('users/reset_title');
Gideon Greenspan committed
91
	$qa_content['error']=@$errors['page'];
Gideon Greenspan committed
92 93 94 95 96

	if (empty($inemailhandle) || isset($errors['emailhandle']))
		$forgotpath=qa_path('forgot');
	else
		$forgotpath=qa_path('forgot',  array('e' => $inemailhandle));
Scott Vivian committed
97

Gideon Greenspan committed
98
	$qa_content['form']=array(
Gideon Greenspan committed
99
		'tags' => 'method="post" action="'.qa_self_html().'"',
Scott Vivian committed
100

Gideon Greenspan committed
101
		'style' => 'tall',
Scott Vivian committed
102

Gideon Greenspan committed
103
		'ok' => empty($incode) ? qa_lang_html('users/reset_code_emailed') : null,
Scott Vivian committed
104

Gideon Greenspan committed
105 106
		'fields' => array(
			'email_handle' => array(
Gideon Greenspan committed
107
				'label' => qa_opt('allow_login_email_only') ? qa_lang_html('users/email_label') : qa_lang_html('users/email_handle_label'),
Gideon Greenspan committed
108
				'tags' => 'name="emailhandle" id="emailhandle"',
Gideon Greenspan committed
109 110 111 112 113 114
				'value' => qa_html(@$inemailhandle),
				'error' => qa_html(@$errors['emailhandle']),
			),

			'code' => array(
				'label' => qa_lang_html('users/reset_code_label'),
Gideon Greenspan committed
115
				'tags' => 'name="code" id="code"',
Gideon Greenspan committed
116 117 118
				'value' => qa_html(@$incode),
				'error' => qa_html(@$errors['code']),
				'note' => qa_lang_html('users/reset_code_emailed').' - '.
Gideon Greenspan committed
119
					'<a href="'.qa_html($forgotpath).'">'.qa_lang_html('users/reset_code_another').'</a>',
Gideon Greenspan committed
120 121
			),
		),
Scott Vivian committed
122

Gideon Greenspan committed
123 124 125 126 127
		'buttons' => array(
			'reset' => array(
				'label' => qa_lang_html('users/send_password_button'),
			),
		),
Scott Vivian committed
128

Gideon Greenspan committed
129 130
		'hidden' => array(
			'doreset' => '1',
Gideon Greenspan committed
131
			'formcode' => qa_get_form_security_code('reset'),
Gideon Greenspan committed
132 133
		),
	);
Scott Vivian committed
134

Gideon Greenspan committed
135
	$qa_content['focusid']=(isset($errors['emailhandle']) || !strlen(@$inemailhandle)) ? 'emailhandle' : 'code';
Gideon Greenspan committed
136

Scott Vivian committed
137

Gideon Greenspan committed
138 139 140 141 142 143
	return $qa_content;


/*
	Omit PHP closing tag to help avoid accidental output
*/