<?php
require_once "SystemController/DbConnection.php";
require_once 'en/php/class.user.php';
$db_handle= new DbConnection();
$msg = "";
$email = "";
if(isset($_POST['recover'])){

  $email = $db_handle->sanitize($_POST['email']);

 $userExist = $db_handle->runBaseQuery("SELECT * FROM members WHERE email = '$email'");

if($userExist){
$msg = "<div class='alert alert-success mx-auto'><i class='fa fa-info-circle'></i> 
          A link to reset your password has been sent to your email address. Follow this link to reset your password
        
        </div>";



$reg_user = new User();
$time = time();
$token = bin2hex(random_bytes(50));

$db_handle->insertRecord("INSERT into password_reset (email,hash,timeout) VALUES (?,?,?)", "ssi", array($email,$token, $time));
    $messag = '<div style="font-family:Times new roman,HelveticaNeue-Light,Arial,sans-serif;background-color:#404040">
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#EAFFF5">
<tbody>
<tr>
<td>
<table align="center" width="770px" border="0" cellspacing="0" cellpadding="0" bgcolor="#EAFFF5" style="width:750px!important">
<tbody>
<tr>
<td>
<table width="690" align="center" border="0" cellspacing="0" cellpadding="0" bgcolor="#EAFFF5">
<tbody>
<tr><td colspan="3" height="10"></td></tr>
<tr>
<td colspan="3" height="80" align="center" border="0" cellspacing="0" cellpadding="0" bgcolor="#EAFFF5" style="padding:
0;margin:0;font-size:0;line-height:0">
<table width="690" align="center" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="30"></td>
<td valign="top" align="center" style="font-weight:bold; font-size: 50px;padding:0;margin:0;line-height:0"><a href="https://spcxinvs.com/" target="_blank"><img src="https://spcxinvs.com/asset/images/logo.png" width="300"  alt="SpaceX Tesla Investments Trades"></a></td>
<td width="30"></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<table width="630" align="center" border="0" cellspacing="0" cellpadding="0">
<tbody>

<tr><td colspan="3" height="20"></td></tr>
</tbody>
</table>
</td>
</tr>

<tr bgcolor="#ffffff">

<td>
<table width="600" align="center" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td colspan="4" align="center">&nbsp;</td>
</tr>
<tr>
<tr>
<td colspan="3" height="40"></td></tr><tr><td colspan="5" align="left">
<p style="color:#404040;font-size:30px;line-height:50px;font-weight:bold;padding:0;margin:0">Hello!</p>
</td>
</tr>
</tr>
<tr>
<tr>
<td colspan="3" height="40"></td></tr><tr><td colspan="5" align="left">
<p style="color:#404040;font-size:30px;line-height:50px;font-weight:lighter;padding:0;margin:0">You are receiving this email because we received a password reset request for your account.</p>
</td>
</tr>
</tr>
<tr>
<td align="center">
<div style="text-align:center;width:100%;padding:40px 0">
<table align="center" cellpadding="0" cellspacing="0" style="margin:0 auto;padding:0">
<tbody>
<tr>
<td align="center" style="margin:0;text-align:center"><a href="https://spcxinvs.com/recover-password?token='. $token.'&email='.$email.'" style="font-size:22px;font-family:HelveticaNeue-Light,Arial,sans-serif;line-height:22px;text-decoration:none;color:#ffffff;font-weight:bold;border-radius:2px;background-color:#BC27F6;padding:20px 40px;display:block" target="_blank">Reset Password</a></td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
<tr>
<td width="10"></td>
<tr>
<td colspan="3" height="10"></td></tr><tr><td colspan="5" align="left">
<p style="color:#404040;font-size:30px;line-height:50px;font-weight:lighter;padding:0;margin:0">This password reset link will expire in 60minutes.</p>
</td>
</tr>
<td width="10"></td>
</tr>
<tr>
<td width="10"></td>
<tr>
<td colspan="3" height="20"></td></tr><tr><td colspan="5" align="left">
<p style="color:#404040;font-size:30px;line-height:50px;font-weight:lighter;padding:0;margin:0">If you did not request a password reset, no further action is required.</p>
</td>
</tr>
<td width="10"></td>
</tr>
<tr>
<td width="10"></td>
<tr>
<td colspan="3" height="40"></td></tr><tr><td colspan="5" align="left">
<p style="color:#404040;font-size:30px;line-height:50px;font-weight:lighter;padding:0;margin:0">Regards,</p>
</td>
</tr>
<td width="10"></td>
</tr>
<tr>
<td width="10"></td>
<tr>
<td colspan="3" height="40"></td></tr><tr><td colspan="5" align="left">
<p style="color:#404040;font-size:30px;line-height:50px;font-weight:lighter;padding:0;margin:0">SpaceX Tesla Investments Trades.</p>
</td>
</tr>
<td width="10"></td>
</tr>
<tr>
<td width="10"></td>
<tr>
<td colspan="3" height="40"></td></tr><tr><td colspan="5" align="left">
<p style="color:#404040;font-size:20px;line-height:34px;font-weight:lighter;padding:0;margin:0">If you are having trouble clicking the "Reset Password" button, copy and paste the URL below into your web browser: https://spcxinvs.com/recover-password.php?token='.$token .'&email='.$email.'</p>
</td>
</tr>
<td width="10"></td>
</tr>
<tr>
<td colspan="5" height="40" style="padding:0;margin:0;font-size:0;line-height:0"></td>
</tr>
<tr>

<td width="10"></td>
<td width="10"></td>
</tr>
<tr>
<td colspan="5" height="30" style="padding:0;margin:0;font-size:0;line-height:0"></td>
</tr>
<tr>
<td width="10"></td>
<td width="10"></td>
</tr>
<tr>
<td colspan="4">&nbsp;</td>
</tr>
</tbody>
</table>
<table width="570" align="center" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<h2 style="color:#404040;font-size:22px;font-weight:bold;line-height:26px;padding:0;margin:0">&nbsp;</h2>

</td>
</tr>
<tr>
</tr><tr><td>&nbsp;</td>
</tr></tbody></table></td>
<td width="10" bgcolor="#EAFFF5"></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>';
            $subject = "Reset Password Notification";
                  
$reg_user->send_maile($email,$messag,$subject);

}else{
  $msg = "<div class='alert alert-danger mx-auto'><i class='fa fa-info-circle'></i> 
           Email does not exist on our database. Please make sure the email account is correct!
        
        </div>";
}

}

