<?php  
session_start(); 

	include("connect.php");

  $par1=$_POST['username'];
	 $par2=md5($_POST['password']);
	
	date_default_timezone_set("Asia/Singapore");
	$mdate= date('Y-m-d H:i:s');
	$hour=date('H');
	$minute= date('i');
	
	$d1 = $hour + ($minute * 0.01);
	
	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 {
	 $eve = "select * from res_user_master where userloginid='$par1' and usersecurepasword='$par2' and userstatus='Active'";

	
	$re = mysqli_query($conn, $eve);
	
	if(mysqli_num_rows($re) > 0)
	{
		 while($rt = mysqli_fetch_assoc($re))
		 {
			 if ($rt['usersecurepasword'] == $par2 && $rt['userloginid']==$par1){
				$_SESSION['linkarisecrm'] = "yes";
				$_SESSION['linkariseumid'] = $rt['usermid'];
				$_SESSION['linkariseufullname'] = $rt['username'];
				$_SESSION['linkariseutype'] = $rt['userdesignation'];
				$_SESSION['linkariseumobile'] = $rt['usermobile'];
				
				
				
				$_SESSION['linkarisecmpmid'] = $rt['companymid'];
				
				$_SESSION['dashboardurl'] = "dashboard.php";
				$_SESSION['appicationlink']="";
					
					$umid = $rt['usermid'];
					$cmpmid = $rt['companymid'];
					
			 	$eve_display= "select * from res_company_master where companymid=$cmpmid";
				$re_display = mysqli_query($conn, $eve_display);
				while($rt_display = mysqli_fetch_assoc($re_display))
				{
					$_SESSION['linkarisecmpmid']=$rt_display['companymid'];
					
					$_SESSION['linkarisecmpname']=$rt_display['companyname'];
					$_SESSION['linkarisecmptype']=$rt_display['companytype'];
					
				}
			header('Location: dashboard.php');
			 }
		    else
   {
				
	   
	   
	   $_SESSION['msg1'] = "Invalid Userid or Password";
		header('Location: index.php');
	
   }
		 }
		 
		
	
   }
   else
   {
				
	   
	   
	   $_SESSION['msg1'] = "Invalid Userid or Password";
		header('Location: index.php');
	
   }
	}
	}
	
?>