123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- class DiscoveryHints {
- static function fromXRD(XML_XRD $xrd)
- {
- $hints = array();
- 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:
- case Salmon::NS_REPLIES:
- $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;
- }
- }
- 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)
- {
- $hcard = self::_hcard($body, $url);
- if (empty($hcard)) {
- return array();
- }
- $hints = array();
-
- if (array_key_exists('nickname', $hcard) && !empty($hcard['nickname'][0])) {
- $hints['nickname'] = $hcard['nickname'][0];
- }
- if (array_key_exists('name', $hcard) && !empty($hcard['name'][0])) {
- $hints['fullname'] = $hcard['name'][0];
- }
- if (array_key_exists('photo', $hcard) && count($hcard['photo'])) {
- $hints['avatar'] = $hcard['photo'][0];
- }
- if (array_key_exists('note', $hcard) && !empty($hcard['note'][0])) {
- $hints['bio'] = $hcard['note'][0];
- }
- if (array_key_exists('adr', $hcard) && !empty($hcard['adr'][0])) {
- $hints['location'] = $hcard['adr'][0]['value'];
- }
- if (array_key_exists('url', $hcard) && !empty($hcard['url'][0])) {
- $hints['homepage'] = $hcard['url'][0];
- }
- return $hints;
- }
- static function _hcard($body, $url)
- {
- $mf2 = new Mf2\Parser($body, $url);
- $mf2 = $mf2->parse();
-
- if (empty($mf2['items'])) {
- return null;
- }
- $hcards = array();
- foreach ($mf2['items'] as $item) {
- if (!in_array('h-card', $item['type'])) {
- continue;
- }
-
- if (isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) {
- return $item['properties'];
- }
-
- $hcards[] = $item['properties'];
- }
-
- if (count($hcards) > 0) {
- return $hcards[0];
- }
- return null;
- }
- }
|