alltube/controllers/FrontController.php

268 lines
8.5 KiB
PHP
Raw Normal View History

2015-10-29 21:32:36 +00:00
<?php
2015-10-31 14:50:32 +00:00
/**
* FrontController class
*
* PHP Version 5.3.10
*
* @category Youtube-dl
* @package Youtubedl
* @author Pierre Rudloff <contact@rudloff.pro>
* @license GNU General Public License http://www.gnu.org/licenses/gpl.html
* @link http://rudloff.pro
* */
2015-10-29 21:32:36 +00:00
namespace Alltube\Controller;
2016-03-29 23:49:08 +00:00
2015-10-29 21:32:36 +00:00
use Alltube\VideoDownload;
2015-10-31 14:42:25 +00:00
use Alltube\Config;
2016-03-29 23:49:08 +00:00
2015-10-31 14:50:32 +00:00
/**
* Main controller
*
* PHP Version 5.3.10
*
* @category Youtube-dl
* @package Youtubedl
* @author Pierre Rudloff <contact@rudloff.pro>
* @license GNU General Public License http://www.gnu.org/licenses/gpl.html
* @link http://rudloff.pro
* */
class FrontController
{
2015-10-29 21:32:36 +00:00
2015-10-31 14:50:32 +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-03-29 23:49:08 +00:00
public static function index($request, $response)
2015-10-31 14:50:32 +00:00
{
2016-03-29 23:39:47 +00:00
global $container;
2015-10-31 14:42:25 +00:00
$config = Config::getInstance();
2016-03-29 23:39:47 +00:00
$container->view->render(
$response,
2015-10-29 21:32:36 +00:00
'head.tpl',
array(
'class'=>'index'
)
);
2016-03-29 23:39:47 +00:00
$container->view->render(
$response,
2015-10-29 21:32:36 +00:00
'header.tpl'
);
2016-03-29 23:39:47 +00:00
$container->view->render(
$response,
2015-10-29 21:32:36 +00:00
'index.tpl',
array(
2015-10-31 14:42:25 +00:00
'convert'=>$config->convert
2015-10-29 21:32:36 +00:00
)
);
2016-03-29 23:39:47 +00:00
$container->view->render($response, 'footer.tpl');
2015-10-29 21:32:36 +00:00
}
2015-10-31 14:50:32 +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-03-29 23:49:08 +00:00
public static function extractors($request, $response)
2015-10-31 14:50:32 +00:00
{
2016-03-29 23:39:47 +00:00
global $container;
$container->view->render(
$response,
2015-10-29 21:32:36 +00:00
'head.tpl',
array(
'class'=>'extractors'
)
);
2016-03-29 23:39:47 +00:00
$container->view->render($response, 'header.tpl');
$container->view->render($response, 'logo.tpl');
$container->view->render(
$response,
2015-10-29 21:32:36 +00:00
'extractors.tpl',
array(
'extractors'=>VideoDownload::listExtractors()
)
);
2016-03-29 23:39:47 +00:00
$container->view->render($response, 'footer.tpl');
2015-10-29 21:32:36 +00:00
}
2015-10-31 14:50:32 +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
*
2015-10-31 14:50:32 +00:00
* @return void
*/
2016-03-29 23:49:08 +00:00
public static function video($request, $response)
2015-10-31 14:50:32 +00:00
{
2016-03-29 23:39:47 +00:00
global $container;
2016-04-07 00:09:34 +00:00
$params = $request->getQueryParams();
2015-10-31 14:42:25 +00:00
$config = Config::getInstance();
2016-04-07 00:09:34 +00:00
if (isset($params["url"])) {
if (isset($params['audio'])) {
2015-10-29 21:32:36 +00:00
try {
2016-04-07 00:09:34 +00:00
$video = VideoDownload::getJSON($params["url"]);
2015-10-29 21:32:36 +00:00
//Vimeo needs a correct user-agent
$UA = VideoDownload::getUA();
ini_set(
'user_agent',
$UA
);
$url_info = parse_url($video->url);
if ($url_info['scheme'] == 'rtmp') {
ob_end_flush();
2015-10-29 21:32:36 +00:00
header(
'Content-Disposition: attachment; filename="'.
html_entity_decode(
pathinfo(
VideoDownload::getFilename(
$video->webpage_url
2016-03-29 23:49:08 +00:00
),
PATHINFO_FILENAME
).'.mp3',
ENT_COMPAT,
'ISO-8859-1'
2015-10-29 21:32:36 +00:00
).'"'
);
header("Content-Type: audio/mpeg");
passthru(
'/usr/bin/rtmpdump -q -r '.escapeshellarg($video->url).
2015-10-31 14:50:32 +00:00
' | '.$config->avconv.
' -v quiet -i - -f mp3 -vn pipe:1'
2015-10-29 21:32:36 +00:00
);
exit;
} else {
ob_end_flush();
2015-10-29 21:32:36 +00:00
header(
'Content-Disposition: attachment; filename="'.
html_entity_decode(
pathinfo(
VideoDownload::getFilename(
$video->webpage_url
2016-03-29 23:49:08 +00:00
),
PATHINFO_FILENAME
).'.mp3',
ENT_COMPAT,
'ISO-8859-1'
2015-10-29 21:32:36 +00:00
).'"'
);
header("Content-Type: audio/mpeg");
passthru(
2016-02-28 22:04:53 +00:00
'curl '.$config->curl_params.
' --user-agent '.escapeshellarg($UA).
2015-10-29 21:32:36 +00:00
' '.escapeshellarg($video->url).
2015-10-31 14:50:32 +00:00
' | '.$config->avconv.
' -v quiet -i - -f mp3 -vn pipe:1'
2015-10-29 21:32:36 +00:00
);
exit;
}
} catch (\Exception $e) {
$error = $e->getMessage();
}
} else {
try {
2016-04-07 00:09:34 +00:00
$video = VideoDownload::getJSON($params["url"]);
2016-03-29 23:39:47 +00:00
$container->view->render(
$response,
2015-10-29 21:32:36 +00:00
'head.tpl',
array(
'class'=>'video'
)
);
2016-03-29 23:39:47 +00:00
$container->view->render(
$response,
2015-10-29 21:32:36 +00:00
'video.tpl',
array(
'video'=>$video
)
);
2016-03-29 23:39:47 +00:00
$container->view->render($response, 'footer.tpl');
2015-10-29 21:32:36 +00:00
} catch (\Exception $e) {
$error = $e->getMessage();
}
}
}
if (isset($error)) {
2016-03-29 23:39:47 +00:00
$container->view->render(
$response,
2015-10-29 21:32:36 +00:00
'head.tpl',
array(
'class'=>'video'
)
);
2016-03-29 23:39:47 +00:00
$container->view->render(
$response,
2015-10-29 21:32:36 +00:00
'error.tpl',
array(
'errors'=>$error
)
);
2016-03-29 23:39:47 +00:00
$container->view->render($response, 'footer.tpl');
2015-10-29 21:32:36 +00:00
}
}
2015-10-31 10:48:14 +00:00
2015-10-31 14:50:32 +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
*
2015-10-31 14:50:32 +00:00
* @return void
*/
2016-03-29 23:49:08 +00:00
public static function redirect($request, $response)
2015-10-31 14:50:32 +00:00
{
2015-10-31 10:48:14 +00:00
global $app;
2016-04-07 00:09:34 +00:00
$params = $request->getQueryParams();
if (isset($params["url"])) {
2015-10-31 10:48:14 +00:00
try {
2016-04-07 00:09:34 +00:00
$format = isset($params["format"]) ? $params["format"] : 'best';
$video = VideoDownload::getJSON($params["url"], $format);
$client = new \GuzzleHttp\Client();
$stream = $client->request('GET', $video->url, array('stream'=>true));
$response = $response->withHeader('Content-Disposition', 'inline; filename="'.$video->_filename.'"');
$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());
}
return $response;
2015-10-31 10:48:14 +00:00
} catch (\Exception $e) {
2016-04-07 00:09:34 +00:00
$response->getBody()->write($e->getMessage().PHP_EOL);
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
/**
* 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
*
2015-10-31 14:50:32 +00:00
* @return void
*/
2016-03-29 23:49:08 +00:00
public static function json($request, $response)
2015-10-31 14:50:32 +00:00
{
2015-10-31 10:48:14 +00:00
global $app;
2016-04-07 00:09:34 +00:00
$params = $request->getQueryParams();
if (isset($params["url"])) {
2015-10-31 10:48:14 +00:00
try {
2016-04-07 00:09:34 +00:00
$video = VideoDownload::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(
array('success'=>false, 'error'=>$e->getMessage())
);
2015-10-31 10:48:14 +00:00
}
}
}
2015-10-29 21:32:36 +00:00
}