<?php
# ---------------------------------------------------------------------
# truc is a tool for requirement and use case tracking
# Copyright (C) 2006 ASDIS - http://sf.net/projects/truc
#
# (rth) Initial truc version based on rth
#       Copyright (C) 2005 George Holbrook - rth@lists.sourceforge.net
#
# This program is distributed under the terms and conditions of the GPL
# See the README and LICENSE files for details
#----------------------------------------------------------------------

include"./api/include_api.php";
include"./api/rndPass.class.php";

html_window_title();
html_print_body();
html_page_title( lang_get('forgot_password_page') . PAGE_TITLE );

print"<div align=center>";

error_report_check( $_GET );

print"<br>";


# if user requests password to be reset
if( isset($_POST['email']) ) {

	$email			= $_POST['email'];

	$user_details	= user_get_info_by_email($email);

	# if user does not exist
	if( !$user_details ) {
		error_report_show( "login_reset_password_page.php", ERROR_ACCOUNT_NOT_FOUND );
	}

	# create reset link
	$reset		= new rndPass(25);
	$reset_link	= $reset->PassGen();

	# add reset link to the database
	user_new_reset_password($email, $reset_link);

	$url		= TRUC_URL."login_reset_password_action.php?reset_link=$reset_link";

	$subject	 = "TRUC: Reset Password Request";
	$message	 = "Someone has requested your TRUC password to be reset. If it was not you, please ignore this email.\n\n";
	$message	.= "If you do want to reset your password, please click the link below:\n";
	$message	.= "$url";

	email_send( $recipients=array($email), $subject, $message, $headers="From: TRUC:Admin\r\n" );

	print lang_get( "new_reset_password" );

# if user clicks the reset link in email
} elseif( isset($_GET['reset_link']) ) {

	$reset_link		= $_GET['reset_link'];

	# create new password
	$password		= new rndPass(6);
	$new_password	= $password->PassGen();

	# reset password and return users email address
	$email			= user_reset_password($reset_link, $new_password);

	# if reset password was successful, send out email with new password details
	if( $email ) {

		$user_details	= user_get_info_by_email($email);
		$username		= $user_details[USER_UNAME];

		$url		= TRUC_URL."login.php";

		$subject	 = "TRUC: Password has been Reset";
		$message	 = "Your TRUC password has been reset.\n\n";
		$message	.= "Username: $username\n";
		$message	.= "Password: $new_password\n\n";
		$message	.= "You may change your password by clicking '".lang_get('user_link')."' on the TRUC menu.\n\n";
		$message	.= "Click the following link to login to TRUC:\n";
		$message	.= "$url";

		email_send( $recipients=array($email), $subject, $message, $headers="From: TRUC:Admin\r\n" );

		print lang_get( "reset_password" );

	} else {

		error_report_show( "login.php", ERROR_CANNOT_RESET_PASSWORD );
	}
}


print"</div>";

print"<br>";

//auth_authenticate_user();

html_print_footer();
?>