3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Content;
25 use Friendica\App\BaseURL;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Content\Text\BBCode\Video;
28 use Friendica\Content\Text\HTML;
29 use Friendica\Core\Hook;
30 use Friendica\Core\L10n;
31 use Friendica\Core\Logger;
32 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
33 use Friendica\Core\Protocol;
34 use Friendica\Core\Session\Capability\IHandleUserSessions;
35 use Friendica\Core\System;
36 use Friendica\Database\DBA;
37 use Friendica\Model\Attach;
38 use Friendica\Model\Contact;
39 use Friendica\Model\Conversation;
40 use Friendica\Model\FileTag;
41 use Friendica\Model\Group;
42 use Friendica\Model\Item as ItemModel;
43 use Friendica\Model\Photo;
44 use Friendica\Model\Tag;
45 use Friendica\Model\Post;
46 use Friendica\Model\User;
47 use Friendica\Network\HTTPException;
48 use Friendica\Object\EMail\ItemCCEMail;
49 use Friendica\Protocol\Activity;
50 use Friendica\Util\ACLFormatter;
51 use Friendica\Util\DateTimeFormat;
52 use Friendica\Util\Emailer;
53 use Friendica\Util\ParseUrl;
54 use Friendica\Util\Profiler;
55 use Friendica\Util\Proxy;
56 use Friendica\Util\XML;
59 * A content helper class for displaying items
69 /** @var IHandleUserSessions */
73 /** @var ACLFormatter */
74 private $aclFormatter;
75 /** @var IManagePersonalConfigValues */
84 public function __construct(Profiler $profiler, Activity $activity, L10n $l10n, IHandleUserSessions $userSession, Video $bbCodeVideo, ACLFormatter $aclFormatter, IManagePersonalConfigValues $pConfig, BaseURL $baseURL, Emailer $emailer, App $app)
86 $this->profiler = $profiler;
87 $this->activity = $activity;
89 $this->userSession = $userSession;
90 $this->bbCodeVideo = $bbCodeVideo;
91 $this->aclFormatter = $aclFormatter;
92 $this->baseURL = $baseURL;
93 $this->pConfig = $pConfig;
94 $this->emailer = $emailer;
99 * Return array with details for categories and folders for an item
103 * @return [array, array]
106 * [ // categories array
108 * 'name': 'category name',
109 * 'removeurl': 'url to remove this category',
110 * 'first': 'is the first in this array? true/false',
111 * 'last': 'is the last in this array? true/false',
117 * 'name': 'folder name',
118 * 'removeurl': 'url to remove this folder',
119 * 'first': 'is the first in this array? true/false',
120 * 'last': 'is the last in this array? true/false',
126 public function determineCategoriesTerms(array $item, int $uid = 0): array
132 $uid = $item['uid'] ?: $uid;
134 if (empty($item['has-categories'])) {
135 return [$categories, $folders];
138 foreach (Post\Category::getArrayByURIId($item['uri-id'], $uid, Post\Category::CATEGORY) as $savedFolderName) {
139 if (!empty($item['author-link'])) {
140 $url = $item['author-link'] . "?category=" . rawurlencode($savedFolderName);
145 'name' => $savedFolderName,
147 'removeurl' => $this->userSession->getLocalUserId() == $uid ? 'filerm/' . $item['id'] . '?cat=' . rawurlencode($savedFolderName) : '',
154 if (count($categories)) {
155 $categories[count($categories) - 1]['last'] = true;
158 if ($this->userSession->getLocalUserId() == $uid) {
159 foreach (Post\Category::getArrayByURIId($item['uri-id'], $uid, Post\Category::FILE) as $savedFolderName) {
161 'name' => $savedFolderName,
163 'removeurl' => $this->userSession->getLocalUserId() == $uid ? 'filerm/' . $item['id'] . '?term=' . rawurlencode($savedFolderName) : '',
171 if (count($folders)) {
172 $folders[count($folders) - 1]['last'] = true;
175 return [$categories, $folders];
179 * This function removes the tag $tag from the text $body and replaces it with
180 * the appropriate link.
182 * @param string $body the text to replace the tag in
183 * @param int $profile_uid the user id to replace the tag for (0 = anyone)
184 * @param string $tag the tag to replace
185 * @param string $network The network of the post
187 * @return array|bool ['replaced' => $replaced, 'contact' => $contact] or "false" on if already replaced
188 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
189 * @throws \ImagickException
191 public static function replaceTag(string &$body, int $profile_uid, string $tag, string $network = '')
195 //is it a person tag?
196 if (Tag::isType($tag, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION)) {
197 $tag_type = substr($tag, 0, 1);
198 //is it already replaced?
199 if (strpos($tag, '[url=')) {
203 //get the person's name
204 $name = substr($tag, 1);
206 // Sometimes the tag detection doesn't seem to work right
207 // This is some workaround
208 $nameparts = explode(' ', $name);
209 $name = $nameparts[0];
211 // Try to detect the contact in various ways
212 if (strpos($name, 'http://') || strpos($name, '@')) {
213 $contact = Contact::getByURLForUser($name, $profile_uid);
216 $fields = ['id', 'url', 'nick', 'name', 'alias', 'network', 'forum', 'prv'];
218 if (strrpos($name, '+')) {
219 // Is it in format @nick+number?
220 $tagcid = intval(substr($name, strrpos($name, '+') + 1));
221 $contact = DBA::selectFirst('contact', $fields, ['id' => $tagcid, 'uid' => $profile_uid]);
224 // select someone by nick in the current network
225 if (!DBA::isResult($contact) && ($network != '')) {
226 $condition = ["`nick` = ? AND `network` = ? AND `uid` = ?",
227 $name, $network, $profile_uid];
228 $contact = DBA::selectFirst('contact', $fields, $condition);
231 // select someone by attag in the current network
232 if (!DBA::isResult($contact) && ($network != '')) {
233 $condition = ["`attag` = ? AND `network` = ? AND `uid` = ?",
234 $name, $network, $profile_uid];
235 $contact = DBA::selectFirst('contact', $fields, $condition);
238 //select someone by name in the current network
239 if (!DBA::isResult($contact) && ($network != '')) {
240 $condition = ['name' => $name, 'network' => $network, 'uid' => $profile_uid];
241 $contact = DBA::selectFirst('contact', $fields, $condition);
244 // select someone by nick in any network
245 if (!DBA::isResult($contact)) {
246 $condition = ["`nick` = ? AND `uid` = ?", $name, $profile_uid];
247 $contact = DBA::selectFirst('contact', $fields, $condition);
250 // select someone by attag in any network
251 if (!DBA::isResult($contact)) {
252 $condition = ["`attag` = ? AND `uid` = ?", $name, $profile_uid];
253 $contact = DBA::selectFirst('contact', $fields, $condition);
256 // select someone by name in any network
257 if (!DBA::isResult($contact)) {
258 $condition = ['name' => $name, 'uid' => $profile_uid];
259 $contact = DBA::selectFirst('contact', $fields, $condition);
263 // Check if $contact has been successfully loaded
264 if (DBA::isResult($contact)) {
265 $profile = $contact['url'];
266 $newname = ($contact['name'] ?? '') ?: $contact['nick'];
269 //if there is an url for this persons profile
270 if (isset($profile) && ($newname != '')) {
272 // create profile link
273 $profile = str_replace(',', '%2c', $profile);
274 $newtag = $tag_type.'[url=' . $profile . ']' . $newname . '[/url]';
275 $body = str_replace($tag_type . $name, $newtag, $body);
279 return ['replaced' => $replaced, 'contact' => $contact];
283 * Render actions localized
287 * @throws ImagickException
288 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
290 public function localize(array &$item)
292 $this->profiler->startRecording('rendering');
293 /// @todo The following functionality needs to be cleaned up.
294 if (!empty($item['verb'])) {
295 $xmlhead = '<?xml version="1.0" encoding="UTF-8" ?>';
297 if ($this->activity->match($item['verb'], Activity::TAG)) {
298 $fields = ['author-id', 'author-link', 'author-name', 'author-network',
299 'verb', 'object-type', 'resource-id', 'body', 'plink'];
300 $obj = Post::selectFirst($fields, ['uri' => $item['parent-uri']]);
301 if (!DBA::isResult($obj)) {
302 $this->profiler->stopRecording();
308 'id' => $item['author-id'],
309 'network' => $item['author-network'],
310 'url' => $item['author-link'],
312 $author = '[url=' . Contact::magicLinkByContact($author_arr) . ']' . $item['author-name'] . '[/url]';
316 'id' => $obj['author-id'],
317 'network' => $obj['author-network'],
318 'url' => $obj['author-link'],
320 $objauthor = '[url=' . Contact::magicLinkByContact($author_arr) . ']' . $obj['author-name'] . '[/url]';
322 switch ($obj['verb']) {
324 switch ($obj['object-type']) {
325 case Activity\ObjectType::EVENT:
326 $post_type = $this->l10n->t('event');
329 $post_type = $this->l10n->t('status');
334 if ($obj['resource-id']) {
335 $post_type = $this->l10n->t('photo');
336 $m=[]; preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
337 $rr['plink'] = $m[1];
339 $post_type = $this->l10n->t('status');
341 // Let's break everthing ... ;-)
344 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
346 $parsedobj = XML::parseString($xmlhead . $item['object']);
348 $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
349 $item['body'] = $this->l10n->t('%1$s tagged %2$s\'s %3$s with %4$s', $author, $objauthor, $plink, $tag);
353 $this->profiler->stopRecording();
357 * Renders photo menu based on item
360 * @param string $formSecurityToken
363 public function photoMenu(array $item, string $formSecurityToken): string
365 $this->profiler->startRecording('rendering');
366 $sub_link = $contact_url = $pm_url = $status_link = '';
367 $photos_link = $posts_link = $block_link = $ignore_link = '';
369 if ($this->userSession->getLocalUserId() && $this->userSession->getLocalUserId() == $item['uid'] && $item['gravity'] == ItemModel::GRAVITY_PARENT && !$item['self'] && !$item['mention']) {
370 $sub_link = 'javascript:doFollowThread(' . $item['id'] . '); return false;';
375 'id' => $item['author-id'],
376 'network' => $item['author-network'],
377 'url' => $item['author-link'],
379 $profile_link = Contact::magicLinkByContact($author, $item['author-link']);
380 $sparkle = (strpos($profile_link, 'contact/redir/') === 0);
383 $pcid = $item['author-id'];
386 $condition = ['uid' => $this->userSession->getLocalUserId(), 'uri-id' => $item['author-uri-id']];
387 $contact = DBA::selectFirst('contact', ['id', 'network', 'rel'], $condition);
388 if (DBA::isResult($contact)) {
389 $cid = $contact['id'];
390 $network = $contact['network'];
391 $rel = $contact['rel'];
395 $status_link = $profile_link . '/status';
396 $photos_link = $profile_link . '/photos';
397 $profile_link = $profile_link . '/profile';
401 $contact_url = 'contact/' . $pcid;
402 $posts_link = $contact_url . '/posts';
403 $block_link = $item['self'] ? '' : $contact_url . '/block?t=' . $formSecurityToken;
404 $ignore_link = $item['self'] ? '' : $contact_url . '/ignore?t=' . $formSecurityToken;
405 $collapse_link = $item['self'] ? '' : $contact_url . '/collapse?t=' . $formSecurityToken;
408 if ($cid && !$item['self']) {
409 $contact_url = 'contact/' . $cid;
410 $posts_link = $contact_url . '/posts';
412 if (in_array($network, [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA])) {
413 $pm_url = 'message/new/' . $cid;
417 if ($this->userSession->getLocalUserId()) {
419 $this->l10n->t('Follow Thread') => $sub_link,
420 $this->l10n->t('View Status') => $status_link,
421 $this->l10n->t('View Profile') => $profile_link,
422 $this->l10n->t('View Photos') => $photos_link,
423 $this->l10n->t('Network Posts') => $posts_link,
424 $this->l10n->t('View Contact') => $contact_url,
425 $this->l10n->t('Send PM') => $pm_url,
426 $this->l10n->t('Block') => $block_link,
427 $this->l10n->t('Ignore') => $ignore_link,
428 $this->l10n->t('Collapse') => $collapse_link
431 if (!empty($item['language'])) {
432 $menu[$this->l10n->t('Languages')] = 'javascript:alert(\'' . ItemModel::getLanguageMessage($item) . '\');';
435 if ((($cid == 0) || ($rel == Contact::FOLLOWER)) &&
436 in_array($item['network'], Protocol::FEDERATED)) {
437 $menu[$this->l10n->t('Connect/Follow')] = 'contact/follow?url=' . urlencode($item['author-link']) . '&auto=1';
440 $menu = [$this->l10n->t('View Profile') => $item['author-link']];
443 $args = ['item' => $item, 'menu' => $menu];
445 Hook::callAll('item_photo_menu', $args);
447 $menu = $args['menu'];
450 foreach ($menu as $k => $v) {
451 if (strpos($v, 'javascript:') === 0) {
453 $o .= '<li role="menuitem"><a onclick="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
455 $o .= '<li role="menuitem"><a href="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
458 $this->profiler->stopRecording();
463 * Checks if the activity is visible to current user
465 * @param array $item Activity item
466 * @return bool Whether the item is visible to the user
468 public function isVisibleActivity(array $item): bool
470 // Empty verb or hidden?
471 if (empty($item['verb']) || $this->activity->isHidden($item['verb'])) {
476 return (!($this->activity->match($item['verb'], Activity::FOLLOW) &&
477 $item['object-type'] === Activity\ObjectType::NOTE &&
478 empty($item['self']) &&
479 $item['uid'] == $this->userSession->getLocalUserId())
483 public function expandTags(array $item, bool $setPermissions = false): array
485 // Look for any tags and linkify them
486 $item['inform'] = '';
487 $private_forum = false;
489 $only_to_forum = false;
493 // Convert mentions in the body to a unified format
494 $item['body'] = BBCode::setMentions($item['body'], $item['uid'], $item['network']);
496 // Search for forum mentions
497 foreach (Tag::getFromBody($item['body'], Tag::TAG_CHARACTER[Tag::MENTION] . Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION]) as $tag) {
498 $contact = Contact::getByURLForUser($tag[2], $item['uid']);
499 if (empty($contact)) {
503 $receivers[] = $contact['id'];
505 if (!empty($item['inform'])) {
506 $item['inform'] .= ',';
508 $item['inform'] .= 'cid:' . $contact['id'];
510 if (($item['gravity'] == ItemModel::GRAVITY_COMMENT) || empty($contact['cid']) || ($contact['contact-type'] != Contact::TYPE_COMMUNITY)) {
514 if (!empty($contact['prv']) || ($tag[1] == Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION])) {
515 $private_forum = $contact['prv'];
516 $only_to_forum = ($tag[1] == Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION]);
517 $private_id = $contact['id'];
518 $forum_contact = $contact;
519 Logger::info('Private forum or exclusive mention', ['url' => $tag[2], 'mention' => $tag[1]]);
520 } elseif ($item['allow_cid'] == '<' . $contact['id'] . '>') {
521 $private_forum = false;
522 $only_to_forum = true;
523 $private_id = $contact['id'];
524 $forum_contact = $contact;
525 Logger::info('Public forum', ['url' => $tag[2], 'mention' => $tag[1]]);
527 Logger::info('Post with forum mention will not be converted to a forum post', ['url' => $tag[2], 'mention' => $tag[1]]);
530 Logger::info('Got inform', ['inform' => $item['inform']]);
532 if (($item['gravity'] == ItemModel::GRAVITY_PARENT) && !empty($forum_contact) && ($private_forum || $only_to_forum)) {
533 // we tagged a forum in a top level post. Now we change the post
534 $item['private'] = $private_forum ? ItemModel::PRIVATE : ItemModel::UNLISTED;
536 if ($only_to_forum) {
537 $item['postopts'] = '';
540 $item['deny_cid'] = '';
541 $item['deny_gid'] = '';
543 if ($private_forum) {
544 $item['allow_cid'] = '<' . $private_id . '>';
545 $item['allow_gid'] = '<' . Group::getIdForForum($forum_contact['id']) . '>';
547 $item['allow_cid'] = '';
548 $item['allow_gid'] = '';
550 } elseif ($setPermissions && ($item['gravity'] == ItemModel::GRAVITY_PARENT)) {
551 if (empty($receivers)) {
552 // For security reasons direct posts without any receiver will be posts to yourself
553 $self = Contact::selectFirst(['id'], ['uid' => $item['uid'], 'self' => true]);
554 $receivers[] = $self['id'];
557 $item['private'] = ItemModel::PRIVATE;
558 $item['allow_cid'] = '';
559 $item['allow_gid'] = '';
560 $item['deny_cid'] = '';
561 $item['deny_gid'] = '';
563 foreach ($receivers as $receiver) {
564 $item['allow_cid'] .= '<' . $receiver . '>';
570 public function getAuthorAvatar(array $item): string
572 if (in_array($item['network'], [Protocol::FEED, Protocol::MAIL])) {
573 $author_avatar = $item['contact-id'];
574 $author_updated = '';
575 $author_thumb = $item['contact-avatar'];
577 $author_avatar = $item['author-id'];
578 $author_updated = $item['author-updated'];
579 $author_thumb = $item['author-avatar'];
583 if (empty($author_thumb) || Photo::isPhotoURI($author_thumb)) {
584 $author_thumb = Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated);
587 return $author_thumb;
590 public function getOwnerAvatar(array $item): string
592 if (in_array($item['network'], [Protocol::FEED, Protocol::MAIL])) {
593 $owner_avatar = $item['contact-id'];
595 $owner_thumb = $item['contact-avatar'];
597 $owner_avatar = $item['owner-id'];
598 $owner_updated = $item['owner-updated'];
599 $owner_thumb = $item['owner-avatar'];
602 if (empty($owner_thumb) || Photo::isPhotoURI($owner_thumb)) {
603 $owner_thumb = Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated);
610 * Add a share block for the given uri-id
613 * @param string $body
616 public function addSharedPost(array $item, string $body = ''): string
619 $body = $item['body'];
622 if (empty($item['quote-uri-id'])) {
626 $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network', 'quote-uri-id'];
627 $shared_item = Post::selectFirst($fields, ['uri-id' => $item['quote-uri-id'], 'uid' => [$item['uid'], 0], 'private' => [ItemModel::PUBLIC, ItemModel::UNLISTED]]);
628 if (!DBA::isResult($shared_item)) {
629 Logger::notice('Post does not exist.', ['uri-id' => $item['quote-uri-id'], 'uid' => $item['uid']]);
633 return trim(BBCode::removeSharedData($body) . "\n" . $this->createSharedBlockByArray($shared_item, true));
637 * Add a share block for the given guid
639 * @param string $guid
640 * @param integer $uid
641 * @param bool $add_media
644 private function createSharedPostByGuid(string $guid, bool $add_media): string
646 $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network'];
647 $shared_item = Post::selectFirst($fields, ['guid' => $guid, 'uid' => 0, 'private' => [ItemModel::PUBLIC, ItemModel::UNLISTED]]);
649 if (!DBA::isResult($shared_item)) {
650 Logger::notice('Post does not exist.', ['guid' => $guid]);
654 return $this->createSharedBlockByArray($shared_item, $add_media);
658 * Add a share block for the given item array
661 * @param bool $add_media
664 public function createSharedBlockByArray(array $item, bool $add_media = false): string
666 if ($item['network'] == Protocol::FEED) {
667 return PageInfo::getFooterFromUrl($item['plink']);
668 } elseif (!in_array($item['network'] ?? '', Protocol::FEDERATED)) {
674 $item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
677 $shared_content = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'], $item['plink'], $item['created'], $item['guid'], $item['uri']);
679 if (!empty($item['title'])) {
680 $shared_content .= '[h3]' . $item['title'] . "[/h3]\n";
683 $shared = $this->getShareArray($item);
685 // If it is a reshared post then reformat it to avoid display problems with two share elements
686 if (!empty($shared)) {
687 if (!empty($shared['guid']) && ($encaspulated_share = $this->createSharedPostByGuid($shared['guid'], true))) {
688 if (!empty(BBCode::fetchShareAttributes($item['body']))) {
689 $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encaspulated_share, $item['body']);
691 $item['body'] .= $encaspulated_share;
694 $item['body'] = HTML::toBBCode(BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::ACTIVITYPUB));
697 $shared_content .= $item['body'] . '[/share]';
699 return $shared_content;
703 * Return the shared post from an item array (if the item is shared item)
706 * @param array $fields
708 * @return array with the shared post
710 public function getSharedPost(array $item, array $fields = []): array
712 if (!empty($item['quote-uri-id'])) {
713 $shared = Post::selectFirst($fields, ['uri-id' => $item['quote-uri-id'], 'uid' => [0, $item['uid'] ?? 0]]);
714 if (is_array($shared)) {
716 'comment' => BBCode::removeSharedData($item['body'] ?? ''),
722 $attributes = BBCode::fetchShareAttributes($item['body'] ?? '');
723 if (!empty($attributes)) {
724 $shared = Post::selectFirst($fields, ['guid' => $attributes['guid'], 'uid' => [0, $item['uid'] ?? 0]]);
725 if (is_array($shared)) {
727 'comment' => $attributes['comment'],
737 * Return share data from an item array (if the item is shared item)
738 * We are providing the complete Item array, because at some time in the future
739 * we hopefully will define these values not in the body anymore but in some item fields.
740 * This function is meant to replace all similar functions in the system.
744 * @return array with share information
746 private function getShareArray(array $item): array
748 $attributes = BBCode::fetchShareAttributes($item['body'] ?? '');
749 if (!empty($attributes)) {
753 if (!empty($item['quote-uri-id'])) {
754 $shared = Post::selectFirst(['author-name', 'author-link', 'author-avatar', 'plink', 'created', 'guid', 'uri', 'body'], ['uri-id' => $item['quote-uri-id']]);
755 if (!empty($shared)) {
757 'author' => $shared['author-name'],
758 'profile' => $shared['author-link'],
759 'avatar' => $shared['author-avatar'],
760 'link' => $shared['plink'],
761 'posted' => $shared['created'],
762 'guid' => $shared['guid'],
763 'message_id' => $shared['uri'],
764 'comment' => $item['body'],
765 'shared' => $shared['body'],
774 * Add a link to a shared post at the end of the post
776 * @param string $body
777 * @param integer $quote_uri_id
780 public function addShareLink(string $body, int $quote_uri_id): string
782 $post = Post::selectFirstPost(['uri', 'plink'], ['uri-id' => $quote_uri_id]);
787 $body = BBCode::removeSharedData($body);
789 $body .= "\n♲ " . ($post['plink'] ?: $post['uri']);
794 public function storeAttachmentFromRequest(array $request): string
796 $attachment_type = $request['attachment_type'] ?? '';
797 $attachment_title = $request['attachment_title'] ?? '';
798 $attachment_text = $request['attachment_text'] ?? '';
800 $attachment_url = hex2bin($request['attachment_url'] ?? '');
801 $attachment_img_src = hex2bin($request['attachment_img_src'] ?? '');
803 $attachment_img_width = $request['attachment_img_width'] ?? 0;
804 $attachment_img_height = $request['attachment_img_height'] ?? 0;
806 // Fetch the basic attachment data
807 $attachment = ParseUrl::getSiteinfoCached($attachment_url);
808 unset($attachment['keywords']);
810 // Overwrite the basic data with possible changes from the frontend
811 $attachment['type'] = $attachment_type;
812 $attachment['title'] = $attachment_title;
813 $attachment['text'] = $attachment_text;
814 $attachment['url'] = $attachment_url;
816 if (!empty($attachment_img_src)) {
817 $attachment['images'] = [
819 'src' => $attachment_img_src,
820 'width' => $attachment_img_width,
821 'height' => $attachment_img_height
825 unset($attachment['images']);
828 return "\n" . PageInfo::getFooterFromData($attachment);
831 public function addCategories(array $post, string $category): array
833 if (!empty($post['file'])) {
834 // get the "fileas" tags for this post
835 $filedas = FileTag::fileToArray($post['file']);
838 $list_array = explode(',', trim($category));
839 $post['file'] = FileTag::arrayToFile($list_array, 'category');
841 if (!empty($filedas) && is_array($filedas)) {
842 // append the fileas stuff to the new categories list
843 $post['file'] .= FileTag::arrayToFile($filedas);
848 public function getACL(array $post, array $toplevel_item, array $request): array
850 // If this is a comment, set the permissions from the parent.
851 if ($toplevel_item) {
852 $post['allow_cid'] = $toplevel_item['allow_cid'] ?? '';
853 $post['allow_gid'] = $toplevel_item['allow_gid'] ?? '';
854 $post['deny_cid'] = $toplevel_item['deny_cid'] ?? '';
855 $post['deny_gid'] = $toplevel_item['deny_gid'] ?? '';
856 $post['private'] = $toplevel_item['private'];
860 $user = User::getById($post['uid'], ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);
862 throw new HTTPException\NotFoundException($this->l10n->t('Unable to fetch user.'));
865 $post['allow_cid'] = isset($request['contact_allow']) ? $this->aclFormatter->toString($request['contact_allow']) : $user['allow_cid'] ?? '';
866 $post['allow_gid'] = isset($request['group_allow']) ? $this->aclFormatter->toString($request['group_allow']) : $user['allow_gid'] ?? '';
867 $post['deny_cid'] = isset($request['contact_deny']) ? $this->aclFormatter->toString($request['contact_deny']) : $user['deny_cid'] ?? '';
868 $post['deny_gid'] = isset($request['group_deny']) ? $this->aclFormatter->toString($request['group_deny']) : $user['deny_gid'] ?? '';
870 $visibility = $request['visibility'] ?? '';
871 if ($visibility === 'public') {
872 // The ACL selector introduced in version 2019.12 sends ACL input data even when the Public visibility is selected
873 $post['allow_cid'] = $post['allow_gid'] = $post['deny_cid'] = $post['deny_gid'] = '';
874 } else if ($visibility === 'custom') {
875 // Since we know from the visibility parameter the item should be private, we have to prevent the empty ACL
876 // case that would make it public. So we always append the author's contact id to the allowed contacts.
877 // See https://github.com/friendica/friendica/issues/9672
878 $post['allow_cid'] .= $this->aclFormatter->toString(Contact::getPublicIdByUserId($post['uid']));
881 if ($post['allow_gid'] || $post['allow_cid'] || $post['deny_gid'] || $post['deny_cid']) {
882 $post['private'] = ItemModel::PRIVATE;
883 } elseif ($this->pConfig->get($post['uid'], 'system', 'unlisted')) {
884 $post['private'] = ItemModel::UNLISTED;
886 $post['private'] = ItemModel::PUBLIC;
892 public function moveAttachmentsFromBodyToAttach(array $post): array
894 if (!preg_match_all('/(\[attachment\]([0-9]+)\[\/attachment\])/',$post['body'], $match)) {
898 foreach ($match[2] as $attachment_id) {
899 $attachment = Attach::selectFirst(['id', 'uid', 'filename', 'filesize', 'filetype'], ['id' => $attachment_id, 'uid' => $post['uid']]);
900 if (empty($attachment)) {
903 if ($post['attach']) {
904 $post['attach'] .= ',';
906 $post['attach'] .= Post\Media::getAttachElement($this->baseURL->get() . '/attach/' . $attachment['id'],
907 $attachment['filesize'], $attachment['filetype'], $attachment['filename'] ?? '');
909 $fields = ['allow_cid' => $post['allow_cid'], 'allow_gid' => $post['allow_gid'],
910 'deny_cid' => $post['deny_cid'], 'deny_gid' => $post['deny_gid']];
911 $condition = ['id' => $attachment_id];
912 Attach::update($fields, $condition);
915 $post['body'] = str_replace($match[1], '', $post['body']);
920 private function setObjectType(array $post): array
922 if (empty($post['post-type'])) {
923 $post['post-type'] = empty($post['title']) ? ItemModel::PT_NOTE : ItemModel::PT_ARTICLE;
926 // embedded bookmark or attachment in post? set bookmark flag
927 $data = BBCode::getAttachmentData($post['body']);
928 if ((preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $post['body'], $match, PREG_SET_ORDER) || isset($data['type']))
929 && ($post['post-type'] != ItemModel::PT_PERSONAL_NOTE)) {
930 $post['post-type'] = ItemModel::PT_PAGE;
931 $post['object-type'] = Activity\ObjectType::BOOKMARK;
934 // Setting the object type if not defined before
935 if (empty($post['object-type'])) {
936 $post['object-type'] = ($post['gravity'] == ItemModel::GRAVITY_PARENT) ? Activity\ObjectType::NOTE : Activity\ObjectType::COMMENT;
938 $objectdata = BBCode::getAttachedData($post['body']);
940 if ($objectdata['type'] == 'link') {
941 $post['object-type'] = Activity\ObjectType::BOOKMARK;
942 } elseif ($objectdata['type'] == 'video') {
943 $post['object-type'] = Activity\ObjectType::VIDEO;
944 } elseif ($objectdata['type'] == 'photo') {
945 $post['object-type'] = Activity\ObjectType::IMAGE;
951 public function initializePost(array $post): array
953 $post['network'] = Protocol::DFRN;
954 $post['protocol'] = Conversation::PARCEL_DIRECT;
955 $post['direction'] = Conversation::PUSH;
956 $post['received'] = DateTimeFormat::utcNow();
957 $post['origin'] = true;
958 $post['wall'] = $post['wall'] ?? true;
959 $post['guid'] = $post['guid'] ?? System::createUUID();
960 $post['verb'] = $post['verb'] ?? Activity::POST;
961 $post['uri'] = $post['uri'] ?? ItemModel::newURI($post['guid']);
962 $post['thr-parent'] = $post['thr-parent'] ?? $post['uri'];
964 if (empty($post['gravity'])) {
965 $post['gravity'] = ($post['uri'] == $post['thr-parent']) ? ItemModel::GRAVITY_PARENT : ItemModel::GRAVITY_COMMENT;
968 $owner = User::getOwnerDataById($post['uid']);
970 if (!isset($post['allow_cid']) || !isset($post['allow_gid']) || !isset($post['deny_cid']) || !isset($post['deny_gid'])) {
971 $post['allow_cid'] = $owner['allow_cid'];
972 $post['allow_gid'] = $owner['allow_gid'];
973 $post['deny_cid'] = $owner['deny_cid'];
974 $post['deny_gid'] = $owner['deny_gid'];
977 if ($post['allow_gid'] || $post['allow_cid'] || $post['deny_gid'] || $post['deny_cid']) {
978 $post['private'] = ItemModel::PRIVATE;
979 } elseif ($this->pConfig->get($post['uid'], 'system', 'unlisted')) {
980 $post['private'] = ItemModel::UNLISTED;
982 $post['private'] = ItemModel::PUBLIC;
985 if (empty($post['contact-id'])) {
986 $post['contact-id'] = $owner['id'];
989 if (empty($post['author-link']) && empty($post['author-id'])) {
990 $post['author-link'] = $owner['url'];
991 $post['author-id'] = Contact::getIdForURL($post['author-link']);
992 $post['author-name'] = $owner['name'];
993 $post['author-avatar'] = $owner['thumb'];
996 if (empty($post['owner-link']) && empty($post['owner-id'])) {
997 $post['owner-link'] = $post['author-link'];
998 $post['owner-id'] = Contact::getIdForURL($post['owner-link']);
999 $post['owner-name'] = $post['author-name'];
1000 $post['owner-avatar'] = $post['author-avatar'];
1006 public function finalizePost(array $post): array
1008 if (preg_match("/\[attachment\](.*?)\[\/attachment\]/ism", $post['body'], $matches)) {
1009 $post['body'] = preg_replace("/\[attachment].*?\[\/attachment\]/ism", PageInfo::getFooterFromUrl($matches[1]), $post['body']);
1012 // Convert links with empty descriptions to links without an explicit description
1013 $post['body'] = trim(preg_replace('#\[url=([^\]]*?)\]\[/url\]#ism', '[url]$1[/url]', $post['body']));
1014 $post['body'] = $this->bbCodeVideo->transform($post['body']);
1015 $post = $this->setObjectType($post);
1017 // Personal notes must never be altered to a forum post.
1018 if ($post['post-type'] != ItemModel::PT_PERSONAL_NOTE) {
1019 // Look for any tags and linkify them
1020 $post = $this->expandTags($post);
1026 public function postProcessPost(array $post, array $recipients = [])
1028 if (!\Friendica\Content\Feature::isEnabled($post['uid'], 'explicit_mentions') && ($post['gravity'] == ItemModel::GRAVITY_COMMENT)) {
1029 Tag::createImplicitMentions($post['uri-id'], $post['thr-parent-id']);
1032 Hook::callAll('post_local_end', $post);
1034 $author = DBA::selectFirst('contact', ['thumb'], ['uid' => $post['uid'], 'self' => true]);
1036 foreach ($recipients as $recipient) {
1037 $address = trim($recipient);
1042 $this->emailer->send(new ItemCCEMail($this->app, $this->l10n, $this->baseURL,
1043 $post, $address, $author['thumb'] ?? ''));