if(isset($_POST['resetpassword'])){

$token = $_GET['token'];
$email = $_GET['email'];

$details = $db_handle->runBaseQuery("SELECT * FROM password_reset WHERE email = '$email' and hash = '$token'");

$time_requested = $details[0]['timeout'];
$current_time = time();

$time_difference = $current_time - $time_requested;
 $email = $db_handle->sanitize($_POST['email']);
  $password = $db_handle->sanitize($_POST['password']);
$confirm = $db_handle->sanitize($_POST['password2']);

if($time_difference > 3600 ){
  $msg = "<div class='alert alert-danger mx-auto'><i class='fa fa-info-circle'></i> 
           Sorry, your password reset link is expired. Please request for another one.
        </div>";
}else{

if(strlen($password) < 8){
  $msg = "<div class='alert alert-danger mx-auto'><i class='fa fa-info-circle'></i> 
           Password should consist of 8 or more characters.
        </div>";
}elseif($password != $confirm){
$msg = "<div class='alert alert-danger mx-auto' ><i class='fa fa-info-circle'></i> 
           Passwords do not match check and try again
        </div>";
}else{
 $update = $db_handle->update('UPDATE members SET password = ? WHERE email = ?', "ss", array($password, $email));

 if($update){
  $msg = "<div class='alert alert-success mx-auto' ><i class='fa fa-info-circle'></i> 
           Password has been reset successfully.
        </div>";

  $url = "login.php";
   echo '<META HTTP-EQUIV="refresh" content="3; URL='.$url.'">';
 }
}
}


 


}

?>
<!DOCTYPE html>
<html lang="en">


<head>

    <meta charset="utf-8" />
    <title>SpaceX Tesla Investments Trades</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <!-- Favicon -->
    <link rel="shortcut icon" href="images/favicon.png">

    <!-- Template CSS Files -->
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/magnific-popup.css">
    <link rel="stylesheet" href="css/select2.min.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/skins/orange.css">

    <!-- Live Style Switcher - demo only 
    <link rel="alternate stylesheet" type="text/css" title="orange" href="css/skins/orange.css" />
    <link rel="alternate stylesheet" type="text/css" title="green" href="css/skins/green.css" />
    <link rel="alternate stylesheet" type="text/css" title="blue" href="css/skins/blue.css" />-->
    <link rel="stylesheet" type="text/css" href="css/styleswitcher.css" />

    <!-- Template JS Files -->
    <script src="js/modernizr.js"></script>

