<?php
session_start();

// date_default_timezone_set('Asia/Kolkata');
date_default_timezone_set('GMT');

$search_db = array(
	"Database_Server" => "localhost",
	"User_Id" => "jobaajgroup_emp3",
	"Password" => "E1(jSBolQNCx",
	"Database_Name" => "jobaajgroup_emp3"
);

//RDSDB
// $search_db = array(
// 	"Database_Server" => "database-1.csedtqtwjct4.ap-south-1.rds.amazonaws.com",
// 	"User_Id" => "admin",
// 	"Password" => "password",
// 	"Database_Name" => "nishtyainfotech_employee"
// );


$search_db_server      = $search_db['Database_Server'];
$search_db_user        = $search_db['User_Id'];
$search_db_password    = $search_db['Password'];
$search_db_name        = $search_db['Database_Name'];

$db = new mysqli($search_db_server, $search_db_user, $search_db_password, $search_db_name);
mysqli_set_charset($db, 'utf8');

$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://emp3.jobaaj.com/";

$cdn_url = "https://cdn.nishtyainfotech.com/";

require_once 'fun/edFunction.php';

if (isset($_COOKIE['RememberMe'])) {

	$dc = $_COOKIE['RememberMe'];
	$user = mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM `users` WHERE COALESCE(working_status, '')='' AND `remember_token`='$dc' "));
	if(!empty($user) && !isset($_SESSION['user_id'])){
		$_SESSION['user_id'] = $user['id'];
		$_SESSION['user_role'] = $user['role'];
		$_SESSION['user_name'] = $user['name'];
		$_SESSION['user_email'] = $user['email'];
		$_SESSION['emp_id'] = $user['employee_id'];
		$_SESSION['image'] = $user['image'];
		$_SESSION['hash_email'] = $user['hash_email'];
	}else if(empty($user)){
		setcookie("RememberMe", "", time() - 3600, "/");
		session_destroy();
		header('location:/');
	}
}



########################################################################## check request origin
$http_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
$allowed_origin = ["emp3.jobaaj.com", 'jobaaj.com'];
// Check if the origin or parsed referer matches the allowed origin
if (!in_array($http_host, $allowed_origin)) {
	echo json_encode(['error' => 'Access denied.']);
    exit();
}
########################################################################## check request origin