-
Notifications
You must be signed in to change notification settings - Fork 0
/
guestbook.inc.php
97 lines (87 loc) · 2.47 KB
/
guestbook.inc.php
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
<?php
/**
* PHP/SQLite Guestbook script
*
* Copyright (c) 2015-21, Irwin Associates and Graham R Irwin
*
* See license.txt for details
*/
// script to display guestbook entries (paginated version)
require 'constants.inc.php';
try {
// open db connection and create db and tables if first time here
$db = new PDO('sqlite:'.ADMIN_DIR.'/guestbook.sqlite');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('CREATE TABLE IF NOT EXISTS guestbook (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT,
comment TEXT,
timedate TEXT,
ipaddress TEXT,
approved INTEGER
);');
$db->exec('CREATE TABLE IF NOT EXISTS whitelist (
email TEXT PRIMARY KEY
);');
$perpage = PERPAGE; // number of comments per page
$p = isset($_GET['p']) ? (int)$_GET['p'] : 1;
if ($p < 1) $p = 1;
$start = ($p-1) * $perpage;
$query = 'SELECT count(id) FROM guestbook WHERE approved = 1;';
$result = $db->query($query);
$rows = $result->fetch();
$totPages = ceil((int)$rows['count(id)'] / $perpage);
$query = 'SELECT name, comment, timedate
FROM guestbook
WHERE approved=1
ORDER BY id DESC
LIMIT ?, ?;';
$stmt = $db->prepare($query);
$stmt->execute([$start, $perpage]);
$rows = $stmt->fetchAll();
foreach ($rows as $row) {
$date = date("j M Y", strtotime($row['timedate']));
$comment = trim($row['comment']);
$comment = str_replace("\n", '<br>', $comment);
echo <<<EOT
<div class="gbentry">
<div class="gbcomment">{$comment}</div>
<div class="gbauthor">{$row['name']} <span>{$date}</span></div>
</div>
EOT;
}
// pagination links ********
if ($totPages > 1) {
echo '<p class="more">';
// previous page
if ($p > 1) {
echo ' <a href="', htmlspecialchars($_SERVER['PHP_SELF']);
if ($p > 2)
echo '?p=', $p-1;
echo '" title="Previous page">‹</a> ';
}
// all pages by number
for ($pp = 1; $pp <= $totPages; $pp++) {
if ($pp === $p) {
// current page
echo ' <span>', $pp, '</span> ';
} else {
echo ' <a href="', htmlspecialchars($_SERVER['PHP_SELF']);
if ($pp !== 1)
echo '?p=', $pp;
echo '">', $pp, '</a> ';
}
}
// next page
if ($p < $totPages)
echo ' <a href="', htmlspecialchars($_SERVER['PHP_SELF']), '?p=', $p+1, '" title="Next page">›</a> ';
//
echo "</p>\n";
}
}
// error handling
catch (PDOException $e) {
echo $e->getMessage();
}
$db = null;