Add podcast proxy support for Martin master github/master
authorChristian Weiske <cweiske@cweiske.de>
Mon, 6 Jan 2020 20:28:36 +0000 (21:28 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 6 Jan 2020 20:28:36 +0000 (21:28 +0100)
data/config.php.dist
src/podcasts.php
www/proxy.php [new file with mode: 0644]

index 3294c49882eb91a6ad88519d8180f3e08d74fb8d..e5bdbc5ee9c9882b1d47018e806ced6442cc6e89 100644 (file)
@@ -21,4 +21,7 @@ $clientSupport = [
 
 //if this is set, transcode caching is activated
 $enableCache = false;
 
 //if this is set, transcode caching is activated
 $enableCache = false;
+
+//if this is true, podcast URLs are proxied
+$enablePodcastProxy = false;
 ?>
 ?>
index bb8f5ce7d675988482cbfe2a4620b216701f3f5e..707d3506f229d87468cacbb42d9a5cc471495481 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 function sendPodcast($path)
 {
 <?php
 function sendPodcast($path)
 {
-    global $varDir, $host1;
+    global $varDir, $host1, $enablePodcastProxy;
 
     $file = urldecode($path);
     if (strpos($file, '..') !== false) {
 
     $file = urldecode($path);
     if (strpos($file, '..') !== false) {
@@ -22,6 +22,11 @@ function sendPodcast($path)
     $sx = simplexml_load_file($cacheFile);
     $listItems = array();
 
     $sx = simplexml_load_file($cacheFile);
     $listItems = array();
 
+    $urlHandler = $host1 . 'deredirect.php';
+    if ($enablePodcastProxy) {
+        $urlHandler = $host1 . 'proxy.php';
+    }
+
     foreach ($sx->channel->item as $item) {
         $title = (string) $item->title;
         $desc = (string) $item->description;
     foreach ($sx->channel->item as $item) {
         $title = (string) $item->title;
         $desc = (string) $item->description;
@@ -29,7 +34,7 @@ function sendPodcast($path)
 
         $listItems[] = getEpisodeItem(
             $title,
 
         $listItems[] = getEpisodeItem(
             $title,
-            $host1 . 'deredirect.php?url=' . urlencode($url),
+            $urlHandler . '?url=' . urlencode($url),
             $desc,
             'MP3'
         );
             $desc,
             'MP3'
         );
@@ -65,4 +70,4 @@ function downloadIfNewer($url, $file)
     }
 }
 
     }
 }
 
-?>
\ No newline at end of file
+?>
diff --git a/www/proxy.php b/www/proxy.php
new file mode 100644 (file)
index 0000000..e220241
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+/**
+ * PHP proxy script that proxies the URL given in the "url" parameter.
+ *
+ * Sends all incoming headers, and also returns all remote headers.
+ * Streams the response, so that large responses should work fine.
+ *
+ * @author Christian Weiske <cweiske@cweiske.de>
+ */
+require_once __DIR__ . '/../data/config.php';
+if (!isset($enablePodcastProxy) || $enablePodcastProxy == false) {
+    header('HTTP/1.0 403 Forbidden');
+    echo "Proxying is not enabled in config.php\n";
+    exit(1);
+}
+
+if (!isset($_GET['url']) || $_GET['url'] == '') {
+    header('HTTP/1.0 400 Bad Request');
+    echo "url parameter missing\n";
+    exit(1);
+}
+if (substr($_GET['url'], 0, 7) != 'http://'
+    && substr($_GET['url'], 0, 8) != 'https://'
+) {
+    header('HTTP/1.0 400 Bad Request');
+    echo "Only http and https URLs supported\n";
+    exit(1);
+}
+
+$url = $_GET['url'];
+
+//send original http headers
+$headers = [];
+foreach (apache_request_headers() as $name => $value) {
+    if (strtolower($name) == 'host') {
+        continue;
+    }
+    $headers[] = $name . ': ' . $value;
+}
+$context = stream_context_create(
+    ['http' => ['header' => $headers, 'ignore_errors' => true]]
+);
+
+$fp = fopen($url, 'r', false, $context);
+if (!$fp) {
+    header('HTTP/1.0 400 Bad Request');
+    echo "Error fetching URL\n";
+    exit(1);
+}
+
+//send original headers
+if (is_array($http_response_header)) {
+    foreach ($http_response_header as $header) {
+        header($header);
+    }
+}
+
+//stream the data in 1kiB blocks
+while(!feof($fp)) {
+    echo fread($fp, 1024);
+    flush();
+}
+fclose($fp);
+?>