]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Item.php
Fix magic links in contact photo menu
[friendica.git] / src / Content / Item.php
index c4af86e6b1dc2eee3f8a6a2321f44789bb04093b..172729f2689faca7ad91fe8fc89b26e2962277e8 100644 (file)
@@ -96,32 +96,33 @@ class Item
        }
 
        /**
-        * Return array with details for categories and folders for an item
+        * Lists categories and folders for an item
         *
         * @param array $item
         * @param int   $uid
-        * @return [array, array]
-        *
+        * @return array
         * [
-        *      [ // categories array
-        *          {
-        *               'name': 'category name',
-        *               'removeurl': 'url to remove this category',
-        *               'first': 'is the first in this array? true/false',
-        *               'last': 'is the last in this array? true/false',
-        *           } ,
-        *           ....
-        *       ],
-        *       [ //folders array
-        *                      {
-        *               'name': 'folder name',
-        *               'removeurl': 'url to remove this folder',
-        *               'first': 'is the first in this array? true/false',
-        *               'last': 'is the last in this array? true/false',
-        *           } ,
-        *           ....
-        *       ]
-        *  ]
+        *     [ // categories array
+        *         {
+        *             'name': 'category name',
+        *             'removeurl': 'url to remove this category',
+        *             'first': 'is the first in this array? true/false',
+        *             'last': 'is the last in this array? true/false',
+        *         },
+        *         ...
+        *     ],
+        *     [ //folders array
+        *         {
+        *             'name': 'folder name',
+        *             'removeurl': 'url to remove this folder',
+        *             'first': 'is the first in this array? true/false',
+        *             'last': 'is the last in this array? true/false',
+        *         } ,
+        *         ...
+        *     ]
+        * ]
+        *
+        * @throws \Exception
         */
        public function determineCategoriesTerms(array $item, int $uid = 0): array
        {
@@ -135,9 +136,9 @@ class Item
                        return [$categories, $folders];
                }
 
-               foreach (Post\Category::getArrayByURIId($item['uri-id'], $uid, Post\Category::CATEGORY) as $savedFolderName) {
+               foreach (Post\Category::getArrayByURIId($item['uri-id'], $uid) as $savedFolderName) {
                        if (!empty($item['author-link'])) {
-                               $url = $item['author-link'] . "?category=" . rawurlencode($savedFolderName);
+                               $url = $item['author-link'] . '/conversations?category=' . rawurlencode($savedFolderName);
                        } else {
                                $url = '#';
                        }
@@ -223,15 +224,13 @@ class Item
 
                                // select someone by nick in the current network
                                if (!DBA::isResult($contact) && ($network != '')) {
-                                       $condition = ["`nick` = ? AND `network` = ? AND `uid` = ?",
-                                               $name, $network, $profile_uid];
+                                       $condition = ['nick' => $name, 'network' => $network, 'uid' => $profile_uid];
                                        $contact = DBA::selectFirst('contact', $fields, $condition);
                                }
 
                                // select someone by attag in the current network
                                if (!DBA::isResult($contact) && ($network != '')) {
-                                       $condition = ["`attag` = ? AND `network` = ? AND `uid` = ?",
-                                               $name, $network, $profile_uid];
+                                       $condition = ['attag' => $name, 'network' => $network, 'uid' => $profile_uid];
                                        $contact = DBA::selectFirst('contact', $fields, $condition);
                                }
 
@@ -243,13 +242,13 @@ class Item
 
                                // select someone by nick in any network
                                if (!DBA::isResult($contact)) {
-                                       $condition = ["`nick` = ? AND `uid` = ?", $name, $profile_uid];
+                                       $condition = ['nick' => $name, 'uid' => $profile_uid];
                                        $contact = DBA::selectFirst('contact', $fields, $condition);
                                }
 
                                // select someone by attag in any network
                                if (!DBA::isResult($contact)) {
-                                       $condition = ["`attag` = ? AND `uid` = ?", $name, $profile_uid];
+                                       $condition = ['attag' => $name, 'uid' => $profile_uid];
                                        $contact = DBA::selectFirst('contact', $fields, $condition);
                                }
 
@@ -271,7 +270,7 @@ class Item
                                $replaced = true;
                                // create profile link
                                $profile = str_replace(',', '%2c', $profile);
-                               $newtag = $tag_type.'[url=' . $profile . ']' . $newname . '[/url]';
+                               $newtag = $tag_type . '[url=' . $profile . ']' . $newname . '[/url]';
                                $body = str_replace($tag_type . $name, $newtag, $body);
                        }
                }
