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