<?php

 
$title .= $languageArray["resetpassword.title"];

if( $_SESSION["msmbilisim_userlogin"] == 1  || $user["client_type"] == 1 || $settings["resetpass_page"] == 1  ){
  Header("Location:".site_url());
}

if( !route(1) ){

if( $_POST ):

  $captcha        = $_POST['g-recaptcha-response'];
  $googlesecret   = $settings["recaptcha_secret"];
  $captcha_control= file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$googlesecret&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
  $captcha_control= json_decode($captcha_control);
  $user = $_POST["user"];
  $type = $_POST["type"];
$row= $conn->prepare("SELECT * FROM clients WHERE username=:email ");
    $row->execute(array("email"=>$user));
    $row= $conn->prepare("SELECT * FROM clients WHERE email=:email ");
    $row->execute(array("email"=>$user));
    if( empty($user) ):
      $error      = 1;
      $errorText  = $languageArray["error.resetpassword.user.empty"];
    elseif( !$row->rowCount() ):
      $error      = 1;
      $errorText  = $languageArray["error.resetpassword.user.notmatch"];
    elseif( $settings["recaptcha"] == 2 && $captcha_control->success == false ):
      $error      = 1;
      $errorText  = $languageArray["error.resetpassword.recaptcha"];

    else:
      $row    = $row->fetch(PDO::FETCH_ASSOC);
      $token   = md5($row["email"].$row["username"].rand(9999,2324332));
      $update = $conn->prepare("UPDATE clients SET passwordreset_token=:pass WHERE client_id=:id ");
      $update->execute(array("id"=>$row["client_id"],"pass"=> $token ));
    
        // forget password mail html

  $htmlContent = "<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Reset Your Password</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
            color: #333;
        }
        .email-container {
            background-color: #ffffff;
            padding: 30px;
            border-radius: 8px;
            max-width: 600px;
            margin: 0 auto;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); /* Subtle Box Shadow */
            text-align: center;
            position: relative;
            overflow: hidden;
        }
        .header-container {
            background-color: #007bff;
            padding: 20px;
            margin: -30px -30px 20px -30px; /* Extends full width */
        }
        .header {
            color: white;
            font-size: 22px;
            font-weight: bold;
            text-align: center; /* Align text to the right */
            max-width: 600px;
            margin: 0 auto;
            padding-right: 20px;
        }
        .btn-container {
            text-align: center;
            margin-top: 20px;
        }
        .btn {
            display: inline-block;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            padding: 12px 20px;
            font-size: 16px;
            border-radius: 5px;
            box-shadow: 0 3px 6px rgba(0, 0, 0, 0.2); /* Button shadow */
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .alternative-link {
            display: block;
            margin-top: 15px;
            font-size: 14px;
            color: #007bff;
            text-decoration: none;
        }
        .alternative-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class='email-container'>
        <div class='header-container'>
            <div class='header'>Reset Password</div>
        </div>
        <p>Hello,</p>
        <p>We've received a request to reset your password. If you made this request, please click the button below to choose a new password.</p>
        
        <div class='btn-container'>
            <a href='" . site_url() . "resetpassword/$token' class='btn'>Reset Your Password</a>
        </div>

        <p>If the button doesn't work, you can also copy and paste the following link into your browser:</p>
        <p><a href='" . site_url() . "resetpassword/$token' class='alternative-link'>" . site_url() . "resetpassword/$token</a></p>

        <p>If you did not request a password reset, simply ignore this email. Your account is safe and unchanged.</p>
        <br>
        <p>Thank you!<br>Our Team</p>
    </div>
</body>
</html>";



$to = $row["email"]; 
$from =  $settings["smtp_user"]; 
$fromName = $settings["site_seo"]; 
$subject = "Reset Your Password"; 
$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
$headers .= 'From: '.$fromName.'<'.$from.'>' . "\r\n"; 
$headers .= 'Cc: '.$from . "\r\n"; 
$headers .= 'Bcc: '.$from . "\r\n"; 
 if(mail($to, $subject, $htmlContent, $headers)){ 
$success    = 1;
        $successText= $languageArray["error.resetpassword.success"];
        echo '<script>setTimeout(function(){window.location="'.site_url().'"},2000)</script>';
}else{ 
$error      = 1;
        $errorText  = $languageArray["error.resetpassword.fail"];
     }    
        


    endif;

endif;
} else {
$templateDir  = "setnewpassword";
$passwordreset_token = route(1);
$user = $conn->prepare("SELECT * FROM clients WHERE passwordreset_token=:id");
$user->execute(array("id"=> route(1) ));
$user = $user->fetch(PDO::FETCH_ASSOC);

$client= $conn->prepare("SELECT * FROM clients WHERE passwordreset_token=:email ");
    $client->execute(array("email"=>$passwordreset_token));
 
if( !$client->rowCount() ):
Header("Location:".site_url(resetpassword));
endif;
if( $_POST ):
$pass = $_POST["password"];

  $again = $_POST["password_again"];
$passwordreset_token = route(1);
if($pass != $again):
$error      = 1;
      $errorText  = "Passwords not matched";
else:
$update = $conn->prepare("UPDATE clients SET password=:pass, passwordreset_token=:token WHERE client_id=:id ");
      $update->execute(array("id"=> $user["client_id"],"token" => "" ,"pass"=> md5($pass) ));

if( $update ):
        $success    = 1;
        $successText= "Successfully Changed";
        echo '<script>setTimeout(function(){window.location="'.site_url().'"},2000)</script>';
      else:
        $error      = 1;
        $errorText  = $languageArray["error.resetpassword.fail"];
      endif;


    endif;
endif;
    
      




} 

