Skip to content

Instantly share code, notes, and snippets.

@davidrecordon
Created May 12, 2010 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidrecordon/398277 to your computer and use it in GitHub Desktop.
Save davidrecordon/398277 to your computer and use it in GitHub Desktop.
<?php
$host_meta = fetch_host_meta($curl, $parsed['scheme'], $parsed['domain'], $identifier);
if (isset($host_meta['lrdd']['priority'])) {
$priority = $host_meta['lrdd']['priority'];
} else {
$priority = 'host';
}
$link_href = false;
if (!$host_meta || $priority == 'resource' || !isset($host_meta['openid']['token_endpoint'])) {
$links = fetch_identifier($curl, $identifier);
if ($links['header'] && $links['html']) {
$link_href = $priority == 'host' ? $links['header'] : $links['html'];
} else if ($links['header']) {
$link_href = $links['header'];
} else if ($links['html']){
$link_href = $links['html'];
}
}
// given an identifier either returns an associative array with keys
// of header and html or false on failure.
function fetch_identifier($curl, $identifier) {
// can't fetch emails
if (strpos($identifier, 'acct:') === 0) {
return false;
}
curl_setopt($curl, CURLOPT_URL, $identifier);
curl_setopt($curl, CURLOPT_HEADER, true);
list($header, $html) = explode("\r\n\r\n", curl_exec($curl), 2);
curl_setopt($curl, CURLOPT_HEADER, false);
if (!$html) {
return false;
}
$header_href = '';
$html_href = '';
// this now needs to parse the dom and look for a link tag in the head
// with a rel of openid.
$old_libxml_error = libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);
libxml_use_internal_errors($old_libxml_error);
$head = $doc->getElementsByTagName('head');
if ($head->length > 0) {
foreach ($head->item(0)->getElementsByTagName('link') AS $link) {
if ($link->hasAttribute('rel')) {
$rel = $link->getAttribute('rel');
$rel_values = explode(' ', $rel);
foreach ($rel_values AS $value) {
if ($value == 'openid') {
$html_href = $link->getAttribute('href');
break 2;
}
}
}
}
}
$headers = explode("\r\n", $header);
foreach ($headers AS $header) {
if (stripos($header, 'Link:') === 0) {
list(,$link_value) = explode(': ', $header, 2);
$link_params = explode(';', $link_value);
$link_uri = array_shift($link_params);
if ($link_uri[0] !== '<' || substr($link_uri, -1) !== '>') {
break;
}
// trim < and > from the end
$link_uri = substr($link_uri, 1, -1);
foreach ($link_params AS $link_param) {
$link_param = trim($link_param);
if (strpos($link_param, 'rel=') === 0) {
preg_match('/"((?:[^"]).*)"/', $link_param, $matches);
if (isset($matches[1])) {
$rel_values = explode(' ', $matches[1]);
foreach ($rel_values AS $value) {
if ($value == 'openid') {
$header_href = $link_uri;
break 3;
}
}
}
}
}
}
}
return array(
'header' => $header_href,
'html' => $html_href
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment