2019-04-22 14:05:58 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2019-04-22 14:05:58 +00:00
|
|
|
/**
|
|
|
|
* BaseController class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube\Controller;
|
|
|
|
|
|
|
|
use Alltube\Config;
|
2020-06-20 23:44:20 +00:00
|
|
|
use Alltube\Library\Downloader;
|
|
|
|
use Alltube\Library\Video;
|
2019-11-27 22:15:49 +00:00
|
|
|
use Alltube\LocaleManager;
|
2019-04-22 14:05:58 +00:00
|
|
|
use Aura\Session\Segment;
|
2022-02-27 09:54:56 +00:00
|
|
|
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidURLException;
|
|
|
|
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Options;
|
|
|
|
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Url;
|
2019-04-22 14:05:58 +00:00
|
|
|
use Psr\Container\ContainerInterface;
|
2021-02-06 17:22:19 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-04-22 14:05:58 +00:00
|
|
|
use Slim\Http\Request;
|
2020-06-20 23:44:20 +00:00
|
|
|
use Slim\Http\Response;
|
2020-10-21 23:00:52 +00:00
|
|
|
use Slim\Router;
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class used by every controller.
|
|
|
|
*/
|
|
|
|
abstract class BaseController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Current video.
|
|
|
|
*
|
|
|
|
* @var Video
|
|
|
|
*/
|
2023-03-21 19:18:21 +00:00
|
|
|
protected Video $video;
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default youtube-dl format.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-03-21 19:18:21 +00:00
|
|
|
protected string $defaultFormat = 'best/bestvideo';
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Slim dependency container.
|
|
|
|
*
|
|
|
|
* @var ContainerInterface
|
|
|
|
*/
|
2023-03-21 19:18:21 +00:00
|
|
|
protected ContainerInterface $container;
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Config instance.
|
|
|
|
*
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Session segment used to store session variables.
|
|
|
|
*
|
|
|
|
* @var Segment
|
|
|
|
*/
|
|
|
|
protected $sessionSegment;
|
|
|
|
|
2019-11-27 22:15:49 +00:00
|
|
|
/**
|
|
|
|
* LocaleManager instance.
|
|
|
|
*
|
|
|
|
* @var LocaleManager
|
|
|
|
*/
|
|
|
|
protected $localeManager;
|
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
/**
|
|
|
|
* Downloader instance.
|
|
|
|
*
|
|
|
|
* @var Downloader
|
|
|
|
*/
|
|
|
|
protected $downloader;
|
|
|
|
|
2020-07-15 20:39:46 +00:00
|
|
|
/**
|
2021-02-06 17:22:19 +00:00
|
|
|
* @var LoggerInterface
|
2020-07-15 20:39:46 +00:00
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
2020-10-21 23:00:52 +00:00
|
|
|
/**
|
|
|
|
* @var Router
|
|
|
|
*/
|
|
|
|
protected $router;
|
|
|
|
|
2019-04-22 14:05:58 +00:00
|
|
|
/**
|
|
|
|
* BaseController constructor.
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container Slim dependency container
|
|
|
|
*/
|
2019-04-22 15:00:51 +00:00
|
|
|
public function __construct(ContainerInterface $container)
|
2019-04-22 14:05:58 +00:00
|
|
|
{
|
2020-07-15 21:05:41 +00:00
|
|
|
$this->config = $container->get('config');
|
2019-04-22 14:05:58 +00:00
|
|
|
$this->container = $container;
|
2020-10-21 20:47:15 +00:00
|
|
|
$session = $container->get('session');
|
2019-04-22 14:05:58 +00:00
|
|
|
$this->sessionSegment = $session->getSegment(self::class);
|
2019-11-27 22:15:49 +00:00
|
|
|
$this->localeManager = $this->container->get('locale');
|
2020-06-20 23:44:20 +00:00
|
|
|
$this->downloader = $this->config->getDownloader();
|
2020-10-21 23:00:52 +00:00
|
|
|
$this->router = $this->container->get('router');
|
2020-10-18 10:49:44 +00:00
|
|
|
$this->logger = $this->container->get('logger');
|
|
|
|
$this->downloader->setLogger($this->logger);
|
2019-04-22 14:05:58 +00:00
|
|
|
|
2020-06-20 11:33:16 +00:00
|
|
|
if (!$this->config->stream) {
|
|
|
|
// Force HTTP if stream is not enabled.
|
|
|
|
$this->defaultFormat = Config::addHttpToFormat($this->defaultFormat);
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get video format from request parameters or default format if none is specified.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
*
|
|
|
|
* @return string format
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
protected function getFormat(Request $request): string
|
2019-04-22 14:05:58 +00:00
|
|
|
{
|
|
|
|
$format = $request->getQueryParam('format');
|
|
|
|
if (!isset($format)) {
|
|
|
|
$format = $this->defaultFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the password entered for the current video.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
*
|
2020-12-17 21:43:05 +00:00
|
|
|
* @return string|null Password
|
2022-02-27 09:54:56 +00:00
|
|
|
* @throws InvalidURLException
|
2019-04-22 14:05:58 +00:00
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
protected function getPassword(Request $request): ?string
|
2019-04-22 14:05:58 +00:00
|
|
|
{
|
2022-02-27 09:54:56 +00:00
|
|
|
$url = $this->getVideoPageUrl($request);
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
$password = $request->getParam('password');
|
|
|
|
if (isset($password)) {
|
|
|
|
$this->sessionSegment->setFlash($url, $password);
|
|
|
|
} else {
|
|
|
|
$password = $this->sessionSegment->getFlash($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $password;
|
|
|
|
}
|
2020-06-20 23:44:20 +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
|
2020-06-20 23:44:20 +00:00
|
|
|
{
|
|
|
|
$controller = new FrontController($this->container);
|
|
|
|
|
|
|
|
return $controller->displayError($request, $response, $message);
|
|
|
|
}
|
2022-02-27 09:54:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return string
|
|
|
|
* @throws InvalidURLException
|
|
|
|
*/
|
|
|
|
protected function getVideoPageUrl(Request $request): string
|
|
|
|
{
|
|
|
|
// Prevent SSRF attacks.
|
2022-02-27 22:32:08 +00:00
|
|
|
$parts = Url::validateUrl($request->getQueryParam('url'), new Options());
|
2022-02-27 09:54:56 +00:00
|
|
|
|
|
|
|
return $parts['url'];
|
|
|
|
}
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|