]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/discoveryhints.php
Debugging log fix.
[quix0rs-gnu-social.git] / plugins / OStatus / lib / discoveryhints.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Some utilities for generating hint data
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 class DiscoveryHints {
23     static function fromXRD(XML_XRD $xrd)
24     {
25         $hints = array();
26
27         if (Event::handle('StartDiscoveryHintsFromXRD', array($xrd, &$hints))) {
28             foreach ($xrd->links as $link) {
29                 switch ($link->rel) {
30                 case WebFingerResource_Profile::PROFILEPAGE:
31                     $hints['profileurl'] = $link->href;
32                     break;
33                 case Salmon::REL_SALMON:
34                 case Salmon::NS_MENTIONS:   // XXX: deprecated, remove in the future
35                 case Salmon::NS_REPLIES:    // XXX: deprecated, remove in the future
36                     $hints['salmon'] = $link->href;
37                     break;
38                 case Discovery::UPDATESFROM:
39                     if (empty($link->type) || $link->type == 'application/atom+xml') {
40                         $hints['feedurl'] = $link->href;
41                     }
42                     break;
43                 case Discovery::HCARD:
44                 case Discovery::MF2_HCARD:
45                     $hints['hcard'] = $link->href;
46                     break;
47                 default:
48                     break;
49                 }
50             }
51             Event::handle('EndDiscoveryHintsFromXRD', array($xrd, &$hints));
52         }
53
54         return $hints;
55     }
56
57     static function fromHcardUrl($url)
58     {
59         $client = new HTTPClient();
60         $client->setHeader('Accept', 'text/html,application/xhtml+xml');
61         try {
62             $response = $client->get($url);
63
64             if (!$response->isOk()) {
65                 return null;
66             }
67         } catch (HTTP_Request2_Exception $e) {
68             // Any HTTPClient error that might've been thrown
69             common_log(LOG_ERR, __METHOD__ . ':'.$e->getMessage());
70             return null;
71         }
72
73         return self::hcardHints($response->getBody(),
74                                 $response->getEffectiveUrl());
75     }
76
77     static function hcardHints($body, $url)
78     {
79         $hcard = self::_hcard($body, $url);
80
81         if (empty($hcard)) {
82             return array();
83         }
84
85         $hints = array();
86
87         // XXX: don't copy stuff into an array and then copy it again
88
89         if (array_key_exists('nickname', $hcard) && !empty($hcard['nickname'][0])) {
90             $hints['nickname'] = $hcard['nickname'][0];
91         }
92
93         if (array_key_exists('name', $hcard) && !empty($hcard['name'][0])) {
94             $hints['fullname'] = $hcard['name'][0];
95         }
96
97         if (array_key_exists('photo', $hcard) && count($hcard['photo'])) {
98             $hints['avatar'] = $hcard['photo'][0];
99         }
100
101         if (array_key_exists('note', $hcard) && !empty($hcard['note'][0])) {
102             $hints['bio'] = $hcard['note'][0];
103         }
104
105         if (array_key_exists('adr', $hcard) && !empty($hcard['adr'][0])) {
106             $hints['location'] = $hcard['adr'][0]['value'];
107         }
108
109         if (array_key_exists('url', $hcard) && !empty($hcard['url'][0])) {
110             $hints['homepage'] = $hcard['url'][0];
111         }
112
113         return $hints;
114     }
115
116     static function _hcard($body, $url)
117     {
118         $mf2 = new Mf2\Parser($body, $url);
119         $mf2 = $mf2->parse();
120         
121         if (empty($mf2['items'])) {
122             return null;
123         }
124
125         $hcards = array();
126
127         foreach ($mf2['items'] as $item) {
128             if (!in_array('h-card', $item['type'])) {
129                 continue;
130             }
131
132             // We found a match, return it immediately
133             if (isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) {
134                 return $item['properties'];
135             }
136
137             // Let's keep all the hcards for later, to return one of them at least
138             $hcards[] = $item['properties'];
139         }
140
141         // No match immediately for the url we expected, but there were h-cards found
142         if (count($hcards) > 0) {
143             return $hcards[0];
144         }
145
146         return null;
147     }
148 }