]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
The quote functionality is simplified
[friendica.git] / src / Protocol / Diaspora.php
index c5ad51215b72bdd134bae6b367daa777140f169a..88665f949a029c657b7db8c11d4b9d10e0ba5591 100644 (file)
@@ -128,7 +128,7 @@ class Diaspora
                        return false;
                }
 
-               $children = $basedom->children('http://salmon-protocol.org/ns/magic-env');
+               $children = $basedom->children(ActivityNamespace::SALMON_ME);
 
                if (sizeof($children) == 0) {
                        Logger::notice('XML has no children');
@@ -475,7 +475,7 @@ class Diaspora
                }
 
                if (!($fields = self::validPosting($msg))) {
-                       Logger::notice('Invalid posting');
+                       Logger::warning('Invalid posting');
                        return false;
                }
 
@@ -510,7 +510,7 @@ class Diaspora
                if (is_null($fields)) {
                        $private = true;
                        if (!($fields = self::validPosting($msg))) {
-                               Logger::notice('Invalid posting');
+                               Logger::warning('Invalid posting');
                                return false;
                        }
                } else {
@@ -629,7 +629,7 @@ class Diaspora
                $type = $element->getName();
                $orig_type = $type;
 
-               Logger::debug('Got message type ' . $type . ': ' . $msg['message']);
+               Logger::debug('Got message', ['type' => $type, 'message' => $msg['message']]);
 
                // All retractions are handled identically from now on.
                // In the new version there will only be "retraction".
@@ -714,9 +714,15 @@ class Diaspora
                if (!in_array($type, ['comment', 'like'])) {
                        return $fields;
                }
+
+               if (!isset($author_signature) && ($msg['author'] == $fields->author)) {
+                       Logger::debug('No author signature, but the sender matches the author', ['type' => $type, 'msg-author' => $msg['author'], 'message' => $msg['message']]);
+                       return $fields;
+               }
+
                // No author_signature? This is a must, so we quit.
                if (!isset($author_signature)) {
-                       Logger::info('No author signature for type ' . $type . ' - Message: ' . $msg['message']);
+                       Logger::info('No author signature', ['type' => $type, 'msg-author' => $msg['author'], 'fields-author' => $fields->author, 'message' => $msg['message']]);
                        return false;
                }
 
@@ -728,7 +734,7 @@ class Diaspora
                        }
 
                        if (!Crypto::rsaVerify($signed_data, $parent_author_signature, $key, 'sha256')) {
-                               Logger::info('No valid parent author signature for parent author ' . $msg['author'] . ' in type ' . $type . ' - signed data: ' . $signed_data . ' - Message: ' . $msg['message'] . ' - Signature ' . $parent_author_signature);
+                               Logger::info('No valid parent author signature', ['author' => $msg['author'], 'type' => $type, 'signed data' => $signed_data, 'message'  => $msg['message'], 'signature' => $parent_author_signature]);
                                return false;
                        }
                }
