<?php
include("../../assets/constant/config.php");
include("activity.php");

// session_start();

// Set timeout duration in seconds (e.g., 1800 seconds for 30 minutes)
$timeout_duration = 1800;

// Check if last activity timestamp is set
if (isset($_SESSION['last_activity'])) {
    // Calculate the session's "time to live" (TTL)
    $session_ttl = time() - $_SESSION['last_activity'];
    if ($session_ttl > $timeout_duration) {
        // If TTL is greater than the timeout duration, destroy the session
        session_unset();
        session_destroy();
        echo '<script>
            alert("Session expired. Please log in again.");
            window.location.href = "../../index.php";
            </script>';
        exit();
    }
}

// Update last activity time stamp
$_SESSION['last_activity'] = time();

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    if (isset($_POST['submit'])) {
        if (isset($_POST['g-recaptcha-response'])) {
            $stmt1 = $conn->prepare("SELECT * FROM `manage_web` ");
            $stmt1->execute();
            $record1 = $stmt1->fetchAll();
            foreach ($record1 as $key1) {
                $secretekey = $key1['secretkey'];
            }
            $ip = $_SERVER['REMOTE_ADDR'];
            $response = $_POST['g-recaptcha-response'];
            $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretekey&response=$response&remoteip=$ip";
            $fire = file_get_contents($url);
            $data = json_decode($fire);
            if ($data->success == true) {
                $passw = hash('sha256', $_POST['password']);

                function createSalt()
                {
                    return '2123293dsj2hu2nikhiljdsd';
                }

                $salt = createSalt();
                $user_input_password = hash('sha256', $salt . hash('sha256', $_POST['password']));

                $stmt1 = $conn->prepare("SELECT * FROM `login` where email=:email");
                $stmt1->bindParam(':email', $_POST['email']);
                $stmt1->execute();

$msg="hello,
Good morning";
$phone='';
$instance='';
$access_token='';
$message = urlencode($msg); 

$url='https://api.rangebroadcast.com/api/send?number=91'.$phone.'&type=text&message='.$message.'&instance_id='.$instance.'&access_token='.$access_token.'';

 // echo $url;
 $response = file_get_contents($url);
      $data1=json_decode($response);
      // print_r($data1); exit;

                $record = $stmt1->fetchAll();

                if (count($record) > 0) {
                    foreach ($record as $res) {
                        $stored_password = hash('sha256', $salt . $res['password']);
                        if ($user_input_password == $stored_password) {
                            $_SESSION['id'] = $res['id'];
                            $_SESSION['login_success'] = 'Login successful';
                            $notes = "login successfully";
                            user_activity($conn, $_SESSION['id'], $notes);

                            header("location:../dashboard.php");
                            exit();
                        }
                    }
                }

                echo '<script>
                    alert("Wrong Password or Email");
                    window.location.href = "../../index.php";
                    </script>';
            } else {
                echo '<script>
                    alert("Please Fill the Captcha");
                    window.location.href = "../../index.php";
                    </script>';
            }
        }
    }
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>