5 if (php_sapi_name() == 'cli') {
7 errorInput('No URL given as command line parameter');
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']);
16 $pageUrl = file_get_contents('php://input');
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');
29 function getYoutubeDlJson($pageUrl, $youtubedlPath)
32 . ' --no-playlist'//would otherwise cause multiple json blocks
35 . ' ' . escapeshellarg($pageUrl);
38 1 => ['pipe', 'w'],//stdout
39 2 => ['pipe', 'w'],//stderr
41 $proc = proc_open($cmd, $descriptors, $pipes);
42 if ($proc === false) {
43 errorOut('Error running youtube-dl');
45 $stdout = stream_get_contents($pipes[1]);
46 $stderr = stream_get_contents($pipes[2]);
48 $exitCode = proc_close($proc);
50 if ($exitCode === 0) {
51 //stdout contains the JSON data
55 if (strlen($stderr)) {
56 $lines = explode("\n", trim($stderr));
57 $lastLine = end($lines);
59 $lines = explode("\n", trim($stdout));
60 $lastLine = end($lines);
63 if ($exitCode === 127) {
65 'youtube-dl not found at ' . $youtubedlPath,
66 '500 youtube-dl not found'
68 } else if (strpos($lastLine, 'Unsupported URL') !== false) {
70 'Unsupported URL at ' . $pageUrl,
71 '406 Unsupported URL (No video found)'
75 errorOut('youtube-dl error: ' . $lastLine);
78 function extractVideoUrlFromJson($json)
80 $data = json_decode($json);
82 errorOut('Cannot decode JSON: ' . json_last_error_msg());
86 foreach ($data->formats as $format) {
87 if (strpos($format->format, 'hls') !== false) {
88 //dreambox 7080hd does not play hls files
91 if ($format->protocol == 'http_dash_segments') {
92 //split up into multiple small files
99 //use URL chosen by youtube-dl
105 'No video URL found',
106 '406 No video URL found'
112 function playVideoOnDreambox($videoUrl, $dreamboxUrl)
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
121 //dreambox web interface authentication has been enabled
123 'Error: Web interface authentication is required',
124 '401 Dreambox web authentication required'
128 'Failed to fetch dreambox session token: ' . $php_errormsg,
129 $code . ' ' . $message
133 $sx = simplexml_load_string($xml);
134 $token = (string) $sx;
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));
141 $ctx = stream_context_create(
145 'header' => 'Content-type: application/x-www-form-urlencoded',
146 'content' => 'sessionid=' . $token,
147 //'ignore_errors' => true
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');
156 echo "Video play request sent to dreambox\n";
160 'Failed to send video play request to dreambox: ' . $php_errormsg