]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/webfinger.php
fix up hub queueing to work w/ stomp queues
[quix0rs-gnu-social.git] / plugins / OStatus / lib / webfinger.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * A sample module to show best practices for StatusNet plugins
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @package   StatusNet
24  * @author    James Walker <james@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27  * @link      http://status.net/
28  */
29
30 define('WEBFINGER_SERVICE_REL_VALUE', 'lrdd');
31
32 /**
33  * Implement the webfinger protocol.
34  */
35 class Webfinger
36 {
37     /**
38      * Perform a webfinger lookup given an account.
39      */ 
40     public function lookup($id)
41     {
42         $id = $this->normalize($id);
43         list($name, $domain) = explode('@', $id);
44
45         $links = $this->getServiceLinks($domain);
46         if (!$links) {
47             return false;
48         }
49         
50         $services = array();
51         foreach ($links as $link) {
52             if ($link['template']) {
53                 return $this->getServiceDescription($link['template'], $id);
54             }
55             if ($link['href']) {
56                 return $this->getServiceDescription($link['href'], $id);
57             }
58         }
59     }
60
61     /**
62      * Normalize an account ID
63      */
64     function normalize($id)
65     {
66         if (substr($id, 0, 7) == 'acct://') {
67             return substr($id, 7); 
68         } else if (substr($id, 0, 5) == 'acct:') {
69             return substr($id, 5);
70         }
71
72         return $id;
73     }
74
75     function getServiceLinks($domain)
76     {
77         $url = 'http://'. $domain .'/.well-known/host-meta';
78         $content = $this->fetchURL($url);
79         if (empty($content)) {
80             common_log(LOG_DEBUG, 'Error fetching host-meta');
81             return false;
82         }
83         $result = XRD::parse($content);
84
85         // Ensure that the host == domain (spec may include signing later)
86         if ($result->host != $domain) {
87             return false;
88         }
89         
90         $links = array();
91         foreach ($result->links as $link) {
92             if ($link['rel'] == WEBFINGER_SERVICE_REL_VALUE) {
93                 $links[] = $link;
94             }
95
96         }
97         return $links;
98     }
99
100     function getServiceDescription($template, $id)
101     {
102         $url = $this->applyTemplate($template, 'acct:' . $id);
103
104         $content = $this->fetchURL($url);
105
106         return XRD::parse($content);
107     }
108
109     function fetchURL($url)
110     {
111         try {
112             $client = new HTTPClient();
113             $response = $client->get($url);
114         } catch (HTTP_Request2_Exception $e) {
115             return false;
116         }
117
118         if ($response->getStatus() != 200) {
119             return false;
120         }
121
122         return $response->getBody();
123     }
124
125     function applyTemplate($template, $id)
126     {
127         $template = str_replace('{uri}', urlencode($id), $template);
128
129         return $template;
130     }
131
132     function getHostMeta($domain, $template) {
133         $xrd = new XRD();
134         $xrd->host = $domain;
135         $xrd->links[] = array('rel' => 'lrdd',
136                               'template' => $template,
137                               'title' => array('Resource Descriptor'));
138
139         return $xrd->toXML();
140     }
141 }
142
143