-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview-post.php
More file actions
195 lines (179 loc) · 4.47 KB
/
Copy pathview-post.php
File metadata and controls
195 lines (179 loc) · 4.47 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
<?php
require 'lib/common.php';
require 'lib/view-post.php';
session_start();
try {
$pdo = connectDatabase();
$postId = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$postId) {
redirectAndExit('index.php?not-found=1');
}
$row = getPostRow($pdo, $postId);
if (!$row) {
throw new Exception('No post found with the specified ID.');
}
$bodyText = escapeHtml($row['body']);
$paraText = nl2br($bodyText);
$commentData = ['name' => '', 'website' => '', 'text' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$commentData = [
'name' => filter_input(INPUT_POST, 'comment-name', FILTER_SANITIZE_STRING),
'website' => filter_input(INPUT_POST, 'comment-website', FILTER_SANITIZE_URL),
'text' => filter_input(INPUT_POST, 'comment-text', FILTER_SANITIZE_STRING),
];
$errors = addCommentToPost($pdo, $postId, $commentData);
if (empty($errors)) {
redirectAndExit('view-post.php?id=' . $postId);
}
}
} catch (Exception $e) {
echo '<p>Error: ' . escapeHtml($e->getMessage()) . '</p>';
echo '<p><a href="index.php">Back to Blog</a></p>';
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo escapeHtml($row['title']); ?> | My Blog</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f0f2f5;
margin: 0;
padding: 0;
line-height: 1.6;
}
header, footer {
text-align: center;
padding: 20px;
background-color: #007BFF;
color: white;
}
header h1, footer {
margin: 0;
}
main {
max-width: 800px;
margin: 30px auto;
padding: 20px;
}
.post-card {
background-color: white;
padding: 25px;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
margin-bottom: 40px;
}
.post-card h2 {
font-size: 2rem;
margin-bottom: 10px;
color: #333;
}
.post-meta {
font-size: 0.9rem;
color: #777;
margin-bottom: 20px;
}
.post-body {
font-size: 1.1rem;
color: #444;
line-height: 1.8;
}
.comment-section {
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0,0,0,0.05);
}
.comment-section h3 {
margin-top: 0;
color: #007BFF;
}
.comment {
border-top: 1px solid #eee;
padding: 15px 0;
}
.comment:first-child { border-top: none; }
.comment-author {
font-weight: bold;
color: #007BFF;
}
.comment-date {
font-size: 0.85rem;
color: #777;
margin-left: 10px;
}
.comment-text {
margin-top: 5px;
color: #555;
}
.comment-form input, .comment-form textarea {
width: 100%;
padding: 10px;
margin-top: 5px;
margin-bottom: 15px;
border-radius: 5px;
border: 1px solid #ccc;
font-size: 1rem;
}
.comment-form button {
background-color: #007BFF;
color: white;
padding: 10px 15px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.comment-form button:hover {
background-color: #0056b3;
}
.back-btn {
display: inline-block;
margin-bottom: 20px;
text-decoration: none;
background-color: #6c757d;
color: white;
padding: 8px 15px;
border-radius: 5px;
transition: 0.3s;
}
.back-btn:hover {
background-color: #495057;
}
</style>
</head>
<body>
<header>
<h1>My Blog</h1>
</header>
<main>
<a class="back-btn" href="index.php">← Back to My Posts</a>
<div class="post-card">
<h2><?php echo escapeHtml($row['title']); ?></h2>
<div class="post-meta">
Published on <?php echo convertSqlDate($row['created_at']); ?>
</div>
<div class="post-body">
<?php echo $paraText; ?>
</div>
</div>
<div class="comment-section">
<h3><?php echo countCommentsForPost($postId); ?> Comments</h3>
<?php foreach (getCommentsForPost($postId) as $comment): ?>
<div class="comment">
<span class="comment-author"><?php echo escapeHtml($comment['name']); ?></span>
<span class="comment-date"><?php echo convertSqlDate($comment['created_at']); ?></span>
<div class="comment-text"><?php echo nl2br(escapeHtml($comment['text'])); ?></div>
</div>
<?php endforeach; ?>
<?php require 'templates/comment-form.php'; ?>
</div>
</main>
<footer>
© <?php echo date("Y"); ?> My Blog. All rights reserved.
</footer>
</body>
</html>