<?php
/***************************************************************************************************
 PROGRAM DESCRIPTION : PROGRAM TO CLOSE ADMIN SESSION
***************************************************************************************************/

include '../includes/session.php';
include '../includes/constants.php';
include '../includes/functions.php';
include_once '../includes/allstripslashes.php';

$partners = new partners;
$partners->connection($host, $user, $pass, $db);

// Get IP address
$ip = $_SERVER['REMOTE_ADDR'];
$adminUserId = isset($_SESSION['ADMINUSERID']) ? (int)$_SESSION['ADMINUSERID'] : 0;

// Create mysqli connection
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Update master admin IP if ID is 1
if ($adminUserId === 1) {
    $sql = "UPDATE `partners_admin` SET `admin_ip` = ?, `admin_lastLogin` = ''";
    $stmt = $conn->prepare($sql);
    if ($stmt) {
        $stmt->bind_param("s", $ip);
        $stmt->execute();
        $stmt->close();
    }
}

// Update admin user IP and last login
$sql = "UPDATE `partners_adminusers` SET `adminusers_ip` = ?, `adminusers_lastLogin` = '' WHERE `adminusers_id` = ?";
$stmt = $conn->prepare($sql);
if ($stmt) {
    $stmt->bind_param("si", $ip, $adminUserId);
    $stmt->execute();
    $stmt->close();
} else {
    echo "Database Error: " . $conn->error;
}

$conn->close();

// Destroy session and redirect
session_destroy();
$Err = "ad1003";
header("Location: index.php?Err=$Err");
exit;
?>
