Certainly! I’ve reviewed your PHP code and made several improvements to clean it up and address potential issues. Below is the revised code, along with explanations for each change:
1. Consistent Use of Constants
Changed all constants to use const for consistency and modern best practices:
<?php
const POSTS_PER_PAGE = 5;
require 'Parsedown.php';
$Parsedown = new Parsedown();
$Parsedown->setSafeMode(true); // Ensure safe parsing to prevent XSS
// Define your desired date formats
const FILE_DATE_FORMAT = 'Y-m-d'; // Format for file names
const DISPLAY_DATE_FORMAT = 'F j, Y'; // Format for displaying post dates
2. Applying Search Filtering Before Pagination
Modified the get_posts function to apply the search filter before pagination to ensure accurate results when searching:
function get_posts(int $page, string $search_query = ''): array {
$posts = get_filtered_posts($search_query);
// Sort posts by date (newest first)
usort($posts, function($a, $b) {
$a_time = !empty($a['timestamp']) ? $a['timestamp'] : 0;
$b_time = !empty($b['timestamp']) ? $b['timestamp'] : 0;
return $b_time - $a_time;
});
// Paginate the sorted posts
return array_slice($posts, ($page - 1) * POSTS_PER_PAGE, POSTS_PER_PAGE);
}
3. Consolidated File Parsing and Filtering
Introduced a new get_filtered_posts function to avoid duplicate code and improve efficiency:
function get_filtered_posts(string $search_query = ''): array {
$post_dir = 'posts';
$posts = [];
// Get all markdown files from the 'posts' directory
$files = glob("$post_dir/*.md");
foreach ($files as $filepath) {
$post = parse_markdown($filepath);
// Filter posts by search query if provided
if ($search_query === '' ||
stripos($post['title'], $search_query) !== false ||
stripos($post['content'], $search_query) !== false) {
$post['filename'] = basename($filepath);
$posts[] = $post;
}
}
return $posts;
}
4. Improved Date Handling
Enhanced the date parsing in parse_markdown for better robustness and reliability:
function parse_markdown(string $filepath): array {
if (!file_exists($filepath)) {
return [
'title' => 'Untitled',
'date' => '',
'timestamp' => 0,
'content' => '',
];
}
$file_contents = file_get_contents($filepath);
// Split content into front matter and markdown content using regex
if (preg_match('/---\s*(.*?)\s*---\s*(.*)/s', $file_contents, $matches)) {
$front_matter = parse_yaml($matches[1]);
$content = trim($matches[2]);
} else {
$front_matter = [];
$content = trim($file_contents);
}
// Parse the date from front matter
$date_str = $front_matter['date'] ?? '';
$timestamp = strtotime($date_str);
$formatted_date = $timestamp ? date(DISPLAY_DATE_FORMAT, $timestamp) : '';
return [
'title' => htmlspecialchars($front_matter['title'] ?? 'Untitled'),
'date' => $formatted_date,
'timestamp' => $timestamp ?: filemtime($filepath),
'content' => $content,
];
}
5. Added Safe Mode to Parsedown
Enabled safe mode in Parsedown to prevent potential XSS vulnerabilities from untrusted markdown content:
$Parsedown = new Parsedown();
$Parsedown->setSafeMode(true); // Prevents unsafe HTML output
6. Updated Pagination and Search Handling
Modified the pagination and search logic to reflect the changes:
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
$search_query = isset($_GET['search']) ? trim($_GET['search']) : '';
$posts = get_posts($page, $search_query);
$total_pages = get_total_pages($search_query);
7. Ensured Proper Output Escaping
Verified that all outputs are properly escaped to prevent XSS attacks:
<h2><a href="view.php?file=<?= urlencode($post['filename']) ?>"><?= htmlspecialchars($post['title']) ?></a></h2>
<div class="date"><?= htmlspecialchars($post['date']) ?></div>
8. Improved YAML Parsing Function
While keeping the YAML parser simple, made it more robust to handle keys without spaces around the colon:
function parse_yaml(string $yaml_string): array {
$lines = explode("\n", $yaml_string);
$data = [];
foreach ($lines as $line) {
if (preg_match('/^\s*([^\s:]+)\s*:\s*(.*?)\s*$/', $line, $matches)) {
$key = trim($matches[1]);
$value = trim($matches[2], '"\''); // Remove quotes if present
$data[$key] = $value;
}
}
return $data;
}
9. Corrected View Links and File Handling
Ensured that the view.php file securely handles the file parameter and that only valid filenames are used:
// In view.php
$filename = basename($_GET['file']); // Prevent directory traversal
$filepath = "posts/$filename";
if (file_exists($filepath) && pathinfo($filepath, PATHINFO_EXTENSION) === 'md') {
// Proceed to display the post
} else {
// Handle error
}
10. Overall Code Cleanup
Performed general code cleanup for readability and adherence to coding standards, such as adding proper indentation, using consistent naming conventions, and adding comments where necessary.
Final Revised Code:
<?php
const POSTS_PER_PAGE = 5;
require 'Parsedown.php';
$Parsedown = new Parsedown();
$Parsedown->setSafeMode(true); // Ensure safe parsing to prevent XSS
// Define your desired date formats
const FILE_DATE_FORMAT = 'Y-m-d'; // Format for file names
const DISPLAY_DATE_FORMAT = 'F j, Y'; // Format for displaying post dates
function get_filtered_posts(string $search_query = ''): array {
$post_dir = 'posts';
$posts = [];
// Get all markdown files from the 'posts' directory
$files = glob("$post_dir/*.md");
foreach ($files as $filepath) {
$post = parse_markdown($filepath);
// Filter posts by search query if provided
if ($search_query === '' ||
stripos($post['title'], $search_query) !== false ||
stripos($post['content'], $search_query) !== false) {
$post['filename'] = basename($filepath);
$posts[] = $post;
}
}
return $posts;
}
function get_posts(int $page, string $search_query = ''): array {
$posts = get_filtered_posts($search_query);
// Sort posts by date (newest first)
usort($posts, function($a, $b) {
$a_time = $a['timestamp'] ?? 0;
$b_time = $b['timestamp'] ?? 0;
return $b_time - $a_time;
});
// Paginate the sorted posts
return array_slice($posts, ($page - 1) * POSTS_PER_PAGE, POSTS_PER_PAGE);
}
function get_total_pages(string $search_query = ''): int {
$posts = get_filtered_posts($search_query);
return ceil(count($posts) / POSTS_PER_PAGE);
}
// Function to parse YAML front matter and content from .md files
function parse_markdown(string $filepath): array {
if (!file_exists($filepath)) {
return [
'title' => 'Untitled',
'date' => '',
'timestamp' => 0,
'content' => '',
];
}
$file_contents = file_get_contents($filepath);
// Split content into front matter and markdown content using regex
if (preg_match('/---\s*(.*?)\s*---\s*(.*)/s', $file_contents, $matches)) {
$front_matter = parse_yaml($matches[1]);
$content = trim($matches[2]);
} else {
$front_matter = [];
$content = trim($file_contents);
}
// Parse the date from front matter
$date_str = $front_matter['date'] ?? '';
$timestamp = strtotime($date_str);
$formatted_date = $timestamp ? date(DISPLAY_DATE_FORMAT, $timestamp) : '';
return [
'title' => htmlspecialchars($front_matter['title'] ?? 'Untitled'),
'date' => $formatted_date,
'timestamp' => $timestamp ?: filemtime($filepath),
'content' => $content,
];
}
// Improved YAML parser
function parse_yaml(string $yaml_string): array {
$lines = explode("\n", $yaml_string);
$data = [];
foreach ($lines as $line) {
if (preg_match('/^\s*([^\s:]+)\s*:\s*(.*?)\s*$/', $line, $matches)) {
$key = trim($matches[1]);
$value = trim($matches[2], '"\''); // Remove quotes if present
$data[$key] = $value;
}
}
return $data;
}
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
$search_query = isset($_GET['search']) ? trim($_GET['search']) : '';
$posts = get_posts($page, $search_query);
$total_pages = get_total_pages($search_query);
?>
<?php include 'includes/header.php'; ?>
<?php if (count($posts) > 0): ?>
<?php foreach ($posts as $post): ?>
<div class="post">
<img class="avatar" src="images/avatar.png" alt="Avatar" />
<h2><a href="view.php?file=<?= urlencode($post['filename']) ?>"><?= htmlspecialchars($post['title']) ?></a></h2>
<div class="date"><?= htmlspecialchars($post['date']) ?></div>
<?php
// Check if there's a 'Read More' tag in the post content
if (stripos($post['content'], '<!--more-->') !== false) {
list($content, $rest) = explode('<!--more-->', $post['content'], 2);
echo $Parsedown->text($content);
echo '<p><a href="view.php?file=' . urlencode($post['filename']) . '">Read More</a></p>';
} else {
// Display full content if no 'Read More' tag is found
echo $Parsedown->text($post['content']);
}
?>
</div>
<?php endforeach; ?>
<?php else: ?>
<div style="text-align: center; margin: 100px; border: 0;" class="post">
<h2>Nothing Found</h2>
</div>
<?php endif; ?>
<div class="pagination">
<?php if ($page > 1): ?>
<a href="index.php?page=<?= $page - 1 ?>&search=<?= urlencode($search_query) ?>">« Previous</a>
<?php endif; ?>
<?php if ($page < $total_pages): ?>
<a href="index.php?page=<?= $page + 1 ?>&search=<?= urlencode($search_query) ?>">Next »</a>
<?php endif; ?>
</div>
<?php include 'includes/footer.php'; ?>
Notes:
- Search Filtering Before Pagination: By filtering posts before pagination, users will see accurate results that match their search query across all pages.
- Improved Date Handling: The date parsing now checks if
strtotime returns a valid timestamp. If not, it falls back to the file's modification time.
- Safe Markdown Parsing: Enabling
setSafeMode(true) in Parsedown ensures that any HTML within the markdown content is sanitized, preventing potential XSS attacks.
- Proper Escaping: Used
htmlspecialchars when outputting titles and dates to prevent XSS and ensure that special characters are displayed correctly.
- Optimized File Handling: Used
glob to get markdown files, which simplifies the code and improves performance.
- YAML Parsing: The
parse_yaml function now handles cases where there might not be a space after the colon and strips any leading/trailing whitespace.
Additional Recommendations:
- Use a Robust YAML Parser: For more complex front matter, consider using a dedicated YAML parsing library like Symfony YAML Component or Spyc.
- Caching: If performance becomes an issue with a large number of posts, implement caching to store parsed post data temporarily.
- Input Validation: Ensure that all user inputs, including
$_GET parameters, are properly validated and sanitized.
- Error Handling: Add error handling for file operations and parsing to handle possible exceptions or invalid data gracefully.
By incorporating these changes, the code becomes cleaner, more efficient, and more secure. Let me know if you have any questions or need further assistance!