alltube/controllers/FrontController.php

311 lines
10 KiB
PHP
Raw Normal View History

2015-10-29 22:32:36 +01:00
<?php
2015-10-31 15:50:32 +01: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 22:32:36 +01:00
namespace Alltube\Controller;
2016-03-30 01:49:08 +02:00
2015-10-29 22:32:36 +01:00
use Alltube\VideoDownload;
2015-10-31 15:42:25 +01:00
use Alltube\Config;
use Symfony\Component\Process\ProcessBuilder;
use Chain\Chain;
use Slim\Http\Stream;
2016-03-30 01:49:08 +02:00
2015-10-31 15:50:32 +01: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 19:06:41 +02:00
public function __construct()
{
$this->config = Config::getInstance();
$this->download = new VideoDownload();
}
2015-10-29 22:32:36 +01:00
2015-10-31 15:50:32 +01:00
/**
* Display index page
2016-02-28 23:04:53 +01:00
*
2016-03-30 01:39:47 +02:00
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
2015-10-31 15:50:32 +01:00
* @return void
*/
2016-04-08 19:06:41 +02:00
public function index($request, $response)
2015-10-31 15:50:32 +01:00
{
2016-03-30 01:39:47 +02:00
global $container;
$container->view->render(
$response,
2015-10-29 22:32:36 +01:00
'head.tpl',
array(
'class'=>'index',
'description'=>'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.'
2015-10-29 22:32:36 +01:00
)
);
2016-03-30 01:39:47 +02:00
$container->view->render(
$response,
2015-10-29 22:32:36 +01:00
'header.tpl'
);
2016-03-30 01:39:47 +02:00
$container->view->render(
$response,
2015-10-29 22:32:36 +01:00
'index.tpl',
array(
2016-04-08 19:06:41 +02:00
'convert'=>$this->config->convert
2015-10-29 22:32:36 +01:00
)
);
2016-03-30 01:39:47 +02:00
$container->view->render($response, 'footer.tpl');
2015-10-29 22:32:36 +01:00
}
2015-10-31 15:50:32 +01:00
/**
* Display a list of extractors
2016-02-28 23:04:53 +01:00
*
2016-03-30 01:39:47 +02:00
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
2015-10-31 15:50:32 +01:00
* @return void
*/
2016-04-08 19:06:41 +02:00
public function extractors($request, $response)
2015-10-31 15:50:32 +01:00
{
2016-03-30 01:39:47 +02:00
global $container;
$container->view->render(
$response,
2015-10-29 22:32:36 +01:00
'head.tpl',
array(
'class'=>'extractors',
'title'=>'Supported websites',
'description'
=>'List of all supported websites from which Alltube Download can extract video or audio files'
2015-10-29 22:32:36 +01:00
)
);
2016-03-30 01:39:47 +02:00
$container->view->render($response, 'header.tpl');
$container->view->render($response, 'logo.tpl');
$container->view->render(
$response,
2015-10-29 22:32:36 +01:00
'extractors.tpl',
array(
2016-04-08 19:06:41 +02:00
'extractors'=>$this->download->listExtractors()
2015-10-29 22:32:36 +01:00
)
);
2016-03-30 01:39:47 +02:00
$container->view->render($response, 'footer.tpl');
2015-10-29 22:32:36 +01:00
}
2015-10-31 15:50:32 +01:00
/**
* Dislay information about the video
2016-02-28 23:04:53 +01:00
*
2016-03-30 01:39:47 +02:00
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
2015-10-31 15:50:32 +01:00
* @return void
*/
2016-04-08 19:06:41 +02:00
public function video($request, $response)
2015-10-31 15:50:32 +01:00
{
2016-03-30 01:39:47 +02:00
global $container;
2016-04-09 00:47:51 +02:00
$params = $request->getQueryParams();
2016-04-08 19:06:41 +02:00
$this->config = Config::getInstance();
2016-04-09 00:47:51 +02:00
if (isset($params["url"])) {
if (isset($params['audio'])) {
2015-10-29 22:32:36 +01:00
try {
2016-05-04 16:13:32 +02:00
$url = $this->download->getURL($params["url"], 'mp3[protocol^=http]');
2016-05-01 01:25:08 +02:00
return $response->withRedirect($url);
} catch (\Exception $e) {
$video = $this->download->getJSON($params["url"], 'bestaudio/best');
if (!shell_exec('which '.$this->config->avconv)) {
throw(new \Exception('Can\'t find avconv or ffmpeg'));
}
2016-05-01 01:25:08 +02:00
$avconvProc = ProcessBuilder::create(
array(
$this->config->avconv,
'-v', 'quiet',
'-i', '-',
'-f', 'mp3',
'-vn',
'pipe:1'
)
);
2015-10-29 22:32:36 +01:00
2016-05-01 01:25:08 +02:00
//Vimeo needs a correct user-agent
ini_set(
'user_agent',
$video->http_headers->{'User-Agent'}
);
2016-04-14 12:42:13 +02:00
2016-05-01 01:25:08 +02:00
$response = $response->withHeader(
'Content-Disposition',
'attachment; filename="'.
html_entity_decode(
pathinfo(
$video->_filename,
PATHINFO_FILENAME
).'.mp3',
ENT_COMPAT,
'ISO-8859-1'
).'"'
);
$response = $response->withHeader('Content-Type', 'audio/mpeg');
2016-04-14 12:42:13 +02:00
2016-05-01 01:25:08 +02:00
if (parse_url($video->url, PHP_URL_SCHEME) == 'rtmp') {
if (!shell_exec('which '.$this->config->rtmpdump)) {
throw(new \Exception('Can\'t find rtmpdump'));
}
2016-05-01 01:25:08 +02:00
$builder = new ProcessBuilder(
array(
$this->config->rtmpdump,
'-q',
'-r',
$video->url,
'--pageUrl', $video->webpage_url
)
);
if (isset($video->player_url)) {
$builder->add('--swfVfy');
$builder->add($video->player_url);
2016-04-10 19:41:25 +02:00
}
2016-05-01 01:25:08 +02:00
if (isset($video->flash_version)) {
$builder->add('--flashVer');
$builder->add($video->flash_version);
2016-04-30 03:22:46 +02:00
}
2016-05-01 01:25:08 +02:00
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);
} else {
if (!shell_exec('which curl')) {
throw(new \Exception('Can\'t find curl'));
}
2016-05-01 01:25:08 +02:00
$chain = new Chain(
ProcessBuilder::create(
array_merge(
array(
'curl',
'--silent',
'--user-agent', $video->http_headers->{'User-Agent'},
$video->url
),
$this->config->curl_params
)
)
);
$chain->add('|', $avconvProc);
2015-10-29 22:32:36 +01:00
}
2016-05-01 01:25:08 +02:00
if ($request->isGet()) {
$response = $response->withBody(new Stream(popen($chain->getProcess()->getCommandLine(), 'r')));
2016-05-01 01:25:08 +02:00
}
return $response;
2015-10-29 22:32:36 +01:00
}
} else {
2016-05-01 01:25:08 +02:00
$video = $this->download->getJSON($params["url"]);
$container->view->render(
$response,
'head.tpl',
array(
'class'=>'video',
'title'=>$video->title,
'description'=>'Download "'.$video->title.'" from '.$video->extractor_key
2016-05-01 01:25:08 +02:00
)
);
$container->view->render(
$response,
'video.tpl',
array(
'video'=>$video
)
);
$container->view->render($response, 'footer.tpl');
2015-10-29 22:32:36 +01:00
}
2016-06-09 21:15:44 +02:00
} else {
return $response->withRedirect($container->get('router')->pathFor('index'));
2015-10-29 22:32:36 +01:00
}
2016-05-01 01:25:08 +02:00
}
public function error($request, $response, $exception)
{
global $container;
$container->view->render(
$response,
'head.tpl',
array(
'class'=>'video',
'title'=>'Error'
2016-05-01 01:25:08 +02:00
)
);
$container->view->render(
$response,
'error.tpl',
array(
'errors'=>$exception->getMessage()
)
);
$container->view->render($response, 'footer.tpl');
return $response->withStatus(500);
2015-10-29 22:32:36 +01:00
}
2015-10-31 11:48:14 +01:00
2015-10-31 15:50:32 +01:00
/**
* Redirect to video file
2016-02-28 23:04:53 +01:00
*
2016-03-30 01:39:47 +02:00
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
2015-10-31 15:50:32 +01:00
* @return void
*/
2016-04-08 19:06:41 +02:00
public function redirect($request, $response)
2015-10-31 15:50:32 +01:00
{
2015-10-31 11:48:14 +01:00
global $app;
2016-04-09 00:47:51 +02:00
$params = $request->getQueryParams();
if (isset($params["url"])) {
2015-10-31 11:48:14 +01:00
try {
2016-04-10 21:42:38 +02:00
$url = $this->download->getURL($params["url"], $params["format"]);
2016-04-08 20:08:04 +02:00
return $response->withRedirect($url);
2015-10-31 11:48:14 +01:00
} catch (\Exception $e) {
2016-04-10 19:28:59 +02:00
$response->getBody()->write($e->getMessage());
2016-03-30 01:39:47 +02:00
return $response->withHeader('Content-Type', 'text/plain');
2015-10-31 11:48:14 +01:00
}
}
}
2015-10-31 15:50:32 +01:00
/**
* Output JSON info about the video
2016-02-28 23:04:53 +01:00
*
2016-03-30 01:39:47 +02:00
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
2015-10-31 15:50:32 +01:00
* @return void
*/
2016-04-08 19:06:41 +02:00
public function json($request, $response)
2015-10-31 15:50:32 +01:00
{
2015-10-31 11:48:14 +01:00
global $app;
2016-04-09 00:47:51 +02:00
$params = $request->getQueryParams();
if (isset($params["url"])) {
2015-10-31 11:48:14 +01:00
try {
2016-04-09 00:47:51 +02:00
$video = $this->download->getJSON($params["url"]);
2016-03-30 01:39:47 +02:00
return $response->withJson($video);
2015-10-31 11:48:14 +01:00
} catch (\Exception $e) {
2016-03-30 01:39:47 +02:00
return $response->withJson(
array('success'=>false, 'error'=>$e->getMessage())
);
2015-10-31 11:48:14 +01:00
}
}
}
2015-10-29 22:32:36 +01:00
}