ffd6667c685d520d0b9d110e055d54134e40b5ba
[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
37     $descriptors = [
38         1 => ['pipe', 'w'],//stdout
39         2 => ['pipe', 'w'],//stderr
40     ];
41     $proc = proc_open($cmd, $descriptors, $pipes);
42     if ($proc === false) {
43         errorOut('Error running youtube-dl');
44     }
45     $stdout = stream_get_contents($pipes[1]);
46     $stderr = stream_get_contents($pipes[2]);
47
48     $exitCode = proc_close($proc);
49
50     if ($exitCode === 0) {
51         //stdout contains the JSON data
52         return $stdout;
53     }
54
55     if (strlen($stderr)) {
56         $lines = explode("\n", trim($stderr));
57         $lastLine = end($lines);
58     } else {
59         $lines = explode("\n", trim($stdout));
60         $lastLine = end($lines);
61     }
62
63     if ($exitCode === 127) {
64         errorOut(
65             'youtube-dl not found at ' . $youtubedlPath,
66             '500 youtube-dl not found'
67         );
68     } else if (strpos($lastLine, 'Unsupported URL') !== false) {
69         errorOut(
70             'Unsupported URL  at ' . $pageUrl,
71             '406 Unsupported URL (No video found)'
72         );
73     }
74
75     errorOut('youtube-dl error: ' . $lastLine);
76 }
77
78 function extractVideoUrlFromJson($json)
79 {
80     $data = json_decode($json);
81     if ($data === null) {
82         errorOut('Cannot decode JSON: ' . json_last_error_msg());
83     }
84
85     $url = null;
86     foreach ($data->formats as $format) {
87         if (strpos($format->format, 'hls') !== false) {
88             //dreambox 7080hd does not play hls files
89             continue;
90         }
91         if ($format->protocol == 'http_dash_segments') {
92             //split up into multiple small files
93             continue;
94         }
95         $url = $format->url;
96     }
97
98     if ($url === null) {
99         //use URL chosen by youtube-dl
100         $url = $data->url;
101     }
102
103     if ($url == '') {
104         errorOut(
105             'No video URL found',
106             '406 No video URL found'
107         );
108     }
109     return $url;
110 }
111
112 function playVideoOnDreambox($videoUrl, $dreamboxUrl)
113 {
114     ini_set('track_errors', 1);
115     $xml = @file_get_contents($dreamboxUrl . '/web/session');
116     if ($xml === false) {
117         list($http, $code, $message) = explode(
118             ' ', $http_response_header[0], 3
119         );
120         if ($code == 401) {
121             //dreambox web interface authentication has been enabled
122             errorOut(
123                 'Error: Web interface authentication is required',
124                 '401 Dreambox web authentication required'
125             );
126         } else {
127             errorOut(
128                 'Failed to fetch dreambox session token: ' . $php_errormsg,
129                 $code . ' ' . $message
130             );
131         }
132     }
133     $sx = simplexml_load_string($xml);
134     $token = (string) $sx;
135
136     $playUrl = $dreamboxUrl
137         . '/web/mediaplayerplay'
138         . '?file=4097:0:1:0:0:0:0:0:0:0:'
139         . str_replace('%3A', '%253A', rawurlencode($videoUrl));
140
141     $ctx = stream_context_create(
142         array(
143             'http' => array(
144                 'method'  => 'POST',
145                 'header'  => 'Content-type: application/x-www-form-urlencoded',
146                 'content' => 'sessionid=' . $token,
147                 //'ignore_errors' => true
148             )
149         )
150     );
151     $ret = file_get_contents($playUrl, false, $ctx);
152     if ($ret !== false) {
153         if (php_sapi_name() != 'cli') {
154             header('HTTP/1.0 200 OK');
155         }
156         echo "Video play request sent to dreambox\n";
157         exit(0);
158     } else {
159         errorOut(
160             'Failed to send video play request to dreambox: ' . $php_errormsg
161         );
162     }
163 }
164 ?>