]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/WebFinger/lib/webfinger.php
Implemented WebFinger and replaced our XRD with PEAR XML_XRD
[quix0rs-gnu-social.git] / plugins / WebFinger / lib / webfinger.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * WebFinger functions
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   GNUSocial
24  * @author    Mikael Nordfeldth
25  * @copyright 2013 Free Software Foundation, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27  * @link      http://status.net/
28  */
29
30 class WebFinger
31 {
32     const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
33
34     /*
35      * Reconstructs a WebFinger ID from data we know about the profile.
36      *
37      * @param  Profile  $profile    The profile we want a WebFinger ID for
38      * 
39      * @return string   $acct       acct:user@example.com URI
40      */
41     public static function reconstruct(Profile $profile)
42     {
43         $acct = null;
44
45         if (Event::handle('StartWebFingerReconstruction', array($profile, &$acct))) {
46             // TODO: getUri may not always give us the correct host on remote users?
47             $host = parse_url($profile->getUri(), PHP_URL_HOST);
48             if (empty($profile->nickname) || empty($host)) {
49                 throw new WebFingerReconstructionException($profile);
50             }
51             $acct = sprintf('acct:%s@%s', $profile->nickname, $host);
52
53             Event::handle('EndWebFingerReconstruction', array($profile, &$acct));
54         }
55
56         return $acct;
57     }
58
59     /*
60      * Gets all URI aliases for a Profile
61      *
62      * @param  Profile  $profile    The profile we want aliases for
63      * 
64      * @return array    $aliases    All the Profile's alternative URLs
65      */
66     public static function getAliases(Profile $profile)
67     {
68         $aliases = array();
69         $aliases[] = $profile->getUri();
70         try {
71             $aliases[] = $profile->getUrl();
72         } catch (InvalidUrlException $e) {
73             common_debug('Profile id='.$profile->id.' has invalid profileurl: ' .
74                             var_export($profile->profileurl, true));
75         }
76         return $aliases;
77     }
78
79     /*
80      * Gets all identities for a Profile, includes WebFinger acct: if
81      * available, as well as alias URLs.
82      *
83      * @param  Profile  $profile    The profile we want aliases for
84      * 
85      * @return array    $uris       WebFinger acct: URI and alias URLs
86      */
87     public static function getIdentities(Profile $profile)
88     {
89         $uris = array();
90         try {
91             $uris[] = self::reconstruct($profile);
92         } catch (WebFingerReconstructionException $e) {
93             common_debug('WebFinger reconstruction for Profile failed, ' .
94                             ' (id='.$profile->id.')');
95         }
96         $uris = array_merge($uris, self::getAliases($profile));
97
98         return $uris;
99     }
100 }