catch Mediatomb exceptions, encode urls correctly
authorChristian Weiske <cweiske@cweiske.de>
Wed, 25 Nov 2015 06:36:08 +0000 (07:36 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 25 Nov 2015 06:36:08 +0000 (07:36 +0100)
src/mediatomb.php
www/index.php

index 40861ae4bda2b348090ebb116030212c48ab6c60..74b7f8479fdaa5d07f16cdf1ae621b8840355a72 100644 (file)
@@ -6,27 +6,38 @@ function handleRequestMediatomb($fullPath, $prefix)
     global $mediatomb;
 
     extract($mediatomb);
-    $smt = new Services_MediaTomb($user, $pass, $host, $port);
+    try {
+        $smt = new Services_MediaTomb($user, $pass, $host, $port);
 
-    $path = substr(urldecode($fullPath), strlen($prefix));
-    $container = $smt->getContainerByPath($path);
-    $listItems = array();
-    addPreviousItem($listItems, $fullPath);
+        $path = substr(urldecode($fullPath), strlen($prefix));
+        $container = $smt->getContainerByPath($path);
+        $listItems = array();
+        addPreviousItem($listItems, $fullPath);
 
-    foreach ($container->getContainers() as $subContainer) {
-        $listItems[] = getDirItem(
-            $subContainer->title,
-            $fullPath . urlencode($subContainer->title) . '/'
-        );
-    }
+        foreach ($container->getContainers() as $subContainer) {
+            $listItems[] = getDirItem(
+                $subContainer->title,
+                $fullPath . rawurlencode($subContainer->title) . '/'
+            );
+        }
 
-    foreach ($container->getItemIterator(false) as $item) {
-        $listItems[] = getEpisodeItem(
-            $item->title,
-            $item->url,
-            '',
-            'MP3'
-        );
+        foreach ($container->getItemIterator(false) as $item) {
+            $di = $item->getDetailedItem();
+            if ($di->mimetype !== 'audio/mpeg') {
+                //noxon iRadio cube does not want to play .ogg files
+                //FIXME: convert to mp3
+                //$di->location (on the server)
+            }
+            $listItems[] = getEpisodeItem(
+                $item->title,
+                $item->url,
+                '',
+                'MP3'
+            );
+        }
+    } catch (Exception $e) {
+        sendMessage('Mediatomb error: ' . $e->getMessage());
+        return;
     }
 
     sendListItems($listItems);
index 4db5f2ef36658b7268671ed2188012f66a7fc137..b7426326e1d46e8925c199bd822b0c39024e0bd3 100644 (file)
@@ -40,6 +40,7 @@ if (strtolower($fullUri) == '/setupapp/radio567/asp/browsexpa/loginxml.asp?token
 } else if ($path == '/RadioNative.php') {
     //"My Noxon"
     $path = '/mynoxon/';
+    $path = '/internetradio/';
 } else if ($path == '/setupapp/radio567/asp/BrowseXML/FavXML.asp') {
     //Internet Radio Station favorites favorited on device
     sendMessage('Unsupported');
@@ -91,6 +92,11 @@ function handleRequest($path)
     }
 }
 
+function pathEncode($urlPath)
+{
+    return str_replace('%2F', '/', rawurlencode($urlPath));
+}
+
 function sendDir($path)
 {
     global $varDir;
@@ -101,7 +107,7 @@ 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)));
         if (is_dir($entry)) {
             ++$count;
             $listItems[] = getDirItem(basename($entry), $urlPath . '/');
@@ -129,16 +135,20 @@ function sendTextFile($path)
 
     $lines = file($varDir . $path);
     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>';
 }