-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartworkid.php
282 lines (240 loc) · 10.6 KB
/
artworkid.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
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
<?php
require_once('auth.php');
// Connect to the database using PDO
$db = new PDO('sqlite:database.sqlite');
// Get the filename from the query string
$artworkId = $_GET['artworkid'];
// Get the current image information from the database
$stmt = $db->prepare("SELECT * FROM images WHERE id = :artworkid ");
$stmt->bindParam(':artworkid', $artworkId);
$stmt->execute();
$image = $stmt->fetch();
// Check if the image exists in the database
if (!$image) {
header("Location: error.php");
exit; // Stop further execution
}
// Get the ID of the current image and the email of the owner
$image_id = $image['id'];
$email = $image['email'];
// Get the previous image information from the database
$stmt = $db->prepare("SELECT * FROM images WHERE id < :id AND email = :email ORDER BY id DESC LIMIT 1");
$stmt->bindParam(':id', $image_id);
$stmt->bindParam(':email', $email);
$stmt->execute();
$prev_image = $stmt->fetch();
// Get the next image information from the database
$stmt = $db->prepare("SELECT * FROM images WHERE id > :id AND email = :email ORDER BY id ASC LIMIT 1");
$stmt->bindParam(':id', $image_id);
$stmt->bindParam(':email', $email);
$stmt->execute();
$next_image = $stmt->fetch();
// Get the image information from the database
$stmt = $db->prepare("SELECT * FROM images WHERE id = :artworkid");
$stmt->bindParam(':artworkid', $artworkId);
$stmt->execute();
$image = $stmt->fetch();
$image_id = $image['id'];
// Check if the user is logged in and get their email
$email = '';
if (isset($_SESSION['email'])) {
$email = $_SESSION['email'];
}
// Get the email of the selected user
$user_email = $image['email'];
// Get the selected user's information from the database
$query = $db->prepare('SELECT * FROM users WHERE email = :email');
$query->bindParam(':email', $user_email);
$query->execute();
$user = $query->fetch();
// Check if the logged-in user is already following the selected user
$query = $db->prepare('SELECT COUNT(*) FROM following WHERE follower_email = :follower_email AND following_email = :following_email');
$query->bindParam(':follower_email', $email);
$query->bindParam(':following_email', $user_email);
$query->execute();
$is_following = $query->fetchColumn();
// Handle following/unfollowing actions
if (isset($_POST['follow'])) {
// Add a following relationship between the logged-in user and the selected user
$query = $db->prepare('INSERT INTO following (follower_email, following_email) VALUES (:follower_email, :following_email)');
$query->bindParam(':follower_email', $email);
$query->bindParam(':following_email', $user_email);
$query->execute();
$is_following = true;
header("Location: ?artworkid={$image['id']}");
exit;
} elseif (isset($_POST['unfollow'])) {
// Remove the following relationship between the logged-in user and the selected user
$query = $db->prepare('DELETE FROM following WHERE follower_email = :follower_email AND following_email = :following_email');
$query->bindParam(':follower_email', $email);
$query->bindParam(':following_email', $user_email);
$query->execute();
$is_following = false;
header("Location: ?artworkid={$image['id']}");
exit;
}
// Process any favorite/unfavorite requests
if (isset($_POST['favorite'])) {
$image_id = $_POST['image_id'];
// Check if the image has already been favorited by the current user
$stmt = $db->prepare("SELECT COUNT(*) FROM favorites WHERE email = :email AND image_id = :image_id");
$stmt->bindParam(':email', $_SESSION['email']);
$stmt->bindParam(':image_id', $image_id);
$stmt->execute();
$existing_fav = $stmt->fetchColumn();
if ($existing_fav == 0) {
$stmt = $db->prepare("INSERT INTO favorites (email, image_id) VALUES (:email, :image_id)");
$stmt->bindParam(':email', $_SESSION['email']);
$stmt->bindParam(':image_id', $image_id);
$stmt->execute();
}
// Redirect to the same page to prevent duplicate form submissions
header("Location: ?artworkid={$image['id']}");
exit();
} elseif (isset($_POST['unfavorite'])) {
$image_id = $_POST['image_id'];
$stmt = $db->prepare("DELETE FROM favorites WHERE email = :email AND image_id = :image_id");
$stmt->bindParam(':email', $_SESSION['email']);
$stmt->bindParam(':image_id', $image_id);
$stmt->execute();
// Redirect to the same page to prevent duplicate form submissions
header("Location: ?artworkid={$image['id']}");
exit();
}
$url_comment = "comments_preview.php?imageid=" . $image_id;
// Increment the view count for the image
$stmt = $db->prepare("UPDATE images SET view_count = view_count + 1 WHERE id = :artworkid");
$stmt->bindParam(':artworkid', $artworkId);
$stmt->execute();
// Get the updated image information from the database
$stmt = $db->prepare("SELECT * FROM images WHERE id = :artworkid");
$stmt->bindParam(':artworkid', $artworkId);
$stmt->execute();
$image = $stmt->fetch();
// Retrieve the updated view count from the image information
$viewCount = $image['view_count'];
// Create the "history" table if it does not exist
$stmt = $db->prepare("CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY AUTOINCREMENT, history TEXT, email TEXT, image_artworkid TEXT, date_history DATETIME)");
$stmt->execute();
// Store the link URL and image ID into the "history" table
if (isset($_GET['artworkid'])) {
$artworkId = $_GET['artworkid'];
$uri = $_SERVER['REQUEST_URI'];
$email = $_SESSION['email'];
$currentDate = date('Y-m-d'); // Get the current date
// Check if the same URL and image ID exist in the history for the current day
$stmt = $db->prepare("SELECT * FROM history WHERE history = :history AND image_artworkid = :artworkId AND email = :email AND date_history = :date_history");
$stmt->bindParam(':history', $uri);
$stmt->bindParam(':artworkId', $artworkId);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':date_history', $currentDate);
$stmt->execute();
$existing_entry = $stmt->fetch();
if (!$existing_entry) {
// Insert the URL and image ID into the history table
$stmt = $db->prepare("INSERT INTO history (history, email, image_artworkid, date_history) VALUES (:history, :email, :artworkId, :date_history)");
$stmt->bindParam(':history', $uri);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':artworkId', $artworkId);
$stmt->bindParam(':date_history', $currentDate);
$stmt->execute();
}
}
// Get all child images associated with the current image from the "image_child" table
$stmt = $db->prepare("SELECT * FROM image_child WHERE image_id = :image_id");
$stmt->bindParam(':image_id', $image_id);
$stmt->execute();
$child_images = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Count the total number of images from "images" table for the specific artworkid
$stmt = $db->prepare("SELECT COUNT(*) as total_images FROM images WHERE id = :artworkid");
$stmt->bindParam(':artworkid', $artworkId);
$stmt->execute();
$total_images = $stmt->fetch()['total_images'];
// Count the total number of images from "image_child" table for the specific artworkid
$stmt = $db->prepare("SELECT COUNT(*) as total_child_images FROM image_child WHERE image_id = :artworkid");
$stmt->bindParam(':artworkid', $artworkId);
$stmt->execute();
$total_child_images = $stmt->fetch()['total_child_images'];
// Calculate the combined total
$total_all_images = $total_images + $total_child_images;
// Get image size of the original image in megabytes
$original_image_size = round(filesize('images/' . $image['filename']) / (1024 * 1024), 2);
// Get image size of the thumbnail in megabytes
$thumbnail_image_size = round(filesize('thumbnails/' . $image['filename']) / (1024 * 1024), 2);
// Calculate the percentage of reduction
$reduction_percentage = ((($original_image_size - $thumbnail_image_size) / $original_image_size) * 100);
// Get image dimensions
list($width, $height) = getimagesize('images/' . $image['filename']);
// Get the current date
$currentDate = date('Y-m-d');
// Check if there's already a record for today in the daily table
$stmt = $db->prepare("SELECT * FROM daily WHERE image_id = :image_id AND date = :date");
$stmt->bindParam(':image_id', $image['id']);
$stmt->bindParam(':date', $currentDate);
$stmt->execute();
$daily_view = $stmt->fetch();
if ($daily_view) {
// If there's already a record for today, increment the view count
$stmt = $db->prepare("UPDATE daily SET views = views + 1 WHERE id = :id");
$stmt->bindParam(':id', $daily_view['id']);
$stmt->execute();
} else {
// If there's no record for today, insert a new record
$stmt = $db->prepare("INSERT INTO daily (image_id, views, date) VALUES (:image_id, 1, :date)");
$stmt->bindParam(':image_id', $image['id']);
$stmt->bindParam(':date', $currentDate);
$stmt->execute();
}
// New Section: Extract the filename only (e.g., d7361061dd44_i0.png)
$filenameWithoutExtension = preg_replace('/.*\/([^\/]+)$/i', '$1', $image['filename']);
// New Section: Extract the base path up to `imageassets_<unique_id>` (e.g., uid_1/data/imageid-24/imageassets_d7361061dd44)
$baseFilename = preg_replace('/(.*imageassets_[^\/]+)\/[^\/]+$/i', '$1', $image['filename']);
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="<?php include($_SERVER['DOCUMENT_ROOT'] . '/appearance/mode.php'); ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo $image['title']; ?></title>
<link rel="icon" type="image/png" href="icon/favicon.png">
<?php include('bootstrapcss.php'); ?>
<style>
body, html {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
overflow: hidden; /* Remove scrollbars */
}
iframe {
border: none; /* Remove default border */
width: 100%;
height: 100%;
display: block; /* Ensure iframe takes up the full container */
}
.spinner-container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
background-color: #fff; /* Optional: background color for better visibility */
position: absolute;
}
</style>
</head>
<body>
<div class="spinner-container" id="spinner">
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
<iframe src="/images/viewer.php?<?php echo $baseFilename; ?>#pid=<?php echo $filenameWithoutExtension; ?>" sandbox="allow-scripts allow-same-origin" onload="hideSpinner()"></iframe>
<button class="position-absolute end-0 bottom-0 m-3 btn btn-primary rounded-pill fw-bold btn-sm" onclick="window.location.reload();"><i class="bi bi-arrow-clockwise"></i></button>
<script>
function hideSpinner() {
document.getElementById('spinner').style.display = 'none';
}
</script>
</body>
</html>