add dead simple transcoding with ffmpeg
[noxon-gateway.git] / www / index.php
index 3f06722943073ca3ad8f69ece56abb44ecc81708..65f6715fd26bfe6cf9f878941c714bbb9b953aeb 100644 (file)
@@ -8,45 +8,44 @@ if (isset($_SERVER['REDIRECT_URL'])) {
 }
 $dataDir = __DIR__ . '/../data/';
 $varDir  = realpath(__DIR__ . '/../var') . '/';
+$cacheDir = __DIR__ . '/../www/cache/';
 $host1 = 'http://radio567.vtuner.com/';
 $host2 = 'http://radio5672.vtuner.com/';
 if ($_SERVER['HTTP_HOST'] !== '') {
     $host1 = 'http://' . $_SERVER['HTTP_HOST'] . '/';
     $host2 = 'http://' . $_SERVER['HTTP_HOST'] . '/';
 }
+$cacheDirUrl = $host1 . 'cache/';
+$cfgFile = $dataDir . 'config.php';
+if (file_exists($cfgFile)) {
+    include $cfgFile;
+}
 
-if (strtolower($fullUri) == '/setupapp/radio567/asp/browsexpa/loginxml.asp?token=0') {
-    //initial login for "internet radio" and podcasts
-    //lowercase tags
-    header('Content-type: text/html');
-    readfile($dataDir . 'initial-login.xml');
-    exit();
-} else if ($fullUri == '/RadioNativeLogin.php') {
-    //initial login for "My noxon"
-    //this one wants CamelCased tags
+if (strtolower($fullUri) == '/setupapp/radio567/asp/browsexpa/loginxml.asp?token=0'
+    || $fullUri == '/RadioNativeLogin.php'
+) {
+    //initial login for "internet radio", podcasts and "my noxon"
     header('Content-type: text/html');
-    readfile($dataDir . 'login-mynoxon.xml');
+    readfile($dataDir . 'login-camelcase.xml');
     exit();
 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/LoginXML.asp') {
     //"Internet Radio"
-    $path = '/internetradio';
+    $path = '/internetradio/';
 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/navXML.asp') {
     //"Podcasts"
-    $path = '/podcasts';
+    $path = '/podcasts/';
 } else if ($path == '/RadioNative.php') {
     //"My Noxon"
-    $path = '/mynoxon';
+    $path = '/mynoxon/';
 } else if ($path == '/setupapp/radio567/asp/BrowseXML/FavXML.asp') {
     //Internet Radio Station favorites favorited on device
+    sendMessage('Unsupported');
 } else if ($path == '/RadioNativeFavorites.php') {
     //Favorites, defined via web interface
-} else if (substr($path, 0, 9) == '/play-url') {
-    //play a given URL, but first follow all redirects
-    //noxon iRadio Cube does not like too many redirections
-    // 3 redirects did not work.
-    $url = $_GET['url'];
-    header('HTTP/1.0 301 Moved Permanently');
-    header('Location: ' . getFinalUrl($url));
+    sendMessage('Unsupported');
+} else if ($path == '/transcode') {
+    require_once 'mediatomb.php';
+    transcodeMediatombItem($_GET['mtParentId'], $_GET['mtItemTitle']);
     exit();
 }
 
@@ -60,24 +59,39 @@ function handleRequest($path)
         return;
     }
 
+    if (substr($path, 0, 14) == 'internetradio/') {
+        require_once 'mediatomb.php';
+        handleRequestMediatomb($path, 'internetradio/');
+        return;
+    }
+
+
     $fullPath = $varDir . $path;
     if (!file_exists($fullPath)) {
         sendMessage('Not found: ' . $path);
         return;
     }
 
+    $ext = pathinfo($path, PATHINFO_EXTENSION);
     if (is_dir($fullPath)) {
         sendDir($path);
-    } else if (substr($path, -4) == '.url') {
+    } else if ($ext == 'url') {
         require_once 'podcasts.php';
         sendPodcast($path);
-    } else if (substr($path, -4) == '.txt') {
+    } else if ($ext == 'txt') {
         sendTextFile($path);
+    } else if ($ext == 'sh') {
+        sendScript($path);
     } else {
         sendMessage('Unknown file type');
     }
 }
 
