-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathedit-post.php
More file actions
394 lines (353 loc) · 22.1 KB
/
Copy pathedit-post.php
File metadata and controls
394 lines (353 loc) · 22.1 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
declare(strict_types=1);
/**
* Edit Post
*/
// Check if blog is installed
$config_path = file_exists(__DIR__ . '/../config.php') ? __DIR__ . '/../config.php' : '../config.php';
if (!file_exists($config_path)) {
header('Location: ../install.php');
exit;
}
// Define constant to allow access
define('ALLOW_DIRECT_ACCESS', true);
try {
$functions_path = file_exists(__DIR__ . '/../functions.php') ? __DIR__ . '/../functions.php' : '../functions.php';
require_once $functions_path;
} catch (Exception $e) {
error_log('Error loading functions.php: ' . $e->getMessage());
die('System error. Please try again later.');
}
// Load SecurityHardener if available
$security_hardener_path = __DIR__ . '/../libs/SecurityHardener.php';
if (file_exists($security_hardener_path)) {
require_once $security_hardener_path;
// Initialize security system
if (class_exists('SecurityHardener')) {
SecurityHardener::init();
}
}
// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// Set cache control headers to prevent caching
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
// Check if user is logged in
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
header('Location: login');
exit;
}
$success_message = '';
$error_message = '';
// Get post slug and sanitize
$slug = trim($_GET['slug'] ?? '');
if (empty($slug)) {
header('Location: index');
exit;
}
$slug = htmlspecialchars($slug, ENT_QUOTES, 'UTF-8');
// Load post - clear cache first to ensure fresh data
clearstatcache(true);
$post = get_post($slug);
if (!$post) {
header('Location: index?error=Post+not+found');
exit;
}
// Determine effective content type for editing
$effective_content_type = $post['content_type'] ?? (isset($post['content_markdown']) ? 'markdown' : 'html');
// Ensure CSRF token exists
if (!isset($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$all_posts = all_posts();
$all_categories = collect_all_categories();
// Handle success/error messages from URL parameters
if (isset($_GET['success'])) {
$success_message = htmlspecialchars(urldecode($_GET['success']), ENT_QUOTES, 'UTF-8');
}
if (isset($_GET['error'])) {
$error_message = htmlspecialchars(urldecode($_GET['error']), ENT_QUOTES, 'UTF-8');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>Edit Post - <?php echo SITE_TITLE; ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
<link href="<?php echo BASE_URL; ?>assets/css/main.css" rel="stylesheet">
<link href="<?php echo BASE_URL; ?>admin/assets/css/admin.css" rel="stylesheet">
<style>
#serp-preview-title { color: #1a0dab; }
#serp-preview-url { color: #006621; }
</style>
</head>
<body class="bg-light" data-base-url="<?php echo htmlspecialchars(BASE_URL, ENT_QUOTES, 'UTF-8'); ?>">
<div class="container-fluid">
<div class="row">
<!-- Sidebar -->
<div class="col-md-3 col-lg-2 bg-dark min-vh-100 p-0">
<div class="p-3">
<center>
<h5 class="text-light mb-4">
<a href="." class="text-light text-decoration-none">
Admin Panel
</a>
</h5>
</center>
<nav class="nav flex-column">
<a class="nav-link text-light" href="<?php echo BASE_URL; ?>admin/">
<i class="bi bi-house"></i> Dashboard
</a>
<a class="nav-link text-light" href="<?php echo BASE_URL; ?>admin/posts">
<i class="bi bi-journal-text"></i> All Posts
</a>
<a class="nav-link text-light" href="<?php echo BASE_URL; ?>admin/new-post">
<i class="bi bi-plus-circle"></i> New Post
</a>
<a class="nav-link text-light" href="<?php echo BASE_URL; ?>admin/settings">
<i class="bi bi-gear"></i> Settings
</a>
<a class="nav-link text-light" href="<?php echo BASE_URL; ?>admin/admin-tools">
<i class="bi bi-tools"></i> Tools
</a>
<hr class="text-light">
<a class="nav-link text-light" href="<?php echo BASE_URL; ?>" target="_blank">
<i class="bi bi-eye"></i> View Site
</a>
<a class="nav-link text-light" href="<?php echo BASE_URL; ?>admin/logout">
<i class="bi bi-box-arrow-right"></i> Logout
</a>
</nav>
</div>
</div>
<!-- Main Content -->
<div class="col-md-9 col-lg-10">
<div class="container-fluid py-4">
<!-- Header -->
<div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
<h1 class="h3 mb-0">Edit Post</h1>
<div>
<button type="button" class="btn btn-outline-dark me-2" data-bs-toggle="modal" data-bs-target="#aiModal">
<i class="bi bi-stars"></i> Regenerate with AI
</button>
<a href="<?php echo BASE_URL; ?><?php echo $post['slug']; ?>" target="_blank"
class="btn btn-outline-primary">
<i class="bi bi-eye"></i> View Post
</a>
</div>
</div>
<!-- Messages -->
<?php if ($success_message): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<i class="bi bi-check-circle"></i> <?php echo htmlspecialchars($success_message); ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-exclamation-triangle"></i> <?php echo htmlspecialchars($error_message); ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<!-- Post Form -->
<form method="POST" action="<?php echo BASE_URL; ?>admin_action" enctype="multipart/form-data">
<input type="hidden" name="action" value="update">
<input type="hidden" name="original_slug"
value="<?php echo htmlspecialchars($post['slug']); ?>">
<input type="hidden" name="csrf_token"
value="<?php echo htmlspecialchars($_SESSION['csrf_token']); ?>">
<div class="row">
<div class="col-lg-8">
<!-- Main Content -->
<div class="card mb-4">
<div class="card-header">
<h5 class="mb-0">Post Content</h5>
</div>
<div class="card-body">
<div class="mb-3">
<label for="title" class="form-label">H1 / Blog Title *</label>
<input type="text" class="form-control" id="title" name="title"
value="<?php echo htmlspecialchars($post['title']); ?>" required>
<div class="form-text">Displayed as the main heading; can differ from meta title.</div>
</div>
<div class="mb-3">
<label for="slug" class="form-label">Slug</label>
<input type="text" class="form-control" id="slug" name="slug"
value="<?php echo htmlspecialchars($post['slug']); ?>">
</div>
<div class="mb-3">
<label for="excerpt" class="form-label">Excerpt / Short Description</label>
<textarea class="form-control" id="excerpt" name="excerpt" rows="3"
placeholder="Brief description of the post"><?php echo htmlspecialchars($post['excerpt'] ?? ''); ?></textarea>
</div>
<div class="mb-3">
<label for="content_type" class="form-label">Content Type</label>
<select class="form-select" id="content_type" name="content_type">
<option value="markdown" <?php echo ($effective_content_type === 'markdown') ? 'selected' : ''; ?>>
Markdown</option>
<option value="html" <?php echo ($effective_content_type === 'html') ? 'selected' : ''; ?>>HTML</option>
</select>
</div>
<div class="mb-3">
<label for="content" class="form-label">Content *</label>
<textarea class="form-control" id="content" name="content" rows="15"
required><?php echo htmlspecialchars(($effective_content_type === 'markdown') ? ($post['content_markdown'] ?? $post['content'] ?? '') : ($post['content_html'] ?? $post['content'] ?? '')); ?></textarea>
<div class="form-text" id="content-help">
<strong>Markdown supported:</strong> Use **bold**, *italic*, `code`,
[links](url), # headers, etc.
</div>
</div>
</div>
</div>
<?php $current_slug = $post['slug']; include __DIR__ . '/includes/post-cms-seo.php'; ?>
</div>
<div class="col-lg-4">
<!-- Publish Settings -->
<div class="card mb-4">
<div class="card-header">
<h5 class="mb-0">Publish Settings</h5>
</div>
<div class="card-body">
<div class="mb-3">
<label for="status" class="form-label">Status</label>
<select class="form-select" id="status" name="status">
<option value="draft" <?php echo $post['status'] === 'draft' ? 'selected' : ''; ?>>Draft</option>
<option value="published" <?php echo $post['status'] === 'published' ? 'selected' : ''; ?>>Published</option>
</select>
</div>
<div class="mb-3">
<label for="author" class="form-label">Author</label>
<input type="text" class="form-control" id="author" name="author"
value="<?php echo htmlspecialchars($post['author'] ?? 'Admin'); ?>">
</div>
<div class="mb-3">
<label for="featured_image" class="form-label">Featured Image</label>
<input type="file" class="form-control" id="featured_image"
name="featured_image" accept="image/*">
<div class="form-text">Upload a new featured image (JPG, PNG, GIF, WebP)
</div>
<?php if (!empty($post['meta']['image'])): ?>
<div class="mt-2">
<small class="text-muted">Current image: <a
href="<?php echo htmlspecialchars($post['meta']['image']); ?>"
target="_blank" rel="noopener">View</a></small>
</div>
<div class="form-check mt-2">
<input class="form-check-input" type="checkbox" name="remove_featured_image"
value="1" id="remove_featured_image">
<label class="form-check-label" for="remove_featured_image">
Remove featured image from this post
</label>
</div>
<?php endif; ?>
</div>
<div class="mb-3">
<label for="featured_image_alt" class="form-label">Featured Image Alt Text</label>
<input type="text" class="form-control" id="featured_image_alt" name="featured_image_alt"
value="<?php echo htmlspecialchars($post['meta']['image_alt'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
</div>
<div class="mb-3">
<label for="date" class="form-label">Publish Date/Time</label>
<input type="datetime-local" class="form-control" id="date" name="date"
value="<?php echo htmlspecialchars(date('Y-m-d\\TH:i', strtotime($post['date']))); ?>">
<div class="form-text">Set a future date to schedule the post. Posts with future dates won't appear until that date.</div>
</div>
<div class="mb-3">
<label for="updated" class="form-label">Last Edited Time</label>
<input type="datetime-local" class="form-control" id="updated"
name="updated"
value="<?php echo htmlspecialchars(date('Y-m-d\\TH:i', strtotime($post['updated'] ?? $post['date']))); ?>">
</div>
</div>
</div>
<!-- Tags & Categories -->
<div class="card mb-4">
<div class="card-header">
<h5 class="mb-0">Tags & Categories</h5>
</div>
<div class="card-body">
<div class="mb-3">
<label for="tags" class="form-label">Tags</label>
<input type="text" class="form-control" id="tags" name="tags"
value="<?php echo htmlspecialchars(implode(', ', $post['tags'] ?? [])); ?>"
placeholder="tag1, tag2, tag3">
</div>
<div class="mb-3">
<label class="form-label">Categories</label>
<?php foreach ($all_categories as $cat): ?>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="categories[]"
value="<?php echo htmlspecialchars($cat, ENT_QUOTES, 'UTF-8'); ?>"
id="cat_<?php echo htmlspecialchars(md5($cat), ENT_QUOTES, 'UTF-8'); ?>"
<?php echo in_array($cat, $post['categories'] ?? [], true) ? 'checked' : ''; ?>>
<label class="form-check-label" for="cat_<?php echo htmlspecialchars(md5($cat), ENT_QUOTES, 'UTF-8'); ?>">
<?php echo htmlspecialchars($cat, ENT_QUOTES, 'UTF-8'); ?>
</label>
</div>
<?php endforeach; ?>
<input type="text" class="form-control form-control-sm mt-2" name="new_category"
placeholder="Add new category">
</div>
</div>
</div>
<?php include __DIR__ . '/includes/post-cms-advanced.php'; ?>
<!-- Actions -->
<div class="card">
<div class="card-body">
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">
<i class="bi bi-check-circle"></i> Update Post
</button>
<a href="<?php echo BASE_URL; ?>admin" class="btn btn-outline-secondary">
<i class="bi bi-x-circle"></i> Cancel
</a>
</div>
</div>
</div>
</div>
</div>
</form>
<div class="modal fade" id="aiModal" tabindex="-1" aria-labelledby="aiModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="aiModalLabel"><i class="bi bi-stars"></i> Generate with AI</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="ai_topic" class="form-label">Topic</label>
<input type="text" class="form-control" id="ai_topic"
placeholder="e.g. Benefits of local SEO for small businesses">
<div class="form-text">Replaces post body and SEO fields. Review before saving.</div>
</div>
<div id="ai_error" class="alert alert-danger d-none" role="alert"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" id="ai_generate_btn" class="btn btn-primary">
<span class="spinner-border spinner-border-sm d-none" id="ai_spinner" role="status" aria-hidden="true"></span>
<span id="ai_generate_text">Generate</span>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="<?php echo BASE_URL; ?>assets/js/admin-post-cms.js"></script>
<script src="<?php echo BASE_URL; ?>assets/js/admin-edit-post.js"></script>
</body>
</html>