From b215ba3b603d3d12c164c2a0da0427ef9df1c351 Mon Sep 17 00:00:00 2001 From: Lars Gebhardt-Kusche Date: Tue, 3 Feb 2026 02:37:44 +0100 Subject: [PATCH] debug --- config/current.ver | 2 +- inc/helpers.php | 40 ++++++++++++++++++++++++++++++++++++++++ public/index.php | 4 +--- src/ApiKernel.php | 27 +++++++-------------------- 4 files changed, 49 insertions(+), 24 deletions(-) diff --git a/config/current.ver b/config/current.ver index 513314c..752b663 100644 --- a/config/current.ver +++ b/config/current.ver @@ -1 +1 @@ -1.2.23 \ No newline at end of file +1.2.24 \ No newline at end of file diff --git a/inc/helpers.php b/inc/helpers.php index 37a0d8a..29f620f 100644 --- a/inc/helpers.php +++ b/inc/helpers.php @@ -10,6 +10,46 @@ if (!isset($GLOBALS['page_styles'])) { $GLOBALS['page_styles'] = []; } +function debug_log_write(string $name, $payload, array $options = []): bool +{ + $dir = (string)($options['dir'] ?? (dirname(__DIR__) . '/debug')); + if ($dir === '') { + return false; + } + if (!is_dir($dir)) { + @mkdir($dir, 0777, true); + @chmod($dir, 0777); + } + + $fileName = (string)($options['filename'] ?? ''); + if ($fileName === '') { + $safeName = preg_replace('/[^a-zA-Z0-9_\.\-]/', '_', $name) ?: 'debug'; + $fileName = $safeName . '.log'; + } else { + $fileName = basename($fileName); + } + + $asJson = $options['json'] ?? (is_array($payload) || is_object($payload)); + if ($asJson) { + $line = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + } else { + $line = is_string($payload) ? $payload : (string)$payload; + } + + if (($options['newline'] ?? true) && $line !== '' && substr($line, -1) !== "\n") { + $line .= "\n"; + } + + $path = rtrim($dir, '/\\') . '/' . $fileName; + $flags = ($options['append'] ?? false) ? FILE_APPEND : 0; + $result = @file_put_contents($path, $line, $flags | LOCK_EX); + if ($result === false) { + return false; + } + @chmod($path, 0666); + return true; +} + function helper_asset_version(): ?string { if (isset($GLOBALS['layoutContext']['asset_version'])) { diff --git a/public/index.php b/public/index.php index 08a9f17..f2faa65 100644 --- a/public/index.php +++ b/public/index.php @@ -20,9 +20,7 @@ $clientIp = trim($clientIp); $version = $GLOBALS['app_version'] ?? ''; $versionLabel = $version ? (' | v' . $version) : ''; $accessLine = date(DATE_ATOM) . ' | ' . ($clientIp !== '' ? $clientIp : 'unknown') . $versionLabel . "\n"; -$logFile = $debugDir . '/last_access.log'; -$existing = is_file($logFile) ? (string)@file_get_contents($logFile) : ''; -@file_put_contents($logFile, $accessLine . $existing); +debug_log_write('last_access', $accessLine, ['append' => true, 'json' => false]); $pageTitle = 'Email Template System – Admin'; $pageId = 'home'; diff --git a/src/ApiKernel.php b/src/ApiKernel.php index 5507c5e..f9a8e41 100644 --- a/src/ApiKernel.php +++ b/src/ApiKernel.php @@ -9,6 +9,7 @@ ob_start(); // Lade den AuthService require_once __DIR__ . '/AuthService.php'; +require_once __DIR__ . '/../inc/helpers.php'; // ----------------------------------------------------------------- // ApiKernel.php (OPTIMIERT & KORRIGIERT) @@ -4925,26 +4926,12 @@ SQL; } } $dir = $this->debugDir(); - if (!is_dir($dir)) { - @mkdir($dir, 0777, true); - } - @chmod($dir, 0777); - $safeName = preg_replace('/[^a-zA-Z0-9_\.\-]/', '_', $name) ?: 'debug'; - $file = rtrim($dir, '/') . '/' . $safeName . '.log'; - $data = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); - @file_put_contents($file, $data ?: ''); - @chmod($file, 0666); - - $fallback = sys_get_temp_dir() . '/emailtemplate_debug.log'; - $line = json_encode([ - 'time' => date(DATE_ATOM), - 'name' => $safeName, - 'file' => $file, - 'wrote_bytes' => is_string($data) ? strlen($data) : 0, - 'dir_exists' => is_dir($dir), - 'dir_writable' => is_writable($dir), - ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); - @file_put_contents($fallback, ($line ?: '') . "\n", FILE_APPEND); + debug_log_write($name, $payload, [ + 'dir' => $dir, + 'append' => false, + 'json' => true, + 'newline' => true, + ]); } private function defaultApiBase(): string