-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle.php
More file actions
240 lines (214 loc) · 10.4 KB
/
article.php
File metadata and controls
240 lines (214 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
// =====================================================
// Single Article View with Comments
// =====================================================
session_start();
require_once 'includes/config.php';
// Get article URL from query string
$article_url = isset($_GET['url']) ? trim($_GET['url']) : '';
if (empty($article_url)) {
header('Location: index.php');
exit;
}
// Fetch article
$stmt = mysqli_prepare($conn, "SELECT * FROM articles WHERE article_url = ? AND is_visible = 1");
mysqli_stmt_bind_param($stmt, 's', $article_url);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$article = mysqli_fetch_assoc($result);
// If article not found, redirect to homepage
if (!$article) {
header('Location: index.php');
exit;
}
// Increment view counter
mysqli_query($conn, "UPDATE articles SET user_views = user_views + 1 WHERE id = " . $article['id']);
$article['user_views']++;
// Handle comment submission with PRG pattern (Post-Redirect-Get)
$comment_success = '';
$comment_error = '';
// Check for success/error messages from session
$scroll_to_comments = false;
if (isset($_SESSION['comment_success'])) {
$comment_success = $_SESSION['comment_success'];
unset($_SESSION['comment_success']);
$scroll_to_comments = true;
} elseif (isset($_SESSION['comment_error'])) {
$comment_error = $_SESSION['comment_error'];
unset($_SESSION['comment_error']);
$scroll_to_comments = true;
}
if (isset($_SESSION['scroll_to_comments'])) {
unset($_SESSION['scroll_to_comments']);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_comment'])) {
$user_name = trim($_POST['user_name'] ?? '');
$user_email = trim($_POST['user_email'] ?? '');
$comment_text = trim($_POST['comment_text'] ?? '');
if (empty($user_name) || empty($comment_text)) {
$comment_error = 'Please enter your name and comment.';
} else {
$stmt = mysqli_prepare($conn, "INSERT INTO comments (article_id, user_name, user_email, comment_text) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'isss', $article['id'], $user_name, $user_email, $comment_text);
if (mysqli_stmt_execute($stmt)) {
$_SESSION['comment_success'] = 'Your comment has been submitted, Thank You!';
$_SESSION['scroll_to_comments'] = true;
header('Location: ' . SITE_URL . 'article/' . urlencode($article_url));
exit;
} else {
$_SESSION['comment_error'] = 'Failed to post comment. Please try again.';
$_SESSION['scroll_to_comments'] = true;
header('Location: ' . SITE_URL . 'article/' . urlencode($article_url));
exit;
}
}
}
// Get approved comments for this article
$comments_result = mysqli_query($conn, "SELECT * FROM comments WHERE article_id = " . $article['id'] . " AND is_approved = 1 ORDER BY created_at DESC");
$comments_count = mysqli_num_rows($comments_result);
// Set page meta data
$page_title = $article['meta_title'] . ' - ' . SITE_NAME;
$meta_description = $article['meta_description'];
$meta_keywords = $article['meta_keywords'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="<?php echo htmlspecialchars($meta_description); ?>">
<meta name="keywords" content="<?php echo htmlspecialchars($meta_keywords); ?>">
<title><?php echo htmlspecialchars($page_title); ?></title>
<link rel="icon" type="image/x-icon" href="<?php echo SITE_URL; ?>gif/favicon.ico">
<link rel="stylesheet" href="<?php echo SITE_URL; ?>css/frontend.css">
</head>
<body>
<!-- Navigation Menu -->
<nav class="navbar">
<div class="container">
<a href="<?php echo SITE_URL; ?>" class="logo"><img src="<?php echo SITE_URL; ?>gif/blog_system_logo.svg" alt="<?php echo SITE_NAME; ?>"></a>
<button class="menu-toggle" onclick="toggleMenu()">
<span></span>
<span></span>
<span></span>
</button>
<ul class="nav-menu" id="navMenu">
<li><a href="<?php echo SITE_URL; ?>">Home</a></li>
<li><a href="<?php echo SITE_URL; ?>articles.php">All Articles</a></li>
<li><a href="<?php echo SITE_URL; ?>most-read.php">Most Read</a></li>
<li><a href="<?php echo SITE_URL; ?>about.php">About</a></li>
<li><a href="<?php echo SITE_URL; ?>contact.php">Contact</a></li>
</ul>
</div>
</nav>
<main class="main-content">
<div class="container">
<article class="single-article">
<!-- Back to articles link -->
<a href="<?php echo SITE_URL; ?>articles.php" class="back-link">← Back to Articles</a>
<!-- Article Header -->
<header class="article-header">
<h1><?php echo htmlspecialchars($article['article_title']); ?></h1>
<p class="article-meta">
<span class="date">Published on <?php echo date('F d, Y', strtotime($article['created_at'])); ?></span>
<?php if ($article['updated_at'] !== $article['created_at']): ?>
<span class="updated">• Updated on <?php echo date('F d, Y', strtotime($article['updated_at'])); ?></span>
<?php endif; ?>
<span class="views">• <?php echo number_format($article['user_views']); ?> views</span>
<span class="comments-count">• <?php echo $comments_count; ?> comments</span>
</p>
</header>
<!-- Article Content -->
<div class="article-body">
<?php
// Fix relative image paths to absolute
$content = $article['article_description'];
// Convert relative uploads/ paths to absolute
$content = preg_replace('/src=["\']uploads\//', 'src="' . SITE_URL . 'uploads/', $content);
// Also handle ../uploads/ paths (from admin)
$content = preg_replace('/src=["\']\.\.\/uploads\//', 'src="' . SITE_URL . 'uploads/', $content);
echo $content;
?>
</div>
<!-- Article Footer -->
<footer class="article-footer">
<?php if ($article['meta_keywords']): ?>
<div class="article-tags">
<strong>Tags:</strong>
<?php
$keywords = explode(',', $article['meta_keywords']);
foreach ($keywords as $keyword) {
echo '<span class="tag">' . htmlspecialchars(trim($keyword)) . '</span>';
}
?>
</div>
<?php endif; ?>
</footer>
</article>
<!-- Comments Section -->
<section class="comments-section" id="comments">
<h2>Comments (<?php echo $comments_count; ?>)</h2>
<!-- Comment Form -->
<div class="comment-form-wrapper">
<h3>Leave a Comment</h3>
<?php if ($comment_success): ?>
<div class="alert alert-success"><?php echo $comment_success; ?></div>
<?php endif; ?>
<?php if ($comment_error): ?>
<div class="alert alert-error"><?php echo $comment_error; ?></div>
<?php endif; ?>
<form method="POST" action="" class="comment-form">
<div class="form-row">
<div class="form-group">
<label for="user_name">Name *</label>
<input type="text" id="user_name" name="user_name" required placeholder="Your name">
</div>
<div class="form-group">
<label for="user_email">Email (optional)</label>
<input type="email" id="user_email" name="user_email" placeholder="Your email">
</div>
</div>
<div class="form-group">
<label for="comment_text">Comment *</label>
<textarea id="comment_text" name="comment_text" rows="4" required placeholder="Write your comment here..."></textarea>
</div>
<button type="submit" name="submit_comment" class="btn btn-primary">Post Comment</button>
</form>
</div>
<!-- Comments List -->
<?php if ($comments_count > 0): ?>
<div class="comments-list">
<?php while ($comment = mysqli_fetch_assoc($comments_result)): ?>
<div class="comment-item">
<div class="comment-header">
<span class="comment-author"><?php echo htmlspecialchars($comment['user_name']); ?></span>
<span class="comment-date"><?php echo date('F d, Y \a\t h:i A', strtotime($comment['created_at'])); ?></span>
</div>
<div class="comment-body">
<?php echo nl2br(htmlspecialchars($comment['comment_text'])); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php else: ?>
<p class="no-comments">No comments yet. Be the first to comment!</p>
<?php endif; ?>
</section>
</div>
</main>
<!-- Footer -->
<footer class="footer">
<div class="container">
<p>© <?php echo date('Y'); ?> <?php echo SITE_NAME; ?>. All rights reserved.</p>
</div>
</footer>
<script src="<?php echo SITE_URL; ?>js/main.js"></script>
<?php if ($scroll_to_comments): ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('comments').scrollIntoView({ behavior: 'smooth' });
});
</script>
<?php endif; ?>
</body>
</html>