diff --git a/php/facebook.php b/php/facebook.php index f594560..42b9b70 100644 --- a/php/facebook.php +++ b/php/facebook.php @@ -10,10 +10,46 @@ require __DIR__ . '/common.php'; * notre propre domaine pour avoir une URL stable a partager. */ const FETCHRSS_URL = 'https://fetchrss.com/feed/1xABzW0ZB9CM1xAByX0rYAhW.atom'; +const SELF_URL = 'https://sillycone-village.be/silly-rss/facebook.php'; +/** + * Le flux FetchRSS brut ne passe pas le validateur W3C : + * - de chaque est l'identifiant numerique brut du post + * Facebook, alors que la spec Atom exige une URI complete. On reutilise + * le de l'entree (deja une URL complete vers le + * post) a la place. + * - Le pointe vers l'URL FetchRSS d'origine au lieu de + * celle ou ce flux est reellement servi. + */ function fetch_facebook_feed(): string { - return http_get(FETCHRSS_URL, ['Accept: application/atom+xml, application/xml']); + $raw = http_get(FETCHRSS_URL, ['Accept: application/atom+xml, application/xml']); + + $doc = new DOMDocument(); + if (!$doc->loadXML($raw)) { + return $raw; // XML illisible : on sert quand meme le brut plutot que de casser le flux + } + + $atomNs = 'http://www.w3.org/2005/Atom'; + $xpath = new DOMXPath($doc); + $xpath->registerNamespace('a', $atomNs); + + foreach ($xpath->query('/a:feed/a:link[@rel="self"]') as $selfLink) { + $selfLink->setAttribute('href', SELF_URL); + } + + foreach ($xpath->query('/a:feed/a:entry') as $entry) { + $idNode = $xpath->query('a:id', $entry)->item(0); + $linkNode = $xpath->query('a:link[@rel="alternate"]', $entry)->item(0); + if ($idNode && $linkNode) { + $href = $linkNode->getAttribute('href'); + if ($href && !preg_match('#^https?://#', $idNode->textContent)) { + $idNode->textContent = $href; + } + } + } + + return $doc->saveXML(); } // text/xml plutot que application/atom+xml : voir la note dans common.php