class.dc.autodiscovery.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # This file is part of DotClear.
  4. # Copyright (c) 2004 Olivier Meunier and contributors. All rights
  5. # reserved.
  6. #
  7. # DotClear is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # DotClear is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with DotClear; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. # ***** END LICENSE BLOCK *****
  22. #
  23. # Initial developer of this file is Nicky Duong
  24. class autodiscover
  25. {
  26. var $url;
  27. function autoDiscovery($text)
  28. {
  29. $uriArray = $this->__getPermaLinksFromText($text);
  30. $res = array();
  31. for ($i=0;$i<count($uriArray);$i++)
  32. {
  33. if (($infoRDF = $this->__getRDFFromLink($uriArray[$i])) !== false)
  34. {
  35. if (($tburi = $this->__getPingURL($infoRDF)) !== false)
  36. {
  37. $res[] = $tburi;
  38. }
  39. }
  40. }
  41. return $res;
  42. }
  43. /*
  44. * Produit un tableau contenant la liste des permaliens contenus dans un texte.
  45. *
  46. * @param String $text texte le texte à parser.
  47. * @return String[] tableaux contenant la liste des URI contenues dans le texte.
  48. */
  49. function __getPermaLinksFromText($text)
  50. {
  51. $linkArray = array();
  52. # Attributs href des liens
  53. if (preg_match_all('/<a ([^>]+)>/ms', $text, $array, PREG_SET_ORDER))
  54. {
  55. for ($i = 0; $i<count($array); $i++)
  56. {
  57. if (preg_match('/href="http:\/\/([^"]+)"/ms', $array[$i][1], $matches)) {
  58. $linkArray[$matches[1]] = 1;
  59. }
  60. }
  61. }
  62. unset($array);
  63. # Attributs cite sur blockquote et q
  64. if (preg_match_all('/<(blockquote|q) ([^>]+)>/ms', $text, $array, PREG_SET_ORDER))
  65. {
  66. for ($i = 0; $i<count($array); $i++)
  67. {
  68. if (preg_match('/cite="http:\/\/([^"]+)"/ms', $array[$i][2], $matches)) {
  69. $linkArray[$matches[1]] = 1;
  70. }
  71. }
  72. }
  73. return array_keys($linkArray);
  74. }
  75. /*
  76. * Cherche, si il y en a un, le RDF lié à une URI.
  77. *
  78. * @param String $link Permalien
  79. * @return String Informations RDF
  80. */
  81. function __getRDFFromLink($link)
  82. {
  83. $pageContents = HttpClient::quickGet('http://'.$link);
  84. preg_match_all('/(<rdf:RDF.*?<\/rdf:RDF>)/sm', $pageContents, $rdf_all, PREG_SET_ORDER);
  85. for ($i=0; $i<count($rdf_all); $i++)
  86. {
  87. if (preg_match('|dc:identifier="http://'.preg_quote($link).'"|ms',$rdf_all[$i][1]))
  88. {
  89. $encoding = $this->__discoverCharset($pageContents);
  90. $rdf = '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n".$rdf_all[$i][1];
  91. return array('rdf' => $rdf, 'encoding' => $encoding);
  92. }
  93. }
  94. return false;
  95. }
  96. function __discoverCharset($str)
  97. {
  98. # Recherche du prologue
  99. if (preg_match('/^\s*<?xml[^>]*encoding="(.+)"/',$str,$matches)) {
  100. return $matches[1];
  101. } elseif (preg_match('/<meta[^>]+content="[^"]*charset=([a-zA-Z0-9-]+)/m',$str,$matches)) {
  102. return $matches[1];
  103. } else {
  104. return 'iso-8859-1';
  105. }
  106. }
  107. function __getPingURL($xml)
  108. {
  109. $this->url = '';
  110. $xp = xml_parser_create($xml['encoding']);
  111. xml_parser_set_option($xp,XML_OPTION_CASE_FOLDING,false);
  112. xml_set_object($xp,$this);
  113. xml_set_element_handler($xp,'__openTag','__closeTag');
  114. xml_parse($xp,$xml['rdf']);
  115. xml_parser_free($xp);
  116. return ($this->url != '') ? $this->url : false;
  117. }
  118. function __openTag($p,$tag,$attr='')
  119. {
  120. if ($tag == 'rdf:Description' && !empty($attr['trackback:ping'])) {
  121. $this->url = $attr['trackback:ping'];
  122. }
  123. }
  124. function __closeTag($p,$tag)
  125. {
  126. }
  127. }
  128. ?>