From 16538e30f2ee79f15989ca3607466dd77c804458 Mon Sep 17 00:00:00 2001
From: Fil <fil@rezo.net>
Date: Wed, 20 Dec 2006 23:02:52 +0000
Subject: [PATCH] s'assurer de la compliance des microformats
 a[rel='enclosure'], en changeant length en title (idee d'ESJ) ; leve un bug
 de htmlpurifier qui tue a[rel]

---
 ecrire/inc/filtres.php | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/ecrire/inc/filtres.php b/ecrire/inc/filtres.php
index db45fb426b..1ca0564439 100644
--- a/ecrire/inc/filtres.php
+++ b/ecrire/inc/filtres.php
@@ -1394,31 +1394,35 @@ function afficher_tags($tags, $rels='tag,directory') {
 
 // Passe un <enclosure url="fichier" length="5588242" type="audio/mpeg"/>
 // au format microformat <a rel="enclosure" href="fichier" ...>fichier</a>
+// attention length="zz" devient title="zz", pour rester conforme
 // http://doc.spip.org/@enclosure2microformat
 function enclosure2microformat($e) {
-	$url = extraire_attribut($e, 'url');
-	$fichier = basename($url) OR $fichier;
-	$e = preg_replace(',<enclosure[[:space:]],iS','<a rel="enclosure" ', $e)
-		. $fichier.'</a>';
-	$e = vider_attribut($e, 'url');
-	$e = inserer_attribut($e, 'href', filtrer_entites($url));
-	$e = str_replace('/>', '>', $e);
-	return $e;
+	$url = filtrer_entites(extraire_attribut($e, 'url'));
+	$type = extraire_attribut($e, 'type');
+	$length = extraire_attribut($e, 'length');
+	$fichier = basename($url);
+	return '<a rel="enclosure"'
+		. ($url? ' href="'.htmlspecialchars($url).'"' : '')
+		. ($type? ' type="'.htmlspecialchars($type).'"' : '')
+		. ($length? ' title="'.htmlspecialchars($length).'"' : '')
+		. '>'.$fichier.'</a>';
 }
 // La fonction inverse
 // http://doc.spip.org/@microformat2enclosure
 function microformat2enclosure($tags) {
 	$enclosures = array();
 	foreach (extraire_tags($tags) as $e)
-	if (extraire_attribut($e, rel) == 'enclosure') {
-		$url = extraire_attribut($e, 'href');
-		$fichier = basename($url) OR $fichier;
-		$e = preg_replace(',<a[[:space:]],iS','<enclosure ', $e);
-		$e = preg_replace(',( ?/?)>.*,S',' />', $e);
-		$e = vider_attribut($e, 'href');
-		$e = vider_attribut($e, 'rel');
-		$e = inserer_attribut($e, 'url', filtrer_entites($url));
-		$enclosures[] = $e;
+	if (extraire_attribut($e, 'rel') == 'enclosure') {
+		$url = filtrer_entites(extraire_attribut($e, 'href'));
+		$type = extraire_attribut($e, 'type');
+		if (!$length = intval(extraire_attribut($e, 'title')))
+			$length = intval(extraire_attribut($e, 'length')); # vieux data
+		$fichier = basename($url);
+		$enclosures[] = '<enclosure'
+			. ($url? ' url="'.htmlspecialchars($url).'"' : '')
+			. ($type? ' type="'.htmlspecialchars($type).'"' : '')
+			. ($length? ' length="'.$length.'"' : '')
+			. ' />';
 	}
 	return join("\n", $enclosures);
 }
-- 
GitLab