<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

include("settings.inc.php");
include("functions.inc.php");
include('sessions.inc.php');
db_connect($db_host, $db_user, $db_pass);
sessao_start();

$miscf = $default_language."/misc.inc.php";
include(dirname(__FILE__) . "/languages/$miscf");

$strSkinName = '/skins/skin_1';

if(array_key_exists('username', $_POST))
{
	
	
	//sessao_start();
	
	if(!get_magic_quotes_gpc())
	{
		$_POST['username'] = addslashes($_POST['username']);
	}
	
	
	$redir = '';

	// get account info
	$account_info = array();
	if ($account_info = checkLogin($_POST['username'])){
		$type = array_key_exists('type', $account_info) ? $account_info['type'] : 0;
		$email = array_key_exists('email', $account_info) ? $account_info['email'] : '';
	}
	if (!$account_info) {
		// to avoid using this feature to  check which users exist
		$_SESSION['_error'] = ltext('lost_pwd_nomail');
	}
	elseif ($account_info['locked']) {
		// do not allow locked users to generate temporary passwords
		$_SESSION['_error'] = ltext('lost_pwd_nomail'); // like there are no such a user
	}
	elseif(!$type || !trim($_POST['formemail']))
	{
		// improper email 
		$_SESSION['_error'] = ltext('lost_pwd_nomail');
	}
	elseif ($type == USERTYPE_FTPUSER || $type == USERTYPE_POPUSER)
	{
		// can not send temporary password to ftp/pop3 users 
		// due to they have no email address specified
		$_SESSION['_error'] = ltext('lost_pwd_ftppopuser');
	}
	elseif($type == USERTYPE_ADMIN)
	{
		// can not send temporary password for admin 
		// due to we have no admin email address specified
		$_SESSION['_error'] = ltext('lost_admin_pwd');
	}
	else 
	{
		if($email == $_POST['formemail']){
			$newpw = mkpasswd(8);
			if ( !$confixx_user ) 
			  $confixx_user = 'confixx';  

			$from = $confixx_user.'@'.$hostname;	
			$mailtext = "From: \"Confixx on $hostname\" <$from>".
			"\n\n".ltext('lost_pwd_mail', array($_POST['username'], $newpw))."\n\n";

			if(@mail($email, ltext('lost_pwd_mail_subj'), "",$mailtext,"-f$from")) {
			  safe_query2( "INSERT INTO pwdreminder (user, password, usertype) VALUES ( ?, ?, ? )",
										 new SQLString( $_POST['username'] ), new SQLString( $newpw ), new SQLString( $type) );
			  $_SESSION['_error'] = ltext('lost_pwd_sent');
			}
			$redir = 'index.php';
		}
		else 
		{
			//$_SESSION['_error'] = ltext('lost_pwd_nomail');
			if($type == USERTYPE_RESELLER)
			{
				$_SESSION['_error'] = ltext('lost_pwd_nomail_reseller', array($_POST['username']));
			}
			if($type == USERTYPE_USER)
			{
				$_SESSION['_error'] = ltext('lost_pwd_nomail_user', array($_POST['username']));
			}
		}
	}
	
	if($redir)
	{
		db_close();
		header('Location: ' . $redir);
		exit();
	}
	
}

?>
<HTML>
<HEAD>
<META content="text/html; charset=<?=$lang_html_charset?>" http-equiv=Content-Type>
<LINK href="<?=$strSkinName?>/style.css" rel=stylesheet type=text/css>
</HEAD>

<BODY bgColor=#ffffff bottomMargin=0 leftMargin=0 topMargin=0 MARGINHEIGHT="0" MARGINWIDTH="0" onload="document.loginform.username.focus()">
<form name="topredirect" action="/lostpwd.php" target="_top" method="GET"></form>
<script>
if(window != parent)
{
	document.topredirect.submit();
}
</script>
<br>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=400 align="center">
<TR> 
	<TD valign="top" align="center"><img src="/loginscreen.gif" width="396" height="85"></TD>
</TR>
<? if($_SESSION['_error']): ?>
<TR>
	<TD align="center"><br><p><?=$_SESSION['_error']?></p></TD>
</TR>
<? $_SESSION['_error'] = false; endif; ?>
<TR><TD><fieldset><legend><?=ltext('lost_pwd_forgot')?></legend>
<?=ltext('lost_pwd_forgot_warning')?>
<TABLE border=0 cellPadding=2 cellSpacing=2 align="center" width="100%">
<FORM name="loginform" action="/lostpwd.php" method="POST" target="_top">
<TR>
	<TD width="20%" align="right"><?=ltext('form_username')?></TD>
	<TD width="80%"><INPUT type="text" name="username"></TD>
</TR>
<TR>
	<TD width="20%" align="right"><?=ltext('form_email')?></TD>
	<TD width="80%"><INPUT type="text" name="formemail"></TD>
</TR>
<TR>
	<TD colspan="2" align="right">	<table border="0">
	<tr>
	<td><?=std_button(ltext('form_sent_pwd'), 'javascript:document.forms[\'loginform\'].submit()')?></td>
	<td><?=std_button(ltext('form_cancel'), 'javascript:location.href=\'index.php\'')?></td>
	</tr>
	</table></TD>
</TR>
</FORM>
</TABLE></fieldset>
</TD></TR>
</TABLE>
</BODY>
</HTML>
<?
db_close();
?>