@@ -740,7 +746,7 @@ class Diaspora
                }
 
                if (!Crypto::rsaVerify($signed_data, $author_signature, $key, 'sha256')) {
-                       Logger::info('No valid author signature for author ' . $fields->author . ' in type ' . $type . ' - signed data: ' . $signed_data . ' - Message: ' . $msg['message'] . ' - Signature ' . $author_signature);
+                       Logger::info('No valid author signature for author', ['author' => $fields->author, 'type' => $type, 'signed data' => $signed_data, 'message'  => $msg['message'], 'signature' => $author_signature]);
                        return false;
                } else {
                        return $fields;
@@ -1019,7 +1025,7 @@ class Diaspora
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function storeByGuid(string $guid, string $server, bool $force)
+       public static function storeByGuid(string $guid, string $server, bool $force)
        {
                $serverparts = parse_url($server);
 
@@ -1535,13 +1541,7 @@ class Diaspora
                $datarray['owner-id'] = Contact::getIdForURL($contact['url'], 0);
 
                // Will be overwritten for sharing accounts in Item::insert
-               if (in_array($direction, [self::FETCHED, self::FORCED_FETCH])) {
-                       $datarray['post-reason'] = Item::PR_FETCHED;
-               } elseif ($datarray['uid'] == 0) {
-                       $datarray['post-reason'] = Item::PR_GLOBAL;
-               } else {
-                       $datarray['post-reason'] = Item::PR_COMMENT;
-               }
+               $datarray = self::setDirection($datarray, $direction);
 
                $datarray['guid'] = $guid;
                $datarray['uri'] = self::getUriFromGuid($author, $guid);
@@ -1557,7 +1557,8 @@ class Diaspora
 
                $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
                $datarray['source'] = $xml;
-               $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
+
+               $datarray = self::setDirection($datarray, $direction);
 
                $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $created_at;
 
@@ -1710,7 +1711,7 @@ class Diaspora
                        }
                }
                if (!$conversation) {
-                       Logger::notice('Unable to create conversation.');
+                       Logger::warning('Unable to create conversation.');
                        return false;
                }
 
@@ -1786,12 +1787,13 @@ class Diaspora
                $datarray = [];
 
                $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
-               $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
 
                $datarray['uid'] = $importer['uid'];
                $datarray['contact-id'] = $author_contact['cid'];
                $datarray['network']  = $author_contact['network'];
 
+               $datarray = self::setDirection($datarray, $direction);
+
                $datarray['owner-link'] = $datarray['author-link'] = $person['url'];
                $datarray['owner-id'] = $datarray['author-id'] = Contact::getIdForURL($person['url'], 0);
 
@@ -1965,12 +1967,13 @@ class Diaspora
                $datarray = [];
 
                $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
-               $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
 
                $datarray['uid'] = $importer['uid'];
                $datarray['contact-id'] = $author_contact['cid'];
                $datarray['network']  = $author_contact['network'];
 
+               $datarray = self::setDirection($datarray, $direction);
+
                $datarray['owner-link'] = $datarray['author-link'] = $person['url'];
                $datarray['owner-id'] = $datarray['author-id'] = Contact::getIdForURL($person['url'], 0);
 
@@ -1998,11 +2001,11 @@ class Diaspora
                Logger::info('Participation stored', ['id' => $message_id, 'guid' => $guid, 'parent_guid' => $parent_guid, 'author' => $author]);
 
                // Send all existing comments and likes to the requesting server
