]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Escape url tags before attempting to add missing mention links in Protocol\ActivityPu...
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index 9e8ea2976e5ad3046c5f5c18e4acaddd11a0780b..47a5300352d03ee73104ad05290d4f134c4035b8 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
  *
@@ -21,9 +21,9 @@
 
 namespace Friendica\Protocol\ActivityPub;
 
-use Friendica\Content\PageInfo;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
+use Friendica\Content\Text\Markdown;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Database\DBA;
@@ -32,6 +32,7 @@ use Friendica\Model\APContact;
 use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\Event;
+use Friendica\Model\GServer;
 use Friendica\Model\Item;
 use Friendica\Model\ItemURI;
 use Friendica\Model\Mail;
@@ -51,20 +52,36 @@ use Friendica\Util\Strings;
 class Processor
 {
        /**
-        * Converts mentions from Pleroma into the Friendica format
+        * Extracts the tag character (#, @, !) from mention links
         *
         * @param string $body
+        * @return string
+        */
+       protected static function normalizeMentionLinks(string $body): string
+       {
+               return preg_replace('%\[url=([^\[\]]*)]([#@!])(.*?)\[/url]%ism', '$2[url=$1]$3[/url]', $body);
+       }
+
+       /**
+        * Convert the language array into a language JSON
         *
-        * @return string converted body
+        * @param array $languages
+        * @return string language JSON
         */
-       private static function convertMentions($body)
+       private static function processLanguages(array $languages)
        {
-               $URLSearchString = "^\[\]";
-               $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
+               $codes = array_keys($languages);
+               $lang = [];
+               foreach ($codes as $code) {
+                       $lang[$code] = 1;
+               }
 
-               return $body;
-       }
+               if (empty($lang)) {
+                       return '';
+               }
 
+               return json_encode($lang);
+       }
        /**
         * Replaces emojis in the body
         *
@@ -101,24 +118,11 @@ class Processor
                }
 
                $data = ['uri-id' => $uriid];
-
-               $filetype = strtolower(substr($attachment['mediaType'], 0, strpos($attachment['mediaType'], '/')));
-               if ($filetype == 'image') {
-                       $data['type'] = Post\Media::IMAGE;
-               } elseif ($filetype == 'video') {
-                       $data['type'] = Post\Media::VIDEO;
-               } elseif ($filetype == 'audio') {
-                       $data['type'] = Post\Media::AUDIO;
-               } elseif (in_array($attachment['mediaType'], ['application/x-bittorrent', 'application/x-bittorrent;x-scheme-handler/magnet'])) {
-                       $data['type'] = Post\Media::TORRENT;
-               } else {
-                       Logger::info('Unknown type', ['attachment' => $attachment]);
-                       return;
-               }
-
+               $data['type'] = Post\Media::UNKNOWN;
                $data['url'] = $attachment['url'];
                $data['mimetype'] = $attachment['mediaType'];
                $data['height'] = $attachment['height'] ?? null;
+               $data['width'] = $attachment['width'] ?? null;
                $data['size'] = $attachment['size'] ?? null;
                $data['preview'] = $attachment['image'] ?? null;
                $data['description'] = $attachment['name'] ?? null;
@@ -127,84 +131,20 @@ class Processor
        }
 
        /**
-        * Add attachment data to the item array
+        * Stire attachment data
         *
         * @param array   $activity
         * @param array   $item
-        *
-        * @return array array
         */
-       private static function constructAttachList($activity, $item)
+       private static function storeAttachments($activity, $item)
        {
                if (empty($activity['attachments'])) {
-                       return $item;
+                       return;
                }
 
                foreach ($activity['attachments'] as $attach) {
-                       switch ($attach['type']) {
-                               case 'link':
-                                       $data = [
-                                               'url'      => $attach['url'],
-                                               'type'     => $attach['type'],
-                                               'title'    => $attach['title'] ?? '',
-                                               'text'     => $attach['desc']  ?? '',
-                                               'image'    => $attach['image'] ?? '',
-                                               'images'   => [],
-                                               'keywords' => [],
-                                       ];
-                                       $item['body'] = PageInfo::appendDataToBody($item['body'], $data);
-                                       break;
-                               default:
-                                       self::storeAttachmentAsMedia($item['uri-id'], $attach);
-
-                                       $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
-                                       if ($filetype == 'image') {
-                                               if (!empty($activity['source'])) {
-                                                       foreach ([0, 1, 2] as $size) {
-                                                               if (preg_match('#/photo/.*-' . $size . '\.#ism', $attach['url']) && 
-                                                                       strpos(preg_replace('#(/photo/.*)-[012]\.#ism', '$1-' . $size . '.', $activity['source']), $attach['url'])) {
-                                                                       continue 3;
-                                                               }
-                                                       }
-                                                       if (strpos($activity['source'], $attach['url'])) {
-                                                               continue 2;
-                                                       }
-                                               }
-
-                                               $item['body'] .= "\n";
-
-                                               // image is the preview/thumbnail URL
-                                               if (!empty($attach['image'])) {
-                                                       $item['body'] .= '[url=' . $attach['url'] . ']';
-                                                       $attach['url'] = $attach['image'];
-                                               }
-
-                                               if (empty($attach['name'])) {
-                                                       $item['body'] .= '[img]' . $attach['url'] . '[/img]';
-                                               } else {
-                                                       $item['body'] .= '[img=' . $attach['url'] . ']' . $attach['name'] . '[/img]';
-                                               }
-
-                                               if (!empty($attach['image'])) {
-                                                       $item['body'] .= '[/url]';
-                                               }
-                                       } elseif ($filetype == 'audio') {
-                                               if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
-                                                       continue 2;
-                                               }
-
-                                               $item['body'] .= "\n[audio]" . $attach['url'] . '[/audio]';
-                                       } elseif ($filetype == 'video') {
-                                               if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
-                                                       continue 2;
-                                               }
-
-                                               $item['body'] .= "\n[video]" . $attach['url'] . '[/video]';
-                                       }
-                       }
+                       self::storeAttachmentAsMedia($item['uri-id'], $attach);
                }
