Add CORS header for firefox extension
[playVideoOnDreamboxProxy.git] / www / play.php
1 <?php
2 $youtubedlPath = '/usr/bin/youtube-dl';
3 $dreamboxUrl   = 'http://dreambox';
4
5 require_once __DIR__ . '/functions.php';
6 $cfgFile = __DIR__ . '/../data/config.php';
7 if (file_exists($cfgFile)) {
8     include $cfgFile;
9 }
10
11 //for the firefox extension
12 header('Access-Control-Allow-Origin: *');
13
14 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'GET') {
15     require __DIR__ . '/form.php';
16     exit();
17 }
18
19 set_error_handler('errorHandlerStore');
20
21 $pageUrl  = getPageUrl();
22 $json     = getYoutubeDlJson($pageUrl, $youtubedlPath);
23 $videoUrl = extractVideoUrlFromJson($json);
24 if (php_sapi_name() == 'cli') {
25     echo $videoUrl .  "\n";
26 } else {
27     header('Video-URL: ' . $videoUrl);
28 }
29 playVideoOnDreambox($videoUrl, $dreamboxUrl);
30
31
32 function errorInput($msg)
33 {
34     if (!headers_sent()) {
35         header('HTTP/1.0 400 Bad Request');
36         header('Content-type: text/plain');
37     }
38     echo $msg . "\n";
39     exit(1);
40 }
41
42 function errorOut($msg, $httpStatus = '500 Internal Server Error')
43 {
44     if (!headers_sent()) {
45         header('HTTP/1.0 ' . $httpStatus);
46         header('Content-type: text/plain');
47     }
48     echo $msg . "\n";
49     syslog(LOG_ERR, 'playVideoOnDreamboxProxy: ' . $httpStatus . ': ' . $msg);
50     exit(2);
51 }
52 ?>