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) {
53 errorOut('Unsupported URL', '400 Unsupported URL (No video found)');
55 errorOut('youtube-dl error: ' . $lastLine);
59 $json = implode("\n", $output);
60 $data = json_decode($json);
63 foreach ($data->formats as $format) {
64 //dreambox 7080hd does not play hls files
65 if (strpos($format->format, 'hls') !== false) {
72 //use URL chosen by youtube-dl
78 function playVideoOnDreambox($videoUrl, $dreamboxHost)
80 ini_set('track_errors', 1);
81 $xml = file_get_contents('http://' . $dreamboxHost . '/web/session');
83 errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
85 $sx = simplexml_load_string($xml);
86 $token = (string) $sx;
88 $playUrl = 'http://' . $dreamboxHost
89 . '/web/mediaplayerplay'
90 . '?file=4097:0:1:0:0:0:0:0:0:0:'
91 . str_replace('%3A', '%253A', rawurlencode($videoUrl));
93 $ctx = stream_context_create(
97 'header' => 'Content-type: application/x-www-form-urlencoded',
98 'content' => 'sessionid=' . $token,
99 //'ignore_errors' => true
103 $ret = file_get_contents($playUrl, false, $ctx);
104 if ($ret !== false) {
105 header('HTTP/1.0 200 OK');
106 echo "Video play request sent to dreambox\n";
110 'Failed to send video play request to dreambox: ' . $php_errormsg
115 function errorInput($msg)
117 header('HTTP/1.0 400 Bad Request');
118 header('Content-type: text/plain');
123 function errorOut($msg, $httpStatus = '500 Internal Server Error')
125 header('HTTP/1.0 ' . $httpStatus);
126 header('Content-type: text/plain');