open();
if(!$db->listExists($_GET["vid"])){
failure("No video information found.", true);
}
$info = $db->getAll($_GET["vid"]);
$db->close();
unset($db);
if(!$info){
failure("No video information found.", true);
}
if(empty($_GET["task"]))
failure("I don't understand your question.", true);
# array: 0 => http code; 1 => mime; 2 => size; 3 => url
$curl = curlInfo($info[3]);
if($_GET["task"] == "stream" || $_GET["task"] == "player")
$MAXSIZE = 200000000;
if($_GET["ddl"])
$MAXSIZE = 2000000000;
# checks file size
if($curl[2] > $MAXSIZE){ // 50M
$f=BytesHumanSize($curl[2] - $MAXSIZE);
failure("File too large. It's ".$f." bytes over the maximum",true);
}
# checks http status code
if($curl[0]>= 400){
failure("Foreign server responded with a invalid status code. (".$curl[0].").
That is not an error on our side and there is nothing we can do.
Maybe it's blocked in our country as well?", true);
}
/*
# checks file size
if($info[10] > 50000000){ // 50M
$f = $info[10]-50000000;
failure("File too large. It's ".$f." bytes over the maximum", true);
}
*/
# checks http status code
/*
if($info[8] >= 400)
failure("Failure. Foreign server responded with a invalid status code. (".$info[8].")
That is not an error on our side and there is nothing we can do.", true);
*/
switch($_GET["task"]){
case("ddl"): /* direct download */
if(preg_match("/^video.+/i", $curl[1])){
$mime = $curl[1];
$file = $curl[3];
$length = $curl[2];
} else {
$ret = video_dl($info[0], $CACHEDIR, $info[2]);
if(!$ch)
failure("Fetching the video failed.", true);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($CACHEDIR . "/" . $info[5]);
$length = filesize($CACHEDIR . "/" . $info[5]);
}
header("Content-Type: ".$mime);
header('Content-Disposition: attachment; filename="'.$info[5].'"');
header("Content-Length: ".$length);
readfile($file);
break;
case("xtau"): /* extracting audio */
ignore_user_abort(true);
# get file information
exec("youtube-dl -x --get-filename --output ".escapeshellarg($CACHEDIR."/".$info[2].".%(ext)s") . " ".escapeshellarg($info[0]), $output, $ret);
if($ret != 0)
failure("Fetching the video failed", true);
# file name with correct extension, but cache dir at first
$file = $output[0];
if(!file_exists($file) || !file_exists($CACHEDIR."/".$info[2].".audio.txt")){
$ret = video_xt_audio($info[0], $CACHEDIR, $info[2]);
if(!$ret)
failure("Fetching the video failed.", true);
else
unlink($CACHEDIR."/".$info[2].".audio.txt");
}
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($file);
# prepare dir name for regexp
$PREG_CACHEDIR = preg_quote($CACHEDIR, "/");
# replace the cache dir so that we send it to the browser as filename
$filename = preg_replace("/^".$PREG_CACHEDIR."\/".$info[2]."/", "", $file);
$filename = $info[1] . $filename;
header("Content-type: " . $mime);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-length: " . filesize($file));
readfile($file);
break;
case("stream"):
ignore_user_abort(true);
if(file_exists($CACHEDIR."/".$info[2].".txt"))
failure('Already doing the request.', false);
# send 'cached' response in case we already have the video on disk
if(file_exists($CACHEDIR."/".$info[2]) && !file_exists($CACHEDIR."/".$info[2].".txt")){
echo "cached";
exit;
}
# but in most cases we don't, so let's download it
$ret = video_dl($info[0], $CACHEDIR, $info[2]);
if(!$ret)
failure("Fetching the video failed.", false);
else
unlink($CACHEDIR."/".$info[2].".txt");
}
break;
case("player"):
# prints the player
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($CACHEDIR . "/" . $info[2]);
pr_player(htmlentities($CACHEDIR . "/" . $info[2]), htmlentities($info[4]), htmlentities($mime), htmlentities($info[1]), htmlentities($info[2]));
break;
case("convert"):
$dir = array_diff(scandir($CACHEDIR), array('.', '..'));
if(!in_array($_GET['vid'], $dir)){
failure('Video not found.' false);
}
if(file_exists($CACHEDIR."/".$info[2].".webm.txt"))
exit;
if(file_exists($CACHEDIR."/".$info[2]."webm")){
echo "cached";
exit;
}
if(!convert_video($info[2]))
failure("Converting video failed.", false);
else
unlink($CACHEDIR."/".$info[2].".webm.txt");
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($CACHEDIR . "/" . $info[2]);
pr_player(htmlentities($CACHEDIR . "/" . $info[2]), htmlentities($info[4]), htmlentities($mime), htmlentities($info[1]), htmlentities($info[2]), true);
break;
default:
failure("I don't understand your question.", true);
break;
}