<?php

ini_set('session.gc-maxlifetime', 3400);
session_set_cookie_params(3400);
session_name("user_session");
// session_id($_COOKIE['user_session']);
session_start();

$_SERVER['HTTP_HOST'] ? $_SERVER['HTTP_HOST'] : "chat.ahrefs.pk";
 
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 3400)) {
    // last request was more than 56 minutes ago
    session_unset(); // unset $_SESSION variable for the run-time 
    session_destroy(); // destroy session data in storage
}

if (!isset($_SESSION['plan_id']) || (!in_array('chat_gpt', $_SESSION['plan_id']) && $_SESSION['user_agent'] !== $_SERVER['HTTP_USER_AGENT'])) {
    header("Location: /login.php");
    exit('Session Expired, Reaccess from Dashboard!');
}else{

if($_SERVER['REQUEST_URI'] === '/chatgpt1'){
    // Set the POST data
    $postData = array(
    'plan_id' => 'chat_gpt',
    'username' => $_SESSION['username'],
    'ip' => $_SERVER['REMOTE_ADDR'],
    'user_agent' => $_SERVER['HTTP_USER_AGENT'],
    );

    $postData = json_encode($postData);

    function makePOSTRequest($url, $postData) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        ));

    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
    }


    // Send the POST request to connect.php on Domain A
    $response = makePOSTRequest('https://chatpride.cloudasd.com/connect', $postData);

    // Verify the response
    if ($response == 'error') {
    // Handle the error response
    echo 'You Not Have Access For This Tool';
    } else {
    // Set the cookie
    // echo $response;
    header("Location: https://chatpride.cloudasd.com/access?token=$response");
    }
    die;
}

if($_SERVER['REQUEST_URI'] === '/chatgpt2'){
    // Set the POST data
    $postData = array(
    'plan_id' => 'chat_gpt',
    'username' => $_SESSION['username'],
    'ip' => $_SERVER['REMOTE_ADDR'],
    'user_agent' => $_SERVER['HTTP_USER_AGENT'],
    );

    $postData = json_encode($postData);

    function makePOSTRequest($url, $postData) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        ));

    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
    }


    // Send the POST request to connect.php on Domain A
    $response = makePOSTRequest('https://chatpride2.cloudasd.com/connect', $postData);

    // Verify the response
    if ($response == 'error') {
    // Handle the error response
    echo 'You Not Have Access For This Tool';
    } else {
    // Set the cookie
    // echo $response;
    header("Location: https://chatpride2.cloudasd.com/access?token=$response");
    }
    die;
}


}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Select Access</title>
  <style>
    body {
      background: #f3f4f6;
      font-family: sans-serif;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100vh;
      margin: 0;
    }
    .box {
      background: #fff;
      padding: 2rem;
      border-radius: 10px;
      box-shadow: 0 4px 12px rgba(0,0,0,0.1);
      text-align: center;
    }
    .box h2 {
      margin-bottom: 1.5rem;
    }
    .btn {
      background: #4f46e5;
      color: white;
      border: none;
      padding: 0.75rem 1.5rem;
      margin: 0.5rem;
      font-size: 1rem;
      border-radius: 8px;
      cursor: pointer;
    }
    .btn:hover {
      background: #4338ca;
    }
  </style>
</head>
<body>
  <div class="box">
    <h2>Select Access</h2>
    <button class="btn" onclick="window.location.href='/chatgpt1'">ChatGPT Access 1</button>
    <button class="btn" onclick="window.location.href='/chatgpt2'">ChatGPT Access 2</button>
  </div>
</body>
</html>
