Guest User

Untitled

a guest
May 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. <?php
  2. $host_meta = fetch_host_meta($curl, $parsed['scheme'], $parsed['domain'], $identifier);
  3.  
  4. if (isset($host_meta['lrdd']['priority'])) {
  5. $priority = $host_meta['lrdd']['priority'];
  6. } else {
  7. $priority = 'host';
  8. }
  9.  
  10. $link_href = false;
  11. if (!$host_meta || $priority == 'resource' || !isset($host_meta['openid']['token_endpoint'])) {
  12. $links = fetch_identifier($curl, $identifier);
  13.  
  14. if ($links['header'] && $links['html']) {
  15. $link_href = $priority == 'host' ? $links['header'] : $links['html'];
  16. } else if ($links['header']) {
  17. $link_href = $links['header'];
  18. } else if ($links['html']){
  19. $link_href = $links['html'];
  20. }
  21. }
  22.  
  23.  
  24. // given an identifier either returns an associative array with keys
  25. // of header and html or false on failure.
  26. function fetch_identifier($curl, $identifier) {
  27. // can't fetch emails
  28. if (strpos($identifier, 'acct:') === 0) {
  29. return false;
  30. }
  31.  
  32. curl_setopt($curl, CURLOPT_URL, $identifier);
  33. curl_setopt($curl, CURLOPT_HEADER, true);
  34. list($header, $html) = explode("\r\n\r\n", curl_exec($curl), 2);
  35. curl_setopt($curl, CURLOPT_HEADER, false);
  36.  
  37. if (!$html) {
  38. return false;
  39. }
  40.  
  41. $header_href = '';
  42. $html_href = '';
  43.  
  44. // this now needs to parse the dom and look for a link tag in the head
  45. // with a rel of openid.
  46. $old_libxml_error = libxml_use_internal_errors(true);
  47. $doc = new DOMDocument();
  48. $doc->loadHTML($html);
  49. libxml_use_internal_errors($old_libxml_error);
  50.  
  51. $head = $doc->getElementsByTagName('head');
  52. if ($head->length > 0) {
  53. foreach ($head->item(0)->getElementsByTagName('link') AS $link) {
  54. if ($link->hasAttribute('rel')) {
  55. $rel = $link->getAttribute('rel');
  56. $rel_values = explode(' ', $rel);
  57. foreach ($rel_values AS $value) {
  58. if ($value == 'openid') {
  59. $html_href = $link->getAttribute('href');
  60. break 2;
  61. }
  62. }
  63. }
  64. }
  65. }
  66.  
  67. $headers = explode("\r\n", $header);
  68. foreach ($headers AS $header) {
  69. if (stripos($header, 'Link:') === 0) {
  70. list(,$link_value) = explode(': ', $header, 2);
  71. $link_params = explode(';', $link_value);
  72. $link_uri = array_shift($link_params);
  73.  
  74. if ($link_uri[0] !== '<' || substr($link_uri, -1) !== '>') {
  75. break;
  76. }
  77.  
  78. // trim < and > from the end
  79. $link_uri = substr($link_uri, 1, -1);
  80.  
  81. foreach ($link_params AS $link_param) {
  82. $link_param = trim($link_param);
  83. if (strpos($link_param, 'rel=') === 0) {
  84. preg_match('/"((?:[^"]).*)"/', $link_param, $matches);
  85. if (isset($matches[1])) {
  86. $rel_values = explode(' ', $matches[1]);
  87. foreach ($rel_values AS $value) {
  88. if ($value == 'openid') {
  89. $header_href = $link_uri;
  90. break 3;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98.  
  99. return array(
  100. 'header' => $header_href,
  101. 'html' => $html_href
  102. );
  103. }
Add Comment
Please, Sign In to add comment