2015-10-29 21:32:36 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
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
|
|
|
|
2019-04-22 19:08:36 +00:00
|
|
|
use Alltube\Exception\PasswordException;
|
2017-05-30 21:30:21 +00:00
|
|
|
use Alltube\Locale;
|
2017-11-12 15:39:56 +00:00
|
|
|
use Alltube\LocaleManager;
|
2019-04-21 16:30:02 +00:00
|
|
|
use Alltube\Video;
|
2018-02-05 15:48:58 +00:00
|
|
|
use Exception;
|
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;
|
2018-02-05 15:48:58 +00:00
|
|
|
use Slim\Views\Smarty;
|
2019-11-27 20:22:23 +00:00
|
|
|
use Symfony\Component\Debug\ExceptionHandler;
|
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
|
|
|
*/
|
2019-04-22 14:05:58 +00:00
|
|
|
class FrontController extends BaseController
|
2015-10-31 14:50:32 +00:00
|
|
|
{
|
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-05-30 21:49:38 +00:00
|
|
|
/**
|
|
|
|
* LocaleManager instance.
|
|
|
|
*
|
|
|
|
* @var LocaleManager
|
|
|
|
*/
|
|
|
|
private $localeManager;
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2019-04-22 14:05:58 +00:00
|
|
|
* BaseController constructor.
|
2016-09-07 22:28:28 +00:00
|
|
|
*
|
2019-03-30 18:13:48 +00:00
|
|
|
* @param ContainerInterface $container Slim dependency container
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2019-04-22 15:00:51 +00:00
|
|
|
public function __construct(ContainerInterface $container)
|
2016-04-08 17:06:41 +00:00
|
|
|
{
|
2019-04-22 15:00:51 +00:00
|
|
|
parent::__construct($container);
|
2019-04-22 14:05:58 +00:00
|
|
|
|
2017-05-30 21:49:38 +00:00
|
|
|
$this->localeManager = $this->container->get('locale');
|
2019-04-22 14:05:58 +00:00
|
|
|
$this->view = $this->container->get('view');
|
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.'),
|
2019-10-03 19:24:12 +00:00
|
|
|
'domain' => $uri->getScheme() . '://' . $uri->getAuthority(),
|
2017-05-31 07:32:11 +00:00
|
|
|
'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'),
|
2019-10-03 19:24:12 +00:00
|
|
|
'description' => _('List of all supported websites from which Alltube Download ' .
|
2018-01-26 10:37:43 +00:00
|
|
|
'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
|
|
|
/**
|
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-22 13:20:05 +00:00
|
|
|
private function getInfoResponse(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 {
|
2019-04-22 13:20:05 +00:00
|
|
|
$template = 'info.tpl';
|
2017-04-24 23:53:38 +00:00
|
|
|
}
|
2018-01-26 10:37:43 +00:00
|
|
|
$title = _('Video download');
|
2019-10-03 19:24:12 +00:00
|
|
|
$description = _('Download video from ') . $this->video->extractor_key;
|
2019-04-21 16:30:02 +00:00
|
|
|
if (isset($this->video->title)) {
|
|
|
|
$title = $this->video->title;
|
2019-10-03 19:24:12 +00:00
|
|
|
$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-04-22 13:20:05 +00:00
|
|
|
'class' => 'info',
|
2019-03-26 23:25:02 +00:00
|
|
|
'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
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function info(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)) {
|
2019-04-22 14:05:58 +00:00
|
|
|
$this->video = new Video($url, $this->getFormat($request), $this->getPassword($request));
|
2019-04-21 16:30:02 +00:00
|
|
|
|
2019-04-21 21:16:27 +00:00
|
|
|
if ($this->config->convert && $request->getQueryParam('audio')) {
|
2019-04-22 13:20:05 +00:00
|
|
|
// We skip the info page and get directly to the download.
|
|
|
|
return $response->withRedirect(
|
2019-10-03 19:24:12 +00:00
|
|
|
$this->container->get('router')->pathFor('download') .
|
|
|
|
'?' . http_build_query($request->getQueryParams())
|
2019-04-22 13:20:05 +00:00
|
|
|
);
|
2015-10-29 21:32:36 +00:00
|
|
|
} else {
|
2019-04-22 13:20:05 +00:00
|
|
|
return $this->getInfoResponse($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
|
|
|
{
|
2019-11-27 20:22:23 +00:00
|
|
|
if ($this->config->debug) {
|
|
|
|
$handler = new ExceptionHandler();
|
|
|
|
$handler->handle($exception);
|
|
|
|
} else {
|
|
|
|
$this->view->render(
|
|
|
|
$response,
|
|
|
|
'error.tpl',
|
|
|
|
[
|
|
|
|
'config' => $this->config,
|
|
|
|
'errors' => $exception->getMessage(),
|
|
|
|
'class' => 'video',
|
|
|
|
'title' => _('Error'),
|
|
|
|
'canonical' => $this->getCanonicalUrl($request),
|
|
|
|
'locale' => $this->localeManager->getLocale(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
|
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)) {
|
2019-10-03 19:24:12 +00:00
|
|
|
$return .= '?' . $query;
|
2017-01-16 13:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2015-10-29 21:32:36 +00:00
|
|
|
}
|