add custom block templates and logging
This commit is contained in:
1 parent
a48e99b454
commit
e48534c69d
1 file changed
+140
-10
@@ -43,6 +43,7 @@ $dirs = [
|
||||
'WEBROOT/src/MVC/Middleware',
|
||||
'WEBROOT/src/MVC/Layout',
|
||||
'WEBROOT/src/MVC/Layout/Templates',
|
||||
'WEBROOT/src/MVC/Layout/Block',
|
||||
'WEBROOT/storage',
|
||||
'WEBROOT/storage/uploads',
|
||||
'WEBROOT/logs',
|
||||
@@ -396,6 +397,11 @@ DB_LIVE_DATABASE = ""
|
||||
[mail]
|
||||
MAIL_FROM = "bruno.eckhardt07@gmail.com"
|
||||
MAIL_TO = "bruno.eckhardt07@gmail.com"
|
||||
|
||||
[logging]
|
||||
LOG_ACCESS = true
|
||||
LOG_SECURITY = true
|
||||
BLOCK_PAGE_TEMPLATE = "modern"
|
||||
INI;
|
||||
|
||||
// --- DB: databaseConnect.php ---
|
||||
@@ -557,19 +563,42 @@ PHP;
|
||||
|
||||
$files['WEBROOT/src/MVC/Classes/Logger.php'] = <<<'PHP'
|
||||
<?php
|
||||
|
||||
namespace MVC\Classes;
|
||||
|
||||
class Logger {
|
||||
private static function getLogDir() { return dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'logs'; }
|
||||
private static function getLogDir() {
|
||||
return dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'logs';
|
||||
}
|
||||
|
||||
private static $config = null;
|
||||
|
||||
private static function getConfig() {
|
||||
if (self::$config === null) {
|
||||
$configFile = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'env.ini';
|
||||
self::$config = file_exists($configFile) ? parse_ini_file($configFile) : [];
|
||||
}
|
||||
return self::$config;
|
||||
}
|
||||
|
||||
public static function log($message, $level = 'INFO', $file = 'system.log') {
|
||||
$config = self::getConfig();
|
||||
if ($file === 'access.log' && empty($config['LOG_ACCESS'])) return;
|
||||
if ($file === 'blocked.log' && empty($config['LOG_SECURITY'])) return;
|
||||
|
||||
$logDir = self::getLogDir();
|
||||
if (!is_dir($logDir) && !mkdir($logDir, 0777, true)) return;
|
||||
|
||||
$logFile = $logDir . DIRECTORY_SEPARATOR . $file;
|
||||
$timestamp = date('Y-m-d H:i:s');
|
||||
file_put_contents($logDir . DIRECTORY_SEPARATOR . $file, "[$timestamp] [$level] $message\n", FILE_APPEND | LOCK_EX);
|
||||
file_put_contents($logFile, "[$timestamp] [$level] $message\n", FILE_APPEND | LOCK_EX);
|
||||
}
|
||||
|
||||
public static function logException(\Throwable $e) {
|
||||
$msg = sprintf("Uncaught Exception: '%s' with message '%s' in %s:%d\nStack trace:\n%s", get_class($e), $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
|
||||
self::log($msg, 'CRITICAL', 'error.log');
|
||||
$message = sprintf("Uncaught Exception: '%s' with message '%s' in %s:%d\nStack trace:\n%s", get_class($e), $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
|
||||
self::log($message, 'CRITICAL', 'error.log');
|
||||
}
|
||||
|
||||
public static function logError($errno, $errstr, $errfile, $errline) {
|
||||
$levels = [E_ERROR => 'ERROR', E_WARNING => 'WARNING', E_PARSE => 'PARSE_ERROR', E_NOTICE => 'NOTICE', E_USER_ERROR => 'USER_ERROR', E_USER_WARNING => 'USER_WARNING', E_USER_NOTICE => 'USER_NOTICE', E_DEPRECATED => 'DEPRECATED', E_USER_DEPRECATED => 'USER_DEPRECATED'];
|
||||
self::log("$errstr in $errfile on line $errline", $levels[$errno] ?? 'UNKNOWN', 'error.log');
|
||||
@@ -1340,12 +1369,25 @@ use MVC\Classes\Client;
|
||||
*/
|
||||
class BlockMiddleware implements MiddlewareInterface {
|
||||
public function handle() {
|
||||
$blockedIps = ['127.0.0.2'];
|
||||
$blockedIps = ['127.0.0.1'];
|
||||
$ip = Client::ip();
|
||||
if (in_array($ip, $blockedIps)) {
|
||||
Logger::log("Blocked IP attempt: $ip", 'SECURITY', 'blocked.log');
|
||||
|
||||
// Lade Konfiguration für das Template
|
||||
$configFile = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'env.ini';
|
||||
$config = file_exists($configFile) ? parse_ini_file($configFile) : [];
|
||||
$template = $config['BLOCK_PAGE_TEMPLATE'] ?? 'standard';
|
||||
|
||||
$templatePath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Layout' . DIRECTORY_SEPARATOR . 'Block' . DIRECTORY_SEPARATOR . $template . '.php';
|
||||
|
||||
http_response_code(403);
|
||||
die("Ihre IP-Adresse wurde blockiert.");
|
||||
if (file_exists($templatePath)) {
|
||||
include $templatePath;
|
||||
} else {
|
||||
die("Ihre IP-Adresse wurde blockiert.");
|
||||
}
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1358,18 +1400,28 @@ namespace MVC\Middleware;
|
||||
|
||||
use MVC\Core\MiddlewareInterface;
|
||||
use MVC\Classes\Logger;
|
||||
use MVC\Classes\Client;
|
||||
|
||||
/**
|
||||
* Beispiel-Middleware: Wird bei jedem Request ausgeführt (Global)
|
||||
*/
|
||||
class EveryRequestMiddleware implements MiddlewareInterface {
|
||||
public function handle() {
|
||||
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
|
||||
$service = $_GET['service'] ?? 'home';
|
||||
$ip = Client::ip();
|
||||
|
||||
// Bestimme Service (Logik konsistent mit index.php)
|
||||
$service = $_GET['service'] ?? null;
|
||||
if ($service === null) {
|
||||
$uri = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH);
|
||||
$service = trim($uri, '/');
|
||||
if (empty($service)) {
|
||||
$service = 'home';
|
||||
}
|
||||
}
|
||||
|
||||
Logger::log("IP: $ip - Service: $service", 'ACCESS', 'access.log');
|
||||
}
|
||||
}
|
||||
|
||||
PHP;
|
||||
|
||||
$files['WEBROOT/src/MVC/Layout/Layout.php'] = <<<'PHP'
|
||||
@@ -2885,7 +2937,85 @@ $files['WEBROOT/src/MVC/View/test1.php'] = "<h1>Service 1</h1><p><?= \$msg ?></p
|
||||
</style>
|
||||
HTML;
|
||||
|
||||
// --- CONFIG: routes.php ---
|
||||
$files['WEBROOT/src/MVC/Layout/Block/standard.php'] = <<<'HTML'
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Zugriff verweigert</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; background: #f4f4f4; color: #333; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
|
||||
.container { background: white; padding: 2rem; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center; max-width: 400px; border-top: 5px solid #d9534f; }
|
||||
h1 { color: #d9534f; margin-top: 0; }
|
||||
p { line-height: 1.6; }
|
||||
.ip { font-weight: bold; color: #777; font-size: 0.9rem; margin-top: 1rem; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>403 - Zugriff verweigert</h1>
|
||||
<p>Ihre IP-Adresse wurde aufgrund von Sicherheitsrichtlinien vorübergehend blockiert.</p>
|
||||
<p>Bitte wenden Sie sich an den Administrator, falls Sie glauben, dass dies ein Fehler ist.</p>
|
||||
<div class="ip">IP: <?php echo htmlspecialchars($ip); ?></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
HTML;
|
||||
|
||||
$files['WEBROOT/src/MVC/Layout/Block/modern.php'] = <<<'HTML'
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Systemmeldung</title>
|
||||
<style>
|
||||
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #1a1a2e; color: #e94560; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
|
||||
.card { background: #16213e; padding: 3rem; border-radius: 20px; text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,0.5); border: 1px solid #0f3460; }
|
||||
.icon { font-size: 4rem; margin-bottom: 1rem; }
|
||||
h1 { margin: 0; font-weight: 300; letter-spacing: 2px; }
|
||||
p { color: #95a5a6; margin-top: 1.5rem; }
|
||||
.footer { margin-top: 2rem; font-size: 0.8rem; opacity: 0.5; color: white; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<div class="icon">🛡️</div>
|
||||
<h1>ZUGRIFF GESPERRT</h1>
|
||||
<p>Ihre Verbindung wurde aus Sicherheitsgründen unterbrochen.</p>
|
||||
<div class="footer">ID: <?php echo htmlspecialchars($ip); ?> | <?php echo date('Y-m-d H:i:s'); ?></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
HTML;
|
||||
|
||||
$files['WEBROOT/src/MVC/Layout/Block/minimal.php'] = <<<'HTML'
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>403 Forbidden</title>
|
||||
<style>
|
||||
body { background: white; color: black; font-family: monospace; padding: 50px; }
|
||||
h1 { border-bottom: 1px solid black; padding-bottom: 10px; }
|
||||
pre { background: #eee; padding: 15px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>403 Forbidden</h1>
|
||||
<p>Access to this resource is denied.</p>
|
||||
<hr>
|
||||
<pre>
|
||||
Status: Blocked
|
||||
Source: <?php echo htmlspecialchars($ip); ?>
|
||||
Time: <?php echo date('r'); ?>
|
||||
</pre>
|
||||
</body>
|
||||
</html>
|
||||
HTML;
|
||||
|
||||
// --- CONFIG: routes.php ---
|
||||
$files['WEBROOT/src/config/routes.php'] = <<<'PHP' <?php
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user