summaryrefslogtreecommitdiff
path: root/public
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-06-20 00:33:54 +0500
committerilotterytea <iltsu@alright.party>2025-06-20 00:33:54 +0500
commit063b21f28fd70bb04524e04a489804e04293b900 (patch)
treeb8254e334c1423ff427dc47b593616c773b50924 /public
parentb9b45c246343a2d4f61f09974edae2e9cb6ed93f (diff)
feat: don't show same random files
Diffstat (limited to 'public')
-rw-r--r--public/index.php32
1 files changed, 26 insertions, 6 deletions
diff --git a/public/index.php b/public/index.php
index f5809a5..265a03e 100644
--- a/public/index.php
+++ b/public/index.php
@@ -10,13 +10,33 @@ session_start();
$db = new PDO(DB_URL, DB_USER, DB_PASS);
if (FILE_CATALOG_RANDOM && isset($_GET['random'])) {
- $stmt = $db->query('SELECT id, extension FROM files ORDER BY rand() LIMIT 1');
- $stmt->execute();
+ $random_viewed_files = $_SESSION['random_viewed_files'] ?? [];
- if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
- header("Location: /{$row['id']}.{$row['extension']}");
- exit;
- }
+ $in = !empty($random_viewed_files) ? (str_repeat('?,', count($random_viewed_files) - 1) . '?') : '';
+ $in_condition = !empty($random_viewed_files) ? "WHERE id NOT IN ($in)" : "";
+
+ do {
+ $stmt = $db->prepare("SELECT id, extension FROM files $in_condition ORDER BY rand() LIMIT 1");
+ if (empty($random_viewed_files)) {
+ $stmt->execute();
+ } else {
+ $stmt->execute($random_viewed_files);
+ }
+
+ if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+ $file_id = $row['id'];
+ $file_path = "{$row['id']}.{$row['extension']}";
+ } else {
+ $random_viewed_files = array_diff($random_viewed_files, $random_viewed_files);
+ $in_condition = '';
+ }
+ } while (!$file_id || in_array($file_id, $random_viewed_files));
+
+ array_push($random_viewed_files, $file_id);
+ $_SESSION['random_viewed_files'] = $random_viewed_files;
+
+ header("Location: /$file_path");
+ exit;
}
$file = null;