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_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 common_debug("starting tidy");
68 $body = self::_tidy($body);
70 common_debug("done with tidy");
72 set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/plugins/OStatus/extlib/hkit/');
73 require_once('hkit.class.php');
77 $hcards = $h->getByString('hcard', $body);
83 if (count($hcards) == 1) {
86 foreach ($hcards as $try) {
87 if (array_key_exists('url', $try)) {
88 if (is_string($try['url']) && $try['url'] == $url) {
91 } else if (is_array($try['url'])) {
92 foreach ($try['url'] as $tryurl) {
93 if ($tryurl == $url) {
101 // last chance; grab the first one
109 if (array_key_exists('nickname', $hcard)) {
110 $hints['nickname'] = $hcard['nickname'];
113 if (array_key_exists('fn', $hcard)) {
114 $hints['fullname'] = $hcard['fn'];
115 } else if (array_key_exists('n', $hcard)) {
116 $hints['fullname'] = implode(' ', $hcard['n']);
119 if (array_key_exists('photo', $hcard)) {
120 $hints['avatar'] = $hcard['photo'];
123 if (array_key_exists('note', $hcard)) {
124 $hints['bio'] = $hcard['note'];
127 if (array_key_exists('adr', $hcard)) {
128 if (is_string($hcard['adr'])) {
129 $hints['location'] = $hcard['adr'];
130 } else if (is_array($hcard['adr'])) {
131 $hints['location'] = implode(' ', $hcard['adr']);
135 if (array_key_exists('url', $hcard)) {
136 if (is_string($hcard['url'])) {
137 $hints['homepage'] = $hcard['url'];
138 } else if (is_array($hcard['url'])) {
139 // HACK get the last one; that's how our hcards look
140 $hints['homepage'] = $hcard['url'][count($hcard['url'])-1];
147 private static function _tidy($body)
149 if (function_exists('tidy_parse_string')) {
150 common_debug("Tidying with extension");
151 $text = tidy_parse_string($body);
152 $text = tidy_clean_repair($text);
154 } else if ($fullpath = self::_findProgram('tidy')) {
155 common_debug("Tidying with program $fullpath");
156 $tempfile = tempnam('/tmp', 'snht'); // statusnet hcard tidy
157 file_put_contents($tempfile, $source);
158 exec("$fullpath -utf8 -indent -asxhtml -numeric -bare -quiet $tempfile", $tidy);
160 return implode("\n", $tidy);
162 common_debug("Not tidying.");
167 private static function _findProgram($name)
169 $path = $_ENV['PATH'];
171 $parts = explode(':', $path);
173 foreach ($parts as $part) {
174 $fullpath = $part . '/' . $name;
175 if (is_executable($fullpath)) {