summaryrefslogtreecommitdiff
path: root/dl.php
blob: 0881018f38454447704368e0f162aab3da01fc17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?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;

if($_GET["ddl"])
	$MAXSIZE = 2000000000;

# 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 */
		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);
		# 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;
		
}