<?php
require_once dirname(__FILE__) . '/../config/config.php';

if($_POST['action'] == 'restore_my_login') {
	require dirname(__FILE__) . '/../src/plugins/class.phpmailer.php';
	require dirname(__FILE__) . '/../src/plugins/class.smtp.php';

	$email = mysqli_real_escape_string($mysqli, $_POST['email']);

	$get_account_info = $mysqli->query("SELECT * FROM $table_accounts WHERE email='$email'");
	if($get_account_info->num_rows == 1) {
		$row = $get_account_info->fetch_assoc();
		$reset_message = 'Hi,<br><br>
		Please click this link to login and reset your password:<br><br> <a href="' . $site_url . 'admin/action.php?a=' . $row['password'] . '&b=' . $row['email'] . '&action=reset">' . $site_url . 'admin/action.php?a=' . $row['password'] . '&b=' . $row['email'] . '&action=reset</a>';

		if($email_server == 'SMTP') {
		    //Sending the email to the sender of the file
		    $mail             = new PHPMailer();

		    $body             = eregi_replace("[\]",'',$reset_message);

		    $mail->IsSMTP(); // telling the class to use SMTP
		    $mail->Host       = $smtp_host;
		    $mail->SMTPAuth   = $smtp_auth;
		    $mail->SMTPSecure = $smtp_secure;                       
		    $mail->Port       = $smtp_port;
		    $mail->Username   = $smtp_username;  
		    $mail->Password   = $smtp_password; 

		    $mail->From = $email_from_email;
		    $mail->FromName = $email_from_name;

		    $mail->AddReplyTo($email_from_email, $email_from_name);

		    $mail->Subject    = "Password reset";

		    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
		     
		    $mail->MsgHTML($body);
		     
		    $mail->AddAddress($email, 'Droppy admin');

		    $mail->Send();
		}
		else
		{
			//Sending the email to the sender of the file
		    $mail             = new PHPMailer();

		    $body             = eregi_replace("[\]",'',$reset_message);

		    $mail->From = $email_from_email;
		    $mail->FromName = $email_from_name;

		    $mail->AddReplyTo($email_from_email, $email_from_name);

		    $mail->Subject    = "Password reset";

		    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
		     
		    $mail->MsgHTML($body);
		     
		    $mail->AddAddress($email, 'Droppy User');

		    $mail->Send();
		}
		header('Location: login.php?reset=sent');
	}
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Admin login">
    <meta name="author" content="Proxibolt">
    <meta name="keyword" content="">

    <title><?php echo $site_name; ?> | Login</title>

	<!-- Bootstrap core CSS -->
    <link href="../src/css/bootstrap/bootstrap.css" rel="stylesheet">
    <!--external css-->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="../src/plugins/lineicons/style.css">   
    
    <!-- Custom styles for this template -->
    <link href="../src/css/admin.css" rel="stylesheet">
    <link href="../src/css/admin-responsive.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>

    <![endif]-->
  </head>

  <body>

      <!-- **********************************************************************************************************************************************************
      MAIN CONTENT
      *********************************************************************************************************************************************************** -->

	<div id="login-page">
	  	<div class="container">
	  		<?php
	  		if($_GET['page'] == 'restore'):
	  		?>
	  		<form class="form-login" action="login.php" method="POST">
			    <h2 class="form-login-heading">Reset login</h2>
			    <div class="login-wrap">
			    	<input type="hidden" name="action" value="restore_my_login">
		            <input type="text" name="email" class="form-control" placeholder="E-Mail" autofocus>
		            <br>
		            <input type="submit" class="btn btn-theme btn-block" value="RESTORE">
		        </div>			
	      	</form>
	  		<?php
	  		else:
	  		?>
		    <form class="form-login" action="action.php" method="POST">
			    <h2 class="form-login-heading">Admin panel</h2>
			    <div class="login-wrap">
			    	<input type="hidden" id="action" name="action" value="login">
		            <input type="text" id="email" name="email" class="form-control" placeholder="E-Mail" autofocus>
		            <br>
		            <input type="password" id="password" name="password" class="form-control" placeholder="Password">
                    <input type="text" name="validate_id" style="visibility: hidden;">
		            <br>
		            <input type="submit" class="btn btn-theme btn-block" value="SIGN IN">
		            <p style="text-align: right; padding-top: 10px;"><a href="login.php?page=restore">Lost password</a></p>
		        </div>			
	      	</form>
	      	<?php
	      	endif;
	      	?>
	      	<div id="errorDiv" style="width: 330px; text-align:center; margin-left: auto; margin-right: auto; padding-top: 10px;"><?php if($_GET['reset'] == 'sent') { echo '<div class="alert alert-success" role="alert">Reset email has been sent</div>'; } elseif($_GET['e'] == 'wrong') { echo '<div class="alert alert-danger" role="alert">Invalid login details !</div>'; } ?></div> 	 		  
	  	</div>
	</div>

    <!-- js placed at the end of the document so the pages load faster -->
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="../src/js/admin/bootstrap.min.js"></script>

    <!--BACKSTRETCH-->
    <!-- You can use an image of whatever size. This script will stretch to fit in any screen size.-->
    <script type="text/javascript" src="../src/js/admin/jquery.backstretch.min.js"></script>
    <script>
        $.backstretch("../src/images/bg-admin.jpg", {speed: 50});
    </script>


  </body>
</html>
