Skip to content

Commit

Permalink
improvements in content db and other important tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mskian committed Nov 26, 2024
1 parent 1e4092a commit de73a36
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 66 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
- HTML
- Bulma CSS
- SEO Meta tags
- Pagination
- 404 Page
- Dynamic Sitemap and Robots.txt

## Content Database

- Add kavithai in `content/quotes.md` Check the Below Format
- Add kavithai in `content` Check the Below Format
- keep File name as `quote1.md, quote2.md etc...`

```md
# Kavithai 1
Expand Down
8 changes: 8 additions & 0 deletions content/quote1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# உயரத்திற்கு கொண்டு செல்லும் படிகளாக மாறுவார்கள்
slug: kavithai-7
content: "உங்களை தாழ்வாக
பேசும் மனிதர்கள்
ஒருநாள் உங்களை
உயரத்திற்கு
கொண்டு செல்லும்
படிகளாக மாறுவார்கள்"
3 changes: 3 additions & 0 deletions content/quote2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# வருங்காலத்தை கட்டமைக்கின்றது
slug: kavithai-8
content: "உங்கள் முயற்சிதான் உங்கள் வருங்காலத்தை கட்டமைக்கின்றது"
5 changes: 5 additions & 0 deletions content/quote3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# அது உழைப்பின் முடிவு
slug: kavithai-6
content: "வெற்றியென்றால் சாதனையின்
கதை அல்ல
அது உழைப்பின் முடிவு"
3 changes: 3 additions & 0 deletions content/quote4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# நாளைய வெற்றியை தீர்மானிக்கிறது
slug: kavithai-1
content: "இன்று செய்யும் முயற்சியே நாளைய வெற்றியை தீர்மானிக்கிறது"
3 changes: 3 additions & 0 deletions content/quote5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# தன்னம்பிக்கை வேண்டும்
slug: kavithai-2
content: "வெற்றி பெற துணிவு வேண்டும் தோல்வியை ஏற்க தன்னம்பிக்கை வேண்டும்"
4 changes: 4 additions & 0 deletions content/quote6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# வெற்றிக்கான எல்லை அருகில் இருக்கிறது
slug: kavithai-3
content: "துணிவுடன் ஒரு அடியை எடுத்து வை
வெற்றிக்கான எல்லை அருகில் இருக்கிறது"
4 changes: 4 additions & 0 deletions content/quote7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# உனக்குள் உள்ள சக்தியை உணரச் செய்கிறது
slug: kavithai-4
content: "வாழ்க்கை உன்னை எதற்கு சோதிக்கிறது என கேட்காதே,
அது உனக்குள் உள்ள சக்தியை உணரச் செய்கிறது"
5 changes: 5 additions & 0 deletions content/quote8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# சூரியன் மறைந்தாலும் நாளை மீண்டும் உதிக்கும்
slug: kavithai-5
content: "வாழ்க்கையில் பாதைகள் மாறலாம்,
ஆனால் பயணம் தொடர வேண்டும்;
சூரியன் மறைந்தாலும், நாளை மீண்டும் உதிக்கும்"
42 changes: 0 additions & 42 deletions content/quotes.md

This file was deleted.

90 changes: 90 additions & 0 deletions includes/404.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* Handle and display a 404 error page with a custom message.
*
* @param string $message The error message to display.
*/
function showErrorPage(string $message): void
{
// Validate server protocol
$protocol = $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0';
if (!in_array($protocol, ['HTTP/1.0', 'HTTP/1.1', 'HTTP/2'], true)) {
$protocol = 'HTTP/1.0';
}

// Send 404 HTTP response code and security headers
header("$protocol 404 Not Found", true, 404);
header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
header('X-Content-Type-Options: nosniff');
header('Strict-Transport-Security: max-age=63072000; includeSubDomains');
header('Referrer-Policy: no-referrer');

// Sanitize the message
$safeMessage = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');

// Render the 404 error page
echo <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>404 - Not Found</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Catamaran:wght@100..900&display=swap" rel="stylesheet">
<style>
body {
margin: 0;
font-family: 'Catamaran', sans-serif;
background: linear-gradient(135deg, rgba(255, 120, 150, 0.8), rgba(255, 227, 67, 0.8));
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.error-container {
text-align: center;
background: #fff;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
max-width: 600px;
}
.error-container h1 {
font-size: 2.5rem;
color: #e63946;
margin-bottom: 1rem;
}
.error-container p {
font-size: 1.2rem;
margin: 1rem 0;
}
.error-container a {
color: #007bff;
text-decoration: none;
font-weight: bold;
font-size: 1rem;
}
.error-container a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="error-container">
<h1>404 - Page Not Found</h1>
<p>{$safeMessage}</p>
<p><a href="/">Return to Homepage</a></p>
</div>
</body>
</html>
HTML;

// Terminate script after displaying the error page
exit;
}
80 changes: 65 additions & 15 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
function parseMarkdownFile(string $filename): array {
$file_path = CONTENT_DIR . '/' . $filename . '.md';

// Ensure the file exists before proceeding
if (!file_exists($file_path)) {
throw new Exception("File not found: $filename.md");
}

$content = file_get_contents($file_path);
// Attempt to read the file content
$content = @file_get_contents($file_path);
if ($content === false) {
throw new Exception("Unable to read the file: $filename.md");
}
Expand All @@ -34,10 +36,9 @@ function parseMarkdownQuotes(string $markdown): array {

if (preg_match_all($pattern, $markdown, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {

$quotes[] = [
'title' => escapeHtml($match[1]),
'slug' => escapeHtml($match[2]),
'title' => escapeHtml($match[1]),
'slug' => escapeHtml($match[2]),
'content' => escapeHtml($match[3])
];
}
Expand All @@ -47,19 +48,35 @@ function parseMarkdownQuotes(string $markdown): array {
}

/**
* Get quotes for a specific page, with pagination.
* Get quotes from separate markdown files with pagination.
*
* @param int $page The page number to retrieve.
* @return array Paginated quotes.
* @throws Exception If there is an issue retrieving quotes.
*/
function getQuotesForPage(int $page = 1): array {
try {
$quotes = parseMarkdownFile('quotes');
// Get the list of all markdown files in the quotes directory.
$files = glob(CONTENT_DIR . '/quote*.md');
if (!$files) {
throw new Exception("No quote markdown files found in the directory.");
}

$quotes = [];
// Loop through files and parse quotes.
foreach ($files as $file) {
$content = @file_get_contents($file);
if ($content === false) {
throw new Exception("Unable to read the file: $file");
}
$quotes = array_merge($quotes, parseMarkdownQuotes($content));
}

// Implement pagination
$offset = ($page - 1) * ITEMS_PER_PAGE;
return array_slice($quotes, $offset, ITEMS_PER_PAGE);
} catch (Exception $e) {

// Catch and log error, then rethrow with message
throw new Exception("Error fetching quotes for page $page: " . $e->getMessage());
}
}
Expand All @@ -71,10 +88,24 @@ function getQuotesForPage(int $page = 1): array {
*/
function getTotalPages(): int {
try {
$quotes = parseMarkdownFile('quotes');
$files = glob(CONTENT_DIR . '/quote*.md');
if (!$files) {
throw new Exception("No quote markdown files found in the directory.");
}

$quotes = [];
// Loop through files and parse quotes
foreach ($files as $file) {
$content = @file_get_contents($file);
if ($content === false) {
throw new Exception("Unable to read the file: $file");
}
$quotes = array_merge($quotes, parseMarkdownQuotes($content));
}

return ceil(count($quotes) / ITEMS_PER_PAGE);
} catch (Exception $e) {

// Catch and log error, then rethrow with message
throw new Exception("Error calculating total pages: " . $e->getMessage());
}
}
Expand All @@ -87,15 +118,28 @@ function getTotalPages(): int {
*/
function getQuoteBySlug(string $slug): ?array {
try {
$quotes = parseMarkdownFile('quotes');
foreach ($quotes as $quote) {
if ($quote['slug'] === $slug) {
return $quote;
$files = glob(CONTENT_DIR . '/quote*.md');
if (!$files) {
throw new Exception("No quote markdown files found in the directory.");
}

foreach ($files as $file) {
$content = @file_get_contents($file);
if ($content === false) {
throw new Exception("Unable to read the file: $file");
}

$quotes = parseMarkdownQuotes($content);
foreach ($quotes as $quote) {
if ($quote['slug'] === $slug) {
return $quote;
}
}
}

return null;
} catch (Exception $e) {

// Catch and log error, then rethrow with message
throw new Exception("Error fetching quote by slug '$slug': " . $e->getMessage());
}
}
Expand All @@ -105,10 +149,16 @@ function getQuoteBySlug(string $slug): ?array {
*
* @param mixed $page The page number to validate.
* @return int The validated page number.
* @throws InvalidArgumentException If the page is invalid.
*/
function validatePage($page): int {
$validatedPage = filter_var($page, FILTER_VALIDATE_INT, ["options" => ["min_range" => 1]]);
return $validatedPage ? $validatedPage : 1;

if ($validatedPage === false) {
throw new InvalidArgumentException('Invalid page number.');
}

return $validatedPage;
}

/**
Expand Down
10 changes: 4 additions & 6 deletions includes/single.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_once('./includes/config.php');
require_once('./includes/functions.php');
require_once('./includes/404.php');

header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
Expand All @@ -19,8 +20,7 @@ function getFullUrl() {
$slug = filter_var($_GET['slug'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);

if (!preg_match('/^[a-zA-Z0-9\-]+$/', $slug)) {
echo "<h2 class='title'>Invalid slug format.</h2>";
require_once('./includes/footer.php');
showErrorPage('Invalid slug format');
exit;
}

Expand All @@ -32,13 +32,11 @@ function getFullUrl() {
}
} catch (Exception $e) {
error_log($e->getMessage());
echo "<h2 class='title'>An error occurred. Please try again later.</h2>";
require_once('./includes/footer.php');
showErrorPage('404 - Quote not found.');
exit;
}
} else {
echo "<h2 class='title'>Invalid request.</h2>";
require_once('./includes/footer.php');
showErrorPage('Invalid request.');
exit;
}

Expand Down
Loading

0 comments on commit de73a36

Please sign in to comment.