Project

General

Profile

1 4 ryan
<?php
2
3 40 stefan
// $Id$
4 4 ryan
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8 915 Ruebenwurz
 Copyright (C) 2004-2009, 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
// Include the configuration file
27
require('../../../config.php');
28
// Include the language file
29
require(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
30
// Include the database class file and initiate an object
31
require(WB_PATH.'/framework/class.admin.php');
32 258 ryan
$admin = new admin('Start', 'start', false, false);
33 4 ryan
$database = new database();
34
35
// Get the website title
36
$results = $database->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'title'");
37
$results = $results->fetchRow();
38
$website_title = $results['value'];
39
40
// Check if the user has already submitted the form, otherwise show it
41
if(isset($_POST['email']) AND $_POST['email'] != "") {
42
43 1115 Ruebenwurz
	$email = htmlspecialchars($_POST['email'],ENT_QUOTES);
44 4 ryan
45
	// Check if the email exists in the database
46 293 stefan
	$query = "SELECT user_id,username,display_name,email,last_reset,password FROM ".TABLE_PREFIX."users WHERE email = '".$admin->add_slashes($_POST['email'])."'";
47 4 ryan
	$results = $database->query($query);
48
	if($results->numRows() > 0) {
49
50
		// Get the id, username, email, and last_reset from the above db query
51
		$results_array = $results->fetchRow();
52
53
		// Check if the password has been reset in the last 2 hours
54
		$last_reset = $results_array['last_reset'];
55 1072 Ruebenwurz
		$time_diff = time()-$last_reset; // Time since last reset in seconds
56 4 ryan
		$time_diff = $time_diff/60/60; // Time since last reset in hours
57
		if($time_diff < 2) {
58
59
			// Tell the user that their password cannot be reset more than once per hour
60
			$message = $MESSAGE['FORGOT_PASS']['ALREADY_RESET'];
61
62
		} else {
63
64 293 stefan
			$old_pass = $results_array['password'];
65
66 4 ryan
			// Generate a random password then update the database with it
67
			$new_pass = '';
68
			$salt = "abchefghjkmnpqrstuvwxyz0123456789";
69
			srand((double)microtime()*1000000);
70
			$i = 0;
71
			while ($i <= 7) {
72
				$num = rand() % 33;
73
				$tmp = substr($salt, $num, 1);
74
				$new_pass = $new_pass . $tmp;
75
				$i++;
76
			}
77
78 1072 Ruebenwurz
			$database->query("UPDATE ".TABLE_PREFIX."users SET password = '".md5($new_pass)."', last_reset = '".time()."' WHERE user_id = '".$results_array['user_id']."'");
79 4 ryan
80
			if($database->is_error()) {
81
				// Error updating database
82
				$message = $database->get_error();
83
			} else {
84
				// Setup email to send
85
				$mail_to = $email;
86 921 doc
				$mail_subject = $MESSAGE['SIGNUP2']['SUBJECT_LOGIN_INFO'];
87
88
				// Replace placeholders from language variable with values
89
				$search = array('{LOGIN_DISPLAY_NAME}', '{LOGIN_WEBSITE_TITLE}', '{LOGIN_NAME}', '{LOGIN_PASSWORD}');
90
				$replace = array($results_array['display_name'], WEBSITE_TITLE, $results_array['username'], $new_pass);
91
				$mail_message = str_replace($search, $replace, $MESSAGE['SIGNUP2']['BODY_LOGIN_INFO']);
92
93 4 ryan
				// Try sending the email
94 344 stefan
				if($admin->mail(SERVER_EMAIL,$mail_to,$mail_subject,$mail_message)) {
95 4 ryan
					$message = $MESSAGE['FORGOT_PASS']['PASSWORD_RESET'];
96
					$display_form = false;
97
				} else {
98 293 stefan
					$database->query("UPDATE ".TABLE_PREFIX."users SET password = '".$old_pass."' WHERE user_id = '".$results_array['user_id']."'");
99 4 ryan
					$message = $MESSAGE['FORGOT_PASS']['CANNOT_EMAIL'];
100
				}
101
			}
102
103
		}
104
105
	} else {
106
		// Email doesn't exist, so tell the user
107
		$message = $MESSAGE['FORGOT_PASS']['EMAIL_NOT_FOUND'];
108 1115 Ruebenwurz
		// and delete the wrong Email
109
		$email = '';
110 4 ryan
	}
111
112
} else {
113
	$email = '';
114
}
115
116
if(!isset($message)) {
117
	$message = $MESSAGE['FORGOT_PASS']['NO_DATA'];
118
	$message_color = '000000';
119
} else {
120
	$message_color = 'FF0000';
121
}
122
123
// Setup the template
124 944 Ruebenwurz
$template = new Template(THEME_PATH.'/templates');
125
$template->set_file('page', 'login_forgot.htt');
126 4 ryan
$template->set_block('page', 'main_block', 'main');
127
if(defined('FRONTEND')) {
128
	$template->set_var('ACTION_URL', 'forgot.php');
129
} else {
130
	$template->set_var('ACTION_URL', 'index.php');
131
}
132
$template->set_var('EMAIL', $email);
133
134
if(isset($display_form)) {
135
	$template->set_var('DISPLAY_FORM', 'none');
136
}
137
138
$template->set_var(array(
139
								'SECTION_FORGOT' => $MENU['FORGOT'],
140
								'MESSAGE_COLOR' => $message_color,
141
								'MESSAGE' => $message,
142
								'WB_URL' => WB_URL,
143
								'ADMIN_URL' => ADMIN_URL,
144 944 Ruebenwurz
								'THEME_URL' => THEME_URL,
145 1081 Ruebenwurz
								'LANGUAGE' => strtolower(LANGUAGE),
146 4 ryan
								'TEXT_EMAIL' => $TEXT['EMAIL'],
147
								'TEXT_SEND_DETAILS' => $TEXT['SEND_DETAILS'],
148
								'TEXT_HOME' => $TEXT['HOME'],
149
								'TEXT_NEED_TO_LOGIN' => $TEXT['NEED_TO_LOGIN']
150
								)
151
						);
152
153
if(defined('FRONTEND')) {
154
	$template->set_var('LOGIN_URL', WB_URL.'/account/login.php');
155
} else {
156
	$template->set_var('LOGIN_URL', ADMIN_URL);
157
}
158
$template->set_var('INTERFACE_URL', ADMIN_URL.'/interface');
159
160 333 stefan
if(defined('DEFAULT_CHARSET')) {
161
	$charset=DEFAULT_CHARSET;
162
} else {
163
	$charset='utf-8';
164
}
165
166
$template->set_var('CHARSET', $charset);
167
168 4 ryan
$template->parse('main', 'main_block', false);
169
$template->pparse('output', 'page');
170
171 399 Ruebenwurz
?>