From 65be4bdf9b70b6b4f405d70e52ef7c99c1bcb498 Mon Sep 17 00:00:00 2001 From: Eskimue Date: Fri, 15 May 2026 16:37:53 +0200 Subject: [PATCH] index verbessert --- public/index.php | 195 ++++++++++++++++++++++++++--------------------- 1 file changed, 107 insertions(+), 88 deletions(-) diff --git a/public/index.php b/public/index.php index 858fdfb..2ea51e7 100644 --- a/public/index.php +++ b/public/index.php @@ -119,6 +119,68 @@ if (!function_exists('planetVisibilityClass')) { return ['class' => 'planet-visibility-bad', 'label' => 'am Tag']; } } +if (!function_exists('indexPythonCandidates')) { + function indexPythonCandidates(): array + { + if (PHP_OS_FAMILY === 'Windows') { + $candidates = []; + $localAppData = getenv('LOCALAPPDATA'); + if (is_string($localAppData) && $localAppData !== '') { + $localPython = $localAppData . '\\Python\\bin\\python.exe'; + if (is_file($localPython)) { + $candidates[] = escapeshellarg($localPython); + } + } + + return array_values(array_unique(array_merge( + $candidates, + ['py -3', 'py', 'python'] + ))); + } + + return ['/usr/bin/python3', '/usr/bin/python', 'python3', 'python']; + } +} +if (!function_exists('runIndexPythonCommand')) { + function runIndexPythonCommand(string $scriptPath, array $args): array + { + putenv('PYTHONUTF8=1'); + putenv('PYTHONIOENCODING=utf-8'); + + $escapedArguments = array_map('escapeshellarg', array_merge([$scriptPath], $args)); + $lastError = 'Python konnte nicht gestartet werden.'; + + foreach (indexPythonCandidates() as $pythonBinary) { + $command = $pythonBinary . ' ' . implode(' ', $escapedArguments) . ' 2>&1'; + $output = []; + $resultCode = 0; + exec($command, $output, $resultCode); + + $joined = trim(implode("\n", $output)); + $decoded = json_decode($joined, true); + + if (is_array($decoded)) { + return [ + 'ok' => (bool) ($decoded['ok'] ?? false), + 'data' => $decoded, + 'raw' => $joined, + 'command' => $command, + ]; + } + + if ($joined !== '') { + $lastError = $joined; + } elseif ($resultCode !== 0) { + $lastError = 'Fehlercode ' . $resultCode . ' bei ' . $pythonBinary; + } + } + + return [ + 'ok' => false, + 'error' => $lastError, + ]; + } +} function idx_tleChecksum(string $line): int { @@ -227,82 +289,47 @@ if ($loggedIn) { $date = (new DateTimeImmutable('now', new DateTimeZone($tz)))->format('Y-m-d'); $scriptPath = realpath(__DIR__ . '/py/api.py'); - if (PHP_OS_FAMILY === 'Windows') { - $pythonBin = 'python'; - } elseif (file_exists('/usr/bin/python3')) { - $pythonBin = '/usr/bin/python3'; - } elseif (file_exists('/usr/bin/python')) { - $pythonBin = '/usr/bin/python'; - } else { - $pythonBin = 'python'; - } - putenv('PYTHONUTF8=1'); - putenv('PYTHONIOENCODING=utf-8'); // Sonne + Mond - $args = [ - $pythonBin, - escapeshellarg($scriptPath), - escapeshellarg('sun_moon_rise_set'), - escapeshellarg((string)$lat), - escapeshellarg((string)$lon), - escapeshellarg((string)$elev), - escapeshellarg($date), - escapeshellarg($tz), - ]; - $cmd = implode(' ', $args) . ' 2>&1'; - $out = []; - $resultCode = 0; - exec($cmd, $out, $resultCode); - $rawText = trim(implode("\n", $out)); - $raw = json_decode($rawText, true); - if (is_array($raw) && ($raw['ok'] ?? false)) { - $skyData = $raw; + $pythonResult = runIndexPythonCommand($scriptPath, [ + 'sun_moon_rise_set', + (string)$lat, + (string)$lon, + (string)$elev, + $date, + $tz, + ]); + if ($pythonResult['ok'] ?? false) { + $skyData = $pythonResult['data']; $nextDate = (new DateTimeImmutable($date, new DateTimeZone($tz)))->modify('+1 day')->format('Y-m-d'); - $argsNext = [ - $pythonBin, - escapeshellarg($scriptPath), - escapeshellarg('sun_moon_rise_set'), - escapeshellarg((string)$lat), - escapeshellarg((string)$lon), - escapeshellarg((string)$elev), - escapeshellarg($nextDate), - escapeshellarg($tz), - ]; - $cmdNext = implode(' ', $argsNext) . ' 2>&1'; - $outNext = []; - $resultCodeNext = 0; - exec($cmdNext, $outNext, $resultCodeNext); - $rawTextNext = trim(implode("\n", $outNext)); - $rawNext = json_decode($rawTextNext, true); - if (is_array($rawNext) && ($rawNext['ok'] ?? false)) { - $skyDataNext = $rawNext; + $pythonResultNext = runIndexPythonCommand($scriptPath, [ + 'sun_moon_rise_set', + (string)$lat, + (string)$lon, + (string)$elev, + $nextDate, + $tz, + ]); + if ($pythonResultNext['ok'] ?? false) { + $skyDataNext = $pythonResultNext['data']; } } else { - $pyError = $raw['error'] ?? ($rawText !== '' ? $rawText : 'Fehler beim Berechnen der Aufgangszeiten.'); + $pyError = $pythonResult['data']['error'] ?? $pythonResult['error'] ?? 'Fehler beim Berechnen der Aufgangszeiten.'; } // Planeten - $args2 = [ - $pythonBin, - escapeshellarg($scriptPath), - escapeshellarg('planet_rise_set'), - escapeshellarg((string)$lat), - escapeshellarg((string)$lon), - escapeshellarg((string)$elev), - escapeshellarg($date), - escapeshellarg($tz), - ]; - $cmd2 = implode(' ', $args2) . ' 2>&1'; - $out2 = []; - $resultCode2 = 0; - exec($cmd2, $out2, $resultCode2); - $rawText2 = trim(implode("\n", $out2)); - $raw2 = json_decode($rawText2, true); - if (is_array($raw2) && ($raw2['ok'] ?? false)) { - $planetData = $raw2; + $pythonResult2 = runIndexPythonCommand($scriptPath, [ + 'planet_rise_set', + (string)$lat, + (string)$lon, + (string)$elev, + $date, + $tz, + ]); + if ($pythonResult2['ok'] ?? false) { + $planetData = $pythonResult2['data']; } elseif ($pyError === null) { - $pyError = $raw2['error'] ?? ($rawText2 !== '' ? $rawText2 : 'Fehler beim Berechnen der Planetenzeiten.'); + $pyError = $pythonResult2['data']['error'] ?? $pythonResult2['error'] ?? 'Fehler beim Berechnen der Planetenzeiten.'; } // Satelliten-Überflüge heute — Favorit zuerst, dann alle gemerkten @@ -341,28 +368,20 @@ if ($loggedIn) { ], $userSats), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $satsPayload = base64_encode((string) $satsJson); - $args3 = [ - $pythonBin, - escapeshellarg($scriptPath), - escapeshellarg('satellite_passes'), - escapeshellarg((string)$lat), - escapeshellarg((string)$lon), - escapeshellarg((string)$elev), - escapeshellarg($tz), - escapeshellarg($dayStart->format('Y-m-d\TH:i:s\Z')), - escapeshellarg($dayEnd->format('Y-m-d\TH:i:s\Z')), - escapeshellarg($satsPayload), - ]; - $cmd3 = implode(' ', $args3) . ' 2>&1'; - $out3 = []; - $resultCode3 = 0; - exec($cmd3, $out3, $resultCode3); - $rawText3 = trim(implode("\n", $out3)); - $raw3 = json_decode($rawText3, true); - if (is_array($raw3) && ($raw3['ok'] ?? false)) { - $satData = $raw3; + $pythonResult3 = runIndexPythonCommand($scriptPath, [ + 'satellite_passes', + (string)$lat, + (string)$lon, + (string)$elev, + $tz, + $dayStart->format('Y-m-d\TH:i:s\Z'), + $dayEnd->format('Y-m-d\TH:i:s\Z'), + $satsPayload, + ]); + if ($pythonResult3['ok'] ?? false) { + $satData = $pythonResult3['data']; } else { - $satError = $raw3['error'] ?? ($rawText3 !== '' ? $rawText3 : 'Fehler beim Berechnen der Satellitenüberflüge.'); + $satError = $pythonResult3['data']['error'] ?? $pythonResult3['error'] ?? 'Fehler beim Berechnen der Satellitenüberflüge.'; } } }