<?php  
session_start(); 
	include("connect.php");
	include("connectp.php");
    $par1=$_POST['username'];
	$par2=$_POST['password'];
	date_default_timezone_set("Asia/Kolkata");
	$cdatetime= date('Y-m-d H:i:s');
	$cdate= date('Y-m-d');

	$ipaddress = "";

	if (getenv('HTTP_CLIENT_IP'))
		$ipaddress = getenv('HTTP_CLIENT_IP');
	else if(getenv('HTTP_X_FORWARDED_FOR'))
		$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
	else if(getenv('HTTP_X_FORWARDED'))
		$ipaddress = getenv('HTTP_X_FORWARDED');
	else if(getenv('HTTP_FORWARDED_FOR'))
		$ipaddress = getenv('HTTP_FORWARDED_FOR');
	else if(getenv('HTTP_FORWARDED'))
		$ipaddress = getenv('HTTP_FORWARDED');
	else if(getenv('REMOTE_ADDR'))
		$ipaddress = getenv('REMOTE_ADDR');
	else
		$ipaddress = 'UNKNOWN';
	$userserver=$_SERVER['HTTP_USER_AGENT'];
	
	//$eve_gen = "update res_mainuser_master set currentplanstatus='Expired' where planexpirydate<'$cdate'";
	//$re_gen = mysqli_query($conn, $eve_gen);
	
if (isset($_POST['submit'])) {
    $recaptcha = $_POST['g-recaptcha-response'];

    if (!$recaptcha) {
        $_SESSION['msg1'] = "Invalid Captcha";
        header('Location: index.php');
        exit;
    }

    $secretKey = "6LcX-icpAAAAADRlF_3eVzw9sqqABeKB6n0fpvKK";
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    
    $postData = array(
        'secret' => $secretKey,
        'response' => $recaptcha
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

    $response = curl_exec($ch);

    if ($response === false) {
        // Handle cURL error
        $_SESSION['msg1'] = "Error contacting reCAPTCHA server";
        header('Location: index.php');
        exit;
    }

    curl_close($ch);

    $response = json_decode($response);

    if ($response === null || !$response->success) {
        // Handle reCAPTCHA validation failure
        $_SESSION['msg1'] = "Invalid reCAPTCHA";
        header('Location: index.php');
        exit;
    } else {
	
	$query = "select * from res_superadmin_master where superloginid=:p1 and superpassword=:p2";
	$stmt = $db7->prepare($query);
	$stmt->bindValue(':p1', "$par1");
	$stmt->bindValue(':p2',  "$par2");
	$stmt->execute();
	if ($stmt->rowCount() > 0)
	{
		while($rt = $stmt->fetch(PDO::FETCH_ASSOC))
			{    	
				$_SESSION['linkarisesuloginsesion'] = "yes";
				$_SESSION['linkarisesumid'] = $rt['superadminmid'];
				$_SESSION['linkariseuname'] = $rt['superadminname'];
				$_SESSION['linkariseutype'] = 'Superuser';
				
				
				
				header("Location: dashboard.php");
			
		}
	}
	else
	{
		
	   
	   $_SESSION['msg1'] = "Invalid Username and Password";
		header('Location: index.php');
	}
}	
	
}
	
	
	
	
?>