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
|
|
|
*/
|
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;
|
2016-09-07 22:28:28 +00:00
|
|
|
use Alltube\VideoDownload;
|
2016-10-20 21:01:31 +00:00
|
|
|
use Alltube\PasswordException;
|
2016-10-13 14:34:57 +00:00
|
|
|
use Interop\Container\ContainerInterface;
|
2016-10-13 15:02:41 +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-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
|
|
|
|
*/
|
2016-10-13 14:34:57 +00:00
|
|
|
public function __construct(ContainerInterface $container)
|
2016-04-08 17:06:41 +00:00
|
|
|
{
|
|
|
|
$this->config = Config::getInstance();
|
|
|
|
$this->download = new VideoDownload();
|
2016-10-13 15:16:22 +00:00
|
|
|
$this->container = $container;
|
2016-10-20 21:01:31 +00:00
|
|
|
$session_factory = new \Aura\Session\SessionFactory;
|
|
|
|
$session = $session_factory->newInstance($_COOKIE);
|
|
|
|
$this->sessionSegment = $session->getSegment('Alltube\Controller\FrontController');
|
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
|
|
|
{
|
2016-10-13 15:16:22 +00:00
|
|
|
if ($this->container instanceof Container) {
|
|
|
|
$this->container->view->render(
|
|
|
|
$response,
|
|
|
|
'index.tpl',
|
|
|
|
[
|
|
|
|
'convert' => $this->config->convert,
|
|
|
|
'class' => 'index',
|
|
|
|
'description' => 'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
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-13 15:16:22 +00:00
|
|
|
if ($this->container instanceof Container) {
|
|
|
|
$this->container->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',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2015-10-29 21:32:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 21:01:31 +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)
|
|
|
|
{
|
|
|
|
if ($this->container instanceof Container) {
|
|
|
|
$this->container->view->render(
|
|
|
|
$response,
|
|
|
|
'password.tpl',
|
|
|
|
[
|
|
|
|
'class' => 'password',
|
|
|
|
'title' => 'Password prompt',
|
|
|
|
'description' => 'You need a password in order to download this video with Alltube Download',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08 22:47:51 +00:00
|
|
|
$params = $request->getQueryParams();
|
2016-09-07 22:28:28 +00:00
|
|
|
if (isset($params['url'])) {
|
2016-10-20 21:01:31 +00:00
|
|
|
$password = $request->getParam('password');
|
|
|
|
if (isset($password)) {
|
|
|
|
$this->sessionSegment->setFlash($params['url'], $password);
|
|
|
|
}
|
2016-04-08 22:47:51 +00:00
|
|
|
if (isset($params['audio'])) {
|
2015-10-29 21:32:36 +00:00
|
|
|
try {
|
2016-09-07 22:28:28 +00:00
|
|
|
$url = $this->download->getURL($params['url'], 'mp3[protocol^=http]');
|
|
|
|
|
2016-04-30 23:25:08 +00:00
|
|
|
return $response->withRedirect($url);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$response = $response->withHeader(
|
|
|
|
'Content-Disposition',
|
|
|
|
'attachment; filename="'.
|
2016-09-07 22:28:28 +00:00
|
|
|
$this->download->getAudioFilename($params['url'], 'bestaudio/best').'"'
|
2016-04-30 23:25:08 +00:00
|
|
|
);
|
|
|
|
$response = $response->withHeader('Content-Type', 'audio/mpeg');
|
2016-04-14 10:42:13 +00:00
|
|
|
|
2016-04-30 23:25:08 +00:00
|
|
|
if ($request->isGet()) {
|
2016-09-07 22:28:28 +00:00
|
|
|
$process = $this->download->getAudioStream($params['url'], 'bestaudio/best');
|
2016-07-29 22:15:17 +00:00
|
|
|
$response = $response->withBody(new Stream($process));
|
2016-04-30 23:25:08 +00:00
|
|
|
}
|
2016-09-07 22:28:28 +00:00
|
|
|
|
2016-04-30 23:25:08 +00:00
|
|
|
return $response;
|
2015-10-29 21:32:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-10-20 21:01:31 +00:00
|
|
|
try {
|
|
|
|
$video = $this->download->getJSON($params['url'], null, $password);
|
|
|
|
} catch (PasswordException $e) {
|
|
|
|
return $this->password($request, $response);
|
|
|
|
}
|
2016-10-13 15:16:22 +00:00
|
|
|
if ($this->container instanceof Container) {
|
|
|
|
$this->container->view->render(
|
|
|
|
$response,
|
|
|
|
'video.tpl',
|
|
|
|
[
|
|
|
|
'video' => $video,
|
|
|
|
'class' => 'video',
|
|
|
|
'title' => $video->title,
|
|
|
|
'description' => 'Download "'.$video->title.'" from '.$video->extractor_key,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
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-13 15:29:31 +00:00
|
|
|
if ($this->container instanceof Container) {
|
2016-10-13 15:16:22 +00:00
|
|
|
$this->container->view->render(
|
|
|
|
$response,
|
|
|
|
'error.tpl',
|
|
|
|
[
|
|
|
|
'errors' => $exception->getMessage(),
|
|
|
|
'class' => 'video',
|
|
|
|
'title' => 'Error',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
|
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-08 22:47:51 +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 {
|
2016-10-20 21:01:31 +00:00
|
|
|
$url = $this->download->getURL($params['url'], $request->getParam('format'), $this->sessionSegment->getFlash($params['url']));
|
2016-09-07 22:28:28 +00:00
|
|
|
|
2016-04-08 18:08:04 +00:00
|
|
|
return $response->withRedirect($url);
|
2016-10-20 21:01:31 +00:00
|
|
|
} catch (PasswordException $e) {
|
|
|
|
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
|
|
|
|
2016-03-29 23:39:47 +00:00
|
|
|
return $response->withHeader('Content-Type', 'text/plain');
|
2015-10-31 10:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 14:50:32 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Output JSON info 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 json(Request $request, Response $response)
|
2015-10-31 14:50:32 +00:00
|
|
|
{
|
2016-04-08 22:47:51 +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 {
|
2016-09-07 22:28:28 +00:00
|
|
|
$video = $this->download->getJSON($params['url']);
|
|
|
|
|
2016-03-29 23:39:47 +00:00
|
|
|
return $response->withJson($video);
|
2015-10-31 10:48:14 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-03-29 23:39:47 +00:00
|
|
|
return $response->withJson(
|
2016-09-07 22:28:28 +00:00
|
|
|
['success' => false, 'error' => $e->getMessage()]
|
2016-03-29 23:39:47 +00:00
|
|
|
);
|
2015-10-31 10:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-29 21:32:36 +00:00
|
|
|
}
|