summaryrefslogtreecommitdiff
path: root/dl.php
diff options
context:
space:
mode:
authorroot2014-09-13 22:26:58 +0200
committerroot2014-09-13 22:26:58 +0200
commitc5639ee890215e4e8e0f544821ea8d285ca58eb8 (patch)
tree29f685943c61c4d7ec0e376e485686e985b97065 /dl.php
parentf8c60cae423fc78ed21d17a9217716ccc1e6dab1 (diff)
downloadvideo-dl-c5639ee890215e4e8e0f544821ea8d285ca58eb8.tar.gz
init
Diffstat (limited to 'dl.php')
-rw-r--r--dl.php141
1 files changed, 141 insertions, 0 deletions
diff --git a/dl.php b/dl.php
new file mode 100644
index 0000000..a3cdcdd
--- /dev/null
+++ b/dl.php
@@ -0,0 +1,141 @@
+<?php
+
+require_once("func.php");
+require("config.php");
+
+if($_SERVER["REQUEST_METHOD"] != "GET")
+ failure("Request method not supported.", true);
+
+require_once("class/redis.php");
+@ini_set("memory_limit",'150M');
+set_time_limit(0);
+
+$db = new database($REDIS_DBNAME, $REDIS_CONNECT);
+$db->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 <u style='color:red;'>".$f."</u> bytes over the maximum",true);
+}
+
+# checks http status code
+if($curl[0]>= 400){
+ failure("Foreign server responded with a invalid status code. (<a href='https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#".$curl[0]."' target='__blank'>".$curl[0]."</a>).<br>That is not an error on our side and there is nothing we can do.<br><br>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].")<br>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;
+
+}