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 15:00:51 +00:00
|
|
|
use Alltube\SessionManager;
|
2019-04-22 14:05:58 +00:00
|
|
|
use Aura\Session\Segment;
|
2020-07-15 20:39:46 +00:00
|
|
|
use Consolidation\Log\Logger;
|
2019-04-22 14:05:58 +00:00
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
use Slim\Http\Request;
|
2020-06-20 23:44:20 +00:00
|
|
|
use Slim\Http\Response;
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class used by every controller.
|
|
|
|
*/
|
|
|
|
abstract class BaseController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Current video.
|
|
|
|
*
|
|
|
|
* @var Video
|
|
|
|
*/
|
|
|
|
protected $video;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default youtube-dl format.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-06-20 11:33:16 +00:00
|
|
|
protected $defaultFormat = 'best/bestvideo';
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Slim dependency container.
|
|
|
|
*
|
|
|
|
* @var ContainerInterface
|
|
|
|
*/
|
|
|
|
protected $container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
* @var Logger
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
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;
|
2019-04-22 15:00:51 +00:00
|
|
|
$session = SessionManager::getSession();
|
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-07-15 20:39:46 +00:00
|
|
|
$this->downloader->setLogger($this->container->get('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
|
|
|
|
*/
|
|
|
|
protected function getFormat(Request $request)
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
*
|
|
|
|
* @return string Password
|
|
|
|
*/
|
|
|
|
protected function getPassword(Request $request)
|
|
|
|
{
|
|
|
|
$url = $request->getQueryParam('url');
|
|
|
|
|
|
|
|
$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-09-27 13:53:53 +00:00
|
|
|
protected function displayError(Request $request, Response $response, string $message)
|
2020-06-20 23:44:20 +00:00
|
|
|
{
|
|
|
|
$controller = new FrontController($this->container);
|
|
|
|
|
|
|
|
return $controller->displayError($request, $response, $message);
|
|
|
|
}
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|