use DOMDocument;
use DomXPath;
use Friendica\Core\Cache\Duration;
+use Friendica\Core\Hook;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\Core\System;
$newdata = [];
foreach ($fields as $field) {
if (isset($data[$field])) {
- $newdata[$field] = $data[$field];
+ if (in_array($field, ["gsid", "hide", "account-type"])) {
+ $newdata[$field] = (int)$data[$field];
+ } else {
+ $newdata[$field] = $data[$field];
+ }
} elseif ($field != "gsid") {
$newdata[$field] = "";
} else {
// When the previous detection process had got a time out
// we could falsely detect a Friendica profile as AP profile.
if (!self::$istimeout) {
- $ap_profile = ActivityPub::probeProfile($uri);
+ $ap_profile = ActivityPub::probeProfile($uri, !$cache);
if (empty($data) || (!empty($ap_profile) && empty($network) && (($data['network'] ?? '') != Protocol::DFRN))) {
$data = $ap_profile;
}
}
- if (!empty(self::$baseurl)) {
+ if (empty($data['baseurl']) && !empty(self::$baseurl)) {
$data['baseurl'] = self::$baseurl;
}
return false;
}
- /**
- * Checks if a profile url should be OStatus but only provides partial information
- *
- * @param array $webfinger Webfinger data
- * @param string $lrdd Path template for webfinger request
- * @param string $type type
- *
- * @return array fixed webfinger data
- * @throws HTTPException\InternalServerErrorException
- */
- private static function fixOStatus($webfinger, $lrdd, $type)
- {
- if (empty($webfinger['links']) || empty($webfinger['subject'])) {
- return $webfinger;
- }
-
- $is_ostatus = false;
- $has_key = false;
-
- foreach ($webfinger['links'] as $link) {
- if ($link['rel'] == ActivityNamespace::OSTATUSSUB) {
- $is_ostatus = true;
- }
- if ($link['rel'] == 'magic-public-key') {
- $has_key = true;
- }
- }
-
- if (!$is_ostatus || $has_key) {
- return $webfinger;
- }
-
- $url = Network::switchScheme($webfinger['subject']);
- $path = str_replace('{uri}', urlencode($url), $lrdd);
- $webfinger2 = self::webfinger($path, $type);
-
- // Is the new webfinger detectable as OStatus?
- if (self::ostatus($webfinger2, true)) {
- $webfinger = $webfinger2;
- }
-
- return $webfinger;
- }
-
/**
* Fetch the "subscribe" and add it to the result
*
}
foreach ($webfinger['links'] as $link) {
- if ($link['rel'] === ActivityNamespace::OSTATUSSUB) {
+ if (!empty($link['template']) && ($link['rel'] === ActivityNamespace::OSTATUSSUB)) {
$result['subscribe'] = $link['template'];
}
}
{
$parts = parse_url($uri);
+ $hookData = [
+ 'uri' => $uri,
+ 'network' => $network,
+ 'uid' => $uid,
+ 'result' => [],
+ ];
+
+ Hook::callAll('probe_detect', $hookData);
+
+ if ($hookData['result']) {
+ return $hookData['result'];
+ }
+
if (!empty($parts["scheme"]) && !empty($parts["host"])) {
$host = $parts["host"];
if (!empty($parts["port"])) {
continue;
}
- // At first try it with the given uri
- $path = str_replace('{uri}', urlencode($uri), $template);
- $webfinger = self::webfinger($path, $type);
-
- // Fix possible problems with GNU Social probing to wrong scheme
- $webfinger = self::fixOStatus($webfinger, $template, $type);
-
- // We cannot be sure that the detected address was correct, so we don't use the values
- if ($webfinger && ($uri != $addr)) {
- $nick = "";
- $addr = "";
+ // Try the URI first
+ if ($uri != $addr) {
+ $path = str_replace('{uri}', urlencode($uri), $template);
+ $webfinger = self::webfinger($path, $type);
}
- // Try webfinger with the address (user@domain.tld)
+ // Then try the address
if (!$webfinger) {
- $path = str_replace('{uri}', urlencode($addr), $template);
+ $path = str_replace('{uri}', urlencode("acct:" . $addr), $template);
$webfinger = self::webfinger($path, $type);
}
- // Mastodon needs to have it with "acct:"
+ // Finally try without the "acct"
if (!$webfinger) {
- $path = str_replace('{uri}', urlencode("acct:".$addr), $template);
+ $path = str_replace('{uri}', urlencode($addr), $template);
$webfinger = self::webfinger($path, $type);
}
+
+ // We cannot be sure that the detected address was correct, so we don't use the values
+ if ($webfinger && ($uri != $addr)) {
+ $nick = "";
+ $addr = "";
+ }
}
if (!$webfinger) {
Logger::log($uri." is ".$result["network"], Logger::DEBUG);
- if (empty($result["baseurl"]) && ($result["network"] != Protocol::PHANTOM)) {
- $pos = strpos($result["url"], $host);
- if ($pos) {
- $result["baseurl"] = substr($result["url"], 0, $pos).$host;
- }
- }
-
return $result;
}
&& (($link["type"] ?? "") == "text/html")
&& ($link["href"] != "")
) {
- $data["url"] = $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["url"] = $feed_data["header"]["author-link"];
}
- if (($data['poll'] == $data['url']) && ($data["alias"] != '')) {
- $data['url'] = $data["alias"];
+ if ($data["url"] == $data["alias"]) {
$data["alias"] = '';
}
$data['network'] = Protocol::TWITTER;
$data['baseurl'] = 'https://twitter.com';
- $curlResult = Network::curl($data['url'], false);
- if (!$curlResult->isSuccess()) {
- return [];
- }
-
- $body = $curlResult->getBody();
- $doc = new DOMDocument();
- @$doc->loadHTML($body);
- $xpath = new DOMXPath($doc);
-
- $list = $xpath->query('//img[@class]');
- foreach ($list as $node) {
- $img_attr = [];
- if ($node->attributes->length) {
- foreach ($node->attributes as $attribute) {
- $img_attr[$attribute->name] = $attribute->value;
- }
- }
-
- if (empty($img_attr['class'])) {
- continue;
- }
-
- if (strpos($img_attr['class'], 'ProfileAvatar-image') !== false) {
- if (!empty($img_attr['src'])) {
- $data['photo'] = $img_attr['src'];
- }
- if (!empty($img_attr['alt'])) {
- $data['name'] = $img_attr['alt'];
- }
- }
- }
-
return $data;
}