-
-               return $item;
        }
 
        /**
@@ -215,7 +155,7 @@ class Processor
         */
        public static function updateItem($activity)
        {
-               $item = Post::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity'], ['uri' => $activity['id']]);
+               $item = Post::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity', 'post-type'], ['uri' => $activity['id']]);
                if (!DBA::isResult($item)) {
                        Logger::warning('No existing item, item will be created', ['uri' => $activity['id']]);
                        $item = self::createItem($activity);
@@ -228,7 +168,7 @@ class Processor
 
                $item = self::processContent($activity, $item);
 
-               $item = self::constructAttachList($activity, $item);
+               self::storeAttachments($activity, $item);
 
                if (empty($item)) {
                        return;
@@ -301,6 +241,24 @@ class Processor
                        $item['direction'] = Conversation::RELAY;
                }
 
+               if ($activity['object_type'] == 'as:Article') {
+                       $item['post-type'] = Item::PT_ARTICLE;
+               } elseif ($activity['object_type'] == 'as:Audio') {
+                       $item['post-type'] = Item::PT_AUDIO;
+               } elseif ($activity['object_type'] == 'as:Document') {
+                       $item['post-type'] = Item::PT_DOCUMENT;
+               } elseif ($activity['object_type'] == 'as:Event') {
+                       $item['post-type'] = Item::PT_EVENT;
+               } elseif ($activity['object_type'] == 'as:Image') {
+                       $item['post-type'] = Item::PT_IMAGE;
+               } elseif ($activity['object_type'] == 'as:Page') {
+                       $item['post-type'] = Item::PT_PAGE;
+               } elseif ($activity['object_type'] == 'as:Video') {
+                       $item['post-type'] = Item::PT_VIDEO;
+               } else {
+                       $item['post-type'] = Item::PT_NOTE;
+               }
+
                $item['isForum'] = false;
 
                if (!empty($activity['thread-completion'])) {
@@ -344,7 +302,20 @@ class Processor
 
                $item['plink'] = $activity['alternate-url'] ?? $item['uri'];
 
-               $item = self::constructAttachList($activity, $item);
+               self::storeAttachments($activity, $item);
+
+               // We received the post via AP, so we set the protocol of the server to AP
+               $contact = Contact::getById($item['author-id'], ['gsid']);
+               if (!empty($contact['gsid'])) {
+                       GServer::setProtocol($contact['gsid'], Post\DeliveryData::ACTIVITYPUB);
+               }
+
+               if ($item['author-id'] != $item['owner-id']) {
+                       $contact = Contact::getById($item['owner-id'], ['gsid']);
+                       if (!empty($contact['gsid'])) {
+                               GServer::setProtocol($contact['gsid'], Post\DeliveryData::ACTIVITYPUB);
+                       }
+               }
 
                return $item;
        }
@@ -378,7 +349,7 @@ class Processor
                }
 
                foreach ($activity['receiver'] as $receiver) {
-                       $item = Post::selectFirst(['id', 'uri-id', 'tag', 'origin', 'author-link'], ['uri' => $activity['target_id'], 'uid' => $receiver]);
+                       $item = Post::selectFirst(['id', 'uri-id', 'origin', 'author-link'], ['uri' => $activity['target_id'], 'uid' => $receiver]);
                        if (!DBA::isResult($item)) {
                                // We don't fetch missing content for this purpose
                                continue;
@@ -408,6 +379,7 @@ class Processor
                $item['verb'] = $verb;
                $item['thr-parent'] = $activity['object_id'];
                $item['gravity'] = GRAVITY_ACTIVITY;
+               unset($item['post-type']);
                $item['object-type'] = Activity\ObjectType::NOTE;
 
                $item['diaspora_signed_text'] = $activity['diaspora:like'] ?? '';
@@ -420,6 +392,8 @@ class Processor
         *
         * @param array $activity Activity array
         * @param array $item
+        * 
+        * @return int event id
         * @throws \Exception
         */
        public static function createEvent($activity, $item)
@@ -430,7 +404,7 @@ class Processor
                $event['finish']    = $activity['end-time'];
                $event['nofinish']  = empty($event['finish']);
                $event['location']  = $activity['location'];
-               $event['adjust']    = true;
+               $event['adjust']    = $activity['adjust'] ?? true;
                $event['cid']       = $item['contact-id'];
                $event['uid']       = $item['uid'];
                $event['uri']       = $item['uri'];
@@ -443,14 +417,16 @@ class Processor
                $event['direction'] = $item['direction'];
                $event['source']    = $item['source'];
 
-               $condition = ['uri' => $item['uri'], 'uid' => $item['uid']];
-               $ev = DBA::selectFirst('event', ['id'], $condition);
+               $ev = DBA::selectFirst('event', ['id'], ['uri' => $item['uri'], 'uid' => $item['uid']]);
                if (DBA::isResult($ev)) {
                        $event['id'] = $ev['id'];
                }
 
                $event_id = Event::store($event);
+
                Logger::info('Event was stored', ['id' => $event_id]);
+
+               return $event_id;
        }
 
        /**
@@ -463,19 +439,32 @@ class Processor
         */
        private static function processContent($activity, $item)
        {
-               $item['title'] = HTML::toBBCode($activity['name']);
+               if (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/markdown')) {
+                       $item['title'] = Markdown::toBBCode($activity['name']);
+                       $content = Markdown::toBBCode($activity['content']);
+               } elseif (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/bbcode')) {
+                       $item['title'] = $activity['name'];
+                       $content = $activity['content'];
+               } else {
+                       // By default assume "text/html"
+                       $item['title'] = HTML::toBBCode($activity['name']);
+                       $content = HTML::toBBCode($activity['content']);
+               }
 
-               $content = HTML::toBBCode($activity['content']);
+               if (!empty($activity['languages'])) {
+                       $item['language'] = self::processLanguages($activity['languages']);
+               }
 
                if (!empty($activity['emojis'])) {
                        $content = self::replaceEmojis($content, $activity['emojis']);
                }
 
-               $content = self::convertMentions($content);
+               $content = self::addMentionLinks($content, $activity['tags']);
 
                if (!empty($activity['source'])) {
                        $item['body'] = $activity['source'];
                        $item['raw-body'] = $content;
+                       $item['body'] = Item::improveSharedDataInBody($item);
                } else {
                        if (empty($activity['directmessage']) && ($item['thr-parent'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
                                $item_private = !in_array(0, $activity['item_receiver']);
@@ -569,34 +558,34 @@ class Processor
                        $type = $activity['reception_type'][$receiver] ?? Receiver::TARGET_UNKNOWN;
                        switch($type) {
                                case Receiver::TARGET_TO:
-                                       $item['post-type'] = Item::PT_TO;
+                                       $item['post-reason'] = Item::PR_TO;
                                        break;
                                case Receiver::TARGET_CC:
-                                       $item['post-type'] = Item::PT_CC;
+                                       $item['post-reason'] = Item::PR_CC;
                                        break;
                                case Receiver::TARGET_BTO:
-                                       $item['post-type'] = Item::PT_BTO;
+                                       $item['post-reason'] = Item::PR_BTO;
                                        break;
                                case Receiver::TARGET_BCC:
-                                       $item['post-type'] = Item::PT_BCC;
+                                       $item['post-reason'] = Item::PR_BCC;
                                        break;
                                case Receiver::TARGET_FOLLOWER:
-                                       $item['post-type'] = Item::PT_FOLLOWER;
+                                       $item['post-reason'] = Item::PR_FOLLOWER;
                                        break;
                                case Receiver::TARGET_ANSWER:
-                                       $item['post-type'] = Item::PT_COMMENT;
+                                       $item['post-reason'] = Item::PR_COMMENT;
                                        break;
                                case Receiver::TARGET_GLOBAL:
-                                       $item['post-type'] = Item::PT_GLOBAL;
+                                       $item['post-reason'] = Item::PR_GLOBAL;
                                        break;
                                default:
-                                       $item['post-type'] = Item::PT_ARTICLE;
+                                       $item['post-reason'] = Item::PR_NONE;
                        }
 
                        if (!empty($activity['from-relay'])) {
-                               $item['post-type'] = Item::PT_RELAY;
+                               $item['post-reason'] = Item::PR_RELAY;
                        } elseif (!empty($activity['thread-completion'])) {
-                               $item['post-type'] = Item::PT_FETCHED;
+                               $item['post-reason'] = Item::PR_FETCHED;
                        }
 
                        if ($item['isForum'] ?? false) {
@@ -614,6 +603,12 @@ class Processor
                                continue;
                        }
 
+                       if (!($item['isForum'] ?? false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT) &&
+                               ($item['post-reason'] == Item::PR_BCC) && !Contact::isSharingByURL($activity['author'], $receiver)) {
+                               Logger::info('Top level post via BCC from a non sharer, ignoring', ['uid' => $receiver, 'contact' => $item['contact-id']]);
+                               continue;
+                       }
+
                        if (DI::pConfig()->get($receiver, 'system', 'accept_only_sharer', false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT)) {
                                $skip = !Contact::isSharingByURL($activity['author'], $receiver);
 
@@ -630,7 +625,9 @@ class Processor
                        }
 
                        if (($item['gravity'] != GRAVITY_ACTIVITY) && ($activity['object_type'] == 'as:Event')) {
-                               self::createEvent($activity, $item);
+                               $event_id = self::createEvent($activity, $item);
+
+                               $item = Event::getItemArrayForImportedId($event_id, $item);
                        }
 
                        $item_id = Item::insert($item);
@@ -749,7 +746,7 @@ class Processor
                                        $title = $matches[3];
                                }
 
-                               $title = trim(HTML::toPlaintext(BBCode::convert($title, false, BBCode::API, true), 0));
+                               $title = trim(BBCode::toPlaintext($title));
 
                                if (strlen($title) > 20) {
                                        $title = substr($title, 0, 20) . '...';
@@ -782,12 +779,12 @@ class Processor
 
                $object = ActivityPub::fetchContent($url, $uid);
                if (empty($object)) {
-                       Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
+                       Logger::notice('Activity was not fetchable, aborting.', ['url' => $url]);
                        return '';
                }
 
                if (empty($object['id'])) {
-                       Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
+                       Logger::notice('Activity has got not id, aborting. ', ['url' => $url, 'object' => $object]);
                        return '';
                }
 
@@ -868,8 +865,9 @@ class Processor
                }
 
                $replyto = JsonLD::fetchElement($activity['as:object'], 'as:inReplyTo', '@id');
-               if (Post::exists(['uri' => $replyto])) {
-                       Logger::info('Post is a reply to an existing post - accepted', ['id' => $id, 'replyto' => $replyto]);
+               $uriid = ItemURI::getIdByURI($replyto);
+               if (Post::exists(['uri-id' => $uriid])) {
+                       Logger::info('Post is a reply to an existing post - accepted', ['id' => $id, 'uri-id' => $uriid, 'replyto' => $replyto]);
                        return true;
                }
 
@@ -937,7 +935,7 @@ class Processor
                        DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
                }
 
-               Logger::log('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
+               Logger::notice('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
        }
 
        /**
@@ -1142,7 +1140,7 @@ class Processor
 
                $implicit_mentions = [];
                if (empty($parent_author['url'])) {
-                       Logger::notice('Author public contact unknown.', ['author-link' => $parent['author-link'], 'item-id' => $parent['id']]);
+                       Logger::notice('Author public contact unknown.', ['author-link' => $parent['author-link'], 'parent-id' => $parent['id']]);
                } else {
                        $implicit_mentions[] = $parent_author['url'];
                        $implicit_mentions[] = $parent_author['nurl'];
@@ -1196,4 +1194,38 @@ class Processor
 
                return implode('', $kept_mentions);
        }
+
+       /**
+        * Adds links to string mentions
+        *
+        * @param string $body
+        * @param array  $tags
+        * @return string
+        */
+       protected static function addMentionLinks(string $body, array $tags): string
+       {
+               // This prevents links to be added again to Pleroma-style mention links
+               $body = self::normalizeMentionLinks($body);
+
+               $body = BBCode::performWithEscapedTags($body, ['url'], function ($body) use ($tags) {
+                       foreach ($tags as $tag) {
+                               if (empty($tag['name']) || empty($tag['type']) || empty($tag['href']) || !in_array($tag['type'], ['Mention', 'Hashtag'])) {
+                                       continue;
+                               }
+
+                               $hash = substr($tag['name'], 0, 1);
+                               $name = substr($tag['name'], 1);
+                               if (!in_array($hash, Tag::TAG_CHARACTER)) {
+                                       $hash = '';
+                                       $name = $tag['name'];
+                               }
+
+                               $body = str_replace($tag['name'], $hash . '[url=' . $tag['href'] . ']' . $name . '[/url]', $body);
+                       }
+
+                       return $body;
+               });
+
+               return $body;
+       }
 }