alltube/classes/Controller/FrontController.php

405 lines
13 KiB
PHP
Raw Normal View History

2015-10-29 21:32:36 +00:00
<?php
2015-10-31 14:50:32 +00:00
/**
2016-09-07 22:28:28 +00:00
* FrontController class.
2016-08-01 11:29:13 +00:00
*/
2016-12-05 12:12:27 +00:00
2015-10-29 21:32:36 +00:00
namespace Alltube\Controller;
2016-03-29 23:49:08 +00:00
use Alltube\Library\Exception\PasswordException;
use Alltube\Library\Exception\AlltubeLibraryException;
use Alltube\Library\Exception\WrongPasswordException;
2017-05-30 21:30:21 +00:00
use Alltube\Locale;
2020-10-20 21:13:48 +00:00
use Alltube\Middleware\CspMiddleware;
use Exception;
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidURLException;
use GrumPHP\Util\Str;
2020-07-05 09:22:55 +00:00
use Slim\Http\StatusCode;
use Slim\Http\Uri;
use stdClass;
2020-05-13 20:57:25 +00:00
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
2019-11-27 22:49:01 +00:00
use Throwable;
2017-04-25 20:24:44 +00:00
use Psr\Container\ContainerInterface;
2016-07-22 11:58:33 +00:00
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Smarty;
2016-03-29 23:49:08 +00:00
2015-10-31 14:50:32 +00:00
/**
2016-09-07 22:28:28 +00:00
* Main controller.
2016-08-01 11:29:13 +00:00
*/
class FrontController extends BaseController
2015-10-31 14:50:32 +00:00
{
2016-10-23 20:59:37 +00:00
/**
2016-10-23 21:08:32 +00:00
* Smarty view.
*
* @var Smarty
2016-10-23 20:59:37 +00:00
*/
private $view;
2016-08-01 11:29:13 +00:00
/**
* BaseController constructor.
2016-09-07 22:28:28 +00:00
*
* @param ContainerInterface $container Slim dependency container
2016-08-01 11:29:13 +00:00
*/
public function __construct(ContainerInterface $container)
2016-04-08 17:06:41 +00:00
{
parent::__construct($container);
$view = $this->container->get('view');
assert($view instanceof Smarty);
$this->view = $view;
// Plugins
$this->view->registerPlugin('modifier', 'filter_var', 'filter_var');
2016-04-08 17:06:41 +00:00
}
2015-10-29 21:32:36 +00:00
2015-10-31 14:50:32 +00:00
/**
2016-09-07 22:28:28 +00:00
* Display index page.
2016-02-28 22:04:53 +00:00
*
2020-05-13 20:28:05 +00:00
* @param Request $request PSR-7 request
2016-03-29 23:39:47 +00:00
* @param Response $response PSR-7 response
*
2017-01-16 16:47:26 +00:00
* @return Response HTTP response
2015-10-31 14:50:32 +00:00
*/
2020-12-17 21:43:05 +00:00
public function index(Request $request, Response $response): Response
2015-10-31 14:50:32 +00:00
{
2016-10-23 20:59:37 +00:00
$this->view->render(
$response,
'index.tpl',
[
2020-05-13 20:28:05 +00:00
'class' => 'index',
'description' => $this->localeManager->t(
'Easily download videos from YouTube, Dailymotion, Vimeo and other websites.'
),
'supportedLocales' => $this->localeManager->getSupportedLocales(),
2016-10-23 20:59:37 +00:00
]
);
2017-01-16 16:31:20 +00:00
2017-01-16 16:19:19 +00:00
return $response;
2015-10-29 21:32:36 +00:00
}
2017-05-30 20:20:16 +00:00
/**
* Switch locale.
*
2020-05-13 20:28:05 +00:00
* @param Request $request PSR-7 request
2017-05-30 20:20:16 +00:00
* @param Response $response PSR-7 response
2020-05-13 20:28:05 +00:00
* @param string[] $data Query parameters
2017-05-30 20:20:16 +00:00
*
* @return Response
*/
2020-12-17 21:43:05 +00:00
public function locale(Request $request, Response $response, array $data): Response
2017-05-30 20:20:16 +00:00
{
2017-05-30 21:49:38 +00:00
$this->localeManager->setLocale(new Locale($data['locale']));
2017-05-30 20:20:16 +00:00
2020-10-21 23:00:52 +00:00
return $response->withRedirect($this->router->pathFor('index'));
2017-05-30 20:20:16 +00:00
}
2015-10-31 14:50:32 +00:00
/**
2016-09-07 22:28:28 +00:00
* Display a list of extractors.
2016-02-28 22:04:53 +00:00
*
2020-05-13 19:18:32 +00:00
* @param Request $request PSR-7 request
2016-03-29 23:39:47 +00:00
* @param Response $response PSR-7 response
*
2017-01-16 16:47:26 +00:00
* @return Response HTTP response
* @throws AlltubeLibraryException
2015-10-31 14:50:32 +00:00
*/
2020-12-17 21:43:05 +00:00
public function extractors(Request $request, Response $response): Response
2015-10-31 14:50:32 +00:00
{
2016-10-23 20:59:37 +00:00
$this->view->render(
$response,
'extractors.tpl',
[
'extractors' => $this->downloader->getExtractors(),
2020-05-13 20:28:05 +00:00
'class' => 'extractors',
'title' => $this->localeManager->t('Supported websites'),
'description' => $this->localeManager->t('List of all supported websites from which AllTube Download ' .
'can extract video or audio files'),
2016-10-23 20:59:37 +00:00
]
);
2017-01-16 16:31:20 +00:00
2017-01-16 16:19:19 +00:00
return $response;
2015-10-29 21:32:36 +00:00
}
/**
2016-10-20 21:03:13 +00:00
* Display a password prompt.
*
2020-05-13 20:28:05 +00:00
* @param Request $request PSR-7 request
2016-10-20 21:03:13 +00:00
* @param Response $response PSR-7 response
*
* @return Response HTTP response
*/
2020-12-17 21:43:05 +00:00
public function password(Request $request, Response $response): Response
{
2016-10-23 20:59:37 +00:00
$this->view->render(
$response,
'password.tpl',
[
2020-05-13 20:28:05 +00:00
'class' => 'password',
'title' => $this->localeManager->t('Password prompt'),
'description' => $this->localeManager->t(
'You need a password in order to download this video with AllTube Download'
),
2016-10-23 20:59:37 +00:00
]
);
2017-01-16 16:31:20 +00:00
2020-07-05 09:22:55 +00:00
return $response->withStatus(StatusCode::HTTP_FORBIDDEN);
2015-10-29 21:32:36 +00:00
}
2017-01-16 12:43:47 +00:00
/**
2017-01-16 16:31:20 +00:00
* Return the video description page.
*
2020-05-13 20:28:05 +00:00
* @param Request $request PSR-7 request
2017-01-16 16:31:20 +00:00
* @param Response $response PSR-7 response
*
2017-01-16 16:47:26 +00:00
* @return Response HTTP response
* @throws AlltubeLibraryException
2017-01-16 12:43:47 +00:00
*/
2022-02-03 19:21:25 +00:00
private function getInfoResponse(Request $request, Response $response): Response
2017-01-16 12:43:47 +00:00
{
try {
$this->video->getJson();
2017-01-16 12:43:47 +00:00
} catch (PasswordException $e) {
return $this->password($request, $response);
} catch (WrongPasswordException $e) {
return $this->displayError($request, $response, $this->localeManager->t('Wrong password'));
2017-01-16 12:43:47 +00:00
}
if (isset($this->video->entries)) {
2017-04-24 23:53:38 +00:00
$template = 'playlist.tpl';
} else {
$template = 'info.tpl';
2017-04-24 23:53:38 +00:00
}
$title = $this->localeManager->t('Video download');
$description = $this->localeManager->t(
'Download video from @extractor',
['@extractor' => $this->video->extractor_key]
);
if (isset($this->video->title)) {
$title = $this->video->title;
$description = $this->localeManager->t(
'Download @title from @extractor',
[
'@title' => $this->video->title,
'@extractor' => $this->video->extractor_key
]
);
2017-04-25 09:05:49 +00:00
}
$formats = [];
$genericFormatsLabel = $this->localeManager->t('Generic formats');
$detailedFormatsLabel = $this->localeManager->t('Detailed formats');
foreach ($this->config->genericFormats as $id => $genericFormat) {
$formats[$genericFormatsLabel][$id] = $this->localeManager->t($genericFormat);
}
2022-03-06 21:54:54 +00:00
$json = $this->video->getJson();
if (isset($json->formats)) {
/** @var stdClass $format */
foreach ($json->formats as $format) {
if ($this->config->stream || in_array($format->protocol, ['http', 'https'])) {
$formatParts = [
// File extension
$format->ext,
];
if (isset($format->width) || isset($format->height)) {
// Video dimensions
$formatParts[] = implode('x', array_filter([$format->width, $format->height]));
}
if (isset($format->filesize)) {
// File size
$formatParts[] = round($format->filesize / 1000000, 2) . ' MB';
}
if (isset($format->format_note)) {
// Format name
$formatParts[] = $format->format_note;
}
if (isset($format->format_id)) {
// Format ID
$formatParts[] = '(' . $format->format_id . ')';
}
$formats[$detailedFormatsLabel][$format->format_id] = implode(' ', $formatParts);
}
}
}
/* Fetch the thumbnail, if it exists, and add a data URI to the video object */
if (isset($this->video->thumbnail) && $this->video->thumbnail !== '') {
/* Fetch the thumbnail */
$thumbnailData = file_get_contents($this->video->thumbnail);
if ($thumbnailData !== false) {
$thumbnailData = base64_encode($thumbnailData);
/* Guess the mime type */
$thumbnailMime = 'image/jpeg';
if (strpos($this->video->thumbnail, '.png') !== false) {
$thumbnailMime = 'image/png';
} elseif (strpos($this->video->thumbnail, '.gif') !== false) {
$thumbnailMime = 'image/gif';
} elseif (strpos($this->video->thumbnail, '.webp') !== false) {
$thumbnailMime = 'image/webp';
}
$this->video->thumbnail = 'data:' . $thumbnailMime . ';base64,' . $thumbnailData;
}
}
2017-01-16 12:43:47 +00:00
$this->view->render(
$response,
2017-04-24 23:53:38 +00:00
$template,
2017-01-16 12:43:47 +00:00
[
2020-05-13 20:28:05 +00:00
'video' => $this->video,
'class' => 'info',
'title' => $title,
'description' => $description,
'defaultFormat' => $this->defaultFormat,
'formats' => $formats
2017-01-16 12:43:47 +00:00
]
);
2017-01-16 16:31:20 +00:00
2017-01-16 16:19:19 +00:00
return $response;
2017-01-16 12:43:47 +00:00
}
2015-10-31 14:50:32 +00:00
/**
2016-09-07 22:28:28 +00:00
* Dislay information about the video.
2016-02-28 22:04:53 +00:00
*
2020-05-13 20:28:05 +00:00
* @param Request $request PSR-7 request
2016-03-29 23:39:47 +00:00
* @param Response $response PSR-7 response
*
2016-08-01 11:29:13 +00:00
* @return Response HTTP response
* @throws AlltubeLibraryException
* @throws InvalidURLException
2015-10-31 14:50:32 +00:00
*/
2020-12-17 21:43:05 +00:00
public function info(Request $request, Response $response): Response
2015-10-31 14:50:32 +00:00
{
$url = $this->getVideoPageUrl($request);
$this->video = $this->downloader->getVideo($url, $this->getFormat($request), $this->getPassword($request));
if ($this->config->convert && $request->getQueryParam('audio')) {
// We skip the info page and get directly to the download.
return $response->withRedirect(
$this->router->pathFor('download', [], $request->getQueryParams())
);
2016-06-09 19:15:44 +00:00
} else {
return $this->getInfoResponse($request, $response);
2015-10-29 21:32:36 +00:00
}
2016-04-30 23:25:08 +00:00
}
/**
* Display an user-friendly error.
*
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
* @param string $message Error message
*
* @return Response HTTP response
*/
2020-12-17 21:43:05 +00:00
protected function displayError(Request $request, Response $response, string $message): Response
{
$this->view->render(
$response,
'error.tpl',
[
'error' => $message,
'class' => 'video',
'title' => $this->localeManager->t('Error'),
]
);
2020-07-05 09:22:55 +00:00
return $response->withStatus(StatusCode::HTTP_INTERNAL_SERVER_ERROR);
}
2020-07-15 21:17:23 +00:00
/**
* @param Request $request
* @param Response $response
* @return Response
*/
2020-12-17 21:43:05 +00:00
public function notFound(Request $request, Response $response): Response
2020-07-15 21:17:23 +00:00
{
return $this->displayError($request, $response, $this->localeManager->t('Page not found'))
->withStatus(StatusCode::HTTP_NOT_FOUND);
}
/**
* @param Request $request
* @param Response $response
* @return Response
*/
2020-12-17 21:43:05 +00:00
public function notAllowed(Request $request, Response $response): Response
2020-07-15 21:17:23 +00:00
{
return $this->displayError($request, $response, $this->localeManager->t('Method not allowed'))
->withStatus(StatusCode::HTTP_METHOD_NOT_ALLOWED);
}
2016-08-01 11:29:13 +00:00
/**
2016-09-07 22:28:28 +00:00
* Display an error page.
*
2020-05-13 20:28:05 +00:00
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
* @param Throwable $error Error to display
2016-09-07 22:28:28 +00:00
*
2016-08-01 11:29:13 +00:00
* @return Response HTTP response
*/
2020-12-17 21:43:05 +00:00
public function error(Request $request, Response $response, Throwable $error): Response
2016-04-30 23:25:08 +00:00
{
2020-10-18 10:49:44 +00:00
$this->logger->error($error);
// We apply the CSP manually because middlewares are not called on error pages.
$cspMiddleware = new CspMiddleware($this->container);
/** @var Response $response */
$response = $cspMiddleware->applyHeader($response);
2019-11-27 20:22:23 +00:00
if ($this->config->debug) {
$projectDir = $this->container->get('root_path');
assert(is_string($projectDir) || is_null($projectDir));
$renderer = new HtmlErrorRenderer(true, null, null, $projectDir);
2020-05-13 20:57:25 +00:00
$exception = $renderer->render($error);
2020-05-13 20:57:25 +00:00
$response->getBody()->write($exception->getAsString());
foreach ($exception->getHeaders() as $header => $value) {
$response = $response->withHeader($header, $value);
}
2015-10-31 10:48:14 +00:00
2019-12-02 21:04:14 +00:00
return $response->withStatus($exception->getStatusCode());
2019-11-27 22:49:01 +00:00
} else {
2019-12-02 21:04:14 +00:00
if ($error instanceof Exception) {
$message = $error->getMessage();
} else {
$message = '';
}
return $this->displayError($request, $response, $message);
2019-12-02 21:04:14 +00:00
}
2019-11-27 22:49:01 +00:00
}
/**
* Route that mimics YouTube video URLs ("/watch?v=foo")
*
* @param Request $request
* @param Response $response
* @return Response
*/
public function watch(Request $request, Response $response): Response
{
// We build a full YouTube URL from the video ID.
$youtubeUri = Uri::createFromString('https://www.youtube.com/watch')
->withQuery(http_build_query(['v' => $request->getQueryParam('v')]));
// Then pass it to the info route.
return $response->withRedirect(
Uri::createFromString($this->router->pathFor('info'))
->withQuery(http_build_query(['url' => strval($youtubeUri)]))
);
}
2015-10-29 21:32:36 +00:00
}