X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FOStatus%2Flib%2Fdiscoveryhints.php;h=4df7690038f6741347b13766c06175a213ae54c6;hb=e2d85a39e7f82ceef2cb2fe66a3a9a62ac9a363e;hp=db13793ddef0a965be3938d032c0479d57a12cf1;hpb=ca5612c451e4dabde107ff2cfbc737a2f69136df;p=quix0rs-gnu-social.git diff --git a/plugins/OStatus/lib/discoveryhints.php b/plugins/OStatus/lib/discoveryhints.php index db13793dde..4df7690038 100644 --- a/plugins/OStatus/lib/discoveryhints.php +++ b/plugins/OStatus/lib/discoveryhints.php @@ -20,28 +20,35 @@ */ class DiscoveryHints { - - static function fromXRD($xrd) + static function fromXRD(XML_XRD $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; + if (Event::handle('StartDiscoveryHintsFromXRD', array($xrd, &$hints))) { + foreach ($xrd->links as $link) { + switch ($link->rel) { + case WebFingerResource_Profile::PROFILEPAGE: + $hints['profileurl'] = $link->href; + break; + case Salmon::REL_SALMON: + case Salmon::NS_MENTIONS: // XXX: deprecated, remove in the future + case Salmon::NS_REPLIES: // XXX: deprecated, remove in the future + $hints['salmon'] = $link->href; + break; + case Discovery::UPDATESFROM: + if (empty($link->type) || $link->type == 'application/atom+xml') { + $hints['feedurl'] = $link->href; + } + break; + case Discovery::HCARD: + case Discovery::MF2_HCARD: + $hints['hcard'] = $link->href; + break; + default: + break; + } } + Event::handle('EndDiscoveryHintsFromXRD', array($xrd, &$hints)); } return $hints; @@ -63,118 +70,71 @@ class DiscoveryHints { 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'); + $hcard = self::_hcard($body, $url); - $h = new hKit; - - $hcards = $h->getByString('hcard', $body); - - if (empty($hcards)) { + if (empty($hcard)) { 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']; + // XXX: don't copy stuff into an array and then copy it again + + if (array_key_exists('nickname', $hcard) && !empty($hcard['nickname'][0])) { + $hints['nickname'] = $hcard['nickname'][0]; } - 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('name', $hcard) && !empty($hcard['name'][0])) { + $hints['fullname'] = $hcard['name'][0]; } - if (array_key_exists('photo', $hcard)) { - $hints['avatar'] = $hcard['photo']; + if (array_key_exists('photo', $hcard) && count($hcard['photo'])) { + $hints['avatar'] = $hcard['photo'][0]; } - if (array_key_exists('note', $hcard)) { - $hints['bio'] = $hcard['note']; + if (array_key_exists('note', $hcard) && !empty($hcard['note'][0])) { + $hints['bio'] = $hcard['note'][0]; } - 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('adr', $hcard) && !empty($hcard['adr'][0])) { + $hints['location'] = $hcard['adr'][0]['value']; } - 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]; - } + if (array_key_exists('url', $hcard) && !empty($hcard['url'][0])) { + $hints['homepage'] = $hcard['url'][0]; } return $hints; } - private static function _tidy($body) + static function _hcard($body, $url) { - 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; + $mf2 = new Mf2\Parser($body, $url); + $mf2 = $mf2->parse(); + + if (empty($mf2['items'])) { + return null; } - } - private static function _findProgram($name) - { - $path = $_ENV['PATH']; + $hcards = array(); - $parts = explode(':', $path); + foreach ($mf2['items'] as $item) { + if (!in_array('h-card', $item['type'])) { + continue; + } - foreach ($parts as $part) { - $fullpath = $part . '/' . $name; - if (is_executable($fullpath)) { - return $fullpath; + // We found a match, return it immediately + if (isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) { + return $item['properties']; } + + // Let's keep all the hcards for later, to return one of them at least + $hcards[] = $item['properties']; + } + + // No match immediately for the url we expected, but there were h-cards found + if (count($hcards) > 0) { + return $hcards[0]; } return null;