Do not download playlist
[playVideoOnDreamboxProxy.git] / www / functions.php
1 <?php
2 function getPageUrl()
3 {
4     global $argv, $argc;
5     if (php_sapi_name() == 'cli') {
6         if ($argc < 2) {
7             errorInput('No URL given as command line parameter');
8         }
9         $pageUrl = $argv[1];
10     } else {
11         if (!isset($_SERVER['CONTENT_TYPE'])) {
12             errorInput('Content type header missing');
13         } else if ($_SERVER['CONTENT_TYPE'] != 'text/plain') {
14             errorInput('Content type is not text/plain but ' . $_SERVER['CONTENT_TYPE']);
15         }
16         $pageUrl = file_get_contents('php://input');
17     }
18     $parts = parse_url($pageUrl);
19     if ($parts === false) {
20         errorInput('Invalid URL in POST data');
21     } else if (!isset($parts['scheme'])) {
22         errorInput('Invalid URL in POST data: No scheme');
23     } else if ($parts['scheme'] !== 'http' && $parts['scheme'] !== 'https') {
24         errorInput('Invalid URL in POST data: Non-HTTP scheme');
25     }
26     return $pageUrl;
27 }
28
29 function getYoutubeDlJson($pageUrl, $youtubedlPath)
30 {
31     $cmd = $youtubedlPath
32         . ' --no-playlist'//would otherwise cause multiple json blocks
33         . ' --quiet'
34         . ' --dump-json'
35         . ' ' . escapeshellarg($pageUrl)
36         . ' 2> /dev/null';
37
38     $lastLine = exec($cmd, $output, $exitCode);
39     if ($exitCode !== 0) {
40         if (strpos($lastLine, 'Unsupported URL') !== false) {
41             errorOut(
42                 'Unsupported URL  at ' . $pageUrl,
43                 '406 Unsupported URL (No video found)'
44             );
45         } else {
46             errorOut('youtube-dl error: ' . $lastLine);
47         }
48     }
49
50     $json = implode("\n", $output);
51 }
52
53 function extractVideoUrlFromJson($json)
54 {
55     $data = json_decode($json);
56     if ($data === null) {
57         errorOut('Cannot decode JSON: ' . json_last_error_msg());
58     }
59
60     $url = null;
61     foreach ($data->formats as $format) {
62         if (strpos($format->format, 'hls') !== false) {
63             //dreambox 7080hd does not play hls files
64             continue;
65         }
66         if ($format->protocol == 'http_dash_segments') {
67             //split up into multiple small files
68             continue;
69         }
70         $url = $format->url;
71     }
72
73     if ($url === null) {
74         //use URL chosen by youtube-dl
75         $url = $data->url;
76     }
77
78     if ($url == '') {
79         errorOut(
80             'No video URL found',
81             '406 No video URL found'
82         );
83     }
84     return $url;
85 }
86
87 function playVideoOnDreambox($videoUrl, $dreamboxHost)
88 {
89     ini_set('track_errors', 1);
90     $xml = @file_get_contents('http://' . $dreamboxHost . '/web/session');
91     if ($xml === false) {
92         errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
93     }
94     $sx = simplexml_load_string($xml);
95     $token = (string) $sx;
96
97     $playUrl = 'http://' . $dreamboxHost
98         . '/web/mediaplayerplay'
99         . '?file=4097:0:1:0:0:0:0:0:0:0:'
100         . str_replace('%3A', '%253A', rawurlencode($videoUrl));
101
102     $ctx = stream_context_create(
103         array(
104             'http' => array(
105                 'method'  => 'POST',
106                 'header'  => 'Content-type: application/x-www-form-urlencoded',
107                 'content' => 'sessionid=' . $token,
108                 //'ignore_errors' => true
109             )
110         )
111     );
112     $ret = file_get_contents($playUrl, false, $ctx);
113     if ($ret !== false) {
114         header('HTTP/1.0 200 OK');
115         echo "Video play request sent to dreambox\n";
116         exit(0);
117     } else {
118         errorOut(
119             'Failed to send video play request to dreambox: ' . $php_errormsg
120         );
121     }
122 }
123 ?>