Add simple web interface to paste a page URL
[playVideoOnDreamboxProxy.git] / www / functions.php
index ffd6667c685d520d0b9d110e055d54134e40b5ba..6ad1044e27273a404c0ed571536eb184be80a219 100644 (file)
@@ -7,14 +7,21 @@ function getPageUrl()
             errorInput('No URL given as command line parameter');
         }
         $pageUrl = $argv[1];
-    } else {
-        if (!isset($_SERVER['CONTENT_TYPE'])) {
-            errorInput('Content type header missing');
-        } else if ($_SERVER['CONTENT_TYPE'] != 'text/plain') {
-            errorInput('Content type is not text/plain but ' . $_SERVER['CONTENT_TYPE']);
-        }
+    } else if (!isset($_SERVER['CONTENT_TYPE'])) {
+        errorInput('Content type header missing');
+    } else if ($_SERVER['CONTENT_TYPE'] == 'text/plain') {
+        //Android app
         $pageUrl = file_get_contents('php://input');
+    } else if ($_SERVER['CONTENT_TYPE'] == 'application/x-www-form-urlencoded') {
+        //Web form
+        if (!isset($_POST['url'])) {
+            errorInput('"url" POST parameter missing');
+        }
+        $pageUrl = $_POST['url'];
+    } else {
+        errorInput('Content type is not text/plain but ' . $_SERVER['CONTENT_TYPE']);
     }
+
     $parts = parse_url($pageUrl);
     if ($parts === false) {
         errorInput('Invalid URL in POST data');
@@ -114,6 +121,13 @@ function playVideoOnDreambox($videoUrl, $dreamboxUrl)
     ini_set('track_errors', 1);
     $xml = @file_get_contents($dreamboxUrl . '/web/session');
     if ($xml === false) {
+        if (!isset($http_response_header)) {
+            errorOut(
+                'Error fetching dreambox web interface token: '
+                . $GLOBALS['lastError']
+            );
+        }
+
         list($http, $code, $message) = explode(
             ' ', $http_response_header[0], 3
         );
@@ -161,4 +175,11 @@ function playVideoOnDreambox($videoUrl, $dreamboxUrl)
         );
     }
 }
+
+function errorHandlerStore($number, $message, $file, $line)
+{
+    $GLOBALS['lastError'] = $message;
+    return false;
+}
+$GLOBALS['lastError'] = null;
 ?>