2 $youtubedlPath = '/usr/bin/youtube-dl';
3 $dreamboxHost = 'dreambox';
5 $cfgFile = __DIR__ . '/../data/config.php';
6 if (file_exists($cfgFile)) {
10 $pageUrl = getPageUrl();
11 $videoUrl = extractVideoUrl($pageUrl, $youtubedlPath);
12 header('Video-URL: ' . $videoUrl);
13 playVideoOnDreambox($videoUrl, $dreamboxHost);
18 if (php_sapi_name() == 'cli') {
20 errorInput('No URL given as command line parameter');
24 if (!isset($_SERVER['CONTENT_TYPE'])) {
25 errorInput('Content type header missing');
26 } else if ($_SERVER['CONTENT_TYPE'] != 'text/plain') {
27 errorInput('Content type is not text/plain but ' . $_SERVER['CONTENT_TYPE']);
29 $pageUrl = file_get_contents('php://input');
31 $parts = parse_url($pageUrl);
32 if ($parts === false) {
33 errorInput('Invalid URL in POST data');
34 } else if (!isset($parts['scheme'])) {
35 errorInput('Invalid URL in POST data: No scheme');
36 } else if ($parts['scheme'] !== 'http' && $parts['scheme'] !== 'https') {
37 errorInput('Invalid URL in POST data: Non-HTTP scheme');
42 function extractVideoUrl($pageUrl, $youtubedlPath)
47 . ' ' . escapeshellarg($pageUrl)
50 $lastLine = exec($cmd, $output, $exitCode);
51 if ($exitCode !== 0) {
52 if (strpos($lastLine, 'Unsupported URL') !== false) {
54 'Unsupported URL at ' . $pageUrl,
55 '406 Unsupported URL (No video found)'
58 errorOut('youtube-dl error: ' . $lastLine);
62 $json = implode("\n", $output);
63 $data = json_decode($json);
66 foreach ($data->formats as $format) {
67 //dreambox 7080hd does not play hls files
68 if (strpos($format->format, 'hls') !== false) {
75 //use URL chosen by youtube-dl
81 function playVideoOnDreambox($videoUrl, $dreamboxHost)
83 ini_set('track_errors', 1);
84 $xml = @file_get_contents('http://' . $dreamboxHost . '/web/session');
86 errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
88 $sx = simplexml_load_string($xml);
89 $token = (string) $sx;
91 $playUrl = 'http://' . $dreamboxHost
92 . '/web/mediaplayerplay'
93 . '?file=4097:0:1:0:0:0:0:0:0:0:'
94 . str_replace('%3A', '%253A', rawurlencode($videoUrl));
96 $ctx = stream_context_create(
100 'header' => 'Content-type: application/x-www-form-urlencoded',
101 'content' => 'sessionid=' . $token,
102 //'ignore_errors' => true
106 $ret = file_get_contents($playUrl, false, $ctx);
107 if ($ret !== false) {
108 header('HTTP/1.0 200 OK');
109 echo "Video play request sent to dreambox\n";
113 'Failed to send video play request to dreambox: ' . $php_errormsg
118 function errorInput($msg)
120 header('HTTP/1.0 400 Bad Request');
121 header('Content-type: text/plain');
123 syslog(LOG_ERR, 'playVideoOnDreamboxProxy: ' . $httpStatus . ':' . $msg);
127 function errorOut($msg, $httpStatus = '500 Internal Server Error')
129 header('HTTP/1.0 ' . $httpStatus);
130 header('Content-type: text/plain');
132 syslog(LOG_ERR, 'playVideoOnDreamboxProxy: ' . $httpStatus . ': ' . $msg);