/
viewcounts.php
157 lines (139 loc) · 4.88 KB
/
viewcounts.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
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
<?php
// Define the path to the SQLite database
define('DATABASE_PATH', 'viewcounts.sqlite');
// Singleton pattern for database connection
class Database {
private static $instance = null;
private function __construct() {}
private function __clone() {}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new PDO('sqlite:' . DATABASE_PATH);
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
}
// Function to read and parse view counts from the SQLite database
function getViewCounts($limit = 50) {
$counts = [];
try {
$pdo = Database::getInstance();
$query = 'SELECT image_url, count FROM view_counts ORDER BY count DESC LIMIT :limit';
$statement = $pdo->prepare($query);
$statement->bindValue(':limit', $limit, PDO::PARAM_INT);
$statement->execute();
$counts = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// Log error instead of displaying it
error_log('Database Error: ' . $e->getMessage());
}
return $counts;
}
// Function to calculate the total counts from all entries in the database
function getTotalCounts() {
$pdo = Database::getInstance();
$query = 'SELECT SUM(count) AS total FROM view_counts';
$statement = $pdo->query($query);
$result = $statement->fetch(PDO::FETCH_ASSOC);
$countsTotal = $result['total'];
$formattedCountsTotal = number_format($countsTotal, 0, '', ' ');
return $formattedCountsTotal;
}
// Get initial view counts
$initialCounts = getViewCounts();
$countsTotal = getTotalCounts();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Viewcounter!</title>
<style>
body {
font-family: monospace;
background-color: hsl(0, 0%, 11%);
min-width: 1000px;
}
#viewCountsContainer {
display: flex;
flex-wrap: wrap;
margin-top: 25px;
}
.thumbnail-container {
margin: 2px 4px;
text-align: center;
position: relative;
}
.count {
position: absolute;
top: 170px;
left: 20px;
color: #fff;
font-size: 15px;
background-color: hsla(0, 0%, 0%, 0.70);
padding: 1px 20px;
}
.folder {
margin: 4px;
background-color: #444444;
float: left;
padding: 0 4px;
}
#folder-wrapper {
min-width: 1000px;
margin-top: 30px;
height: 63px;
}
#folder-wrapper a, p {
color: #f2f2f2;
font-size: 30pt;
text-align: center;
text-decoration: none;
letter-spacing: 0px;
}
p {
color: #dcdcdc;
text-align: justify;
}
#wrapper {
margin: auto;
width: 700px;
}
#trackingasof {
color:hsla(0, 0%, 100%, 0.4);
font-size:13pt;
margin: 6px 4px 4px 30px;
float: left;
padding: 4px 4px 4px 0px;
}
</style>
</head>
<body>
<div id="folder-wrapper">
<div class="folder"><a href=".."><p style="margin-left:5px;" class="folder">< Return</p></a></div>
<div class="folder"><a href="./viewcounts.php"><p style="margin-left:5px;" class="folder">Refresh</p></a></div>
<?php echo '<p id="trackingasof">Tracking ' . $countsTotal . ' requests as of:<br> December 6th, 2023</p>'; ?>
</div>
<div id="viewCountsContainer">
<?php foreach ($initialCounts as $count): ?>
<div class="thumbnail-container">
<?php
// Create the modified URL with '/.thumb/' correctly appended
$modifiedUrl = dirname($count['image_url']) . '/.thumb/' . basename($count['image_url']);
?>
<a href="<?= htmlspecialchars($count['image_url']) ?>">
<?php if (pathinfo($count['image_url'], PATHINFO_EXTENSION) === 'mp4'): ?>
<video autoplay loop muted playsinline>
<source src="<?= htmlspecialchars($modifiedUrl) ?>" type="video/mp4"></video>
<?php else: ?>
<img src="<?= htmlspecialchars($modifiedUrl) ?>" alt="Thumbnail">
<?php endif; ?>
</a>
<div class="count"><?= $count['count'] ?></div>
</div>
<?php endforeach; ?>
</div>
</body>
</html>