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 if (!isset($http_response_header)) {
119 'Error fetching dreambox web interface token: '
120 . $GLOBALS['lastError']
124 list($http, $code, $message) = explode(
125 ' ', $http_response_header[0], 3
128 //dreambox web interface authentication has been enabled
130 'Error: Web interface authentication is required',
131 '401 Dreambox web authentication required'
135 'Failed to fetch dreambox session token: ' . $php_errormsg,
136 $code . ' ' . $message
140 $sx = simplexml_load_string($xml);
141 $token = (string) $sx;
143 $playUrl = $dreamboxUrl
144 . '/web/mediaplayerplay'
145 . '?file=4097:0:1:0:0:0:0:0:0:0:'
146 . str_replace('%3A', '%253A', rawurlencode($videoUrl));
148 $ctx = stream_context_create(
152 'header' => 'Content-type: application/x-www-form-urlencoded',
153 'content' => 'sessionid=' . $token,
154 //'ignore_errors' => true
158 $ret = file_get_contents($playUrl, false, $ctx);
159 if ($ret !== false) {
160 if (php_sapi_name() != 'cli') {
161 header('HTTP/1.0 200 OK');
163 echo "Video play request sent to dreambox\n";
167 'Failed to send video play request to dreambox: ' . $php_errormsg
172 function errorHandlerStore($number, $message, $file, $line)
174 $GLOBALS['lastError'] = $message;
177 $GLOBALS['lastError'] = null;