<?php 
error_reporting(E_ALL ^ E_DEPRECATED);
?>
<?php require_once('../Connections/ebanking.php'); 
// Include PHPMailer files if not using Composer
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($theValue) : mysqli_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['username'])) {
  $loginUsername = $_POST['username'];
  $password = $_POST['password'];
  $MM_fldUserAuthorization = "accountno";
  $MM_redirectLoginSuccess = "otp_verification.php";
  $MM_redirectLoginFailed = "failedlogin.php";
  $MM_redirecttoReferrer = true;

  //mysql_select_db($database_ebanking, $ebanking);
  $username = mysqli_real_escape_string($ebanking, $loginUsername);
  $password = mysqli_real_escape_string($ebanking, $password);
  $LoginRS__query = "SELECT accountno, acctname, email FROM customer WHERE accountno='$username' AND password='$password'";

  // Execute query
  $LoginRS = mysqli_query($ebanking, $LoginRS__query);
  $loginFoundUser = mysqli_num_rows($LoginRS);

  if ($loginFoundUser > 0) {
    $loginStrGroup = mysqli_fetch_array($LoginRS);

    if (PHP_VERSION >= 5.1) {
      session_regenerate_id(true);
    } else {
      session_regenerate_id();
    }

    // Declare session variables and assign them
    $_SESSION['MM_Username'] = $username;
    $_SESSION['MM_UserGroup'] = $loginStrGroup['accountno'];

    if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
    }

    if ($loginStrGroup) {
      // Generate OTP code
      $otp = rand(1000, 9999);
      $_SESSION['otp'] = $otp;
      $_SESSION['accountno'] = $loginStrGroup['accountno'];

      // Retrieve customer name from the database
      $customer_name = $loginStrGroup['acctname'];

      // Send OTP code via email
      $mail = new PHPMailer(true);
      try {
        $mail->isSMTP();
        $mail->Host       = 'mail.al-shajarahislamicbank.com';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'otp@al-shajarahislamicbank.com';
        $mail->Password   = 'yc1]f2FqB{6J';
        $mail->SMTPSecure = 'ssl';
        $mail->Port       = 465;
        $mail->setFrom('noreply@al-shajarahislamicbank.com', 'Al Shajarah Islamic Bank');
        $mail->addAddress($loginStrGroup['email'], $loginStrGroup['accountno']);
        $mail->isHTML(true);
        $mail->Subject = 'Al Shajarah Islamic Bank - One-Time Password (OTP)';
        $mail->Body    = '<p>Dear ' . $customer_name . ',</p>'
                        . '<p>Your one-time password (OTP) for Al Shajarah Islamic Bank login is:</p>'
                        . '<h2 style="font-size: 24px; color: #4CAF50; margin-bottom: 20px;">' . $otp . '</h2>'
                        . '<p>Please use this OTP to securely log in to your account.</p>'
                        . '<p>If you did not request this OTP, please contact our support team immediately.</p>'
                        . '<p>Thank you for banking with us.</p>'
                        . '<p>Sincerely,</p>'
                        . '<p>Al Shajarah Islamic Bank</p>';
        $mail->send();
      } catch (Exception $e) {
        echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
      }
    }

    header("Location: " . $MM_redirectLoginSuccess);
  } else {
    header("Location: " . $MM_redirectLoginFailed);
  }
}
?>



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Online Banking Login</title>

<!-- Bootstrap Core CSS -->
<link href="../vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="../css/adminnine_classic.css" rel="stylesheet">
<link href="../vendor/roc/jbclient.css" rel="stylesheet">

<!-- Custom Fonts -->
<link href="../vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[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>
    <div id="content">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 col-xs-12">
                    <div class="form-field">
                        <h3 class="text-center"><img src="../img/grand-logo.png" width="349" height="70" alt=""/><br>
                          <br>
                          <br>
<br>
                          <br>
                          <br>
                        LOGIN</h3>
                        <p>Please enter your credentials in the following form</p>
                        <form action="<?php echo $loginFormAction; ?>" METHOD="POST" name="userlogin" id="userlogin">
                            <fieldset>
                                <div class="form-group">
                                    <label for="username">User ID</label>
                                    <input type="username" class="form-control" name="username" autofocus>
                                </div>
                                <div class="form-group">
                                    <label for="password">Password</label>
                                    <input type="password" class="form-control" name="password" value="">
                                </div>
                                <div class="checkbox">
                                    <label><input type="checkbox" value="Remember Me" placeholder="User ID" name="remember">Remember Me</label>
                                </div>
                                <br>
                                <input name="Login" type="submit" class="btn btn-primary" value="Login">
                            </fieldset>

                            <br>
                            <br>
                            <div class="row">
                            <div class="col-md-6 "> <a href="forgotpassword.php">Forgot password?</a> </div>
                            
                            </div>
                        </form>

                        <hr>

                        <h5>Beware of fraudulent attacks</h5>
                        <p>We will never contact you by telephone, e-mail nor internet asking you to disclose or to perform a confirmation by entering your Password or One Time Password (OTP)</p>
                  </div>
                </div>
            </div>
        </div>
    </div>

    <div id="jbfooter">
        <div class="container">
            <div class="row">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                    <hr>
                    <div class="row">
                        <div class="col-md-8 col-sm-12 col-xs-12">
                            <a class="bjb-link-arrow text-nowrap" href="#">About Us</a>
                            <a class="bjb-link-arrow text-nowrap" href="#">Terms Of Use</a>
                            <a class="bjb-link-arrow text-nowrap" href="#">Privacy Policy</a>
                        </div>
                        <div class="col-md-4 col-sm-12 col-xs-12">
                            <p class="copyright">© 2024 Gran KBC Bank Group</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

<script src="../vendor/jquery/jquery.min.js"></script> 

<!-- Bootstrap Core JavaScript --> 
<script src="../vendor/bootstrap/js/bootstrap.min.js"></script> 

<!-- Custom Theme JavaScript --> 
<script src="../js/adminnine.js"></script>
</body>
</html>