]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/Probe.php
spelling: the
[friendica.git] / src / Network / Probe.php
index 87c28619dc7b8f6c1b0f7661084284a0ae50a6ca..d344d556c3ff19f2b8908d8bd609c4cb6f978f87 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -38,6 +38,7 @@ use Friendica\Network\HTTPClient\Client\HttpClientAccept;
 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
 use Friendica\Protocol\ActivityNamespace;
 use Friendica\Protocol\ActivityPub;
+use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\Email;
 use Friendica\Protocol\Feed;
 use Friendica\Protocol\Salmon;
@@ -62,7 +63,7 @@ class Probe
        private static $baseurl;
 
        /**
-        * @var boolean Whether a timeout has occured
+        * @var boolean Whether a timeout has occurred
         */
        private static $isTimeout;
 
@@ -119,6 +120,15 @@ class Probe
 
                $numeric_fields = ['gsid', 'hide', 'account-type', 'manually-approve'];
 
+               if (!empty($data['photo'])) {
+                       $data['photo'] = Network::addBasePath($data['photo'], $data['url']);
+
+                       if (!Network::isValidHttpUrl($data['photo'])) {
+                               Logger::warning('Invalid URL for photo', ['url' => $data['url'], 'photo' => $data['photo']]);
+                               unset($data['photo']);
+                       }
+               }
+
                $newdata = [];
                foreach ($fields as $field) {
                        if (isset($data[$field])) {
@@ -138,8 +148,17 @@ class Probe
                foreach ([Protocol::DIASPORA, Protocol::OSTATUS] as $network) {
                        if (!empty($data['networks'][$network])) {
                                $data['networks'][$network]['subscribe'] = $newdata['subscribe'] ?? '';
-                               $data['networks'][$network]['baseurl'] = $newdata['baseurl'] ?? '';
-                               $data['networks'][$network]['gsid'] = $newdata['gsid'] ?? 0;
+                               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']);
                        }
@@ -159,7 +178,7 @@ class Probe
         */
        private static function ownHost(string $host): bool
        {
-               $own_host = DI::baseUrl()->getHostname();
+               $own_host = DI::baseUrl()->getHost();
 
                $parts = parse_url($host);
 
@@ -358,7 +377,9 @@ class Probe
                        if (empty($data) || (!empty($ap_profile) && empty($network) && (($data['network'] ?? '') != Protocol::DFRN))) {
                                $networks = $data['networks'] ?? [];
                                unset($data['networks']);
-                               $networks[$data['network']] = $data;
+                               if (!empty($data['network'])) {
+                                       $networks[$data['network']] = $data;
+                               }
                                $data = $ap_profile;
                                $data['networks'] = $networks;
                        } elseif (!empty($ap_profile)) {
@@ -507,7 +528,7 @@ class Probe
         * @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);
 
@@ -743,13 +764,13 @@ class Probe
                        $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).
+                       // We overwrite the detected nick with our try if the previous 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;
                        }
@@ -1602,7 +1623,7 @@ class Probe
                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)
+               // OStatus has serious issues when 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'];
@@ -1623,7 +1644,7 @@ class Probe
         *
         * @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())) {
@@ -1667,8 +1688,8 @@ class Probe
                }
                if ($avatar) {
                        foreach ($avatar->attributes as $attribute) {
-                               if ($attribute->name == 'src') {
-                                       $data['photo'] = trim($attribute->value);
+                               if (($attribute->name == 'src') && !empty($attribute->value)) {
+                                       $data['photo'] = Network::addBasePath($attribute->value, $baseurl);
                                }
                        }
                }
@@ -1684,7 +1705,7 @@ class Probe
         *
         * @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
@@ -1716,7 +1737,7 @@ class Probe
                        return [];
                }
 
-               $profile_data = self::pumpioProfileData($data['url']);
+               $profile_data = self::pumpioProfileData($data['url'], $baseurl);
 
                if (!$profile_data) {
                        return [];
@@ -1841,11 +1862,18 @@ class Probe
         */
        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);
 
@@ -2207,6 +2235,8 @@ 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']);
                        }
@@ -2226,10 +2256,31 @@ class Probe
                                '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])
+                               '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
+                       // Default values for nonexistent targets
                        $data = [
                                'name' => $url, 'nick' => $url, 'url' => $url, 'network' => Protocol::PHANTOM,
                                'photo' => DI::baseUrl() . Contact::DEFAULT_AVATAR_PHOTO