<?php
session_set_cookie_params(['secure' => true,'httponly' => true,'samesite' => 'Strict']);
session_start();
$session_lifetime = 3 * 60; // Session timeout
$correct_username = "admin";
$correct_password_hash = '$2y$10$S9naYnE6MqP14zVRfIsig.A98jBGzEImaGLgKVuiSGKpI7yr7XeGG'; // password: 4869
//echo password_hash("pass", PASSWORD_DEFAULT); //generate
if (isset($_GET['logout'])) {
    session_unset();
    session_destroy();
    header("Location: " . $_SERVER['PHP_SELF']);
    exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['heartbeat'])) {
    $_SESSION['last_activity'] = time();
    exit;
}
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $username = $_POST['username'] ?? '';
        $password = $_POST['password'] ?? '';
        if ($username === $correct_username && password_verify($password, $correct_password_hash)) {
            session_regenerate_id(true);
            $_SESSION['logged_in'] = true;
            $_SESSION['last_activity'] = time();
            header("Location: " . $_SERVER['PHP_SELF']);
            exit;
        } else {
            $error = "Invalid username or password.";
        }
    }
    ?>
    <?php if (isset($error)) echo "<p style='color:red;'>$error</p>"; ?>
    <form method="post">
        <label>Username: <input name="username" required></label><br>
        <label>Password: <input type="password" name="password" required></label><br>
        <button type="submit">Login</button>
    </form>

    <?php
    exit;
}
if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity']) > $session_lifetime) {
    session_unset();
    session_destroy();
    header("Location: " . $_SERVER['PHP_SELF']);
    exit;
}
$_SESSION['last_activity'] = time(); // Refresh session activity
$remaining_time = ($_SESSION['last_activity'] + $session_lifetime) - time();
?>
Welcome, you are logged in! <span id="countdown"></span><hr>
<a href="?logout=1">Logout</a>
<script>
    let remaining = <?php echo $remaining_time; ?>;
    function updateCountdown() {
        if (remaining <= 0) {
            clearInterval(timer);
            alert("Session expired due to inactivity.");
            location.reload();
            return;
        }
        const minutes = Math.floor(remaining / 60);
        const seconds = remaining % 60;
        document.getElementById('countdown').textContent =
            minutes + "m " + (seconds < 10 ? "0" : "") + seconds + "s";
        remaining--;
    }
    updateCountdown();
    const timer = setInterval(updateCountdown, 1000);
    setInterval(() => {
        fetch("", {
            method: "POST",
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: "heartbeat=1"
        });
    }, 30000);
</script>





<!--<a target=_blank href="alceawis.com.zip">(dld)</a><br>-->
<?php $phpVersion = phpversion();echo "PHP version: " . $phpVersion;?><!--(must be PHP version: 8.2.20 !)--> - <?php if (extension_loaded('zip')) { echo "Zip extension is enabled."; } else { echo "Zip extension is not enabled.";} ?><br><br>
<!--https://pb.todon.de/@alcea/112823560063665113--https://mastodonapp.uk/@gozzy/112823854328998071-->
<?php
$delfile = 'alceawis.com.zip';
if (file_exists($delfile)) {
    $fileModifiedTime = filemtime($delfile);
    $currentTime = time();
    $fifteenMinutesAgo = $currentTime - (15 * 60);
    if ($fileModifiedTime < $fifteenMinutesAgo) {
        unlink($delfile);
        echo "File deleted successfully.<br>";
    } else {
        echo "File is not older than 15 minutes.<br>";
    }
} else {
    echo "File does not exist.<br>";
}
?>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $zip = new ZipArchive();
    $zipFileName = __DIR__ . '/alceawis.com.zip';
    $intermediateFolderName = pathinfo($zipFileName, PATHINFO_FILENAME);

    if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator('.', RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::LEAVES_ONLY
        );

        foreach ($files as $name => $file) {
            if (!$file->isDir()) {
                $filePath = $file->getRealPath();
                $relativePath = $intermediateFolderName . '/' . substr($filePath, strlen(__DIR__) + 1);
                $zip->addFile($filePath, $relativePath);
            }
        }
        $zip->close();
         //$downloadLink = '<a target=_blank href="' . $zipFileName . '">Click here to download</a>';
         //$downloadLink = '<a target=_blank href="alceawis.com.zip">Click here to download the zip file</a>';
         $downloadLink = '<a target=_blank href="alceawis.com.zip?v=' . time() . '">Click here to download the zip file</a>';
        echo 'Files zipped successfully. ' . $downloadLink;
    } else {
        echo 'Failed to create zip file.';
    }
}
?>
<form method="POST">
    <button type="submit">Zip Files</button>
</form>
<!---delete-->
<?php
$file = 'alceawis.com.zip';
if (isset($_GET['delete'])) {
    if (file_exists($file)) {
        unlink($file);
        echo "File deleted successfully.";
    } else {
        echo "File not found.";
    }
}
?>
<a href="?delete=true">Click here to delete zip</a>

