update readme
[playVideoOnDreamboxProxy.git] / www / play.php
1 <?php
2 $youtubedlPath = '/usr/bin/youtube-dl';
3 $dreamboxHost  = 'dreambox';
4
5 $cfgFile = __DIR__ . '/../data/config.php';
6 if (file_exists($cfgFile)) {
7     include $cfgFile;
8 }
9
10 $pageUrl  = getPageUrl();
11 $videoUrl = extractVideoUrl($pageUrl, $youtubedlPath);
12 header('Video-URL: ' . $videoUrl);
13 playVideoOnDreambox($videoUrl, $dreamboxHost);
14
15 function getPageUrl()
16 {
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']);
21     }
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');
30     }
31     return $pageUrl;
32 }
33
34 function extractVideoUrl($pageUrl, $youtubedlPath)
35 {
36     $cmd = $youtubedlPath
37         . ' --quiet'
38         . ' --get-url'
39         . ' ' . escapeshellarg($pageUrl)
40         . ' 2>&1';
41
42     $lastLine = exec($cmd, $output, $exitCode);
43     if ($exitCode !== 0) {
44         if (strpos($lastLine, 'Unsupported URL') !== false) {
45             errorOut('Unsupported URL', '400 Unsupported URL (No video found)');
46         } else {
47             errorOut('youtube-dl error: ' . $lastLine);
48         }
49     }
50     return $lastLine;
51 }
52
53 function playVideoOnDreambox($videoUrl, $dreamboxHost)
54 {
55     ini_set('track_errors', 1);
56     $xml = file_get_contents('http://' . $dreamboxHost . '/web/session');
57     if ($xml === false) {
58         errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
59     }
60     $sx = simplexml_load_string($xml);
61     $token = (string) $sx;
62
63     $playUrl = 'http://' . $dreamboxHost
64         . '/web/mediaplayerplay'
65         . '?file=4097:0:1:0:0:0:0:0:0:0:'
66         . str_replace('%3A', '%253A', rawurlencode($videoUrl));
67
68     $ctx = stream_context_create(
69         array(
70             'http' => array(
71                 'method'  => 'POST',
72                 'header'  => 'Content-type: application/x-www-form-urlencoded',
73                 'content' => 'sessionid=' . $token,
74                 //'ignore_errors' => true
75             )
76         )
77     );
78     $ret = file_get_contents($playUrl, false, $ctx);
79     if ($ret !== false) {
80         header('HTTP/1.0 200 OK');
81         echo "Video play request sent to dreambox\n";
82         exit(0);
83     } else {
84         errorOut(
85             'Failed to send video play request to dreambox: ' . $php_errormsg
86         );
87     }
88 }
89
90 function errorInput($msg)
91 {
92     header('HTTP/1.0 400 Bad Request');
93     header('Content-type: text/plain');
94     echo $msg . "\n";
95     exit(1);
96 }
97
98 function errorOut($msg, $httpStatus = '500 Internal Server Error')
99 {
100     header('HTTP/1.0 ' . $httpStatus);
101     header('Content-type: text/plain');
102     echo $msg . "\n";
103     exit(2);
104 }
105 ?>