]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/lib/discovery.php
tweak RSSCloud plugin to use new schema layout (but still the old event); the timesta...
[quix0rs-gnu-social.git] / plugins / OStatus / lib / discovery.php
index 8aba31328e5af18969f6312ddba0c1d4b56af2a7..905ece2ca5a7cbff169c894beadced6abd1c710e 100644 (file)
@@ -39,6 +39,7 @@ class Discovery
     const LRDD_REL = 'lrdd';
     const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
     const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
+    const HCARD = 'http://microformats.org/profile/hcard';
 
     public $methods = array();
 
@@ -49,12 +50,11 @@ class Discovery
         $this->registerMethod('Discovery_LRDD_Link_HTML');
     }
 
-
     public function registerMethod($class)
     {
         $this->methods[] = $class;
     }
-    
+
     /**
      * Given a "user id" make sure it's normalized to either a webfinger
      * acct: uri or a profile HTTP URL.
@@ -77,7 +77,7 @@ class Discovery
     public static function isWebfinger($user_id)
     {
         $uri = Discovery::normalize($user_id);
-        
+
         return (substr($uri, 0, 5) == 'acct:');
     }
 
@@ -91,15 +91,14 @@ class Discovery
 
         foreach ($this->methods as $class) {
             $links = call_user_func(array($class, 'discover'), $uri);
-
             if ($link = Discovery::getService($links, Discovery::LRDD_REL)) {
                 // Load the LRDD XRD
-                if ($link['template']) {
+                if (!empty($link['template'])) {
                     $xrd_uri = Discovery::applyTemplate($link['template'], $uri);
                 } else {
                     $xrd_uri = $link['href'];
                 }
-                
+
                 $xrd = $this->fetchXrd($xrd_uri);
                 if ($xrd) {
                     return $xrd;
@@ -107,17 +106,21 @@ class Discovery
             }
         }
 
-        throw new Exception('Unable to find services for '. $id);
+        // TRANS: Exception.
+        throw new Exception(sprintf(_m('Unable to find services for %s.'),$id));
     }
 
     public static function getService($links, $service) {
+        if (!is_array($links)) {
+            return false;
+        }
+
         foreach ($links as $link) {
             if ($link['rel'] == $service) {
                 return $link;
             }
         }
     }
-    
 
     public static function applyTemplate($template, $id)
     {
@@ -126,7 +129,6 @@ class Discovery
         return $template;
     }
 
-    
     public static function fetchXrd($url)
     {
         try {
@@ -141,7 +143,7 @@ class Discovery
         }
 
         return XRD::parse($response->getBody());
-    }    
+    }
 }
 
 interface Discovery_LRDD
@@ -155,9 +157,9 @@ class Discovery_LRDD_Host_Meta implements Discovery_LRDD
     {
         if (Discovery::isWebfinger($uri)) {
             // We have a webfinger acct: - start with host-meta
-            list($name, $domain) = explode('@', $id);
+            list($name, $domain) = explode('@', $uri);
         } else {
-            $domain = @parse_url($uri, PHP_URL_HOST);
+            $domain = parse_url($uri, PHP_URL_HOST);
         }
 
         $url = 'http://'. $domain .'/.well-known/host-meta';
@@ -168,7 +170,7 @@ class Discovery_LRDD_Host_Meta implements Discovery_LRDD
             if ($xrd->host != $domain) {
                 return false;
             }
-            
+
             return $xrd->links;
         }
     }
@@ -180,7 +182,7 @@ class Discovery_LRDD_Link_Header implements Discovery_LRDD
     {
         try {
             $client = new HTTPClient();
-            $response = $client->get($url);
+            $response = $client->get($uri);
         } catch (HTTP_Request2_Exception $e) {
             return false;
         }
@@ -191,49 +193,19 @@ class Discovery_LRDD_Link_Header implements Discovery_LRDD
 
         $link_header = $response->getHeader('Link');
         if (!$link_header) {
-            return false;
+            //            return false;
         }
-        
-        return Discovery_LRDD_Link_Header::parseHeader($header);
+
+        return array(Discovery_LRDD_Link_Header::parseHeader($link_header));
     }
 
     protected static function parseHeader($header)
     {
-        preg_match('/^<[^>]+>/', $header, $uri_reference);
-        if (empty($uri_reference)) return;
-        
-        $link_uri = trim($uri_reference[0], '<>');
-        $link_rel = array();
-        $link_type = null;
-        
-        // remove uri-reference from header
-        $header = substr($header, strlen($uri_reference[0]));
-        
-        // parse link-params
-        $params = explode($header, ';');
-        
-        foreach ($params as $param) {
-            if (empty($param)) continue;
-            list($param_name, $param_value) = explode('=', $param, 2);
-            $param_name = trim($param_name);
-            $param_value = preg_replace('(^"|"$)', '', trim($param_value));
-            
-            // for now we only care about 'rel' and 'type' link params
-            // TODO do something with the other links-params
-            switch ($param_name) {
-            case 'rel':
-                $link_rel = trim($param_value);
-                break;
-                
-            case 'type':
-                $link_type = trim($param_value);
-            }
-        }
-        
-        return array(
-            'href' => $link_uri,
-            'rel' => $link_rel,
-            'type' => $link_type);
+        $lh = new LinkHeader($header);
+
+        return array('href' => $lh->href,
+                     'rel'  => $lh->rel,
+                     'type' => $lh->type);
     }
 }
 
@@ -243,7 +215,7 @@ class Discovery_LRDD_Link_HTML implements Discovery_LRDD
     {
         try {
             $client = new HTTPClient();
-            $response = $client->get($url);
+            $response = $client->get($uri);
         } catch (HTTP_Request2_Exception $e) {
             return false;
         }
@@ -255,49 +227,48 @@ class Discovery_LRDD_Link_HTML implements Discovery_LRDD
         return Discovery_LRDD_Link_HTML::parse($response->getBody());
     }
 
-
     public function parse($html)
     {
         $links = array();
-        
+
         preg_match('/<head(\s[^>]*)?>(.*?)<\/head>/is', $html, $head_matches);
         $head_html = $head_matches[2];
-        
+
         preg_match_all('/<link\s[^>]*>/i', $head_html, $link_matches);
-        
+
         foreach ($link_matches[0] as $link_html) {
             $link_url = null;
             $link_rel = null;
             $link_type = null;
-            
+
             preg_match('/\srel=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $rel_matches);
             if ( isset($rel_matches[3]) ) {
                 $link_rel = $rel_matches[3];
             } else if ( isset($rel_matches[1]) ) {
                 $link_rel = $rel_matches[1];
             }
-            
+
             preg_match('/\shref=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $href_matches);
             if ( isset($href_matches[3]) ) {
                 $link_uri = $href_matches[3];
             } else if ( isset($href_matches[1]) ) {
                 $link_uri = $href_matches[1];
             }
-            
+
             preg_match('/\stype=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $type_matches);
             if ( isset($type_matches[3]) ) {
                 $link_type = $type_matches[3];
             } else if ( isset($type_matches[1]) ) {
                 $link_type = $type_matches[1];
             }
-            
+
             $links[] = array(
                 'href' => $link_url,
                 'rel' => $link_rel,
                 'type' => $link_type,
             );
         }
-        
+
         return $links;
     }
 }