]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/DFRN.php
spelling: importer
[friendica.git] / src / Protocol / DFRN.php
index 47637bc2e2e4f5f2e98125c6c21831afb8af2995..10c43626a1d3e72b2653e3d40e16dbbda5acf241 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
  *
@@ -25,6 +25,7 @@ use DOMDocument;
 use DOMElement;
 use DOMNode;
 use DOMXPath;
+use Friendica\App;
 use Friendica\Content\Text\BBCode;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
@@ -33,7 +34,6 @@ use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\Event;
-use Friendica\Model\FContact;
 use Friendica\Model\GServer;
 use Friendica\Model\Item;
 use Friendica\Model\ItemURI;
@@ -44,6 +44,7 @@ use Friendica\Model\Post;
 use Friendica\Model\Profile;
 use Friendica\Model\Tag;
 use Friendica\Model\User;
+use Friendica\Network\HTTPException;
 use Friendica\Network\Probe;
 use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
@@ -300,16 +301,15 @@ class DFRN
                        DI::config()->set('system', 'site_pubkey', $res['pubkey']);
                }
 
-               $profilephotos = Photo::selectToArray(['resource-id' , 'scale'], ['profile' => true, 'uid' => $uid], ['order' => ['scale']]);
+               $profilephotos = Photo::selectToArray(['resource-id', 'scale', 'type'], ['profile' => true, 'uid' => $uid], ['order' => ['scale']]);
 
                $photos = [];
                $ext = Images::supportedTypes();
 
                foreach ($profilephotos as $p) {
-                       $photos[$p['scale']] = DI::baseUrl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']];
+                       $photos[$p['scale']] = DI::baseUrl() . '/photo/' . $p['resource-id'] . '-' . $p['scale'] . '.' . $ext[$p['type']];
                }
 
-
                $doc = new DOMDocument('1.0', 'utf-8');
                $doc->formatOutput = true;
 
@@ -369,8 +369,8 @@ class DFRN
                XML::addElement($doc, $root, 'id', DI::baseUrl() . '/profile/' . $owner['nick']);
                XML::addElement($doc, $root, 'title', $owner['name']);
 
-               $attributes = ['uri' => 'https://friendi.ca', 'version' => FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION];
-               XML::addElement($doc, $root, 'generator', FRIENDICA_PLATFORM, $attributes);
+               $attributes = ['uri' => 'https://friendi.ca', 'version' => App::VERSION . '-' . DB_UPDATE_VERSION];
+               XML::addElement($doc, $root, 'generator', App::PLATFORM, $attributes);
 
                $attributes = ['rel' => 'license', 'href' => 'http://creativecommons.org/licenses/by/3.0/'];
                XML::addElement($doc, $root, 'link', '', $attributes);
@@ -537,7 +537,7 @@ class DFRN
 
                        XML::addElement($doc, $author, 'poco:utcOffset', DateTimeFormat::timezoneNow($profile['timezone'], 'P'));
 
