3e4e2beeeda9aa1d8c9fe76e27bc294f595d3bd0
[playVideoOnDreamboxProxy.git] / www / functions.php
1 <?php
2 function getPageUrl()
3 {
4     global $argv, $argc;
5
6     $dryRun = false;
7     if (php_sapi_name() == 'cli') {
8         if ($argc < 2) {
9             errorInput(
10                 "No URL given as command line parameter\n"
11                 . "Usage:\n"
12                 . " play.php [--dry-run|-n] <url>"
13             );
14         }
15         $options = [];
16         array_shift($argv);//remove script itself
17         foreach ($argv as $val) {
18             if ($val[0] == '-') {
19                 $options[$val] = true;
20             } else {
21                 $pageUrl = $val;
22             }
23         }
24         if (isset($options['--dry-run']) || isset($options['-n'])) {
25             $dryRun = true;
26         }
27     } else if (!isset($_SERVER['CONTENT_TYPE'])) {
28         errorInput('Content type header missing');
29     } else if ($_SERVER['CONTENT_TYPE'] == 'text/plain') {
30         //Android app
31         $pageUrl = file_get_contents('php://input');
32     } else if ($_SERVER['CONTENT_TYPE'] == 'application/x-www-form-urlencoded') {
33         //Web form
34         if (!isset($_POST['url'])) {
35             errorInput('"url" POST parameter missing');
36         }
37         $pageUrl = $_POST['url'];
38     } else {
39         errorInput('Content type is not text/plain but ' . $_SERVER['CONTENT_TYPE']);
40     }
41
42     $parts = parse_url($pageUrl);
43     if ($parts === false) {
44         errorInput('Invalid URL in POST data');
45     } else if (!isset($parts['scheme'])) {
46         errorInput('Invalid URL in POST data: No scheme');
47     } else if ($parts['scheme'] !== 'http' && $parts['scheme'] !== 'https') {
48         errorInput('Invalid URL in POST data: Non-HTTP scheme');
49     }
50     return [$pageUrl, $dryRun];
51 }
52
53 function getYoutubeDlJson($pageUrl, $youtubedlPath)
54 {
55     $cmd = $youtubedlPath
56         . ' --no-playlist'//would otherwise cause multiple json blocks
57         . ' --quiet'
58         . ' --dump-json'
59         . ' ' . escapeshellarg($pageUrl);
60
61     $descriptors = [
62         1 => ['pipe', 'w'],//stdout
63         2 => ['pipe', 'w'],//stderr
64     ];
65     $proc = proc_open($cmd, $descriptors, $pipes);
66     if ($proc === false) {
67         errorOut('Error running youtube-dl');
68     }
69     $stdout = stream_get_contents($pipes[1]);
70     $stderr = stream_get_contents($pipes[2]);
71
72     $exitCode = proc_close($proc);
73
74     if ($exitCode === 0) {
75         //stdout contains the JSON data
76         return $stdout;
77     }
78
79     if (strlen($stderr)) {
80         $lines = explode("\n", trim($stderr));
81         $lastLine = end($lines);
82     } else {
83         $lines = explode("\n", trim($stdout));
84         $lastLine = end($lines);
85     }
86
87     if ($exitCode === 127) {
88         errorOut(
89             'youtube-dl not found at ' . $youtubedlPath,
90             '500 youtube-dl not found'
91         );
92     } else if (strpos($lastLine, 'Unsupported URL') !== false) {
93         errorOut(
94             'Unsupported URL  at ' . $pageUrl,
95             '406 Unsupported URL (No video found)'
96         );
97     }
98
99     errorOut('youtube-dl error: ' . $lastLine);
100 }
101
102 function extractVideoUrlFromJson($json)
103 {
104     $data = json_decode($json);
105     if ($data === null) {
106         errorOut('Cannot decode JSON: ' . json_last_error_msg());
107     }
108
109     $safeFormats = [];
110     foreach ($data->formats as $format) {
111         if (strpos($format->format, 'hls') !== false) {
112             //dreambox 7080hd does not play hls files
113             continue;
114         }
115         if (strpos($format->format, 'vp9') !== false
116             || $format->vcodec == 'vp9'
117         ) {
118             //dreambox 7080hd does not play VP9 video streams
119             continue;
120         }
121         if ($format->protocol == 'http_dash_segments') {
122             //split up into multiple small files
123             continue;
124         }
125         if ($format->ext == 'flv') {
126             //Internal data flow error
127             continue;
128         }
129         $safeFormats[] = $format;
130     }
131
132     $url = null;
133
134     //filter: best quality
135     usort($safeFormats, function ($a, $b) {
136         if ((($a->acodec != 'none') + ($b->acodec != 'none')) == 1) {
137             return ($b->acodec != 'none') - ($a->acodec != 'none');
138         }
139         return ($b->quality ?? 0) - ($a->quality ?? 0);
140     });
141     foreach ($safeFormats as $format) {
142         $url = $format->url;
143         break;
144     }
145
146     if ($url === null) {
147         //use URL chosen by youtube-dl
148         $url = $data->url;
149     }
150
151     if ($url == '') {
152         errorOut(
153             'No video URL found',
154             '406 No video URL found'
155         );
156     }
157     return $url;
158 }
159
160 function playVideoOnDreambox($videoUrl, $dreamboxUrl)
161 {
162     ini_set('track_errors', 1);
163     $xml = @file_get_contents($dreamboxUrl . '/web/session');
164     if ($xml === false) {
165         if (!isset($http_response_header)) {
166             errorOut(
167                 'Error fetching dreambox web interface token: '
168                 . $GLOBALS['lastError']
169             );
170         }
171
172         list($http, $code, $message) = explode(
173             ' ', $http_response_header[0], 3
174         );
175         if ($code == 401) {
176             //dreambox web interface authentication has been enabled
177             errorOut(
178                 'Error: Web interface authentication is required',
179                 '401 Dreambox web authentication required'
180             );
181         } else {
182             errorOut(
183                 'Failed to fetch dreambox session token: ' . $php_errormsg,
184                 $code . ' ' . $message
185             );
186         }
187     }
188     $sx = simplexml_load_string($xml);
189     $token = (string) $sx;
190
191     $playUrl = $dreamboxUrl
192         . '/web/mediaplayerplay'
193         . '?file=4097:0:1:0:0:0:0:0:0:0:'
194         . str_replace('%3A', '%253A', rawurlencode($videoUrl));
195
196     $ctx = stream_context_create(
197         array(
198             'http' => array(
199                 'method'  => 'POST',
200                 'header'  => 'Content-type: application/x-www-form-urlencoded',
201                 'content' => 'sessionid=' . $token,
202                 //'ignore_errors' => true
203             )
204         )
205     );
206     $ret = file_get_contents($playUrl, false, $ctx);
207     if ($ret !== false) {
208         if (php_sapi_name() != 'cli') {
209             header('HTTP/1.0 200 OK');
210         }
211         echo "Video play request sent to dreambox\n";
212         exit(0);
213     } else {
214         errorOut(
215             'Failed to send video play request to dreambox: ' . $php_errormsg
216         );
217     }
218 }
219
220 function errorHandlerStore($number, $message, $file, $line)
221 {
222     $GLOBALS['lastError'] = $message;
223     return false;
224 }
225 $GLOBALS['lastError'] = null;
226 ?>