-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite.php
More file actions
374 lines (325 loc) · 15.4 KB
/
write.php
File metadata and controls
374 lines (325 loc) · 15.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
session_start();
$db = new SQLite3('info.db');
// Define validation variables
$errors = [];
$success_message = '';
if (!isset($_SESSION['username']))
{
header("Location: login.php");
exit();
}
$username = $_SESSION['username'];
// Helper function to validate and sanitize inputs
function validateInput($data) {
$data = trim($data);
$data = stripslashes($data);
// Use ENT_NOQUOTES to preserve quotes in the content while still preventing XSS
$data = htmlspecialchars($data, ENT_NOQUOTES, 'UTF-8');
return $data;
}
// Helper function to validate date format (YYYY-MM-DD)
function isValidDate($date) {
if (empty($date)) return false;
$date_parts = explode('-', $date);
if (count($date_parts) !== 3) return false;
return checkdate($date_parts[1], $date_parts[2], $date_parts[0]);
}
// Helper function to validate chronological order of dates
function datesInOrder($start_date, $end_date) {
$start = strtotime($start_date);
$end = strtotime($end_date);
return $start <= $end;
}
// Handle Creating a New Article
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['create_article']))
{
// Validate and sanitize inputs
$title = validateInput($_POST['article_title'] ?? '');
$body = validateInput($_POST['article_body'] ?? '');
$start_date = validateInput($_POST['start_date'] ?? '');
$end_date = validateInput($_POST['end_date'] ?? '');
// Check required fields
if (empty($title)) {
$errors[] = "Article title is required";
}
if (empty($body)) {
$errors[] = "Article body is required";
}
// Validate dates
if (empty($start_date)) {
$errors[] = "Start date is required";
} elseif (!isValidDate($start_date)) {
$errors[] = "Invalid start date format";
}
if (empty($end_date)) {
$errors[] = "End date is required";
} elseif (!isValidDate($end_date)) {
$errors[] = "Invalid end date format";
}
// Check that end date is not before start date
if (isValidDate($start_date) && isValidDate($end_date) && !datesInOrder($start_date, $end_date)) {
$errors[] = "End date cannot be before start date";
}
// If validation passes, insert the article
if (empty($errors))
{
try {
// Get the current date in Pacific Standard Time
$current_date = new DateTime('now', new DateTimeZone('America/Los_Angeles'));
$formatted_date = $current_date->format('Y-m-d');
$stmt = $db->prepare("INSERT INTO Articles (ArticleTitle, ArticleBody, CreateDate, StartDate, EndDate, ContributerUsername) VALUES (:title, :body, :create_date, :start_date, :end_date, :username)");
$stmt->bindValue(':title', $title, SQLITE3_TEXT);
$stmt->bindValue(':body', $body, SQLITE3_TEXT);
$stmt->bindValue(':create_date', $formatted_date, SQLITE3_TEXT);
$stmt->bindValue(':username', $username, SQLITE3_TEXT);
$stmt->bindValue(':start_date', $start_date, SQLITE3_TEXT);
$stmt->bindValue(':end_date', $end_date, SQLITE3_TEXT);
$result = $stmt->execute();
if ($result) {
$success_message = "Article created successfully!";
// Clear form data after successful submission
$title = $body = $start_date = $end_date = "";
} else {
$errors[] = "Failed to create article. Database error.";
}
} catch (Exception $e) {
$errors[] = "An error occurred: " . $e->getMessage();
}
}
}
// Handle Article Deletion
if (isset($_POST['delete_article']))
{
$article_id = filter_var($_POST['article_id'] ?? 0, FILTER_VALIDATE_INT);
if (!$article_id) {
$errors[] = "Invalid article ID for deletion";
} else {
try {
// Ensure user owns the article before deleting
$stmt = $db->prepare("DELETE FROM Articles WHERE ArticleId = :id AND ContributerUsername = :username");
$stmt->bindValue(':id', $article_id, SQLITE3_INTEGER);
$stmt->bindValue(':username', $username, SQLITE3_TEXT);
$result = $stmt->execute();
if ($result) {
$success_message = "Article deleted successfully!";
} else {
$errors[] = "Failed to delete article. Database error.";
}
} catch (Exception $e) {
$errors[] = "An error occurred: " . $e->getMessage();
}
}
}
// Handle Article Editing
if (isset($_POST['edit_article']))
{
// Validate and sanitize inputs
$article_id = filter_var($_POST['article_id'] ?? 0, FILTER_VALIDATE_INT);
$new_title = validateInput($_POST['article_title'] ?? '');
$new_body = validateInput($_POST['article_body'] ?? '');
$new_start_date = validateInput($_POST['start_date'] ?? '');
$new_end_date = validateInput($_POST['end_date'] ?? '');
// Check required fields and article ID
if (!$article_id) {
$errors[] = "Invalid article ID for editing";
}
if (empty($new_title)) {
$errors[] = "Article title is required";
}
if (empty($new_body)) {
$errors[] = "Article body is required";
}
// Validate dates
if (empty($new_start_date)) {
$errors[] = "Start date is required";
} elseif (!isValidDate($new_start_date)) {
$errors[] = "Invalid start date format";
}
if (empty($new_end_date)) {
$errors[] = "End date is required";
} elseif (!isValidDate($new_end_date)) {
$errors[] = "Invalid end date format";
}
// Check that end date is not before start date
if (isValidDate($new_start_date) && isValidDate($new_end_date) && !datesInOrder($new_start_date, $new_end_date)) {
$errors[] = "End date cannot be before start date";
}
// If validation passes, update the article
if (empty($errors))
{
try {
$stmt = $db->prepare("UPDATE Articles SET ArticleTitle = :title, ArticleBody = :body, StartDate = :start_date, EndDate = :end_date WHERE ArticleId = :id AND ContributerUsername = :username");
$stmt->bindValue(':title', $new_title, SQLITE3_TEXT);
$stmt->bindValue(':body', $new_body, SQLITE3_TEXT);
$stmt->bindValue(':start_date', $new_start_date, SQLITE3_TEXT);
$stmt->bindValue(':end_date', $new_end_date, SQLITE3_TEXT);
$stmt->bindValue(':id', $article_id, SQLITE3_INTEGER);
$stmt->bindValue(':username', $username, SQLITE3_TEXT);
$result = $stmt->execute();
if ($result) {
$success_message = "Article updated successfully!";
} else {
$errors[] = "Failed to update article. Database error.";
}
} catch (Exception $e) {
$errors[] = "An error occurred: " . $e->getMessage();
}
}
}
$articles = $db->query("SELECT * FROM Articles WHERE ContributerUsername = '$username' ORDER BY CreateDate DESC");
?>
<?php include('./inc/inc_header.php'); ?>
<div class="container mt-5">
<h2 class="text-center mb-4">Manage Your Articles</h2>
<!-- Display validation errors -->
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<strong>Error!</strong>
<ul>
<?php foreach ($errors as $error): ?>
<li><?= htmlspecialchars($error) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<!-- Display success message -->
<?php if (!empty($success_message)): ?>
<div class="alert alert-success">
<?= htmlspecialchars($success_message) ?>
</div>
<?php endif; ?>
<!-- Create Article Form -->
<div class="card shadow-lg p-4 mb-5">
<h3 class="mb-3">Write a New Article</h3>
<form method="post" novalidate>
<div class="mb-3">
<label for="article_title" class="form-label fw-bold">Title</label>
<input type="text" class="form-control" name="article_title" id="article_title" required placeholder="Enter article title" value="<?= htmlspecialchars($title ?? '') ?>">
</div>
<div class="mb-3">
<label for="article_body" class="form-label fw-bold">Body</label>
<textarea class="form-control" name="article_body" id="article_body" rows="6" required placeholder="Write your article here..."><?= htmlspecialchars($body ?? '') ?></textarea>
</div>
<!-- Start Date Field -->
<div class="mb-3">
<label for="start_date" class="form-label fw-bold">Start Date</label>
<input type="date" class="form-control" name="start_date" id="start_date" required value="<?= htmlspecialchars($start_date ?? '') ?>">
</div>
<!-- End Date Field -->
<div class="mb-3">
<label for="end_date" class="form-label fw-bold">End Date</label>
<input type="date" class="form-control" name="end_date" id="end_date" required value="<?= htmlspecialchars($end_date ?? '') ?>">
</div>
<button type="submit" name="create_article" class="btn btn-primary">Post Article</button>
</form>
</div>
<!-- List of User Articles -->
<h3 class="mb-3">Your Articles</h3>
<?php while ($row = $articles->fetchArray(SQLITE3_ASSOC)) { ?>
<div class="card mb-3">
<div class="card-body">
<h4 class="card-title"><?= htmlspecialchars($row['ArticleTitle']) ?></h4>
<p class="card-text"><?= nl2br($row['ArticleBody']) ?></p>
<p class="text-muted"><small>Posted on <?= $row['CreateDate'] ?></small></p>
<p class="text-muted"><small>Visible from <?= $row['StartDate'] ?> to <?= $row['EndDate'] ?></small></p>
<!-- Edit Article Button -->
<button class="btn btn-warning edit-article-btn"
data-id="<?= $row['ArticleId'] ?>"
data-title="<?= htmlspecialchars($row['ArticleTitle'], ENT_QUOTES, 'UTF-8') ?>"
data-body="<?= htmlspecialchars($row['ArticleBody'], ENT_QUOTES, 'UTF-8') ?>"
data-start-date="<?= $row['StartDate'] ?>"
data-end-date="<?= $row['EndDate'] ?>">Edit</button>
<!-- Delete Article Form -->
<form method="post" class="d-inline">
<input type="hidden" name="article_id" value="<?= $row['ArticleId'] ?>">
<button type="submit" name="delete_article" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this article?');">Delete</button>
</form>
</div>
</div>
<?php } ?>
</div>
<!-- Edit Article Modal -->
<div id="editArticleModal" class="modal fade" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title text-dark">Edit Article</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<form method="post">
<input type="hidden" name="article_id" id="edit_article_id">
<div class="mb-3">
<label for="edit_article_title" class="form-label fw-bold text-dark">Title</label>
<input type="text" class="form-control" name="article_title" id="edit_article_title" required>
</div>
<div class="mb-3">
<label for="edit_article_body" class="form-label fw-bold text-dark">Body</label>
<textarea class="form-control" name="article_body" id="edit_article_body" rows="6" required></textarea>
</div>
<div class="mb-3">
<label for="edit_article_start_date" class="form-label fw-bold text-dark">Start Date</label>
<input type="date" class="form-control" name="start_date" id="edit_article_start_date" required>
</div>
<div class="mb-3">
<label for="edit_article_end_date" class="form-label fw-bold text-dark">End Date</label>
<input type="date" class="form-control" name="end_date" id="edit_article_end_date" required>
</div>
<button type="submit" name="edit_article" class="btn btn-success">Update Article</button>
</form>
</div>
</div>
</div>
</div>
<!-- JavaScript to Open Edit Modal -->
<script>
// Add event listeners for all edit buttons
document.addEventListener('DOMContentLoaded', function() {
const editButtons = document.querySelectorAll('.edit-article-btn');
// Helper function to safely decode HTML entities
function decodeHtmlEntities(text) {
const textArea = document.createElement('textarea');
textArea.innerHTML = text;
return textArea.value;
}
editButtons.forEach(button => {
button.addEventListener('click', function() {
try {
const id = this.getAttribute('data-id');
const title = decodeHtmlEntities(this.getAttribute('data-title'));
const body = decodeHtmlEntities(this.getAttribute('data-body'));
const startDate = this.getAttribute('data-start-date');
const endDate = this.getAttribute('data-end-date');
// Ensure proper values are set
document.getElementById("edit_article_id").value = id || '';
document.getElementById("edit_article_title").value = title || '';
document.getElementById("edit_article_body").value = body || '';
document.getElementById("edit_article_start_date").value = startDate || '';
document.getElementById("edit_article_end_date").value = endDate || '';
// Open the modal
const modal = new bootstrap.Modal(document.getElementById('editArticleModal'));
modal.show();
} catch (error) {
console.error("Error loading edit modal:", error);
alert("There was an error trying to edit this article. Please try again.");
}
});
});
});
// Keep the old function for backward compatibility
function editArticle(id, title, body, start_date, end_date) {
try {
document.getElementById("edit_article_id").value = id || '';
document.getElementById("edit_article_title").value = title || '';
document.getElementById("edit_article_body").value = body || '';
document.getElementById("edit_article_start_date").value = start_date || '';
document.getElementById("edit_article_end_date").value = end_date || '';
new bootstrap.Modal(document.getElementById('editArticleModal')).show();
} catch (error) {
console.error("Error in editArticle function:", error);
alert("There was an error trying to edit this article. Please try again.");
}
}
</script>
<?php include('./inc/inc_footer.php'); ?>