<?php
/*
	File     : lostPassword.php
	Version  : 0.2.1 Beta
	Copyright: Attila Soki 2001
	e-mail   : atiware@gmx.net
	Date     : 2001-05-12
*/

	if (strlen($cancel)>0) {
		header("Location: index.php?logout=1");
		printf('<a href="index.php?logout=1">Your browser does not support Header:Location tag. Please CLICK HERE to continue</a>');
		die;
	}
	define("INVERTCOLOR","#F2F2E0");
	
	include_once("Admin.class.php");
	function TfColor($error,$control) {
		global $invert,$invert2;
		if ($error&$control)
			$retval=' bgcolor="'.ERRORCOLOR.'"';
		elseif ($invert>0)
			$retval=' bgcolor="'.INVERTCOLOR.'"';
		else
			$retval="";
		$invert*=-1;
		return $retval;
	}

	$sent=false;
	$invert=-1;
	$error=0;
	if (strlen($submitButton)>0) {
		$tfusername=trim($tfusername);
		$tfemail=trim($tfemail);
		if (strlen($tfusername)==0)
			$error|=1;
		if (strlen($tfemail)==0)
			$error|=2;
		if (!$error) {
			$admin=new Admin();
			$dbo=new MySql();
			$dbo->query("select Id from ".MMCADMINSTABLE." where Email='".$tfemail."' and Username='".$tfusername."'");
			if ($dbo->numRows()) {
				$mr=$dbo->fetchArray();
				$admin=new Admin($mr["Id"]);
				$dbo->freeResult();
			}
			else {
				$error|=1;
				$error|=2;
			}
			$dbo->close();
			if (($admin->isValid()) && (!$error)) {
				$newpass=uniqid(rand());
				if ((DEMOMODE) && ($admin->getId()==1)) {
					;
				}
				else
					$admin->setPassword($newpass);
				$text="You receive this e-mail because your password was changed in MySql Based Exim/QPopper Webadmin system.\n";
				$text.="Please log in, and change your password.\n";
				$text.="Your username:".$tfusername."\n";
				$text.="Your new password:".$newpass."\n";
				$text.="\n";
				$text.="Modification date:".date("d/m/Y H:i:s",time())."\n";
				$text.="Client IP:".$REMOTE_ADDR."\n";
				$text.="Client agent:".$HTTP_USER_AGENT."\n";
				$text.="Server name:".$SERVER_NAME."\n";
				$text.="HTTP Host:".$HTTP_HOST."\n";
				$text.="\n";
				if ((DEMOMODE) && ($admin->getId()==1)) {
					;
				}
				else
					mail($tfemail,"Password reminder. MySql Based Exim/QPopper Webadmin",$text);
				$admin=null;
				$sent=true;
				$text=null;
			}
		}
		
	}
	$headline="Lost passwords?";
	if (DEMOMODE) {
		$headline="<i>(In demo mode this site do nothing on admin 1)</i> ".$headline;
	}
	require("head.inc.php");
?>
  <tr> 
    <td colspan="2" align="left" valign="top"> 
<?php
	if (!$sent) {
?>
      <form method="post" action="lostPassword.php" name="form1" ENCTYPE="multipart/form-data">
        <table width="100%" border="0" cellspacing="0" cellpadding="0" vspace="0" hspace="0">
          <tr> 
            <td colspan="2" bgcolor="#0099CC"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><b><font size="2"><?php echo $headline ?></font></b></font></div>
            </td>
          </tr>
          <tr> 
            <td colspan="2" bgcolor="#FFFFCC"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><b><font size="2">Enter your username 
              and email address, if the email address you enter is the same as the one we have on record, we will generate a new password for you, and your new password will be mailed to you.
              </font></b></font></div>
            </td>
          </tr>
          <tr <?php echo TfColor($error,1) ?>> 
            <td width="50%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Username:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfusername" size="40" maxlength="63" value="<?php echo $tfusername ?>">
              </font></td>
          </tr>
          <tr <?php echo TfColor($error,2) ?>> 
            <td width="50%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">E-mail:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfemail" size="40" maxlength="63" value="<?php echo $tfemail ?>">
              </font></td>
          </tr>
          <tr> 
            <td colspan="2" bgcolor="#FFFFCC" align="center" valign="middle"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
                <input type="submit" name="submitButton" value="Send">
                <input type="submit" name="cancel" value="Cancel">
                </font></div>
            </td>
          </tr>
                    <tr> 
            <td colspan="2"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><b><font size="2">
               <img src="images/norights.gif" alt="Security">
              </font></b></font></div>
            </td>
          </tr>
          <tr> 
            <td colspan="2" bgcolor="#FFFFCC"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><b><font size="2">Or contact one of these admins via e-mail.
              </font></b></font></div>
            </td>
          </tr>
<?php
	$dbo=new MySql();
	$dbo->query("select Id from ".MMCADMINSTABLE." where Root>0");
	if ($dbo->numRows()) {
		while ($mr=$dbo->fetchArray()) {
			$admin=new Admin($mr["Id"]);
			if (strlen($admin->getEmail())!=0) {
?>
          <tr>
            <td colspan="2"<?php echo TfColor($error,0) ?>> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><b><font size="2">
               <a href="mailto:<?php echo $admin->getEmail() ?>"><?php echo $admin->getEmail() ?></a>
              </font></b></font></div>
            </td>
          </tr>
<?php
			}
		}
		$dbo->freeResult();
	}
	$dbo->close();
?>
        </table>
      </form>
<?php
	}
	else  {
?>
		<div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><a href="index.php?logout=1">
		Your password is successfully changed. Click here to continue...
		</a></b></font></div>
<?php
	}
?>
    </td>
  </tr>
<?php
	require("foot.inc.php");
?>