pull/7/head
dev2 siliconpin 2024-12-16 08:49:59 +00:00
parent 21fe7928cd
commit ff87ffd713
1 changed files with 62 additions and 61 deletions

View File

@ -1,76 +1,77 @@
<?php <?php
require('.hta_config/env.php'); require('.hta_config/env.php');
// $requestUri = $_SERVER['REQUEST_URI'];
// $slug = getSlug($requestUri);
// function getSlug($requestUri) {
// $parts = explode('/', $requestUri);
// return isset($parts[2]) ? $parts[2] : '';
// }
function getSlug($requestUri) { // function serveStaticFile($filePath) {
$parts = explode('/', $requestUri); // if (file_exists($filePath)) {
return isset($parts[2]) ? $parts[2] : ''; // echo file_get_contents($filePath);
} // return true;
// }
// return false;
// }
function serveStaticFile($filePath) { // function fetchFromDatabase($slug, $pdo) {
if (file_exists($filePath)) { // $stmt = $pdo->prepare("SELECT * FROM `scc24` WHERE `slug` = :slug");
echo file_get_contents($filePath); // $stmt->bindParam(':slug', $slug, PDO::PARAM_STR);
return true; // $stmt->execute();
} // return $stmt->fetch(PDO::FETCH_ASSOC);
return false; // }
}
function fetchFromDatabase($slug, $pdo) { // function saveToCache($filePath, $content) {
$stmt = $pdo->prepare("SELECT * FROM `scc24` WHERE `slug` = :slug"); // if (!file_put_contents($filePath, $content)) {
$stmt->bindParam(':slug', $slug, PDO::PARAM_STR); // error_log("Failed to write cache file: $filePath");
$stmt->execute(); // }
return $stmt->fetch(PDO::FETCH_ASSOC); // }
}
function saveToCache($filePath, $content) {
if (!file_put_contents($filePath, $content)) {
error_log("Failed to write cache file: $filePath");
}
}
$requestUri = $_SERVER['REQUEST_URI'];
$slug = getSlug($requestUri);
if (empty($slug)) { // if (empty($slug)) {
// echo "Slug not provided!"; // // echo "Slug not provided!";
// exit; // // exit;
} // }
$fileName = $slug . ".html"; // $fileName = $slug . ".html";
$filePath = __DIR__ . '/notice/' . $fileName; // $filePath = __DIR__ . '/notice/' . $fileName;
ob_start(); // ob_start();
$shouldCache = false; // Flag to determine if content should be cached. // $shouldCache = false; // Flag to determine if content should be cached.
if (!serveStaticFile($filePath)) { // if (!serveStaticFile($filePath)) {
try { // try {
$pdo = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass); // $pdo = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$content = fetchFromDatabase($slug, $pdo); // $content = fetchFromDatabase($slug, $pdo);
if ($content && isset($content['content']) && $content['content']) { // if ($content && isset($content['content']) && $content['content']) {
echo '<div class="container mx-auto"> // echo '<div class="container mx-auto">
<div style="padding-top:20px; padding-bottom:15px; font-size:25px;"> // <div style="padding-top:20px; padding-bottom:15px; font-size:25px;">
<p style="text-align: center;">' . htmlspecialchars($content['title']) . '</p> // <p style="text-align: center;">' . htmlspecialchars($content['title']) . '</p>
</div> // </div>
<div>' . $content['content'] . '</div> // <div>' . $content['content'] . '</div>
</div>'; // </div>';
$shouldCache = true; // Mark content as cacheable. // $shouldCache = true; // Mark content as cacheable.
} else { // } 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> // 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> // <a href="/">Back to home</a>
</div>'; // </div>';
} // }
} catch (PDOException $e) { // } catch (PDOException $e) {
echo "<p class='text-danger'>Error: " . htmlspecialchars($e->getMessage()) . "</p>"; // echo "<p class='text-danger'>Error: " . htmlspecialchars($e->getMessage()) . "</p>";
} // }
} // }
$htmlContent = ob_get_clean(); // $htmlContent = ob_get_clean();
// Save to cache only if content was found. // // Save to cache only if content was found.
if ($shouldCache) { // if ($shouldCache) {
saveToCache($filePath, $htmlContent); // saveToCache($filePath, $htmlContent);
} // }
echo $htmlContent; // echo $htmlContent;
?> ?>
<h2>404 New</h2>