]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Merge pull request #11886 from annando/database
[friendica.git] / src / Protocol / Diaspora.php
index 71eee8f316777606eae045e28d8e1f43d38dae6c..9e80c130282428401353223353ec1cac6fd4c6d9 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".
@@ -705,7 +705,7 @@ class Diaspora
                // This is something that shouldn't happen at all.
                if (in_array($type, ['status_message', 'reshare', 'profile'])) {
                        if ($msg['author'] != $fields->author) {
-                               Logger::notice('Message handle is not the same as envelope sender. Quitting this message.');
+                               Logger::notice('Message handle is not the same as envelope sender. Quitting this message.', ['author1' => $msg['author'], 'author2' => $fields->author]);
                                return false;
                        }
                }
@@ -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;
@@ -760,7 +766,7 @@ class Diaspora
        {
                $handle = strval($handle);
 
-               Logger::notice('Fetching diaspora key for: ' . $handle);
+               Logger::notice('Fetching diaspora key', ['handle' => $handle, 'callstack' => System::callstack(20)]);
 
                $fcontact = FContact::getByURL($handle);
                if (!empty($fcontact['pubkey'])) {
@@ -1357,15 +1363,15 @@ class Diaspora
                }
 
                $fields = [
-                       'url' => $data['url'],
-                       'nurl' => Strings::normaliseLink($data['url']),
-                       'name' => $data['name'],
-                       'nick' => $data['nick'],
-                       'addr' => $data['addr'],
-                       'batch' => $data['batch'],
-                       'notify' => $data['notify'],
-                       'poll' => $data['poll'],
-                       'network' => $data['network']
+                       'url'     => $data['url'],
+                       'nurl'    => Strings::normaliseLink($data['url']),
+                       'name'    => $data['name'],
+                       'nick'    => $data['nick'],
+                       'addr'    => $data['addr'],
+                       'batch'   => $data['batch'],
+                       'notify'  => $data['notify'],
+                       'poll'    => $data['poll'],
+                       'network' => $data['network'],
                ];
 
                Contact::update($fields, ['addr' => $old_handle]);
@@ -1419,7 +1425,7 @@ class Diaspora
 
                        $parts = parse_url($person['url']);
                        unset($parts['path']);
-                       $host_url = Uri::fromParts($parts);
+                       $host_url = (string)Uri::fromParts($parts);
 
                        return $host_url . '/objects/' . $guid;
                }
@@ -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);
                        }
                }
@@ -2382,6 +2385,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'];
@@ -2468,7 +2472,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);
 
                /// @todo Copy tag data from original post
 
@@ -2690,6 +2695,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 +2808,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 +2975,7 @@ class Diaspora
                        ]
                ];
 
-               $namespaces = ['me' => 'http://salmon-protocol.org/ns/magic-env'];
+               $namespaces = ['me' => ActivityNamespace::SALMON_ME];
 
                return XML::fromArray($xmldata, $xml, false, $namespaces);
        }
@@ -3114,7 +3137,7 @@ class Diaspora
                        Logger::notice('Empty addr', ['contact' => $contact ?? [], 'callstack' => System::callstack(20)]);
                }
 
-               $envelope = self::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $pubkey, $public_batch);
+               $envelope = self::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $pubkey ?? '', $public_batch);
 
                $return_code = self::transmit($owner, $contact, $envelope, $public_batch, $guid);
 
@@ -3621,7 +3644,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 +3998,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'] = '';