first version
[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         errorOut('youtube-dl error: ' . $lastLine);
45     }
46     return $lastLine;
47 }
48
49 function playVideoOnDreambox($videoUrl, $dreamboxHost)
50 {
51     ini_set('track_errors', 1);
52     $xml = file_get_contents('http://' . $dreamboxHost . '/web/session');
53     if ($xml === false) {
54         errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
55     }
56     $sx = simplexml_load_string($xml);
57     $token = (string) $sx;
58
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));
63
64     $ctx = stream_context_create(
65         array(
66             'http' => array(
67                 'method'  => 'POST',
68                 'header'  => 'Content-type: application/x-www-form-urlencoded',
69                 'content' => 'sessionid=' . $token,
70                 //'ignore_errors' => true
71             )
72         )
73     );
74     $ret = file_get_contents($playUrl, false, $ctx);
75     if ($ret !== false) {
76         header('HTTP/1.0 200 OK');
77         echo "Video play request sent to dreambox\n";
78         exit(0);
79     } else {
80         errorOut(
81             'Failed to send video play request to dreambox: ' . $php_errormsg
82         );
83     }
84 }
85
86 function errorInput($msg)
87 {
88     header('HTTP/1.0 400 Bad Request');
89     header('Content-type: text/plain');
90     echo $msg . "\n";
91     exit(1);
92 }
93
94 function errorOut($msg)
95 {
96     header('HTTP/1.0 500 Internal Server Error');
97     header('Content-type: text/plain');
98     echo $msg . "\n";
99     exit(2);
100 }
101 ?>