Use file executable bit for script detection.
[noxon-gateway.git] / www / index.php
index 2a380377f89e175fcdfccfd73cfea60e9b4d909d..1670931256fb90df76956f3b46b181dae5b9503c 100644 (file)
@@ -1,35 +1,12 @@
 <?php
-set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/../src/');
-$fullUri = $_SERVER['REQUEST_URI'];
-if (isset($_SERVER['REDIRECT_URL'])) {
-    $path    = $_SERVER['REDIRECT_URL'];
-} else {
-    $path = '/';
-}
-$dataDir = __DIR__ . '/../data/';
-$varDir  = realpath(__DIR__ . '/../var') . '/';
-$host1 = 'http://radio567.vtuner.com/';
-$host2 = 'http://radio5672.vtuner.com/';
-if ($_SERVER['HTTP_HOST'] !== '') {
-    $host1 = 'http://' . $_SERVER['HTTP_HOST'] . '/';
-    $host2 = 'http://' . $_SERVER['HTTP_HOST'] . '/';
-}
-$cfgFile = $dataDir . 'config.php';
-if (file_exists($cfgFile)) {
-    include $cfgFile;
-}
+require_once __DIR__ . '/../src/header.php';
 
-if (strtolower($fullUri) == '/setupapp/radio567/asp/browsexpa/loginxml.asp?token=0') {
-    //initial login for "internet radio" and podcasts
-    //lowercase 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 . 'initial-login.xml');
-    exit();
-} else if ($fullUri == '/RadioNativeLogin.php') {
-    //initial login for "My noxon"
-    //this one wants CamelCased tags
-    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"
@@ -40,21 +17,12 @@ 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');
 } else if ($path == '/RadioNativeFavorites.php') {
     //Favorites, defined via web interface
     sendMessage('Unsupported');
-} 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));
-    exit();
 }
 
 handleRequest(ltrim($path, '/'));
@@ -69,7 +37,11 @@ function handleRequest($path)
 
     if (substr($path, 0, 14) == 'internetradio/') {
         require_once 'mediatomb.php';
-        handleRequestMediatomb($path, 'internetradio/');
+        handleMediatomb('browse', $path, 'internetradio/');
+        return;
+    } else if (substr($path, 0, 11) == '.mt-single/') {
+        require_once 'mediatomb.php';
+        handleMediatomb('single', $path, '.mt-single/');
         return;
     }
 
@@ -88,7 +60,7 @@ function handleRequest($path)
         sendPodcast($path);
     } else if ($ext == 'txt') {
         sendTextFile($path);
-    } else if ($ext == 'sh') {
+    } else if (is_executable($fullPath)) {
         sendScript($path);
     } else {
         sendMessage('Unknown file type');
@@ -112,20 +84,25 @@ function sendDir($path)
     foreach ($entries as $entry) {
         $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 . '/');
+            $listItems[] = getDirItem($titleBase, $urlPath . '/');
         } else if ($ext == 'url') {
             //podcast
             ++$count;
-            $listItems[] = getPodcastItem(basename($entry, '.url'), $urlPath);
-        } else if (substr($entry, -8) == '.auto.sh') {
+            $listItems[] = getPodcastItem(basename($titleBase, '.url'), $urlPath);
+        } else if (is_executable($entry)
+            && strpos(basename($entry), '.auto') !== false
+        ) {
             //automatically execute script while listing this directory
             addScriptOutput($listItems, $entry);
-        } else if ($ext == 'txt' || $ext == 'sh') {
+        } else if ($ext == 'txt' || is_executable($entry)) {
             //plain text file
             ++$count;
-            $listItems[] = getDirItem(basename($entry, '.' . $ext), $urlPath);
+            $listItems[] = getDirItem(basename($titleBase, '.' . $ext), $urlPath);
         }
     }
     if (!$count) {
@@ -204,7 +181,7 @@ function getEpisodeItem($title, $fullUrl, $desc, $type)
     return '<Item>'
         . '<ItemType>ShowEpisode</ItemType>'
         . '<ShowEpisodeName>' . utf8_decode(htmlspecialchars($title)) . '</ShowEpisodeName>'
-        . '<ShowEpisodeURL>' . $fullUrl . '</ShowEpisodeURL>'
+        . '<ShowEpisodeURL>' . htmlspecialchars($fullUrl) . '</ShowEpisodeURL>'
         . '<ShowDesc>' . utf8_decode(htmlspecialchars($desc)) . '</ShowDesc>'
         . '<ShowMime>' . $type . '</ShowMime>'
         . '</Item>';
@@ -247,19 +224,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)));
@@ -285,7 +249,7 @@ function sendListItems($listItems)
     }
     $xml .= "</ListOfItems>\n";
 
-    header('Content-type: text/xml');
+    header('Content-type: text/xml; charset=iso-8859-1');
     echo $xml;
 }
 ?>