Project

General

Profile

1 1420 Luisehahne
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         account
6
 * @author          WebsiteBaker Project
7
 * @copyright       2009-2011, Website Baker Org. e.V.
8
 * @link			http://www.websitebaker2.org/
9
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11
 * @requirements    PHP 5.2.2 and higher
12
 * @version         $Id$
13
 * @filesource		$HeadURL$
14
 * @lastmodified    $Date$
15
 *
16
 */
17
18
// Must include code to stop this file being access directly
19
if(defined('WB_PATH') == false) { die("Cannot access this file directly"); }
20
// Check if the user has already submitted the form, otherwise show it
21
if(isset($_POST['email']) && $_POST['email'] != "" &&
22 1551 Luisehahne
    preg_match("/([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}/i", $_POST['email']))
23
{
24 1420 Luisehahne
	$email = strip_tags($_POST['email']);
25 1551 Luisehahne
// Check if the email exists in the database
26
	$sql  = 'SELECT `user_id`,`username`,`display_name`,`email`,`last_reset`,`password` '.
27
	        'FROM `'.TABLE_PREFIX.'users` '.
28
	        'WHERE `email`=\''.$wb->add_slashes($_POST['email']).'\'';
29
	if(($results = $database->query($sql)))
30
	{
31
		if(($results_array = $results->fetchRow()))
32 1420 Luisehahne
	if($results->numRows() > 0) {
33
34 1551 Luisehahne
		{ // Get the id, username, email, and last_reset from the above db query
35 1420 Luisehahne
		// Check if the password has been reset in the last 2 hours
36 1551 Luisehahne
			if( (time() - (int)$results_array['last_reset']) < (2 * 3600) ) {
37 1420 Luisehahne
			// Tell the user that their password cannot be reset more than once per hour
38 1551 Luisehahne
				$message = $MESSAGE['FORGOT_PASS']['ALREADY_RESET'];
39
			} else {
40
				require_once(WB_PATH.'/framework/PasswordHash.php');
41
				$pwh = new PasswordHash(0, true);
42
				$old_pass = $results_array['password'];
43 1420 Luisehahne
			// Generate a random password then update the database with it
44 1551 Luisehahne
				$new_pass = $pwh->NewPassword();
45
				$sql = 'UPDATE `'.TABLE_PREFIX.'users` '.
46
				       'SET `password`=\''.$pwh->HashPassword($new_pass, true).'\', '.
47
				           '`last_reset`='.time().' '.
48
				       'WHERE `user_id`='.(int)$results_array['user_id'];
49
				unset($pwh); // destroy $pwh-Object
50
				if($database->query($sql))
51
				{ // Setup email to send
52
					$mail_to = $email;
53
					$mail_subject = $MESSAGE['SIGNUP2_SUBJECT_LOGIN_INFO'];
54 1420 Luisehahne
				// Replace placeholders from language variable with values
55 1551 Luisehahne
					$search = array('{LOGIN_DISPLAY_NAME}', '{LOGIN_WEBSITE_TITLE}', '{LOGIN_NAME}', '{LOGIN_PASSWORD}');
56
					$replace = array($results_array['display_name'], WEBSITE_TITLE, $results_array['username'], $new_pass);
57
					$mail_message = str_replace($search, $replace, $MESSAGE['SIGNUP2_BODY_LOGIN_FORGOT']);
58 1420 Luisehahne
				// Try sending the email
59 1551 Luisehahne
					if($wb->mail(SERVER_EMAIL,$mail_to,$mail_subject,$mail_message)) {
60
						$message = $MESSAGE['FORGOT_PASS_PASSWORD_RESET'];
61
						$display_form = false;
62
					}else { // snd mail failed, rollback
63
						$sql = 'UPDATE `'.TABLE_PREFIX.'users` '.
64
						       'SET `password`=\''.$old_pass.'\' '.
65
						       'WHERE `user_id`='.(int)$results_array['user_id'];
66
						$database->query($sql);
67
						$message = $MESSAGE['FORGOT_PASS_CANNOT_EMAIL'];
68
					}
69
				}else { // Error updating database
70
					$message = $MESSAGE['RECORD_MODIFIED_FAILED'];
71
					if(DEBUG) {
72
						$message .= '<br />'.$database->get_error();
73
						$message .= '<br />'.$sql;
74
					}
75 1420 Luisehahne
				}
76
			}
77 1551 Luisehahne
		}else { // no record found - Email doesn't exist, so tell the user
78
			$message = $MESSAGE['FORGOT_PASS_EMAIL_NOT_FOUND'];
79 1420 Luisehahne
		}
80 1551 Luisehahne
	} else { // Query failed
81
		$message = 'SystemError:: Database query failed!';
82
		if(DEBUG) {
83
			$message .= '<br />'.$database->get_error();
84
			$message .= '<br />'.$sql;
85
		}
86 1420 Luisehahne
	}
87
} else {
88
	$email = '';
89
}
90
91 1551 Luisehahne
if(isset($message) && $message != '') {
92 1420 Luisehahne
	$message = $MESSAGE['FORGOT_PASS']['NO_DATA'];
93 1551 Luisehahne
	$message_color = 'FF0000';
94
} else {
95
	$message = $MESSAGE['FORGOT_PASS_NO_DATA'];
96 1420 Luisehahne
	$message_color = '000000';
97
}
98 1509 Luisehahne
99
$_SESSION['PAGE_LINK'] = get_page_link( $_SESSION['PAGE_ID'] );
100
$_SESSION['HTTP_REFERER'] = page_link($_SESSION['PAGE_LINK']);
101 1420 Luisehahne
102
?>
103 1509 Luisehahne
<div style="margin: 1em auto;">
104
	<button type="button" value="cancel" onClick="javascript: window.location = '<?php print $_SESSION['HTTP_REFERER'] ?>';"><?php print $TEXT['CANCEL'] ?></button>
105
</div>
106 1420 Luisehahne
<h1 style="text-align: center;"><?php echo $MENU['FORGOT']; ?></h1>
107
<form name="forgot_pass" action="<?php echo WB_URL.'/account/forgot.php'; ?>" method="post">
108
	<input type="hidden" name="url" value="{URL}" />
109
		<table cellpadding="5" cellspacing="0" border="0" align="center" width="500">
110
		<tr>
111
			<td height="40" align="center" style="color: #<?php echo $message_color; ?>;" colspan="2">
112
			<?php echo $message; ?>
113
			</td>
114
		</tr>
115 1551 Luisehahne
<?php if(!isset($display_form) OR $display_form != false) { ?>
116 1420 Luisehahne
		<tr>
117
			<td height="10" colspan="2"></td>
118
		</tr>
119
		<tr>
120
			<td width="165" height="30" align="right"><?php echo $TEXT['EMAIL']; ?>:</td>
121
			<td><input type="text" maxlength="255" name="email" value="<?php echo $email; ?>" style="width: 180px;" /></td>
122
			<td><input type="submit" name="submit" value="<?php echo $TEXT['SEND_DETAILS']; ?>" style="width: 180px; font-size: 10px; color: #003366; border: 1px solid #336699; background-color: #DDDDDD; padding: 3px; text-transform: uppercase;" /></td>
123
		</tr>
124 1551 Luisehahne
<?php } ?>
125 1420 Luisehahne
		</table>
126 239 stefan
</form>