X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FNetwork%2FProbe.php;h=e1bedf5e5358e1fa51bf9e26aa2d6d4c455a7d9b;hb=ba4860b7879f0dc6ea7a2dd0d09e698a913a9129;hp=00df1758d2501b0d60c5d90e0079c4fd7c4fdd93;hpb=e56a53647bd5469551bf4f9ef2df50a5dd16b943;p=friendica.git diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 00df1758d2..e1bedf5e53 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -1,6 +1,6 @@ getScheme()) { + return $uri->__toString(); } // Remove the URL fragment, since these shouldn't be part of any profile URL - unset($parts['fragment']); - - $URI = Network::unparseURL($parts); + $uri = $uri->withFragment(''); - return $URI; + return $uri->__toString(); } /** * Rearrange the array so that it always has the same order * * @param array $data Unordered data - * * @return array Ordered data */ - private static function rearrangeData($data) + private static function rearrangeData(array $data): array { - $fields = ["name", "nick", "guid", "url", "addr", "alias", "photo", "header", - "account-type", "community", "keywords", "location", "about", "xmpp", "matrix", - "hide", "batch", "notify", "poll", "request", "confirm", "subscribe", "poco", - "following", "followers", "inbox", "outbox", "sharedinbox", - "priority", "network", "pubkey", "manually-approve", "baseurl", "gsid"]; + $fields = ['name', 'given_name', 'family_name', 'nick', 'guid', 'url', 'addr', 'alias', + 'photo', 'photo_medium', 'photo_small', 'header', + 'account-type', 'community', 'keywords', 'location', 'about', 'xmpp', 'matrix', + 'hide', 'batch', 'notify', 'poll', 'request', 'confirm', 'subscribe', 'poco', + 'following', 'followers', 'inbox', 'outbox', 'sharedinbox', + 'priority', 'network', 'pubkey', 'manually-approve', 'baseurl', 'gsid']; - $numeric_fields = ["gsid", "hide", "account-type", "manually-approve"]; + $numeric_fields = ['gsid', 'hide', 'account-type', 'manually-approve']; + + if (!empty($data['photo']) && !Network::isValidHttpUrl($data['photo'])) { + Logger::info('Invalid URL for photo', ['url' => $data['url'], 'photo' => $data['photo']]); + unset($data['photo']); + } $newdata = []; foreach ($fields as $field) { @@ -103,17 +131,37 @@ class Probe if (in_array($field, $numeric_fields)) { $newdata[$field] = (int)$data[$field]; } else { - $newdata[$field] = $data[$field]; + $newdata[$field] = trim($data[$field]); } } elseif (!in_array($field, $numeric_fields)) { - $newdata[$field] = ""; + $newdata[$field] = ''; } else { $newdata[$field] = null; } } + $newdata['networks'] = []; + foreach ([Protocol::DIASPORA, Protocol::OSTATUS] as $network) { + if (!empty($data['networks'][$network])) { + $data['networks'][$network]['subscribe'] = $newdata['subscribe'] ?? ''; + if (empty($data['networks'][$network]['baseurl'])) { + $data['networks'][$network]['baseurl'] = $newdata['baseurl'] ?? ''; + } else { + $newdata['baseurl'] = $data['networks'][$network]['baseurl']; + } + if (!empty($newdata['baseurl'])) { + $newdata['gsid'] = $data['networks'][$network]['gsid'] = GServer::getID($newdata['baseurl']); + } else { + $newdata['gsid'] = $data['networks'][$network]['gsid'] = null; + } + + $newdata['networks'][$network] = self::rearrangeData($data['networks'][$network]); + unset($newdata['networks'][$network]['networks']); + } + } + // We don't use the "priority" field anymore and replace it with a dummy. - $newdata["priority"] = 0; + $newdata['priority'] = 0; return $newdata; } @@ -122,17 +170,16 @@ class Probe * Check if the hostname belongs to the own server * * @param string $host The hostname that is to be checked - * * @return bool Does the testes hostname belongs to the own server? */ - private static function ownHost($host) + private static function ownHost(string $host): bool { $own_host = DI::baseUrl()->getHostname(); $parts = parse_url($host); if (!isset($parts['scheme'])) { - $parts = parse_url('http://'.$host); + $parts = parse_url('http://' . $host); } if (!isset($parts['host'])) { @@ -152,17 +199,17 @@ class Probe * @return array with template and type of the webfinger template for JSON or XML * @throws HTTPException\InternalServerErrorException */ - private static function hostMeta($host) + private static function hostMeta(string $host): array { // Reset the static variable self::$baseurl = ''; // Handles the case when the hostname contains the scheme if (!parse_url($host, PHP_URL_SCHEME)) { - $ssl_url = "https://" . $host . "/.well-known/host-meta"; - $url = "http://" . $host . "/.well-known/host-meta"; + $ssl_url = 'https://' . $host . self::HOST_META; + $url = 'http://' . $host . self::HOST_META; } else { - $ssl_url = $host . "/.well-known/host-meta"; + $ssl_url = $host . self::HOST_META; $url = ''; } @@ -171,7 +218,7 @@ class Probe Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url, 'callstack' => System::callstack(20)]); $xrd = null; - $curlResult = DI::httpClient()->get($ssl_url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => ['application/xrd+xml']]); + $curlResult = DI::httpClient()->get($ssl_url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]); $ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0); if ($curlResult->isSuccess()) { $xml = $curlResult->getBody(); @@ -183,19 +230,19 @@ class Probe } } elseif ($curlResult->isTimeout()) { Logger::info('Probing timeout', ['url' => $ssl_url]); - self::$istimeout = true; + self::$isTimeout = true; return []; } if (!is_object($xrd) && !empty($url)) { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => ['application/xrd+xml']]); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]); $connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0); if ($curlResult->isTimeout()) { Logger::info('Probing timeout', ['url' => $url]); - self::$istimeout = true; + self::$isTimeout = true; return []; } elseif ($connection_error && $ssl_connection_error) { - self::$istimeout = true; + self::$isTimeout = true; return []; } @@ -209,26 +256,26 @@ class Probe } $links = XML::elementToArray($xrd); - if (!isset($links["xrd"]["link"])) { + if (!isset($links['xrd']['link'])) { Logger::info('No xrd data found', ['host' => $host]); return []; } $lrdd = []; - foreach ($links["xrd"]["link"] as $value => $link) { - if (!empty($link["@attributes"])) { - $attributes = $link["@attributes"]; - } elseif ($value == "@attributes") { + foreach ($links['xrd']['link'] as $value => $link) { + if (!empty($link['@attributes'])) { + $attributes = $link['@attributes']; + } elseif ($value == '@attributes') { $attributes = $link; } else { continue; } - if (!empty($attributes["rel"]) && $attributes["rel"] == "lrdd" && !empty($attributes["template"])) { - $type = (empty($attributes["type"]) ? '' : $attributes["type"]); + if (!empty($attributes['rel']) && $attributes['rel'] == 'lrdd' && !empty($attributes['template'])) { + $type = (empty($attributes['type']) ? '' : $attributes['type']); - $lrdd[$type] = $attributes["template"]; + $lrdd[$type] = $attributes['template']; } } @@ -244,58 +291,14 @@ class Probe return $lrdd; } - /** - * Perform Webfinger lookup and return DFRN data - * - * Given an email style address, perform webfinger lookup and - * return the resulting DFRN profile URL, or if no DFRN profile URL - * is located, returns an OStatus subscription template (prefixed - * with the string 'stat:' to identify it as on OStatus template). - * If this isn't an email style address just return $webbie. - * Return an empty string if email-style addresses but webfinger fails, - * or if the resultant personal XRD doesn't contain a supported - * subscription/friend-request attribute. - * - * amended 7/9/2011 to return an hcard which could save potentially loading - * a lengthy content page to scrape dfrn attributes - * - * @param string $webbie Address that should be probed - * @param string $hcard_url Link to the hcard - is returned by reference - * - * @return string profile link - * @throws HTTPException\InternalServerErrorException - */ - public static function webfingerDfrn(string $webbie, string &$hcard_url) - { - $profile_link = ''; - - $links = self::lrdd($webbie); - Logger::debug('Result', ['url' => $webbie, 'links' => $links]); - if (!empty($links) && is_array($links)) { - foreach ($links as $link) { - if ($link['@attributes']['rel'] === ActivityNamespace::DFRN) { - $profile_link = $link['@attributes']['href']; - } - if (($link['@attributes']['rel'] === ActivityNamespace::OSTATUSSUB) && ($profile_link == "")) { - $profile_link = 'stat:'.$link['@attributes']['template']; - } - if ($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard') { - $hcard_url = $link['@attributes']['href']; - } - } - } - return $profile_link; - } - /** * Check an URI for LRDD data * * @param string $uri Address that should be probed - * * @return array uri data * @throws HTTPException\InternalServerErrorException */ - public static function lrdd(string $uri) + public static function lrdd(string $uri): array { $data = self::getWebfingerArray($uri); if (empty($data)) { @@ -303,22 +306,25 @@ class Probe } $webfinger = $data['webfinger']; - if (empty($webfinger["links"])) { + if (empty($webfinger['links'])) { Logger::info('No webfinger links found', ['uri' => $uri]); return []; } $data = []; - foreach ($webfinger["links"] as $link) { - $data[] = ["@attributes" => $link]; + foreach ($webfinger['links'] as $link) { + $data[] = ['@attributes' => $link]; } - if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) { - foreach ($webfinger["aliases"] as $alias) { - $data[] = ["@attributes" => - ["rel" => "alias", - "href" => $alias]]; + if (!empty($webfinger['aliases']) && is_array($webfinger['aliases'])) { + foreach ($webfinger['aliases'] as $alias) { + $data[] = [ + '@attributes' => [ + 'rel' => 'alias', + 'href' => $alias, + ] + ]; } } @@ -337,7 +343,7 @@ class Probe * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function uri($uri, $network = '', $uid = -1) + public static function uri(string $uri, string $network = '', int $uid = -1): array { // Local profiles aren't probed via network if (empty($network) && Contact::isLocal($uri)) { @@ -348,7 +354,7 @@ class Probe } if ($uid == -1) { - $uid = local_user(); + $uid = DI::userSession()->getLocalUserId(); } if (empty($network) || ($network == Protocol::ACTIVITYPUB)) { @@ -357,7 +363,7 @@ class Probe $ap_profile = []; } - self::$istimeout = false; + self::$isTimeout = false; if ($network != Protocol::ACTIVITYPUB) { $data = self::detect($uri, $network, $uid, $ap_profile); @@ -365,7 +371,13 @@ class Probe $data = []; } if (empty($data) || (!empty($ap_profile) && empty($network) && (($data['network'] ?? '') != Protocol::DFRN))) { + $networks = $data['networks'] ?? []; + unset($data['networks']); + if (!empty($data['network'])) { + $networks[$data['network']] = $data; + } $data = $ap_profile; + $data['networks'] = $networks; } elseif (!empty($ap_profile)) { $ap_profile['batch'] = ''; $data = array_merge($ap_profile, $data); @@ -425,12 +437,11 @@ class Probe * Fetches the "hide" status from the profile * * @param string $url URL of the profile - * * @return boolean "hide" status */ - private static function getHideStatus($url) + private static function getHideStatus(string $url): bool { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000]); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::CONTENT_LENGTH => 1000000]); if (!$curlResult->isSuccess()) { return false; } @@ -485,11 +496,12 @@ class Probe /** * Fetch the "subscribe" and add it to the result * - * @param array $result - * @param array $webfinger - * @return array result + * @param array $result Result array + * @param array $webfinger Webfinger data + * + * @return array result Altered/unaltered result array */ - private static function getSubscribeLink(array $result, array $webfinger) + private static function getSubscribeLink(array $result, array $webfinger): array { if (empty($webfinger['links'])) { return $result; @@ -507,8 +519,9 @@ class Probe /** * Get webfinger data from a given URI * - * @param string $uri - * @return array + * @param string $uri URI + * + * @return array Webfinger data * @throws HTTPException\InternalServerErrorException */ private static function getWebfingerArray(string $uri): array @@ -532,7 +545,7 @@ class Probe $addr = $nick . '@' . $host; } - $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr); + $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); if (empty($webfinger)) { $lrdd = self::hostMeta($host); } @@ -546,7 +559,7 @@ class Probe $addr = $nick . '@' . $host; } - $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr); + $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); if (empty($webfinger)) { $lrdd = self::hostMeta($host); } @@ -564,14 +577,14 @@ class Probe $nick = substr($uri, 0, strpos($uri, '@')); $addr = $uri; - $webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr); - if (self::$istimeout) { + $webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); + if (self::$isTimeout) { return []; } if (empty($webfinger)) { - $webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr); - if (self::$istimeout) { + $webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); + if (self::$isTimeout) { return []; } } else { @@ -580,7 +593,7 @@ class Probe if (empty($webfinger)) { $lrdd = self::hostMeta($host); - if (self::$istimeout) { + if (self::$isTimeout) { return []; } $baseurl = self::$baseurl; @@ -623,9 +636,10 @@ class Probe * @param string $type * @param string $uri * @param string $addr + * * @return array webfinger results */ - private static function getWebfinger(string $template, string $type, string $uri, string $addr) + private static function getWebfinger(string $template, string $type, string $uri, string $addr): array { if (Network::isUrlBlocked($template)) { Logger::info('Domain is blocked', ['url' => $template]); @@ -635,9 +649,9 @@ class Probe // First try the address because this is the primary purpose of webfinger if (!empty($addr)) { $detected = $addr; - $path = str_replace('{uri}', urlencode("acct:" . $addr), $template); + $path = str_replace('{uri}', urlencode('acct:' . $addr), $template); $webfinger = self::webfinger($path, $type); - if (self::$istimeout) { + if (self::$isTimeout) { return []; } } @@ -647,7 +661,7 @@ class Probe $detected = $uri; $path = str_replace('{uri}', urlencode($uri), $template); $webfinger = self::webfinger($path, $type); - if (self::$istimeout) { + if (self::$isTimeout) { return []; } } @@ -668,11 +682,10 @@ class Probe * @param string $network Test for this specific network * @param integer $uid User ID for the probe (only used for mails) * @param array $ap_profile Previously probed AP profile - * - * @return array uri data + * @return array URI data * @throws HTTPException\InternalServerErrorException */ - private static function detect(string $uri, string $network, int $uid, array $ap_profile) + private static function detect(string $uri, string $network, int $uid, array $ap_profile): array { $hookData = [ 'uri' => $uri, @@ -688,22 +701,21 @@ class Probe } $parts = parse_url($uri); - - if (empty($parts['scheme']) || !empty($parts['host']) && strstr($uri, '@')) { - // If the URI starts with "mailto:" then jump directly to the mail detection - if (strpos($uri, 'mailto:') !== false) { - $uri = str_replace('mailto:', '', $uri); - return self::mail($uri, $uid); - } - - if ($network == Protocol::MAIL) { - return self::mail($uri, $uid); - } - } else { + if (empty($parts['scheme']) && empty($parts['host']) && (empty($parts['path']) || strpos($parts['path'], '@') === false)) { Logger::info('URI was not detectable', ['uri' => $uri]); return []; } + // If the URI starts with "mailto:" then jump directly to the mail detection + if (strpos($uri, 'mailto:') !== false) { + $uri = str_replace('mailto:', '', $uri); + return self::mail($uri, $uid); + } + + if ($network == Protocol::MAIL) { + return self::mail($uri, $uid); + } + Logger::info('Probing start', ['uri' => $uri]); if (!empty($ap_profile['addr']) && ($ap_profile['addr'] != $uri)) { @@ -731,50 +743,54 @@ class Probe $result = []; - if (in_array($network, ["", Protocol::DFRN])) { + if (in_array($network, ['', Protocol::DFRN])) { $result = self::dfrn($webfinger); } - if ((!$result && ($network == "")) || ($network == Protocol::DIASPORA)) { + if ((!$result && ($network == '')) || ($network == Protocol::DIASPORA)) { $result = self::diaspora($webfinger); + } else { + $result['networks'][Protocol::DIASPORA] = self::diaspora($webfinger); } - if ((!$result && ($network == "")) || ($network == Protocol::OSTATUS)) { + if ((!$result && ($network == '')) || ($network == Protocol::OSTATUS)) { $result = self::ostatus($webfinger); + } else { + $result['networks'][Protocol::OSTATUS] = self::ostatus($webfinger); } if (in_array($network, ['', Protocol::ZOT])) { $result = self::zot($webfinger, $result, $baseurl); } - if ((!$result && ($network == "")) || ($network == Protocol::PUMPIO)) { - $result = self::pumpio($webfinger, $addr); + if ((!$result && ($network == '')) || ($network == Protocol::PUMPIO)) { + $result = self::pumpio($webfinger, $addr, $baseurl); } if (empty($result['network']) && empty($ap_profile['network']) || ($network == Protocol::FEED)) { $result = self::feed($uri); } else { // We overwrite the detected nick with our try if the previois routines hadn't detected it. // Additionally it is overwritten when the nickname doesn't make sense (contains spaces). - if ((empty($result["nick"]) || (strstr($result["nick"], " "))) && ($nick != "")) { - $result["nick"] = $nick; + if ((empty($result['nick']) || (strstr($result['nick'], ' '))) && ($nick != '')) { + $result['nick'] = $nick; } - if (empty($result["addr"]) && ($addr != "")) { - $result["addr"] = $addr; + if (empty($result['addr']) && ($addr != '')) { + $result['addr'] = $addr; } } $result = self::getSubscribeLink($result, $webfinger); - if (empty($result["network"])) { - $result["network"] = Protocol::PHANTOM; + if (empty($result['network'])) { + $result['network'] = Protocol::PHANTOM; } if (empty($result['baseurl']) && !empty($baseurl)) { $result['baseurl'] = $baseurl; } - if (empty($result["url"])) { - $result["url"] = $uri; + if (empty($result['url'])) { + $result['url'] = $uri; } - Logger::info('Probing done', ['uri' => $uri, 'network' => $result["network"]]); + Logger::info('Probing done', ['uri' => $uri, 'network' => $result['network']]); return $result; } @@ -782,24 +798,25 @@ class Probe /** * Check for Zot contact * - * @param array $webfinger Webfinger data - * @param array $data previously probed data + * @param array $webfinger Webfinger data + * @param array $data previously probed data + * @param string $baseUrl Base URL * * @return array Zot data * @throws HTTPException\InternalServerErrorException */ - private static function zot($webfinger, $data, $baseurl) + private static function zot(array $webfinger, array $data, string $baseurl): array { - if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) { - foreach ($webfinger["aliases"] as $alias) { + if (!empty($webfinger['aliases']) && is_array($webfinger['aliases'])) { + foreach ($webfinger['aliases'] as $alias) { if (substr($alias, 0, 5) == 'acct:') { - $data["addr"] = substr($alias, 5); + $data['addr'] = substr($alias, 5); } } } - if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == "acct:")) { - $data["addr"] = substr($webfinger["subject"], 5); + if (!empty($webfinger['subject']) && (substr($webfinger['subject'], 0, 5) == 'acct:')) { + $data['addr'] = substr($webfinger['subject'], 5); } $zot_url = ''; @@ -834,9 +851,9 @@ class Probe return $data; } - public static function pollZot($url, $data) + public static function pollZot(string $url, array $data): array { - $curlResult = DI::httpClient()->get($url); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::JSON); if ($curlResult->isTimeout()) { return $data; } @@ -929,20 +946,20 @@ class Probe * @return array webfinger data * @throws HTTPException\InternalServerErrorException */ - public static function webfinger($url, $type) + public static function webfinger(string $url, string $type): array { $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20); - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => [$type]]); + $curlResult = DI::httpClient()->get($url, $type, [HttpClientOptions::TIMEOUT => $xrd_timeout]); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return []; } $data = $curlResult->getBody(); $webfinger = json_decode($data, true); if (!empty($webfinger)) { - if (!isset($webfinger["links"])) { + if (!isset($webfinger['links'])) { Logger::info('No json webfinger links', ['url' => $url]); return []; } @@ -957,33 +974,33 @@ class Probe } $xrd_arr = XML::elementToArray($xrd); - if (!isset($xrd_arr["xrd"]["link"])) { + if (!isset($xrd_arr['xrd']['link'])) { Logger::info('No XML webfinger links', ['url' => $url]); return []; } $webfinger = []; - if (!empty($xrd_arr["xrd"]["subject"])) { - $webfinger["subject"] = $xrd_arr["xrd"]["subject"]; + if (!empty($xrd_arr['xrd']['subject'])) { + $webfinger['subject'] = $xrd_arr['xrd']['subject']; } - if (!empty($xrd_arr["xrd"]["alias"])) { - $webfinger["aliases"] = $xrd_arr["xrd"]["alias"]; + if (!empty($xrd_arr['xrd']['alias'])) { + $webfinger['aliases'] = $xrd_arr['xrd']['alias']; } - $webfinger["links"] = []; + $webfinger['links'] = []; - foreach ($xrd_arr["xrd"]["link"] as $value => $data) { - if (!empty($data["@attributes"])) { - $attributes = $data["@attributes"]; - } elseif ($value == "@attributes") { + foreach ($xrd_arr['xrd']['link'] as $value => $data) { + if (!empty($data['@attributes'])) { + $attributes = $data['@attributes']; + } elseif ($value == '@attributes') { $attributes = $data; } else { continue; } - $webfinger["links"][] = $attributes; + $webfinger['links'][] = $attributes; } return $webfinger; } @@ -1000,11 +1017,11 @@ class Probe * @return array noscrape data * @throws HTTPException\InternalServerErrorException */ - private static function pollNoscrape($noscrape_url, $data) + private static function pollNoscrape(string $noscrape_url, array $data): array { - $curlResult = DI::httpClient()->get($noscrape_url); + $curlResult = DI::httpClient()->get($noscrape_url, HttpClientAccept::JSON); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return $data; } $content = $curlResult->getBody(); @@ -1019,78 +1036,78 @@ class Probe return $data; } - if (!empty($json["fn"])) { - $data["name"] = $json["fn"]; + if (!empty($json['fn'])) { + $data['name'] = $json['fn']; } - if (!empty($json["addr"])) { - $data["addr"] = $json["addr"]; + if (!empty($json['addr'])) { + $data['addr'] = $json['addr']; } - if (!empty($json["nick"])) { - $data["nick"] = $json["nick"]; + if (!empty($json['nick'])) { + $data['nick'] = $json['nick']; } - if (!empty($json["guid"])) { - $data["guid"] = $json["guid"]; + if (!empty($json['guid'])) { + $data['guid'] = $json['guid']; } - if (!empty($json["comm"])) { - $data["community"] = $json["comm"]; + if (!empty($json['comm'])) { + $data['community'] = $json['comm']; } - if (!empty($json["tags"])) { - $keywords = implode(", ", $json["tags"]); - if ($keywords != "") { - $data["keywords"] = $keywords; + if (!empty($json['tags'])) { + $keywords = implode(', ', $json['tags']); + if ($keywords != '') { + $data['keywords'] = $keywords; } } $location = Profile::formatLocation($json); if ($location) { - $data["location"] = $location; + $data['location'] = $location; } - if (!empty($json["about"])) { - $data["about"] = $json["about"]; + if (!empty($json['about'])) { + $data['about'] = $json['about']; } - if (!empty($json["xmpp"])) { - $data["xmpp"] = $json["xmpp"]; + if (!empty($json['xmpp'])) { + $data['xmpp'] = $json['xmpp']; } - if (!empty($json["matrix"])) { - $data["matrix"] = $json["matrix"]; + if (!empty($json['matrix'])) { + $data['matrix'] = $json['matrix']; } - if (!empty($json["key"])) { - $data["pubkey"] = $json["key"]; + if (!empty($json['key'])) { + $data['pubkey'] = $json['key']; } - if (!empty($json["photo"])) { - $data["photo"] = $json["photo"]; + if (!empty($json['photo'])) { + $data['photo'] = $json['photo']; } - if (!empty($json["dfrn-request"])) { - $data["request"] = $json["dfrn-request"]; + if (!empty($json['dfrn-request'])) { + $data['request'] = $json['dfrn-request']; } - if (!empty($json["dfrn-confirm"])) { - $data["confirm"] = $json["dfrn-confirm"]; + if (!empty($json['dfrn-confirm'])) { + $data['confirm'] = $json['dfrn-confirm']; } - if (!empty($json["dfrn-notify"])) { - $data["notify"] = $json["dfrn-notify"]; + if (!empty($json['dfrn-notify'])) { + $data['notify'] = $json['dfrn-notify']; } - if (!empty($json["dfrn-poll"])) { - $data["poll"] = $json["dfrn-poll"]; + if (!empty($json['dfrn-poll'])) { + $data['poll'] = $json['dfrn-poll']; } - if (isset($json["hide"])) { - $data["hide"] = (bool)$json["hide"]; + if (isset($json['hide'])) { + $data['hide'] = (bool)$json['hide']; } else { - $data["hide"] = false; + $data['hide'] = false; } return $data; @@ -1103,7 +1120,7 @@ class Probe * * @return int Number of errors */ - public static function validDfrn($data) + public static function validDfrn(array $data): int { $errors = 0; if (!isset($data['key'])) { @@ -1128,48 +1145,47 @@ class Probe * Fetch data from a DFRN profile page and via "noscrape" * * @param string $profile_link Link to the profile page - * * @return array profile data * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function profile($profile_link) + public static function profile(string $profile_link): array { $data = []; Logger::info('Check profile', ['link' => $profile_link]); // Fetch data via noscrape - this is faster - $noscrape_url = str_replace(["/hcard/", "/profile/"], "/noscrape/", $profile_link); + $noscrape_url = str_replace(['/hcard/', '/profile/'], '/noscrape/', $profile_link); $data = self::pollNoscrape($noscrape_url, $data); - if (!isset($data["notify"]) - || !isset($data["confirm"]) - || !isset($data["request"]) - || !isset($data["poll"]) - || !isset($data["name"]) - || !isset($data["photo"]) + if (!isset($data['notify']) + || !isset($data['confirm']) + || !isset($data['request']) + || !isset($data['poll']) + || !isset($data['name']) + || !isset($data['photo']) ) { $data = self::pollHcard($profile_link, $data, true); } $prof_data = []; - if (empty($data["addr"]) || empty($data["nick"])) { + if (empty($data['addr']) || empty($data['nick'])) { $probe_data = self::uri($profile_link); - $data["addr"] = ($data["addr"] ?? '') ?: $probe_data["addr"]; - $data["nick"] = ($data["nick"] ?? '') ?: $probe_data["nick"]; + $data['addr'] = ($data['addr'] ?? '') ?: $probe_data['addr']; + $data['nick'] = ($data['nick'] ?? '') ?: $probe_data['nick']; } - $prof_data["addr"] = $data["addr"]; - $prof_data["nick"] = $data["nick"]; - $prof_data["dfrn-request"] = $data['request'] ?? null; - $prof_data["dfrn-confirm"] = $data['confirm'] ?? null; - $prof_data["dfrn-notify"] = $data['notify'] ?? null; - $prof_data["dfrn-poll"] = $data['poll'] ?? null; - $prof_data["photo"] = $data['photo'] ?? null; - $prof_data["fn"] = $data['name'] ?? null; - $prof_data["key"] = $data['pubkey'] ?? null; + $prof_data['addr'] = $data['addr']; + $prof_data['nick'] = $data['nick']; + $prof_data['dfrn-request'] = $data['request'] ?? null; + $prof_data['dfrn-confirm'] = $data['confirm'] ?? null; + $prof_data['dfrn-notify'] = $data['notify'] ?? null; + $prof_data['dfrn-poll'] = $data['poll'] ?? null; + $prof_data['photo'] = $data['photo'] ?? null; + $prof_data['fn'] = $data['name'] ?? null; + $prof_data['key'] = $data['pubkey'] ?? null; Logger::debug('Result', ['link' => $profile_link, 'data' => $prof_data]); @@ -1180,73 +1196,71 @@ class Probe * Check for DFRN contact * * @param array $webfinger Webfinger data - * * @return array DFRN data * @throws HTTPException\InternalServerErrorException */ - private static function dfrn($webfinger) + private static function dfrn(array $webfinger): array { - $hcard_url = ""; + $hcard_url = ''; $data = []; // The array is reversed to take into account the order of preference for same-rel links // See: https://tools.ietf.org/html/rfc7033#section-4.4.4 - foreach (array_reverse($webfinger["links"]) as $link) { - if (($link["rel"] == ActivityNamespace::DFRN) && !empty($link["href"])) { - $data["network"] = Protocol::DFRN; - } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) { - $data["poll"] = $link["href"]; - } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (($link["type"] ?? "") == "text/html") && !empty($link["href"])) { - $data["url"] = $link["href"]; - } elseif (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) { - $hcard_url = $link["href"]; - } elseif (($link["rel"] == ActivityNamespace::POCO) && !empty($link["href"])) { - $data["poco"] = $link["href"]; - } elseif (($link["rel"] == "http://webfinger.net/rel/avatar") && !empty($link["href"])) { - $data["photo"] = $link["href"]; - } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) { - $data["baseurl"] = trim($link["href"], '/'); - } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) { - $data["guid"] = $link["href"]; - } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) { - $data["pubkey"] = base64_decode($link["href"]); - - //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA")) - if (strstr($data["pubkey"], 'RSA ')) { - $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]); + foreach (array_reverse($webfinger['links']) as $link) { + if (($link['rel'] == ActivityNamespace::DFRN) && !empty($link['href'])) { + $data['network'] = Protocol::DFRN; + } elseif (($link['rel'] == ActivityNamespace::FEED) && !empty($link['href'])) { + $data['poll'] = $link['href']; + } elseif (($link['rel'] == 'http://webfinger.net/rel/profile-page') && (($link['type'] ?? '') == 'text/html') && !empty($link['href'])) { + $data['url'] = $link['href']; + } elseif (($link['rel'] == 'http://microformats.org/profile/hcard') && !empty($link['href'])) { + $hcard_url = $link['href']; + } elseif (($link['rel'] == ActivityNamespace::POCO) && !empty($link['href'])) { + $data['poco'] = $link['href']; + } elseif (($link['rel'] == 'http://webfinger.net/rel/avatar') && !empty($link['href'])) { + $data['photo'] = $link['href']; + } elseif (($link['rel'] == 'http://joindiaspora.com/seed_location') && !empty($link['href'])) { + $data['baseurl'] = trim($link['href'], '/'); + } elseif (($link['rel'] == 'http://joindiaspora.com/guid') && !empty($link['href'])) { + $data['guid'] = $link['href']; + } elseif (($link['rel'] == 'diaspora-public-key') && !empty($link['href'])) { + $data['pubkey'] = base64_decode($link['href']); + + if (strstr($data['pubkey'], 'RSA ')) { + $data['pubkey'] = Crypto::rsaToPem($data['pubkey']); } } } - if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) { - foreach ($webfinger["aliases"] as $alias) { - if (empty($data["url"]) && !strstr($alias, "@")) { - $data["url"] = $alias; - } elseif (!strstr($alias, "@") && Strings::normaliseLink($alias) != Strings::normaliseLink($data["url"])) { - $data["alias"] = $alias; + if (!empty($webfinger['aliases']) && is_array($webfinger['aliases'])) { + foreach ($webfinger['aliases'] as $alias) { + if (empty($data['url']) && !strstr($alias, '@')) { + $data['url'] = $alias; + } elseif (!strstr($alias, '@') && Strings::normaliseLink($alias) != Strings::normaliseLink($data['url'])) { + $data['alias'] = $alias; } elseif (substr($alias, 0, 5) == 'acct:') { - $data["addr"] = substr($alias, 5); + $data['addr'] = substr($alias, 5); } } } - if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == "acct:")) { - $data["addr"] = substr($webfinger["subject"], 5); + if (!empty($webfinger['subject']) && (substr($webfinger['subject'], 0, 5) == 'acct:')) { + $data['addr'] = substr($webfinger['subject'], 5); } - if (!isset($data["network"]) || ($hcard_url == "")) { + if (!isset($data['network']) || ($hcard_url == '')) { return []; } // Fetch data via noscrape - this is faster - $noscrape_url = str_replace("/hcard/", "/noscrape/", $hcard_url); + $noscrape_url = str_replace('/hcard/', '/noscrape/', $hcard_url); $data = self::pollNoscrape($noscrape_url, $data); - if (isset($data["notify"]) - && isset($data["confirm"]) - && isset($data["request"]) - && isset($data["poll"]) - && isset($data["name"]) - && isset($data["photo"]) + if (isset($data['notify']) + && isset($data['confirm']) + && isset($data['request']) + && isset($data['poll']) + && isset($data['name']) + && isset($data['photo']) ) { return $data; } @@ -1262,15 +1276,14 @@ class Probe * @param string $hcard_url Link to the hcard page * @param array $data The already fetched data * @param boolean $dfrn Poll DFRN specific data - * * @return array hcard data * @throws HTTPException\InternalServerErrorException */ - private static function pollHcard($hcard_url, $data, $dfrn = false) + private static function pollHcard(string $hcard_url, array $data, bool $dfrn = false): array { - $curlResult = DI::httpClient()->get($hcard_url); + $curlResult = DI::httpClient()->get($hcard_url, HttpClientAccept::HTML); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return []; } $content = $curlResult->getBody(); @@ -1290,8 +1303,8 @@ class Probe return []; } - if (!isset($data["baseurl"])) { - $data["baseurl"] = ""; + if (!isset($data['baseurl'])) { + $data['baseurl'] = ''; } if ($vcards->length > 0) { @@ -1300,40 +1313,50 @@ class Probe // We have to discard the guid from the hcard in favour of the guid from lrdd // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does. $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */ - if (($search->length > 0) && empty($data["guid"])) { - $data["guid"] = $search->item(0)->nodeValue; + if (($search->length > 0) && empty($data['guid'])) { + $data['guid'] = $search->item(0)->nodeValue; } $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */ if ($search->length > 0) { - $data["nick"] = $search->item(0)->nodeValue; + $data['nick'] = $search->item(0)->nodeValue; } $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */ if ($search->length > 0) { - $data["name"] = $search->item(0)->nodeValue; + $data['name'] = $search->item(0)->nodeValue; + } + + $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' given_name ')]", $vcard); // */ + if ($search->length > 0) { + $data["given_name"] = $search->item(0)->nodeValue; + } + + $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' family_name ')]", $vcard); // */ + if ($search->length > 0) { + $data["family_name"] = $search->item(0)->nodeValue; } $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */ if ($search->length > 0) { - $data["searchable"] = $search->item(0)->nodeValue; + $data['hide'] = (strtolower($search->item(0)->nodeValue) != 'true'); } $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */ if ($search->length > 0) { - $data["pubkey"] = $search->item(0)->nodeValue; - if (strstr($data["pubkey"], 'RSA ')) { - $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]); + $data['pubkey'] = $search->item(0)->nodeValue; + if (strstr($data['pubkey'], 'RSA ')) { + $data['pubkey'] = Crypto::rsaToPem($data['pubkey']); } } $search = $xpath->query("//*[@id='pod_location']", $vcard); // */ if ($search->length > 0) { - $data["baseurl"] = trim($search->item(0)->nodeValue, "/"); + $data['baseurl'] = trim($search->item(0)->nodeValue, '/'); } } - $avatar = []; + $avatars = []; if (!empty($vcard)) { $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */ foreach ($photos as $photo) { @@ -1342,21 +1365,28 @@ class Probe $attr[$attribute->name] = trim($attribute->value); } - if (isset($attr["src"]) && isset($attr["width"])) { - $avatar[$attr["width"]] = $attr["src"]; + if (isset($attr['src']) && isset($attr['width'])) { + $avatars[$attr['width']] = self::fixAvatar($attr['src'], $data['baseurl']); } // We don't have a width. So we just take everything that we got. // This is a Hubzilla workaround which doesn't send a width. - if ((sizeof($avatar) == 0) && !empty($attr["src"])) { - $avatar[] = $attr["src"]; + if (!$avatars && !empty($attr['src'])) { + $avatars[] = self::fixAvatar($attr['src'], $data['baseurl']); } } } - if (sizeof($avatar)) { - ksort($avatar); - $data["photo"] = self::fixAvatar(array_pop($avatar), $data["baseurl"]); + if ($avatars) { + ksort($avatars); + $data['photo'] = array_pop($avatars); + if ($avatars) { + $data['photo_medium'] = array_pop($avatars); + } + + if ($avatars) { + $data['photo_small'] = array_pop($avatars); + } } if ($dfrn) { @@ -1364,23 +1394,22 @@ class Probe $search = $xpath->query("//link[contains(concat(' ', @rel), ' dfrn-')]"); if ($search->length > 0) { foreach ($search as $link) { - //$data["request"] = $search->item(0)->nodeValue; + //$data['request'] = $search->item(0)->nodeValue; $attr = []; foreach ($link->attributes as $attribute) { $attr[$attribute->name] = trim($attribute->value); } - $data[substr($attr["rel"], 5)] = $attr["href"]; + $data[substr($attr['rel'], 5)] = $attr['href']; } } // Older Friendica versions had used the "uid" field differently than newer versions - if (!empty($data["nick"]) && !empty($data["guid"]) && ($data["nick"] == $data["guid"])) { - unset($data["guid"]); + if (!empty($data['nick']) && !empty($data['guid']) && ($data['nick'] == $data['guid'])) { + unset($data['guid']); } } - return $data; } @@ -1392,60 +1421,59 @@ class Probe * @return array Diaspora data * @throws HTTPException\InternalServerErrorException */ - private static function diaspora($webfinger) + private static function diaspora(array $webfinger): array { - $hcard_url = ""; + $hcard_url = ''; $data = []; // The array is reversed to take into account the order of preference for same-rel links // See: https://tools.ietf.org/html/rfc7033#section-4.4.4 - foreach (array_reverse($webfinger["links"]) as $link) { - if (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) { - $hcard_url = $link["href"]; - } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) { - $data["baseurl"] = trim($link["href"], '/'); - } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) { - $data["guid"] = $link["href"]; - } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (($link["type"] ?? "") == "text/html") && !empty($link["href"])) { - $data["url"] = $link["href"]; - } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && empty($link["type"]) && !empty($link["href"])) { - $profile_url = $link["href"]; - } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) { - $data["poll"] = $link["href"]; - } elseif (($link["rel"] == ActivityNamespace::POCO) && !empty($link["href"])) { - $data["poco"] = $link["href"]; - } elseif (($link["rel"] == "salmon") && !empty($link["href"])) { - $data["notify"] = $link["href"]; - } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) { - $data["pubkey"] = base64_decode($link["href"]); - - //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA")) - if (strstr($data["pubkey"], 'RSA ')) { - $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]); + foreach (array_reverse($webfinger['links']) as $link) { + if (($link['rel'] == 'http://microformats.org/profile/hcard') && !empty($link['href'])) { + $hcard_url = $link['href']; + } elseif (($link['rel'] == 'http://joindiaspora.com/seed_location') && !empty($link['href'])) { + $data['baseurl'] = trim($link['href'], '/'); + } elseif (($link['rel'] == 'http://joindiaspora.com/guid') && !empty($link['href'])) { + $data['guid'] = $link['href']; + } elseif (($link['rel'] == 'http://webfinger.net/rel/profile-page') && (($link['type'] ?? '') == 'text/html') && !empty($link['href'])) { + $data['url'] = $link['href']; + } elseif (($link['rel'] == 'http://webfinger.net/rel/profile-page') && empty($link['type']) && !empty($link['href'])) { + $profile_url = $link['href']; + } elseif (($link['rel'] == ActivityNamespace::FEED) && !empty($link['href'])) { + $data['poll'] = $link['href']; + } elseif (($link['rel'] == ActivityNamespace::POCO) && !empty($link['href'])) { + $data['poco'] = $link['href']; + } elseif (($link['rel'] == 'salmon') && !empty($link['href'])) { + $data['notify'] = $link['href']; + } elseif (($link['rel'] == 'diaspora-public-key') && !empty($link['href'])) { + $data['pubkey'] = base64_decode($link['href']); + + if (strstr($data['pubkey'], 'RSA ')) { + $data['pubkey'] = Crypto::rsaToPem($data['pubkey']); } } } - if (empty($data["url"]) && !empty($profile_url)) { - $data["url"] = $profile_url; + if (empty($data['url']) && !empty($profile_url)) { + $data['url'] = $profile_url; } - if (empty($data["url"]) || empty($hcard_url)) { + if (empty($data['url']) || empty($hcard_url)) { return []; } - if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) { - foreach ($webfinger["aliases"] as $alias) { - if (Strings::normaliseLink($alias) != Strings::normaliseLink($data["url"]) && ! strstr($alias, "@")) { - $data["alias"] = $alias; + if (!empty($webfinger['aliases']) && is_array($webfinger['aliases'])) { + foreach ($webfinger['aliases'] as $alias) { + if (Strings::normaliseLink($alias) != Strings::normaliseLink($data['url']) && ! strstr($alias, '@')) { + $data['alias'] = $alias; } elseif (substr($alias, 0, 5) == 'acct:') { - $data["addr"] = substr($alias, 5); + $data['addr'] = substr($alias, 5); } } } - if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == 'acct:')) { - $data["addr"] = substr($webfinger["subject"], 5); + if (!empty($webfinger['subject']) && (substr($webfinger['subject'], 0, 5) == 'acct:')) { + $data['addr'] = substr($webfinger['subject'], 5); } // Fetch further information from the hcard @@ -1455,23 +1483,23 @@ class Probe return []; } - if (!empty($data["url"]) - && !empty($data["guid"]) - && !empty($data["baseurl"]) - && !empty($data["pubkey"]) + if (!empty($data['url']) + && !empty($data['guid']) + && !empty($data['baseurl']) + && !empty($data['pubkey']) && !empty($hcard_url) ) { - $data["network"] = Protocol::DIASPORA; - $data["manually-approve"] = false; + $data['network'] = Protocol::DIASPORA; + $data['manually-approve'] = false; // The Diaspora handle must always be lowercase - if (!empty($data["addr"])) { - $data["addr"] = strtolower($data["addr"]); + if (!empty($data['addr'])) { + $data['addr'] = strtolower($data['addr']); } // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it - $data["notify"] = $data["baseurl"] . "/receive/users/" . $data["guid"]; - $data["batch"] = $data["baseurl"] . "/receive/public"; + $data['notify'] = $data['baseurl'] . '/receive/users/' . $data['guid']; + $data['batch'] = $data['baseurl'] . '/receive/public'; } else { return []; } @@ -1488,39 +1516,39 @@ class Probe * @return array|bool OStatus data or "false" on error or "true" on short mode * @throws HTTPException\InternalServerErrorException */ - private static function ostatus($webfinger, $short = false) + private static function ostatus(array $webfinger, bool $short = false) { $data = []; - if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) { - foreach ($webfinger["aliases"] as $alias) { - if (strstr($alias, "@") && !strstr(Strings::normaliseLink($alias), "http://")) { - $data["addr"] = str_replace('acct:', '', $alias); + if (!empty($webfinger['aliases']) && is_array($webfinger['aliases'])) { + foreach ($webfinger['aliases'] as $alias) { + if (strstr($alias, '@') && !strstr(Strings::normaliseLink($alias), 'http://')) { + $data['addr'] = str_replace('acct:', '', $alias); } } } - if (!empty($webfinger["subject"]) && strstr($webfinger["subject"], "@") - && !strstr(Strings::normaliseLink($webfinger["subject"]), "http://") + if (!empty($webfinger['subject']) && strstr($webfinger['subject'], '@') + && !strstr(Strings::normaliseLink($webfinger['subject']), 'http://') ) { - $data["addr"] = str_replace('acct:', '', $webfinger["subject"]); + $data['addr'] = str_replace('acct:', '', $webfinger['subject']); } - if (!empty($webfinger["links"])) { + if (!empty($webfinger['links'])) { // The array is reversed to take into account the order of preference for same-rel links // See: https://tools.ietf.org/html/rfc7033#section-4.4.4 - foreach (array_reverse($webfinger["links"]) as $link) { - if (($link["rel"] == "http://webfinger.net/rel/profile-page") - && (($link["type"] ?? "") == "text/html") - && ($link["href"] != "") + foreach (array_reverse($webfinger['links']) as $link) { + if (($link['rel'] == 'http://webfinger.net/rel/profile-page') + && (($link['type'] ?? '') == 'text/html') + && ($link['href'] != '') ) { - $data["url"] = $data["alias"] = $link["href"]; - } elseif (($link["rel"] == "salmon") && !empty($link["href"])) { - $data["notify"] = $link["href"]; - } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) { - $data["poll"] = $link["href"]; - } elseif (($link["rel"] == "magic-public-key") && !empty($link["href"])) { - $pubkey = $link["href"]; + $data['url'] = $data['alias'] = $link['href']; + } elseif (($link['rel'] == 'salmon') && !empty($link['href'])) { + $data['notify'] = $link['href']; + } elseif (($link['rel'] == ActivityNamespace::FEED) && !empty($link['href'])) { + $data['poll'] = $link['href']; + } elseif (($link['rel'] == 'magic-public-key') && !empty($link['href'])) { + $pubkey = $link['href']; if (substr($pubkey, 0, 5) === 'data:') { if (strstr($pubkey, ',')) { @@ -1529,31 +1557,30 @@ class Probe $pubkey = substr($pubkey, 5); } } elseif (Strings::normaliseLink($pubkey) == 'http://') { - $curlResult = DI::httpClient()->get($pubkey); + $curlResult = DI::httpClient()->get($pubkey, HttpClientAccept::MAGIC_KEY); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return $short ? false : []; } + Logger::debug('Fetched public key', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $pubkey]); $pubkey = $curlResult->getBody(); } - $key = explode(".", $pubkey); + try { + $data['pubkey'] = Salmon::magicKeyToPem($pubkey); + } catch (\Throwable $e) { - if (sizeof($key) >= 3) { - $m = Strings::base64UrlDecode($key[1]); - $e = Strings::base64UrlDecode($key[2]); - $data["pubkey"] = Crypto::meToPem($m, $e); } } } } - if (isset($data["notify"]) && isset($data["pubkey"]) - && isset($data["poll"]) - && isset($data["url"]) + if (isset($data['notify']) && isset($data['pubkey']) + && isset($data['poll']) + && isset($data['url']) ) { - $data["network"] = Protocol::OSTATUS; - $data["manually-approve"] = false; + $data['network'] = Protocol::OSTATUS; + $data['manually-approve'] = false; } else { return $short ? false : []; } @@ -1563,9 +1590,9 @@ class Probe } // Fetch all additional data from the feed - $curlResult = DI::httpClient()->get($data["poll"]); + $curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::FEED_XML); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return []; } $feed = $curlResult->getBody(); @@ -1574,32 +1601,32 @@ class Probe return []; } - if (!empty($feed_data["header"]["author-name"])) { - $data["name"] = $feed_data["header"]["author-name"]; + if (!empty($feed_data['header']['author-name'])) { + $data['name'] = $feed_data['header']['author-name']; } - if (!empty($feed_data["header"]["author-nick"])) { - $data["nick"] = $feed_data["header"]["author-nick"]; + if (!empty($feed_data['header']['author-nick'])) { + $data['nick'] = $feed_data['header']['author-nick']; } - if (!empty($feed_data["header"]["author-avatar"])) { - $data["photo"] = self::fixAvatar($feed_data["header"]["author-avatar"], $data["url"]); + if (!empty($feed_data['header']['author-avatar'])) { + $data['photo'] = self::fixAvatar($feed_data['header']['author-avatar'], $data['url']); } - if (!empty($feed_data["header"]["author-id"])) { - $data["alias"] = $feed_data["header"]["author-id"]; + if (!empty($feed_data['header']['author-id'])) { + $data['alias'] = $feed_data['header']['author-id']; } - if (!empty($feed_data["header"]["author-location"])) { - $data["location"] = $feed_data["header"]["author-location"]; + if (!empty($feed_data['header']['author-location'])) { + $data['location'] = $feed_data['header']['author-location']; } - if (!empty($feed_data["header"]["author-about"])) { - $data["about"] = $feed_data["header"]["author-about"]; + if (!empty($feed_data['header']['author-about'])) { + $data['about'] = $feed_data['header']['author-about']; } // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl) // So we take the value that we just fetched, although the other one worked as well - if (!empty($feed_data["header"]["author-link"])) { - $data["url"] = $feed_data["header"]["author-link"]; + if (!empty($feed_data['header']['author-link'])) { + $data['url'] = $feed_data['header']['author-link']; } - if ($data["url"] == $data["alias"]) { - $data["alias"] = ''; + if ($data['url'] == $data['alias']) { + $data['alias'] = ''; } /// @todo Fetch location and "about" from the feed as well @@ -1611,11 +1638,11 @@ class Probe * * @param string $profile_link Link to the profile page * - * @return array profile data + * @return array Profile data */ - private static function pumpioProfileData($profile_link) + private static function pumpioProfileData(string $profile_link, string $baseurl): array { - $curlResult = DI::httpClient()->get($profile_link); + $curlResult = DI::httpClient()->get($profile_link, HttpClientAccept::HTML); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { return []; } @@ -1628,28 +1655,27 @@ class Probe $xpath = new DomXPath($doc); $data = []; + $data['name'] = $xpath->query("//span[contains(@class, 'p-name')]")->item(0)->nodeValue; - $data["name"] = $xpath->query("//span[contains(@class, 'p-name')]")->item(0)->nodeValue; - - if ($data["name"] == '') { + if ($data['name'] == '') { // This is ugly - but pump.io doesn't seem to know a better way for it - $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue); - $pos = strpos($data["name"], chr(10)); + $data['name'] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue); + $pos = strpos($data['name'], chr(10)); if ($pos) { - $data["name"] = trim(substr($data["name"], 0, $pos)); + $data['name'] = trim(substr($data['name'], 0, $pos)); } } - $data["location"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-locality')]"); + $data['location'] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-locality')]"); - if ($data["location"] == '') { - $data["location"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'location')]"); + if ($data['location'] == '') { + $data['location'] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'location')]"); } - $data["about"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-note')]"); + $data['about'] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-note')]"); - if ($data["about"] == '') { - $data["about"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'summary')]"); + if ($data['about'] == '') { + $data['about'] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'summary')]"); } $avatar = $xpath->query("//img[contains(@class, 'u-photo')]")->item(0); @@ -1658,8 +1684,11 @@ class Probe } if ($avatar) { foreach ($avatar->attributes as $attribute) { - if ($attribute->name == "src") { - $data["photo"] = trim($attribute->value); + if ($attribute->name == 'src') { + $data['photo'] = trim($attribute->value); + if (!empty($data['photo']) && !parse_url($data['photo'], PHP_URL_SCHEME) && !parse_url($data['photo'], PHP_URL_HOST)) { + $data['photo'] = $baseurl . $data['photo']; + } } } } @@ -1672,41 +1701,42 @@ class Probe * * @param array $webfinger Webfinger data * @param string $addr + * * @return array pump.io data */ - private static function pumpio($webfinger, $addr) + private static function pumpio(array $webfinger, string $addr, string $baseurl): array { $data = []; // The array is reversed to take into account the order of preference for same-rel links // See: https://tools.ietf.org/html/rfc7033#section-4.4.4 - foreach (array_reverse($webfinger["links"]) as $link) { - if (($link["rel"] == "http://webfinger.net/rel/profile-page") - && (($link["type"] ?? "") == "text/html") - && ($link["href"] != "") + foreach (array_reverse($webfinger['links']) as $link) { + if (($link['rel'] == 'http://webfinger.net/rel/profile-page') + && (($link['type'] ?? '') == 'text/html') + && ($link['href'] != '') ) { - $data["url"] = $link["href"]; - } elseif (($link["rel"] == "activity-inbox") && ($link["href"] != "")) { - $data["notify"] = $link["href"]; - } elseif (($link["rel"] == "activity-outbox") && ($link["href"] != "")) { - $data["poll"] = $link["href"]; - } elseif (($link["rel"] == "dialback") && ($link["href"] != "")) { - $data["dialback"] = $link["href"]; - } - } - if (isset($data["poll"]) && isset($data["notify"]) - && isset($data["dialback"]) - && isset($data["url"]) + $data['url'] = $link['href']; + } elseif (($link['rel'] == 'activity-inbox') && ($link['href'] != '')) { + $data['notify'] = $link['href']; + } elseif (($link['rel'] == 'activity-outbox') && ($link['href'] != '')) { + $data['poll'] = $link['href']; + } elseif (($link['rel'] == 'dialback') && ($link['href'] != '')) { + $data['dialback'] = $link['href']; + } + } + if (isset($data['poll']) && isset($data['notify']) + && isset($data['dialback']) + && isset($data['url']) ) { // by now we use these fields only for the network type detection // So we unset all data that isn't used at the moment - unset($data["dialback"]); + unset($data['dialback']); - $data["network"] = Protocol::PUMPIO; + $data['network'] = Protocol::PUMPIO; } else { return []; } - $profile_data = self::pumpioProfileData($data["url"]); + $profile_data = self::pumpioProfileData($data['url'], $baseurl); if (!$profile_data) { return []; @@ -1729,6 +1759,7 @@ class Probe * * @param string $url Page link * @param string $body Page body string + * * @return string|false Feed link or false if body was invalid HTML document */ public static function getFeedLink(string $url, string $body) @@ -1745,6 +1776,7 @@ class Probe $xpath = new DOMXPath($doc); $feedUrl = $xpath->evaluate('string(/html/head/link[@type="application/rss+xml" and @rel="alternate"]/@href)'); + $feedUrl = $feedUrl ?: $xpath->evaluate('string(/html/head/link[@type="application/atom+xml" and @rel="alternate"]/@href)'); $feedUrl = $feedUrl ? self::ensureAbsoluteLinkFromHTMLDoc($feedUrl, $url, $xpath) : ''; @@ -1761,9 +1793,10 @@ class Probe * @param string $href The potential relative href found in the HTML document * @param string $base The HTML document URL * @param DOMXPath $xpath The HTML document XPath - * @return string + * + * @return string Absolute URL */ - private static function ensureAbsoluteLinkFromHTMLDoc(string $href, string $base, DOMXPath $xpath) + private static function ensureAbsoluteLinkFromHTMLDoc(string $href, string $base, DOMXPath $xpath): string { if (filter_var($href, FILTER_VALIDATE_URL)) { return $href; @@ -1794,7 +1827,7 @@ class Probe // Resolve arbitrary relative path // Lifted from https://www.php.net/manual/en/function.realpath.php#84012 $parts = array_filter(explode('/', $path), 'strlen'); - $absolutes = array(); + $absolutes = []; foreach ($parts as $part) { if ('.' == $part) continue; if ('..' == $part) { @@ -1826,11 +1859,11 @@ class Probe * @return array feed data * @throws HTTPException\InternalServerErrorException */ - private static function feed($url, $probe = true) + private static function feed(string $url, bool $probe = true): array { - $curlResult = DI::httpClient()->get($url); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::FEED_XML); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return []; } $feed = $curlResult->getBody(); @@ -1850,26 +1883,26 @@ class Probe return self::feed($feed_url, false); } - if (!empty($feed_data["header"]["author-name"])) { - $data["name"] = $feed_data["header"]["author-name"]; + if (!empty($feed_data['header']['author-name'])) { + $data['name'] = $feed_data['header']['author-name']; } - if (!empty($feed_data["header"]["author-nick"])) { - $data["nick"] = $feed_data["header"]["author-nick"]; + if (!empty($feed_data['header']['author-nick'])) { + $data['nick'] = $feed_data['header']['author-nick']; } - if (!empty($feed_data["header"]["author-avatar"])) { - $data["photo"] = $feed_data["header"]["author-avatar"]; + if (!empty($feed_data['header']['author-avatar'])) { + $data['photo'] = $feed_data['header']['author-avatar']; } - if (!empty($feed_data["header"]["author-id"])) { - $data["alias"] = $feed_data["header"]["author-id"]; + if (!empty($feed_data['header']['author-id'])) { + $data['alias'] = $feed_data['header']['author-id']; } - $data["url"] = $url; - $data["poll"] = $url; + $data['url'] = $url; + $data['poll'] = $url; - $data["network"] = Protocol::FEED; + $data['network'] = Protocol::FEED; return $data; } @@ -1883,7 +1916,7 @@ class Probe * @return array mail data * @throws \Exception */ - private static function mail($uri, $uid) + private static function mail(string $uri, int $uid): array { if (!Network::isEmailDomainValid($uri)) { return []; @@ -1920,43 +1953,49 @@ class Probe $phost = substr($uri, strpos($uri, '@') + 1); - $data = []; - $data["addr"] = $uri; - $data["network"] = Protocol::MAIL; - $data["name"] = substr($uri, 0, strpos($uri, '@')); - $data["nick"] = $data["name"]; - $data["photo"] = Network::lookupAvatarByEmail($uri); - $data["url"] = 'mailto:'.$uri; - $data["notify"] = 'smtp ' . Strings::getRandomHex(); - $data["poll"] = 'email ' . Strings::getRandomHex(); + $data = [ + 'addr' => $uri, + 'network' => Protocol::MAIL, + 'name' => substr($uri, 0, strpos($uri, '@')), + 'photo' => Network::lookupAvatarByEmail($uri), + 'url' => 'mailto:' . $uri, + 'notify' => 'smtp ' . Strings::getRandomHex(), + 'poll' => 'email ' . Strings::getRandomHex(), + ]; + + $data['nick'] = $data['name']; $x = Email::messageMeta($mbox, $msgs[0]); + if (stristr($x[0]->from, $uri)) { $adr = imap_rfc822_parse_adrlist($x[0]->from, ''); } elseif (stristr($x[0]->to, $uri)) { $adr = imap_rfc822_parse_adrlist($x[0]->to, ''); } + if (isset($adr)) { foreach ($adr as $feadr) { - if ((strcasecmp($feadr->mailbox, $data["name"]) == 0) + if ((strcasecmp($feadr->mailbox, $data['name']) == 0) &&(strcasecmp($feadr->host, $phost) == 0) && (strlen($feadr->personal)) ) { $personal = imap_mime_header_decode($feadr->personal); - $data["name"] = ""; + $data['name'] = ''; foreach ($personal as $perspart) { - if ($perspart->charset != "default") { - $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text); + if ($perspart->charset != 'default') { + $data['name'] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text); } else { - $data["name"] .= $perspart->text; + $data['name'] .= $perspart->text; } } } } } + if (!empty($mbox)) { imap_close($mbox); } + return $data; } @@ -1969,7 +2008,7 @@ class Probe * @return string fixed avatar path * @throws \Exception */ - public static function fixAvatar($avatar, $base) + public static function fixAvatar(string $avatar, string $base): string { $base_parts = parse_url($base); @@ -2001,10 +2040,11 @@ class Probe /** * Fetch the last date that the contact had posted something (publically) * - * @param string $data probing result + * @param array $data probing result + * * @return string last activity */ - public static function getLastUpdate(array $data) + public static function getLastUpdate(array $data): string { $uid = User::getIdForURL($data['url']); if (!empty($uid)) { @@ -2033,11 +2073,10 @@ class Probe * Fetch the last activity date from the "noscrape" endpoint * * @param array $data Probing result - * @return string last activity * - * @return bool 'true' if update was successful or the server was unreachable + * @return string last activity or true if update was successful or the server was unreachable */ - private static function updateFromNoScrape(array $data) + private static function updateFromNoScrape(array $data): string { if (empty($data['baseurl'])) { return ''; @@ -2050,7 +2089,7 @@ class Probe return ''; } - $curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick']); + $curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], HttpClientAccept::JSON); if ($curlResult->isSuccess() && !empty($curlResult->getBody())) { $noscrape = json_decode($curlResult->getBody(), true); @@ -2067,10 +2106,11 @@ class Probe * * @param string $feed * @param array $data Probing result + * * @return string last activity * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - private static function updateFromOutbox(string $feed, array $data) + private static function updateFromOutbox(string $feed, array $data): string { $outbox = ActivityPub::fetchContent($feed); if (empty($outbox)) { @@ -2122,10 +2162,10 @@ class Probe * @param array $data Probing result * @return string last activity */ - private static function updateFromFeed(array $data) + private static function updateFromFeed(array $data): string { // Search for the newest entry in the feed - $curlResult = DI::httpClient()->get($data['poll']); + $curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::ATOM_XML); if (!$curlResult->isSuccess() || !$curlResult->getBody()) { return ''; } @@ -2171,6 +2211,7 @@ class Probe * Probe data from local profiles without network traffic * * @param string $url + * * @return array probed data * @throws HTTPException\InternalServerErrorException * @throws HTTPException\NotFoundException @@ -2186,26 +2227,49 @@ class Probe $owner = User::getOwnerDataById($uid); $approfile = ActivityPub\Transmitter::getProfile($uid); + $split_name = Diaspora::splitName($owner['name']); + if (empty($owner['gsid'])) { $owner['gsid'] = GServer::getID($approfile['generator']['url']); } $data = [ - 'name' => $owner['name'], 'nick' => $owner['nick'], 'guid' => $approfile['diaspora:guid'] ?? '', - 'url' => $owner['url'], 'addr' => $owner['addr'], 'alias' => $owner['alias'], - 'photo' => User::getAvatarUrl($owner), - 'header' => $owner['header'] ? Contact::getHeaderUrlForId($owner['id'], $owner['updated']) : '', - 'account-type' => $owner['contact-type'], 'community' => ($owner['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY), - 'keywords' => $owner['keywords'], 'location' => $owner['location'], 'about' => $owner['about'], - 'xmpp' => $owner['xmpp'], 'matrix' => $owner['matrix'], - 'hide' => !$owner['net-publish'], 'batch' => '', 'notify' => $owner['notify'], - 'poll' => $owner['poll'], 'request' => $owner['request'], 'confirm' => $owner['confirm'], - 'subscribe' => $approfile['generator']['url'] . '/follow?url={uri}', 'poco' => $owner['poco'], - 'following' => $approfile['following'], 'followers' => $approfile['followers'], - 'inbox' => $approfile['inbox'], 'outbox' => $approfile['outbox'], - 'sharedinbox' => $approfile['endpoints']['sharedInbox'], 'network' => Protocol::DFRN, - 'pubkey' => $owner['upubkey'], 'baseurl' => $approfile['generator']['url'], 'gsid' => $owner['gsid'], - 'manually-approve' => in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]) + 'name' => $owner['name'], 'nick' => $owner['nick'], 'guid' => $approfile['diaspora:guid'] ?? '', + 'url' => $owner['url'], 'addr' => $owner['addr'], 'alias' => $owner['alias'], + 'photo' => User::getAvatarUrl($owner), + 'header' => $owner['header'] ? Contact::getHeaderUrlForId($owner['id'], $owner['updated']) : '', + 'account-type' => $owner['contact-type'], 'community' => ($owner['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY), + 'keywords' => $owner['keywords'], 'location' => $owner['location'], 'about' => $owner['about'], + 'xmpp' => $owner['xmpp'], 'matrix' => $owner['matrix'], + 'hide' => !$owner['net-publish'], 'batch' => '', 'notify' => $owner['notify'], + 'poll' => $owner['poll'], 'request' => $owner['request'], 'confirm' => $owner['confirm'], + 'subscribe' => $approfile['generator']['url'] . '/contact/follow?url={uri}', 'poco' => $owner['poco'], + 'following' => $approfile['following'], 'followers' => $approfile['followers'], + 'inbox' => $approfile['inbox'], 'outbox' => $approfile['outbox'], + 'sharedinbox' => $approfile['endpoints']['sharedInbox'], 'network' => Protocol::DFRN, + 'pubkey' => $owner['upubkey'], 'baseurl' => $approfile['generator']['url'], 'gsid' => $owner['gsid'], + 'manually-approve' => in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]), + 'networks' => [ + Protocol::DIASPORA => [ + 'name' => $owner['name'], + 'given_name' => $split_name['first'], + 'family_name' => $split_name['last'], + 'nick' => $owner['nick'], + 'guid' => $approfile['diaspora:guid'], + 'url' => $owner['url'], + 'addr' => $owner['addr'], + 'alias' => $owner['alias'], + 'photo' => $owner['photo'], + 'photo_medium' => $owner['thumb'], + 'photo_small' => $owner['micro'], + 'batch' => $approfile['generator']['url'] . '/receive/public', + 'notify' => $owner['notify'], + 'poll' => $owner['poll'], + 'poco' => $owner['poco'], + 'network' => Protocol::DIASPORA, + 'pubkey' => $owner['upubkey'], + ] + ] ]; } catch (Exception $e) { // Default values for non existing targets