0, 'path' => '/', 'secure' => $secure, 'httponly' => true, 'samesite' => 'Strict', ]); session_start(); } function freeloader_require_auth(): void { freeloader_bootstrap_session(); if (empty($_SESSION['freeloader_loggedin'])) { http_response_code(403); die('Access denied. Please log in.'); } $timeout = 1800; if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > $timeout)) { session_unset(); session_destroy(); http_response_code(403); die('Session expired. Please log in again.'); } $_SESSION['last_activity'] = time(); } function freeloader_csrf_token(): string { freeloader_bootstrap_session(); if (empty($_SESSION['freeloader_csrf'])) { $_SESSION['freeloader_csrf'] = bin2hex(random_bytes(32)); } return $_SESSION['freeloader_csrf']; } function freeloader_verify_csrf(?string $token): bool { if (empty($token) || empty($_SESSION['freeloader_csrf'])) { return false; } return hash_equals($_SESSION['freeloader_csrf'], $token); } function freeloader_helper(string $command, array $args = []): array { $helper = '/usr/local/bin/freeloader-helper'; if (!file_exists($helper)) { return [false, 'Helper script not found.']; } $cmd = 'sudo ' . escapeshellarg($helper) . ' ' . escapeshellarg($command); foreach ($args as $a) { $cmd .= ' ' . escapeshellarg($a); } $output = []; $returnCode = 0; exec($cmd . ' 2>&1', $output, $returnCode); return [$returnCode === 0, implode("\n", $output)]; }