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
82 '406 No video URL found'
88 function playVideoOnDreambox($videoUrl, $dreamboxHost)
90 ini_set('track_errors', 1);
91 $xml = @file_get_contents('http://' . $dreamboxHost . '/web/session');
93 errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
95 $sx = simplexml_load_string($xml);
96 $token = (string) $sx;
98 $playUrl = 'http://' . $dreamboxHost
99 . '/web/mediaplayerplay'
100 . '?file=4097:0:1:0:0:0:0:0:0:0:'
101 . str_replace('%3A', '%253A', rawurlencode($videoUrl));
103 $ctx = stream_context_create(
107 'header' => 'Content-type: application/x-www-form-urlencoded',
108 'content' => 'sessionid=' . $token,
109 //'ignore_errors' => true
113 $ret = file_get_contents($playUrl, false, $ctx);
114 if ($ret !== false) {
115 header('HTTP/1.0 200 OK');
116 echo "Video play request sent to dreambox\n";
120 'Failed to send video play request to dreambox: ' . $php_errormsg
125 function errorInput($msg)
127 header('HTTP/1.0 400 Bad Request');
128 header('Content-type: text/plain');
130 syslog(LOG_ERR, 'playVideoOnDreamboxProxy: ' . $httpStatus . ':' . $msg);
134 function errorOut($msg, $httpStatus = '500 Internal Server Error')
136 header('HTTP/1.0 ' . $httpStatus);
137 header('Content-type: text/plain');
139 syslog(LOG_ERR, 'playVideoOnDreamboxProxy: ' . $httpStatus . ': ' . $msg);