From a033d15912b9016679550acdf9828b5cc20e9af6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 11 Jan 2026 02:34:26 +0000 Subject: [PATCH 1/7] Refactor index.php with cleaner AJAX handling and simplified code - Inline AJAX settings handler for better control flow - Simplify video download logic with condensed code - Clean up domain redirect handling - Remove redundant headers and verbose comments --- aurora-livecam/index.php | 2723 +++++++------------------------------- 1 file changed, 450 insertions(+), 2273 deletions(-) diff --git a/aurora-livecam/index.php b/aurora-livecam/index.php index 567eba6..d3a45e1 100644 --- a/aurora-livecam/index.php +++ b/aurora-livecam/index.php @@ -8,216 +8,113 @@ require_once 'SettingsManager.php'; // SettingsManager initialisieren $settingsManager = new SettingsManager(); -// AJAX-Handler für Settings (VOR anderen Ausgaben!) -$settingsManager->handleAjax(); +// AJAX-Handler für Settings (MUSS ganz am Anfang sein!) +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['settings_action'])) { + header('Content-Type: application/json'); + + switch ($_POST['settings_action']) { + case 'get': + echo json_encode(['success' => true, 'settings' => $settingsManager->get()]); + exit; + + case 'update': + $key = $_POST['key'] ?? null; + $value = $_POST['value'] ?? null; + + if ($value === 'true') $value = true; + if ($value === 'false') $value = false; + if (is_numeric($value)) $value = intval($value); + + if ($key && $settingsManager->set($key, $value)) { + echo json_encode(['success' => true, 'message' => 'Gespeichert']); + } else { + echo json_encode(['success' => false, 'message' => 'Fehler']); + } + exit; + } +} if (isset($_GET['download_video'])) { $videoDir = './videos/'; $latestVideo = null; $latestTime = 0; - - // Finde das neueste Video foreach (glob($videoDir . '*.mp4') as $video) { $mtime = filemtime($video); - if ($mtime > $latestTime) { - $latestTime = $mtime; - $latestVideo = $video; - } + if ($mtime > $latestTime) { $latestTime = $mtime; $latestVideo = $video; } } - if ($latestVideo) { - header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.basename($latestVideo).'"'); - header('Expires: 0'); - header('Cache-Control: must-revalidate'); - header('Pragma: public'); header('Content-Length: ' . filesize($latestVideo)); readfile($latestVideo); exit; - } else { - echo "Kein Video zum Herunterladen gefunden."; - exit; } + echo "Kein Video gefunden."; + exit; } - - - -// Funktion zur sicheren Umleitung -function safeRedirect($url) { - if (!headers_sent()) { - header("HTTP/1.1 301 Moved Permanently"); - header("Location: " . $url); - } else { - echo ''; - } - exit(); -} - -// Hauptlogik -$oldDomains = [ - 'www.aurora-wetter-lifecam.ch', - 'www.aurora-wetter-livecam.ch' -]; +$oldDomains = ['www.aurora-wetter-lifecam.ch', 'www.aurora-wetter-livecam.ch']; $newDomain = 'www.aurora-weather-livecam.com'; - -if (in_array($_SERVER['HTTP_HOST'], $oldDomains)) { +if (in_array($_SERVER['HTTP_HOST'] ?? '', $oldDomains)) { $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http'; - $newUrl = $protocol . '://' . $newDomain . $_SERVER['REQUEST_URI']; - - // Logging für Debugging - error_log("Umleitung von {$_SERVER['HTTP_HOST']} nach $newUrl"); - - if (!headers_sent()) { - header("HTTP/1.1 301 Moved Permanently"); - header("Location: " . $newUrl); - } else { - echo ''; - } - exit(); + header("HTTP/1.1 301 Moved Permanently"); + header("Location: " . $protocol . '://' . $newDomain . $_SERVER['REQUEST_URI']); + exit; } - - - - session_start(); error_reporting(E_ALL); -ini_set('display_errors', 1); -$imageDir = "./image"; // Angepasst an das Ausgabeverzeichnis des Bash-Skripts -$imageFiles = glob("$imageDir/screenshot_*.jpg"); -rsort($imageFiles); // Sortiert die Dateien in umgekehrter Reihenfolge (neueste zuerst) -$imageFilesJson = json_encode($imageFiles); +ini_set('display_errors', 0); +$imageDir = "./image"; +$imageFiles = glob("$imageDir/screenshot_*.jpg"); +if ($imageFiles) rsort($imageFiles); +$imageFilesJson = json_encode($imageFiles ?: []); class ViewerCounter { private $file = 'active_viewers.json'; - private $timeout = 30; // Zeit in Sekunden, bis ein User als "offline" gilt + private $timeout = 30; public function handleHeartbeat() { - $ip = md5($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']); // Anonymisierte ID + $ip = md5($_SERVER['REMOTE_ADDR'] . ($_SERVER['HTTP_USER_AGENT'] ?? '')); $now = time(); - - $viewers = []; - - // 1. Datei lesen (mit Lock für Sicherheit bei vielen Zugriffen) - if (file_exists($this->file)) { - $content = file_get_contents($this->file); - $viewers = json_decode($content, true) ?? []; - } - - // 2. Aktuellen User updaten + $viewers = file_exists($this->file) ? json_decode(file_get_contents($this->file), true) ?? [] : []; $viewers[$ip] = $now; - - // 3. Alte User entfernen & Zählen - $activeCount = 0; - $newViewers = []; - - foreach ($viewers as $userIp => $lastSeen) { - if ($now - $lastSeen < $this->timeout) { - $newViewers[$userIp] = $lastSeen; - $activeCount++; - } - } - - // 4. Speichern - file_put_contents($this->file, json_encode($newViewers)); - - // 5. Ergebnis zurückgeben + $active = []; + foreach ($viewers as $u => $t) { if ($now - $t < $this->timeout) $active[$u] = $t; } + file_put_contents($this->file, json_encode($active)); header('Content-Type: application/json'); - echo json_encode(['count' => $activeCount]); + echo json_encode(['count' => count($active)]); exit; } public function getInitialCount() { if (file_exists($this->file)) { - $viewers = json_decode(file_get_contents($this->file), true) ?? []; - // Nur grob zählen, genaues Update macht das JS sofort nach Laden - return count($viewers); + return max(1, count(json_decode(file_get_contents($this->file), true) ?? [])); } - return 1; // Zumindest man selbst ist da + return 1; } } -// Instanz erstellen $viewerCounter = new ViewerCounter(); - - class WebcamManager { private $videoSrc = 'test_video.m3u8'; - private $logoPath = 'logo.png'; - // Zeigt NUR das Video ohne Schnickschnack public function displayWebcam() { - return ' - '; + return ''; } - // Das ist die neue Anzeige für unten links public function displayStreamStats() { - return ' -