</head>



<body>

    <!-- SVG Preloader Starts -->
 <div id="preloader">
    <div id="preloader-content">
       
       <img id="logo" class="img-responsive" src="images/pionex_logo.png" alt="logo" width="">
    </div>
 </div>
 
 <!-- <div id="showSwitcher" class="styleSecondColor"><i class="fa fa-cog fa-spin"></i></div> -->
 <!-- Live Style Switcher Ends - demo only -->   <!-- Wrapper Starts -->
   <div class="wrapper">
      <!-- Header Starts -->
      
      <div class="container-fluid user-auth">
         <div class="hidden-xs col-sm-4 col-md-4 col-lg-4">
            <!-- Logo Starts -->
            <a class="logo" href="./?a=page">
               <img id="logo-user" class="img-responsive" src="images/pionex_logo.png" alt="logo">
            </a>
            <!-- Logo Ends -->
            
         </div>
         <div class="col-xs-12 col-sm-8 col-md-8 col-lg-8">
            <!-- Logo Starts -->
            <a class="visible-xs" href="./?a=page">
               <img id="logo" class="img-responsive mobile-logo" src="images/pionex_logo.png" alt="logo" width="150">
            </a>
            <!-- Logo Ends -->
             <?php
  if(isset($_GET['token'])){
$email = $_GET['email'];
    ?>
            <div class="form-container">
               <div>
                  <!-- Section Title Starts -->
                  <div class="row text-center">
                     <h2 class="title-head hidden-xs" style="font-size: 25px;">Recover <span>My Password</span></h2><br>
                  </div>
                                                      <!-- Section Title Ends -->
                  <br>
                  <!-- Form Starts -->
                  <form method="post">
                     <?php echo $msg; ?>
                     <!-- Input Field Starts -->
                     <div class="form-group">
                        <input type="email" class="form-control" name="email" value="<?php echo $email; ?>" placeholder="Enter your E-mail" required="required" readonly/>
                     </div>

                      <div class="form-group">
                        <input type="password" class="form-control" name="password" value="" placeholder="Password" required="required" />
                     </div>

                     <div class="form-group">
                        <input type="password" class="form-control" name="password2" value="" placeholder="Confirm Password" required="required" />
                     </div>
                     
                     <div class="form-group">
                        <button name="resetpassword" type="submit" class="btn btn-primary">Recover Password</button>
                     </div>
                     <!-- Submit Form Button Ends -->
                  </form>
                  <!-- Form Ends -->
               </div>
            </div>

             <?php

  }else{

  ?>

  <div class="form-container">
               <div>
                  <!-- Section Title Starts -->
                  <div class="row text-center">
                     <h2 class="title-head hidden-xs" style="font-size: 25px;">Recover <span>My Password</span></h2><br>
                  </div>
                                                      <!-- Section Title Ends -->
                  <br>
                  <!-- Form Starts -->
                  <form method="post">
                     <?php echo $msg; ?>
                     <!-- Input Field Starts -->
                     <div class="form-group">
                        <input type="email" class="form-control" name="email" value="" placeholder="Enter your E-mail" required="required" />
                     </div>
                     
                     <div class="form-group">
                        <button name="recover" type="submit" class="btn btn-primary">Recover Password</button>
                     </div>
                     <!-- Submit Form Button Ends -->
                  </form>
                  <!-- Form Ends -->
               </div>
            </div>
            
            <?php } ?>

            <!-- Copyright Text Starts -->
            <!-- <p class="text-center copyright-text"><a href="templateshub.net">Templates Hub</a></p> -->
            <!-- Copyright Text Ends -->
         </div>
      </div>
      <!-- Template JS Files -->
      <script src="js/jquery-2.2.4.min.js"></script>
      <script src="js/bootstrap.min.js"></script>
      <script src="js/select2.min.js"></script>
      <script src="js/jquery.magnific-popup.min.js"></script>
      <script src="js/custom.js"></script>

      <!-- Live Style Switcher JS File - only demo -->
      <script src="js/styleswitcher.js"></script>

   </div>
   <!-- Wrapper Ends -->
</body>

</html>