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