function webfinger_find_by_email($email) { // Obtain the URL to gather user information from $query_url = webfinger_query_url_for_email($email); if (empty($query_url)) { return null; } // Try to grab the information help on this email address $query_result = http_request($query_url); if (!did_http_succeed($query_result)) { return null; } $query_xml = $query_result['body']; $query_data = convert_xml_string_to_array($query_xml); if (!isset($query_data['xrd']['link'])) { return null; } $query_links = $query_data['xrd']['link']; // If there was a single link tag, we'll get its contents in $query_links, but // if multiple links were present, the contents will be an numeric array of them all. // To normalize this, put lone tags into their own array. if (!isset($query_links[0])) { $query_links_list = array($query_links); } else { $query_links_list = $query_links; } // Go through all the links and extract the information from any we recognize $result = array(); foreach ($query_links_list as $link) { if (!isset($link['@attributes']['rel']) || !isset($link['@attributes']['href'])) { continue; } $rel = $link['@attributes']['rel']; $href = $link['@attributes']['href']; // At the moment this only understands hCard formatted pages if ($rel === HCARD_REL_URL) { $hkit = new hKit(); $hcard_result = $hkit->getByURL('hcard', $href); if (empty($hcard_result) || !isset($hcard_result[0])) { continue; } $hcard_info = $hcard_result[0]; $user_id = $href; $user_name = ''; if (isset($hcard_info['fn'])) { $display_name = $hcard_info['fn']; } else { $display_name = ''; } if (isset($hcard_info['photo'])) { $portrait_url = $hcard_info['photo']; } else { $portrait_url = ''; } if (isset($hcard_info['adr'])) { $location = $hcard_info['adr']; } else { $location = ''; } $result = array('webfinger' => array('user_id' => $user_id, 'user_name' => $user_name, 'display_name' => $display_name, 'portrait_url' => $portrait_url, 'location' => $location)); } } if (empty($result)) { return null; } return $result; }
/** * Calls the Rapleaf API to get information about the user associated with this * email address. For more details on the call, see * http://www.rapleaf.com/apidoc/v2/person * * @since Unknown * * @param string $email The email address of the user */ function rapleaf_find_by_email($email) { $user_info_url = 'http://api.rapleaf.com/v2/person/'; $user_info_url .= urlencode($email); $user_info_url .= '?api_key=' . RAPLEAF_API_KEY; $user_info_result = http_request($user_info_url); if (!did_http_succeed($user_info_result)) { return null; } // Take the XML string containing all the user's information and pull it into a PHP array as the final result $user_info_xml_string = $user_info_result['body']; $user_info_object = convert_xml_string_to_array($user_info_xml_string); if (!isset($user_info_object['person']['@attributes']['id'])) { return null; } $user_id = $user_info_object['person']['@attributes']['id']; $user_name = ''; if (isset($user_info_object['person']['basics']['name'])) { $display_name = $user_info_object['person']['basics']['name']; } else { $display_name = ''; } if (isset($user_info_object['person']['basics']['location'])) { $location = $user_info_object['person']['basics']['location']; } else { $location = ''; } $portrait_url = ''; $result = array('rapleaf' => array('user_id' => $user_id, 'user_name' => $user_name, 'display_name' => $display_name, 'portrait_url' => $portrait_url, 'location' => $location)); $all_services = $user_info_object['person']['memberships']['primary']['membership']; foreach ($all_services as $service) { $service_url = $service['@attributes']['site']; if (preg_match('/(.*)\\.com/', $service_url, $matches)) { $service_name = $matches[1]; } else { $service_name = $service_url; } $user_name = ''; $user_id = ''; if (!isset($service['@attributes']['profile_url'])) { continue; } $profile_url = $service['@attributes']['profile_url']; $service_result = get_profile_for_service($service_name, $user_name, $user_id, $profile_url); if (isset($service_result)) { // If Rapleaf has given us an image URL, and there's none found otherwise, use theirs if ($service_result['portrait_url'] == '' && isset($service['@attributes']['image_url'])) { $service_result['portrait_url'] = $service['@attributes']['image_url']; } $result[$service_name] = $service_result; } } return $result; }
/** * Queries the domain for this email address to see if it supports WebFinger, and * if it does returns the right URL to call to get information on the address * * @param string $email The email address of the user * @return string The URL to call to get information, or null if none found */ function webfinger_query_url_for_email($email) { $domain = get_domain_from_email($email); if (empty($domain)) { return null; } // First, ask the server for a list of the services it supports, so we can // look through that list for WebFinger $endpoint_url = WEBFINGER_ENDPOINT_PREFIX; $endpoint_url .= $domain; $endpoint_url .= WEBFINGER_ENDPOINT_SUFFIX; $endpoint_result = http_request($endpoint_url); if (!did_http_succeed($endpoint_result)) { return null; } $endpoint_xml = $endpoint_result['body']; $endpoint_data = convert_xml_string_to_array($endpoint_xml); if (!isset($endpoint_data['xrd']['link'])) { return null; } $endpoint_links = $endpoint_data['xrd']['link']; // If there was a single link tag, we'll get its contents in $endpoint_links, but // if multiple links were present, the contents will be an numeric array of them all. // To normalize this, put lone tags into their own array. if (!isset($endpoint_links[0])) { $endpoint_links_list = array($endpoint_links); } else { $endpoint_links_list = $endpoint_links; } // Now search for a link with the right service rel tag, and get the URL template $template = null; foreach ($endpoint_links_list as $link) { if (!isset($link['@attributes']['rel'])) { continue; } $rel = $link['@attributes']['rel']; if ($rel !== WEBFINGER_SERVICE_REL_TYPE) { continue; } if (!isset($link['@attributes']['template'])) { continue; } $template = $link['@attributes']['template']; } if (empty($template)) { return null; } if (!strpos($template, '{uri}')) { return null; } // Finally substitute the actual email address into the generic template $result = str_replace('{uri}', urlencode('acct://' . $email), $template); return $result; }