2015-10-29 22:32:36 +01:00
|
|
|
<?php
|
2019-10-03 21:24:12 +02:00
|
|
|
|
2015-10-31 15:50:32 +01:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* FrontController class.
|
2016-08-01 13:29:13 +02:00
|
|
|
*/
|
2016-12-05 13:12:27 +01:00
|
|
|
|
2015-10-29 22:32:36 +01:00
|
|
|
namespace Alltube\Controller;
|
2016-03-30 01:49:08 +02:00
|
|
|
|
2020-06-21 01:44:20 +02:00
|
|
|
use Alltube\Library\Exception\PasswordException;
|
|
|
|
use Alltube\Library\Exception\AlltubeLibraryException;
|
|
|
|
use Alltube\Library\Exception\WrongPasswordException;
|
2017-05-30 23:30:21 +02:00
|
|
|
use Alltube\Locale;
|
2020-10-20 23:13:48 +02:00
|
|
|
use Alltube\Middleware\CspMiddleware;
|
2020-06-21 01:44:20 +02:00
|
|
|
use Exception;
|
2020-07-05 11:22:55 +02:00
|
|
|
use Slim\Http\StatusCode;
|
2020-05-13 22:57:25 +02:00
|
|
|
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
|
2019-11-27 23:49:01 +01:00
|
|
|
use Throwable;
|
2017-04-25 22:24:44 +02:00
|
|
|
use Psr\Container\ContainerInterface;
|
2016-07-22 13:58:33 +02:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
2018-02-05 16:48:58 +01:00
|
|
|
use Slim\Views\Smarty;
|
2016-03-30 01:49:08 +02:00
|
|
|
|
2015-10-31 15:50:32 +01:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Main controller.
|
2016-08-01 13:29:13 +02:00
|
|
|
*/
|
2019-04-22 16:05:58 +02:00
|
|
|
class FrontController extends BaseController
|
2015-10-31 15:50:32 +01:00
|
|
|
{
|
2016-10-23 22:59:37 +02:00
|
|
|
/**
|
2016-10-23 21:08:32 +00:00
|
|
|
* Smarty view.
|
|
|
|
*
|
2018-02-05 16:48:58 +01:00
|
|
|
* @var Smarty
|
2016-10-23 22:59:37 +02:00
|
|
|
*/
|
|
|
|
private $view;
|
|
|
|
|
2016-08-01 13:29:13 +02:00
|
|
|
/**
|
2019-04-22 16:05:58 +02:00
|
|
|
* BaseController constructor.
|
2016-09-07 22:28:28 +00:00
|
|
|
*
|
2019-03-30 19:13:48 +01:00
|
|
|
* @param ContainerInterface $container Slim dependency container
|
2016-08-01 13:29:13 +02:00
|
|
|
*/
|
2019-04-22 17:00:51 +02:00
|
|
|
public function __construct(ContainerInterface $container)
|
2016-04-08 19:06:41 +02:00
|
|
|
{
|
2019-04-22 17:00:51 +02:00
|
|
|
parent::__construct($container);
|
2019-04-22 16:05:58 +02:00
|
|
|
|
|
|
|
$this->view = $this->container->get('view');
|
2016-04-08 19:06:41 +02:00
|
|
|
}
|
2015-10-29 22:32:36 +01:00
|
|
|
|
2015-10-31 15:50:32 +01:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Display index page.
|
2016-02-28 23:04:53 +01:00
|
|
|
*
|
2020-05-13 22:28:05 +02:00
|
|
|
* @param Request $request PSR-7 request
|
2016-03-30 01:39:47 +02:00
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
2017-01-16 17:47:26 +01:00
|
|
|
* @return Response HTTP response
|
2015-10-31 15:50:32 +01:00
|
|
|
*/
|
2020-12-17 22:43:05 +01:00
|
|
|
public function index(Request $request, Response $response): Response
|
2015-10-31 15:50:32 +01:00
|
|
|
{
|
2016-10-23 22:59:37 +02:00
|
|
|
$this->view->render(
|
|
|
|
$response,
|
|
|
|
'index.tpl',
|
|
|
|
[
|
2020-05-13 22:28:05 +02:00
|
|
|
'class' => 'index',
|
|
|
|
'description' => $this->localeManager->t(
|
2020-12-05 15:00:46 +01:00
|
|
|
'Easily download videos from YouTube, Dailymotion, Vimeo and other websites.'
|
2019-11-27 23:15:49 +01:00
|
|
|
),
|
2017-05-31 09:32:11 +02:00
|
|
|
'supportedLocales' => $this->localeManager->getSupportedLocales(),
|
2016-10-23 22:59:37 +02:00
|
|
|
]
|
|
|
|
);
|
2017-01-16 16:31:20 +00:00
|
|
|
|
2017-01-16 17:19:19 +01:00
|
|
|
return $response;
|
2015-10-29 22:32:36 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 22:20:16 +02:00
|
|
|
/**
|
|
|
|
* Switch locale.
|
|
|
|
*
|
2020-05-13 22:28:05 +02:00
|
|
|
* @param Request $request PSR-7 request
|
2017-05-30 22:20:16 +02:00
|
|
|
* @param Response $response PSR-7 response
|
2020-05-13 22:28:05 +02:00
|
|
|
* @param string[] $data Query parameters
|
2017-05-30 22:20:16 +02:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2020-12-17 22:43:05 +01:00
|
|
|
public function locale(Request $request, Response $response, array $data): Response
|
2017-05-30 22:20:16 +02:00
|
|
|
{
|
2017-05-30 23:49:38 +02:00
|
|
|
$this->localeManager->setLocale(new Locale($data['locale']));
|
2017-05-30 22:20:16 +02:00
|
|
|
|
2020-10-22 01:00:52 +02:00
|
|
|
return $response->withRedirect($this->router->pathFor('index'));
|
2017-05-30 22:20:16 +02:00
|
|
|
}
|
|
|
|
|
2015-10-31 15:50:32 +01:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Display a list of extractors.
|
2016-02-28 23:04:53 +01:00
|
|
|
*
|
2020-05-13 21:18:32 +02:00
|
|
|
* @param Request $request PSR-7 request
|
2016-03-30 01:39:47 +02:00
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
2017-01-16 17:47:26 +01:00
|
|
|
* @return Response HTTP response
|
2020-06-21 01:44:20 +02:00
|
|
|
* @throws AlltubeLibraryException
|
2015-10-31 15:50:32 +01:00
|
|
|
*/
|
2020-12-17 22:43:05 +01:00
|
|
|
public function extractors(Request $request, Response $response): Response
|
2015-10-31 15:50:32 +01:00
|
|
|
{
|
2016-10-23 22:59:37 +02:00
|
|
|
$this->view->render(
|
|
|
|
$response,
|
|
|
|
'extractors.tpl',
|
|
|
|
[
|
2020-06-21 01:44:20 +02:00
|
|
|
'extractors' => $this->downloader->getExtractors(),
|
2020-05-13 22:28:05 +02:00
|
|
|
'class' => 'extractors',
|
|
|
|
'title' => $this->localeManager->t('Supported websites'),
|
2020-12-05 15:00:46 +01:00
|
|
|
'description' => $this->localeManager->t('List of all supported websites from which AllTube Download ' .
|
2018-01-26 11:37:43 +01:00
|
|
|
'can extract video or audio files'),
|
2016-10-23 22:59:37 +02:00
|
|
|
]
|
|
|
|
);
|
2017-01-16 16:31:20 +00:00
|
|
|
|
2017-01-16 17:19:19 +01:00
|
|
|
return $response;
|
2015-10-29 22:32:36 +01:00
|
|
|
}
|
|
|
|
|
2016-10-20 23:01:31 +02:00
|
|
|
/**
|
2016-10-20 21:03:13 +00:00
|
|
|
* Display a password prompt.
|
|
|
|
*
|
2020-05-13 22:28:05 +02:00
|
|
|
* @param Request $request PSR-7 request
|
2016-10-20 21:03:13 +00:00
|
|
|
* @param Response $response PSR-7 response
|
2016-10-20 23:01:31 +02:00
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
|
|
|
*/
|
2020-12-17 22:43:05 +01:00
|
|
|
public function password(Request $request, Response $response): Response
|
2016-10-20 23:01:31 +02:00
|
|
|
{
|
2016-10-23 22:59:37 +02:00
|
|
|
$this->view->render(
|
|
|
|
$response,
|
|
|
|
'password.tpl',
|
|
|
|
[
|
2020-05-13 22:28:05 +02:00
|
|
|
'class' => 'password',
|
|
|
|
'title' => $this->localeManager->t('Password prompt'),
|
2019-11-27 23:15:49 +01:00
|
|
|
'description' => $this->localeManager->t(
|
2020-12-05 15:00:46 +01:00
|
|
|
'You need a password in order to download this video with AllTube Download'
|
2019-11-27 23:15:49 +01:00
|
|
|
),
|
2016-10-23 22:59:37 +02:00
|
|
|
]
|
|
|
|
);
|
2017-01-16 16:31:20 +00:00
|
|
|
|
2020-07-05 11:22:55 +02:00
|
|
|
return $response->withStatus(StatusCode::HTTP_FORBIDDEN);
|
2015-10-29 22:32:36 +01:00
|
|
|
}
|
|
|
|
|
2017-01-16 13:43:47 +01:00
|
|
|
/**
|
2017-01-16 16:31:20 +00:00
|
|
|
* Return the video description page.
|
|
|
|
*
|
2020-05-13 22:28:05 +02:00
|
|
|
* @param Request $request PSR-7 request
|
2017-01-16 16:31:20 +00:00
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
2017-01-16 17:47:26 +01:00
|
|
|
* @return Response HTTP response
|
2020-06-21 01:44:20 +02:00
|
|
|
* @throws AlltubeLibraryException
|
2017-01-16 13:43:47 +01:00
|
|
|
*/
|
2019-04-22 15:20:05 +02:00
|
|
|
private function getInfoResponse(Request $request, Response $response)
|
2017-01-16 13:43:47 +01:00
|
|
|
{
|
|
|
|
try {
|
2019-04-21 18:30:02 +02:00
|
|
|
$this->video->getJson();
|
2017-01-16 13:43:47 +01:00
|
|
|
} catch (PasswordException $e) {
|
|
|
|
return $this->password($request, $response);
|
2020-06-21 01:44:20 +02:00
|
|
|
} catch (WrongPasswordException $e) {
|
|
|
|
return $this->displayError($request, $response, $this->localeManager->t('Wrong password'));
|
2017-01-16 13:43:47 +01:00
|
|
|
}
|
2019-03-24 15:13:01 +01:00
|
|
|
|
2019-04-21 18:30:02 +02:00
|
|
|
if (isset($this->video->entries)) {
|
2017-04-25 01:53:38 +02:00
|
|
|
$template = 'playlist.tpl';
|
|
|
|
} else {
|
2019-04-22 15:20:05 +02:00
|
|
|
$template = 'info.tpl';
|
2017-04-25 01:53:38 +02:00
|
|
|
}
|
2019-11-27 23:15:49 +01:00
|
|
|
$title = $this->localeManager->t('Video download');
|
2019-11-29 21:33:49 +01:00
|
|
|
$description = $this->localeManager->t(
|
|
|
|
'Download video from @extractor',
|
|
|
|
['@extractor' => $this->video->extractor_key]
|
|
|
|
);
|
2019-04-21 18:30:02 +02:00
|
|
|
if (isset($this->video->title)) {
|
|
|
|
$title = $this->video->title;
|
2019-11-29 21:33:49 +01:00
|
|
|
$description = $this->localeManager->t(
|
|
|
|
'Download @title from @extractor',
|
|
|
|
[
|
|
|
|
'@title' => $this->video->title,
|
|
|
|
'@extractor' => $this->video->extractor_key
|
|
|
|
]
|
|
|
|
);
|
2017-04-25 11:05:49 +02:00
|
|
|
}
|
2017-01-16 13:43:47 +01:00
|
|
|
$this->view->render(
|
|
|
|
$response,
|
2017-04-25 01:53:38 +02:00
|
|
|
$template,
|
2017-01-16 13:43:47 +01:00
|
|
|
[
|
2020-05-13 22:28:05 +02:00
|
|
|
'video' => $this->video,
|
|
|
|
'class' => 'info',
|
|
|
|
'title' => $title,
|
|
|
|
'description' => $description,
|
2019-03-27 00:25:02 +01:00
|
|
|
'defaultFormat' => $this->defaultFormat,
|
2017-01-16 13:43:47 +01:00
|
|
|
]
|
|
|
|
);
|
2017-01-16 16:31:20 +00:00
|
|
|
|
2017-01-16 17:19:19 +01:00
|
|
|
return $response;
|
2017-01-16 13:43:47 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 15:50:32 +01:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Dislay information about the video.
|
2016-02-28 23:04:53 +01:00
|
|
|
*
|
2020-05-13 22:28:05 +02:00
|
|
|
* @param Request $request PSR-7 request
|
2016-03-30 01:39:47 +02:00
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
2016-08-01 13:29:13 +02:00
|
|
|
* @return Response HTTP response
|
2020-06-21 01:44:20 +02:00
|
|
|
* @throws AlltubeLibraryException
|
2015-10-31 15:50:32 +01:00
|
|
|
*/
|
2020-12-17 22:43:05 +01:00
|
|
|
public function info(Request $request, Response $response): Response
|
2015-10-31 15:50:32 +01:00
|
|
|
{
|
2019-04-21 18:30:02 +02:00
|
|
|
$url = $request->getQueryParam('url') ?: $request->getQueryParam('v');
|
2018-08-04 15:46:49 +02:00
|
|
|
|
2019-04-21 18:30:02 +02:00
|
|
|
if (isset($url) && !empty($url)) {
|
2020-06-21 01:44:20 +02:00
|
|
|
$this->video = $this->downloader->getVideo($url, $this->getFormat($request), $this->getPassword($request));
|
2019-04-21 18:30:02 +02:00
|
|
|
|
2019-04-21 23:16:27 +02:00
|
|
|
if ($this->config->convert && $request->getQueryParam('audio')) {
|
2019-04-22 15:20:05 +02:00
|
|
|
// We skip the info page and get directly to the download.
|
|
|
|
return $response->withRedirect(
|
2020-10-22 01:00:52 +02:00
|
|
|
$this->router->pathFor('download', [], $request->getQueryParams())
|
2019-04-22 15:20:05 +02:00
|
|
|
);
|
2015-10-29 22:32:36 +01:00
|
|
|
} else {
|
2019-04-22 15:20:05 +02:00
|
|
|
return $this->getInfoResponse($request, $response);
|
2015-10-29 22:32:36 +01:00
|
|
|
}
|
2016-06-09 21:15:44 +02:00
|
|
|
} else {
|
2020-10-22 01:00:52 +02:00
|
|
|
return $response->withRedirect($this->router->pathFor('index'));
|
2015-10-29 22:32:36 +01:00
|
|
|
}
|
2016-05-01 01:25:08 +02:00
|
|
|
}
|
|
|
|
|
2020-06-21 01:44:20 +02: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 22:43:05 +01:00
|
|
|
protected function displayError(Request $request, Response $response, string $message): Response
|
2020-06-21 01:44:20 +02:00
|
|
|
{
|
|
|
|
$this->view->render(
|
|
|
|
$response,
|
|
|
|
'error.tpl',
|
|
|
|
[
|
|
|
|
'error' => $message,
|
|
|
|
'class' => 'video',
|
|
|
|
'title' => $this->localeManager->t('Error'),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2020-07-05 11:22:55 +02:00
|
|
|
return $response->withStatus(StatusCode::HTTP_INTERNAL_SERVER_ERROR);
|
2020-06-21 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 23:17:23 +02:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @param Response $response
|
|
|
|
* @return Response
|
|
|
|
*/
|
2020-12-17 22:43:05 +01:00
|
|
|
public function notFound(Request $request, Response $response): Response
|
2020-07-15 23:17:23 +02: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 22:43:05 +01:00
|
|
|
public function notAllowed(Request $request, Response $response): Response
|
2020-07-15 23:17:23 +02:00
|
|
|
{
|
|
|
|
return $this->displayError($request, $response, $this->localeManager->t('Method not allowed'))
|
|
|
|
->withStatus(StatusCode::HTTP_METHOD_NOT_ALLOWED);
|
|
|
|
}
|
|
|
|
|
2016-08-01 13:29:13 +02:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Display an error page.
|
|
|
|
*
|
2020-05-13 22:28:05 +02: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 13:29:13 +02:00
|
|
|
* @return Response HTTP response
|
|
|
|
*/
|
2020-12-17 22:43:05 +01:00
|
|
|
public function error(Request $request, Response $response, Throwable $error): Response
|
2016-05-01 01:25:08 +02:00
|
|
|
{
|
2020-10-18 12:49:44 +02:00
|
|
|
$this->logger->error($error);
|
|
|
|
|
2020-10-19 23:43:33 +02:00
|
|
|
// 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 21:22:23 +01:00
|
|
|
if ($this->config->debug) {
|
2021-02-09 22:17:29 +01:00
|
|
|
$renderer = new HtmlErrorRenderer(true, null, null, $this->container->get('root_path'));
|
2020-05-13 22:57:25 +02:00
|
|
|
$exception = $renderer->render($error);
|
2020-06-21 01:44:20 +02:00
|
|
|
|
2020-05-13 22:57:25 +02:00
|
|
|
$response->getBody()->write($exception->getAsString());
|
2020-06-21 01:44:20 +02:00
|
|
|
foreach ($exception->getHeaders() as $header => $value) {
|
|
|
|
$response = $response->withHeader($header, $value);
|
|
|
|
}
|
2015-10-31 11:48:14 +01:00
|
|
|
|
2019-12-02 22:04:14 +01:00
|
|
|
return $response->withStatus($exception->getStatusCode());
|
2019-11-27 23:49:01 +01:00
|
|
|
} else {
|
2019-12-02 22:04:14 +01:00
|
|
|
if ($error instanceof Exception) {
|
|
|
|
$message = $error->getMessage();
|
|
|
|
} else {
|
|
|
|
$message = '';
|
|
|
|
}
|
|
|
|
|
2020-06-21 01:44:20 +02:00
|
|
|
return $this->displayError($request, $response, $message);
|
2019-12-02 22:04:14 +01:00
|
|
|
}
|
2019-11-27 23:49:01 +01:00
|
|
|
}
|
2015-10-29 22:32:36 +01:00
|
|
|
}
|