complete rearrangement an splitting into functions

master
Marc Wäckerlin 10 years ago
parent f6a262ff79
commit 33120e071b
  1. 401
      src/index.php

@ -1,215 +1,252 @@
<?php <?php
$DIR="/var/data/spielfilme";
$POSTER="/var/tmp/spielfilme";
$CONVERT=$POSTER;
$DIR="/var/data/spielfilme";
$POSTER="/var/tmp/spielfilme";
$CONVERT=$POSTER;
/** Check the HTTP Get Variables */
function checkGet($var) {
if (isset($_GET[$var]) && ereg('[^-_.A-Za-z0-9]', $_GET[$var])) {
?><html>
<body>
<p>wrong parameter <?php echo $var ?>:
"<code><?php echo htmlentities($_GET["img"]) ?></code>"
</p>
</body>
</html><?php
exit;
}
}
/** Check whether a specific file path exists */
function checkPath($path, $file) {
if (!is_file($path.'/'.$file)) {
?><html>
<body>
<p>File not found:
"<code><?php echo htmlentities($file) ?></code>"
</p>
</body>
</html><?php
exit;
}
return $path.'/'.$file;
}
/** Get Full Page URL */
function pageUrl() { function pageUrl() {
$pageURL = 'http'; $pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://"; $pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") { if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"]
.$_SERVER["REQUEST_URI"];
} else { } else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
} }
return $pageURL; return $pageURL;
} }
$URL=pageUrl();
/** Generate an Image Tag to a Poster Image.
@param $alt alternative image description attribute
@param $src should be an image path below $POSTER
@param $class optionall class attribute
Returns an image tag, if the image file exists, empty otherwise. */
function img($alt, $src, $class="image") { function img($alt, $src, $class="image") {
global $URL, $POSTER; global $POSTER;
$URL=pageUrl();
if (is_file($POSTER.'/'.$src.'.jpg')) if (is_file($POSTER.'/'.$src.'.jpg'))
return '<img alt="'.htmlentities($alt).'" src="'.htmlentities($URL.'?img='.$src).'.jpg" class="'.$class.'"/>'; return '<img alt="'.htmlentities($alt).'" src="'
.htmlentities($URL.'?img='.$src).'.jpg" class="'.$class.'"/>';
return '';
} }
if (isset($_GET["img"])) { /** Generate a source entry for a given file.
if (ereg('[^-_.A-Za-z0-9]', $_GET["img"])) { Returns source tag, if the image file exists, empty otherwise. */
?><html> function source($dir, $file, $mime=NULL) {
<body> $URL=pageUrl();
<p>Illegal Image name: "<code><?php echo htmlentities($_GET["img"]) ?></code>"</p> $path=$dir.'/'.$file;
</body> if (is_file($path)) {
</html> if (!$mime) $type=mime_content_type($path);
<?php else $type='video/'.$mime;
} elseif (is_file($POSTER.'/'.$_GET["img"])) { echo '<source src="'.$URL.'&play=1'.($mime?'&mime='.$mime:'')
header('Content-Type: image/jpeg'); .'" type="'.$type.'">';
readfile($POSTER.'/'.$_GET["img"]); } else {
exit; return '';
} else { }
?><html> }
<body>
<p>Image not found: "<code><?php echo htmlentities($_GET["img"]) ?></code>"</p>
</body>
</html>
<?php
}
} elseif (isset($_GET["video"])) {
if (ereg('[^-_.A-Za-z0-9]', $_GET["video"]) || isset($_GET["mime"]) && ereg('[^-_.A-Za-z0-9]', $_GET["mime"])) {
?><html>
<body>
<p>Illegal Video name: "<code><?php echo htmlentities($_GET["video"]) ?></code>"</p>
</body>
</html>
<?php
} elseif (is_file($DIR.'/'.$_GET["video"])) {
$mime = mime_content_type($DIR.'/'.$_GET["video"]);
if (isset($_GET["play"])) {
/* if (isset($_GET["convert"])) {
switch ($_GET["convert"]) {
case "webm":
header('Content-type: video/webm');
exec('ffmpeg -i "'.$DIR.'/'.$_GET["video"].'" -b 1500k -vcodec libvpx -acodec libvorbis -ab 160000 -f webm -g 30 - 2>/dev/null');
break;
case "mp4":
header('Content-type: video/mp4');
exec('ffmpeg -i "'.$DIR.'/'.$_GET["video"].'" -qscale 4 -vcodec libx264 -f mp4 - 2>/dev/null');
break;
}
} else { */
// Clears the cache and prevent unwanted output
ob_clean();
@ini_set('error_reporting', E_ALL & ~ E_NOTICE);
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 'Off');
if (isset($_GET["mime"]))
$file = $CONVERT.'/'.$_GET["mime"].'/'.preg_replace('/\.[^.]*$/', '.'.$_GET["mime"], $_GET["video"]);
else
$file = $DIR.'/'.$_GET["video"]; // The media file's location
$size = filesize($file); // The size of the file
$mime = mime_content_type($file);
// Send the content type header /** Output only a part of a file */
header('Content-type: ' . $mime); function range($path, $size, $mime) {
$ranges = array_map('intval', // Parse the parts into integer
// Check if it's a HTTP range request explode('-', // The range separator
if(isset($_SERVER['HTTP_RANGE'])){ // Skip the `bytes=` part of the header
// Parse the range header to get the byte offset substr($_SERVER['HTTP_RANGE'], 6)));
$ranges = array_map( // If the last range param is empty, it means the EOF (End of File)
'intval', // Parse the parts into integer if (!$ranges[1]) $ranges[1] = $size-1;
explode(
'-', // The range separator // Send the appropriate headers
substr($_SERVER['HTTP_RANGE'], 6) // Skip the `bytes=` part of the header header('HTTP/1.1 206 Partial Content');
) header('Accept-Ranges: bytes');
); header('Content-Length: '.($ranges[1]-$ranges[0])); // The size of the range
// If the last range param is empty, it means the EOF (End of File)
if(!$ranges[1]){
$ranges[1] = $size - 1;
}
// Send the appropriate headers
header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges: bytes');
header('Content-Length: ' . ($ranges[1] - $ranges[0])); // The size of the range
// Send the ranges we offered
header(
sprintf(
'Content-Range: bytes %d-%d/%d', // The header format
$ranges[0], // The start range
$ranges[1], // The end range
$size // Total size of the file
)
);
// It's time to output the file // Send the ranges we offered
$f = fopen($file, 'rb'); // Open the file in binary mode header(sprintf('Content-Range: bytes %d-%d/%d', // The header format
$chunkSize = 8192; // The size of each chunk to output $ranges[0], // The start range
$ranges[1], // The end range
// Seek to the requested start range $size)); // Total size of the file
fseek($f, $ranges[0]);
// It's time to output the file
// Start outputting the data $f = fopen($file, 'rb'); // Open the file in binary mode
while(true){ $chunkSize = 8192; // The size of each chunk to output
// Check if we have outputted all the data requested
if(ftell($f) >= $ranges[1]){
break;
}
// Output the data // Seek to the requested start range
echo fread($f, $chunkSize); fseek($f, $ranges[0]);
// Flush the buffer immediately // output the data
@ob_flush(); while (ftell($f) < $ranges[1]) {
flush(); echo fread($f, $chunkSize);
} @ob_flush();
flush();
}
} }
else {
// It's not a range request, output the file anyway /** Output a file */
function output($path) {
$size = filesize($path); // The size of the file
$mime = mime_content_type($path);
ob_clean();
@ini_set('error_reporting', E_ALL & ~ E_NOTICE);
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 'Off');
header('Content-type: ' . $mime);
if (isset($_SERVER['HTTP_RANGE'])) {
range($path, $size, $mime);
} else {
header('Content-Length: ' . $size); header('Content-Length: ' . $size);
// Read the file
@readfile($file); @readfile($file);
// and flush the buffer
@ob_flush(); @ob_flush();
flush(); flush();
}
exit;
} }
// }
} else {
?><!DOCTYPE html>
<html>
<body>
<p> /** Replace file extension */
<video controls> function ext($file, $newext) {
<source src="<?php echo $URL.'&play=1' ?>" type="<?php echo $mime ?>"> return preg_replace('/\.[^.]*$/', '.'.$newext, $file);
<?php /* <source src="<?php echo $URL.'&play=1&convert=webm' ?>" type="video/webm">*/ }
if ($d=opendir($CONVERT)) {
while (false!==($f=readdir($d))) if ($f!='.' && $f!=".." && is_dir($CONVERT.'/'.$f)) {
$p=$CONVERT.'/'.$f.'/'.preg_replace('/\.[^.]*$/', '.'.$f, $_GET['video']);
if (is_file($p))
echo '<source src="'.$URL.'&play=1&mime='.$f.'" type="video/'.$f.'">';
}
closedir($d);
}
/*<source src="<?php echo $URL.'&play=1&convert=mp4' ?>" type="video/mp4"> */ ?>
Your browser does not support the video tag.
</video><br/>
[<a href="<?php echo $URL.'&play=1' ?>">Click to Download or copy link to VLC player</a>]
</p>
</body> /** Show a video player for the given video */
</html> function player($video) {
<?php $URL=pageUrl();
} ?><!DOCTYPE html>
<html>
<body>
<p>
<video controls>
<?php
source($DIR, $video);
if ($d=opendir($CONVERT)) {
while (false!==($f=readdir($d)))
if ($f!='.' && $f!=".." && is_dir($CONVERT.'/'.$f)) {
source($CONVERT.'/'.$f, ext($video, $f), $f);
}
closedir($d);
}
?>
Your browser does not support the video tag.
</video>
<br/>
[<a href="<?php echo $URL.'&play=1' ?>">Click to Download or
copy link to VLC player</a>]
</p>
</body>
</html><?php
exit;
}
/** Show list and preview images of available videos */
function overview() {
?><html>
<head>
<title>Videofilme</title>
<style>
ul {
list-style-type: none;
}
ul li {
display: inline-block;
text-weight: bold;
border: .5ex groove green;
padding: 0;
margin: .5ex;
}
table {}
td.poster {}
img.poster {
height: 210px;
}
img.preview {
height: 70px;
}
</style>
</head>
<body>
<h1>Videofilme</h1>
<ul>
<?php
$URL=pageUrl();
if ($d=opendir($DIR)) {
while (false!==($f=readdir($d)))
if (is_file($DIR.'/'.$f)) {
$b=ereg_replace('\.[^.]*', '', $f);
$t=ereg_replace(' - ', '<br/>',
ereg_replace('[0-9]*-[^ ]*$', '',
ereg_replace('( - )?(part|[cC][dD]|[Tt][eE][iI][lL])[0-9]+', '',
ereg_replace('_', ' ', htmlentities($b)))));
if (preg_match('/.*(part|[cC][dD]|[Tt][eE][iI][lL])([0-9]+).*/',
$b, $m))
$t.='<br/>Teil '.$m[2];
$t=ereg_replace('<br/>$', '', ereg_replace('(<br/>)+', '<br/>', $t));
echo '<li><table><tr><th colspan="2" class="poster"><a href="'
.htmlentities($URL.'?video='.urlencode($f).'&play=1').'">'.$t
.'</a></td></tr><tr><td rowspan="3"><a href="'
.htmlentities($URL.'?video='.urlencode($f)).'">'
.img($b, $b, "poster").'</a></td><td>'
.img("$b preview 25%", "$b-25", "preview")
.'</td></tr><tr><td>'
.img("$b preview 50%", "$b-50", "preview").'</td></tr><tr><td>'
.img("$b preview 75%", "$b-75", "preview")
.'</td></tr></table></li>'."\n";
}
closedir($d);
}
?>
</ul>
</body>
</html><?php
exit;
}
/* Check all Parameters */
checkGet('img');
checkGet('video');
checkGet('mime');
/* Now $_GET Parameters are save */
if (isset($_GET["img"])) {
output(checkPath($POSTER, $_GET["img"]));
} elseif (isset($_GET["video"])) {
if (isset($_GET["play"])) {
if (isset($_GET["mime"])) {
output(checkPath($CONVERT.'/'.$_GET["mime"],
ext($_GET["video"], $_GET["mime"])));
} else { } else {
?><html> output(checkPath($DIR, $_GET["video"]));
<body>
<p>Video not found: "<code><?php echo htmlentities($_GET["video"]) ?></code>"</p>
</body>
</html>
<?php
} }
} else { } else {
?><html> player($_GET["video"]);
<head> }
<style> } else {
ul {list-style-type: none;} overview();
ul li {display: inline-block; text-weight: bold; border: .5ex groove green; padding: 0; margin: .5ex;}
table {}
td.poster {}
img.poster {height: 210px;}
img.preview {height: 70px;}
</style>
</head>
<body>
<h1>Videofilme</h1>
<ul>
<?php
if ($d=opendir($DIR)) {
while (false!==($f=readdir($d))) if (is_file($DIR.'/'.$f)) {
$b=ereg_replace('\.[^.]*', '', $f);
$t=ereg_replace(' - ', '<br/>', ereg_replace('[0-9]*-[^ ]*$', '', ereg_replace('( - )?(part|[cC][dD]|[Tt][eE][iI][lL])[0-9]+', '', ereg_replace('_', ' ', htmlentities($b)))));
if (preg_match('/.*(part|[cC][dD]|[Tt][eE][iI][lL])([0-9]+).*/', $b, $m))
$t.='<br/>Teil '.$m[2];
$t=ereg_replace('<br/>$', '', ereg_replace('(<br/>)+', '<br/>', $t));
echo '<li><table><tr><th colspan="2" class="poster"><a href="'.htmlentities($URL.'?video='.urlencode($f).'&play=1').'">'.$t.'</a></td></tr><tr><td rowspan="3"><a href="'.htmlentities($URL.'?video='.urlencode($f)).'">'.img($b, $b, "poster").'</a></td><td>'.img("$b preview 25%", "$b-25", "preview").'</td></tr><tr><td>'.img("$b preview 50%", "$b-50", "preview").'</td></tr><tr><td>'.img("$b preview 75%", "$b-75", "preview").'</td></tr></table></li>'."\n";
}
closedir($d);
} }
?>
</ul>
</body>
</html>
<?php } ?>

Loading…
Cancel
Save