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 $hints['feedurl'] = $link['href'];
39 case Discovery::HCARD:
40 $hints['hcardurl'] = $link['href'];
50 static function fromHcardUrl($url)
52 $client = new HTTPClient();
53 $client->setHeader('Accept', 'text/html,application/xhtml+xml');
54 $response = $client->get($url);
56 if (!$response->isOk()) {
60 return self::hcardHints($response->getBody(),
64 static function hcardHints($body, $url)
66 $hcard = self::_hcard($body, $url);
74 // XXX: don't copy stuff into an array and then copy it again
76 if (array_key_exists('nickname', $hcard)) {
77 $hints['nickname'] = $hcard['nickname'];
80 if (array_key_exists('fn', $hcard)) {
81 $hints['fullname'] = $hcard['fn'];
82 } else if (array_key_exists('n', $hcard)) {
83 $hints['fullname'] = implode(' ', $hcard['n']);
86 if (array_key_exists('photo', $hcard) && count($hcard['photo'])) {
87 $hints['avatar'] = $hcard['photo'][0];
90 if (array_key_exists('note', $hcard)) {
91 $hints['bio'] = $hcard['note'];
94 if (array_key_exists('adr', $hcard)) {
95 if (is_string($hcard['adr'])) {
96 $hints['location'] = $hcard['adr'];
97 } else if (is_array($hcard['adr'])) {
98 $hints['location'] = implode(' ', $hcard['adr']);
102 if (array_key_exists('url', $hcard)) {
103 if (is_string($hcard['url'])) {
104 $hints['homepage'] = $hcard['url'];
105 } else if (is_array($hcard['url']) && !empty($hcard['url'])) {
106 // HACK get the last one; that's how our hcards look
107 $hints['homepage'] = $hcard['url'][count($hcard['url'])-1];
114 static function _hcard($body, $url)
116 // DOMDocument::loadHTML may throw warnings on unrecognized elements,
117 // and notices on unrecognized namespaces.
119 $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
121 $doc = new DOMDocument();
122 $doc->loadHTML($body);
124 error_reporting($old);
126 $xp = new DOMXPath($doc);
128 $hcardNodes = self::_getChildrenByClass($doc->documentElement, 'vcard', $xp);
132 for ($i = 0; $i < $hcardNodes->length; $i++) {
134 $hcardNode = $hcardNodes->item($i);
136 $hcard = self::_hcardFromNode($hcardNode, $xp, $url);
143 foreach ($hcards as $hcard) {
144 if (in_array($url, $hcard['url'])) {
150 if (!is_null($repr)) {
152 } else if (count($hcards) > 0) {
159 function _getChildrenByClass($el, $cls, $xp)
161 // borrowed from hkit. Thanks dudes!
163 $qry = ".//*[contains(concat(' ',normalize-space(@class),' '),' $cls ')]";
165 $nodes = $xp->query($qry, $el);
170 function _hcardFromNode($hcardNode, $xp, $base)
174 $hcard['url'] = array();
176 $urlNodes = self::_getChildrenByClass($hcardNode, 'url', $xp);
178 for ($j = 0; $j < $urlNodes->length; $j++) {
180 $urlNode = $urlNodes->item($j);
182 if ($urlNode->hasAttribute('href')) {
183 $url = $urlNode->getAttribute('href');
185 $url = $urlNode->textContent;
188 $hcard['url'][] = self::_rel2abs($url, $base);
191 $hcard['photo'] = array();
193 $photoNodes = self::_getChildrenByClass($hcardNode, 'photo', $xp);
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');
202 $url = $photoNode->textContent;
204 $hcard['photo'][] = self::_rel2abs($url, $base);
207 $singles = array('nickname', 'note', 'fn', 'n', 'adr');
209 foreach ($singles as $single) {
211 $nodes = self::_getChildrenByClass($hcardNode, $single, $xp);
213 if ($nodes->length > 0) {
214 $node = $nodes->item(0);
215 $hcard[$single] = $node->textContent;
222 // XXX: this is a first pass; we probably need
223 // to handle things like ../ and ./ and so on
225 static function _rel2abs($rel, $wrt)
227 $parts = parse_url($rel);
229 if ($parts === false) {
233 // If it's got a scheme, use it
235 if (!empty($parts['scheme'])) {
239 $w = parse_url($wrt);
241 $base = $w['scheme'].'://'.$w['host'];
243 if ($rel[0] == '/') {
247 $wp = explode('/', $w['path']);
251 return $base.implode('/', $wp).'/'.$rel;