This commit is contained in:
2024-12-16 13:51:59 +00:00
parent 7efb5727c3
commit ebbc4fbc22
7 changed files with 786 additions and 70 deletions

View File

@@ -1,76 +1,56 @@
<?php
require('.hta_config/env.php');
$requestUri = $_SERVER['REQUEST_URI'];
$slug = getSlug($requestUri);
function getSlug($requestUri) {
require('.hta_config/env.php');
$headerContent = file_get_contents('./.hta_header.php');
$requestUri = $_SERVER['REQUEST_URI'];
$parts = explode('/', $requestUri);
return isset($parts[2]) ? $parts[2] : '';
}
$slug = $parts[2];
$fileName = $slug;
$fileExtension = explode('.', $fileName);
function serveStaticFile($filePath) {
if (file_exists($filePath)) {
echo file_get_contents($filePath);
return true;
$querySlug = '';
if($fileExtension[1] && $fileExtension[1] === 'html'){
$querySlug = $fileExtension[0];
} else{
$querySlug = $fileExtension[0];
}
return false;
}
function fetchFromDatabase($slug, $pdo) {
$stmt = $pdo->prepare("SELECT * FROM `scc24` WHERE `slug` = :slug");
$stmt->bindParam(':slug', $slug, PDO::PARAM_STR);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
}
// echo $querySlug;
function saveToCache($filePath, $content) {
if (!file_put_contents($filePath, $content)) {
error_log("Failed to write cache file: $filePath");
}
}
if (empty($slug)) {
// echo "Slug not provided!";
// exit;
}
$fileName = $slug . ".html";
$filePath = __DIR__ . '/notice/' . $fileName;
ob_start();
$shouldCache = false; // Flag to determine if content should be cached.
if (!serveStaticFile($filePath)) {
try {
$pdo = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$content = fetchFromDatabase($slug, $pdo);
if ($content && isset($content['content']) && $content['content']) {
echo '<div class="container mx-auto">
<div style="padding-top:20px; padding-bottom:15px; font-size:25px;">
<p style="text-align: center;">' . htmlspecialchars($content['title']) . '</p>
</div>
<div>' . $content['content'] . '</div>
</div>';
$shouldCache = true; // Mark content as cacheable.
} else {
echo '<div style="display: flex; flex-direction: column; align-items: center; justify-content: center; margin: 100px 0;"><p style="text-align: center; font-size: 30px; font-weight: bold;">Page not found (404).</p>
<a href="/">Back to home</a>
</div>';
}
$conn = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM `scc24` WHERE `slug` = '".$querySlug."'");
$stmt->execute();
$content = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(isset($content[0]['content']) && $content[0]['content'])
$pageContent = '<div class="container mx-auto" style="">
<div class="" style="padding-top:20px; padding-bottom:15px; font-size:25px;"><p style="text-align: center;">'.$content[0]['title'].'</p></div>
<div class="">'.$content[0]['content'].'</div>
</div>';
else $pageContent = '
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center; margin: 100px 0;"><p style="text-align: center; font-size: 30px; font-weight: bold;">Page not found (404).</p>
<a href="/">Back to home</a>
</div>
';
} catch (PDOException $e) {
echo "<p class='text-danger'>Error: " . htmlspecialchars($e->getMessage()) . "</p>";
$in_page_message = "<p class='text-danger'>Error: " . $e->getMessage() . "</p>";
}
}
$htmlContent = ob_get_clean();
// Render page content here
echo $pageContent;
// Save to cache only if content was found.
if ($shouldCache) {
saveToCache($filePath, $htmlContent);
}
$footerContent = file_get_contents('./.hta_footer.php');
// if($fileExtension[1] && $fileExtension[1] === 'html'){
// $filePath = __DIR__ . '/notice/' . $fileName;
// } else{
// $filePath = __DIR__ . '/notice/' . $fileName . '.html';
// }
$filePath = __DIR__ . '/notice/' . $fileName;
$totalPageContent = $headerContent . $pageContent . $footerContent;
file_put_contents($filePath, $totalPageContent);
echo $htmlContent;
?>