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 {
24 static function fromXRD($xrd)
28 foreach ($xrd->links as $link) {
29 switch ($link['rel']) {
30 case Discovery::PROFILEPAGE:
31 $hints['profileurl'] = $link['href'];
33 case Salmon::NS_MENTIONS:
34 case Salmon::NS_REPLIES:
35 $hints['salmon'] = $link['href'];
37 case Discovery::UPDATESFROM:
38 $hints['feedurl'] = $link['href'];
40 case Discovery::HCARD:
41 $hints['hcardurl'] = $link['href'];
51 static function fromHcardUrl($url)
53 $client = new HTTPClient();
54 $client->setHeader('Accept', 'text/html,application/xhtml+xml');
55 $response = $client->get($url);
57 if (!$response->isOk()) {
61 return self::hcardHints($response->getBody(),
65 static function hcardHints($body, $url)
67 $hcard = self::_hcard($body, $url);
75 // XXX: don't copy stuff into an array and then copy it again
77 if (array_key_exists('nickname', $hcard)) {
78 $hints['nickname'] = $hcard['nickname'];
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']);
87 if (array_key_exists('photo', $hcard) && count($hcard['photo'])) {
88 $hints['avatar'] = $hcard['photo'][0];
91 if (array_key_exists('note', $hcard)) {
92 $hints['bio'] = $hcard['note'];
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']);
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];
115 static function _hcard($body, $url)
117 // DOMDocument::loadHTML may throw warnings on unrecognized elements,
118 // and notices on unrecognized namespaces.
120 $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
122 $doc = new DOMDocument();
123 $doc->loadHTML($body);
125 error_reporting($old);
127 $xp = new DOMXPath($doc);
129 $hcardNodes = self::_getChildrenByClass($doc->documentElement, 'vcard', $xp);
133 for ($i = 0; $i < $hcardNodes->length; $i++) {
135 $hcardNode = $hcardNodes->item($i);
137 $hcard = self::_hcardFromNode($hcardNode, $xp, $url);
144 foreach ($hcards as $hcard) {
145 if (in_array($url, $hcard['url'])) {
151 if (!is_null($repr)) {
153 } else if (count($hcards) > 0) {
160 function _getChildrenByClass($el, $cls, $xp)
162 // borrowed from hkit. Thanks dudes!
164 $qry = ".//*[contains(concat(' ',normalize-space(@class),' '),' $cls ')]";
166 $nodes = $xp->query($qry, $el);
171 function _hcardFromNode($hcardNode, $xp, $base)
175 $hcard['url'] = array();
177 $urlNodes = self::_getChildrenByClass($hcardNode, 'url', $xp);
179 for ($j = 0; $j < $urlNodes->length; $j++) {
181 $urlNode = $urlNodes->item($j);
183 if ($urlNode->hasAttribute('href')) {
184 $url = $urlNode->getAttribute('href');
186 $url = $urlNode->textContent;
189 $hcard['url'][] = self::_rel2abs($url, $base);
192 $hcard['photo'] = array();
194 $photoNodes = self::_getChildrenByClass($hcardNode, 'photo', $xp);
196 for ($j = 0; $j < $photoNodes->length; $j++) {
197 $photoNode = $photoNodes->item($j);
198 if ($photoNode->hasAttribute('src')) {
199 $url = $photoNode->getAttribute('src');
200 } else if ($photoNode->hasAttribute('href')) {
201 $url = $photoNode->getAttribute('href');
203 $url = $photoNode->textContent;
205 $hcard['photo'][] = self::_rel2abs($url, $base);
208 $singles = array('nickname', 'note', 'fn', 'n', 'adr');
210 foreach ($singles as $single) {
212 $nodes = self::_getChildrenByClass($hcardNode, $single, $xp);
214 if ($nodes->length > 0) {
215 $node = $nodes->item(0);
216 $hcard[$single] = $node->textContent;
223 // XXX: this is a first pass; we probably need
224 // to handle things like ../ and ./ and so on
226 static function _rel2abs($rel, $wrt)
228 $parts = parse_url($rel);
230 if ($parts === false) {
234 // If it's got a scheme, use it
236 if (!empty($parts['scheme'])) {
240 $w = parse_url($wrt);
242 $base = $w['scheme'].'://'.$w['host'];
244 if ($rel[0] == '/') {
248 $wp = explode('/', $w['path']);
252 return $base.implode('/', $wp).'/'.$rel;