132 lines
4.6 KiB
PHP
132 lines
4.6 KiB
PHP
<?php
|
|
// Démarrer la session avant tout
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
// Inclure la configuration
|
|
require_once '../includes/config.php';
|
|
|
|
// Vérifier que la requête est faite par AJAX
|
|
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') {
|
|
http_response_code(403); // Accès non autorisé
|
|
echo json_encode(['error' => 'Accès non autorisé']);
|
|
exit;
|
|
}
|
|
|
|
// Vérifier l'origine de la requête
|
|
if (!validateAjaxOrigin()) {
|
|
http_response_code(403);
|
|
echo json_encode(['error' => 'Origine non autorisée']);
|
|
exit;
|
|
}
|
|
|
|
// Vérifier le token CSRF
|
|
if (!isset($_POST['csrf_token']) || !validateCSRFToken($_POST['csrf_token'])) {
|
|
http_response_code(403);
|
|
echo json_encode(['error' => 'Token CSRF invalide']);
|
|
exit;
|
|
}
|
|
|
|
// Récupérer les paramètres
|
|
$type = isset($_GET['type']) ? $_GET['type'] : '';
|
|
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
|
|
$categoryId = isset($_GET['category']) ? intval($_GET['category']) : 0;
|
|
|
|
// Vérifier que le type est valide
|
|
if (!in_array($type, ['recent', 'trending', 'independence', 'category'])) {
|
|
http_response_code(400); // Requête incorrecte
|
|
echo json_encode(['error' => 'Type de vidéos non valide']);
|
|
exit;
|
|
}
|
|
|
|
// Vérifier que la catégorie est fournie si le type est 'category'
|
|
if ($type === 'category' && $categoryId <= 0) {
|
|
http_response_code(400); // Requête incorrecte
|
|
echo json_encode(['error' => 'ID de catégorie manquant ou invalide']);
|
|
exit;
|
|
}
|
|
|
|
// Récupérer les vidéos en fonction du type
|
|
$videos = [];
|
|
// Utiliser offset directement s'il est fourni, sinon calculer à partir de page
|
|
$offset = isset($_GET['offset']) ? intval($_GET['offset']) : $page * LOAD_MORE_COUNT;
|
|
|
|
switch ($type) {
|
|
case 'recent':
|
|
// Récupérer les vidéos récentes
|
|
$data = callPeerTubeApi('videos', [
|
|
'sort' => '-publishedAt',
|
|
'count' => LOAD_MORE_COUNT,
|
|
'start' => $offset,
|
|
'isLocal' => true
|
|
]);
|
|
$videos = formatVideosData($data['data'] ?? []);
|
|
break;
|
|
|
|
case 'trending':
|
|
// Récupérer les vidéos tendances
|
|
$data = callPeerTubeApi('videos', [
|
|
'sort' => '-views',
|
|
'count' => LOAD_MORE_COUNT,
|
|
'start' => $offset,
|
|
'isLocal' => true
|
|
]);
|
|
$videos = formatVideosData($data['data'] ?? []);
|
|
break;
|
|
|
|
case 'independence':
|
|
// Récupérer les vidéos sur l'indépendance
|
|
$data = callPeerTubeApi('videos', [
|
|
'tagsOneOf' => TAG_INDEPENDENCE,
|
|
'count' => LOAD_MORE_COUNT,
|
|
'start' => $offset,
|
|
'isLocal' => true
|
|
]);
|
|
$videos = formatVideosData($data['data'] ?? []);
|
|
break;
|
|
|
|
case 'category':
|
|
// Récupérer les vidéos de la catégorie
|
|
$data = callPeerTubeApi('videos', [
|
|
'categoryOneOf' => $categoryId,
|
|
'count' => LOAD_MORE_COUNT,
|
|
'start' => $offset,
|
|
'sort' => '-publishedAt', // Les plus récentes d'abord
|
|
'isLocal' => true
|
|
]);
|
|
$videos = formatVideosData($data['data'] ?? []);
|
|
break;
|
|
}
|
|
|
|
// Préparer la réponse HTML
|
|
$html = '';
|
|
|
|
foreach ($videos as $video) {
|
|
$html .= '<div class="video-card" data-video-id="' . htmlspecialchars($video['id']) . '">';
|
|
$html .= ' <div class="video-thumbnail">';
|
|
$html .= ' <img src="' . htmlspecialchars($video['thumbnail']) . '" alt="' . htmlspecialchars($video['title']) . '">';
|
|
$html .= ' <div class="video-play-icon">';
|
|
$html .= ' <i class="fas fa-play-circle"></i>';
|
|
$html .= ' </div>';
|
|
$html .= ' <div class="video-duration">' . formatDuration($video['duration']) . '</div>';
|
|
$html .= ' </div>';
|
|
$html .= ' <div class="video-info">';
|
|
$html .= ' <h3 class="video-title">' . htmlspecialchars($video['title']) . '</h3>';
|
|
$html .= ' <div class="video-channel">' . htmlspecialchars($video['channel']) . '</div>';
|
|
$html .= ' <div class="video-metadata">';
|
|
$html .= ' <span class="video-views"><i class="fas fa-eye"></i> ' . formatViewCount($video['views']) . ' vues</span>';
|
|
$html .= ' <span class="video-date"><i class="far fa-calendar-alt"></i> ' . formatDate($video['date']) . '</span>';
|
|
$html .= ' </div>';
|
|
$html .= ' </div>';
|
|
$html .= '</div>';
|
|
}
|
|
|
|
// Retourner la réponse
|
|
echo json_encode([
|
|
'success' => true,
|
|
'html' => $html,
|
|
'page' => $page,
|
|
'hasMore' => count($videos) >= LOAD_MORE_COUNT
|
|
]);
|
|
?>
|