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
|
<?php
$config = array();
define('VERSION', '0.0.1');
define('GIT_REPO', 'https://git.aleteoryx.me/cgit/visitors_dot_php/');
/* CONFIG OPTIONS - COSMETIC */
// Title: string
//
// The title for the page and <title> element.
$config['title'] = 'Guestbook';
// Custom CSS: ?string
//
// Will be injected after the builtin CSS. Respects `use_path_info` when being served.
//$config['custom_css'] = 'body { background: red; }';
// Use builtin CSS: bool
//
// Toggles the builtin CSS
$config['builtin_css'] = true;
// Form mode: choice
//
// 0 - Only ask for name
// 1 - Ask for name and website
// 2 - Ask for name, website, and email
// 3 - Ask for name and (website or email)
//
// Any entries in one mode will display fine in any other.
$config['form_mode'] = 0;
// E-Mail display mode: choice
//
// 0 - Show an icon with a `mailto:` link
// 1 - Show an icon with a `mailto:` link, or use the username text as the link if no website is present
// 2 - Show the text `[e-mail]` with a `mailto:` link
// 3 - Show the text `[e-mail]` with a `mailto:` link, or use the username text for the link if no website is present
// 4 - Show the e-mail as escaped text, e.g. 'alyx at aleteoryx dot me'
$config['email_display'] = 1;
// E-Mail icon/link: ?string
//
// Link should be a link to an image for the email icon. Icon should be the icon's data, base64 encoded, prefixed with `<mimetype>:`.
// Only set one, or neither. This option only applies for email_display modes 0 and 1.
//$config['email_link'] = '/static/my_cool_email_icon.gif';
//$config['email_icon'] = 'image/png:...';
/* CONFIG OPTIONS - BACKEND */
// Send assets via PATH_INFO: bool
//
// If true, assets like CSS, GIFs, etc, will be served from URLs like `/visitors.php/foo.css`.
// Otherwise, inline them into a single HTML document.
// Only enable this if your webserver supports PATH_INFO.
$config['use_path_info'] = true;
// Database path: string
//
// The path to the database file, in one of 3 formats.
//
// *.json - use a JSON array
// *.jsonl - use newline-delimited JSON objects (jsonlines)
// *.csv - use CSV
// *.db, *.sqlite, *.sqlite3 - use Sqlite3
//
// If the file extension is unknown, CSV is picked as default.
$config['db'] = 'visitors.csv';
/* --- END OF CONFIG, CODE BELOW, PROBABLY DON'T EDIT PAST THIS POINT --- */
/* CONFIG FIXING */
if (!isset($config['email_link'])) { // stolen from <https://forum.melonland.net/Themes/pimp-my-classic/images/email_sm.gif>
$config['email_icon'] ??= 'image/gif:R0lGODlhEAAQALMEAAAAADExY2NjMWNjzv///5ycY5yc/86cY87O///OnP//zv///wAAAAAAAAAAAAAAACH5BAEAAAQALAAAAAAQABAAAARfkJBCqy1CyrK690qmcUd5KKiSiNuSvio7LWVinqs2w2kuDaSbLTYgDAwcHmplCAwWBprplTA0nwiDspp1LhBZq9gKvn6z4DS6izWo1W6z+w2/Hsd46yBACAD+gIGABBEAOw==';
if (!$config['use_path_info']) {
[$mime, $base64] = explode(':', $config['email_icon'], 2);
$config['email_link'] = 'data:'.$mime.';base64,'.$base64;
unset($config['email_icon']);
}
}
/* BUILTIN_CSS */
define('BUILTIN_CSS', <<<EOT
EOT);
/* PATH_INFO HANDLING */
if (@$_SERVER['PATH_INFO']) {
switch ($_SERVER['PATH_INFO']) {
case '/email_icon':
[$mime, $base64] = explode(':', $config['email_icon'], 2);
header('Content-Type: '.$mime);
echo base64_decode($base64);
break;
case '/builtin_css':
header('Content-Type: text/css');
echo BUILTIN_CSS;
break;
case '/custom_css':
header('Content-Type: text/css');
echo $config['custom_css']??'';
break;
default:
http_response_code('404');
header('Content-Type: text/html');
echo "<h1>404: Asset Not Found.</h1>";
break;
}
exit();
}
/* DATABASES */
// db_row: ['id' => int (only for list_rows), 'name' => string, 'message' => string, 'email' => ?string, 'website' => ?string, 'timestamp' => string (or DateTimeInterface for list_rows)]
// message is pre-escaped at insert time.
// timestamp is DateTimeInterface::ISO8601_EXPANDED format.
abstract class Database {
public function append_row(string $name, string $message, ?string $email = NULL, ?string $website = NULL) {
$timestamp = date(DATE_ISO8601_EXPANDED);
$this->_append_row(compact('name', 'message', 'email', 'website', 'timestamp'));
}
public function list_rows(): array {
$data = $this->_list_rows();
foreach($data as &$row) {
$row['timestamp'] = DateTime::createFromFormat(DATE_ISO8601_EXPANDED, $row['timestamp']);
}
return $data;
}
public function delete_row(int $id): bool {
return $this->_delete_row($id);
}
abstract public function __construct(string $file);
abstract protected function _append_row(array $db_row);
abstract protected function _list_rows(): array;
abstract protected function _delete_row(int $id): bool;
}
// sigh, refactor later, it wont be a perf issue for any realistic number of entries.
abstract class FileDatabase extends Database {
protected string $file;
protected array $data;
public function __construct(string $file) {
if (file_exists($file))
$this->data = $this->load($file);
else
$this->data = array();
$this->file = $file;
}
protected function _append_row(array $db_row) {
array_unshift($this->data, array(...$db_row, 'id' => count($this->data) ? max(array_map(fn($a) => $a['id'], $this->data)) : 0));
$this->save();
}
protected function _list_rows(): array {
$data = $this->data;
return $data;
}
protected function _delete_row(int $id): bool {
foreach(array_keys($this->data) as $key)
if ($this->data[$key]['id'] === $id) {
unset($this->data[$key]);
$this->save();
return true;
}
return false;
}
abstract protected function save();
abstract protected function load(string $file): array;
}
// notes: `id` is not stable
final class JsonDatabase extends FileDatabase {
protected function save() {
file_put_contents($this->file, json_encode($this->data));
}
protected function load(string $file): array {
return json_decode(file_get_contents($file), associative: true);
}
}
// notes: `id` is not stable
final class JsonlDatabase extends FileDatabase {
protected function save() {
$fp = fopen($this->file, 'w');
foreach($data as $row) {
fwrite($fp, json_encode($row)."\n");
}
fclose($fp);
}
protected function load(string $file): array {
return array_map(fn($s) => json_decode($s, associative: true), file($file));
}
}
// notes: `id` is not stable
final class CsvDatabase extends FileDatabase {
private const array KEY_ORDER = ['id', 'name', 'message', 'email', 'website', 'timestamp'];
protected function save() {
$fp = fopen($this->file, 'w');
foreach($this->data as $row) {
fputcsv($fp, array_map(fn($k) => $row[$k]??'', self::KEY_ORDER));
}
fclose($fp);
}
protected function load(string $file): array {
return array_map(fn($s) => array_combine(self::KEY_ORDER, str_getcsv($s)), file($file));
}
}
final class SqliteDatabase extends Database {
private Sqlite3 $handle;
public function __construct(string $file) {
$this->handle = new Sqlite3($file);
if (!$this->handle->querySingle('SELECT count(*) FROM sqlite_master'))
$this->handle->exec('CREATE TABLE visitors (id INTEGER PRIMARY KEY, name TEXT NOT NULL, message TEXT NOT NULL, email TEXT, website TEXT, timestamp TEXT NOT NULL)');
}
protected function _append_row(array $db_row) {
$stmt = $this->handle->prepare('INSERT INTO visitors (name, message, email, website, timestamp) VALUES (:name, :message, :email, :website, :timestamp)');
foreach($db_row as $key => $value)
$stmt->bindValue($key, $value);
$stmt->execute();
}
protected function _list_rows(): array {
$rows = array();
$res = $this->handle->query('SELECT * FROM visitors ORDER BY id DESC');
while ($row = $res->fetchArray(SQLITE3_ASSOC))
$rows[] = $row;
return $rows;
}
protected function _delete_row(int $id): bool {
$stmt = $this->handle->prepare('SELECT count(*) FROM visitors WHERE id=:id');
$stmt->bindValue('id', $id);
if (!$stmt->execute()->fetchArray(SQLITE3_NUM)[0])
return false;;
$stmt = $this->handle->prepare('DELETE FROM visitors WHERE id=:id');
$stmt->bindValue('id', $id);
$stmt->execute();
return true;
}
}
function get_database_for_file(string $file): Database {
$ext = preg_replace('/.+\.([^.]+)$/', '${1}', $file);
return match ($ext) {
'db', 'sqlite', 'sqlite3' => new SqliteDatabase($file),
'csv' => new CsvDatabase($file),
'json' => new JsonDatabase($file),
'jsonl' => new JsonlDatabase($file),
default => new CsvDatabase($file)
};
}
$db = get_database_for_file($config['db']);
//$db->append_row('foo', 'bar');
/* ACTUAL UI */
$ed_1_or_3 = $config['email_display'] == 1 || $config['email_display'] == 3;
?>
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
<title><?= $config['title'] ?></title>
<?= $config['use_path_info'] ?
'<link rel=stylesheet href="'.$_SERVER['REQUEST_URI'].'/builtin_css"/><link rel=stylesheet href="'.$_SERVER['REQUEST_URI'].'/custom_css"/>' :
'<style>'.BUILTIN_CSS.'</style>'.(@$config['custom_css'] ? '<style>'.$config['custom_css'].'</style>' : '') ?>
</head>
<body>
<h1><?= $config['title'] ?></h1>
<main>
<?php foreach($db->list_rows() as $row):
$author_link = $row['website'] ?
'<a href="'.$row['website'].'">'.$row['name'].'</a>' :
(($row['email'] && $ed_1_or_3) ?
'<a href="mailto:'.$row['email'].'">'.$row['name'].'</a>' :
$row['name']);
$email_element = $row['email'] && (($row['website'] && $ed_1_or_3) || !$ed_1_or_3) ?
match ($config['email_display']) {
0, 1 => '<a href="mailto:'.$row['email'].'"><img alt="e-mail icon" src="'.($config['email_link']??$_SERVER['REQUEST_URI'].'/email_icon').'" /></a>',
2, 3 => '<a href="mailto:'.$row['email'].'">[e-mail]</a>',
4 => 'mail: <u>'.str_replace(['@', '.'], [' at ', ' dot '], $row['email']).'</u>'
} : ''; ?>
<article id=visit-<?= $row['id'] ?>
data-visit-id=<?= $row['id'] ?>
data-visit-name="<?= $row['name'] ?>"
data-visit-message="<?= $row['message'] ?>"
data-visit-email="<?= $row['email'] ?>"
data-visit-website="<?= $row['website'] ?>">
<div class='visit-info'>
<span class='author-link'><?= $author_link ?></span> <span class='email'><?= $email_element ?></span>
<span class='wide'>
<time datetime="<?= $row['timestamp']->format(DATE_ISO8601) ?>"><?= $row['timestamp']->format("Y/m/d H:i") ?></time>
</div>
<q><?= $row['message'] ?></q>
</article>
<?php endforeach; ?>
</main>
</body>
</html>
|