Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Create simple Application #224

Merged
merged 6 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion common.php → bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ function($custom = 'custom')

$container->set('template', $template);

// Create Application\ControllerFactory
$factory = new \YoutubeDownloader\Application\ControllerFactory;

$container->set('controller_factory', $factory);

// Create Toolkit
$container->set('toolkit', new \YoutubeDownloader\Toolkit);

return $container;
},
[getenv('CONFIG_ENV') ?: 'custom']
Expand All @@ -80,4 +88,4 @@ function($custom = 'custom')

date_default_timezone_set($container->get('config')->get('default_timezone'));

return $container;
return new \YoutubeDownloader\Application\App($container);
105 changes: 2 additions & 103 deletions download.php
Original file line number Diff line number Diff line change
@@ -1,105 +1,4 @@
<?php
$container = include_once('common.php');
$app = include_once('bootstrap.php');

$config = $container->get('config');
$template = $container->get('template');

// Check download token
if (empty($_GET['mime']) OR empty($_GET['token']))
{
echo $template->render('error.php', [
'error_message' => 'Invalid download token 8{',
]);
exit();
}

// Set operation params
$mime = filter_var($_GET['mime']);
$ext = str_replace(['/', 'x-'], '', strstr($mime, '/'));
$url = base64_decode(filter_var($_GET['token']));
$name = urldecode($_GET['title']) . '.' . $ext;

// Fetch and serve
if ($url)
{
global $config;
// prevent unauthorized download
if($config->get('VideoLinkMode') === "direct" and !isset($_GET['getmp3']))
{
echo $template->render('error.php', [
'error_message' => 'VideoLinkMode: proxy download not enabled',
]);
exit;
}
if($config->get('VideoLinkMode') !== "direct" and !isset($_GET['getmp3']) and !preg_match('@https://[^\.]+\.googlevideo.com/@', $url))
{
echo $template->render('error.php', [
'error_message' => 'unauthorized access (^_^)',
]);
exit;
}

// check if request for mp3 download
if(isset($_GET['getmp3']))
{
if($config->get('MP3Enable'))
{
$mp3_info = array();
$mp3_info = \YoutubeDownloader\YoutubeDownloader::getDownloadMP3($url, $config);
if(isset($mp3_info['mp3']))
{
$url = $mp3_info['mp3'];
}
else
{
if($config->get('debug') && isset($mp3_info['debugMessage']))
{
var_dump($mp3_info['debugMessage']);
}
echo $template->render('error.php', [
'error_message' => $mp3_info['message'],
]);
exit;
}
}
else
{
echo $template->render('error.php', [
'error_message' => 'Option for MP3 download is not enabled.',
]);
exit;
}
}

if(isset($mp3_info['mp3']))
{
$size = filesize($mp3_info['mp3']);
}
else
{
$size = \YoutubeDownloader\YoutubeDownloader::get_size($url, $config);
}

// Generate the server headers
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Content-Length: '.$size);
header('Pragma: no-cache');

if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
{
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
}

readfile($url);
exit;
}

// Not found
echo $template->render('error.php', [
'error_message' => 'File not found 8{',
]);
exit;
$app->runWithRoute('download');
59 changes: 3 additions & 56 deletions getimage.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,4 @@
<?PHP
$container = include_once('common.php');
<?php
$app = include_once('bootstrap.php');

$config = $container->get('config');
$template = $container->get('template');

if ( ! isset($_GET['videoid']) )
{
echo $template->render('error.php', [
'error_message' => 'No video id passed in',
]);
exit;
}

$my_id = \YoutubeDownloader\YoutubeDownloader::validateVideoId($_GET['videoid']);

if ( $my_id === null )
{
echo $template->render('error.php', [
'error_message' => 'Invalid video id passed in',
]);
exit;
}

$szName = 'default';

/**
* Player Background Thumbnail (480x360px) : http://i1.ytimg.com/vi/VIDEO_ID/0.jpg
* Normal Quality Thumbnail (120x90px) : http://i1.ytimg.com/vi/VIDEO_ID/default.jpg
* Medium Quality Thumbnail (320x180px) : http://i1.ytimg.com/vi/VIDEO_ID/mqdefault.jpg
* High Quality Thumbnail (480x360px) : http://i1.ytimg.com/vi/VIDEO_ID/hqdefault.jpg
* Start Thumbnail (120x90px) : http://i1.ytimg.com/vi/VIDEO_ID/1.jpg
* Middle Thumbnail (120x90px) : http://i1.ytimg.com/vi/VIDEO_ID/2.jpg
* End Thumbnail (120x90px) : http://i1.ytimg.com/vi/VIDEO_ID/3.jpg
*/
if (!empty($_GET['sz']))
{
$arg = $_GET['sz'];

switch ($arg)
{
case 'hd':
$szName = 'hqdefault';
break;
case 'sd':
$szName = 'default';
break;
default:
$szName = $arg;
break;
}
}

$thumbnail_url = "http://i1.ytimg.com/vi/" . $my_id . "/$szName.jpg"; // make image link

header("Content-Type: image/jpeg"); // set headers
readfile($thumbnail_url); // show image
$app->runWithRoute('image');
Loading