-               $comments = Post::select(['id', 'uri-id', 'parent-author-network', 'author-network', 'verb'],
+               $comments = Post::select(['id', 'uri-id', 'parent-author-network', 'author-network', 'verb', 'gravity'],
                        ['parent' => $toplevel_parent_item['id'], 'gravity' => [GRAVITY_COMMENT, GRAVITY_ACTIVITY]]);
                while ($comment = Post::fetch($comments)) {
-                       if (in_array($comment['verb'], [Activity::FOLLOW, Activity::TAG])) {
-                               Logger::info('participation messages are not relayed', ['item' => $comment['id']]);
+                       if (($comment['gravity'] == GRAVITY_ACTIVITY) && !in_array($comment['verb'], [Activity::LIKE, Activity::DISLIKE])) {
+                               Logger::info('Unsupported activities are not relayed', ['item' => $comment['id'], 'verb' => $comment['verb']]);
                                continue;
                        }
 
@@ -2017,7 +2020,7 @@ class Diaspora
                        }
 
                        Logger::info('Deliver participation', ['item' => $comment['id'], 'contact' => $author_contact['cid']]);
-                       if (Worker::add(PRIORITY_HIGH, 'Delivery', Delivery::POST, $comment['id'], $author_contact['cid'])) {
+                       if (Worker::add(PRIORITY_HIGH, 'Delivery', Delivery::POST, $comment['uri-id'], $author_contact['cid'], $datarray['uid'])) {
                                Post\DeliveryData::incrementQueueCount($comment['uri-id'], 1);
                        }
                }
@@ -2270,83 +2273,6 @@ class Diaspora
                return true;
        }
 
-       /**
-        * Fetches a message with a given guid
-        *
-        * @param string $guid        message guid
-        * @param string $orig_author handle of the original post
-        * @return array|bool The fetched item or false on failure
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        * @throws \ImagickException
-        */
-       public static function originalItem(string $guid, string $orig_author)
-       {
-               if (empty($guid)) {
-                       Logger::notice('Empty guid. Quitting.');
-                       return false;
-               }
-
-               // Do we already have this item?
-               $fields = ['body', 'title', 'app', 'created', 'object-type', 'uri', 'guid',
-                       'author-name', 'author-link', 'author-avatar', 'plink', 'uri-id'];
-               $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => [Item::PUBLIC, Item::UNLISTED]];
-               $item = Post::selectFirst($fields, $condition);
-
-               if (DBA::isResult($item)) {
-                       Logger::notice("reshared message " . $guid . " already exists on system.");
-
-                       // Maybe it is already a reshared item?
-                       // Then refetch the content, if it is a reshare from a reshare.
-                       // If it is a reshared post from another network then reformat to avoid display problems with two share elements
-                       if (self::isReshare($item['body'], true)) {
-                               $item = [];
-                       } elseif (self::isReshare($item['body'], false) || strstr($item['body'], '[share')) {
-                               $item['body'] = Markdown::toBBCode(BBCode::toMarkdown($item['body']));
-
-                               $item['body'] = self::replacePeopleGuid($item['body'], $item['author-link']);
-
-                               return $item;
-                       } else {
-                               return $item;
-                       }
-               }
-
-               if (!DBA::isResult($item)) {
-                       if (empty($orig_author)) {
-                               Logger::notice('Empty author for guid ' . $guid . '. Quitting.');
-                               return false;
-                       }
-
-                       $server = 'https://' . substr($orig_author, strpos($orig_author, '@') + 1);
-                       Logger::notice('1st try: reshared message ' . $guid . ' will be fetched via SSL from the server ' . $server);
-                       $stored = self::storeByGuid($guid, $server, true);
-
-                       if (!$stored) {
-                               $server = 'http://' . substr($orig_author, strpos($orig_author, '@') + 1);
-                               Logger::notice('2nd try: reshared message ' . $guid . ' will be fetched without SSL from the server ' . $server);
-                               $stored = self::storeByGuid($guid, $server, true);
-                       }
-
-                       if ($stored) {
-                               $fields = ['body', 'title', 'app', 'created', 'object-type', 'uri', 'guid',
-                                       'author-name', 'author-link', 'author-avatar', 'plink', 'uri-id'];
-                               $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => [Item::PUBLIC, Item::UNLISTED]];
-                               $item = Post::selectFirst($fields, $condition);
-
-                               if (DBA::isResult($item)) {
-                                       // If it is a reshared post from another network then reformat to avoid display problems with two share elements
-                                       if (self::isReshare($item['body'], false)) {
-                                               $item['body'] = Markdown::toBBCode(BBCode::toMarkdown($item['body']));
-                                               $item['body'] = self::replacePeopleGuid($item['body'], $item['author-link']);
-                                       }
-
-                                       return $item;
-                               }
-                       }
-               }
-               return false;
-       }
-
        /**
         * Stores a reshare activity
         *
@@ -2382,6 +2308,7 @@ class Diaspora
                $datarray['protocol'] = $item['protocol'];
                $datarray['source'] = $item['source'];
                $datarray['direction'] = $item['direction'];
+               $datarray['post-reason'] = $item['post-reason'];
 
                $datarray['plink'] = self::plink($author, $datarray['guid']);
                $datarray['private'] = $item['private'];
@@ -2438,15 +2365,11 @@ class Diaspora
                        return true;
                }
 
-               $original_item = self::originalItem($root_guid, $root_author);
-               if (!$original_item) {
+               $original_person = FContact::getByURL($root_author);
+               if (!$original_person) {
                        return false;
                }
 
-               if (empty($original_item['plink'])) {
-                       $original_item['plink'] = self::plink($root_author, $root_guid);
-               }
-
                $datarray = [];
 
                $datarray['uid'] = $importer['uid'];
@@ -2468,35 +2391,18 @@ class Diaspora
 
                $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
                $datarray['source'] = $xml;
-               $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
 
-               /// @todo Copy tag data from original post
-
-               $prefix = BBCode::getShareOpeningTag(
-                       $original_item['author-name'],
-                       $original_item['author-link'],
-                       $original_item['author-avatar'],
-                       $original_item['plink'],
-                       $original_item['created'],
-                       $original_item['guid']
-               );
+               $datarray = self::setDirection($datarray, $direction);
 
-               if (!empty($original_item['title'])) {
-                       $prefix .= '[h3]' . $original_item['title'] . "[/h3]\n";
-               }
-
-               $datarray['body'] = $prefix.$original_item['body'] . '[/share]';
+               $datarray['body'] = DI::contentItem()->createSharedPostByGuid($root_guid, $importer['uid'], $original_person['url']);
 
+               /// @todo Copy tag data from original post
                Tag::storeFromBody($datarray['uri-id'], $datarray['body']);
 
-               $datarray['app']  = $original_item['app'];
-
                $datarray['plink'] = self::plink($author, $guid);
                $datarray['private'] = (($public == 'false') ? Item::PRIVATE : Item::PUBLIC);
                $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $created_at;
 
-               $datarray['object-type'] = $original_item['object-type'];
-
                self::fetchGuid($datarray);
 
                if (Item::isTooOld($datarray)) {
@@ -2690,6 +2596,29 @@ class Diaspora
                Post\Media::insert($data);
        }
 
+       /**
+        * Set direction and post reason
+        *
+        * @param array $datarray
+        * @param integer $direction
+        *
+        * @return array
+        */
+       public static function setDirection(array $datarray, int $direction): array
+       {
+               $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
+
+               if (in_array($direction, [self::FETCHED, self::FORCED_FETCH])) {
+                       $datarray['post-reason'] = Item::PR_FETCHED;
+               } elseif ($datarray['uid'] == 0) {
+                       $datarray['post-reason'] = Item::PR_GLOBAL;
+               } else {
+                       $datarray['post-reason'] = Item::PR_PUSHED;
+               }
+
+               return $datarray;
+       }
+
        /**
         * Receives status messages
         *
@@ -2780,13 +2709,8 @@ class Diaspora
 
                $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
                $datarray['source'] = $xml;
-               $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
 
-               if (in_array($direction, [self::FETCHED, self::FORCED_FETCH])) {
-                       $datarray['post-reason'] = Item::PR_FETCHED;
-               } elseif ($datarray['uid'] == 0) {
-                       $datarray['post-reason'] = Item::PR_GLOBAL;
-               }
+               $datarray = self::setDirection($datarray, $direction);
 
                $datarray['body'] = self::replacePeopleGuid($body, $contact['url']);
                $datarray['raw-body'] = self::replacePeopleGuid($raw_body, $contact['url']);
@@ -2952,7 +2876,7 @@ class Diaspora
                        ]
                ];
 
-               $namespaces = ['me' => 'http://salmon-protocol.org/ns/magic-env'];
+               $namespaces = ['me' => ActivityNamespace::SALMON_ME];
 
                return XML::fromArray($xmldata, $xml, false, $namespaces);
        }
@@ -3451,9 +3375,9 @@ class Diaspora
                                }
                        }
 
+                       // @todo Check if this is obsolete and if we are still using different owners. (Possibly a fragment from the forum functionality)
                        if ($item['author-link'] != $item['owner-link']) {
-                               $body = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'],
-                                       $item['plink'], $item['created']) . $body . '[/share]';
+                               $body = DI::contentItem()->createSharedBlockByArray($item);
                        }
 
                        // convert to markdown
@@ -3621,7 +3545,7 @@ class Diaspora
                                $attend_answer = 'tentative';
                                break;
                        default:
-                               Logger::notice('Unknown verb ' . $item['verb'] . ' in item ' . $item['guid']);
+                               Logger::warning('Unknown verb ' . $item['verb'] . ' in item ' . $item['guid']);
                                return false;
                }
 
@@ -3975,7 +3899,7 @@ class Diaspora
                                $data['birthday'] = DateTimeFormat::utc($year . '-' . $month . '-' . $day, 'Y-m-d');
                        }
 
-                       $data['about'] = BBCode::toMarkdown($profile['about']);
+                       $data['about'] = BBCode::toMarkdown($profile['about'] ?? '');
 
                        $data['location'] = $profile['location'];
                        $data['tag_string'] = '';
@@ -4110,6 +4034,11 @@ class Diaspora
                        return false;
                }
 
+               if (!self::parentSupportDiaspora($item['thr-parent-id'])) {
+                       Logger::info('One of the parents does not support Diaspora. A signature will not be created.', ['uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
+                       return false;
+               }
+
                $message = self::constructComment($item, $owner);
                if ($message === false) {
                        return false;
@@ -4120,26 +4049,42 @@ class Diaspora
                return $message;
        }
 
-       public static function performReshare(int $UriId, int $uid): int
+       /**
+        * Check if the parent and their parents support Diaspora
+        *
+        * @param integer $parent_id
+        * @return boolean
+        */
+       private static function parentSupportDiaspora(int $parent_id): bool
        {
-               $fields = ['uri-id', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink'];
-               $item = Post::selectFirst($fields, ['uri-id' => $UriId, 'uid' => [$uid, 0], 'private' => [Item::PUBLIC, Item::UNLISTED]]);
-               if (!DBA::isResult($item)) {
-                       return 0;
+               $parent_post = Post::selectFirstPost(['gravity', 'signed_text', 'author-link', 'thr-parent-id'], ['uri-id' => $parent_id]);
+               if (empty($parent_post['thr-parent-id'])) {
+                       Logger::warning('Parent post does not exist.', ['parent-id' => $parent_id]);
+                       return false;
                }
 
-               if (strpos($item['body'], '[/share]') !== false) {
-                       $pos = strpos($item['body'], '[share');
-                       $post = substr($item['body'], $pos);
-               } else {
-                       $post = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'], $item['plink'], $item['created'], $item['guid']);
+               if (empty(FContact::getByURL($parent_post['author-link'], false))) {
+                       Logger::info('Parent author is no Diaspora contact.', ['parent-id' => $parent_id]);
+                       return false;
+               }
 
-                       if (!empty($item['title'])) {
-                               $post .= '[h3]' . $item['title'] . "[/h3]\n";
-                       }
+               if (($parent_post['gravity'] == GRAVITY_COMMENT) && empty($parent_post['signed_text'])) {
+                       Logger::info('Parent comment has got no Diaspora signature.', ['parent-id' => $parent_id]);
+                       return false;
+               }
+
+               if ($parent_post['gravity'] == GRAVITY_COMMENT) {
+                       return self::parentSupportDiaspora($parent_post['thr-parent-id']);
+               }
+
+               return true;
+       }
 
-                       $post .= $item['body'];
-                       $post .= '[/share]';
+       public static function performReshare(int $UriId, int $uid): int
+       {
+               $post = DI::contentItem()->createSharedPostByUriId($UriId, $uid);
+               if (empty($post)) {
+                       return 0;
                }
 
                $owner  = User::getOwnerDataById($uid);