<?php  session_start(); error_reporting(0); 
//if session
if(!isset($_SESSION['j_admin']) && !isset($_SESSION['j_admin'])){ }else{ 
    die(
    "
    <div class='alert alert-danger'> Error 100. Try again.</div>
    "
    );
}
require_once("../j_conn/url.php");
require_once("../j_conn/dbc.php");
require_once("../j_incs/functions.php");
if(isset($_POST['username'])){ }else{ 
die(
"
<div class='alert alert-danger'> Error 101. Try again.</div>
"
);
}
?>
<script>
$('.rMe').each(function() {
	 if ($(this).val() == "") {
 	   $(this).css({"border-color": "brown", 
		 "border-width":"1px", 
		 "border-style":"solid"});
	 }else{
		$(this).css({"border-color": "#fafafa", 
		 "border-width":"0px", 
		 "border-style":"solid"});	 
	 }
});
</script>
<?php
$url_get = RMC($_POST["url"]); $url_get = urldecode($url_get); 
$username = RMC($_POST["username"]); 
$password = RMC($_POST["password"]); 
$rememberMe = RMC($_POST["rememberMe"]);

if(!empty($username) 
 && !empty($password)
 ){ //check if required field is not empty
     
 }else{
     die(
        "
        <div class='alert alert-danger'> A required field is empty. Try again.</div>
        "
        );
 } 
//hash password
 $passwordH = base64_encode("$hash$password");
//check if email and password exists
$sql4="SELECT count(*) as sum from j_users WHERE 
j_phone='".$username."' AND j_password='".$passwordH."' AND j_blocked < 1 AND j_deleted < 1 AND j_status > 0
|| j_email='".$username."' AND j_password='".$passwordH."' AND j_blocked < 1 AND j_status > 0
|| j_username='".$username."' AND j_password='".$passwordH."' AND j_blocked < 1 AND j_status > 0
"; //check 
$result4 = runSQL($sql4);
$data4 = mysqli_fetch_array($result4);
$sum4 = $data4['sum'];
if($sum4 > 0){
    
}else{
     die(
        "
        <div class='alert alert-danger'> Invalid Login Details. Try again.</div>
        <script> $('[name=username]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
        <script> $('[name=password]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
        "
        );
 } 
//check for banned words
//not_in_use_words($username,"Username","username");
//
if($rememberMe>0){
    setcookie('rememberMe', $username, time() + (86400 * 30), "/"); // 86400 = 1 day
}else{
    if(isset($_COOKIE['rememberMe'])){
        setcookie("rememberMe", "", time() - 3600,"/"); 
        //To delete cookie; set the expiration date to one hour ago. Cookies must be delete witht the same arguement or parameters //with which they are created.
    }
}
//get info using username
$sql25="SELECT * from j_users WHERE 
j_phone='".$username."'
|| j_email='".$username."'
|| j_username='".$username."'
"; //  
$result25 = runSQL($sql25);
$data25 = mysqli_fetch_array($result25);
$last_seen = $data25['j_last_seen'];
$user_status = $data25['j_status'];
$id = $data25['j_id'];
$email = $data25['j_email'];
$username = $data25['j_username'];
$firstname = $data25['j_firstname'];
$lastname = $data25['j_lastname'];
$name = trim("$firstname $lastname");
//
$u_email = $email;
$u_subject ="LOGIN ACCESS";
$u_message ="<p>Hi <b>$name</b>;</p> <p>Your account was recently accessed. Be sure you have authorized this action otherwise feel free to contact us via e-mail: <b>$company_email</b></p>";
//require("../j_incs/mail.php");
//echo $message;
//die("");
//send notify
//send_notify("",$id,$u_subject,$u_message);
//start session	
if($user_status=="1"){$_SESSION['j_admin']=$id;}else{$_SESSION['j_admin']=$id;}
//start timeout
$_SESSION['j_timeout'] = $CurrentDate;
//update last seen and sign in num
$sql5="UPDATE j_users SET j_last_seen='".$CurrentDate."', j_last_seen_update='".$last_seen."', j_logins=j_logins+1  WHERE j_id='".$id."'";
$result5=runSQL($sql5);
//Delete all other session
foreach($_SESSION as $key => $val){
	if($key !=='remember' && $key !=='j_admin' && $key !=='j_user' && $key !=='j_timeout'){
		unset($_SESSION[$key]);
	}
}  
//
if(empty($url_get)){
    $url="".$dir."";
}else{
    $url="".$url_get."";
}
//
?>
<script>
    $(".formR").remove();
    $("#form").remove();
     $("#submit").attr('disabled', '');
    //document.getElementById("form").reset();
    setTimeout(function(){
        window.location.href="<?php echo $url; ?>";           
    }, 2000);
</script>
<div class="card">
    <div class="card-body">
        <div class="container">
            <div class="row">
                 <div class="col-md-12" style="text-align:center;">
                    <div class="responseBox success">
                        <i class="anticon anticon-check"></i>
                    </div>
                     <p>
                        Login Successful. 
                        <br/>
                        <i class='anticon anticon-loading'></i>
                        Redirecting... 
                    </p>
                    <a class="btn btn-primary btn-block" href="<?php echo $url; ?>"> Continue</a>
                 </div>
            </div>
        </div>
    </div>
</div>
<?php 
/*
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>';	
*/
?>