]> git.mxchange.org Git - friendica.git/commitdiff
modularise webfinger and make it service agnostic
authorMike Macgirvin <mike@macgirvin.com>
Tue, 12 Oct 2010 23:50:12 +0000 (16:50 -0700)
committerMike Macgirvin <mike@macgirvin.com>
Tue, 12 Oct 2010 23:50:12 +0000 (16:50 -0700)
boot.php
mod/dfrn_request.php

index 4deafe317c279c1a4a514e4d19ee34d2cc742b2e..bd6d6cc867702ac2bee2ddc1b7738edb119b48c9 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -833,58 +833,90 @@ function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
 // Return an empty string if email-style addresses but webfinger fails,
 // or if the resultant personal XRD doesn't contain a DFRN profile.
 
-if(! function_exists('webfinger')) {
-function webfinger($s) {
+if(! function_exists('webfinger_dfrn')) {
+function webfinger_dfrn($s) {
        if(! strstr($s,'@')) {
                return $s;
        }
-       $host = substr($s,strpos($s,'@') + 1);
-       $url = 'http://' . $host . '/.well-known/host-meta' ;
-       $xml = fetch_url($url);
-       if (! $xml)
-               return '';
-       $h = simplexml_load_string($xml);
-       $arr = convert_xml_element_to_array($h);
+       $links = webfinger($s);
+       if(count($links)) {
+               foreach($links as $link)
+                       if($link['@attributes']['rel'] == NAMESPACE_DFRN)
+                               return $link['@attributes']['href'];
+       }
+       return '';
+}}
 
-       if(! isset($arr['xrd']['link']))
-               return '';
+// Given an email style address, perform webfinger lookup and 
+// return the array of link attributes from the personal XRD file.
+// On error/failure return an empty array.
 
-       $link = $arr['xrd']['link'];
-       if(! isset($link[0]))
-               $links = array($link);
-       else
-               $links = $link;
 
-       foreach($links as $link)
-               if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
-                       $tpl = $link['@attributes']['template'];
-       if((empty($tpl)) || (! strpos($tpl, '{uri}')))
-               return '';
+if(! function_exists('webfinger')) {
+function webfinger($s) {
+       $host = '';
+       if(strstr($s,'@')) {
+               $host = substr($s,strpos($s,'@') + 1);
+       }
+       if(strlen($host)) {
+               $tpl = fetch_lrdd_template($host);
+               if(strlen($tpl)) {
+                       $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
+                       $links = fetch_xrd_links($pxrd);
+                       if(! count($links)) {
+                               // try without the double slashes
+                               $pxrd = str_replace('{uri}', urlencode('acct:'.$s), $tpl);
+                               $links = fetch_xrd_links($pxrd);
+                       }
+                       return $links;
+               }
+       }
+       return array();
+}}
+
+// Given a host name, locate the LRDD template from that
+// host. Returns the LRDD template or an empty string on
+// error/failure.
+
+if(! function_exists('fetch_lrdd_template')) {
+function fetch_lrdd_template($host) {
+       $tpl = '';
+       $url = 'http://' . $host . '/.well-known/host-meta' ;
+       $links = fetch_xrd_links($url);
+       if(count($links)) {
+               foreach($links as $link)
+                       if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
+                               $tpl = $link['@attributes']['template'];
+       }
+       if(! strpos($tpl,'{uri}'))
+               $tpl = '';
+       return $tpl;
+}}
+
+// Given a URL, retrieve the page as an XRD document.
+// Return an array of links.
+// on error/failure return empty array.
 
-       $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
+if(! function_exists('fetch_xrd_links')) {
+function fetch_xrd_links($url) {
 
-       $xml = fetch_url($pxrd);
+       $xml = fetch_url($url);
        if (! $xml)
-               return '';
+               return array();
        $h = simplexml_load_string($xml);
        $arr = convert_xml_element_to_array($h);
 
-       if(! isset($arr['xrd']['link']))
-               return '';
-
-       $link = $arr['xrd']['link'];
-       if(! isset($link[0]))
-               $links = array($link);
-       else
-               $links = $link;
-
-       foreach($links as $link)
-               if($link['@attributes']['rel'] == NAMESPACE_DFRN)
-                       return $link['@attributes']['href'];
-       return '';
+       if(isset($arr['xrd']['link'])) {
+               $link = $arr['xrd']['link'];
+               if(! isset($link[0]))
+                       $links = array($link);
+               else
+                       $links = $link;
+               return $links;
+       }
+       return array();
 }}
 
-
 // Convert an ACL array to a storable string
 
 if(! function_exists('perms2str')) {
index 1458ba8e467dadfb5eb235d5590570fed93cb254..3cb2a16a4ddd74edff0cce3ef6de8071ae80a08b 100644 (file)
@@ -181,7 +181,7 @@ function dfrn_request_post(&$a) {
 
                // Canonicalise email-style profile locator
 
-               $url = webfinger($url);
+               $url = webfinger_dfrn($url);
 
                if(! strlen($url)) {
                        notice( t("Unable to resolve your name at the provided location.") . EOL);