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-04-12 09:49:50 +00:00
|
|
|
use Symfony\Component\Process\ProcessBuilder;
|
|
|
|
use Chain\Chain;
|
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
|
|
|
|
{
|
2016-04-08 17:06:41 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->config = Config::getInstance();
|
|
|
|
$this->download = new VideoDownload();
|
|
|
|
}
|
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-04-08 17:06:41 +00:00
|
|
|
public function index($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'=>'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(
|
2016-04-08 17:06:41 +00:00
|
|
|
'convert'=>$this->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-04-08 17:06:41 +00:00
|
|
|
public 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(
|
2016-04-08 17:06:41 +00:00
|
|
|
'extractors'=>$this->download->listExtractors()
|
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
|
|
|
/**
|
|
|
|
* 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-04-08 17:06:41 +00:00
|
|
|
public 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();
|
2016-04-08 17:06:41 +00:00
|
|
|
$this->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-08 21:01:07 +00:00
|
|
|
try {
|
2016-04-10 23:22:01 +00:00
|
|
|
return $this->getStream($params["url"], 'bestaudio[protocol^=http]', $response, $request);
|
2016-04-08 21:01:07 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-04-12 09:49:50 +00:00
|
|
|
$video = $this->download->getJSON($params["url"], 'best');
|
|
|
|
|
|
|
|
$avconvProc = ProcessBuilder::create(
|
|
|
|
array(
|
|
|
|
$this->config->avconv,
|
|
|
|
'-v', 'quiet',
|
|
|
|
'-i', '-',
|
|
|
|
'-f', 'mp3',
|
|
|
|
'-vn',
|
|
|
|
'pipe:1'
|
|
|
|
)
|
|
|
|
);
|
2015-10-29 21:32:36 +00:00
|
|
|
|
2016-04-10 17:41:25 +00:00
|
|
|
//Vimeo needs a correct user-agent
|
|
|
|
ini_set(
|
|
|
|
'user_agent',
|
|
|
|
$video->http_headers->{'User-Agent'}
|
2015-10-29 21:32:36 +00:00
|
|
|
);
|
2016-04-10 17:49:44 +00:00
|
|
|
if (parse_url($video->url, PHP_URL_SCHEME) == 'rtmp') {
|
2016-04-12 09:49:50 +00:00
|
|
|
$builder = new ProcessBuilder(
|
|
|
|
array(
|
2016-04-12 19:13:43 +00:00
|
|
|
$this->config->rtmpdump,
|
2016-04-12 13:00:27 +00:00
|
|
|
'-q',
|
2016-04-12 09:49:50 +00:00
|
|
|
'-r',
|
|
|
|
$video->url,
|
|
|
|
'--pageUrl', $video->webpage_url
|
|
|
|
)
|
|
|
|
);
|
|
|
|
if (isset($video->player_url)) {
|
|
|
|
$builder->add('--swfVfy');
|
|
|
|
$builder->add($video->player_url);
|
|
|
|
}
|
|
|
|
if (isset($video->flash_version)) {
|
|
|
|
$builder->add('--flashVer');
|
|
|
|
$builder->add($video->flash_version);
|
|
|
|
}
|
|
|
|
if (isset($video->play_path)) {
|
|
|
|
$builder->add('--playpath');
|
|
|
|
$builder->add($video->play_path);
|
|
|
|
}
|
|
|
|
foreach ($video->rtmp_conn as $conn) {
|
|
|
|
$builder->add('--conn');
|
|
|
|
$builder->add($conn);
|
|
|
|
}
|
|
|
|
$chain = new Chain($builder->getProcess());
|
|
|
|
$chain->add('|', $avconvProc);
|
2016-04-08 21:01:07 +00:00
|
|
|
ob_end_flush();
|
|
|
|
header(
|
|
|
|
'Content-Disposition: attachment; filename="'.
|
|
|
|
html_entity_decode(
|
|
|
|
pathinfo(
|
|
|
|
$video->_filename,
|
|
|
|
PATHINFO_FILENAME
|
|
|
|
).'.mp3',
|
|
|
|
ENT_COMPAT,
|
|
|
|
'ISO-8859-1'
|
|
|
|
).'"'
|
|
|
|
);
|
|
|
|
header("Content-Type: audio/mpeg");
|
2016-04-12 09:49:50 +00:00
|
|
|
passthru($chain->getProcess()->getCommandLine());
|
2016-04-08 21:01:07 +00:00
|
|
|
exit;
|
|
|
|
} else {
|
2016-04-12 09:49:50 +00:00
|
|
|
$chain = new Chain(
|
|
|
|
ProcessBuilder::create(
|
|
|
|
array_merge(
|
|
|
|
array('curl'),
|
|
|
|
$this->config->curl_params,
|
|
|
|
array(
|
|
|
|
'--user-agent', $video->http_headers->{'User-Agent'},
|
|
|
|
$video->url
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$chain->add('|', $avconvProc);
|
2016-04-08 21:01:07 +00:00
|
|
|
ob_end_flush();
|
|
|
|
header(
|
|
|
|
'Content-Disposition: attachment; filename="'.
|
|
|
|
html_entity_decode(
|
|
|
|
pathinfo(
|
|
|
|
$video->_filename,
|
|
|
|
PATHINFO_FILENAME
|
|
|
|
).'.mp3',
|
|
|
|
ENT_COMPAT,
|
|
|
|
'ISO-8859-1'
|
|
|
|
).'"'
|
|
|
|
);
|
|
|
|
header("Content-Type: audio/mpeg");
|
2016-04-12 09:49:50 +00:00
|
|
|
passthru($chain->getProcess()->getCommandLine());
|
2016-04-08 21:01:07 +00:00
|
|
|
exit;
|
|
|
|
}
|
2015-10-29 21:32:36 +00:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$error = $e->getMessage();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
2016-04-08 19:11:43 +00:00
|
|
|
$video = $this->download->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
|
|
|
|
2016-04-08 21:01:07 +00:00
|
|
|
private function getStream($url, $format, $response, $request)
|
|
|
|
{
|
|
|
|
if (!isset($format)) {
|
|
|
|
$format = 'best';
|
|
|
|
}
|
|
|
|
$video = $this->download->getJSON($url, $format);
|
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
$stream = $client->request('GET', $video->url, array('stream'=>true));
|
2016-04-08 22:12:57 +00:00
|
|
|
$response = $response->withHeader('Content-Disposition', 'attachment; filename="'.$video->_filename.'"');
|
2016-04-08 21:01:07 +00:00
|
|
|
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
|
2016-04-08 22:12:57 +00:00
|
|
|
//$response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length'));
|
2016-04-08 21:01:07 +00:00
|
|
|
if ($request->isGet()) {
|
|
|
|
$response = $response->withBody($stream->getBody());
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
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-04-08 17:06:41 +00:00
|
|
|
public 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-08 21:01:07 +00:00
|
|
|
return $this->getStream($params["url"], $params["format"], $response, $request);
|
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-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-04-08 17:06:41 +00:00
|
|
|
public 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-08 19:11:43 +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(
|
|
|
|
array('success'=>false, 'error'=>$e->getMessage())
|
|
|
|
);
|
2015-10-31 10:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-29 21:32:36 +00:00
|
|
|
}
|