@@ -295,8 +294,10 @@ class Item
                        $xmlhead = '<?xml version="1.0" encoding="UTF-8" ?>';
 
                        if ($this->activity->match($item['verb'], Activity::TAG)) {
-                               $fields = ['author-id', 'author-link', 'author-name', 'author-network',
-                                       'verb', 'object-type', 'resource-id', 'body', 'plink'];
+                               $fields = [
+                                       'author-id', 'author-link', 'author-name', 'author-network',
+                                       'verb', 'object-type', 'resource-id', 'body', 'plink'
+                               ];
                                $obj = Post::selectFirst($fields, ['uri' => $item['parent-uri']]);
                                if (!DBA::isResult($obj)) {
                                        $this->profiler->stopRecording();
@@ -333,12 +334,12 @@ class Item
                                        default:
                                                if ($obj['resource-id']) {
                                                        $post_type = $this->l10n->t('photo');
-                                                       $m=[]; preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
-                                                       $rr['plink'] = $m[1];
+                                                       preg_match("/\[url=([^]]*)\]/", $obj['body'], $matches);
+                                                       $rr['plink'] = $matches[1];
                                                } else {
                                                        $post_type = $this->l10n->t('status');
                                                }
-                                               // Let's break everthing ... ;-)
+                                               // Let's break everything ... ;-)
                                                break;
                                }
                                $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
@@ -377,7 +378,11 @@ class Item
                        'url' => $item['author-link'],
                ];
                $profile_link = Contact::magicLinkByContact($author, $item['author-link']);
-               $sparkle = (strpos($profile_link, 'contact/redir/') === 0);
+               if (strpos($profile_link, 'contact/redir/') === 0) {
+                       $status_link  = $profile_link . '?' . http_build_query(['url' => $item['author-link'] . '/status']);
+                       $photos_link  = $profile_link . '?' . http_build_query(['url' => $item['author-link'] . '/photos']);
+                       $profile_link = $profile_link . '?' . http_build_query(['url' => $item['author-link'] . '/profile']);
+               }
 
                $cid = 0;
                $pcid = $item['author-id'];
@@ -391,17 +396,12 @@ class Item
                        $rel = $contact['rel'];
                }
 
-               if ($sparkle) {
-                       $status_link = $profile_link . '/status';
-                       $photos_link = $profile_link . '/photos';
-                       $profile_link = $profile_link . '/profile';
-               }
-
                if (!empty($pcid)) {
-                       $contact_url = 'contact/' . $pcid;
-                       $posts_link  = $contact_url . '/posts';
-                       $block_link  = $item['self'] ? '' : $contact_url . '/block?t=' . $formSecurityToken;
-                       $ignore_link = $item['self'] ? '' : $contact_url . '/ignore?t=' . $formSecurityToken;
+                       $contact_url   = 'contact/' . $pcid;
+                       $posts_link    = $contact_url . '/posts';
+                       $block_link    = $item['self'] ? '' : $contact_url . '/block?t=' . $formSecurityToken;
+                       $ignore_link   = $item['self'] ? '' : $contact_url . '/ignore?t=' . $formSecurityToken;
+                       $collapse_link = $item['self'] ? '' : $contact_url . '/collapse?t=' . $formSecurityToken;
                }
 
                if ($cid && !$item['self']) {
@@ -423,7 +423,8 @@ class Item
                                $this->l10n->t('View Contact') => $contact_url,
                                $this->l10n->t('Send PM') => $pm_url,
                                $this->l10n->t('Block') => $block_link,
-                               $this->l10n->t('Ignore') => $ignore_link
+                               $this->l10n->t('Ignore') => $ignore_link,
+                               $this->l10n->t('Collapse') => $collapse_link
                        ];
 
                        if (!empty($item['language'])) {
@@ -431,7 +432,8 @@ class Item
                        }
 
                        if ((($cid == 0) || ($rel == Contact::FOLLOWER)) &&
-                               in_array($item['network'], Protocol::FEDERATED)) {
+                               in_array($item['network'], Protocol::FEDERATED)
+                       ) {
                                $menu[$this->l10n->t('Connect/Follow')] = 'contact/follow?url=' . urlencode($item['author-link']) . '&auto=1';
                        }
                } else {
@@ -545,7 +547,7 @@ class Item
                                $item['allow_cid'] = '';
                                $item['allow_gid'] = '';
                        }
