From f21f78364a9cbde2ca535a3983b384707ad097ae Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 16 Mar 2010 11:25:18 -0500 Subject: Change the workflow to get better discovery Tried to re-structure the workflow of discovery to get more and richer data and hints. --- plugins/OStatus/lib/discoveryhints.php | 182 +++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 plugins/OStatus/lib/discoveryhints.php (limited to 'plugins/OStatus/lib/discoveryhints.php') diff --git a/plugins/OStatus/lib/discoveryhints.php b/plugins/OStatus/lib/discoveryhints.php new file mode 100644 index 000000000..db13793dd --- /dev/null +++ b/plugins/OStatus/lib/discoveryhints.php @@ -0,0 +1,182 @@ +. + */ + +class DiscoveryHints { + + static function fromXRD($xrd) + { + $hints = array(); + + foreach ($xrd->links as $link) { + switch ($link['rel']) { + case Discovery::PROFILEPAGE: + $hints['profileurl'] = $link['href']; + break; + case Salmon::NS_REPLIES: + $hints['salmon'] = $link['href']; + break; + case Discovery::UPDATESFROM: + $hints['feedurl'] = $link['href']; + break; + case Discovery::HCARD: + $hints['hcardurl'] = $link['href']; + break; + default: + break; + } + } + + return $hints; + } + + static function fromHcardUrl($url) + { + $client = new HTTPClient(); + $client->setHeader('Accept', 'text/html,application/xhtml+xml'); + $response = $client->get($url); + + if (!$response->isOk()) { + return null; + } + + return self::hcardHints($response->getBody(), + $response->getUrl()); + } + + static function hcardHints($body, $url) + { + common_debug("starting tidy"); + + $body = self::_tidy($body); + + common_debug("done with tidy"); + + set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/plugins/OStatus/extlib/hkit/'); + require_once('hkit.class.php'); + + $h = new hKit; + + $hcards = $h->getByString('hcard', $body); + + if (empty($hcards)) { + return array(); + } + + if (count($hcards) == 1) { + $hcard = $hcards[0]; + } else { + foreach ($hcards as $try) { + if (array_key_exists('url', $try)) { + if (is_string($try['url']) && $try['url'] == $url) { + $hcard = $try; + break; + } else if (is_array($try['url'])) { + foreach ($try['url'] as $tryurl) { + if ($tryurl == $url) { + $hcard = $try; + break 2; + } + } + } + } + } + // last chance; grab the first one + if (empty($hcard)) { + $hcard = $hcards[0]; + } + } + + $hints = array(); + + if (array_key_exists('nickname', $hcard)) { + $hints['nickname'] = $hcard['nickname']; + } + + if (array_key_exists('fn', $hcard)) { + $hints['fullname'] = $hcard['fn']; + } else if (array_key_exists('n', $hcard)) { + $hints['fullname'] = implode(' ', $hcard['n']); + } + + if (array_key_exists('photo', $hcard)) { + $hints['avatar'] = $hcard['photo']; + } + + if (array_key_exists('note', $hcard)) { + $hints['bio'] = $hcard['note']; + } + + if (array_key_exists('adr', $hcard)) { + if (is_string($hcard['adr'])) { + $hints['location'] = $hcard['adr']; + } else if (is_array($hcard['adr'])) { + $hints['location'] = implode(' ', $hcard['adr']); + } + } + + if (array_key_exists('url', $hcard)) { + if (is_string($hcard['url'])) { + $hints['homepage'] = $hcard['url']; + } else if (is_array($hcard['url'])) { + // HACK get the last one; that's how our hcards look + $hints['homepage'] = $hcard['url'][count($hcard['url'])-1]; + } + } + + return $hints; + } + + private static function _tidy($body) + { + if (function_exists('tidy_parse_string')) { + common_debug("Tidying with extension"); + $text = tidy_parse_string($body); + $text = tidy_clean_repair($text); + return $body; + } else if ($fullpath = self::_findProgram('tidy')) { + common_debug("Tidying with program $fullpath"); + $tempfile = tempnam('/tmp', 'snht'); // statusnet hcard tidy + file_put_contents($tempfile, $source); + exec("$fullpath -utf8 -indent -asxhtml -numeric -bare -quiet $tempfile", $tidy); + unlink($tempfile); + return implode("\n", $tidy); + } else { + common_debug("Not tidying."); + return $body; + } + } + + private static function _findProgram($name) + { + $path = $_ENV['PATH']; + + $parts = explode(':', $path); + + foreach ($parts as $part) { + $fullpath = $part . '/' . $name; + if (is_executable($fullpath)) { + return $fullpath; + } + } + + return null; + } +} -- cgit v1.2.3-54-g00ecf