PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/spip/ecrire/inc/feedfinder.php

https://github.com/eyeswebcrea/espace-couture-sittler.fr
PHP | 214 lines | 85 code | 15 blank | 114 comment | 29 complexity | b522b21d0cd2bf3edd9a4342849727bd MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. if (!defined('_ECRIRE_INC_VERSION')) return;
  3. /**********************************
  4. 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
  8. adaptation en php, je ne reprends qu'une partie de cette algorithme
  9. 0) A chaque etape on verifie si les feed indiques sont reellement des feeds
  10. 1) Si l'uri passe est un feed on retourne le resultat tout simplement
  11. 2) Si le header de la page contient des balises LINK qui renvoient vers des feed on les retourne
  12. 3) on cherche les liens <a> qui se termine par ".rss", ".rdf", ".xml", ou ".atom"
  13. 4) on cherche les liens <a> contenant "rss", "rdf", "xml", ou "atom"
  14. j'integre pas l'interrogation avec xml_rpc de syndic8, mais on peut le faire assez facilement
  15. dans la phase de test sur differentes url je n'ai constate aucune diffrerence entre les reponses
  16. donnees par feedfinder.py et les miennes donc je ne suis pas sur de voir l'interet
  17. Je ne me preoccupe pas comme l'auteur de savoir si mes liens de feed sont sur le meme serveur ou pas
  18. exemple d'utilisation
  19. print_r (get_feed_from_url("http://willy.boerland.com/myblog/"));
  20. on obtient
  21. Array
  22. (
  23. [0] => http://willy.boerland.com/myblog/atom/feed
  24. [1] => http://willy.boerland.com/myblog/blogapi/rsd
  25. [2] => http://willy.boerland.com/myblog/rss.xml
  26. [3] => http://willy.boerland.com/myblog/node/feed
  27. )
  28. *****************************************************************/
  29. $verif_complete = 0; //mettez le a 1 si vous voulez controler la validite des feed trouves mais le temps d'execution
  30. //est alors plus long
  31. //une fonction qui permet de si un lien est un feed ou nom, si c'est un feed elle retourne son type
  32. //si c'est pas un feed elle retourne 0, cette verification est evidemment tres tres legere
  33. // http://doc.spip.org/@is_feed
  34. function is_feed($url){
  35. # methode SPIP
  36. if (function_exists('recuperer_page')) {
  37. $buffer = recuperer_page($url);
  38. if (preg_match("/<(\w*) .*/", $buffer, $matches)){
  39. //ici on detecte la premiere balise
  40. $type_feed = $matches[1];
  41. switch ($type_feed) {
  42. case "rss": return "rss";
  43. case "feed": return "atom";
  44. case "rdf": return "rdf";
  45. }
  46. }
  47. return '';
  48. }
  49. $fp = @fopen($url, "r");
  50. if (!$fp ) {
  51. return 0;
  52. }
  53. //verifion la nature de ce fichier
  54. while (!feof($fp)) {
  55. $buffer = fgets($fp, 4096);
  56. if (preg_match("/<(\w*) .*/", $buffer, $matches)){
  57. //ici on detecte la premiere balise
  58. $type_feed = $matches[1];
  59. switch ($type_feed) {
  60. case "rss": fclose($fp); return "rss";
  61. case "feed": fclose($fp); return "atom";
  62. case "rdf": fclose($fp); return "rdf";
  63. default : fclose($fp); return 0;
  64. }
  65. }
  66. }
  67. }
  68. /*****************test is_feed******************************
  69. echo is_feed("http://spip-contrib.net/backend" _EXTENSIO_PHP") . "<br />"; //retourne rss
  70. echo is_feed("http://liberation.fr/rss.php") . "<br />"; //retourne rss
  71. echo is_feed("http://liberation.fr/rss.php") . "<br />"; //retourne rss
  72. echo is_feed("http://willy.boerland.com/myblog/atom/feed") //retourne atom
  73. echo is_feed("http://spip.net/") . "<br />"; //retoune 0
  74. //pas trouver d'exmples avec rdf j'ai encore du mal a saisir ce que rdf apporte de plus que rss
  75. //mais bon j'ai pas aprofondi
  76. ************************************************************/
  77. //fonction sans finesse mais efficace
  78. //on parcourt ligne par ligne a la recherche de balise <a> ou <link>
  79. //si dans le corps de celle-ci on trouve les mots rss, xml, atom ou rdf
  80. //alors on recupere la valeur href='<url>', on adapte celle-ci si elle
  81. //est relative et on verifie que c'est bien un feed si oui on l'ajoute
  82. //au tableau des feed si on ne trouve rien ou si aucun feed est trouve on retourne
  83. //un tableau vide
  84. // http://doc.spip.org/@get_feed_from_url
  85. function get_feed_from_url($url, $buffer=false){
  86. global $verif_complete;
  87. //j'ai prevenu ce sera pas fin
  88. if (!preg_match("/^http:\/\/.*/", $url)) $url = "http://www." . $url;
  89. if (!$buffer) $buffer = @file_get_contents($url);
  90. $feed_list = array();
  91. //extraction des <link>
  92. if (preg_match_all("/<link [^>]*>/i", $buffer, $matches)){
  93. //y a t-y rss atom rdf ou xml dans ces balises
  94. foreach($matches[0] as $link){
  95. if ( strpos($link, "rss")
  96. || strpos($link, "rdf")
  97. || strpos($link, "atom")
  98. || strpos($link, "xml") ){
  99. //voila un candidat on va extraire sa partie href et la placer dans notre tableau
  100. if (preg_match("/href=['|\"]?([^\s'\"]*)['|\"]?/",$link,$matches2)){
  101. //on aura pris soin de verifier si ce lien est relatif d'en faire un absolu
  102. if (!preg_match("/^http:\/\/.*/", $matches2[1])){
  103. $matches2[1] = concat_url($url,$matches2[1]);
  104. }
  105. if($verif_complete){
  106. if (is_feed($matches2[1])) $feed_list[] = $matches2[1];
  107. }else $feed_list[] = $matches2[1];
  108. }
  109. }
  110. }
  111. //print_r($matches);
  112. }
  113. //extraction des <a>
  114. if (preg_match_all("/<a [^>]*>/i", $buffer, $matches)){
  115. //y a t-y rss atom rdf ou xml dans ces balises
  116. foreach($matches[0] as $link){
  117. if ( strpos($link, "rss")
  118. || strpos($link, "rdf")
  119. || strpos($link, "atom")
  120. || strpos($link, "xml") ){
  121. //voila un candidat on va extraire sa partie href et la placer dans notre tableau
  122. if (preg_match("/href=['|\"]?([^\s'\"]*)['|\"]?/",$link,$matches2)){
  123. //on aura pris soin de verifier si ce lien est relatif d'en faire un absolu
  124. if (!preg_match("/^http:\/\/.*/", $matches2[1])){
  125. $matches2[1] = concat_url($url,$matches2[1]);
  126. }
  127. if($verif_complete){
  128. if (is_feed($matches2[1])) $feed_list[] = $matches2[1];
  129. }else $feed_list[] = $matches2[1];
  130. }
  131. }
  132. }
  133. }
  134. return $feed_list;
  135. }
  136. /************************************ getFeed ****************************
  137. print_r (get_feed_from_url("spip-contrib.net"));
  138. print_r (get_feed_from_url("http://liberation.fr/"));
  139. print_r (get_feed_from_url("cnn.com"));
  140. print_r (get_feed_from_url("http://willy.boerland.com/myblog/"));
  141. ***************************** Resultat *****************************************
  142. Array
  143. (
  144. [0] => http://www.spip-contrib.net/backend.php
  145. )
  146. Array
  147. (
  148. [0] => http://www.liberation.fr/rss.php
  149. )
  150. Array
  151. (
  152. [0] => http://rss.cnn.com/rss/cnn_topstories.rss
  153. [1] => http://rss.cnn.com/rss/cnn_latest.rss
  154. [2] => http://www.cnn.com/services/rss/
  155. [3] => http://www.cnn.com/services/rss/
  156. [4] => http://www.cnn.com/services/rss/
  157. )
  158. Array
  159. (
  160. [0] => http://willy.boerland.com/myblog/atom/feed
  161. [1] => http://willy.boerland.com/myblog/blogapi/rsd
  162. [2] => http://willy.boerland.com/myblog/rss.xml
  163. [3] => http://willy.boerland.com/myblog/node/feed
  164. )
  165. ************************************************************************/
  166. //petite fonction qui prend en charge les problemes de double slash
  167. //qunad on concatene les lien
  168. // http://doc.spip.org/@concat_url
  169. function concat_url($url1, $path){
  170. # methode spip
  171. if(function_exists('suivre_lien')) {
  172. return suivre_lien($url1,$path);
  173. }
  174. $url = $url1 . "/" . $path;
  175. //cette operation peut tres facilement avoir genere // ou ///
  176. $url = str_replace("///", "/", $url);
  177. $url = str_replace("//", "/", $url);
  178. //cas particulier de http://
  179. $url = str_replace("http:/", "http://", $url);
  180. return $url;
  181. }
  182. /****************************test concat**********************
  183. echo concat_url("http://spip.net" , "ecrire")."<br />";
  184. echo concat_url("http://spip.net/" , "ecrire")."<br />";
  185. echo concat_url("http://spip.net" , "/ecrire")."<br />";
  186. echo concat_url("http://spip.net/" , "/ecrire")."<br />";
  187. *************************************************************/
  188. ?>