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);
17 if (!isset($_SERVER['CONTENT_TYPE'])) {
18 errorInput('Content type header missing');
19 } else if ($_SERVER['CONTENT_TYPE'] != 'text/plain') {
20 errorInput('Content type is not text/plain but ' . $_SERVER['CONTENT_TYPE']);
22 $pageUrl = file_get_contents('php://input');
23 $parts = parse_url($pageUrl);
24 if ($parts === false) {
25 errorInput('Invalid URL in POST data');
26 } else if (!isset($parts['scheme'])) {
27 errorInput('Invalid URL in POST data: No scheme');
28 } else if ($parts['scheme'] !== 'http' && $parts['scheme'] !== 'https') {
29 errorInput('Invalid URL in POST data: Non-HTTP scheme');
34 function extractVideoUrl($pageUrl, $youtubedlPath)
39 . ' ' . escapeshellarg($pageUrl)
42 $lastLine = exec($cmd, $output, $exitCode);
43 if ($exitCode !== 0) {
44 errorOut('youtube-dl error: ' . $lastLine);
49 function playVideoOnDreambox($videoUrl, $dreamboxHost)
51 ini_set('track_errors', 1);
52 $xml = file_get_contents('http://' . $dreamboxHost . '/web/session');
54 errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
56 $sx = simplexml_load_string($xml);
57 $token = (string) $sx;
59 $playUrl = 'http://' . $dreamboxHost
60 . '/web/mediaplayerplay'
61 . '?file=4097:0:1:0:0:0:0:0:0:0:'
62 . str_replace('%3A', '%253A', rawurlencode($videoUrl));
64 $ctx = stream_context_create(
68 'header' => 'Content-type: application/x-www-form-urlencoded',
69 'content' => 'sessionid=' . $token,
70 //'ignore_errors' => true
74 $ret = file_get_contents($playUrl, false, $ctx);
76 header('HTTP/1.0 200 OK');
77 echo "Video play request sent to dreambox\n";
81 'Failed to send video play request to dreambox: ' . $php_errormsg
86 function errorInput($msg)
88 header('HTTP/1.0 400 Bad Request');
89 header('Content-type: text/plain');
94 function errorOut($msg)
96 header('HTTP/1.0 500 Internal Server Error');
97 header('Content-type: text/plain');