<?php
	include "../db_connection/db_connection.php";
	//include "../config/config.php";
	$SLCT_cur_dt_time = "SELECT DATE_ADD( now( ) , INTERVAL 5.30 HOUR_MINUTE ) AS CUR_TIME";
	$QRY_cur_dt_time  = mysqli_query($connect,$SLCT_cur_dt_time);
	$FTCH_cur_dt_time = mysqli_fetch_object($QRY_cur_dt_time);
	$cur_time         = $FTCH_cur_dt_time->CUR_TIME;
	$cur_date		  = explode(" ",$cur_time);
	$cur_date		  = $cur_date[0];

	date_default_timezone_set("Asia/Kolkata");
    $cur_time 	= date("Y-m-d H:i:s");
	$cur_date 	= date("Y-m-d");

	$u_name=mysqli_real_escape_string($connect,$_GET['value_one']);
	$p_name=mysqli_real_escape_string($connect,$_GET['value_two']);

	if($u_name=="" && $p_name=="")
	{
		$login_msg ="* Please enter Username and Password";
		echo $login_msg;
	}
	else if($u_name=="")
	{
		$login_msg ="* Please enter Username";
		echo $login_msg;
	}
	else if($p_name=="")
	{
		$login_msg ="* Please enter Password";
		echo $login_msg;
	}
	else if($u_name!='' && $p_name!='')
	{
		$SLCT_admin = "SELECT * FROM admin WHERE BINARY user_name='".addslashes($u_name)."' AND BINARY pass_word='".addslashes($p_name)."' AND ( designation='super_admin' OR designation='admin')";
		//echo $SLCT_admin;
		$QURY_admin = mysqli_query($connect,$SLCT_admin) or die ("Error in query: $query " . mysqli_error($conn));
			$row = mysqli_fetch_array($QURY_admin,MYSQLI_ASSOC);
		$check_rows=mysqli_num_rows($QURY_admin);

		if($check_rows==0)
		{
			$login_msg ="<p><strong>You have entered incorrect Username / password</strong></p>";
		}
		else
		{
			$SLCT_admin = "SELECT * FROM admin WHERE BINARY user_name='".addslashes($u_name)."' AND BINARY pass_word='".addslashes($p_name)."' AND ( designation='super_admin' OR designation='admin')";
			$QURY_admin = mysqli_query($connect,$SLCT_admin) or die ("Error in query: $query " . mysqli_error($conn));
			$FTCH=mysqli_fetch_object($QURY_admin);
			if($FTCH->designation=='admin')
			{
				/*$st_ip = $_SERVER['REMOTE_ADDR'];
				//$st_ip = $_SERVER["HTTP_X_REAL_IP"];

				$IPStart = explode(".",$st_ip); 
				//$IPFinish = explode(".","122.165.225.195"); //OLD IP
				$IPFinish = explode(".","122.165.198.58"); // DYNAMIC IP 122.178.139.147
				for($i=0;$i<4;$i++) 
				{
					if($IPStart[$i]==$IPFinish[$i]) 
					{
						$Between = 1; 
					} 
					else
					{
						$Between = 0; 
						break;
					}
				} */
				$bet_flg = 1;
			}
			else if($FTCH->designation!='admin')
			{
				if($FTCH->designation=='super_admin')
				{

				/*	$st_ip = $_SERVER['REMOTE_ADDR'];
					//$st_ip = $_SERVER["HTTP_X_REAL_IP"];

					$IPStart = explode(".",$st_ip); 
					//$IPFinish = explode(".","122.165.225.195"); //OLD IP
				    $IPFinish = explode(".","122.165.198.58");
					for($i=0;$i<4;$i++) 
					{
						if($IPStart[$i]==$IPFinish[$i]) 
						{
							$bet_flg = 1; 
						} 
						else
						{
							$bet_flg = 0; 
							break;
						}
					}*/
				} 
				else
				{
					$bet_flg = 1; 
				}
				$Between = 1; 
			}
			$Between = 1; 
			$bet_flg=0;
			$st_ip='';
			if($Between==1) 
			{
				$login_msg = 'splt_adm,'.base64_encode(base64_encode(base64_encode(base64_encode($FTCH->admin_id))));
				if($bet_flg==0) 
				{ 	
					$INST_IP = "INSERT INTO static_ip_log(`admin_id`, `designation`, `user_name`, `pass_word`, `ip_address`, `post_dt`)VALUES('$FTCH->admin_id','$FTCH->designation','$u_name','$p_name','$st_ip','$cur_time')";			
					$QURY_IP = mysqli_query($connect,$INST_IP);
				}
			} 
			else if($Between==0) 
			{
				/*$login_msg = 'splt_adm,existe,'.base64_encode(base64_encode(base64_encode(base64_encode($FTCH->admin_id))));
				$INST_IP = "INSERT INTO static_ip_log(`admin_id`, `designation`, `user_name`, `pass_word`, `ip_address`, `post_dt`)VALUES('$FTCH->admin_id','$FTCH->designation','$u_name','$p_name','$st_ip','$cur_time')";
				$QURY_IP = mysqli_query($connect,$INST_IP);*/
			}
		}
	
		$admin_id= $row['admin_id'];
		$username= $row['user_name'];
		$password= $row['pass_word'];
		echo $login_msg;
	}
?>	