]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/APContact.php
Standards
[friendica.git] / src / Model / APContact.php
index 44773bf7e7e325eb6ad1be5b1db120390ecfdb96..bd6931c4cbe566a99bd8109bccf2fffb3a831aa0 100644 (file)
@@ -26,6 +26,7 @@ use Friendica\Core\Cache\Duration;
 use Friendica\Core\Logger;
 use Friendica\Core\System;
 use Friendica\Database\DBA;
+use Friendica\Database\DBStructure;
 use Friendica\DI;
 use Friendica\Network\Probe;
 use Friendica\Protocol\ActivityNamespace;
@@ -165,10 +166,11 @@ class APContact
                        return $fetched_contact;
                }
 
-               if (empty($data['id'])) {
+               $compacted = JsonLD::compact($data);
+               if (empty($compacted['@id'])) {
                        return $fetched_contact;
                }
-               
+
                // Detect multiple fast repeating request to the same address
                // See https://github.com/friendica/friendica/issues/9303
                $cachekey = 'apcontact:getByURL:' . $url;
@@ -179,39 +181,45 @@ class APContact
                        DI::cache()->set($cachekey, System::callstack(20), Duration::FIVE_MINUTES);
                }
 
-               $apcontact['url'] = $data['id'];
-               $apcontact['uuid'] = JsonLD::fetchElement($data, 'diaspora:guid');
-               $apcontact['type'] = JsonLD::fetchElement($data, 'type');
-               $apcontact['following'] = JsonLD::fetchElement($data, 'following');
-               $apcontact['followers'] = JsonLD::fetchElement($data, 'followers');
-               $apcontact['inbox'] = JsonLD::fetchElement($data, 'inbox');
+               $apcontact['url'] = $compacted['@id'];
+               $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid', '@value');
+               $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
+               $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
+               $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
+               $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
                self::unarchiveInbox($apcontact['inbox'], false);
-               $apcontact['outbox'] = JsonLD::fetchElement($data, 'outbox');
+
+               $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
 
                $apcontact['sharedinbox'] = '';
-               if (!empty($data['endpoints'])) {
-                       $apcontact['sharedinbox'] = JsonLD::fetchElement($data['endpoints'], 'sharedInbox');
+               if (!empty($compacted['as:endpoints'])) {
+                       $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
                        self::unarchiveInbox($apcontact['sharedinbox'], true);
                }
 
-               $apcontact['nick'] = JsonLD::fetchElement($data, 'preferredUsername') ?? '';
-               $apcontact['name'] = JsonLD::fetchElement($data, 'name');
+               $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername', '@value') ?? '';
+               $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name', '@value');
 
                if (empty($apcontact['name'])) {
                        $apcontact['name'] = $apcontact['nick'];
                }
 
-               $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($data, 'summary'));
+               $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary', '@value'));
 
-               $apcontact['photo'] = JsonLD::fetchElement($data, 'icon');
-               if (is_array($apcontact['photo']) || !empty($data['icon']['url'])) {
-                       $apcontact['photo'] = JsonLD::fetchElement($data['icon'], 'url');
+               $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
+               if (is_array($apcontact['photo']) || !empty($compacted['as:icon']['as:url']['@id'])) {
+                       $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
+               }
+
+               $apcontact['header'] = JsonLD::fetchElement($compacted, 'as:image', '@id');
+               if (is_array($apcontact['header']) || !empty($compacted['as:image']['as:url']['@id'])) {
+                       $apcontact['header'] = JsonLD::fetchElement($compacted['as:image'], 'as:url', '@id');
                }
 
                if (empty($apcontact['alias'])) {
-                       $apcontact['alias'] = JsonLD::fetchElement($data, 'url');
+                       $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
                        if (is_array($apcontact['alias'])) {
-                               $apcontact['alias'] = JsonLD::fetchElement($data['url'], 'href');
+                               $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
                        }
                }
 
@@ -241,44 +249,38 @@ class APContact
                }
 
                $apcontact['pubkey'] = null;
