3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Some utilities for generating hint data
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.
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.
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/>.
22 class DiscoveryHints {
23 static function fromXRD($xrd)
27 foreach ($xrd->links as $link) {
28 switch ($link['rel']) {
29 case Discovery::PROFILEPAGE:
30 $hints['profileurl'] = $link['href'];
32 case Salmon::NS_MENTIONS:
33 case Salmon::NS_REPLIES:
34 $hints['salmon'] = $link['href'];
36 case Discovery::UPDATESFROM:
37 if (empty($link['type']) || $link['type'] == 'application/atom+xml') {
38 $hints['feedurl'] = $link['href'];
41 case Discovery::HCARD:
42 $hints['hcardurl'] = $link['href'];
52 static function fromHcardUrl($url)
54 $client = new HTTPClient();
55 $client->setHeader('Accept', 'text/html,application/xhtml+xml');
56 $response = $client->get($url);
58 if (!$response->isOk()) {
62 return self::hcardHints($response->getBody(),
66 static function hcardHints($body, $url)
68 $hcard = self::_hcard($body, $url);
76 // XXX: don't copy stuff into an array and then copy it again
78 if (array_key_exists('nickname', $hcard)) {
79 $hints['nickname'] = $hcard['nickname'];
82 if (array_key_exists('fn', $hcard)) {
83 $hints['fullname'] = $hcard['fn'];
84 } else if (array_key_exists('n', $hcard)) {
85 $hints['fullname'] = implode(' ', $hcard['n']);
88 if (array_key_exists('photo', $hcard) && count($hcard['photo'])) {
89 $hints['avatar'] = $hcard['photo'][0];
92 if (array_key_exists('note', $hcard)) {
93 $hints['bio'] = $hcard['note'];
96 if (array_key_exists('adr', $hcard)) {
97 if (is_string($hcard['adr'])) {
98 $hints['location'] = $hcard['adr'];
99 } else if (is_array($hcard['adr'])) {
100 $hints['location'] = implode(' ', $hcard['adr']);
104 if (array_key_exists('url', $hcard)) {
105 if (is_string($hcard['url'])) {
106 $hints['homepage'] = $hcard['url'];
107 } else if (is_array($hcard['url']) && !empty($hcard['url'])) {
108 // HACK get the last one; that's how our hcards look
109 $hints['homepage'] = $hcard['url'][count($hcard['url'])-1];
116 static function _hcard($body, $url)
118 // DOMDocument::loadHTML may throw warnings on unrecognized elements,
119 // and notices on unrecognized namespaces.
121 $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
123 $doc = new DOMDocument();
124 $doc->loadHTML($body);
126 error_reporting($old);
128 $xp = new DOMXPath($doc);
130 $hcardNodes = self::_getChildrenByClass($doc->documentElement, 'vcard', $xp);
134 for ($i = 0; $i < $hcardNodes->length; $i++) {
136 $hcardNode = $hcardNodes->item($i);
138 $hcard = self::_hcardFromNode($hcardNode, $xp, $url);
145 foreach ($hcards as $hcard) {
146 if (in_array($url, $hcard['url'])) {
152 if (!is_null($repr)) {
154 } else if (count($hcards) > 0) {
161 function _getChildrenByClass($el, $cls, $xp)
163 // borrowed from hkit. Thanks dudes!
165 $qry = ".//*[contains(concat(' ',normalize-space(@class),' '),' $cls ')]";
167 $nodes = $xp->query($qry, $el);
172 function _hcardFromNode($hcardNode, $xp, $base)
176 $hcard['url'] = array();
178 $urlNodes = self::_getChildrenByClass($hcardNode, 'url', $xp);
180 for ($j = 0; $j < $urlNodes->length; $j++) {
182 $urlNode = $urlNodes->item($j);
184 if ($urlNode->hasAttribute('href')) {
185 $url = $urlNode->getAttribute('href');
187 $url = $urlNode->textContent;
190 $hcard['url'][] = self::_rel2abs($url, $base);
193 $hcard['photo'] = array();
195 $photoNodes = self::_getChildrenByClass($hcardNode, 'photo', $xp);
197 for ($j = 0; $j < $photoNodes->length; $j++) {
198 $photoNode = $photoNodes->item($j);
199 if ($photoNode->hasAttribute('src')) {
200 $url = $photoNode->getAttribute('src');
201 } else if ($photoNode->hasAttribute('href')) {
202 $url = $photoNode->getAttribute('href');
204 $url = $photoNode->textContent;
206 $hcard['photo'][] = self::_rel2abs($url, $base);
209 $singles = array('nickname', 'note', 'fn', 'n', 'adr');
211 foreach ($singles as $single) {
213 $nodes = self::_getChildrenByClass($hcardNode, $single, $xp);
215 if ($nodes->length > 0) {
216 $node = $nodes->item(0);
217 $hcard[$single] = $node->textContent;
224 // XXX: this is a first pass; we probably need
225 // to handle things like ../ and ./ and so on
227 static function _rel2abs($rel, $wrt)
229 $parts = parse_url($rel);
231 if ($parts === false) {
235 // If it's got a scheme, use it
237 if (!empty($parts['scheme'])) {
241 $w = parse_url($wrt);
243 $base = $w['scheme'].'://'.$w['host'];
245 if ($rel[0] == '/') {
249 $wp = explode('/', $w['path']);
253 return $base.implode('/', $wp).'/'.$rel;