-                       if (trim($profile['homepage']) != '') {
+                       if (trim($profile['homepage'])) {
                                $urls = $doc->createElement('poco:urls');
                                XML::addElement($doc, $urls, 'poco:type', 'homepage');
                                XML::addElement($doc, $urls, 'poco:value', $profile['homepage']);
@@ -545,7 +545,7 @@ class DFRN
                                $author->appendChild($urls);
                        }
 
-                       if (trim($profile['pub_keywords']) != '') {
+                       if (trim($profile['pub_keywords'] ?? '')) {
                                $keywords = explode(',', $profile['pub_keywords']);
 
                                foreach ($keywords as $keyword) {
@@ -553,7 +553,7 @@ class DFRN
                                }
                        }
 
-                       if (trim($profile['xmpp']) != '') {
+                       if (trim($profile['xmpp'])) {
                                $ims = $doc->createElement('poco:ims');
                                XML::addElement($doc, $ims, 'poco:type', 'xmpp');
                                XML::addElement($doc, $ims, 'poco:value', $profile['xmpp']);
@@ -561,7 +561,7 @@ class DFRN
                                $author->appendChild($ims);
                        }
 
-                       if (trim($profile['locality'] . $profile['region'] . $profile['country-name']) != '') {
+                       if (trim($profile['locality'] . $profile['region'] . $profile['country-name'])) {
                                $element = $doc->createElement('poco:address');
 
                                XML::addElement($doc, $element, 'poco:formatted', Profile::formatLocation($profile));
@@ -710,7 +710,7 @@ class DFRN
         */
        private static function getAttachment($doc, $root, array $item)
        {
-               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
+               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
                        $attributes = ['rel' => 'enclosure',
                                'href' => $attachment['url'],
                                'type' => $attachment['mimetype']];
@@ -773,7 +773,8 @@ class DFRN
                        $entry->setAttribute("xmlns:statusnet", ActivityNamespace::STATUSNET);
                }
 
-               $body = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body'] ?? '');
+               $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
+               $body = Post\Media::addHTMLAttachmentToBody($item['uri-id'], $body);
 
                if ($item['private'] == Item::PRIVATE) {
                        $body = Item::fixPrivatePhotos($body, $owner['uid'], $item, $cid);
@@ -799,7 +800,7 @@ class DFRN
                $dfrnowner = self::addEntryAuthor($doc, "dfrn:owner", $item["owner-link"], $item);
                $entry->appendChild($dfrnowner);
 
-               if ($item['gravity'] != GRAVITY_PARENT) {
+               if ($item['gravity'] != Item::GRAVITY_PARENT) {
                        $parent = Post::selectFirst(['guid', 'plink'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]);
                        if (DBA::isResult($parent)) {
                                $attributes = ["ref" => $item['thr-parent'], "type" => "text/html",
@@ -888,7 +889,7 @@ class DFRN
 
                if ($item['object-type'] != '') {
                        XML::addElement($doc, $entry, 'activity:object-type', $item['object-type']);
-               } elseif ($item['gravity'] == GRAVITY_PARENT) {
+               } elseif ($item['gravity'] == Item::GRAVITY_PARENT) {
                        XML::addElement($doc, $entry, 'activity:object-type', Activity\ObjectType::NOTE);
                } else {
                        XML::addElement($doc, $entry, 'activity:object-type', Activity\ObjectType::COMMENT);
@@ -980,12 +981,12 @@ class DFRN
                                }
                        }
 
-                       $fcontact = FContact::getByURL($contact['addr']);
-                       if (empty($fcontact)) {
+                       try {
+                               $pubkey = DI::dsprContact()->getByAddr(WebFingerUri::fromString($contact['addr']))->pubKey;
+                       } catch (HTTPException\NotFoundException|\InvalidArgumentException $e) {
                                Logger::notice('Unable to find contact details for ' . $contact['id'] . ' - ' . $contact['addr']);
                                return -22;
                        }
-                       $pubkey = $fcontact['pubkey'] ?? '';
                } else {
                        $pubkey = '';
                }
@@ -1014,6 +1015,10 @@ class DFRN
                $xml = $postResult->getBody();
 
                $curl_stat = $postResult->getReturnCode();
+               if (!empty($contact['gsid']) && ($postResult->isTimeout() || empty($curl_stat))) {
+                       GServer::setFailureById($contact['gsid']);
+               }
+
                if (empty($curl_stat) || empty($xml)) {
                        Logger::notice('Empty answer from ' . $contact['id'] . ' - ' . $dest_url);
                        return -9; // timed out
@@ -1035,6 +1040,10 @@ class DFRN
                        return -23;
                }
 
+               if (!empty($contact['gsid'])) {
+                       GServer::setReachableById($contact['gsid'], Protocol::DFRN);
+               }
+
                if (!empty($res->message)) {
                        Logger::info('Transmit to ' . $dest_url . ' returned status '.$res->status.' - '.$res->message);
                }
@@ -1065,8 +1074,8 @@ class DFRN
 
                $fields = ['id', 'uid', 'url', 'network', 'avatar-date', 'avatar', 'name-date', 'uri-date', 'addr',
                        'name', 'nick', 'about', 'location', 'keywords', 'xmpp', 'bdyear', 'bd', 'hidden', 'contact-type'];
-               $condition = ["`uid` = ? AND `nurl` = ? AND `network` != ? AND NOT `pending` AND NOT `blocked`",
-                       $importer["importer_uid"], Strings::normaliseLink($author["link"]), Protocol::STATUSNET];
+               $condition = ["`uid` = ? AND `nurl` = ? AND NOT `pending` AND NOT `blocked`",
+                       $importer["importer_uid"], Strings::normaliseLink($author["link"])];
 
                if ($importer['account-type'] != User::ACCOUNT_TYPE_COMMUNITY) {
                        $condition = DBA::mergeConditions($condition, ['rel' => [Contact::SHARING, Contact::FRIEND]]);
@@ -1333,7 +1342,7 @@ class DFRN
         */
        private static function processMail(DOMXPath $xpath, DOMNode $mail, array $importer)
        {
-               Logger::notice("Processing mails");
+               Logger::info("Processing mails");
 
                $msg = [];
                $msg['uid'] = $importer['importer_uid'];
@@ -1361,7 +1370,7 @@ class DFRN
         */
        private static function processSuggestion(DOMXPath $xpath, DOMNode $suggestion, array $importer)
        {
-               Logger::notice('Processing suggestions');
+               Logger::info('Processing suggestions');
 
                $url = $xpath->evaluate('string(dfrn:url[1]/text())', $suggestion);
                $cid = Contact::getIdForURL($url);
@@ -1437,7 +1446,7 @@ class DFRN
         */
        private static function processRelocation(DOMXPath $xpath, DOMNode $relocation, array $importer): bool
        {
-               Logger::notice("Processing relocations");
+               Logger::info("Processing relocations");
 
                /// @TODO Rewrite this to one statement
                $relocate = [];
@@ -1490,7 +1499,7 @@ class DFRN
 
                Contact::updateAvatar($importer['id'], $relocate['avatar'], true);
 
-               Logger::notice('Contacts are updated.');
+               Logger::info('Contacts are updated.');
 
                /// @TODO
                /// merge with current record, current contents have priority
@@ -1585,22 +1594,22 @@ class DFRN
                        // Big question: Do we need these functions? They were part of the "consume_feed" function.
                        // This function once was responsible for DFRN and OStatus.
                        if ($activity->match($item['verb'], Activity::FOLLOW)) {
-                               Logger::notice("New follower");
+                               Logger::info("New follower");
                                Contact::addRelationship($importer, $contact, $item);
                                return false;
                        }
                        if ($activity->match($item['verb'], Activity::UNFOLLOW)) {
-                               Logger::notice("Lost follower");
+                               Logger::info("Lost follower");
                                Contact::removeFollower($contact);
                                return false;
                        }
                        if ($activity->match($item['verb'], Activity::REQ_FRIEND)) {
-                               Logger::notice("New friend request");
+                               Logger::info("New friend request");
                                Contact::addRelationship($importer, $contact, $item, true);
                                return false;
                        }
                        if ($activity->match($item['verb'], Activity::UNFRIEND)) {
-                               Logger::notice("Lost sharer");
+                               Logger::info("Lost sharer");
                                Contact::removeSharer($contact);
                                return false;
                        }
@@ -1612,21 +1621,21 @@ class DFRN
                                || ($item['verb'] == Activity::ATTENDMAYBE)
                                || ($item['verb'] == Activity::ANNOUNCE)
                        ) {
-                               $item['gravity'] = GRAVITY_ACTIVITY;
+                               $item['gravity'] = Item::GRAVITY_ACTIVITY;
                                // only one like or dislike per person
                                // split into two queries for performance issues
                                $condition = [
-                                       'uid' => $item['uid'],
-                                       'author-id' => $item['author-id'],
-                                       'gravity' => GRAVITY_ACTIVITY,
-                                       'verb' => $item['verb'],
+                                       'uid'        => $item['uid'],
+                                       'author-id'  => $item['author-id'],
+                                       'gravity'    => Item::GRAVITY_ACTIVITY,
+                                       'verb'       => $item['verb'],
                                        'parent-uri' => $item['thr-parent'],
                                ];
                                if (Post::exists($condition)) {
                                        return false;
                                }
 
-                               $condition = ['uid' => $item['uid'], 'author-id' => $item['author-id'], 'gravity' => GRAVITY_ACTIVITY,
+                               $condition = ['uid' => $item['uid'], 'author-id' => $item['author-id'], 'gravity' => Item::GRAVITY_ACTIVITY,
                                        'verb' => $item['verb'], 'thr-parent' => $item['thr-parent']];
                                if (Post::exists($condition)) {
                                        return false;
@@ -1704,7 +1713,7 @@ class DFRN
         * Checks if an incoming message is wanted
         *
         * @param array $item
-        * @param array $imporer
+        * @param array $importer
         * @return boolean Is the message wanted?
         */
        private static function isSolicitedMessage(array $item, array $importer): bool
@@ -1750,7 +1759,7 @@ class DFRN
         */
        private static function processEntry(array $header, DOMXPath $xpath, DOMNode $entry, array $importer, string $xml, int $protocol)
        {
-               Logger::notice("Processing entries");
+               Logger::info("Processing entries");
 
                $item = $header;
 
@@ -1837,7 +1846,11 @@ class DFRN
 
                $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
 
-               $item['body'] = Item::improveSharedDataInBody($item);
+               $quote_uri_id = Item::getQuoteUriId($item['body'], $item['uid']);
+               if (!empty($quote_uri_id)) {
+                       $item['quote-uri-id'] = $quote_uri_id;
+                       $item['body']         = BBCode::removeSharedData($item['body']);
+               }
 
                Tag::storeFromBody($item['uri-id'], $item['body']);
 
@@ -1938,7 +1951,7 @@ class DFRN
 
                // Now assign the rest of the values that depend on the type of the message
                if (in_array($entrytype, [self::REPLY, self::REPLY_RC])) {
-                       $item['gravity'] = GRAVITY_COMMENT;
+                       $item['gravity'] = Item::GRAVITY_COMMENT;
 
                        if (!isset($item['object-type'])) {
                                $item['object-type'] = Activity\ObjectType::COMMENT;
@@ -1964,7 +1977,7 @@ class DFRN
                if ($entrytype == self::REPLY_RC) {
                        $item['wall'] = 1;
                } elseif ($entrytype == self::TOP_LEVEL) {
-                       $item['gravity'] = GRAVITY_PARENT;
+                       $item['gravity'] = Item::GRAVITY_PARENT;
 
                        if (!isset($item['object-type'])) {
                                $item['object-type'] = Activity\ObjectType::NOTE;
@@ -2034,7 +2047,7 @@ class DFRN
                                        Logger::info('Contact is not sharing with the user', ['uid' => $item['uid'], 'owner-id' => $item['owner-id'], 'author-id' => $item['author-id'], 'gravity' => $item['gravity'], 'uri' => $item['uri']]);
                                        return;
                                }
-                               if (($item['gravity'] == GRAVITY_ACTIVITY) && DI::pConfig()->get($item['uid'], 'system', 'accept_only_sharer') == Item::COMPLETION_COMMENT) {
+                               if (($item['gravity'] == Item::GRAVITY_ACTIVITY) && DI::pConfig()->get($item['uid'], 'system', 'accept_only_sharer') == Item::COMPLETION_COMMENT) {
                                        Logger::info('Completion is set to "comment", but this is an activity. so we stop here.', ['uid' => $item['uid'], 'owner-id' => $item['owner-id'], 'author-id' => $item['author-id'], 'gravity' => $item['gravity'], 'uri' => $item['uri']]);
                                        return;
                                }
@@ -2067,6 +2080,7 @@ class DFRN
                        // This is my contact on another system, but it's really me.
                        // Turn this into a wall post.
                        $notify = Item::isRemoteSelf($importer, $item);
+                       $item['wall'] = (bool)$notify;
 
                        $posted_id = Item::insert($item, $notify);
 
@@ -2093,7 +2107,7 @@ class DFRN
         */
        private static function processDeletion(DOMXPath $xpath, DOMNode $deletion, array $importer)
        {
-               Logger::notice("Processing deletions");
+               Logger::info("Processing deletions");
                $uri = null;
 
                foreach ($deletion->attributes as $attributes) {
@@ -2119,13 +2133,13 @@ class DFRN
                }
 
                // When it is a starting post it has to belong to the person that wants to delete it
-               if (($item['gravity'] == GRAVITY_PARENT) && ($item['contact-id'] != $importer['id'])) {
+               if (($item['gravity'] == Item::GRAVITY_PARENT) && ($item['contact-id'] != $importer['id'])) {
                        Logger::info('Item with URI ' . $uri . ' do not belong to contact ' . $importer['id'] . ' - ignoring deletion.');
                        return;
                }
 
                // Comments can be deleted by the thread owner or comment owner
-               if (($item['gravity'] != GRAVITY_PARENT) && ($item['contact-id'] != $importer['id'])) {
+               if (($item['gravity'] != Item::GRAVITY_PARENT) && ($item['contact-id'] != $importer['id'])) {
                        $condition = ['id' => $item['parent'], 'contact-id' => $importer['id']];
                        if (!Post::exists($condition)) {
                                Logger::info('Item with URI ' . $uri . ' was not found or must not be deleted by contact ' . $importer['id'] . ' - ignoring deletion.');
@@ -2273,7 +2287,7 @@ class DFRN
                                self::processDeletion($xpath, $deletion, $importer);
                        }
                        if (count($deletions) > 0) {
-                               Logger::notice(count($deletions) . ' deletions had been processed');
+                               Logger::info(count($deletions) . ' deletions had been processed');
                                return 200;
                        }
                }