webring/ring.php

46 lines
No EOL
1.1 KiB
PHP

<?php
// script de redirection pour le webring
$direction = $_GET['direction'] ?? '';
if (!in_array($direction, ['prev', 'next'])) {
http_response_code(400);
die('Direction invalide.');
}
// Récupère l'URL du site d'où vient l'utilisateur
$referer = $_SERVER['HTTP_REFERER'] ?? '';
if (!$referer) {
http_response_code(400);
die('Referer manquant.');
}
// Nettoie l'URL du qui arrive
$referer = rtrim(explode('?', $referer)[0], '/');
$sites_json = file_get_contents('webring.json');
$sites = json_decode($sites_json, true);
if (!$sites) {
http_response_code(500);
die('Configuration du webring introuvable.');
}
// cherche dans la liste
$index = array_search($referer, array_column($sites, 'url'));
if ($index === false) {
http_response_code(404);
die('Site non membre du webring.');
}
// calcul du voisin
$count = count($sites);
if ($direction === 'next') {
$targetIndex = ($index + 1) % $count;
} else {
$targetIndex = ($index - 1 + $count) % $count;
}
$targetUrl = $sites[$targetIndex]['url'];
// redirection
header("Location: $targetUrl", true, 302);
exit;