Browse Source

[formats] Allow multiple enclosures

All formats now support multiple enclosures. RSS
will show a warning if more than one enclosure
is used since many feed reader don't support
multiple enclosures with RSS (also not clearly
specified in the specification)
logmanoriginal 7 years ago
parent
commit
72f40fbd75
3 changed files with 44 additions and 15 deletions
  1. 9 4
      formats/AtomFormat.php
  2. 16 6
      formats/HtmlFormat.php
  3. 19 5
      formats/MrssFormat.php

+ 9 - 4
formats/AtomFormat.php

@@ -27,9 +27,14 @@ class AtomFormat extends FormatAbstract{
 			$entryTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_ATOM, $item['timestamp'])) : '';
 			$entryContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : '';
 
-			$entryEnclosure = '';
-			if(isset($item['enclosure'])){
-				$entryEnclosure = '<link rel="enclosure" href="' . $this->xml_encode($item['enclosure']) . '"/>';
+			$entryEnclosures = '';
+			if(isset($item['enclosures'])){
+				foreach($item['enclosures'] as $enclosure){
+					$entryEnclosures .= '<link rel="enclosure" href="'
+					. $this->xml_encode($enclosure)
+					. '"/>'
+					. PHP_EOL;
+				}
 			}
 
 			$entries .= <<<EOD
@@ -43,7 +48,7 @@ class AtomFormat extends FormatAbstract{
 		<id>{$entryUri}</id>
 		<updated>{$entryTimestamp}</updated>
 		<content type="html">{$entryContent}</content>
-		{$entryEnclosure}
+		{$entryEnclosures}
 	</entry>
 
 EOD;

+ 16 - 6
formats/HtmlFormat.php

@@ -30,11 +30,21 @@ class HtmlFormat extends FormatAbstract {
 				. '</div>';
 			}
 
-			$entryEnclosure = '';
-			if(isset($item['enclosure'])){
-				$entryEnclosure = '<div class="enclosure"><a href="'
-				. $this->sanitizeHtml($item['enclosure'])
-				. '">enclosure</a><div>';
+			$entryEnclosures = '';
+			if(isset($item['enclosures'])){
+				$entryEnclosures = '<div class="attachments"><p>Attachments:</p>';
+
+				foreach($item['enclosures'] as $enclosure){
+					$url = $this->sanitizeHtml($enclosure);
+
+					$entryEnclosures .= '<li class="enclosure"><a href="'
+					. $url
+					. '">'
+					. substr($url, strrpos($url, '/') + 1)
+					. '</a></li>';
+				}
+
+				$entryEnclosures .= '</div>';
 			}
 
 			$entries .= <<<EOD
@@ -44,7 +54,7 @@ class HtmlFormat extends FormatAbstract {
 	{$entryTimestamp}
 	{$entryAuthor}
 	{$entryContent}
-	{$entryEnclosure}
+	{$entryEnclosures}
 </section>
 
 EOD;

+ 19 - 5
formats/MrssFormat.php

@@ -31,9 +31,23 @@ class MrssFormat extends FormatAbstract {
 			$itemTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_RFC2822, $item['timestamp'])) : '';
 			$itemContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : '';
 
-			$entryEnclosure = '';
-			if(isset($item['enclosure'])){
-				$entryEnclosure = '<enclosure url="' . $this->xml_encode($item['enclosure']) . '"/>';
+			$entryEnclosuresWarning = '';
+			$entryEnclosures = '';
+			if(isset($item['enclosures'])){
+				$entryEnclosures .= '<enclosure url="'
+				. $this->xml_encode($item['enclosures'][0])
+				. '"/>';
+
+				if(count($item['enclosures']) > 1){
+					$entryEnclosures .= PHP_EOL;
+					$entryEnclosuresWarning = '&lt;br&gt;Warning:
+Some media files might not be shown to you. Consider using the ATOM format instead!';
+					foreach($item['enclosures'] as $enclosure){
+						$entryEnclosures .= '<atom:link rel="enclosure" href="'
+						. $enclosure . '" />'
+						. PHP_EOL;
+					}
+				}
 			}
 
 			$items .= <<<EOD
@@ -43,9 +57,9 @@ class MrssFormat extends FormatAbstract {
 		<link>{$itemUri}</link>
 		<guid isPermaLink="true">{$itemUri}</guid>
 		<pubDate>{$itemTimestamp}</pubDate>
-		<description>{$itemContent}</description>
+		<description>{$itemContent}{$entryEnclosuresWarning}</description>
 		<author>{$itemAuthor}</author>
-		{$entryEnclosure}
+		{$entryEnclosures}
 	</item>
 
 EOD;