]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/discoveryhints.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 0.9.x
[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
24     static function fromXRD($xrd)
25     {
26         $hints = array();
27
28         foreach ($xrd->links as $link) {
29             switch ($link['rel']) {
30             case Discovery::PROFILEPAGE:
31                 $hints['profileurl'] = $link['href'];
32                 break;
33             case Salmon::NS_MENTIONS:
34             case Salmon::NS_REPLIES:
35                 $hints['salmon'] = $link['href'];
36                 break;
37             case Discovery::UPDATESFROM:
38                 $hints['feedurl'] = $link['href'];
39                 break;
40             case Discovery::HCARD:
41                 $hints['hcardurl'] = $link['href'];
42                 break;
43             default:
44                 break;
45             }
46         }
47
48         return $hints;
49     }
50
51     static function fromHcardUrl($url)
52     {
53         $client = new HTTPClient();
54         $client->setHeader('Accept', 'text/html,application/xhtml+xml');
55         $response = $client->get($url);
56
57         if (!$response->isOk()) {
58             return null;
59         }
60
61         return self::hcardHints($response->getBody(),
62                                 $response->getUrl());
63     }
64
65     static function hcardHints($body, $url)
66     {
67         $hcard = self::_hcard($body, $url);
68
69         if (empty($hcard)) {
70             return array();
71         }
72
73         $hints = array();
74
75         // XXX: don't copy stuff into an array and then copy it again
76
77         if (array_key_exists('nickname', $hcard)) {
78             $hints['nickname'] = $hcard['nickname'];
79         }
80
81         if (array_key_exists('fn', $hcard)) {
82             $hints['fullname'] = $hcard['fn'];
83         } else if (array_key_exists('n', $hcard)) {
84             $hints['fullname'] = implode(' ', $hcard['n']);
85         }
86
87         if (array_key_exists('photo', $hcard) && count($hcard['photo'])) {
88             $hints['avatar'] = $hcard['photo'][0];
89         }
90
91         if (array_key_exists('note', $hcard)) {
92             $hints['bio'] = $hcard['note'];
93         }
94
95         if (array_key_exists('adr', $hcard)) {
96             if (is_string($hcard['adr'])) {
97                 $hints['location'] = $hcard['adr'];
98             } else if (is_array($hcard['adr'])) {
99                 $hints['location'] = implode(' ', $hcard['adr']);
100             }
101         }
102
103         if (array_key_exists('url', $hcard)) {
104             if (is_string($hcard['url'])) {
105                 $hints['homepage'] = $hcard['url'];
106             } else if (is_array($hcard['url']) && !empty($hcard['url'])) {
107                 // HACK get the last one; that's how our hcards look
108                 $hints['homepage'] = $hcard['url'][count($hcard['url'])-1];
109             }
110         }
111
112         return $hints;
113     }
114
115     static function _hcard($body, $url)
116     {
117         // DOMDocument::loadHTML may throw warnings on unrecognized elements.
118
119         $old = error_reporting(error_reporting() & ~E_WARNING);
120
121         $doc = new DOMDocument();
122         $doc->loadHTML($body);
123
124         error_reporting($old);
125
126         $xp = new DOMXPath($doc);
127
128         $hcardNodes = self::_getChildrenByClass($doc->documentElement, 'vcard', $xp);
129
130         $hcards = array();
131
132         for ($i = 0; $i < $hcardNodes->length; $i++) {
133
134             $hcardNode = $hcardNodes->item($i);
135
136             $hcard = self::_hcardFromNode($hcardNode, $xp, $url);
137
138             $hcards[] = $hcard;
139         }
140
141         $repr = null;
142
143         foreach ($hcards as $hcard) {
144             if (in_array($url, $hcard['url'])) {
145                 $repr = $hcard;
146                 break;
147             }
148         }
149
150         if (!is_null($repr)) {
151             return $repr;
152         } else if (count($hcards) > 0) {
153             return $hcards[0];
154         } else {
155             return null;
156         }
157     }
158
159     function _getChildrenByClass($el, $cls, $xp)
160     {
161         // borrowed from hkit. Thanks dudes!
162
163         $qry = ".//*[contains(concat(' ',normalize-space(@class),' '),' $cls ')]";
164
165         $nodes = $xp->query($qry, $el);
166
167         return $nodes;
168     }
169
170     function _hcardFromNode($hcardNode, $xp, $base)
171     {
172         $hcard = array();
173
174         $hcard['url'] = array();
175
176         $urlNodes = self::_getChildrenByClass($hcardNode, 'url', $xp);
177
178         for ($j = 0; $j < $urlNodes->length; $j++) {
179
180             $urlNode = $urlNodes->item($j);
181
182             if ($urlNode->hasAttribute('href')) {
183                 $url = $urlNode->getAttribute('href');
184             } else {
185                 $url = $urlNode->textContent;
186             }
187
188             $hcard['url'][] = self::_rel2abs($url, $base);
189         }
190
191         $hcard['photo'] = array();
192
193         $photoNodes = self::_getChildrenByClass($hcardNode, 'photo', $xp);
194
195         for ($j = 0; $j < $photoNodes->length; $j++) {
196             $photoNode = $photoNodes->item($j);
197             if ($photoNode->hasAttribute('src')) {
198                 $url = $photoNode->getAttribute('src');
199             } else if ($photoNode->hasAttribute('href')) {
200                 $url = $photoNode->getAttribute('href');
201             } else {
202                 $url = $photoNode->textContent;
203             }
204             $hcard['photo'][] = self::_rel2abs($url, $base);
205         }
206
207         $singles = array('nickname', 'note', 'fn', 'n', 'adr');
208
209         foreach ($singles as $single) {
210
211             $nodes = self::_getChildrenByClass($hcardNode, $single, $xp);
212
213             if ($nodes->length > 0) {
214                 $node = $nodes->item(0);
215                 $hcard[$single] = $node->textContent;
216             }
217         }
218
219         return $hcard;
220     }
221
222     // XXX: this is a first pass; we probably need
223     // to handle things like ../ and ./ and so on
224
225     static function _rel2abs($rel, $wrt)
226     {
227         $parts = parse_url($rel);
228
229         if ($parts === false) {
230             return false;
231         }
232
233         // If it's got a scheme, use it
234
235         if (!empty($parts['scheme'])) {
236             return $rel;
237         }
238
239         $w = parse_url($wrt);
240
241         $base = $w['scheme'].'://'.$w['host'];
242
243         if ($rel[0] == '/') {
244             return $base.$rel;
245         }
246
247         $wp = explode('/', $w['path']);
248
249         array_pop($wp);
250
251         return $base.implode('/', $wp).'/'.$rel;
252     }
253 }