-               } elseif ($setPermissions && ($item['gravity'] == ItemModel::GRAVITY_PARENT)) {
+               } elseif ($setPermissions) {
                        if (empty($receivers)) {
                                // For security reasons direct posts without any receiver will be posts to yourself
                                $self = Contact::selectFirst(['id'], ['uid' => $item['uid'], 'self' => true]);
@@ -682,11 +684,11 @@ class Item
 
                // If it is a reshared post then reformat it to avoid display problems with two share elements
                if (!empty($shared)) {
-                       if (!empty($shared['guid']) && ($encaspulated_share = $this->createSharedPostByGuid($shared['guid'], true))) {
+                       if (!empty($shared['guid']) && ($encapsulated_share = $this->createSharedPostByGuid($shared['guid'], true))) {
                                if (!empty(BBCode::fetchShareAttributes($item['body']))) {
-                                       $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encaspulated_share, $item['body']);
+                                       $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encapsulated_share, $item['body']);
                                } else {
-                                       $item['body'] .= $encaspulated_share;
+                                       $item['body'] .= $encapsulated_share;
                                }
                        }
                        $item['body'] = HTML::toBBCode(BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::ACTIVITYPUB));
@@ -856,8 +858,8 @@ class Item
                }
 
                $user = User::getById($post['uid'], ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);
-               if (!DBA::isResult($user)) {
-                       throw new HTTPException\NotFoundException($this->l10n->t('Unable to locate original post.'));
+               if (!$user) {
+                       throw new HTTPException\NotFoundException($this->l10n->t('Unable to fetch user.'));
                }
 
                $post['allow_cid'] = isset($request['contact_allow']) ? $this->aclFormatter->toString($request['contact_allow']) : $user['allow_cid'] ?? '';
@@ -876,7 +878,7 @@ class Item
                        $post['allow_cid'] .= $this->aclFormatter->toString(Contact::getPublicIdByUserId($post['uid']));
                }
 
