]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #12736 from MrPetovan/bug/12733-webfinger-apcontact
authorMichael Vogel <icarus@dabo.de>
Fri, 27 Jan 2023 07:27:20 +0000 (08:27 +0100)
committerGitHub <noreply@github.com>
Fri, 27 Jan 2023 07:27:20 +0000 (08:27 +0100)
Replace custom WebFinger implementation by Probe::getWebfingerArray in APContact::fetchWebfingerData

1  2 
src/Model/APContact.php
src/Network/Probe.php

diff --combined src/Model/APContact.php
index 072dea9d3e753dbfbe9b8f13779f87f409697749,c72028ef3159b9a430f9f1900e03c12d777340a4..1fedfc0c79c9a607b707b519a0f1bf71aa21e4fd
@@@ -71,21 -71,14 +71,14 @@@ class APContac
                        return $data;
                }
  
-               $data = ['addr' => $addr];
-               $template = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
-               $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClientAccept::JRD_JSON);
-               if (empty($webfinger['links'])) {
-                       $template = 'http://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
-                       $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClientAccept::JRD_JSON);
-                       if (empty($webfinger['links'])) {
-                               return [];
-                       }
-                       $data['baseurl'] = 'http://' . $addr_parts[1];
-               } else {
-                       $data['baseurl'] = 'https://' . $addr_parts[1];
+               $webfinger = Probe::getWebfingerArray($addr);
+               if (empty($webfinger['webfinger']['links'])) {
+                       return [];
                }
  
-               foreach ($webfinger['links'] as $link) {
+               $data['baseurl'] = $webfinger['baseurl'];
+               foreach ($webfinger['webfinger']['links'] as $link) {
                        if (empty($link['rel'])) {
                                continue;
                        }
                // Unhandled from Kroeg
                // kroeg:blocks, updated
  
 +              if (!empty($apcontact['photo']) && !Network::isValidHttpUrl($apcontact['photo'])) {
 +                      Logger::info('Invalid URL for photo', ['url' => $apcontact['url'], 'photo' => $apcontact['photo']]);
 +                      $apcontact['photo'] = null;
 +              }
 +
                // When the photo is too large, try to shorten it by removing parts
                if (strlen($apcontact['photo'] ?? '') > 255) {
                        $parts = parse_url($apcontact['photo']);
diff --combined src/Network/Probe.php
index e1bedf5e5358e1fa51bf9e26aa2d6d4c455a7d9b,12ac8a04f943b8a7faeef08e08a74e6f60df661c..92e049ce39087f0b5a5c51d087545c1f124e690c
@@@ -120,11 -120,6 +120,11 @@@ class Prob
  
                $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) {
                        if (isset($data[$field])) {
         * @return array Webfinger data
         * @throws HTTPException\InternalServerErrorException
         */
-       private static function getWebfingerArray(string $uri): array
+       public static function getWebfingerArray(string $uri): array
        {
                $parts = parse_url($uri);
  
                        $result = self::zot($webfinger, $result, $baseurl);
                }
                if ((!$result && ($network == '')) || ($network == Protocol::PUMPIO)) {
 -                      $result = self::pumpio($webfinger, $addr);
 +                      $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).
+                       // Additionally, it is overwritten when the nickname doesn't make sense (contains spaces).
                        if ((empty($result['nick']) || (strstr($result['nick'], ' '))) && ($nick != '')) {
                                $result['nick'] = $nick;
                        }
         *
         * @return array Profile data
         */
 -      private static function pumpioProfileData(string $profile_link): array
 +      private static function pumpioProfileData(string $profile_link, string $baseurl): array
        {
                $curlResult = DI::httpClient()->get($profile_link, HttpClientAccept::HTML);
                if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
                        foreach ($avatar->attributes as $attribute) {
                                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'];
 +                                      }
                                }
                        }
                }
         *
         * @return array pump.io data
         */
 -      private static function pumpio(array $webfinger, string $addr): array
 +      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
                        return [];
                }
  
 -              $profile_data = self::pumpioProfileData($data['url']);
 +              $profile_data = self::pumpioProfileData($data['url'], $baseurl);
  
                if (!$profile_data) {
                        return [];
         */
        private static function feed(string $url, bool $probe = true): array
        {
-               $curlResult = DI::httpClient()->get($url, HttpClientAccept::FEED_XML);
+               try {
+                       $curlResult = DI::httpClient()->get($url, HttpClientAccept::FEED_XML);
+               } catch(\Throwable $e) {
+                       DI::logger()->info('Error requesting feed URL', ['url' => $url, 'exception' => $e]);
+                       return [];
+               }
                if ($curlResult->isTimeout()) {
                        self::$isTimeout = true;
                        return [];
                }
                $feed = $curlResult->getBody();
                $feed_data = Feed::import($feed);