+function pathEncode($urlPath)
+{
+    return str_replace('%2F', '/', rawurlencode($urlPath));
+}
+
 function sendDir($path)
 {
     global $varDir;
@@ -88,18 +102,25 @@ function sendDir($path)
     $entries = glob(str_replace('//', '/', $varDir . rtrim($path, '/') . '/*'));
     $count = 0;
     foreach ($entries as $entry) {
-        $urlPath = substr($entry, strlen($varDir));
+        $urlPath = pathEncode(substr($entry, strlen($varDir)));
+        $ext = pathinfo($entry, PATHINFO_EXTENSION);
+
+        $titleBase = basename($entry);
+        $titleBase = preg_replace('#^[0-9]+_#', '', $titleBase);
         if (is_dir($entry)) {
             ++$count;
-            $listItems[] = getDirItem(basename($entry), $urlPath . '/');
-        } else if (substr($entry, -4) == '.url') {
+            $listItems[] = getDirItem($titleBase, $urlPath . '/');
+        } else if ($ext == 'url') {
             //podcast
             ++$count;
-            $listItems[] = getPodcastItem(basename($entry, '.url'), $urlPath);
-        } else if (substr($entry, -4) == '.txt') {
+            $listItems[] = getPodcastItem(basename($titleBase, '.url'), $urlPath);
+        } else if (substr($entry, -8) == '.auto.sh') {
+            //automatically execute script while listing this directory
+            addScriptOutput($listItems, $entry);
+        } else if ($ext == 'txt' || $ext == 'sh') {
             //plain text file
             ++$count;
-            $listItems[] = getDirItem(basename($entry, '.txt'), $urlPath);
+            $listItems[] = getDirItem(basename($titleBase, '.' . $ext), $urlPath);
         }
     }
     if (!$count) {
@@ -108,6 +129,30 @@ function sendDir($path)
     sendListItems($listItems);
 }
 
+function sendScript($path)
+{
+    global $varDir;
+
+    $listItems = array();
+    addPreviousItem($listItems, $path);
+
+    $fullPath = $varDir . $path;
+    addScriptOutput($listItems, $fullPath);
+    sendListItems($listItems);
+}
+
+function addScriptOutput(&$listItems, $fullPath)
+{
+    exec($fullPath . ' 2>&1', $output, $retVal);
+
+    if ($retVal == 0) {
+        addTextLines($listItems, $output);
+    } else {
+        $listItems[] = getMessageItem('Error executing script');
+        addTextLines($listItems, $output);
+    }
+}
+
 function sendTextFile($path)
 {
     global $varDir;
@@ -115,17 +160,26 @@ function sendTextFile($path)
     addPreviousItem($listItems, $path);
 
     $lines = file($varDir . $path);
+    addTextLines($listItems, $lines);
+    sendListItems($listItems);
+}
+
+function addTextLines(&$listItems, $lines)
+{
     foreach ($lines as $line) {
-        $listItems[] = getDisplayItem($line);
+        $line = trim($line);
+        if ($line != '') {
+            $listItems[] = getDisplayItem($line);
+        }
     }
-    sendListItems($listItems);
 }
 
 function getDisplayItem($line)
 {
+    $line = preg_replace('#\s+#', ' ', $line);
     return '<Item>'
         . '<ItemType>Display</ItemType>'
-        . '<Display>' . utf8_decode(htmlspecialchars(trim($line))) . '</Display>'
+        . '<Display>' . utf8_decode(htmlspecialchars($line)) . '</Display>'
         . '</Item>';
 }
 
@@ -140,6 +194,17 @@ function getDirItem($title, $urlPath)
         . '</Item>';
 }
 
+function getEpisodeItem($title, $fullUrl, $desc, $type)
+{
+    return '<Item>'
+        . '<ItemType>ShowEpisode</ItemType>'
+        . '<ShowEpisodeName>' . utf8_decode(htmlspecialchars($title)) . '</ShowEpisodeName>'
+        . '<ShowEpisodeURL>' . htmlspecialchars($fullUrl) . '</ShowEpisodeURL>'
+        . '<ShowDesc>' . utf8_decode(htmlspecialchars($desc)) . '</ShowDesc>'
+        . '<ShowMime>' . $type . '</ShowMime>'
+        . '</Item>';
+}
+
 function getPodcastItem($title, $urlPath)
 {
     global $host1;
@@ -177,19 +242,6 @@ function addPreviousItem(&$listItems, $urlPath)
     $listItems[] = getPreviousItem($parentDir);
 }
 
-function getFinalUrl($url)
-{
-    $ctx = stream_context_set_default(
-        array('http' => array('method' => 'HEAD'))
-    );
-    //get_headers follows redirects automatically
-    $headers = get_headers($url, 1);
-    if ($headers !== false && isset($headers['Location'])) {
-        return end($headers['Location']);
-    }
-    return $url;
-}
-
 function sendMessage($msg)
 {
     sendListItems(array(getMessageItem($msg)));