]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/webfinger.php
8d70403102b639040c72a9c3fb638d8ce7aa42cd
[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
36 class Webfinger
37 {
38     const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
39     const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
40     const HCARD       = 'http://microformats.org/profile/hcard';
41
42     /**
43      * Perform a webfinger lookup given an account.
44      */
45
46     public function lookup($id)
47     {
48         $id = $this->normalize($id);
49         list($name, $domain) = explode('@', $id);
50
51         $links = $this->getServiceLinks($domain);
52         if (!$links) {
53             return false;
54         }
55
56         $services = array();
57         foreach ($links as $link) {
58             if ($link['template']) {
59                 return $this->getServiceDescription($link['template'], $id);
60             }
61             if ($link['href']) {
62                 return $this->getServiceDescription($link['href'], $id);
63             }
64         }
65     }
66
67     /**
68      * Normalize an account ID
69      */
70     function normalize($id)
71     {
72         if (substr($id, 0, 7) == 'acct://') {
73             return substr($id, 7);
74         } else if (substr($id, 0, 5) == 'acct:') {
75             return substr($id, 5);
76         }
77
78         return $id;
79     }
80
81     function getServiceLinks($domain)
82     {
83         $url = 'http://'. $domain .'/.well-known/host-meta';
84         $content = $this->fetchURL($url);
85         if (empty($content)) {
86             common_log(LOG_DEBUG, 'Error fetching host-meta');
87             return false;
88         }
89         $result = XRD::parse($content);
90
91         // Ensure that the host == domain (spec may include signing later)
92         if ($result->host != $domain) {
93             return false;
94         }
95
96         $links = array();
97         foreach ($result->links as $link) {
98             if ($link['rel'] == WEBFINGER_SERVICE_REL_VALUE) {
99                 $links[] = $link;
100             }
101
102         }
103         return $links;
104     }
105
106     function getServiceDescription($template, $id)
107     {
108         $url = $this->applyTemplate($template, 'acct:' . $id);
109
110         $content = $this->fetchURL($url);
111
112         if (!$content) {
113             return false;
114         }
115
116         return XRD::parse($content);
117     }
118
119     function fetchURL($url)
120     {
121         try {
122             $client = new HTTPClient();
123             $response = $client->get($url);
124         } catch (HTTP_Request2_Exception $e) {
125             return false;
126         }
127
128         if ($response->getStatus() != 200) {
129             return false;
130         }
131
132         return $response->getBody();
133     }
134
135     function applyTemplate($template, $id)
136     {
137         $template = str_replace('{uri}', urlencode($id), $template);
138
139         return $template;
140     }
141
142     function getHostMeta($domain, $template) {
143         $xrd = new XRD();
144         $xrd->host = $domain;
145         $xrd->links[] = array('rel' => 'lrdd',
146                               'template' => $template,
147                               'title' => array('Resource Descriptor'));
148
149         return $xrd->toXML();
150     }
151 }
152