-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleadmin.php
More file actions
309 lines (276 loc) · 10.4 KB
/
simpleadmin.php
File metadata and controls
309 lines (276 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
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
<?php
require_once 'auth.php';
check_login(); // require login
session_start();
// ===== Configuration =====
$upload_dir = 'uploads/';
$allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
// ========================
// Initialize database connection
include 'config.php';
// Limit how many posts show in dropdown
$limit = 20;
try {
$stmt = $pdo->query("
SELECT id, title, visible, post_date
FROM logs
ORDER BY post_date DESC
LIMIT $limit
");
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Count hidden posts for info
$totalPosts = $pdo->query("SELECT COUNT(*) FROM logs")->fetchColumn();
$hiddenCount = max(0, $totalPosts - $limit);
} catch (PDOException $e) {
$posts = [];
$hiddenCount = 0;
}
// ===== Handle post submission =====
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['title'])) {
try {
$content = $_POST['content'];
$post_date = !empty($_POST['post_date']) ? $_POST['post_date'] : date('Y-m-d H:i:s');
// Use new hidden field to ensure correct update
$edit_id = $_POST['edit_id_hidden'] ?? '';
if (!empty($edit_id)) {
// --- Update existing post ---
$stmt = $pdo->prepare("
UPDATE logs
SET title = ?, content = ?, post_date = ?, category = ?, tags = ?, anchor_name = ?, visible = ?
WHERE id = ?
");
$stmt->execute([
$_POST['title'],
$content,
$post_date,
$_POST['category'] ?? 'Uncategorized',
$_POST['tags'] ?? '',
$_POST['anchor_name'] ?? '',
$_POST['visible'] ?? 'n',
$edit_id
]);
header("Location: admin.php?success=updated"); // for updates
exit;
} else {
// --- Create new post ---
$stmt = $pdo->prepare("
INSERT INTO logs (title, content, post_date, category, tags, anchor_name, visible)
VALUES (?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$_POST['title'],
$content,
$post_date,
$_POST['category'] ?? 'Uncategorized',
$_POST['tags'] ?? '',
$_POST['anchor_name'] ?? '',
$_POST['visible'] ?? 'n'
]);
header("Location: admin.php?success=1");
exit;
}
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Admin Panel</title>
<link rel="stylesheet" href="style.css">
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
}
textarea {
width: 96%;
height: 300px;
}
.container {
background-color: white;
padding: 8px;
min-height: 100vh;
}
.mini-toolbar button {
padding: 4px 6px;
}
</style>
</head>
<body>
<div class="container">
<?php if (!empty($_GET['success'])): ?>
<div class="success">
<?php
if ($_GET['success'] === '1') echo '✅ Post published!';
elseif ($_GET['success'] === 'updated') echo '✅ Post updated!';
?>
<a href="index.php">View Blog</a>
</div>
<?php endif; ?>
<form method="POST" action="" enctype="multipart/form-data">
<p>
<label>Edit existing post:<br>
<select id="post_select" name="edit_id" onchange="loadPost(this.value)">
<option value="">— New Post —</option>
<optgroup label="Drafts">
<?php foreach ($posts as $p): ?>
<?php if ($p['visible'] == 'n'): ?>
<option value="<?= htmlspecialchars($p['id']) ?>">
<?= htmlspecialchars($p['title']) ?> (<?= htmlspecialchars($p['post_date']) ?>)
</option>
<?php endif; ?>
<?php endforeach; ?>
</optgroup>
</optgroup>
<optgroup label="Published">
<?php foreach ($posts as $p): ?>
<?php if ($p['visible'] == 'y'): ?>
<option value="<?= htmlspecialchars($p['id']) ?>">
<?= htmlspecialchars($p['title']) ?> (<?= htmlspecialchars($p['post_date']) ?>)
</option>
<?php endif; ?>
<?php endforeach; ?>
<?php if ($hiddenCount > 0): ?>
<option disabled>+<?= $hiddenCount ?> more posts...</option>
<?php endif; ?>
</optgroup>
</select>
</label>
</p>
<!-- ✅ Hidden field to track the selected post -->
<input type="hidden" name="edit_id_hidden" id="edit_id_hidden" value="">
<p>
<label>Title:<br>
<input type="text" name="title" required style="width:96%">
</label>
</p>
<p>
<label>Content (HTML):<br>
<textarea id="editor" name="content"></textarea>
</label>
</p>
<p>
<label>Date:<br>
<input type="datetime-local" name="post_date">
</label>
</p>
<p>
<label>Tags (comma-separated):<br>
<input type="text" name="tags" placeholder="personal, diary, memories">
</label>
</p>
<p>
<label>Status:<br>
<select name="visible">
<option value="n">Draft (Not Visible)</option>
<option value="y">Published (Visible)</option>
</select>
</label>
</p>
<button type="submit">Publish</button>
</form>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const textarea = document.getElementById('editor');
textarea.style.display = 'none';
// Create editor
const editorDiv = document.createElement('div');
editorDiv.contentEditable = true;
editorDiv.id = 'mini-editor';
editorDiv.style.minHeight = '300px';
editorDiv.style.border = '1px solid #ccc';
editorDiv.style.padding = '8px';
editorDiv.style.marginBottom = '10px';
editorDiv.innerHTML = textarea.value;
textarea.parentNode.insertBefore(editorDiv, textarea.nextSibling);
// Toolbar
const toolbar = document.createElement('div');
toolbar.className = 'mini-toolbar';
toolbar.style.marginBottom = '5px';
const buttons = [
{ cmd: 'bold', label: 'B' },
{ cmd: 'italic', label: 'I' },
{ cmd: 'underline', label: 'U' },
{ cmd: 'link', label: 'Link' },
{ cmd: 'image', label: 'Image' }
];
buttons.forEach(b => {
const btn = document.createElement('button');
btn.type = 'button';
btn.innerHTML = b.label;
btn.style.marginRight = '4px';
btn.addEventListener('click', () => {
if (b.cmd === 'link') {
const url = prompt('Enter URL:');
if (url) document.execCommand('createLink', false, url);
} else if (b.cmd === 'image') {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = 'image/*';
fileInput.onchange = async () => {
const file = fileInput.files[0];
if (!file) return;
const formData = new FormData();
formData.append('image', file);
try {
const res = await fetch('simpleupload.php', { method: 'POST', body: formData });
const data = await res.json();
if (data.location) {
document.execCommand('insertHTML', false, `<img src="${data.location}" />`);
} else {
alert('Upload failed');
}
} catch (err) {
console.error(err);
alert('Upload error');
}
};
fileInput.click();
} else {
document.execCommand(b.cmd, false, null);
}
});
toolbar.appendChild(btn);
});
editorDiv.parentNode.insertBefore(toolbar, editorDiv);
// Sync editor content to textarea
textarea.form.addEventListener('submit', () => {
textarea.value = editorDiv.innerHTML;
});
// === Load post function ===
window.loadPost = function(id) {
const title = document.querySelector('[name="title"]');
const date = document.querySelector('[name="post_date"]');
const tags = document.querySelector('[name="tags"]');
const visible = document.querySelector('[name="visible"]');
const hiddenEditId = document.getElementById('edit_id_hidden');
if (!id) {
title.value = '';
editorDiv.innerHTML = '';
date.value = '';
tags.value = '';
visible.value = 'n';
hiddenEditId.value = ''; // new post
return;
}
fetch('load_post.php?id=' + id)
.then(r => r.json())
.then(post => {
title.value = post.title;
editorDiv.innerHTML = post.content;
date.value = post.post_date.replace(' ', 'T');
tags.value = post.tags;
visible.value = post.visible;
hiddenEditId.value = id; // ✅ keep ID for updates
editorDiv.focus();
document.getSelection().collapse(editorDiv, 0);
});
};
});
</script>
</body>
</html>