LrddMethodLinkHtml.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Component\FreeNetwork\Util\LrddMethod;
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. use Component\FreeNetwork\Util\LrddMethod;
  18. use XML_XRD_Element_Link;
  19. /**
  20. * Implementation of discovery using HTML <link> element
  21. *
  22. * Discovers XRD file for a user by fetching the URL and reading any
  23. * <link> elements in the HTML response.
  24. *
  25. * @category Discovery
  26. * @package GNUsocial
  27. *
  28. * @author James Walker <james@status.net>
  29. * @copyright 2010 StatusNet, Inc.
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class LrddMethodLinkHtml extends LRDDMethod
  33. {
  34. /**
  35. * For HTTP IDs, fetch the URL and look for <link> elements
  36. * in the HTML response.
  37. *
  38. * @param mixed $uri
  39. *
  40. * @todo fail out of WebFinger URIs faster
  41. *
  42. */
  43. public function discover($uri)
  44. {
  45. $response = self::fetchUrl($uri);
  46. return self::parse($response->getContent());
  47. }
  48. /**
  49. * Parse HTML and return <link> elements
  50. *
  51. * Given an HTML string, scans the string for <link> elements
  52. *
  53. * @param string $html HTML to scan
  54. *
  55. * @return array array of associative arrays in JRD-ish array format
  56. */
  57. public function parse($html)
  58. {
  59. $links = [];
  60. preg_match('/<head(\s[^>]*)?>(.*?)<\/head>/is', $html, $head_matches);
  61. if (count($head_matches) != 3) {
  62. return [];
  63. }
  64. [, , $head_html] = $head_matches;
  65. preg_match_all('/<link\s[^>]*>/i', $head_html, $link_matches);
  66. foreach ($link_matches[0] as $link_html) {
  67. $link_url = null;
  68. $link_rel = null;
  69. $link_type = null;
  70. preg_match('/\srel=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $rel_matches);
  71. if (count($rel_matches) > 3) {
  72. $link_rel = $rel_matches[3];
  73. } elseif (count($rel_matches) > 1) {
  74. $link_rel = $rel_matches[1];
  75. }
  76. preg_match('/\shref=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $href_matches);
  77. if (count($href_matches) > 3) {
  78. $link_uri = $href_matches[3];
  79. } elseif (count($href_matches) > 1) {
  80. $link_uri = $href_matches[1];
  81. }
  82. preg_match('/\stype=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $type_matches);
  83. if (count($type_matches) > 3) {
  84. $link_type = $type_matches[3];
  85. } elseif (count($type_matches) > 1) {
  86. $link_type = $type_matches[1];
  87. }
  88. $links[] = new XML_XRD_Element_Link($link_rel, $link_uri, $link_type);
  89. }
  90. return $links;
  91. }
  92. }