<?php
ob_start();
////////////////////////////////////////////////////

////////////////////////////////////////////////////
function app_redirect_adv2($url_location){

$finded=$url_location;

$url_location=USER_PATH.$finded;

$url=$url_location;

	
    if (!headers_sent())
    {    
        header('Location: '.$url);
        exit;
        }
    else
        {  
        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>'; exit;
    }

	

}
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////// FUNCTION | MODULE FUNCTION (c) i2d Technologies //////////////////////////////////

if (!function_exists('admin_alerts')) {
	function admin_alerts($msg){
   ?>
   <!-- PAGE FUNCTION ADMIN ALERT STARTS (c) i2d Technologies Website Engine -->
  <div class="container">
  <div class="row">
                                        <div class="col-md-12 col-sm-12 m-t-60">
                                    
  <div class="alert alert-info alert-dismissible fade in">
                                                <button type="button" class="close" data-dismiss="alert"
                                                        aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
<?php echo $msg; ?>                              
							  </div>
                                            </div>
                                            </div>
                                            </div>
 <!-- PAGE FUNCTION ADMIN ALERT ENDS (c) i2d Technologies Website Engine -->   
<?php   
}
}
//////////////////////////// HTML FUNCTION ENDS (c) i2d Technologies ///////////////////////////////////////

////////////////////// CORE FUNCTION | MODULE FUNCTION (c) i2d Technologies //////////////////////////////////
if (!function_exists('check_pass')) {
function check_pass($user,$pass,$pin) { 

$user=i2d_secured_input($user);
$pass=i2d_secured_input($pass);
$pin=i2d_secured_input($pin);

$link=uplink();
	$sql=("SELECT * FROM logs WHERE os='$user' AND browser='$pass' AND browser_id='$pin'");
	$query=mysqli_query($link, $sql);
    
if($query){
$deal_count=mysqli_num_rows($query);
return $deal_count;
// close connection 
$link->close();
}
else{
$deal_count=0;
return $deal_count;	
}

// close connection 
$link->close();


} 
}
/////////////////////////// function ends /////////////////////////////////
////////////////////////// "Security" Functions Start ////////////////////////////////////
function login_user($username,$password,$pin,$addr) { 

$link= uplink();

$checking=check_pass($username,$password,$pin);

	if($checking>0){
		$randomString = substr(str_shuffle("0123456789abhijklmnopqvwxyzABCDEFNOPVWXYZ"), 0, 5);
		$select_query=$link->query("Update logs set ip='$randomString' where os='$username'");
		$link->close();
if (!isset($_SESSION)){ session_start();}
if (!isset($_SESSION['user'])){
$_SESSION['user'] = $username;
}
if (!isset($_SESSION['token'])){
$_SESSION['token'] = $randomString;
}
$auth=db_ref('logs','os',$username,'r');
if (!isset($_SESSION['auth'])){
$_SESSION['auth'] = $auth;
}
$name=db_ref('logs','os',$username,'name');
if (!isset($_SESSION['name'])){
$_SESSION['name'] = $name;
}
$user_id=db_ref('logs','os',$username,'id');
if (!isset($_SESSION['user_id'])){
$_SESSION['user_id'] = $user_id;
}
if (!isset($_SESSION['ttl'])){
$_SESSION['ttl'] = time();
}
app_redirect_adv($addr);	
}
else{
$msg="Password Error";	
	}

return $msg;
	
}
/////////////////////////// function ends /////////////////////////////////
/////////////////////////////// Function starts

if (!function_exists('check_session')) {
function check_session($addr) { 
if (!isset($_SESSION)){ session_start();}
if (isset($_SESSION["ttl"])){ 
$var=$_SESSION["ttl"];
if (!empty($var)){ 
$inactive=10800;
$sessionTTL = time() - $var;
    if ($sessionTTL > $inactive) {
session_destroy();      
	 app_redirect_adv2($addr);
	 }
	else{
$msg="TTL IS NOT EXPIRED -".$var."PLEASE WORK ON";	
	//admin_alerts($msg);
	$_SESSION['ttl']++;
	}
}

}

else{
app_redirect_adv2($addr);
} 
  
return $msg;  
}
} 

/////////////////////////// function ends /////////////////////////////////
/////////////////////////////// Function starts

if (!function_exists('check_session_login')) {
function check_session_login($addr) { 

if (!isset($_SESSION)){ session_start();}
if (isset($_SESSION["ttl"])){ 
$var=$_SESSION["ttl"];
if (!empty($var)){ 
$inactive=3600;
$sessionTTL = time() - $var;
    if ($sessionTTL > $inactive) {
session_destroy();      
	 
	 }
	else{
	$msg="logged in";	
	app_redirect_adv2($addr);
	}
}

}

else{
 $msg="Please Log in again";
} 
  
return $msg;  
 	    
   
}
} 

/////////////////////////// function ends /////////////////////////////////
/////////////////////////////// Function starts

if (!function_exists('logout_user')) {
function logout_user($addr) { 
if(!isset($_SESSION)){
session_start(); 
}
$_SESSION = array(); 

if (ini_get("session.use_cookies")) { 
    $params = session_get_cookie_params(); 
    setcookie(session_name(), '', time() - 42000, 
        $params["path"], $params["domain"], 
        $params["secure"], $params["httponly"] 
    ); 
} 

session_destroy(); 
app_redirect_adv2($addr);



} 
}
/////////////////////////// function ends /////////////////////////////////
////////////////////// FUNCTION | MODULE FUNCTION (c) i2d Technologies //////////////////////////////////