-               if (!empty($data['publicKey'])) {
-                       $apcontact['pubkey'] = trim(JsonLD::fetchElement($data['publicKey'], 'publicKeyPem'));
+               if (!empty($compacted['w3id:publicKey'])) {
+                       $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted['w3id:publicKey'], 'w3id:publicKeyPem', '@value'));
                        if (strstr($apcontact['pubkey'], 'RSA ')) {
                                $apcontact['pubkey'] = Crypto::rsaToPem($apcontact['pubkey']);
                        }
                }
 
-               $apcontact['manually-approve'] = (int)JsonLD::fetchElement($data, 'manuallyApprovesFollowers');
+               $apcontact['manually-approve'] = (int)JsonLD::fetchElement($compacted, 'as:manuallyApprovesFollowers');
 
-               if (!empty($data['generator'])) {
-                       $apcontact['baseurl'] = JsonLD::fetchElement($data['generator'], 'url');
-                       $apcontact['generator'] = JsonLD::fetchElement($data['generator'], 'name');
+               if (!empty($compacted['as:generator'])) {
+                       $apcontact['baseurl'] = JsonLD::fetchElement($compacted['as:generator'], 'as:url', '@id');
+                       $apcontact['generator'] = JsonLD::fetchElement($compacted['as:generator'], 'as:name', '@value');
                }
 
                if (!empty($apcontact['following'])) {
-                       $content = ActivityPub::fetchContent($apcontact['following']);
-                       if (!empty($content)) {
-                               if (!empty($content['totalItems'])) {
-                                       $apcontact['following_count'] = $content['totalItems'];
-                               }
+                       $following = ActivityPub::fetchContent($apcontact['following']);
+                       if (!empty($following['totalItems'])) {
+                               $apcontact['following_count'] = $following['totalItems'];
                        }
                }
 
                if (!empty($apcontact['followers'])) {
-                       $content = ActivityPub::fetchContent($apcontact['followers']);
-                       if (!empty($content)) {
-                               if (!empty($content['totalItems'])) {
-                                       $apcontact['followers_count'] = $content['totalItems'];
-                               }
+                       $followers = ActivityPub::fetchContent($apcontact['followers']);
+                       if (!empty($followers['totalItems'])) {
+                               $apcontact['followers_count'] = $followers['totalItems'];
                        }
                }
 
                if (!empty($apcontact['outbox'])) {
-                       $content = ActivityPub::fetchContent($apcontact['outbox']);
-                       if (!empty($content)) {
-                               if (!empty($content['totalItems'])) {
-                                       $apcontact['statuses_count'] = $content['totalItems'];
-                               }
+                       $outbox = ActivityPub::fetchContent($apcontact['outbox']);
+                       if (!empty($outbox['totalItems'])) {
+                               $apcontact['statuses_count'] = $outbox['totalItems'];
                        }
                }
 
@@ -331,7 +333,7 @@ class APContact
 
                if (empty($apcontact['subscribe'])) {
                        $apcontact['subscribe'] = null;
-               }               
+               }
 
                if (!empty($apcontact['baseurl']) && empty($fetched_contact['gsid'])) {
                        $apcontact['gsid'] = GServer::getID($apcontact['baseurl']);
@@ -353,6 +355,9 @@ class APContact
                        DBA::delete('apcontact', ['url' => $url]);
                }
 
+               // Limit the length on incoming fields
+               $apcontact = DBStructure::getFieldsForTable('apcontact', $apcontact);
+
                if (DBA::exists('apcontact', ['url' => $apcontact['url']])) {
                        DBA::update('apcontact', $apcontact, ['url' => $apcontact['url']]);
                } else {
@@ -361,7 +366,7 @@ class APContact
 
                Logger::info('Updated profile', ['url' => $url]);
 
-               return $apcontact;
+               return DBA::selectFirst('apcontact', [], ['url' => $apcontact['url']]) ?: [];
        }
 
        /**