3 * adaptation en php de feedfinder.py :
5 * """Ultra-liberal feed finder, de Mark Pilgrim
6 * <http://diveintomark.org/projects/feed_finder/>
7 * Par: courcy.michael@wanadoo.fr
9 * adaptation en php, je ne reprends qu'une partie de cette algorithme
11 * 0) A chaque etape on verifie si les feed indiques sont reellement des feeds
12 * 1) Si l'uri passe est un feed on retourne le resultat tout simplement
13 * 2) Si le header de la page contient des balises LINK qui renvoient vers des feed on les retourne
14 * 3) on cherche les liens <a> qui se termine par ".rss", ".rdf", ".xml", ou ".atom"
15 * 4) on cherche les liens <a> contenant "rss", "rdf", "xml", ou "atom"
17 * j'integre pas l'interrogation avec xml_rpc de syndic8, mais on peut le faire assez facilement
18 * dans la phase de test sur differentes url je n'ai constate aucune diffrerence entre les reponses
19 * donnees par feedfinder.py et les miennes donc je ne suis pas sur de voir l'interet
21 * Je ne me preoccupe pas comme l'auteur de savoir si mes liens de feed sont sur le meme serveur ou pas
23 * exemple d'utilisation
25 * print_r (get_feed_from_url("http://willy.boerland.com/myblog/"));
31 * [0] => http://willy.boerland.com/myblog/atom/feed
32 * [1] => http://willy.boerland.com/myblog/blogapi/rsd
33 * [2] => http://willy.boerland.com/myblog/rss.xml
34 * [3] => http://willy.boerland.com/myblog/node/feed
37 if (!defined('_ECRIRE_INC_VERSION')) {
41 $verif_complete = 0; //mettez le a 1 si vous voulez controler la validite des feed trouves mais le temps d'execution
45 * une fonction qui permet de si un lien est un feed ou nom,
46 * si c'est un feed elle retourne son type, si c'est pas un feed elle retourne 0,
47 * cette verification est évidemment très très légère
52 * Retourne son type (rss|atom|rdf) ou 0 si pas feed
54 function is_feed($url) {
59 if (function_exists('recuperer_page')) {
60 $buffer = recuperer_page($url);
61 if (preg_match("/<(\w*) .*/", $buffer, $matches)) {
62 //ici on detecte la premiere balise
63 $type_feed = $matches[1];
77 $fp = @fopen
($url, "r");
81 //verifion la nature de ce fichier
83 $buffer = fgets($fp, 4096);
84 if (preg_match("/<(\w*) .*/", $buffer, $matches)) {
85 //ici on detecte la premiere balise
86 $type_feed = $matches[1];
109 /*****************test is_feed******************************
110 * echo is_feed("https://contrib.spip.net/spip.php?page=backend" _EXTENSIO_PHP") . "<br />"; //retourne rss
111 * echo is_feed("http://liberation.fr/rss.php") . "<br />"; //retourne rss
112 * echo is_feed("http://liberation.fr/rss.php") . "<br />"; //retourne rss
113 * echo is_feed("http://willy.boerland.com/myblog/atom/feed") //retourne atom
114 * echo is_feed("http://spip.net/") . "<br />"; //retoune 0
115 ************************************************************/
118 * fonction sans finesse mais efficace
119 * on parcourt ligne par ligne a la recherche de balise <a> ou <link>
120 * si dans le corps de celle-ci on trouve les mots rss, xml, atom ou rdf
121 * alors on recupere la valeur href='<url>', on adapte celle-ci si elle
122 * est relative et on verifie que c'est bien un feed si oui on l'ajoute
123 * au tableau des feed si on ne trouve rien ou si aucun feed est trouve on retourne
129 * @return array $feed_list
130 * Le tableau des feed trouvés dans la page
132 function get_feed_from_url($url, $buffer = false) {
133 global $verif_complete;
134 //j'ai prevenu ce sera pas fin
135 if (!preg_match("/^https?:\/\/.*/", $url)) {
136 $url = "http://" . $url;
139 $buffer = @file_get_contents
($url);
142 include_spip("inc/filtres");
144 $feed_list = array();
145 //extraction des <link>
146 if ($links = extraire_balises($buffer, "link")) {
147 //y a t-y rss atom rdf ou xml dans ces balises
148 foreach ($links as $link) {
150 (strpos($link, "rss")
151 ||
strpos($link, "rdf")
152 ||
strpos($link, "atom")
153 ||
strpos($link, "xml"))
155 (!strpos($link, 'opensearch') && !strpos($link, 'oembed'))
157 //voila un candidat on va extraire sa partie href et la placer dans notre tableau
158 if ($href = extraire_attribut($link, "href")) {
159 //on aura pris soin de verifier si ce lien est relatif d'en faire un absolu
160 $href = suivre_lien($url, $href);
161 if (!$verif_complete or is_feed($href)) {
162 $feed_list[] = $href;
169 if ($links = extraire_balises($buffer, "a")) {
170 //y a t-y rss atom rdf ou xml dans ces balises
171 foreach ($links as $link) {
173 (strpos($link, "rss")
174 ||
strpos($link, "rdf")
175 ||
strpos($link, "atom")
176 ||
strpos($link, "xml"))
178 (!strpos($link, 'opensearch') && !strpos($link, 'oembed'))
180 //voila un candidat on va extraire sa partie href et la placer dans notre tableau
181 if ($href = extraire_attribut($link, "href")) {
182 //on aura pris soin de verifier si ce lien est relatif d'en faire un absolu
183 $href = suivre_lien($url, $href);
184 if (!$verif_complete or is_feed($href)) {
185 $feed_list[] = $href;
192 // si c'est un site SPIP, tentons l'url connue
193 if (!count($feed_list)
195 strpos($url, "spip") or stripos($buffer, "spip")
198 $href = suivre_lien($url, "spip.php?page=backend");
199 if (is_feed($href)) {
200 $feed_list[] = $href;
207 /************************************ getFeed ****************************
208 * print_r (get_feed_from_url("contrib.spip.net"));
209 * print_r (get_feed_from_url("http://liberation.fr/"));
210 * print_r (get_feed_from_url("cnn.com"));
211 * print_r (get_feed_from_url("http://willy.boerland.com/myblog/"));
212 ***************************** Resultat *****************************************
215 * [0] => https://contrib.spip.net/backend.php
219 * [0] => http://www.liberation.fr/rss.php
223 * [0] => http://rss.cnn.com/rss/cnn_topstories.rss
224 * [1] => http://rss.cnn.com/rss/cnn_latest.rss
225 * [2] => http://www.cnn.com/services/rss/
226 * [3] => http://www.cnn.com/services/rss/
227 * [4] => http://www.cnn.com/services/rss/
231 * [0] => http://willy.boerland.com/myblog/atom/feed
232 * [1] => http://willy.boerland.com/myblog/blogapi/rsd
233 * [2] => http://willy.boerland.com/myblog/rss.xml
234 * [3] => http://willy.boerland.com/myblog/node/feed
236 ************************************************************************/;