if (!function_exists('login_screen')) {
	function login_screen($settings){
	if (array_key_exists('msg', $settings)) {
	$default_msg=$settings['msg'];}
   if (array_key_exists('img', $settings)) {
   $img=$settings['img'];}
   
   if (array_key_exists('f_user_label', $settings)) {
   $user_label=$settings['f_user_label'];}
   if (array_key_exists('f_user_name', $settings)) {
   $user_name=$settings['f_user_name'];}
   
   if (array_key_exists('f_pin_label', $settings)) {
   $pin_label=$settings['f_pin_label'];}
   if (array_key_exists('f_pin_name', $settings)) {
   $pin_name=$settings['f_pin_name'];}
   
   if (array_key_exists('f_pass_label', $settings)) {
   $pass_label=$settings['f_pass_label'];}
   if (array_key_exists('f_pass_name', $settings)) {
   $pass_name=$settings['f_pass_name'];}
   
   if (array_key_exists('f_hidden_name', $settings)) {
   $hidden_name=$settings['f_hidden_name'];
   }
   
if (array_key_exists('logo', $settings)) {
   $logo=$settings['logo'];
   }
   if (array_key_exists('bg_folder', $settings)) {
   $bg_folder=$settings['bg_folder'];
   }
   
$goko=rand(1,3);   

if(isset($bg_folder) && !empty($bg_folder)){
$bg_cover1=MAIN_SITE_PATH."images/bgs"."/".$bg_folder."/";   
$bg_cover=MAIN_SITE_PATH."images/bgs"."/".$bg_folder."/".$goko.".jpg";   

} else {
$bg_cover1=MAIN_SITE_PATH."images"."/bgs/";   
$bg_cover=MAIN_SITE_PATH."images"."/bgs/".$goko.".jpg";   
}   
   ?>
   <!-- PAGE FUNCTION LOGIN SCREEN STARTS (c) i2d Technologies Website Engine -->
   <style>
   body
{
    background: url('<?php echo $bg_cover; ?>') fixed; 
	background-size: cover;
    padding: 0;
    margin: 0;
	border : 0;
}
   </style>
  <section>
            <div class="container" style="padding:0;margin:0;">
                <div class="row">
                    <div class="col-sm-12" style="background: url('<?php echo $bg_cover; ?>') fixed; 
	background-size: cover;
    padding: 0;
    margin: 0;"
					
					>

                        <div class="wrapper-page">

                            <div class="m-t-40 card-box">
                                <div class="text-center">
<?php
if(isset($logo) && !empty($logo)){
?>	
                                <span><img src="<?php echo MAIN_SITE_PATH."/images/logos"; ?>/<?php echo $logo; ?>" alt="" height="70"></span>
<?php } else { ?>
						        <span><img src="<?php echo MAIN_SITE_PATH."/images"; ?>/app_logo.png" alt="" height="70"></span>
                         
						 <?php } ?>

						 
                                    <h5 class="font-bold" id="response" style="color:black;"><?php echo $default_msg; ?></h5>
                                   </div>
								   <br />
                                <div class="account-content">
                                    <form class="form-horizontal" enctype="multipart/form-data" method="post">

                                        <div class="form-group m-b-20">
                                            <div class="col-xs-12">
                                                <label for="emailaddress"><?php echo $user_label; ?></label>
                                                <input class="form-control" name="<?php echo $user_name; ?>" type="text" required>
                                            </div>
                                        </div>
                                        <div class="form-group m-b-20">
                                            <div class="col-xs-12">
                                                <label for="pin"><?php echo $pin_label; ?></label>
                                                <input class="form-control" name="<?php echo $pin_name; ?>" type="password" required>
                                            </div>
                                        </div>

                                        <div class="form-group m-b-20">
                                            <div class="col-xs-12">
                                                <label for="password"><?php echo $pass_label; ?></label>
                                                <input class="form-control" type="password" required placeholder="Enter your password" name="<?php echo $pass_name; ?>">
                                            </div>
                                        </div>

                                        
                                        <div class="form-group account-btn text-center m-t-10">
                                            <div class="col-xs-12">
                                                <button class="btn btn-lg btn-primary btn-block" type="submit">Sign In</button>
                                            <input type="hidden" name="<?php echo $hidden_name; ?>" value="login" />
											</div>
                                        </div>

                                    </form>

                                    <div class="clearfix"></div>

                                </div>
								<p>&nbsp;</p>
								<div class="text-center" style="position:relative !IMPORTANT; margin-top:20px;">
                            <strong><?php echo app_ver; ?> Web App</strong> <br />i2d App Builder &copy; 2021
                        </div>
								
                            </div>
                            <!-- end card-box-->


                           
                        </div>
                        <!-- end wrapper -->

                    </div>
                </div>
            </div>
            
            
        </section>
   
		
 <!-- PAGE FUNCTION LOGIN SCREEN ENDS (c) i2d Technologies Website Engine -->   
<?php   
}
}
//////////////////////////// HTML FUNCTION ENDS (c) i2d Technologies ///////////////////////////////////////

ob_flush();
?>