refactor: split config.php into modules and lazy-load categories
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<!-- Footer -->
|
||||
<?php require_once __DIR__ . '/nav-context.php'; ?>
|
||||
<div class="footer">
|
||||
<div class="footer-header">
|
||||
<div class="footer-logo">
|
||||
|
||||
+3
-102
@@ -75,108 +75,9 @@ if ($heroType === 'none') {
|
||||
$showNextLiveAnnouncement = defined('NEXT_LIVE_ENABLED') && NEXT_LIVE_ENABLED === true;
|
||||
|
||||
if ($showNextLiveAnnouncement) {
|
||||
// Afficher l'annonce du prochain live
|
||||
// Définir l'image de fond si disponible
|
||||
$bgImageStyle = '';
|
||||
if (!empty(NEXT_LIVE_IMAGE) && file_exists(NEXT_LIVE_IMAGE)) {
|
||||
$bgImageStyle = 'background-image: url(\'' . htmlspecialchars(NEXT_LIVE_IMAGE) . '\');';
|
||||
}
|
||||
?>
|
||||
<?php if (!empty($bgImageStyle)): ?>
|
||||
<style nonce="<?php echo getCspNonce(); ?>">
|
||||
.hero-next-live { <?php echo $bgImageStyle; ?> }
|
||||
</style>
|
||||
<?php endif; ?>
|
||||
<div class="hero-next-live">
|
||||
<?php if (!empty(NEXT_LIVE_IMAGE) && file_exists(NEXT_LIVE_IMAGE)): ?>
|
||||
<div class="hero-next-live-image-container">
|
||||
<img src="<?php echo htmlspecialchars(NEXT_LIVE_IMAGE); ?>"
|
||||
alt="<?php echo htmlspecialchars(NEXT_LIVE_TITLE); ?>"
|
||||
class="hero-next-live-image">
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<div class="hero-next-live-content">
|
||||
<i class="fas fa-calendar-alt"></i>
|
||||
<?php
|
||||
if (!empty(NEXT_LIVE_DATE)) {
|
||||
$liveDate = new DateTime(NEXT_LIVE_DATE, new DateTimeZone(DEFAULT_TIMEZONE));
|
||||
$dayFormatter = new IntlDateFormatter(
|
||||
'fr_FR',
|
||||
IntlDateFormatter::FULL,
|
||||
IntlDateFormatter::NONE,
|
||||
DEFAULT_TIMEZONE,
|
||||
IntlDateFormatter::GREGORIAN,
|
||||
'EEEE d MMMM'
|
||||
);
|
||||
$formattedDay = $dayFormatter->format($liveDate);
|
||||
$formattedDay = ucfirst($formattedDay);
|
||||
$dynamicTitle = NEXT_LIVE_TITLE . ' - ' . $formattedDay;
|
||||
} else {
|
||||
$dynamicTitle = NEXT_LIVE_TITLE;
|
||||
}
|
||||
?>
|
||||
<h2><?php echo htmlspecialchars($dynamicTitle); ?></h2>
|
||||
<?php
|
||||
if (!empty(NEXT_LIVE_DATE)) {
|
||||
$liveHour = $liveDate->format('H\hi');
|
||||
$dynamicDescription = 'Rejoignez-nous à ' . $liveHour . '. ' . NEXT_LIVE_DESCRIPTION;
|
||||
} else {
|
||||
$dynamicDescription = NEXT_LIVE_DESCRIPTION;
|
||||
}
|
||||
?>
|
||||
<p><?php echo nl2br(htmlspecialchars($dynamicDescription)); ?></p>
|
||||
<?php if (!empty(NEXT_LIVE_DATE)): ?>
|
||||
<div class="hero-next-live-datetime">
|
||||
<p class="hero-next-live-date">
|
||||
<i class="fas fa-clock"></i>
|
||||
<?php
|
||||
$formatter = new IntlDateFormatter(
|
||||
'fr_FR',
|
||||
IntlDateFormatter::FULL,
|
||||
IntlDateFormatter::SHORT,
|
||||
DEFAULT_TIMEZONE
|
||||
);
|
||||
echo $formatter->format($liveDate);
|
||||
|
||||
$offset = $liveDate->format('P');
|
||||
echo ' <span class="utc-offset">(UTC' . $offset . ')</span>';
|
||||
?>
|
||||
</p>
|
||||
|
||||
<!-- Autres fuseaux horaires -->
|
||||
<div class="hero-next-live-timezones">
|
||||
<?php
|
||||
// Ordre croissant : du plus en retard au plus en avance
|
||||
$timezones = [
|
||||
'Ma\'ohi Nui' => 'Pacific/Tahiti',
|
||||
'Martinique / Guadeloupe' => 'America/Martinique',
|
||||
'Guyane' => 'America/Cayenne',
|
||||
'France' => 'Europe/Paris',
|
||||
'Kanaky' => 'Pacific/Noumea'
|
||||
];
|
||||
|
||||
foreach($timezones as $name => $timezone):
|
||||
$liveDateLocal = clone $liveDate;
|
||||
$liveDateLocal->setTimezone(new DateTimeZone($timezone));
|
||||
$dayDiff = $liveDateLocal->format('j') - $liveDate->format('j');
|
||||
|
||||
$dayIndicator = '';
|
||||
if ($dayDiff > 0) {
|
||||
$dayIndicator = ' <span class="day-shift">+1j</span>';
|
||||
} elseif ($dayDiff < 0) {
|
||||
$dayIndicator = ' <span class="day-shift">-1j</span>';
|
||||
}
|
||||
?>
|
||||
<span class="hero-timezone-item">
|
||||
<strong><?php echo $name; ?> :</strong> <?php echo $liveDateLocal->format('H\hi'); ?><?php echo $dayIndicator; ?>
|
||||
</span>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
// Afficher l'annonce du prochain live (partial partagé avec direct.php)
|
||||
$nextLiveVariant = 'hero';
|
||||
include __DIR__ . '/partials/next-live.php';
|
||||
} else {
|
||||
?>
|
||||
<div class="hero-no-live">
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
/**
|
||||
* Intégration Castopod : lecture des feeds RSS des podcasts.
|
||||
*
|
||||
* Extrait de includes/config.php (ARC-1) — chargé par ce dernier.
|
||||
* Dépend de lib/http.php (client cURL) et lib/format.php (formatDuration).
|
||||
*/
|
||||
|
||||
/**
|
||||
* Récupère les derniers épisodes d'un ou plusieurs podcasts Castopod via leurs feeds RSS
|
||||
*
|
||||
* @param string $castopodUrl URL de l'instance Castopod
|
||||
* @param array|string $podcastSlugs Slug(s) du/des podcast(s) - tableau ou chaîne unique
|
||||
* @param int $count Nombre d'épisodes à récupérer (total)
|
||||
* @return array Liste des épisodes formatés, triés par date
|
||||
*/
|
||||
function getCastopodEpisodes($castopodUrl = null, $podcastSlugs = null, $count = 5) {
|
||||
if (!defined('CASTOPOD_ENABLED') || !CASTOPOD_ENABLED) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$castopodUrl = $castopodUrl ?? CASTOPOD_URL;
|
||||
$podcastSlugs = $podcastSlugs ?? (defined('CASTOPOD_PODCAST_SLUGS') ? CASTOPOD_PODCAST_SLUGS : ['annu_kute_cedric']);
|
||||
$count = $count ?? CASTOPOD_EPISODES_COUNT;
|
||||
|
||||
// Validation de l'URL Castopod pour prévenir SSRF
|
||||
if (!isValidRemoteUrl($castopodUrl)) {
|
||||
error_log('SECURITY: Invalid Castopod URL detected: ' . $castopodUrl);
|
||||
return [];
|
||||
}
|
||||
|
||||
// Convertir en tableau si c'est une chaîne unique (rétrocompatibilité)
|
||||
if (is_string($podcastSlugs)) {
|
||||
$podcastSlugs = [$podcastSlugs];
|
||||
}
|
||||
|
||||
// Clé de cache unique pour la combinaison de podcasts
|
||||
$cacheKey = 'castopod_' . md5($castopodUrl . implode('_', $podcastSlugs) . '_' . $count);
|
||||
|
||||
// Vérifier le cache
|
||||
if (defined('CACHE_ENABLED') && CACHE_ENABLED) {
|
||||
$cachedData = getFromCache($cacheKey);
|
||||
if ($cachedData !== null) {
|
||||
return $cachedData;
|
||||
}
|
||||
}
|
||||
|
||||
$allEpisodes = [];
|
||||
|
||||
// Itérer sur chaque podcast
|
||||
foreach ($podcastSlugs as $index => $podcastSlug) {
|
||||
try {
|
||||
// Construire l'URL du feed RSS pour ce podcast
|
||||
$feedUrl = rtrim($castopodUrl, '/') . '/@' . $podcastSlug . '/feed';
|
||||
|
||||
// Récupérer le feed RSS avec backoff uniquement sur rate limit (HTTP 429)
|
||||
$maxAttempts = 3;
|
||||
$attempt = 0;
|
||||
$xmlContent = false;
|
||||
$httpCode = 0;
|
||||
|
||||
while ($attempt < $maxAttempts) {
|
||||
$response = httpGet($feedUrl, ['timeout' => 10]);
|
||||
$xmlContent = $response['body'];
|
||||
$httpCode = $response['code'];
|
||||
|
||||
// Succès ou erreur définitive : on sort de la boucle
|
||||
if ($httpCode !== 429) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Rate limit : attendre avant de réessayer (backoff exponentiel simple)
|
||||
$attempt++;
|
||||
if ($attempt < $maxAttempts) {
|
||||
sleep(5 * $attempt);
|
||||
}
|
||||
}
|
||||
|
||||
if ($httpCode !== 200 || !$xmlContent) {
|
||||
continue; // Passer au podcast suivant en cas d'erreur
|
||||
}
|
||||
|
||||
$episodes = [];
|
||||
|
||||
// Vérifier si SimpleXML est disponible
|
||||
if (function_exists('simplexml_load_string')) {
|
||||
// Parser avec SimpleXML (méthode recommandée)
|
||||
$xml = simplexml_load_string($xmlContent);
|
||||
if (!$xml) {
|
||||
continue; // Passer au podcast suivant
|
||||
}
|
||||
|
||||
// Enregistrer les namespaces
|
||||
$namespaces = $xml->getNamespaces(true);
|
||||
|
||||
// Extraire les informations du podcast
|
||||
$podcastTitle = (string) $xml->channel->title;
|
||||
$podcastImage = (string) $xml->channel->image->url;
|
||||
$podcastLink = (string) $xml->channel->link;
|
||||
|
||||
// Parcourir les items (épisodes) - on récupère tous pour trier après
|
||||
foreach ($xml->channel->item as $item) {
|
||||
|
||||
// Extraire les infos de l'épisode
|
||||
$itunesNs = $item->children($namespaces['itunes'] ?? 'http://www.itunes.com/dtds/podcast-1.0.dtd');
|
||||
|
||||
// Récupérer l'image de l'épisode
|
||||
$episodeImage = $podcastImage;
|
||||
if (isset($itunesNs->image)) {
|
||||
$imageAttrs = $itunesNs->image->attributes();
|
||||
if (isset($imageAttrs['href'])) {
|
||||
$episodeImage = (string) $imageAttrs['href'];
|
||||
}
|
||||
}
|
||||
|
||||
$episode = [
|
||||
'title' => (string) $item->title,
|
||||
'link' => (string) $item->link,
|
||||
'pubDate' => (string) $item->pubDate,
|
||||
'description' => strip_tags((string) $item->description),
|
||||
'duration' => (string) $itunesNs->duration ?? '',
|
||||
'image' => $episodeImage,
|
||||
'audioUrl' => (string) $item->enclosure['url'] ?? '',
|
||||
'podcastTitle' => $podcastTitle,
|
||||
'podcastLink' => $podcastLink,
|
||||
'podcastSlug' => $podcastSlug,
|
||||
];
|
||||
|
||||
// Formater la date
|
||||
if ($episode['pubDate']) {
|
||||
try {
|
||||
$date = new DateTime($episode['pubDate']);
|
||||
$episode['formattedDate'] = $date->format('d/m/Y');
|
||||
$episode['timestamp'] = $date->getTimestamp();
|
||||
} catch (Exception $e) {
|
||||
$episode['formattedDate'] = '';
|
||||
$episode['timestamp'] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Formater la durée
|
||||
if ($episode['duration']) {
|
||||
$episode['formattedDuration'] = formatDuration($episode['duration']);
|
||||
} else {
|
||||
$episode['formattedDuration'] = '';
|
||||
}
|
||||
|
||||
// Limiter la description
|
||||
if (strlen($episode['description']) > 150) {
|
||||
$episode['description'] = substr($episode['description'], 0, 150) . '...';
|
||||
}
|
||||
|
||||
$episodes[] = $episode;
|
||||
}
|
||||
} else {
|
||||
// Fallback: Parser avec regex (moins fiable mais fonctionne sans SimpleXML)
|
||||
// Extraire les infos du podcast
|
||||
preg_match('/<channel>.*?<title>(.*?)<\/title>/s', $xmlContent, $podcastTitleMatch);
|
||||
preg_match('/<channel>.*?<link>(.*?)<\/link>/s', $xmlContent, $podcastLinkMatch);
|
||||
preg_match('/<image>.*?<url>(.*?)<\/url>/s', $xmlContent, $podcastImageMatch);
|
||||
|
||||
$podcastTitle = $podcastTitleMatch[1] ?? '';
|
||||
$podcastLink = $podcastLinkMatch[1] ?? '';
|
||||
$podcastImage = $podcastImageMatch[1] ?? '';
|
||||
|
||||
// Extraire tous les items
|
||||
preg_match_all('/<item>(.*?)<\/item>/s', $xmlContent, $items);
|
||||
|
||||
foreach ($items[1] as $itemContent) {
|
||||
// Extraire les données de chaque item
|
||||
preg_match('/<title>(.*?)<\/title>/', $itemContent, $titleMatch);
|
||||
preg_match('/<link>(.*?)<\/link>/', $itemContent, $linkMatch);
|
||||
preg_match('/<pubDate>(.*?)<\/pubDate>/', $itemContent, $dateMatch);
|
||||
preg_match('/<description>(.*?)<\/description>/s', $itemContent, $descMatch);
|
||||
preg_match('/<itunes:duration>(.*?)<\/itunes:duration>/', $itemContent, $durationMatch);
|
||||
preg_match('/<itunes:image[^>]*href=["\']([^"\']*)["\']/', $itemContent, $imageMatch);
|
||||
preg_match('/<enclosure[^>]*url=["\']([^"\']*)["\']/', $itemContent, $audioMatch);
|
||||
|
||||
$episode = [
|
||||
'title' => $titleMatch[1] ?? '',
|
||||
'link' => $linkMatch[1] ?? '',
|
||||
'pubDate' => $dateMatch[1] ?? '',
|
||||
'description' => strip_tags($descMatch[1] ?? ''),
|
||||
'duration' => $durationMatch[1] ?? '',
|
||||
'image' => $imageMatch[1] ?? $podcastImage,
|
||||
'audioUrl' => $audioMatch[1] ?? '',
|
||||
'podcastTitle' => $podcastTitle,
|
||||
'podcastLink' => $podcastLink,
|
||||
'podcastSlug' => $podcastSlug,
|
||||
];
|
||||
|
||||
// Formater la date
|
||||
if ($episode['pubDate']) {
|
||||
try {
|
||||
$date = new DateTime($episode['pubDate']);
|
||||
$episode['formattedDate'] = $date->format('d/m/Y');
|
||||
$episode['timestamp'] = $date->getTimestamp();
|
||||
} catch (Exception $e) {
|
||||
$episode['formattedDate'] = '';
|
||||
$episode['timestamp'] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Formater la durée
|
||||
if ($episode['duration']) {
|
||||
$episode['formattedDuration'] = formatDuration($episode['duration']);
|
||||
} else {
|
||||
$episode['formattedDuration'] = '';
|
||||
}
|
||||
|
||||
// Limiter la description
|
||||
if (strlen($episode['description']) > 150) {
|
||||
$episode['description'] = substr($episode['description'], 0, 150) . '...';
|
||||
}
|
||||
|
||||
$episodes[] = $episode;
|
||||
}
|
||||
}
|
||||
|
||||
// Ajouter les épisodes de ce podcast à la liste globale
|
||||
$allEpisodes = array_merge($allEpisodes, $episodes);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log('Erreur lors de la récupération des épisodes Castopod pour ' . $podcastSlug . ': ' . $e->getMessage());
|
||||
// Continuer avec le podcast suivant
|
||||
}
|
||||
}
|
||||
|
||||
// Trier tous les épisodes par date (du plus récent au plus ancien)
|
||||
usort($allEpisodes, function($a, $b) {
|
||||
return ($b['timestamp'] ?? 0) - ($a['timestamp'] ?? 0);
|
||||
});
|
||||
|
||||
// Limiter au nombre demandé
|
||||
$allEpisodes = array_slice($allEpisodes, 0, $count);
|
||||
|
||||
// Mettre en cache (uniquement si non vide : une erreur temporaire
|
||||
// — rate limit HTTP 429, timeout… — ne doit pas être figée en cache)
|
||||
if (!empty($allEpisodes) && defined('CACHE_ENABLED') && CACHE_ENABLED) {
|
||||
saveToCache($cacheKey, $allEpisodes);
|
||||
}
|
||||
|
||||
return $allEpisodes;
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/**
|
||||
* Fonctions de formatage des données (vidéos, durées, dates, tailles).
|
||||
*
|
||||
* Extrait de includes/config.php (ARC-1) — chargé par ce dernier.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Formate les données brutes des vidéos venant de l'API
|
||||
*
|
||||
* @param array $videosData Données brutes des vidéos
|
||||
* @return array Données formatées
|
||||
*/
|
||||
function formatVideosData($videosData) {
|
||||
$videos = [];
|
||||
|
||||
foreach ($videosData as $video) {
|
||||
// Ignorer les entrées sans uuid : l'identifiant est indispensable
|
||||
if (empty($video['uuid'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Récupérer la vignette (thumbnail)
|
||||
$thumbnail = isset($video['previewPath'])
|
||||
? PEERTUBE_URL . $video['previewPath']
|
||||
: 'img/default-thumbnail.jpg';
|
||||
|
||||
// Récupérer l'avatar de la chaîne
|
||||
$channelAvatar = isset($video['channel']['avatars'][0]['path'])
|
||||
? PEERTUBE_URL . $video['channel']['avatars'][0]['path']
|
||||
: 'img/default-avatar.png';
|
||||
|
||||
// Formater les données (valeurs par défaut pour les champs absents)
|
||||
$videos[] = [
|
||||
'id' => $video['uuid'],
|
||||
'title' => $video['name'] ?? '',
|
||||
'thumbnail' => $thumbnail,
|
||||
'duration' => $video['duration'] ?? 0,
|
||||
'channel' => $video['channel']['displayName'] ?? '',
|
||||
'channelAvatar' => $channelAvatar,
|
||||
'views' => $video['views'] ?? 0,
|
||||
'date' => $video['publishedAt'] ?? '',
|
||||
'aspectRatio' => $video['aspectRatio'] ?? null,
|
||||
'description' => $video['description'] ?? '',
|
||||
'tags' => $video['tags'] ?? [],
|
||||
'isLive' => $video['isLive'] ?? false
|
||||
];
|
||||
}
|
||||
|
||||
return $videos;
|
||||
}
|
||||
|
||||
// Fonctions utilitaires pour formater les données d'affichage
|
||||
function formatDuration($seconds) {
|
||||
$hours = floor($seconds / 3600);
|
||||
$minutes = floor(($seconds % 3600) / 60);
|
||||
$remainingSeconds = $seconds % 60;
|
||||
|
||||
if ($hours > 0) {
|
||||
return sprintf('%d:%02d:%02d', $hours, $minutes, $remainingSeconds);
|
||||
} else {
|
||||
return sprintf('%d:%02d', $minutes, $remainingSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
function formatViewCount($views) {
|
||||
if ($views >= 1000000) {
|
||||
return round($views / 1000000, 1) . 'M';
|
||||
} elseif ($views >= 1000) {
|
||||
return round($views / 1000, 1) . 'K';
|
||||
} else {
|
||||
return $views;
|
||||
}
|
||||
}
|
||||
|
||||
function formatDate($dateString) {
|
||||
// Chaîne vide : rien à formater (new DateTime('') renverrait « maintenant »)
|
||||
if (!is_string($dateString) || trim($dateString) === '') {
|
||||
return (string) $dateString;
|
||||
}
|
||||
|
||||
try {
|
||||
$date = new DateTime($dateString);
|
||||
} catch (Exception $e) {
|
||||
// Date malformée (données API inattendues) : afficher la chaîne brute
|
||||
return $dateString;
|
||||
}
|
||||
|
||||
$now = new DateTime();
|
||||
$interval = $now->diff($date);
|
||||
|
||||
if ($interval->days == 0) {
|
||||
return 'Aujourd\'hui';
|
||||
} elseif ($interval->days == 1) {
|
||||
return 'Hier';
|
||||
} elseif ($interval->days < 7) {
|
||||
return 'Il y a ' . $interval->days . ' jours';
|
||||
} elseif ($interval->days < 30) {
|
||||
$weeks = floor($interval->days / 7);
|
||||
return 'Il y a ' . $weeks . ' semaine' . ($weeks > 1 ? 's' : '');
|
||||
} elseif ($interval->days < 365) {
|
||||
$months = floor($interval->days / 30);
|
||||
return 'Il y a ' . $months . ' mois';
|
||||
} else {
|
||||
$years = floor($interval->days / 365);
|
||||
return 'Il y a ' . $years . ' an' . ($years > 1 ? 's' : '');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formate la taille d'un fichier en format lisible
|
||||
* @param int $bytes Taille en octets
|
||||
* @return string Taille formatée
|
||||
*/
|
||||
function formatFileSize($bytes) {
|
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
|
||||
$bytes = max($bytes, 0);
|
||||
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
||||
$pow = min($pow, count($units) - 1);
|
||||
|
||||
$bytes /= (1 << (10 * $pow));
|
||||
|
||||
return round($bytes, 2) . ' ' . $units[$pow];
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
/**
|
||||
* Intégration Funkwhale : récupération de morceaux aléatoires via l'API.
|
||||
*
|
||||
* Extrait de includes/config.php (ARC-1) — chargé par ce dernier.
|
||||
* Dépend de lib/http.php (client cURL) et lib/format.php (formatDuration).
|
||||
*/
|
||||
|
||||
/**
|
||||
* Récupère des morceaux aléatoires depuis Funkwhale via l'API
|
||||
*
|
||||
* @param string $funkwhaleUrl URL de l'instance Funkwhale
|
||||
* @param int $count Nombre de morceaux à récupérer
|
||||
* @return array Liste des morceaux formatés
|
||||
*/
|
||||
function getFunkwhaleTracks($funkwhaleUrl = null, $count = null) {
|
||||
if (!defined('FUNKWHALE_ENABLED') || !FUNKWHALE_ENABLED) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$funkwhaleUrl = $funkwhaleUrl ?? FUNKWHALE_URL;
|
||||
$count = $count ?? FUNKWHALE_TRACKS_COUNT;
|
||||
|
||||
// Validation de l'URL Funkwhale pour prévenir SSRF
|
||||
if (!isValidRemoteUrl($funkwhaleUrl)) {
|
||||
error_log('SECURITY: Invalid Funkwhale URL detected: ' . $funkwhaleUrl);
|
||||
return [];
|
||||
}
|
||||
|
||||
// Clé de cache - on récupère un grand nombre de morceaux pour le cache
|
||||
$cacheKey = 'funkwhale_' . md5($funkwhaleUrl);
|
||||
$cacheFetchSize = 50; // Nombre de morceaux à mettre en cache
|
||||
|
||||
// Vérifier le cache
|
||||
$allTracks = null;
|
||||
if (defined('CACHE_ENABLED') && CACHE_ENABLED) {
|
||||
$allTracks = getFromCache($cacheKey);
|
||||
}
|
||||
|
||||
// Si pas de cache, récupérer depuis l'API
|
||||
if ($allTracks === null) {
|
||||
try {
|
||||
// Construire l'URL de l'API en ordre aléatoire ; le filtrage local
|
||||
// est fait ensuite côté PHP (comparaison de domaine)
|
||||
$apiUrl = rtrim($funkwhaleUrl, '/') . '/api/v1/tracks?ordering=random&page_size=' . $cacheFetchSize . '&scope=all';
|
||||
|
||||
// Récupérer les morceaux
|
||||
$response = httpGet($apiUrl, ['timeout' => 10]);
|
||||
$jsonContent = $response['body'];
|
||||
|
||||
if ($response['code'] !== 200 || !$jsonContent) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Parser le JSON
|
||||
$data = json_decode($jsonContent, true);
|
||||
if (!$data || !isset($data['results'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$allTracks = [];
|
||||
|
||||
// Extraire le domaine de l'instance locale pour filtrer
|
||||
$localDomain = parse_url($funkwhaleUrl, PHP_URL_HOST);
|
||||
|
||||
// Parcourir les morceaux et filtrer pour garder uniquement les morceaux locaux
|
||||
foreach ($data['results'] as $item) {
|
||||
// Vérifier si le morceau est local en comparant le domaine
|
||||
// Les morceaux fédérés ont généralement un domaine différent dans leur URL
|
||||
$isLocal = true;
|
||||
|
||||
// Vérifier via l'URL de l'artiste ou de l'album
|
||||
if (isset($item['artist']['channel'])) {
|
||||
$artistDomain = parse_url($item['artist']['channel'], PHP_URL_HOST);
|
||||
if ($artistDomain && $artistDomain !== $localDomain) {
|
||||
$isLocal = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Ou vérifier via le champ is_local si disponible
|
||||
if (isset($item['is_local']) && !$item['is_local']) {
|
||||
$isLocal = false;
|
||||
}
|
||||
|
||||
// Ne garder que les morceaux locaux
|
||||
if ($isLocal) {
|
||||
// Récupérer l'URL d'écoute depuis l'API (peut être relative)
|
||||
$listenUrl = $item['listen_url'] ?? '';
|
||||
|
||||
// Si l'URL est relative, la transformer en URL absolue
|
||||
if (!empty($listenUrl) && strpos($listenUrl, 'http') !== 0) {
|
||||
$listenUrl = rtrim($funkwhaleUrl, '/') . $listenUrl;
|
||||
}
|
||||
|
||||
$track = [
|
||||
'title' => $item['title'] ?? '',
|
||||
'artist' => $item['artist']['name'] ?? 'Artiste inconnu',
|
||||
'album' => $item['album']['title'] ?? '',
|
||||
'cover' => $item['album']['cover']['urls']['medium_square_crop'] ?? '',
|
||||
'duration' => $item['uploads'][0]['duration'] ?? 0,
|
||||
'link' => rtrim($funkwhaleUrl, '/') . '/library/tracks/' . ($item['id'] ?? ''),
|
||||
'trackId' => $item['id'] ?? 0,
|
||||
'audioUrl' => $listenUrl,
|
||||
];
|
||||
|
||||
// Formater la durée
|
||||
if ($track['duration']) {
|
||||
$track['formattedDuration'] = formatDuration($track['duration']);
|
||||
} else {
|
||||
$track['formattedDuration'] = '';
|
||||
}
|
||||
|
||||
$allTracks[] = $track;
|
||||
}
|
||||
}
|
||||
|
||||
// Mettre en cache tous les morceaux locaux
|
||||
if (defined('CACHE_ENABLED') && CACHE_ENABLED) {
|
||||
saveToCache($cacheKey, $allTracks);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log('Erreur lors de la récupération des morceaux Funkwhale: ' . $e->getMessage());
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// Si pas assez de morceaux, retourner ce qu'on a
|
||||
if (empty($allTracks)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Sélectionner aléatoirement $count morceaux parmi tous les morceaux en cache
|
||||
$trackCount = count($allTracks);
|
||||
if ($trackCount <= $count) {
|
||||
return $allTracks;
|
||||
}
|
||||
|
||||
// Mélanger et prendre les N premiers
|
||||
shuffle($allTracks);
|
||||
return array_slice($allTracks, 0, $count);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Client HTTP GET sécurisé, commun aux intégrations distantes
|
||||
* (PeerTube, Castopod, Funkwhale).
|
||||
*
|
||||
* Extrait de includes/config.php (ARC-1) — chargé par ce dernier.
|
||||
* Centralise les réglages cURL : timeouts bornés, vérification SSL
|
||||
* systématique, User-Agent identifié.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Effectue une requête HTTP GET avec des options de sécurité par défaut.
|
||||
*
|
||||
* @param string $url URL complète à appeler (validée au préalable contre les SSRF)
|
||||
* @param array $options Options optionnelles :
|
||||
* - timeout (int, défaut 10) : durée maximale totale en secondes
|
||||
* - connectTimeout (int, défaut 10) : durée maximale de connexion en secondes
|
||||
* - followLocation (bool, défaut true) : suivre les redirections
|
||||
* - maxRedirects (int, défaut 3) : nombre maximal de redirections suivies
|
||||
* - userAgent (string, défaut 'fediverse-oki/1.0') : en-tête User-Agent
|
||||
* - headers (array, défaut []) : en-têtes HTTP additionnels
|
||||
* @return array Tableau associatif : 'body' (string|false), 'code' (int), 'error' (string)
|
||||
*/
|
||||
function httpGet($url, $options = []) {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout'] ?? 10);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $options['connectTimeout'] ?? 10);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $options['followLocation'] ?? true);
|
||||
curl_setopt($ch, CURLOPT_MAXREDIRS, $options['maxRedirects'] ?? 3);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, $options['userAgent'] ?? 'fediverse-oki/1.0');
|
||||
|
||||
if (!empty($options['headers'])) {
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $options['headers']);
|
||||
}
|
||||
|
||||
$body = curl_exec($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
return ['body' => $body, 'code' => $code, 'error' => $error];
|
||||
}
|
||||
@@ -0,0 +1,519 @@
|
||||
<?php
|
||||
/**
|
||||
* Client de l'API PeerTube et fonctions de récupération des vidéos.
|
||||
*
|
||||
* Extrait de includes/config.php (ARC-1) — chargé par ce dernier.
|
||||
* Dépend de lib/http.php (client cURL), lib/format.php (formatage) et
|
||||
* includes/simple-cache.php (callPeerTubeApiCached).
|
||||
*/
|
||||
|
||||
/**
|
||||
* Initialise et récupère les catégories depuis l'API PeerTube
|
||||
*
|
||||
* @return array Liste des catégories
|
||||
*/
|
||||
function initCategories() {
|
||||
// Récupérer la liste des catégories depuis l'API
|
||||
$categories = callPeerTubeApi('videos/categories');
|
||||
|
||||
// Tableau de correspondance pour traduire les catégories en français
|
||||
$translations = [
|
||||
'Music' => 'Musique',
|
||||
'Films' => 'Films',
|
||||
'Vehicles' => 'Véhicules',
|
||||
'Art' => 'Art',
|
||||
'Sports' => 'Sports',
|
||||
'Travels' => 'Voyages',
|
||||
'Gaming' => 'Jeux vidéo',
|
||||
'People' => 'Personnes',
|
||||
'Comedy' => 'Humour',
|
||||
'Entertainment' => 'Divertissement',
|
||||
'News & Politics' => 'Actualités & Politique',
|
||||
'How To' => 'Tutoriels',
|
||||
'Education' => 'Éducation',
|
||||
'Activism' => 'Activisme',
|
||||
'Science & Technology' => 'Science & Technologie',
|
||||
'Animals' => 'Animaux',
|
||||
'Kids' => 'Enfants',
|
||||
'Food' => 'Cuisine',
|
||||
];
|
||||
|
||||
// Si une constante PRIORITY_CATEGORIES est définie, utiliser ces traductions
|
||||
if (defined('PRIORITY_CATEGORIES')) {
|
||||
$priorityCategories = PRIORITY_CATEGORIES;
|
||||
foreach ($priorityCategories as $id => $name) {
|
||||
// Trouver la clé anglaise correspondant à l'ID
|
||||
$englishName = array_search($id, array_keys($categories));
|
||||
if ($englishName !== false) {
|
||||
$translations[$englishName] = $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result = [];
|
||||
foreach ($categories as $key => $name) {
|
||||
// Utiliser la traduction si disponible, sinon garder le nom original
|
||||
$translatedName = isset($translations[$name]) ? $translations[$name] : $name;
|
||||
$result[$key] = $translatedName;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne les catégories PeerTube, chargées paresseusement.
|
||||
*
|
||||
* Remplace l'ancienne constante PEERTUBE_CATEGORIES (définie à l'inclusion
|
||||
* de config.php) : l'appel API n'a lieu qu'à la première utilisation et le
|
||||
* résultat est mémoïsé pour le reste de la requête (ARC-2).
|
||||
*
|
||||
* @return array Liste des catégories (id => nom traduit)
|
||||
*/
|
||||
function getPeertubeCategories() {
|
||||
static $categories = null;
|
||||
|
||||
if ($categories === null) {
|
||||
$categories = initCategories();
|
||||
}
|
||||
|
||||
return $categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Version originale pour appeler l'API PeerTube (sans cache)
|
||||
* Cette fonction est maintenant utilisée en interne par callPeerTubeApi
|
||||
*
|
||||
* @param string $endpoint Point de terminaison de l'API
|
||||
* @param array $params Paramètres optionnels pour la requête
|
||||
* @return array Données retournées par l'API
|
||||
*/
|
||||
function callPeerTubeApiOriginal($endpoint, $params = []) {
|
||||
// Validation de l'URL de base PeerTube pour prévenir SSRF
|
||||
if (!isValidPeerTubeUrl(PEERTUBE_URL)) {
|
||||
error_log('SECURITY: Invalid PeerTube URL detected: ' . PEERTUBE_URL);
|
||||
return [];
|
||||
}
|
||||
|
||||
// Nettoyer et valider l'endpoint
|
||||
$endpoint = ltrim($endpoint, '/');
|
||||
if (!isValidApiEndpoint($endpoint)) {
|
||||
error_log('SECURITY: Invalid API endpoint detected: ' . $endpoint);
|
||||
return [];
|
||||
}
|
||||
|
||||
$url = PEERTUBE_URL . '/api/v1/' . $endpoint;
|
||||
|
||||
// Ajouter les paramètres à l'URL
|
||||
if (!empty($params)) {
|
||||
$url .= '?' . http_build_query($params);
|
||||
}
|
||||
|
||||
// Options cURL : pas de redirection suivie (SSRF), timeouts bornés
|
||||
$options = [
|
||||
'timeout' => 30,
|
||||
'connectTimeout' => 10,
|
||||
'followLocation' => false,
|
||||
'maxRedirects' => 0,
|
||||
];
|
||||
|
||||
// Ajouter la clé API si définie
|
||||
if (defined('API_KEY') && !empty(API_KEY)) {
|
||||
$options['headers'] = [
|
||||
'Authorization: ApiKey ' . API_KEY
|
||||
];
|
||||
}
|
||||
|
||||
// Exécuter la requête
|
||||
$response = httpGet($url, $options);
|
||||
|
||||
// Traiter la réponse
|
||||
if ($response['body'] === false || !empty($response['error'])) {
|
||||
error_log('PeerTube API error: ' . $response['error']);
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($response['code'] < 200 || $response['code'] >= 300) {
|
||||
error_log('PeerTube API HTTP error: ' . $response['code']);
|
||||
return [];
|
||||
}
|
||||
|
||||
// Décoder la réponse JSON
|
||||
$data = json_decode($response['body'], true);
|
||||
|
||||
return $data ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction utilitaire pour appeler l'API PeerTube avec cache
|
||||
*
|
||||
* @param string $endpoint Point de terminaison de l'API
|
||||
* @param array $params Paramètres optionnels pour la requête
|
||||
* @return array Données retournées par l'API
|
||||
*/
|
||||
function callPeerTubeApi($endpoint, $params = []) {
|
||||
// Utiliser la fonction cachée si disponible
|
||||
if (function_exists('callPeerTubeApiCached')) {
|
||||
return callPeerTubeApiCached($endpoint, $params);
|
||||
}
|
||||
|
||||
// Fallback vers la version originale
|
||||
return callPeerTubeApiOriginal($endpoint, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Valide l'URL PeerTube pour prévenir les attaques SSRF
|
||||
* Alias historique de isValidRemoteUrl() (includes/security.php).
|
||||
*
|
||||
* @param string $url URL à valider
|
||||
* @return bool True si l'URL est valide et sûre
|
||||
*/
|
||||
function isValidPeerTubeUrl($url) {
|
||||
return isValidRemoteUrl($url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Valide l'endpoint API pour prévenir l'injection de chemins
|
||||
*
|
||||
* @param string $endpoint Endpoint à valider
|
||||
* @return bool True si l'endpoint est valide
|
||||
*/
|
||||
function isValidApiEndpoint($endpoint) {
|
||||
// Bloquer les tentatives de path traversal
|
||||
if (strpos($endpoint, '..') !== false || strpos($endpoint, '//') !== false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Autoriser uniquement les caractères alphanumériques, tirets, underscores et slashes
|
||||
if (!preg_match('/^[a-zA-Z0-9\/_-]+$/', $endpoint)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Liste blanche des endpoints autorisés
|
||||
$allowedEndpoints = [
|
||||
'videos',
|
||||
'videos/categories',
|
||||
'search/videos',
|
||||
'videos/.*', // Pour les endpoints dynamiques comme videos/{id}
|
||||
'videos/.*/comment-threads', // Pour les commentaires
|
||||
'accounts',
|
||||
'accounts/.*/videos', // Pour les vidéos d'un compte spécifique
|
||||
'video-channels/.*/videos' // Pour les vidéos d'une chaîne spécifique
|
||||
];
|
||||
|
||||
foreach ($allowedEndpoints as $pattern) {
|
||||
// Remplacer les .* par des marqueurs temporaires
|
||||
$tempPattern = str_replace('.*', '__WILDCARD__', $pattern);
|
||||
// Échapper les caractères spéciaux regex
|
||||
$escapedPattern = preg_quote($tempPattern, '/');
|
||||
// Remettre les wildcards en place
|
||||
$regexPattern = str_replace('__WILDCARD__', '.*', $escapedPattern);
|
||||
|
||||
if (preg_match('/^' . $regexPattern . '$/', $endpoint)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les catégories depuis l'API PeerTube
|
||||
*
|
||||
* @return array Liste des catégories
|
||||
*/
|
||||
function getCategories() {
|
||||
// Utiliser les catégories déjà récupérées
|
||||
$categories = getPeertubeCategories();
|
||||
|
||||
$result = [];
|
||||
foreach ($categories as $key => $name) {
|
||||
$result[] = [
|
||||
'id' => $key,
|
||||
'name' => $name
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les vidéos récentes depuis l'API PeerTube
|
||||
*
|
||||
* @param int $count Nombre de vidéos à récupérer
|
||||
* @return array Liste des vidéos récentes
|
||||
*/
|
||||
function getRecentVideos($count = RECENT_VIDEOS_COUNT) {
|
||||
// Récupérer les vidéos récentes
|
||||
$data = callPeerTubeApi('videos', [
|
||||
'sort' => '-publishedAt',
|
||||
'count' => $count,
|
||||
'isLocal' => true
|
||||
]);
|
||||
|
||||
return formatVideosData($data['data'] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les vidéos tendances depuis l'API PeerTube
|
||||
*
|
||||
* @param int $count Nombre de vidéos à récupérer
|
||||
* @return array Liste des vidéos tendances
|
||||
*/
|
||||
function getTrendingVideos($count = TRENDING_VIDEOS_COUNT) {
|
||||
// Récupérer les vidéos tendances
|
||||
$data = callPeerTubeApi('videos', [
|
||||
'sort' => '-trending',
|
||||
'count' => $count,
|
||||
'isLocal' => true
|
||||
]);
|
||||
|
||||
return formatVideosData($data['data'] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les vidéos avec un tag spécifique depuis l'API PeerTube
|
||||
*
|
||||
* @param string $tag Tag à filtrer
|
||||
* @param int $count Nombre de vidéos à récupérer
|
||||
* @return array Liste des vidéos
|
||||
*/
|
||||
function getVideosByTag($tag, $count) {
|
||||
// Récupérer les vidéos par tag
|
||||
$data = callPeerTubeApi('videos', [
|
||||
'tagsOneOf' => $tag,
|
||||
'count' => $count,
|
||||
'isLocal' => true
|
||||
]);
|
||||
|
||||
return formatVideosData($data['data'] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les shorts (vidéos courtes) depuis l'API PeerTube
|
||||
* Les shorts sont des vidéos locales de moins de 2 minutes
|
||||
*
|
||||
* @param int $count Nombre de shorts à récupérer
|
||||
* @return array Liste des shorts
|
||||
*/
|
||||
function getShorts($count = SHORTS_COUNT) {
|
||||
// Récupérer plus de vidéos que nécessaire pour pouvoir filtrer
|
||||
$data = callPeerTubeApi('videos', [
|
||||
'sort' => '-publishedAt', // Les plus récentes d'abord
|
||||
'count' => SHORTS_COUNT_SEARCH,
|
||||
'isLocal' => true
|
||||
]);
|
||||
|
||||
// Formater les données
|
||||
$allVideos = formatVideosData($data['data'] ?? []);
|
||||
|
||||
// Filtrer pour ne garder que les vidéos de moins de 2 minutes (120 secondes) et en mode portrait
|
||||
$shortVideos = array_filter($allVideos, function($video) {
|
||||
// Vérifier la durée (moins de 2 minutes)
|
||||
$durationOk = $video['duration'] < SHORTS_MAX_DURATION;
|
||||
|
||||
// Vérifier le ratio (mode portrait)
|
||||
$ratioOk = isset($video['aspectRatio']) && $video['aspectRatio'] <= 1;
|
||||
|
||||
return $durationOk && $ratioOk;
|
||||
});
|
||||
|
||||
// Limiter au nombre demandé
|
||||
return array_slice($shortVideos, 0, $count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les vidéos sur l'indépendance depuis l'API PeerTube
|
||||
*
|
||||
* @param int $count Nombre de vidéos à récupérer
|
||||
* @return array Liste des vidéos sur l'indépendance
|
||||
*/
|
||||
function getIndependenceVideos($count = INDEPENDENCE_VIDEOS_COUNT) {
|
||||
// Récupérer les vidéos sur l'indépendance
|
||||
return getVideosByTag(TAG_INDEPENDENCE, $count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie s'il y a un direct en cours du compte LIVE_ACCOUNT_NAME sur l'instance PeerTube
|
||||
*
|
||||
* @return array|null Informations sur le direct en cours ou null si aucun direct
|
||||
*/
|
||||
function getLiveStream() {
|
||||
// Récupérer les lives du compte spécifié
|
||||
$accountName = LIVE_ACCOUNT_NAME;
|
||||
$data = callPeerTubeApi('accounts/' . $accountName . '/videos', [
|
||||
'count' => 1,
|
||||
'isLocal' => true,
|
||||
'isLive' => true, // Filtrer uniquement les lives
|
||||
'sort' => '-publishedAt' // Les plus récents en premier
|
||||
]);
|
||||
|
||||
// Vérifier si on a des résultats
|
||||
if (empty($data['data']) || count($data['data']) === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Formater les données du live
|
||||
$liveData = formatVideosData($data['data']);
|
||||
|
||||
// Filtrer pour ne garder que les lives en cours
|
||||
$activeLives = array_filter($liveData, function($video) {
|
||||
return isset($video['isLive']) && $video['isLive'] === true;
|
||||
});
|
||||
|
||||
// Retourner le premier live trouvé
|
||||
return !empty($activeLives) ? reset($activeLives) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les vidéos d'une catégorie spécifique depuis l'API PeerTube
|
||||
*
|
||||
* @param int $categoryId Identifiant de la catégorie
|
||||
* @param int $count Nombre de vidéos à récupérer
|
||||
* @return array Liste des vidéos de la catégorie
|
||||
*/
|
||||
function getVideosByCategory($categoryId, $count = CATEGORY_VIDEOS_COUNT) {
|
||||
// Récupérer les vidéos par catégorie
|
||||
$data = callPeerTubeApi('videos', [
|
||||
'categoryOneOf' => $categoryId,
|
||||
'count' => $count,
|
||||
'sort' => '-publishedAt', // Les plus récentes d'abord
|
||||
'isLocal' => true
|
||||
]);
|
||||
|
||||
return formatVideosData($data['data'] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère la liste des catégories à afficher (triées selon les priorités)
|
||||
*
|
||||
* @return array Liste des catégories avec id, name et videos
|
||||
*/
|
||||
function getDisplayCategories() {
|
||||
$categories = [];
|
||||
$priorityCategories = PRIORITY_CATEGORIES;
|
||||
|
||||
// Ajouter uniquement les catégories prioritaires dans l'ordre défini
|
||||
foreach ($priorityCategories as $catId => $categoryName) {
|
||||
$videos = getVideosByCategory($catId);
|
||||
|
||||
// N'ajouter que les catégories qui ont des vidéos
|
||||
if (!empty($videos)) {
|
||||
$categories[] = [
|
||||
'id' => $catId,
|
||||
'name' => $categoryName,
|
||||
'videos' => $videos
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les commentaires d'une vidéo
|
||||
* @param string $videoId ID de la vidéo
|
||||
* @return array Tableau des commentaires
|
||||
*/
|
||||
function getVideoComments($videoId) {
|
||||
$endpoint = "videos/{$videoId}/comment-threads";
|
||||
$response = callPeerTubeApi($endpoint);
|
||||
|
||||
if (!$response || !isset($response['data'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $response['data'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les options de téléchargement pour une vidéo
|
||||
* @param string $videoId ID de la vidéo
|
||||
* @return array Options de téléchargement
|
||||
*/
|
||||
function getVideoDownloadOptions($videoId) {
|
||||
// Récupérer les informations complètes de la vidéo
|
||||
$videoData = callPeerTubeApi('videos/' . $videoId);
|
||||
|
||||
$downloadOptions = [];
|
||||
|
||||
// Ajouter les fichiers directs s'ils existent
|
||||
if (isset($videoData['files']) && !empty($videoData['files'])) {
|
||||
foreach ($videoData['files'] as $file) {
|
||||
if (isset($file['fileDownloadUrl']) && !empty($file['fileDownloadUrl'])) {
|
||||
$downloadOptions[] = [
|
||||
'type' => 'direct',
|
||||
'url' => PEERTUBE_URL . $file['fileDownloadUrl'],
|
||||
'resolution' => isset($file['resolution']['label']) ? $file['resolution']['label'] : 'Original',
|
||||
'size' => isset($file['size']) ? formatFileSize($file['size']) : 'Inconnu'
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ajouter les playlists de streaming s'ils existent
|
||||
if (isset($videoData['streamingPlaylists']) && !empty($videoData['streamingPlaylists'])) {
|
||||
foreach ($videoData['streamingPlaylists'] as $playlist) {
|
||||
if (isset($playlist['files']) && !empty($playlist['files'])) {
|
||||
foreach ($playlist['files'] as $file) {
|
||||
if (isset($file['fileDownloadUrl']) && !empty($file['fileDownloadUrl'])) {
|
||||
$downloadOptions[] = [
|
||||
'type' => 'hls',
|
||||
'url' => $file['fileDownloadUrl'],
|
||||
'resolution' => isset($file['resolution']['label']) ? $file['resolution']['label'] : 'Original',
|
||||
'size' => isset($file['size']) ? formatFileSize($file['size']) : 'Inconnu'
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $downloadOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recherche des vidéos selon un critère
|
||||
*
|
||||
* @param string $query Terme de recherche
|
||||
* @param int $count Nombre de vidéos à récupérer
|
||||
* @param int $start Index de départ pour la pagination
|
||||
* @param int|null $total Total réel renvoyé par l'API (passé par référence)
|
||||
* @return array Liste des vidéos correspondant à la recherche
|
||||
*/
|
||||
function searchVideos($query, $count = COUNT_VIDEO_SEARCH, $start = 0, &$total = null) {
|
||||
$total = 0;
|
||||
if (empty($query)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Vérifier si la recherche concerne un tag (commence par #)
|
||||
if (substr($query, 0, 1) === '#') {
|
||||
$tag = substr($query, 1); // Enlever le # du début
|
||||
|
||||
// Récupérer les vidéos avec ce tag via l'API
|
||||
$data = callPeerTubeApi('videos', [
|
||||
'tagsOneOf' => $tag,
|
||||
'count' => $count,
|
||||
'start' => $start,
|
||||
'isLocal' => true, // Uniquement les vidéos locales
|
||||
'sort' => '-publishedAt' // Les plus récentes d'abord
|
||||
]);
|
||||
|
||||
$videos = formatVideosData($data['data'] ?? []);
|
||||
$total = isset($data['total']) ? (int) $data['total'] : count($videos);
|
||||
return $videos;
|
||||
}
|
||||
|
||||
// Recherche normale (pas un tag)
|
||||
$data = callPeerTubeApi('search/videos', [
|
||||
'search' => $query,
|
||||
'count' => $count,
|
||||
'start' => $start,
|
||||
'isLocal' => true, // Uniquement les vidéos locales
|
||||
'sort' => '-publishedAt' // Les plus récentes d'abord
|
||||
]);
|
||||
|
||||
$videos = formatVideosData($data['data'] ?? []);
|
||||
$total = isset($data['total']) ? (int) $data['total'] : count($videos);
|
||||
return $videos;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
<!-- Menu mobile (masqué par défaut) -->
|
||||
<?php require_once __DIR__ . '/nav-context.php'; ?>
|
||||
<div class="mobile-menu">
|
||||
<button class="mobile-menu-close">
|
||||
<i class="fas fa-times"></i>
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* Contexte de navigation partagé par les partials (sidebar, footer, menu mobile).
|
||||
*
|
||||
* Définit les variables $currentPage, $currentCategoryId, $currentQuery,
|
||||
* $isTagSearch et $currentTag à partir de la requête courante. Chaque partial
|
||||
* l'inclut lui-même (require_once) : l'ordre d'inclusion des partials n'a
|
||||
* plus d'importance et chacun est autonome (ARC-3).
|
||||
*
|
||||
* Les variables déjà définies (par la page ou un partial précédent) ne sont
|
||||
* pas écrasées.
|
||||
*/
|
||||
|
||||
if (!isset($currentPage)) {
|
||||
$currentPage = basename($_SERVER['PHP_SELF']);
|
||||
}
|
||||
if (!isset($currentCategoryId)) {
|
||||
$currentCategoryId = isset($_GET['id']) ? intval($_GET['id']) : null;
|
||||
}
|
||||
if (!isset($currentQuery)) {
|
||||
$currentQuery = isset($_GET['q']) ? trim($_GET['q']) : '';
|
||||
}
|
||||
if (!isset($isTagSearch)) {
|
||||
$isTagSearch = !empty($currentQuery) && substr($currentQuery, 0, 1) === '#';
|
||||
}
|
||||
if (!isset($currentTag)) {
|
||||
$currentTag = $isTagSearch ? substr($currentQuery, 1) : '';
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
/**
|
||||
* Partial : annonce du prochain live (constantes NEXT_LIVE_*).
|
||||
*
|
||||
* Factorise le bloc « prochain live » affiché par direct.php et
|
||||
* includes/hero-section.php quand aucun direct n'est en cours (ARC-4).
|
||||
* Prérequis : n'inclure ce partial que si NEXT_LIVE_ENABLED === true.
|
||||
*
|
||||
* Variable d'entrée :
|
||||
* - $nextLiveVariant (string) : 'page' (direct.php, défaut) ou 'hero'
|
||||
* (hero-section) — adapte les classes CSS, le niveau de titre et le
|
||||
* lien « Retour à l'accueil ».
|
||||
*/
|
||||
|
||||
$nextLiveVariant = $nextLiveVariant ?? 'page';
|
||||
$isHeroVariant = $nextLiveVariant === 'hero';
|
||||
|
||||
// Classes CSS : le hero préfixe toutes ses classes par « hero-next-live »
|
||||
$rootClass = $isHeroVariant ? 'hero-next-live' : 'next-live-announcement';
|
||||
$imageContainerClass = $isHeroVariant ? 'hero-next-live-image-container' : 'next-live-image-container';
|
||||
$imageClass = $isHeroVariant ? 'hero-next-live-image' : 'next-live-image';
|
||||
$contentClass = $isHeroVariant ? 'hero-next-live-content' : 'next-live-content';
|
||||
$datetimeClass = $isHeroVariant ? 'hero-next-live-datetime' : 'next-live-datetime';
|
||||
$dateClass = $isHeroVariant ? 'hero-next-live-date' : 'next-live-date';
|
||||
$timezonesClass = $isHeroVariant ? 'hero-next-live-timezones' : 'next-live-timezones';
|
||||
$timezoneItemClass = $isHeroVariant ? 'hero-timezone-item' : 'timezone-item';
|
||||
$headingTag = $isHeroVariant ? 'h2' : 'h1';
|
||||
|
||||
// Définir l'image de fond si disponible
|
||||
$bgImageStyle = '';
|
||||
if (!empty(NEXT_LIVE_IMAGE) && file_exists(NEXT_LIVE_IMAGE)) {
|
||||
$bgImageStyle = 'background-image: url(\'' . htmlspecialchars(NEXT_LIVE_IMAGE) . '\');';
|
||||
}
|
||||
?>
|
||||
<?php if (!empty($bgImageStyle)): ?>
|
||||
<style nonce="<?php echo getCspNonce(); ?>">
|
||||
.<?php echo $rootClass; ?> { <?php echo $bgImageStyle; ?> }
|
||||
</style>
|
||||
<?php endif; ?>
|
||||
<div class="<?php echo $rootClass; ?>">
|
||||
<?php if (!empty(NEXT_LIVE_IMAGE) && file_exists(NEXT_LIVE_IMAGE)): ?>
|
||||
<div class="<?php echo $imageContainerClass; ?>">
|
||||
<img src="<?php echo htmlspecialchars(NEXT_LIVE_IMAGE); ?>"
|
||||
alt="<?php echo htmlspecialchars(NEXT_LIVE_TITLE); ?>"
|
||||
class="<?php echo $imageClass; ?>">
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<div class="<?php echo $contentClass; ?>">
|
||||
<i class="fas fa-calendar-alt"></i>
|
||||
<?php
|
||||
if (!empty(NEXT_LIVE_DATE)) {
|
||||
$liveDate = new DateTime(NEXT_LIVE_DATE, new DateTimeZone(DEFAULT_TIMEZONE));
|
||||
$dayFormatter = new IntlDateFormatter(
|
||||
'fr_FR',
|
||||
IntlDateFormatter::FULL,
|
||||
IntlDateFormatter::NONE,
|
||||
DEFAULT_TIMEZONE,
|
||||
IntlDateFormatter::GREGORIAN,
|
||||
'EEEE d MMMM'
|
||||
);
|
||||
$formattedDay = $dayFormatter->format($liveDate);
|
||||
$formattedDay = ucfirst($formattedDay);
|
||||
$dynamicTitle = NEXT_LIVE_TITLE . ' - ' . $formattedDay;
|
||||
} else {
|
||||
$dynamicTitle = NEXT_LIVE_TITLE;
|
||||
}
|
||||
?>
|
||||
<<?php echo $headingTag; ?>><?php echo htmlspecialchars($dynamicTitle); ?></<?php echo $headingTag; ?>>
|
||||
<?php
|
||||
if (!empty(NEXT_LIVE_DATE)) {
|
||||
$liveHour = $liveDate->format('H\hi');
|
||||
$dynamicDescription = 'Rejoignez-nous à ' . $liveHour . '. ' . NEXT_LIVE_DESCRIPTION;
|
||||
} else {
|
||||
$dynamicDescription = NEXT_LIVE_DESCRIPTION;
|
||||
}
|
||||
?>
|
||||
<p><?php echo nl2br(htmlspecialchars($dynamicDescription)); ?></p>
|
||||
<?php if (!empty(NEXT_LIVE_DATE)): ?>
|
||||
<div class="<?php echo $datetimeClass; ?>">
|
||||
<p class="<?php echo $dateClass; ?>">
|
||||
<i class="fas fa-clock"></i>
|
||||
<?php
|
||||
$formatter = new IntlDateFormatter(
|
||||
'fr_FR',
|
||||
IntlDateFormatter::FULL,
|
||||
IntlDateFormatter::SHORT,
|
||||
DEFAULT_TIMEZONE
|
||||
);
|
||||
echo $formatter->format($liveDate);
|
||||
|
||||
$offset = $liveDate->format('P');
|
||||
echo ' <span class="utc-offset">(UTC' . $offset . ')</span>';
|
||||
?>
|
||||
</p>
|
||||
|
||||
<!-- Autres fuseaux horaires -->
|
||||
<div class="<?php echo $timezonesClass; ?>">
|
||||
<?php
|
||||
// Ordre croissant : du plus en retard au plus en avance
|
||||
$timezones = [
|
||||
'Ma\'ohi Nui' => 'Pacific/Tahiti',
|
||||
'Martinique / Guadeloupe' => 'America/Martinique',
|
||||
'Guyane' => 'America/Cayenne',
|
||||
'France' => 'Europe/Paris',
|
||||
'Kanaky' => 'Pacific/Noumea'
|
||||
];
|
||||
|
||||
foreach($timezones as $name => $timezone):
|
||||
$liveDateLocal = clone $liveDate;
|
||||
$liveDateLocal->setTimezone(new DateTimeZone($timezone));
|
||||
|
||||
// Vérifier si c'est un jour différent
|
||||
$dayDiff = $liveDateLocal->format('j') - $liveDate->format('j');
|
||||
|
||||
$dayIndicator = '';
|
||||
if ($dayDiff > 0) {
|
||||
$dayIndicator = ' <span class="day-shift">+1j</span>';
|
||||
} elseif ($dayDiff < 0) {
|
||||
$dayIndicator = ' <span class="day-shift">-1j</span>';
|
||||
}
|
||||
?>
|
||||
<span class="<?php echo $timezoneItemClass; ?>">
|
||||
<strong><?php echo $name; ?> :</strong> <?php echo $liveDateLocal->format('H\hi'); ?><?php echo $dayIndicator; ?>
|
||||
</span>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if (!$isHeroVariant): ?>
|
||||
<a href="index.php" class="btn-primary">Retour à l'accueil</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1,18 +1,10 @@
|
||||
<!-- Sidebar de navigation -->
|
||||
<?php require_once __DIR__ . '/nav-context.php'; ?>
|
||||
<nav class="sidebar" role="navigation" aria-label="Navigation principale">
|
||||
<a href="/" class="logo" aria-label="Retour à l'accueil">
|
||||
<img src="img/logo.png" alt="Logo <?php echo SITE_NAME; ?>">
|
||||
</a>
|
||||
|
||||
<?php
|
||||
// Détecter la page courante et ses paramètres
|
||||
$currentPage = basename($_SERVER['PHP_SELF']);
|
||||
$currentCategoryId = isset($_GET['id']) ? intval($_GET['id']) : null;
|
||||
$currentQuery = isset($_GET['q']) ? trim($_GET['q']) : '';
|
||||
$isTagSearch = !empty($currentQuery) && substr($currentQuery, 0, 1) === '#';
|
||||
$currentTag = $isTagSearch ? substr($currentQuery, 1) : '';
|
||||
?>
|
||||
|
||||
<div class="sidebar-nav">
|
||||
<a href="/" class="nav-item <?php echo ($currentPage === 'index.php') ? 'active' : ''; ?>" data-title="Accueil" aria-current="<?php echo ($currentPage === 'index.php') ? 'page' : 'false'; ?>">
|
||||
<i class="fas fa-home" aria-hidden="true"></i> <span>Accueil</span>
|
||||
|
||||
Reference in New Issue
Block a user