From c5639ee890215e4e8e0f544821ea8d285ca58eb8 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 13 Sep 2014 22:26:58 +0200 Subject: init --- dl.php | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 dl.php (limited to 'dl.php') diff --git a/dl.php b/dl.php new file mode 100644 index 0000000..a3cdcdd --- /dev/null +++ b/dl.php @@ -0,0 +1,141 @@ +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; + +# 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 */ + # 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)){ + $ret = video_xt_audio($info[0], $CACHEDIR, $info[2]); + if(!$ret) + failure("Fetching the video failed.", true); + } + $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); + # 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"; + } else { + # 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; + + default: + failure("I don't understand your question.", true); + break; + +} -- cgit v1.2.3