111 lines
3.3 KiB
PHP
111 lines
3.3 KiB
PHP
<?php
|
||
|
||
/*
|
||
* Copyright (c) 2025, Антон Аксенов
|
||
* This file is part of m3u.su project
|
||
* MIT License: https://git.axenov.dev/IPTV/web/src/branch/master/LICENSE
|
||
*/
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Controllers;
|
||
|
||
use App\Errors\PlaylistNotFoundException;
|
||
use Exception;
|
||
use Psr\Http\Message\ResponseInterface;
|
||
use Psr\Http\Message\ServerRequestInterface;
|
||
use Throwable;
|
||
use Twig\Error\LoaderError;
|
||
use Twig\Error\RuntimeError;
|
||
use Twig\Error\SyntaxError;
|
||
|
||
/**
|
||
* Контроллер маршрутов web
|
||
*/
|
||
class WebController extends BasicController
|
||
{
|
||
/**
|
||
* Возвращает главную страницу со списком плейлистов
|
||
*
|
||
* @param ServerRequestInterface $request
|
||
* @param ResponseInterface $response
|
||
* @return ResponseInterface
|
||
* @throws LoaderError
|
||
* @throws RuntimeError
|
||
* @throws SyntaxError
|
||
* @throws Exception
|
||
*/
|
||
public function home(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||
{
|
||
$ini = ini()->load();
|
||
$keys = [];
|
||
$count = count($ini);
|
||
$pageSize = config('app.page_size');
|
||
|
||
if ($pageSize > 0) {
|
||
$pageCurrent = (int) ($request->getAttributes()['page'] ?? $request->getQueryParams()['page'] ?? 1);
|
||
$pageCount = ceil($count / $pageSize);
|
||
$offset = max(0, ($pageCurrent - 1) * $pageSize);
|
||
$ini = array_slice($ini, $offset, $pageSize, true);
|
||
$keys = array_keys($ini);
|
||
}
|
||
|
||
$playlists = ini()->getPlaylists($keys);
|
||
|
||
return $this->view($request, $response, 'list.twig', [
|
||
'updatedAt' => ini()->updatedAt(),
|
||
'playlists' => $playlists,
|
||
'count' => $count,
|
||
'pageCount' => $pageCount ?? 1,
|
||
'pageCurrent' => $pageCurrent ?? 1,
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* Переадресует запрос на прямую ссылку плейлиста
|
||
*
|
||
* @param ServerRequestInterface $request
|
||
* @param ResponseInterface $response
|
||
* @return ResponseInterface
|
||
* @throws LoaderError
|
||
* @throws RuntimeError
|
||
* @throws SyntaxError
|
||
*/
|
||
public function redirect(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||
{
|
||
$code = $request->getAttributes()['code'];
|
||
|
||
try {
|
||
$playlist = ini()->getPlaylist($code);
|
||
|
||
return $response->withHeader('Location', $playlist['url']);
|
||
} catch (Throwable) {
|
||
return $this->notFound($request, $response);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Возвращает страницу с описанием плейлиста
|
||
*
|
||
* @param ServerRequestInterface $request
|
||
* @param ResponseInterface $response
|
||
* @return ResponseInterface
|
||
* @throws LoaderError
|
||
* @throws RuntimeError
|
||
* @throws SyntaxError
|
||
* @throws Exception
|
||
*/
|
||
public function details(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||
{
|
||
$code = $request->getAttributes()['code'];
|
||
|
||
try {
|
||
$playlist = ini()->getPlaylist($code);
|
||
|
||
return $this->view($request, $response, 'details.twig', ['playlist' => $playlist]);
|
||
} catch (PlaylistNotFoundException) {
|
||
return $this->notFound($request, $response);
|
||
}
|
||
}
|
||
}
|