alltube/controllers/FrontController.php

390 lines
12 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
2015-10-31 14:42:25 +00:00
use Alltube\Config;
use Alltube\PasswordException;
2016-09-07 22:28:28 +00:00
use Alltube\VideoDownload;
use Interop\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;
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
/**
2016-09-07 22:28:28 +00:00
* VideoDownload instance.
*
2016-08-01 11:29:13 +00:00
* @var VideoDownload
*/
2016-08-01 01:16:48 +00:00
private $download;
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.
*
2016-10-20 21:27:13 +00:00
* @var \Aura\Session\Segment
*/
private $sessionSegment;
2016-10-23 20:59:37 +00:00
/**
2016-10-23 21:08:32 +00:00
* Smarty view.
*
2016-10-23 20:59:37 +00:00
* @var \Slim\Views\Smarty
*/
private $view;
2016-08-01 11:29:13 +00:00
/**
2016-09-07 22:28:28 +00:00
* FrontController constructor.
*
2016-08-01 11:29:13 +00:00
* @param Container $container Slim dependency container
*/
public function __construct(ContainerInterface $container)
2016-04-08 17:06:41 +00:00
{
$this->config = Config::getInstance();
$this->download = new VideoDownload();
$this->container = $container;
2016-10-23 20:59:37 +00:00
$this->view = $this->container->get('view');
2016-10-20 21:03:13 +00:00
$session_factory = new \Aura\Session\SessionFactory();
$session = $session_factory->newInstance($_COOKIE);
$this->sessionSegment = $session->getSegment('Alltube\Controller\FrontController');
if ($this->config->stream) {
$this->defaultFormat = 'best';
} else {
$this->defaultFormat = 'best[protocol^=http]';
}
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
*
2015-10-31 14:50:32 +00:00
* @return void
*/
2016-07-22 11:58:33 +00:00
public function index(Request $request, Response $response)
2015-10-31 14:50:32 +00:00
{
2017-01-16 00:57:44 +00:00
$uri = $request->getUri();
2016-10-23 20:59:37 +00:00
$this->view->render(
$response,
'index.tpl',
[
2017-01-10 22:39:58 +00:00
'convert' => $this->config->convert,
'uglyUrls' => $this->config->uglyUrls,
2017-01-10 22:39:58 +00:00
'class' => 'index',
'description' => 'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.',
2017-01-16 00:57:44 +00:00
'domain' => $uri->getScheme().'://'.$uri->getAuthority(),
2017-01-16 13:26:12 +00:00
'canonical' => $this->getCanonicalUrl($request),
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
}
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
*
2015-10-31 14:50:32 +00:00
* @return void
*/
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',
[
'extractors' => $this->download->listExtractors(),
'class' => 'extractors',
'title' => 'Supported websites',
'description' => 'List of all supported websites from which Alltube Download '.
'can extract video or audio files',
2017-01-16 13:26:12 +00:00
'canonical' => $this->getCanonicalUrl($request),
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.
*
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
* @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',
[
'class' => 'password',
'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),
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
/**
2017-01-16 16:31:20 +00:00
* Return the converted MP3 file.
*
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
* @param array $params GET query parameters
* @param string $password Video password
*
2017-01-16 12:43:47 +00:00
* @return Response
*/
private function getAudioResponse(Request $request, Response $response, array $params, $password = null)
{
try {
if ($this->config->stream) {
return $this->getStream($params['url'], 'mp3', $response, $request, $password);
} else {
$url = $this->download->getURL($params['url'], 'mp3[protocol^=http]', $password);
return $response->withRedirect($url);
}
} catch (PasswordException $e) {
return $this->password($request, $response);
} catch (\Exception $e) {
$response = $response->withHeader(
'Content-Disposition',
'attachment; filename="'.
$this->download->getAudioFilename($params['url'], 'bestaudio/best', $password).'"'
);
$response = $response->withHeader('Content-Type', 'audio/mpeg');
if ($request->isGet() || $request->isPost()) {
$process = $this->download->getAudioStream($params['url'], 'bestaudio/best', $password);
$response = $response->withBody(new Stream($process));
}
return $response;
}
}
/**
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
* @param array $params GET query parameters
* @param string $password Video password
*
2017-01-16 12:43:47 +00:00
* @return Response
*/
private function getVideoResponse(Request $request, Response $response, array $params, $password = null)
{
try {
$video = $this->download->getJSON($params['url'], $this->defaultFormat, $password);
} catch (PasswordException $e) {
return $this->password($request, $response);
}
if ($this->config->stream) {
$protocol = '';
} else {
$protocol = '[protocol^=http]';
}
$this->view->render(
$response,
'video.tpl',
[
'video' => $video,
'class' => 'video',
'title' => $video->title,
'description' => 'Download "'.$video->title.'" from '.$video->extractor_key,
'protocol' => $protocol,
'config' => $this->config,
2017-01-16 13:26:12 +00:00
'canonical' => $this->getCanonicalUrl($request),
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
{
2016-04-07 00:09:34 +00:00
$params = $request->getQueryParams();
2016-09-07 22:28:28 +00:00
if (isset($params['url'])) {
$password = $request->getParam('password');
if (isset($password)) {
$this->sessionSegment->setFlash($params['url'], $password);
}
2016-04-07 00:09:34 +00:00
if (isset($params['audio'])) {
2017-01-16 12:43:47 +00:00
return $this->getAudioResponse($request, $response, $params, $password);
2015-10-29 21:32:36 +00:00
} else {
2017-01-16 12:43:47 +00:00
return $this->getVideoResponse($request, $response, $params, $password);
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.
*
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
* @param \Exception $exception Error to display
*
2016-08-01 11:29:13 +00:00
* @return Response HTTP response
*/
2016-07-22 11:58:33 +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',
[
2017-01-16 13:26:12 +00:00
'errors' => $exception->getMessage(),
'class' => 'video',
'title' => 'Error',
'canonical' => $this->getCanonicalUrl($request),
2016-10-23 20:59:37 +00:00
]
);
2016-09-07 22:28:28 +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 string $url URL of the video
* @param string $format Requested format
* @param Response $response PSR-7 response
* @param Request $request PSR-7 request
* @param string $password Video password
*
2016-12-26 14:58:07 +00:00
* @return Response
*/
2016-12-26 12:23:47 +00:00
private function getStream($url, $format, $response, $request, $password = null)
2016-04-08 21:01:07 +00:00
{
if (!isset($format)) {
$format = 'best';
}
2016-12-26 12:23:47 +00:00
$video = $this->download->getJSON($url, $format, $password);
2016-12-26 14:50:26 +00:00
if ($video->protocol == 'm3u8') {
$stream = $this->download->getM3uStream($video);
$response = $response->withHeader('Content-Type', 'video/'.$video->ext);
if ($request->isGet()) {
$response = $response->withBody(new Stream($stream));
}
} else {
$client = new \GuzzleHttp\Client();
$stream = $client->request('GET', $video->url, ['stream' => true]);
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
$response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length'));
if ($request->isGet()) {
$response = $response->withBody($stream->getBody());
}
2016-04-08 21:01:07 +00:00
}
2016-12-26 14:50:26 +00:00
$response = $response->withHeader('Content-Disposition', 'attachment; filename="'.$video->_filename.'"');
2016-09-11 17:03:51 +00:00
2016-04-08 21:01:07 +00:00
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
{
2016-04-07 00:09:34 +00:00
$params = $request->getQueryParams();
2016-09-07 22:28:28 +00:00
if (isset($params['url'])) {
2015-10-31 10:48:14 +00:00
try {
if ($this->config->stream) {
return $this->getStream(
$params['url'],
$request->getParam('format'),
$response,
$request,
$this->sessionSegment->getFlash($params['url'])
);
} else {
$url = $this->download->getURL(
$params['url'],
$request->getParam('format'),
$this->sessionSegment->getFlash($params['url'])
);
return $response->withRedirect($url);
}
} catch (PasswordException $e) {
2016-12-03 13:01:35 +00:00
return $response->withRedirect(
$this->container->get('router')->pathFor('video').'?url='.urlencode($params['url'])
);
2015-10-31 10:48:14 +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
}
}
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
}