Move functions to separate file
[playVideoOnDreamboxProxy.git] / www / functions.php
1 <?php
2 function getPageUrl()
3 {
4     global $argv, $argc;
5     if (php_sapi_name() == 'cli') {
6         if ($argc < 2) {
7             errorInput('No URL given as command line parameter');
8         }
9         $pageUrl = $argv[1];
10     } else {
11         if (!isset($_SERVER['CONTENT_TYPE'])) {
12             errorInput('Content type header missing');
13         } else if ($_SERVER['CONTENT_TYPE'] != 'text/plain') {
14             errorInput('Content type is not text/plain but ' . $_SERVER['CONTENT_TYPE']);
15         }
16         $pageUrl = file_get_contents('php://input');
17     }
18     $parts = parse_url($pageUrl);
19     if ($parts === false) {
20         errorInput('Invalid URL in POST data');
21     } else if (!isset($parts['scheme'])) {
22         errorInput('Invalid URL in POST data: No scheme');
23     } else if ($parts['scheme'] !== 'http' && $parts['scheme'] !== 'https') {
24         errorInput('Invalid URL in POST data: Non-HTTP scheme');
25     }
26     return $pageUrl;
27 }
28
29 function getYoutubeDlJson($pageUrl, $youtubedlPath)
30 {
31     $cmd = $youtubedlPath
32         . ' --quiet'
33         . ' --dump-json'
34         . ' ' . escapeshellarg($pageUrl)
35         . ' 2> /dev/null';
36
37     $lastLine = exec($cmd, $output, $exitCode);
38     if ($exitCode !== 0) {
39         if (strpos($lastLine, 'Unsupported URL') !== false) {
40             errorOut(
41                 'Unsupported URL  at ' . $pageUrl,
42                 '406 Unsupported URL (No video found)'
43             );
44         } else {
45             errorOut('youtube-dl error: ' . $lastLine);
46         }
47     }
48
49     $json = implode("\n", $output);
50 }
51
52 function extractVideoUrlFromJson($json)
53 {
54     $data = json_decode($json);
55     if ($data === null) {
56         errorOut('Cannot decode JSON: ' . json_last_error_msg());
57     }
58
59     $url = null;
60     foreach ($data->formats as $format) {
61         if (strpos($format->format, 'hls') !== false) {
62             //dreambox 7080hd does not play hls files
63             continue;
64         }
65         if ($format->protocol == 'http_dash_segments') {
66             //split up into multiple small files
67             continue;
68         }
69         $url = $format->url;
70     }
71
72     if ($url === null) {
73         //use URL chosen by youtube-dl
74         $url = $data->url;
75     }
76
77     if ($url == '') {
78         errorOut(
79             'No video URL found',
80             '406 No video URL found'
81         );
82     }
83     return $url;
84 }
85
86 function playVideoOnDreambox($videoUrl, $dreamboxHost)
87 {
88     ini_set('track_errors', 1);
89     $xml = @file_get_contents('http://' . $dreamboxHost . '/web/session');
90     if ($xml === false) {
91         errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
92     }
93     $sx = simplexml_load_string($xml);
94     $token = (string) $sx;
95
96     $playUrl = 'http://' . $dreamboxHost
97         . '/web/mediaplayerplay'
98         . '?file=4097:0:1:0:0:0:0:0:0:0:'
99         . str_replace('%3A', '%253A', rawurlencode($videoUrl));
100
101     $ctx = stream_context_create(
102         array(
103             'http' => array(
104                 'method'  => 'POST',
105                 'header'  => 'Content-type: application/x-www-form-urlencoded',
106                 'content' => 'sessionid=' . $token,
107                 //'ignore_errors' => true
108             )
109         )
110     );
111     $ret = file_get_contents($playUrl, false, $ctx);
112     if ($ret !== false) {
113         header('HTTP/1.0 200 OK');
114         echo "Video play request sent to dreambox\n";
115         exit(0);
116     } else {
117         errorOut(
118             'Failed to send video play request to dreambox: ' . $php_errormsg
119         );
120     }
121 }
122 ?>