Handle dreambox web interface authentication errors
[playVideoOnDreamboxProxy.git] / www / functions.php
index f0b67723c6d983c366cac89cbfea5382ff52e79d..36aacec10baca5f000e7d9c218bfb4f75467ead5 100644 (file)
@@ -29,6 +29,7 @@ function getPageUrl()
 function getYoutubeDlJson($pageUrl, $youtubedlPath)
 {
     $cmd = $youtubedlPath
+        . ' --no-playlist'//would otherwise cause multiple json blocks
         . ' --quiet'
         . ' --dump-json'
         . ' ' . escapeshellarg($pageUrl)
@@ -36,7 +37,12 @@ function getYoutubeDlJson($pageUrl, $youtubedlPath)
 
     $lastLine = exec($cmd, $output, $exitCode);
     if ($exitCode !== 0) {
-        if (strpos($lastLine, 'Unsupported URL') !== false) {
+        if ($exitCode === 127) {
+            errorOut(
+                'youtube-dl not found at ' . $youtubedlPath,
+                '500 youtube-dl not found'
+            );
+        } else if (strpos($lastLine, 'Unsupported URL') !== false) {
             errorOut(
                 'Unsupported URL  at ' . $pageUrl,
                 '406 Unsupported URL (No video found)'
@@ -47,6 +53,7 @@ function getYoutubeDlJson($pageUrl, $youtubedlPath)
     }
 
     $json = implode("\n", $output);
+    return $json;
 }
 
 function extractVideoUrlFromJson($json)
@@ -88,7 +95,21 @@ function playVideoOnDreambox($videoUrl, $dreamboxHost)
     ini_set('track_errors', 1);
     $xml = @file_get_contents('http://' . $dreamboxHost . '/web/session');
     if ($xml === false) {
-        errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
+        list($http, $code, $message) = explode(
+            ' ', $http_response_header[0], 3
+        );
+        if ($code == 401) {
+            //dreambox web interface authentication has been enabled
+            errorOut(
+                'Error: Web interface authentication is required',
+                '401 Dreambox web authentication required'
+            );
+        } else {
+            errorOut(
+                'Failed to fetch dreambox session token: ' . $php_errormsg,
+                $code . ' ' . $message
+            );
+        }
     }
     $sx = simplexml_load_string($xml);
     $token = (string) $sx;
@@ -110,7 +131,9 @@ function playVideoOnDreambox($videoUrl, $dreamboxHost)
     );
     $ret = file_get_contents($playUrl, false, $ctx);
     if ($ret !== false) {
-        header('HTTP/1.0 200 OK');
+        if (php_sapi_name() != 'cli') {
+            header('HTTP/1.0 200 OK');
+        }
         echo "Video play request sent to dreambox\n";
         exit(0);
     } else {