]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/APContact.php
Standards
[friendica.git] / src / Model / APContact.php
index 37faf240ce1bc5efcf22826e7a4b5ba1b86c3cdf..bd6931c4cbe566a99bd8109bccf2fffb3a831aa0 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -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;
@@ -147,14 +148,25 @@ class APContact
                        $url = $apcontact['url'];
                }
 
-               $data = ActivityPub::fetchContent($url);
-               if (empty($data)) {
+               $curlResult = HTTPSignature::fetchRaw($url);
+               $failed = empty($curlResult) || empty($curlResult->getBody()) ||
+                       (!$curlResult->isSuccess() && ($curlResult->getReturnCode() != 410));
+
+               if (!$failed) {
+                       $data = json_decode($curlResult->getBody(), true);
+                       $failed = empty($data) || !is_array($data);
+               }
+
+               if (!$failed && ($curlResult->getReturnCode() == 410)) {
+                       $data = ['@context' => ActivityPub::CONTEXT, 'id' => $url, 'type' => 'Tombstone'];
+               }
+
+               if ($failed) {
                        self::markForArchival($fetched_contact ?: []);
                        return $fetched_contact;
                }
 
                $compacted = JsonLD::compact($data);
-
                if (empty($compacted['@id'])) {
                        return $fetched_contact;
                }
@@ -199,6 +211,11 @@ class APContact
                        $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($compacted, 'as:url', '@id');
                        if (is_array($apcontact['alias'])) {
@@ -207,8 +224,11 @@ class APContact
                }
 
                // Quit if none of the basic values are set
-               if (empty($apcontact['url']) || empty($apcontact['inbox']) || empty($apcontact['type'])) {
+               if (empty($apcontact['url']) || empty($apcontact['type']) || (($apcontact['type'] != 'Tombstone') && empty($apcontact['inbox']))) {
                        return $fetched_contact;
+               } elseif ($apcontact['type'] == 'Tombstone') {
+                       // The "inbox" field must have a content
+                       $apcontact['inbox'] = '';
                }
 
                // Quit if this doesn't seem to be an account at all
@@ -221,7 +241,7 @@ class APContact
                unset($parts['path']);
 
                if (empty($apcontact['addr'])) {
-                       if (!empty($apcontact['nick'])) {
+                       if (!empty($apcontact['nick']) && is_array($parts)) {
                                $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
                        } else {
                                $apcontact['addr'] = '';
@@ -244,29 +264,23 @@ class APContact
                }
 
                if (!empty($apcontact['following'])) {
-                       $data = ActivityPub::fetchContent($apcontact['following']);
-                       if (!empty($data)) {
-                               if (!empty($data['totalItems'])) {
-                                       $apcontact['following_count'] = $data['totalItems'];
-                               }
+                       $following = ActivityPub::fetchContent($apcontact['following']);
+                       if (!empty($following['totalItems'])) {
+                               $apcontact['following_count'] = $following['totalItems'];
                        }
                }
 
                if (!empty($apcontact['followers'])) {
-                       $data = ActivityPub::fetchContent($apcontact['followers']);
-                       if (!empty($data)) {
-                               if (!empty($data['totalItems'])) {
-                                       $apcontact['followers_count'] = $data['totalItems'];
-                               }
+                       $followers = ActivityPub::fetchContent($apcontact['followers']);
+                       if (!empty($followers['totalItems'])) {
+                               $apcontact['followers_count'] = $followers['totalItems'];
                        }
                }
 
                if (!empty($apcontact['outbox'])) {
-                       $data = ActivityPub::fetchContent($apcontact['outbox']);
-                       if (!empty($data)) {
-                               if (!empty($data['totalItems'])) {
-                                       $apcontact['statuses_count'] = $data['totalItems'];
-                               }
+                       $outbox = ActivityPub::fetchContent($apcontact['outbox']);
+                       if (!empty($outbox['totalItems'])) {
+                               $apcontact['statuses_count'] = $outbox['totalItems'];
                        }
                }
 
@@ -319,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']);
@@ -341,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 {
@@ -349,7 +366,7 @@ class APContact
 
                Logger::info('Updated profile', ['url' => $url]);
 
-               return $apcontact;
+               return DBA::selectFirst('apcontact', [], ['url' => $apcontact['url']]) ?: [];
        }
 
        /**