-               if (strlen($post['allow_gid']) || strlen($post['allow_cid']) || strlen($post['deny_gid']) || strlen($post['deny_cid'])) {
+               if ($post['allow_gid'] || $post['allow_cid'] || $post['deny_gid'] || $post['deny_cid']) {
                        $post['private'] = ItemModel::PRIVATE;
                } elseif ($this->pConfig->get($post['uid'], 'system', 'unlisted')) {
                        $post['private'] = ItemModel::UNLISTED;
@@ -889,7 +891,7 @@ class Item
 
        public function moveAttachmentsFromBodyToAttach(array $post): array
        {
-               if (!preg_match_all('/(\[attachment\]([0-9]+)\[\/attachment\])/',$post['body'], $match)) {
+               if (!preg_match_all('/(\[attachment\]([0-9]+)\[\/attachment\])/', $post['body'], $match)) {
                        return $post;
                }
 
@@ -898,14 +900,20 @@ class Item
                        if (empty($attachment)) {
                                continue;
                        }
-                       if (strlen($post['attach'])) {
+                       if ($post['attach']) {
                                $post['attach'] .= ',';
                        }
-                       $post['attach'] .= Post\Media::getAttachElement($this->baseURL->get() . '/attach/' . $attachment['id'],
-                               $attachment['filesize'], $attachment['filetype'], $attachment['filename'] ?? '');
-
-                       $fields = ['allow_cid' => $post['allow_cid'], 'allow_gid' => $post['allow_gid'],
-                                       'deny_cid' => $post['deny_cid'], 'deny_gid' => $post['deny_gid']];
+                       $post['attach'] .= Post\Media::getAttachElement(
+                               $this->baseURL . '/attach/' . $attachment['id'],
+                               $attachment['filesize'],
+                               $attachment['filetype'],
+                               $attachment['filename'] ?? ''
+                       );
+
+                       $fields = [
+                               'allow_cid' => $post['allow_cid'], 'allow_gid' => $post['allow_gid'],
+                               'deny_cid' => $post['deny_cid'], 'deny_gid' => $post['deny_gid']
+                       ];
                        $condition = ['id' => $attachment_id];
                        Attach::update($fields, $condition);
                }
@@ -923,8 +931,9 @@ class Item
 
                // embedded bookmark or attachment in post? set bookmark flag
                $data = BBCode::getAttachmentData($post['body']);
-               if ((preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $post['body'], $match, PREG_SET_ORDER) || isset($data['type']))
-                       && ($post['post-type'] != ItemModel::PT_PERSONAL_NOTE)) {
+               if ((preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $post['body'], $match, PREG_SET_ORDER) || !empty($data['type']))
+                       && ($post['post-type'] != ItemModel::PT_PERSONAL_NOTE)
+               ) {
                        $post['post-type'] = ItemModel::PT_PAGE;
                        $post['object-type'] = Activity\ObjectType::BOOKMARK;
                }
@@ -932,33 +941,44 @@ class Item
                // Setting the object type if not defined before
                if (empty($post['object-type'])) {
                        $post['object-type'] = ($post['gravity'] == ItemModel::GRAVITY_PARENT) ? Activity\ObjectType::NOTE : Activity\ObjectType::COMMENT;
-
-                       $objectdata = BBCode::getAttachedData($post['body']);
-
-                       if ($objectdata['type'] == 'link') {
-                               $post['object-type'] = Activity\ObjectType::BOOKMARK;
-                       } elseif ($objectdata['type'] == 'video') {
-                               $post['object-type'] = Activity\ObjectType::VIDEO;
-                       } elseif ($objectdata['type'] == 'photo') {
-                               $post['object-type'] = Activity\ObjectType::IMAGE;
-                       }
                }
                return $post;
        }
 
        public function initializePost(array $post): array
        {
-               $post['wall']       = true;
-               $post['origin']     = true;
                $post['network']    = Protocol::DFRN;
                $post['protocol']   = Conversation::PARCEL_DIRECT;
                $post['direction']  = Conversation::PUSH;
-               $post['guid']       = System::createUUID();
-               $post['uri']        = ItemModel::newURI($post['guid']);
-               $post['verb']       = Activity::POST;
                $post['received']   = DateTimeFormat::utcNow();
+               $post['origin']     = true;
+               $post['wall']       = $post['wall'] ?? true;
+               $post['guid']       = $post['guid'] ?? System::createUUID();
+               $post['verb']       = $post['verb'] ?? Activity::POST;
+               $post['uri']        = $post['uri'] ?? ItemModel::newURI($post['guid']);
+               $post['thr-parent'] = $post['thr-parent'] ?? $post['uri'];
+
+               if (empty($post['gravity'])) {
+                       $post['gravity'] = ($post['uri'] == $post['thr-parent']) ? ItemModel::GRAVITY_PARENT : ItemModel::GRAVITY_COMMENT;
+               }
+
                $owner = User::getOwnerDataById($post['uid']);
 
+               if (!isset($post['allow_cid']) || !isset($post['allow_gid']) || !isset($post['deny_cid']) || !isset($post['deny_gid'])) {
+                       $post['allow_cid'] = $owner['allow_cid'];
+                       $post['allow_gid'] = $owner['allow_gid'];
+                       $post['deny_cid']  = $owner['deny_cid'];
+                       $post['deny_gid']  = $owner['deny_gid'];
+               }
+
+               if ($post['allow_gid'] || $post['allow_cid'] || $post['deny_gid'] || $post['deny_cid']) {
+                       $post['private'] = ItemModel::PRIVATE;
+               } elseif ($this->pConfig->get($post['uid'], 'system', 'unlisted')) {
+                       $post['private'] = ItemModel::UNLISTED;
+               } else {
+                       $post['private'] = ItemModel::PUBLIC;
+               }
+
                if (empty($post['contact-id'])) {
                        $post['contact-id'] = $owner['id'];
                }
@@ -989,7 +1009,6 @@ class Item
                // Convert links with empty descriptions to links without an explicit description
                $post['body'] = trim(preg_replace('#\[url=([^\]]*?)\]\[/url\]#ism', '[url]$1[/url]', $post['body']));
                $post['body'] = $this->bbCodeVideo->transform($post['body']);
-               $post['body'] = BBCode::scaleExternalImages($post['body']);
                $post = $this->setObjectType($post);
 
                // Personal notes must never be altered to a forum post.
@@ -1013,12 +1032,18 @@ class Item
 
                foreach ($recipients as $recipient) {
                        $address = trim($recipient);
-                       if (!strlen($address)) {
+                       if (!$address) {
                                continue;
                        }
 
-                       $this->emailer->send(new ItemCCEMail($this->app, $this->l10n, $this->baseURL,
-                               $post, $address, $author['thumb'] ?? ''));
+                       $this->emailer->send(new ItemCCEMail(
+                               $this->app,
+                               $this->l10n,
+                               $this->baseURL,
+                               $post,
+                               $address,
+                               $author['thumb'] ?? ''
+                       ));
                }
        }
 }