mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-14 08:06:47 +02:00
69 lines
2.4 KiB
PHP
69 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
class MediaVisibilityMiddleware implements MiddlewareInterface
|
|
{
|
|
public function process(Request $request, RequestHandlerInterface $handler): Response
|
|
{
|
|
$path = $request->getUri()->getPath();
|
|
|
|
// Map routes to media types
|
|
$mediaRoutes = [
|
|
'/media/games' => 'games',
|
|
'/media/movies' => 'movies',
|
|
'/media/tv-shows' => 'tvshows',
|
|
'/media/music' => 'music',
|
|
'/media/adult' => 'adult',
|
|
'/media/actors' => 'actors'
|
|
];
|
|
|
|
foreach ($mediaRoutes as $route => $mediaType) {
|
|
if (strpos($path, $route) === 0) {
|
|
// Check if this media type is visible to the current user
|
|
if (!$this->isMediaTypeVisible($mediaType)) {
|
|
// Redirect to login or show 404 based on configuration
|
|
if (!is_logged_in()) {
|
|
return $handler->handle($request)->withStatus(401)->withHeader('Location', '/login');
|
|
} else {
|
|
return $handler->handle($request)->withStatus(404);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
return $handler->handle($request);
|
|
}
|
|
|
|
private function isMediaTypeVisible(string $mediaType): bool
|
|
{
|
|
// Get database connection
|
|
$pdo = \App\Database\Database::getInstance();
|
|
|
|
// Get media visibility setting
|
|
$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = :key LIMIT 1");
|
|
$stmt->execute(['key' => "media_visibility_{$mediaType}"]);
|
|
$visibility = $stmt->fetchColumn() ?: 'authenticated'; // Default to authenticated only
|
|
|
|
// Check user authentication status
|
|
$isLoggedIn = is_logged_in();
|
|
|
|
switch ($visibility) {
|
|
case 'public':
|
|
return true; // Visible to everyone
|
|
case 'authenticated':
|
|
return $isLoggedIn; // Visible only to authenticated users
|
|
case 'hidden':
|
|
return false; // Hidden from all users
|
|
default:
|
|
return $isLoggedIn; // Default to authenticated only
|
|
}
|
|
}
|
|
}
|