move redirect resolving to separate script
authorChristian Weiske <cweiske@cweiske.de>
Wed, 25 Nov 2015 22:58:47 +0000 (23:58 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 25 Nov 2015 22:58:47 +0000 (23:58 +0100)
src/podcasts.php
www/deredirect.php [new file with mode: 0644]
www/index.php

index 68d133ce057f4dd34e581b39b852689bc7d7978d..3acccbbb2a1713136be5febc605b60f57746a753 100644 (file)
@@ -30,7 +30,7 @@ function sendPodcast($path)
 
         $listItems[] = getEpisodeItem(
             $title,
-            $host1 . 'play-url?url=' . urlencode($url),
+            $host1 . 'deredirect.php?url=' . urlencode($url),
             $desc,
             'MP3'
         );
diff --git a/www/deredirect.php b/www/deredirect.php
new file mode 100644 (file)
index 0000000..5b90e1d
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+/**
+ * Follow all location redirects of a given URL and redirect to the final URL.
+ *
+ * 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));
+
+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;
+}
+?>
\ No newline at end of file
index 8b803dbd1e84161465d3061f5e0a9b55f1a82b09..d64436cad0ce3ebbe6dbb85f92562496a4f9d2d5 100644 (file)
@@ -41,14 +41,6 @@ if (strtolower($fullUri) == '/setupapp/radio567/asp/browsexpa/loginxml.asp?token
 } 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, '/'));
@@ -244,19 +236,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)));