[SPIP] ~maj 3.0.10 --> 3.0.14
[lhc/web/www.git] / www / plugins-dist / sites / inc / feedfinder.php
1 <?php
2 /**
3 * adaptation en php de feedfinder.py :
4 *
5 * """Ultra-liberal feed finder, de Mark Pilgrim
6 * <http://diveintomark.org/projects/feed_finder/>
7 * Par: courcy.michael@wanadoo.fr
8 *
9 * adaptation en php, je ne reprends qu'une partie de cette algorithme
10 *
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"
16 *
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
20 *
21 * Je ne me preoccupe pas comme l'auteur de savoir si mes liens de feed sont sur le meme serveur ou pas
22 *
23 * exemple d'utilisation
24 *
25 * print_r (get_feed_from_url("http://willy.boerland.com/myblog/"));
26 *
27 * on obtient
28 *
29 * Array
30 * (
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
35 * )
36 */
37 if (!defined('_ECRIRE_INC_VERSION')) return;
38
39 $verif_complete = 0; //mettez le a 1 si vous voulez controler la validite des feed trouves mais le temps d'execution
40 //est alors plus long
41
42 /**
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
46 *
47 * @param string $url
48 * URL à analyser
49 * @return string|0
50 * Retourne son type (rss|atom|rdf) ou 0 si pas feed
51 */
52 function is_feed($url){
53
54 /**
55 * méthode SPIP
56 */
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];
62 switch ($type_feed) {
63 case "rss": return "rss";
64 case "feed": return "atom";
65 case "rdf": return "rdf";
66 }
67 }
68 return '';
69 }
70
71 $fp = @fopen($url, "r");
72 if (!$fp )
73 return 0;
74 //verifion la nature de ce fichier
75 while (!feof($fp)) {
76 $buffer = fgets($fp, 4096);
77 if (preg_match("/<(\w*) .*/", $buffer, $matches)){
78 //ici on detecte la premiere balise
79 $type_feed = $matches[1];
80 switch ($type_feed) {
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;
85 }
86 }
87 }
88 }
89
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 ************************************************************/
97
98 /**
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
105 * un tableau vide
106 *
107 * @param string $url
108 * L'URL à analyser
109 * @param $buffer
110 * @return array $feed_list
111 * Le tableau des feed trouvés dans la page
112 */
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);
118
119 include_spip("inc/filtres");
120
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){
126 if (
127 (strpos($link, "rss")
128 || strpos($link, "rdf")
129 || strpos($link, "atom")
130 || strpos($link, "xml"))
131 &&
132 (!strpos($link,'opensearch') && !strpos($link,'oembed'))
133 ){
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;
140 }
141 }
142 }
143 }
144 }
145 //extraction des <a>
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){
149 if (
150 (strpos($link, "rss")
151 || strpos($link, "rdf")
152 || strpos($link, "atom")
153 || strpos($link, "xml"))
154 &&
155 (!strpos($link,'opensearch') && !strpos($link,'oembed'))
156 ){
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;
163 }
164 }
165 }
166 }
167 }
168
169 // si c'est un site SPIP, tentons l'url connue
170 if (!count($feed_list)
171 AND (
172 strpos($url,"spip") OR stripos($buffer,"spip")
173 )){
174 $href = suivre_lien($url,"spip.php?page=backend");
175 if (is_feed($href))
176 $feed_list[] = $href;
177 }
178 return $feed_list;
179 }
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 *****************************************
186 Array
187 (
188 [0] => http://www.spip-contrib.net/backend.php
189 )
190 Array
191 (
192 [0] => http://www.liberation.fr/rss.php
193 )
194 Array
195 (
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/
201 )
202 Array
203 (
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
208 )
209 ************************************************************************/
210
211 ?>