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
|
|
|
|
2015-10-31 14:42:25 +00:00
|
|
|
use Alltube\Config;
|
2018-05-23 20:38:15 +00:00
|
|
|
use Alltube\EmptyUrlException;
|
2017-05-30 21:30:21 +00:00
|
|
|
use Alltube\Locale;
|
2017-11-12 15:39:56 +00:00
|
|
|
use Alltube\LocaleManager;
|
2016-10-20 21:01:31 +00:00
|
|
|
use Alltube\PasswordException;
|
2019-04-20 22:34:12 +00:00
|
|
|
use Alltube\PlaylistArchiveStream;
|
2019-04-21 16:30:02 +00:00
|
|
|
use Alltube\Video;
|
2018-02-05 15:48:58 +00:00
|
|
|
use Aura\Session\Segment;
|
|
|
|
use Aura\Session\SessionFactory;
|
|
|
|
use Exception;
|
|
|
|
use GuzzleHttp\Client;
|
2017-04-25 20:24:44 +00:00
|
|
|
use Psr\Container\ContainerInterface;
|
2016-09-07 22:28:28 +00:00
|
|
|
use Slim\Container;
|
2016-07-22 11:58:33 +00:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
2016-09-07 22:28:28 +00:00
|
|
|
use Slim\Http\Stream;
|
2018-02-05 15:48:58 +00:00
|
|
|
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
|
|
|
*/
|
2015-10-31 14:50:32 +00:00
|
|
|
class FrontController
|
|
|
|
{
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Config instance.
|
|
|
|
*
|
2016-08-01 11:29:13 +00:00
|
|
|
* @var Config
|
|
|
|
*/
|
2016-08-01 01:16:48 +00:00
|
|
|
private $config;
|
2016-08-01 11:29:13 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-21 16:30:02 +00:00
|
|
|
* Current video.
|
2016-09-07 22:28:28 +00:00
|
|
|
*
|
2019-04-21 16:30:02 +00:00
|
|
|
* @var Video
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private $video;
|
2016-08-01 11:29:13 +00:00
|
|
|
|
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Slim dependency container.
|
|
|
|
*
|
2016-10-13 14:53:23 +00:00
|
|
|
* @var ContainerInterface
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2016-08-01 01:16:48 +00:00
|
|
|
private $container;
|
|
|
|
|
2016-10-20 21:27:13 +00:00
|
|
|
/**
|
2016-10-20 21:27:56 +00:00
|
|
|
* Session segment used to store session variables.
|
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @var Segment
|
2016-10-20 21:27:13 +00:00
|
|
|
*/
|
|
|
|
private $sessionSegment;
|
|
|
|
|
2016-10-23 20:59:37 +00:00
|
|
|
/**
|
2016-10-23 21:08:32 +00:00
|
|
|
* Smarty view.
|
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @var Smarty
|
2016-10-23 20:59:37 +00:00
|
|
|
*/
|
|
|
|
private $view;
|
|
|
|
|
2017-01-16 16:48:32 +00:00
|
|
|
/**
|
2017-01-16 16:50:58 +00:00
|
|
|
* Default youtube-dl format.
|
|
|
|
*
|
2017-01-16 16:48:32 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2019-03-24 14:13:01 +00:00
|
|
|
private $defaultFormat = 'best[protocol=https]/best[protocol=http]';
|
2017-01-16 16:48:32 +00:00
|
|
|
|
2017-05-30 21:49:38 +00:00
|
|
|
/**
|
|
|
|
* LocaleManager instance.
|
|
|
|
*
|
|
|
|
* @var LocaleManager
|
|
|
|
*/
|
|
|
|
private $localeManager;
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* FrontController constructor.
|
|
|
|
*
|
2019-03-30 18:13:48 +00:00
|
|
|
* @param ContainerInterface $container Slim dependency container
|
|
|
|
* @param array $cookies Cookie array
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function __construct(ContainerInterface $container, array $cookies = [])
|
2016-04-08 17:06:41 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->config = Config::getInstance();
|
2016-10-13 15:16:22 +00:00
|
|
|
$this->container = $container;
|
2016-10-23 20:59:37 +00:00
|
|
|
$this->view = $this->container->get('view');
|
2017-05-30 21:49:38 +00:00
|
|
|
$this->localeManager = $this->container->get('locale');
|
2018-02-05 15:48:58 +00:00
|
|
|
$session_factory = new SessionFactory();
|
2017-04-25 21:04:59 +00:00
|
|
|
$session = $session_factory->newInstance($cookies);
|
2018-02-05 15:48:58 +00:00
|
|
|
$this->sessionSegment = $session->getSegment(self::class);
|
2019-03-26 23:25:02 +00:00
|
|
|
if ($this->config->stream) {
|
2017-01-16 10:29:56 +00:00
|
|
|
$this->defaultFormat = 'best';
|
|
|
|
}
|
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
|
|
|
*
|
2016-03-29 23:39:47 +00:00
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @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
|
|
|
*/
|
2016-07-22 11:58:33 +00:00
|
|
|
public function index(Request $request, Response $response)
|
2015-10-31 14:50:32 +00:00
|
|
|
{
|
2019-03-30 18:13:48 +00:00
|
|
|
$uri = $request->getUri()->withUserInfo('');
|
2016-10-23 20:59:37 +00:00
|
|
|
$this->view->render(
|
|
|
|
$response,
|
|
|
|
'index.tpl',
|
|
|
|
[
|
2017-05-31 07:32:11 +00:00
|
|
|
'config' => $this->config,
|
|
|
|
'class' => 'index',
|
2018-01-26 10:37:43 +00:00
|
|
|
'description' => _('Easily download videos from Youtube, Dailymotion, Vimeo and other websites.'),
|
2017-05-31 07:32:11 +00:00
|
|
|
'domain' => $uri->getScheme().'://'.$uri->getAuthority(),
|
|
|
|
'canonical' => $this->getCanonicalUrl($request),
|
|
|
|
'supportedLocales' => $this->localeManager->getSupportedLocales(),
|
|
|
|
'locale' => $this->localeManager->getLocale(),
|
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.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
* @param array $data Query parameters
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function locale(Request $request, Response $response, array $data)
|
|
|
|
{
|
2017-05-30 21:49:38 +00:00
|
|
|
$this->localeManager->setLocale(new Locale($data['locale']));
|
2017-05-30 20:20:16 +00:00
|
|
|
|
|
|
|
return $response->withRedirect($this->container->get('router')->pathFor('index'));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2016-03-29 23:39:47 +00:00
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @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
|
|
|
*/
|
2016-07-22 11:58:33 +00:00
|
|
|
public function extractors(Request $request, Response $response)
|
2015-10-31 14:50:32 +00:00
|
|
|
{
|
2016-10-23 20:59:37 +00:00
|
|
|
$this->view->render(
|
|
|
|
$response,
|
|
|
|
'extractors.tpl',
|
|
|
|
[
|
2019-01-06 15:59:16 +00:00
|
|
|
'config' => $this->config,
|
2019-04-21 16:30:02 +00:00
|
|
|
'extractors' => Video::getExtractors(),
|
2016-10-23 20:59:37 +00:00
|
|
|
'class' => 'extractors',
|
2018-01-26 10:37:43 +00:00
|
|
|
'title' => _('Supported websites'),
|
|
|
|
'description' => _('List of all supported websites from which Alltube Download '.
|
|
|
|
'can extract video or audio files'),
|
2017-10-29 22:21:13 +00:00
|
|
|
'canonical' => $this->getCanonicalUrl($request),
|
|
|
|
'locale' => $this->localeManager->getLocale(),
|
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:01:31 +00:00
|
|
|
/**
|
2016-10-20 21:03:13 +00:00
|
|
|
* Display a password prompt.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @param Response $response PSR-7 response
|
2016-10-20 21:01:31 +00:00
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
|
|
|
*/
|
|
|
|
public function password(Request $request, Response $response)
|
|
|
|
{
|
2016-10-23 20:59:37 +00:00
|
|
|
$this->view->render(
|
|
|
|
$response,
|
|
|
|
'password.tpl',
|
|
|
|
[
|
2019-01-15 10:16:21 +00:00
|
|
|
'config' => $this->config,
|
2016-10-23 20:59:37 +00:00
|
|
|
'class' => 'password',
|
2018-01-26 10:37:43 +00:00
|
|
|
'title' => _('Password prompt'),
|
|
|
|
'description' => _('You need a password in order to download this video with Alltube Download'),
|
2017-01-16 13:26:12 +00:00
|
|
|
'canonical' => $this->getCanonicalUrl($request),
|
2017-05-30 21:49:38 +00:00
|
|
|
'locale' => $this->localeManager->getLocale(),
|
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-01-16 12:43:47 +00:00
|
|
|
/**
|
2018-07-03 17:47:35 +00:00
|
|
|
* Return a converted MP3 file.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private function getConvertedAudioResponse(Request $request, Response $response)
|
2018-07-03 17:47:35 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
$from = $request->getQueryParam('from');
|
|
|
|
$to = $request->getQueryParam('to');
|
2018-07-03 17:52:24 +00:00
|
|
|
|
2018-07-03 17:47:35 +00:00
|
|
|
$response = $response->withHeader(
|
|
|
|
'Content-Disposition',
|
|
|
|
'attachment; filename="'.
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->video->getFileNameWithExtension('mp3').'"'
|
2018-07-03 17:47:35 +00:00
|
|
|
);
|
|
|
|
$response = $response->withHeader('Content-Type', 'audio/mpeg');
|
|
|
|
|
|
|
|
if ($request->isGet() || $request->isPost()) {
|
|
|
|
try {
|
2019-04-21 16:30:02 +00:00
|
|
|
$process = $this->video->getAudioStream($from, $to);
|
2018-07-03 17:47:35 +00:00
|
|
|
} catch (Exception $e) {
|
2019-04-21 16:30:02 +00:00
|
|
|
// Fallback to default format.
|
|
|
|
$this->video = $this->video->withFormat($this->defaultFormat);
|
|
|
|
$process = $this->video->getAudioStream($from, $to);
|
2018-07-03 17:47:35 +00:00
|
|
|
}
|
|
|
|
$response = $response->withBody(new Stream($process));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the MP3 file.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
2017-01-16 16:47:26 +00:00
|
|
|
* @return Response HTTP response
|
2017-01-16 12:43:47 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private function getAudioResponse(Request $request, Response $response)
|
2017-01-16 12:43:47 +00:00
|
|
|
{
|
|
|
|
try {
|
2019-04-21 16:30:02 +00:00
|
|
|
// First, we try to get a MP3 file directly.
|
|
|
|
if (!empty($request->getQueryParam('from')) || !empty($request->getQueryParam('to'))) {
|
2018-07-03 17:47:35 +00:00
|
|
|
throw new Exception('Force convert when we need to seek.');
|
|
|
|
}
|
|
|
|
|
2017-01-16 12:43:47 +00:00
|
|
|
if ($this->config->stream) {
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->video = $this->video->withFormat('mp3');
|
|
|
|
|
|
|
|
return $this->getStream($request, $response);
|
2017-01-16 12:43:47 +00:00
|
|
|
} else {
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->video = $this->video->withFormat('mp3[protocol=https]/mp3[protocol=http]');
|
|
|
|
|
|
|
|
$urls = $this->video->getUrl();
|
2017-01-16 12:43:47 +00:00
|
|
|
|
2017-04-24 22:40:24 +00:00
|
|
|
return $response->withRedirect($urls[0]);
|
2017-01-16 12:43:47 +00:00
|
|
|
}
|
|
|
|
} catch (PasswordException $e) {
|
|
|
|
return $this->password($request, $response);
|
2018-02-05 15:48:58 +00:00
|
|
|
} catch (Exception $e) {
|
2019-04-21 16:30:02 +00:00
|
|
|
// If MP3 is not available, we convert it.
|
|
|
|
$this->video = $this->video->withFormat($this->defaultFormat);
|
|
|
|
|
|
|
|
return $this->getConvertedAudioResponse($request, $response);
|
2017-01-16 12:43:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-16 16:31:20 +00:00
|
|
|
* Return the video description page.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
2017-01-16 16:47:26 +00:00
|
|
|
* @return Response HTTP response
|
2017-01-16 12:43:47 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private function getVideoResponse(Request $request, Response $response)
|
2017-01-16 12:43:47 +00:00
|
|
|
{
|
|
|
|
try {
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->video->getJson();
|
2017-01-16 12:43:47 +00:00
|
|
|
} catch (PasswordException $e) {
|
|
|
|
return $this->password($request, $response);
|
|
|
|
}
|
2019-03-24 14:13:01 +00:00
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
if (isset($this->video->entries)) {
|
2017-04-24 23:53:38 +00:00
|
|
|
$template = 'playlist.tpl';
|
|
|
|
} else {
|
|
|
|
$template = 'video.tpl';
|
|
|
|
}
|
2018-01-26 10:37:43 +00:00
|
|
|
$title = _('Video download');
|
2019-04-21 16:30:02 +00:00
|
|
|
$description = _('Download video from ').$this->video->extractor_key;
|
|
|
|
if (isset($this->video->title)) {
|
|
|
|
$title = $this->video->title;
|
|
|
|
$description = _('Download').' "'.$this->video->title.'" '._('from').' '.$this->video->extractor_key;
|
2017-04-25 09:05:49 +00:00
|
|
|
}
|
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
|
|
|
[
|
2019-04-21 16:30:02 +00:00
|
|
|
'video' => $this->video,
|
2019-03-26 23:25:02 +00:00
|
|
|
'class' => 'video',
|
|
|
|
'title' => $title,
|
|
|
|
'description' => $description,
|
|
|
|
'config' => $this->config,
|
|
|
|
'canonical' => $this->getCanonicalUrl($request),
|
|
|
|
'locale' => $this->localeManager->getLocale(),
|
|
|
|
'defaultFormat' => $this->defaultFormat,
|
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
|
|
|
*
|
2016-03-29 23:39:47 +00:00
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
2016-08-01 11:29:13 +00:00
|
|
|
* @return Response HTTP response
|
2015-10-31 14:50:32 +00:00
|
|
|
*/
|
2016-07-22 11:58:33 +00:00
|
|
|
public function video(Request $request, Response $response)
|
2015-10-31 14:50:32 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
$url = $request->getQueryParam('url') ?: $request->getQueryParam('v');
|
2018-08-04 13:46:49 +00:00
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
if (isset($url) && !empty($url)) {
|
2016-10-20 21:01:31 +00:00
|
|
|
$password = $request->getParam('password');
|
|
|
|
if (isset($password)) {
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->sessionSegment->setFlash($url, $password);
|
2016-10-20 21:01:31 +00:00
|
|
|
}
|
2019-04-21 16:30:02 +00:00
|
|
|
|
|
|
|
$this->video = new Video($url, $this->defaultFormat, $password);
|
|
|
|
|
|
|
|
if ($request->getQueryParam('audio')) {
|
|
|
|
return $this->getAudioResponse($request, $response);
|
2015-10-29 21:32:36 +00:00
|
|
|
} else {
|
2019-04-21 16:30:02 +00:00
|
|
|
return $this->getVideoResponse($request, $response);
|
2015-10-29 21:32:36 +00:00
|
|
|
}
|
2016-06-09 19:15:44 +00:00
|
|
|
} else {
|
2016-07-22 12:43:50 +00:00
|
|
|
return $response->withRedirect($this->container->get('router')->pathFor('index'));
|
2015-10-29 21:32:36 +00:00
|
|
|
}
|
2016-04-30 23:25:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Display an error page.
|
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
* @param Exception $exception Error to display
|
2016-09-07 22:28:28 +00:00
|
|
|
*
|
2016-08-01 11:29:13 +00:00
|
|
|
* @return Response HTTP response
|
|
|
|
*/
|
2018-02-05 15:48:58 +00:00
|
|
|
public function error(Request $request, Response $response, Exception $exception)
|
2016-04-30 23:25:08 +00:00
|
|
|
{
|
2016-10-23 20:59:37 +00:00
|
|
|
$this->view->render(
|
|
|
|
$response,
|
|
|
|
'error.tpl',
|
|
|
|
[
|
2019-01-15 10:16:21 +00:00
|
|
|
'config' => $this->config,
|
2017-01-16 13:26:12 +00:00
|
|
|
'errors' => $exception->getMessage(),
|
|
|
|
'class' => 'video',
|
2018-01-26 10:37:43 +00:00
|
|
|
'title' => _('Error'),
|
2017-01-16 13:26:12 +00:00
|
|
|
'canonical' => $this->getCanonicalUrl($request),
|
2017-05-30 21:49:38 +00:00
|
|
|
'locale' => $this->localeManager->getLocale(),
|
2016-10-23 20:59:37 +00:00
|
|
|
]
|
|
|
|
);
|
2016-09-07 22:28:28 +00:00
|
|
|
|
2016-05-01 18:29:53 +00:00
|
|
|
return $response->withStatus(500);
|
2015-10-29 21:32:36 +00:00
|
|
|
}
|
2015-10-31 10:48:14 +00:00
|
|
|
|
2016-12-26 14:58:07 +00:00
|
|
|
/**
|
|
|
|
* Get a video/audio stream piped through the server.
|
2016-12-26 14:59:16 +00:00
|
|
|
*
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
*
|
2017-01-16 16:47:26 +00:00
|
|
|
* @return Response HTTP response
|
2016-12-26 14:58:07 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private function getStream(Request $request, Response $response)
|
2016-04-08 21:01:07 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
if (isset($this->video->entries)) {
|
|
|
|
$stream = new PlaylistArchiveStream($this->video);
|
2017-05-02 15:04:55 +00:00
|
|
|
$response = $response->withHeader('Content-Type', 'application/x-tar');
|
|
|
|
$response = $response->withHeader(
|
|
|
|
'Content-Disposition',
|
2019-04-21 16:30:02 +00:00
|
|
|
'attachment; filename="'.$this->video->title.'.tar"'
|
2017-05-02 15:04:55 +00:00
|
|
|
);
|
|
|
|
|
2019-04-20 22:34:12 +00:00
|
|
|
return $response->withBody($stream);
|
2019-04-21 16:30:02 +00:00
|
|
|
} elseif ($this->video->protocol == 'rtmp') {
|
|
|
|
$response = $response->withHeader('Content-Type', 'video/'.$this->video->ext);
|
|
|
|
$body = new Stream($this->video->getRtmpStream());
|
|
|
|
} elseif ($this->video->protocol == 'm3u8' || $this->video->protocol == 'm3u8_native') {
|
|
|
|
$response = $response->withHeader('Content-Type', 'video/'.$this->video->ext);
|
|
|
|
$body = new Stream($this->video->getM3uStream());
|
2016-12-26 14:50:26 +00:00
|
|
|
} else {
|
2018-02-05 15:48:58 +00:00
|
|
|
$client = new Client();
|
2018-09-18 16:49:43 +00:00
|
|
|
$stream = $client->request(
|
|
|
|
'GET',
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->video->url,
|
2018-09-18 16:49:43 +00:00
|
|
|
[
|
2018-09-18 16:52:56 +00:00
|
|
|
'stream' => true,
|
|
|
|
'headers' => ['Range' => $request->getHeader('Range')],
|
2018-09-18 16:49:43 +00:00
|
|
|
]
|
|
|
|
);
|
2016-12-26 14:50:26 +00:00
|
|
|
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
|
|
|
|
$response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length'));
|
2018-09-18 16:49:43 +00:00
|
|
|
$response = $response->withHeader('Accept-Ranges', $stream->getHeader('Accept-Ranges'));
|
|
|
|
$response = $response->withHeader('Content-Range', $stream->getHeader('Content-Range'));
|
|
|
|
if ($stream->getStatusCode() == 206) {
|
|
|
|
$response = $response->withStatus(206);
|
|
|
|
}
|
2017-05-04 22:25:08 +00:00
|
|
|
$body = $stream->getBody();
|
|
|
|
}
|
|
|
|
if ($request->isGet()) {
|
|
|
|
$response = $response->withBody($body);
|
2016-04-08 21:01:07 +00:00
|
|
|
}
|
2017-04-24 23:53:38 +00:00
|
|
|
$response = $response->withHeader(
|
|
|
|
'Content-Disposition',
|
|
|
|
'attachment; filename="'.
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->video->getFilename().'"'
|
2017-04-24 23:53:38 +00:00
|
|
|
);
|
2016-09-11 17:03:51 +00:00
|
|
|
|
2016-04-08 21:01:07 +00:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2017-04-25 20:12:11 +00:00
|
|
|
/**
|
|
|
|
* Get a remuxed stream piped through the server.
|
|
|
|
*
|
2017-04-25 20:49:28 +00:00
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
* @param Request $request PSR-7 request
|
2017-04-25 20:12:11 +00:00
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private function getRemuxStream(Request $request, Response $response)
|
2017-04-25 20:12:11 +00:00
|
|
|
{
|
|
|
|
if (!$this->config->remux) {
|
2018-02-05 15:48:58 +00:00
|
|
|
throw new Exception(_('You need to enable remux mode to merge two formats.'));
|
2017-04-25 20:12:11 +00:00
|
|
|
}
|
2019-04-21 16:30:02 +00:00
|
|
|
$stream = $this->video->getRemuxStream();
|
2017-04-25 20:12:11 +00:00
|
|
|
$response = $response->withHeader('Content-Type', 'video/x-matroska');
|
|
|
|
if ($request->isGet()) {
|
|
|
|
$response = $response->withBody(new Stream($stream));
|
|
|
|
}
|
|
|
|
|
2018-12-28 19:01:46 +00:00
|
|
|
return $response->withHeader(
|
|
|
|
'Content-Disposition',
|
2019-04-21 16:30:02 +00:00
|
|
|
'attachment; filename="'.$this->video->getFileNameWithExtension('mkv')
|
2018-12-28 19:01:46 +00:00
|
|
|
);
|
2017-04-25 20:12:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 20:47:52 +00:00
|
|
|
/**
|
2017-04-25 20:49:28 +00:00
|
|
|
* Get video format from request parameters or default format if none is specified.
|
2017-04-25 20:47:52 +00:00
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
*
|
|
|
|
* @return string format
|
|
|
|
*/
|
|
|
|
private function getFormat(Request $request)
|
|
|
|
{
|
|
|
|
$format = $request->getQueryParam('format');
|
|
|
|
if (!isset($format)) {
|
|
|
|
$format = $this->defaultFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get approriate HTTP response to redirect query
|
2017-04-25 20:49:28 +00:00
|
|
|
* Depends on whether we want to stream, remux or simply redirect.
|
2017-04-25 20:47:52 +00:00
|
|
|
*
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private function getRedirectResponse(Request $request, Response $response)
|
2017-04-25 20:47:52 +00:00
|
|
|
{
|
2018-05-23 20:38:15 +00:00
|
|
|
try {
|
2019-04-21 16:30:02 +00:00
|
|
|
$videoUrls = $this->video->getUrl();
|
2018-05-23 20:38:15 +00:00
|
|
|
} catch (EmptyUrlException $e) {
|
|
|
|
/*
|
|
|
|
If this happens it is probably a playlist
|
2018-05-26 12:48:15 +00:00
|
|
|
so it will either be handled by getStream() or throw an exception anyway.
|
2018-05-23 20:38:15 +00:00
|
|
|
*/
|
|
|
|
$videoUrls = [];
|
|
|
|
}
|
2017-04-25 20:47:52 +00:00
|
|
|
if (count($videoUrls) > 1) {
|
2019-04-21 16:30:02 +00:00
|
|
|
return $this->getRemuxStream($request, $response);
|
2017-04-25 20:47:52 +00:00
|
|
|
} elseif ($this->config->stream) {
|
2019-04-21 16:30:02 +00:00
|
|
|
return $this->getStream($request, $response);
|
2017-04-25 20:47:52 +00:00
|
|
|
} else {
|
2017-05-02 15:04:55 +00:00
|
|
|
if (empty($videoUrls[0])) {
|
2018-02-05 15:48:58 +00:00
|
|
|
throw new Exception(_("Can't find URL of video."));
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 20:47:52 +00:00
|
|
|
return $response->withRedirect($videoUrls[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 22:30:24 +00:00
|
|
|
/**
|
|
|
|
* Return a converted video file.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private function getConvertedResponse(Request $request, Response $response)
|
2018-01-24 22:30:24 +00:00
|
|
|
{
|
|
|
|
$response = $response->withHeader(
|
|
|
|
'Content-Disposition',
|
|
|
|
'attachment; filename="'.
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->video->getFileNameWithExtension($request->getQueryParam('customFormat')).'"'
|
2018-01-24 22:30:24 +00:00
|
|
|
);
|
2019-04-21 16:30:02 +00:00
|
|
|
$response = $response->withHeader('Content-Type', 'video/'.$request->getQueryParam('customFormat'));
|
2018-01-24 22:30:24 +00:00
|
|
|
|
|
|
|
if ($request->isGet() || $request->isPost()) {
|
2019-04-21 16:30:02 +00:00
|
|
|
$process = $this->video->getConvertedStream(
|
|
|
|
$request->getQueryParam('customBitrate'),
|
|
|
|
$request->getQueryParam('customFormat')
|
2018-01-24 22:30:24 +00:00
|
|
|
);
|
|
|
|
$response = $response->withBody(new Stream($process));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2015-10-31 14:50:32 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Redirect to video file.
|
2016-02-28 22:04:53 +00:00
|
|
|
*
|
2016-03-29 23:39:47 +00:00
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
2016-08-01 11:29:13 +00:00
|
|
|
* @return Response HTTP response
|
2015-10-31 14:50:32 +00:00
|
|
|
*/
|
2016-07-22 11:58:33 +00:00
|
|
|
public function redirect(Request $request, Response $response)
|
2015-10-31 14:50:32 +00:00
|
|
|
{
|
2017-04-25 20:47:52 +00:00
|
|
|
$format = $this->getFormat($request);
|
2019-04-21 16:30:02 +00:00
|
|
|
$url = $request->getQueryParam('url');
|
|
|
|
|
|
|
|
if (isset($url)) {
|
|
|
|
$this->video = new Video($url, $format, $this->sessionSegment->getFlash($url));
|
|
|
|
|
2015-10-31 10:48:14 +00:00
|
|
|
try {
|
2018-01-24 22:30:24 +00:00
|
|
|
if ($this->config->convertAdvanced && !is_null($request->getQueryParam('customConvert'))) {
|
2019-04-21 16:30:02 +00:00
|
|
|
return $this->getConvertedResponse($request, $response);
|
2018-01-24 22:30:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
return $this->getRedirectResponse($request, $response);
|
2016-10-20 21:01:31 +00:00
|
|
|
} catch (PasswordException $e) {
|
2016-12-03 13:01:35 +00:00
|
|
|
return $response->withRedirect(
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->container->get('router')->pathFor('video').'?url='.urlencode($url)
|
2016-12-03 13:01:35 +00:00
|
|
|
);
|
2018-02-05 15:48:58 +00:00
|
|
|
} catch (Exception $e) {
|
2016-04-10 17:28:59 +00:00
|
|
|
$response->getBody()->write($e->getMessage());
|
2016-09-07 22:28:28 +00:00
|
|
|
|
2017-01-16 16:19:19 +00:00
|
|
|
return $response->withHeader('Content-Type', 'text/plain')->withStatus(500);
|
2015-10-31 10:48:14 +00:00
|
|
|
}
|
2017-01-16 16:19:19 +00:00
|
|
|
} else {
|
|
|
|
return $response->withRedirect($this->container->get('router')->pathFor('index'));
|
2015-10-31 10:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 11:02:21 +00:00
|
|
|
/**
|
|
|
|
* Return the JSON object generated by youtube-dl.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
|
|
|
*/
|
|
|
|
public function json(Request $request, Response $response)
|
|
|
|
{
|
|
|
|
$format = $this->getFormat($request);
|
2019-04-21 16:30:02 +00:00
|
|
|
$url = $request->getQueryParam('url');
|
|
|
|
|
|
|
|
if (isset($url)) {
|
2018-03-20 11:02:21 +00:00
|
|
|
try {
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->video = new Video($url, $format);
|
|
|
|
|
|
|
|
return $response->withJson($this->video->getJson());
|
2018-03-20 11:02:21 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
return $response->withJson(['error' => $e->getMessage()])
|
|
|
|
->withStatus(500);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $response->withJson(['error' => 'You need to provide the url parameter'])
|
|
|
|
->withStatus(400);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 13:26:12 +00:00
|
|
|
/**
|
2017-01-16 16:31:20 +00:00
|
|
|
* Generate the canonical URL of the current page.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 Request
|
|
|
|
*
|
2017-01-16 13:26:12 +00:00
|
|
|
* @return string URL
|
|
|
|
*/
|
|
|
|
private function getCanonicalUrl(Request $request)
|
|
|
|
{
|
|
|
|
$uri = $request->getUri();
|
|
|
|
$return = 'https://alltubedownload.net/';
|
|
|
|
|
|
|
|
$path = $uri->getPath();
|
|
|
|
if ($path != '/') {
|
|
|
|
$return .= $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $uri->getQuery();
|
|
|
|
if (!empty($query)) {
|
|
|
|
$return .= '?'.$query;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2015-10-29 21:32:36 +00:00
|
|
|
}
|