Better error reporting for errors when sending video to dreambox
[playVideoOnDreamboxProxy.git] / www / functions.php
index f6b5b1dc91e8f4974ff26be62c226238dd2fa0d1..b094e36e042c6b417551a0692aa80088957a59f5 100644 (file)
@@ -32,23 +32,47 @@ function getYoutubeDlJson($pageUrl, $youtubedlPath)
         . ' --no-playlist'//would otherwise cause multiple json blocks
         . ' --quiet'
         . ' --dump-json'
-        . ' ' . escapeshellarg($pageUrl)
-        . ' 2> /dev/null';
+        . ' ' . escapeshellarg($pageUrl);
 
-    $lastLine = exec($cmd, $output, $exitCode);
-    if ($exitCode !== 0) {
-        if (strpos($lastLine, 'Unsupported URL') !== false) {
-            errorOut(
-                'Unsupported URL  at ' . $pageUrl,
-                '406 Unsupported URL (No video found)'
-            );
-        } else {
-            errorOut('youtube-dl error: ' . $lastLine);
-        }
+    $descriptors = [
+        1 => ['pipe', 'w'],//stdout
+        2 => ['pipe', 'w'],//stderr
+    ];
+    $proc = proc_open($cmd, $descriptors, $pipes);
+    if ($proc === false) {
+        errorOut('Error running youtube-dl');
+    }
+    $stdout = stream_get_contents($pipes[1]);
+    $stderr = stream_get_contents($pipes[2]);
+
+    $exitCode = proc_close($proc);
+
+    if ($exitCode === 0) {
+        //stdout contains the JSON data
+        return $stdout;
+    }
+
+    if (strlen($stderr)) {
+        $lines = explode("\n", trim($stderr));
+        $lastLine = end($lines);
+    } else {
+        $lines = explode("\n", trim($stdout));
+        $lastLine = end($lines);
+    }
+
+    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)'
+        );
     }
 
-    $json = implode("\n", $output);
-    return $json;
+    errorOut('youtube-dl error: ' . $lastLine);
 }
 
 function extractVideoUrlFromJson($json)
@@ -85,17 +109,38 @@ function extractVideoUrlFromJson($json)
     return $url;
 }
 
-function playVideoOnDreambox($videoUrl, $dreamboxHost)
+function playVideoOnDreambox($videoUrl, $dreamboxUrl)
 {
     ini_set('track_errors', 1);
-    $xml = @file_get_contents('http://' . $dreamboxHost . '/web/session');
+    $xml = @file_get_contents($dreamboxUrl . '/web/session');
     if ($xml === false) {
-        errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
+        if (!isset($http_response_header)) {
+            errorOut(
+                'Error fetching dreambox web interface token: '
+                . $GLOBALS['lastError']
+            );
+        }
+
+        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;
 
-    $playUrl = 'http://' . $dreamboxHost
+    $playUrl = $dreamboxUrl
         . '/web/mediaplayerplay'
         . '?file=4097:0:1:0:0:0:0:0:0:0:'
         . str_replace('%3A', '%253A', rawurlencode($videoUrl));
@@ -112,7 +157,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 {
@@ -121,4 +168,11 @@ function playVideoOnDreambox($videoUrl, $dreamboxHost)
         );
     }
 }
+
+function errorHandlerStore($number, $message, $file, $line)
+{
+    $GLOBALS['lastError'] = $message;
+    return false;
+}
+$GLOBALS['lastError'] = null;
 ?>