function saveToFile($content, $slug) {
// Set DEBUG to false for production to enable file creation
$DEBUG = false;
if (!$DEBUG) {
// Save the content to a file
// Ensure the directory exists
$path = __DIR__ . '/' . $slug;
if (!file_exists($path)) {
mkdir($path, 0755, true); // Use 0755 for directories
}
// Save the content to index.html
$filepath = $path . '/index.html';
file_put_contents($filepath, $content);
}
}
// Get the requested URI from the server
$request_uri = $_SERVER['REQUEST_URI'] ?? '/';
// Determine the protocol (http or https)
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
// Get the domain name from the server
$domain = $_SERVER['HTTP_HOST'] ?? 'localhost';
// Construct the full URL
$full_url = $protocol . "://" . $domain . $request_uri;
// Remove leading and trailing slashes from the URI to extract the slug
$slug = trim($request_uri, '/');
/**
* Get the root domain from the config table.
* The root domain is used to remove a base path from the slug,
* allowing for cleaner URLs.
*/
// Get root domain and active_frontend from config table
try {
$stmt = $pdo->prepare("SELECT config_key, value FROM config WHERE config_key IN ('root_domain', 'active_frontend')");
$stmt->execute();
$configs = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
// Get root_domain
$root_domain = $configs['root_domain'] ?? '';
// Get active_frontend
$active_frontend = $configs['active_frontend'] ?? 'default';
// Validate root domain format
if (!empty($root_domain)) {
// Remove trailing slashes
$root_domain = rtrim($root_domain, '/');
// Ensure root domain starts with http:// or https://
if (!preg_match('~^https?://~i', $root_domain)) {
$root_domain = $protocol . '://' . $root_domain;
}
}
} catch (PDOException $e) {
// Log error but continue with empty root domain - Disabled to prevent flickering
// error_log("Failed to fetch config values: " . $e->getMessage());
$root_domain = '';
$active_frontend = 'default';
}
// Remove the root domain from the full URL if it exists at the beginning
if (strpos($full_url, $root_domain) === 0) {
// Remove the root domain from the beginning of the full URL
$slug = substr($full_url, strlen($root_domain));
// Remove any leading and trailing slashes
$slug = trim($slug, '/');
}
$isLoggedIn = UserAuth::isLoggedIn();
if ($slug == "") {
// Start output buffering
ob_start();
// Set variables for the template
$page_title = 'Dönerfreund - Finde den besten Döner in deiner Stadt';
$meta_description = 'Dönerfreund hilft dir, die besten Döner in deiner Stadt zu finden. Bewertungen, Preise und mehr!';
$theme_dir = $active_frontend;
// Include the index_page.php template directly
include __DIR__ . '/frontend/' . $active_frontend . '/index_page.php';
$content = ob_get_clean();
// Only output the content, don't save it
echo $content;
exit;
}
// Handle city listing pages (e.g., staedte-a, staedte-b, etc.)
if (preg_match('/^staedte-([a-z])$/', $slug, $matches)) {
$letter = strtoupper($matches[1]);
// Start output buffering
ob_start();
// Set variables for the template
$theme_dir = $active_frontend;
$current_letter = $letter;
$page_title = 'Städte mit ' . $letter . ' - Dönerfreund';
$meta_description = 'Finde die besten Döner in Städten mit ' . $letter;
// Include the staedte_page.php template directly
include __DIR__ . '/frontend/' . $active_frontend . '/staedte_page.php';
$content = ob_get_clean();
// Save and output the content
saveToFile($content, $slug);
echo $content;
exit;
}
// Handle special routes
// Search page
if (preg_match('/^search(?:\/|\?|$)/', $slug)) {
ob_start();
// Set variables for the template
$theme_dir = $active_frontend;
$page_title = 'Suche - Dönerfreund';
$meta_description = 'Suche nach Dönerbuden und Städten auf Dönerfreund';
// Include the search_page.php template directly
include __DIR__ . '/frontend/' . $active_frontend . '/search_page.php';
$content = ob_get_clean();
echo $content;
exit;
}
// Login page
if ($slug === 'login') {
// If user is already logged in, redirect to profile
if ($isLoggedIn) {
header('Location: ' . rtrim($root_domain, '/') . '/profile/');
exit;
}
ob_start();
// Set page title and meta description
$page_title = 'Anmelden';
$meta_description = 'Melden Sie sich bei Dönerfreund an, um Bewertungen abzugeben und Ihr Profil zu verwalten.';
// Set variables for the template
$theme_dir = $active_frontend;
$error = ''; // Initialize error variable
// The login form submission is handled here now.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
if (empty($username) || empty($password)) {
$error = 'Bitte geben Sie Benutzername und Passwort ein.';
} else {
$result = UserAuth::login($pdo, $username, $password);
if ($result['success']) {
header('Location: ' . rtrim($root_domain, '/') . '/profile/');
exit;
} else {
$error = $result['message'];
}
}
}
// Include the full page structure for the login page
include __DIR__ . '/frontend/' . $active_frontend . '/header_page.php';
include __DIR__ . '/frontend/' . $active_frontend . '/login_page.php';
include __DIR__ . '/frontend/' . $active_frontend . '/footer_page.php';
$content = ob_get_clean();
echo $content;
exit;
}
// Logout page
if ($slug === 'logout') {
// No need for output buffering, just include the logout logic
include __DIR__ . '/frontend/' . $active_frontend . '/logout_page.php';
exit;
}
// Profile page
if ($slug === 'profile') {
ob_start();
// Set page title and meta description
$page_title = 'Mein Profil';
$meta_description = 'Verwalte dein Dönerfreund-Profil, sieh deine Bewertungen und Favoriten ein.';
// Set variables for the template
$theme_dir = $active_frontend;
// Include the profile_page.php template directly
include __DIR__ . '/frontend/' . $active_frontend . '/profile_page.php';
$content = ob_get_clean();
echo $content;
exit;
}
// Register page
if ($slug === 'register') {
// If user is already logged in, redirect to profile
if ($isLoggedIn) {
header('Location: ' . rtrim($root_domain, '/') . '/profile/');
exit;
}
ob_start();
// Set page title and meta description
$page_title = 'Registrieren';
$meta_description = 'Erstellen Sie ein neues Dönerfreund-Konto.';
// Set variables for the template
$theme_dir = $active_frontend;
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
$password_confirm = $_POST['password_confirm'] ?? '';
if ($password !== $password_confirm) {
$error = 'Passwords do not match.';
} else {
$result = UserAuth::register($pdo, $username, $email, $password);
if ($result['success']) {
$success = $result['message'];
} else {
$error = $result['message'];
}
}
}
// Include the full page structure for the register page
include __DIR__ . '/frontend/' . $active_frontend . '/header_page.php';
include __DIR__ . '/frontend/' . $active_frontend . '/register_page.php';
include __DIR__ . '/frontend/' . $active_frontend . '/footer_page.php';
$content = ob_get_clean();
echo $content;
exit;
}
// Help page
if ($slug === 'help') {
ob_start();
// Set page title and meta description
$page_title = 'Hilfe & FAQ';
$meta_description = 'Häufig gestellte Fragen und Hilfe zur Nutzung von Dönerfreund.';
// Set variables for the template
$theme_dir = $active_frontend;
// Include the help_page.php template directly
include __DIR__ . '/frontend/' . $active_frontend . '/help_page.php';
$content = ob_get_clean();
echo $content;
exit;
}
// Flag to track if a page was successfully generated
$page_found = false;
try {
// First, try to find a location
$stmt = $pdo->prepare("SELECT * FROM locations WHERE slug = ?");
$stmt->execute([$slug]);
$location_data = $stmt->fetch(PDO::FETCH_ASSOC);
if ($location_data) {
$location = new Location(
$pdo,
(int)$location_data['id'],
$location_data['name'],
$location_data['slug'],
(int)$location_data['parent_id'],
$location_data['long_html'],
$location_data['short_html']
);
ob_start();
$theme_dir = $active_frontend;
$page_title = $location->getName();
$meta_description = 'Finde die besten Döner in ' . $location->getName();
include __DIR__ . '/frontend/' . $active_frontend . '/location_page.php';
$content = ob_get_clean();
saveToFile($content, $slug);
echo $content;
$page_found = true;
} else {
// If no location found, try to find a doenerbude
$stmt = $pdo->prepare("SELECT * FROM doenerbuden WHERE slug = ?");
$stmt->execute([$slug]);
$doenerbude_data = $stmt->fetch(PDO::FETCH_ASSOC);
if ($doenerbude_data) {
$doenerbude = new Doenerbude(
$pdo,
(int)$doenerbude_data['id'],
$doenerbude_data['name'],
$doenerbude_data['slug'],
(int)($doenerbude_data['slug_bezahlt'] ?? 0),
$doenerbude_data['beschreibung'] ?? null,
$doenerbude_data['website'] ?? null,
(bool)($doenerbude_data['featured'] ?? false)
);
ob_start();
$theme_dir = $active_frontend;
$page_title = $doenerbude->getName();
$meta_description = 'Bewertungen und Informationen zu ' . $doenerbude->getName();
include __DIR__ . '/frontend/' . $active_frontend . '/doenerbude_page.php';
$content = ob_get_clean();
saveToFile($content, $slug);
echo $content;
$page_found = true;
}
}
} catch (Exception $e) {
header("HTTP/1.0 500 Internal Server Error");
echo "
Internal Server Error
";
echo "Failed to query database: " . $e->getMessage();
exit;
}
// If no page was found (neither location nor doenerbude), show 404
if (!$page_found) {
header("HTTP/1.0 404 Not Found");
echo "
404 Not Found
";
echo "The requested resource was not found.";
exit;
}
?>