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')) return;
39 $verif_complete = 0; //mettez le a 1 si vous voulez controler la validite des feed trouves mais le temps d'execution
43 * une fonction qui permet de si un lien est un feed ou nom,
44 * si c'est un feed elle retourne son type, si c'est pas un feed elle retourne 0,
45 * cette verification est évidemment très très légère
50 * Retourne son type (rss|atom|rdf) ou 0 si pas feed
52 function is_feed($url){
57 if (function_exists('recuperer_page')) {
58 $buffer = recuperer_page($url);
59 if (preg_match("/<(\w*) .*/", $buffer, $matches)){
60 //ici on detecte la premiere balise
61 $type_feed = $matches[1];
63 case "rss": return "rss";
64 case "feed": return "atom";
65 case "rdf": return "rdf";
71 $fp = @fopen
($url, "r");
74 //verifion la nature de ce fichier
76 $buffer = fgets($fp, 4096);
77 if (preg_match("/<(\w*) .*/", $buffer, $matches)){
78 //ici on detecte la premiere balise
79 $type_feed = $matches[1];
81 case "rss": fclose($fp); return "rss";
82 case "feed": fclose($fp); return "atom";
83 case "rdf": fclose($fp); return "rdf";
84 default : fclose($fp); return 0;
90 /*****************test is_feed******************************
91 echo is_feed("http://contrib.spip.net/spip.php?page=backend" _EXTENSIO_PHP") . "<br />"; //retourne rss
92 echo is_feed("http://liberation.fr/rss.php") . "<br />"; //retourne rss
93 echo is_feed("http://liberation.fr/rss.php") . "<br />"; //retourne rss
94 echo is_feed("http://willy.boerland.com/myblog/atom/feed") //retourne atom
95 echo is_feed("http://spip.net/") . "<br />"; //retoune 0
96 ************************************************************/
99 * fonction sans finesse mais efficace
100 * on parcourt ligne par ligne a la recherche de balise <a> ou <link>
101 * si dans le corps de celle-ci on trouve les mots rss, xml, atom ou rdf
102 * alors on recupere la valeur href='<url>', on adapte celle-ci si elle
103 * est relative et on verifie que c'est bien un feed si oui on l'ajoute
104 * au tableau des feed si on ne trouve rien ou si aucun feed est trouve on retourne
110 * @return array $feed_list
111 * Le tableau des feed trouvés dans la page
113 function get_feed_from_url($url, $buffer=false){
114 global $verif_complete;
115 //j'ai prevenu ce sera pas fin
116 if (!preg_match("/^http:\/\/.*/", $url)) $url = "http://www." . $url;
117 if (!$buffer) $buffer = @file_get_contents
($url);
119 include_spip("inc/filtres");
121 $feed_list = array();
122 //extraction des <link>
123 if ($links = extraire_balises($buffer,"link")){
124 //y a t-y rss atom rdf ou xml dans ces balises
125 foreach($links as $link){
127 (strpos($link, "rss")
128 ||
strpos($link, "rdf")
129 ||
strpos($link, "atom")
130 ||
strpos($link, "xml"))
132 (!strpos($link,'opensearch') && !strpos($link,'oembed'))
134 //voila un candidat on va extraire sa partie href et la placer dans notre tableau
135 if ($href = extraire_attribut($link,"href")){
136 //on aura pris soin de verifier si ce lien est relatif d'en faire un absolu
137 $href = suivre_lien($url, $href);
138 if(!$verif_complete OR is_feed($href)){
139 $feed_list[] = $href;
146 if ($links = extraire_balises($buffer,"a")){
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 // si c'est un site SPIP, tentons l'url connue
170 if (!count($feed_list)
172 strpos($url,"spip") OR stripos($buffer,"spip")
174 $href = suivre_lien($url,"spip.php?page=backend");
176 $feed_list[] = $href;
180 /************************************ getFeed ****************************
181 print_r (get_feed_from_url("contrib.spip.net"));
182 print_r (get_feed_from_url("http://liberation.fr/"));
183 print_r (get_feed_from_url("cnn.com"));
184 print_r (get_feed_from_url("http://willy.boerland.com/myblog/"));
185 ***************************** Resultat *****************************************
188 [0] => http://contrib.spip.net/backend.php
192 [0] => http://www.liberation.fr/rss.php
196 [0] => http://rss.cnn.com/rss/cnn_topstories.rss
197 [1] => http://rss.cnn.com/rss/cnn_latest.rss
198 [2] => http://www.cnn.com/services/rss/
199 [3] => http://www.cnn.com/services/rss/
200 [4] => http://www.cnn.com/services/rss/
204 [0] => http://willy.boerland.com/myblog/atom/feed
205 [1] => http://willy.boerland.com/myblog/blogapi/rsd
206 [2] => http://willy.boerland.com/myblog/rss.xml
207 [3] => http://willy.boerland.com/myblog/node/feed
209 ************************************************************************/