Project

General

Profile

1 4 ryan
<?php
2
3 17 stefan
// $Id$
4 4 ryan
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8 310 ryan
 Copyright (C) 2004-2006, Ryan Djurovich
9 4 ryan
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24
*/
25
26
if(!defined('WB_URL')) {
27 17 stefan
	header('Location: ../pages/index.php');
28 286 stefan
	exit(0);
29 4 ryan
}
30
31
// Create new database object
32
$database = new database();
33
34
// Check if the user has already submitted the form, otherwise show it
35
if(isset($_POST['email']) AND $_POST['email'] != "") {
36
37
	$email = $_POST['email'];
38
39
	// Check if the email exists in the database
40 293 stefan
	$query = "SELECT user_id,username,display_name,email,last_reset,password FROM ".TABLE_PREFIX."users WHERE email = '".$wb->add_slashes($_POST['email'])."'";
41 4 ryan
	$results = $database->query($query);
42
	if($results->numRows() > 0) {
43
		// Get the id, username, and email from the above db query
44
		$results_array = $results->fetchRow();
45
46 226 ryan
		// Check if the password has been reset in the last 2 hours
47
		$last_reset = $results_array['last_reset'];
48
		$time_diff = mktime()-$last_reset; // Time since last reset in seconds
49
		$time_diff = $time_diff/60/60; // Time since last reset in hours
50
		if($time_diff < 2) {
51
52
			// Tell the user that their password cannot be reset more than once per hour
53
			$message = $MESSAGE['FORGOT_PASS']['ALREADY_RESET'];
54
55
		} else {
56 4 ryan
57 226 ryan
			// Generate a random password then update the database with it
58
			$new_pass = '';
59
			$salt = "abchefghjkmnpqrstuvwxyz0123456789";
60
			srand((double)microtime()*1000000);
61
			$i = 0;
62
			while ($i <= 7) {
63
				$num = rand() % 33;
64
				$tmp = substr($salt, $num, 1);
65
				$new_pass = $new_pass . $tmp;
66
				$i++;
67
			}
68 293 stefan
			$old_pass = $results_array['password'];
69 226 ryan
			$database->query("UPDATE ".TABLE_PREFIX."users SET password = '".md5($new_pass)."' WHERE user_id = '".$results_array['user_id']."'");
70
71
			if($database->is_error()) {
72
				// Error updating database
73
				$message = $database->get_error();
74
			} else {
75
				// Setup email to send
76
				$mail_subject = 'Your login details...';
77
				$mail_to = $email;
78
				$mail_message = ''.
79 4 ryan
'Hello '.$results_array["display_name"].',
80
81
Your '.WEBSITE_TITLE.' administration login details are:
82
Username: '.$results_array["username"].'
83
Password: '.$new_pass.'
84
85
Your password has been reset to the one above.
86
This means that your old password will no longer work.
87
88 218 ryan
If you have received this message in error, please delete it immediatly.';
89 226 ryan
				// Try sending the email
90 300 stefan
				if($wb->mail('',$mail_to,$mail_subject,$mail_message)) {
91 226 ryan
					$message = $MESSAGE['FORGOT_PASS']['PASSWORD_RESET'];
92
					$display_form = false;
93
				} else {
94 293 stefan
  					$database->query("UPDATE ".TABLE_PREFIX."users SET password = '".$old_pass."' WHERE user_id = '".$results_array['user_id']."'");
95 226 ryan
					$message = $MESSAGE['FORGOT_PASS']['CANNOT_EMAIL'];
96
				}
97 4 ryan
			}
98 226 ryan
		}
99 4 ryan
	} else {
100
		// Email doesn't exist, so tell the user
101
		$message = $MESSAGE['FORGOT_PASS']['EMAIL_NOT_FOUND'];
102
	}
103
104
} else {
105
	$email = '';
106
}
107
108
if(!isset($message)) {
109
	$message = $MESSAGE['FORGOT_PASS']['NO_DATA'];
110
	$message_color = '000000';
111
} else {
112
	$message_color = 'FF0000';
113
}
114
115
?>
116
<h1 style="text-align: center;"><?php echo $MENU['FORGOT']; ?></h1>
117
118
<form name="forgot_pass" action="<?php echo WB_URL.'/account/forgot'.PAGE_EXTENSION; ?>" method="post">
119
	<input type="hidden" name="url" value="{URL}" />
120
		<table cellpadding="5" cellspacing="0" border="0" align="center" width="500">
121
		<tr>
122
			<td height="40" align="center" style="color: #<?php echo $message_color; ?>;" colspan="2">
123
			<?php echo $message; ?>
124
			</td>
125
		</tr>
126
		<?php if(!isset($display_form) OR $display_form != false) { ?>
127
		<tr>
128
			<td height="10" colspan="2"></td>
129
		</tr>
130
		<tr>
131
			<td width="165" height="30" align="right"><?php echo $TEXT['EMAIL']; ?>:</td>
132
			<td><input type="text" maxlength="30" name="email" value="<?php echo $email; ?>" style="width: 180px;" /></td>
133
		</tr>
134
		<tr height="30">
135
			<td>&nbsp;</td>
136
			<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>
137
		</tr>
138
		<tr style="display: {DISPLAY_FORM}">
139
			<td height="10" colspan="2"></td>
140
		</tr>
141
		<?php } ?>
142
		</table>
143 239 stefan
</form>