<?php 
session_start();

$pagetitle = "Account Settings";
include('config.php');

    $length = 10;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $newpass = "";    
    for ($p = 0; $p < $length; $p++) {
        $newpass .= $characters[mt_rand(0, strlen($characters))];
}

	$sitequery = 'select * from settings;';
	$siteresult = mysql_query($sitequery,$connection) or die(mysql_error());
	
	//Create site settings variables		
	$siteinfo = mysql_fetch_array($siteresult);
	$sitetitle = $siteinfo['title'];
	$siteurl = $siteinfo['url'];

	$adminquery = 'select * from admins;';
	$adminresults = mysql_query($adminquery,$connection) or die(mysql_error());
	$admininfo = mysql_fetch_array($adminresults);
	$adminemail = $admininfo['email'];

// Update the settings 
if(isset($_POST['getpass'])) { 
	
	$email = $_POST['email'];
	$checkforemails = mysql_query("SELECT * FROM authors WHERE email = '".$email."'");
	
if(mysql_num_rows($checkforemails) != 0){
	$query = "select * from authors where email ='".$email."'";
	$result = mysql_query($query,$connection) or die(mysql_error());
	$info = mysql_fetch_array($result);
	$id = $info['id'];
	$username = $info['username'];

	$sql = "UPDATE `authors` SET `password`='".md5($newpass)."' WHERE `id`=".$id;
	$query = mysql_query($sql);
			
	// send e-mail to ...
	$to=$email; 

	// Your subject 
	$subject="Temporary Password for ".$sitetitle; 

	// From 
	$header="from: Admin <".$adminemail.">"; 

	// Your message 
	$messages ="Your temporary password is $newpass \r\n\n";
	$messages.="You may now login  and update your password from the Account Settings page: \r\n";
	$messages.="Username: $username \r\n";
	$messages.="Password: $newpass \r\n";
	$messages.= $siteurl."/login.php \r\n";

	// send email 
	$sentmail = mail($to,$subject,$messages,$header);

			
	header('Location: forgot.php?emailed=true');
	exit();
	} else {
	header('Location: forgot.php?error=noemail');
	}
	
} 


include('header.php'); ?>



<?php include('sidebar.php'); ?>

<!-- CONTENT AREA -->
<div id="content-full">	


<h1>Password Recovery</h1>

<br/>

<!-- Confirmations -->

<?php if($_GET["error"] == "noemail") {
	echo '<div class="warning"><b>ERROR: That username doesn\'t exist in our system</b></div>';
} elseif($_GET["emailed"] == "true") {
	echo '<div class="warning"><b>A new password has been emailed to you</b></div>';
}

?>



<!-- SETTINGS UPDATE -->
<table>
<tr><td valign="top" style="width:420px; padding:0 20px 0 10px; border-right: 3px solid #F3F1E9;">

<form method="post" action="forgot.php">
<table>
<tr><td>Email Address:</td>
<td><input type="text" name="email" value="<?php echo $email; ?>" style="width:200px;"></td></tr>
<tr><td colspan="2" align="center">
<br/><input type="submit" id="submitstyle" value="Recover"></td></tr>
<input name="getpass" type="hidden" id="getpass" />
</table></form>

</td>
<td style="padding:0 0 0 20px;" valign="top">
<p><b>Note:</b><br/><br/>
Enter your email address to the left and a new auto-generated password will be emailed to you. You may then login to your account and update your password from the Account Settings page.</p>

</td></tr></table>
<br/>

<div style="clear:both; width: 100%; padding-top: 15px; border-bottom: 3px solid #F3F1E9;"></div>


</div> <!-- End Content -->


<?php include('footer.php'); ?>