[SPIP][PLUGINS] v3.0-->v3.2
[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')) {
38 return;
39 }
40
41 $verif_complete = 0; //mettez le a 1 si vous voulez controler la validite des feed trouves mais le temps d'execution
42 //est alors plus long
43
44 /**
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
48 *
49 * @param string $url
50 * URL à analyser
51 * @return string|0
52 * Retourne son type (rss|atom|rdf) ou 0 si pas feed
53 */
54 function is_feed($url) {
55
56 /**
57 * méthode SPIP
58 */
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];
64 switch ($type_feed) {
65 case "rss":
66 return "rss";
67 case "feed":
68 return "atom";
69 case "rdf":
70 return "rdf";
71 }
72 }
73
74 return '';
75 }
76
77 $fp = @fopen($url, "r");
78 if (!$fp) {
79 return 0;
80 }
81 //verifion la nature de ce fichier
82 while (!feof($fp)) {
83 $buffer = fgets($fp, 4096);
84 if (preg_match("/<(\w*) .*/", $buffer, $matches)) {
85 //ici on detecte la premiere balise
86 $type_feed = $matches[1];
87 switch ($type_feed) {
88 case "rss":
89 fclose($fp);
90
91 return "rss";
92 case "feed":
93 fclose($fp);
94
95 return "atom";
96 case "rdf":
97 fclose($fp);
98
99 return "rdf";
100 default :
101 fclose($fp);
102
103 return 0;
104 }
105 }
106 }
107 }
108
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 ************************************************************/
116
117 /**
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
124 * un tableau vide
125 *
126 * @param string $url
127 * L'URL à analyser
128 * @param $buffer
129 * @return array $feed_list
130 * Le tableau des feed trouvés dans la page
131 */
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;
137 }
138 if (!$buffer) {
139 $buffer = @file_get_contents($url);
140 }
141
142 include_spip("inc/filtres");
143
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) {
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 //extraction des <a>
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) {
172 if (
173 (strpos($link, "rss")
174 || strpos($link, "rdf")
175 || strpos($link, "atom")
176 || strpos($link, "xml"))
177 &&
178 (!strpos($link, 'opensearch') && !strpos($link, 'oembed'))
179 ) {
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;
186 }
187 }
188 }
189 }
190 }
191
192 // si c'est un site SPIP, tentons l'url connue
193 if (!count($feed_list)
194 and (
195 strpos($url, "spip") or stripos($buffer, "spip")
196 )
197 ) {
198 $href = suivre_lien($url, "spip.php?page=backend");
199 if (is_feed($href)) {
200 $feed_list[] = $href;
201 }
202 }
203
204 return $feed_list;
205 }
206
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 *****************************************
213 * Array
214 * (
215 * [0] => https://contrib.spip.net/backend.php
216 * )
217 * Array
218 * (
219 * [0] => http://www.liberation.fr/rss.php
220 * )
221 * Array
222 * (
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/
228 * )
229 * Array
230 * (
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
235 * )
236 ************************************************************************/;