3 * @copyright Copyright (C) 2010-2022, 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;
24 use Friendica\Core\Hook;
25 use Friendica\Core\L10n;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Session;
28 use Friendica\Database\DBA;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Item as ModelItem;
31 use Friendica\Model\Tag;
32 use Friendica\Model\Post;
33 use Friendica\Protocol\Activity;
34 use Friendica\Util\Profiler;
35 use Friendica\Util\Strings;
36 use Friendica\Util\XML;
39 * A content helper class for displaying items
50 public function __construct(Profiler $profiler, Activity $activity, L10n $l10n)
52 $this->profiler = $profiler;
53 $this->activity = $activity;
58 * Return array with details for categories and folders for an item
62 * @return [array, array]
65 * [ // categories array
67 * 'name': 'category name',
68 * 'removeurl': 'url to remove this category',
69 * 'first': 'is the first in this array? true/false',
70 * 'last': 'is the last in this array? true/false',
76 * 'name': 'folder name',
77 * 'removeurl': 'url to remove this folder',
78 * 'first': 'is the first in this array? true/false',
79 * 'last': 'is the last in this array? true/false',
85 public function determineCategoriesTerms(array $item, int $uid = 0)
91 $uid = $item['uid'] ?: $uid;
93 foreach (Post\Category::getArrayByURIId($item['uri-id'], $uid, Post\Category::CATEGORY) as $savedFolderName) {
94 if (!empty($item['author-link'])) {
95 $url = $item['author-link'] . "?category=" . rawurlencode($savedFolderName);
100 'name' => $savedFolderName,
102 'removeurl' => local_user() == $uid ? 'filerm/' . $item['id'] . '?cat=' . rawurlencode($savedFolderName) : '',
109 if (count($categories)) {
110 $categories[count($categories) - 1]['last'] = true;
113 if (local_user() == $uid) {
114 foreach (Post\Category::getArrayByURIId($item['uri-id'], $uid, Post\Category::FILE) as $savedFolderName) {
116 'name' => $savedFolderName,
118 'removeurl' => local_user() == $uid ? 'filerm/' . $item['id'] . '?term=' . rawurlencode($savedFolderName) : '',
126 if (count($folders)) {
127 $folders[count($folders) - 1]['last'] = true;
130 return [$categories, $folders];
134 * This function removes the tag $tag from the text $body and replaces it with
135 * the appropriate link.
137 * @param string $body the text to replace the tag in
138 * @param integer $profile_uid the user id to replace the tag for (0 = anyone)
139 * @param string $tag the tag to replace
140 * @param string $network The network of the post
142 * @return array|bool ['replaced' => $replaced, 'contact' => $contact];
143 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
144 * @throws \ImagickException
146 public static function replaceTag(&$body, $profile_uid, $tag, $network = '')
150 //is it a person tag?
151 if (Tag::isType($tag, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION)) {
152 $tag_type = substr($tag, 0, 1);
153 //is it already replaced?
154 if (strpos($tag, '[url=')) {
158 //get the person's name
159 $name = substr($tag, 1);
161 // Sometimes the tag detection doesn't seem to work right
162 // This is some workaround
163 $nameparts = explode(' ', $name);
164 $name = $nameparts[0];
166 // Try to detect the contact in various ways
167 if (strpos($name, 'http://') || strpos($name, '@')) {
168 $contact = Contact::getByURLForUser($name, $profile_uid);
171 $fields = ['id', 'url', 'nick', 'name', 'alias', 'network', 'forum', 'prv'];
173 if (strrpos($name, '+')) {
174 // Is it in format @nick+number?
175 $tagcid = intval(substr($name, strrpos($name, '+') + 1));
176 $contact = DBA::selectFirst('contact', $fields, ['id' => $tagcid, 'uid' => $profile_uid]);
179 // select someone by nick in the current network
180 if (!DBA::isResult($contact) && ($network != '')) {
181 $condition = ["`nick` = ? AND `network` = ? AND `uid` = ?",
182 $name, $network, $profile_uid];
183 $contact = DBA::selectFirst('contact', $fields, $condition);
186 // select someone by attag in the current network
187 if (!DBA::isResult($contact) && ($network != '')) {
188 $condition = ["`attag` = ? AND `network` = ? AND `uid` = ?",
189 $name, $network, $profile_uid];
190 $contact = DBA::selectFirst('contact', $fields, $condition);
193 //select someone by name in the current network
194 if (!DBA::isResult($contact) && ($network != '')) {
195 $condition = ['name' => $name, 'network' => $network, 'uid' => $profile_uid];
196 $contact = DBA::selectFirst('contact', $fields, $condition);
199 // select someone by nick in any network
200 if (!DBA::isResult($contact)) {
201 $condition = ["`nick` = ? AND `uid` = ?", $name, $profile_uid];
202 $contact = DBA::selectFirst('contact', $fields, $condition);
205 // select someone by attag in any network
206 if (!DBA::isResult($contact)) {
207 $condition = ["`attag` = ? AND `uid` = ?", $name, $profile_uid];
208 $contact = DBA::selectFirst('contact', $fields, $condition);
211 // select someone by name in any network
212 if (!DBA::isResult($contact)) {
213 $condition = ['name' => $name, 'uid' => $profile_uid];
214 $contact = DBA::selectFirst('contact', $fields, $condition);
218 // Check if $contact has been successfully loaded
219 if (DBA::isResult($contact)) {
220 $profile = $contact['url'];
221 $newname = ($contact['name'] ?? '') ?: $contact['nick'];
224 //if there is an url for this persons profile
225 if (isset($profile) && ($newname != '')) {
227 // create profile link
228 $profile = str_replace(',', '%2c', $profile);
229 $newtag = $tag_type.'[url=' . $profile . ']' . $newname . '[/url]';
230 $body = str_replace($tag_type . $name, $newtag, $body);
234 return ['replaced' => $replaced, 'contact' => $contact];
238 * Render actions localized
241 * @throws ImagickException
242 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
244 public function localize(&$item)
246 $this->profiler->startRecording('rendering');
247 /// @todo The following functionality needs to be cleaned up.
248 if (!empty($item['verb'])) {
249 $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
251 if (stristr($item['verb'], Activity::POKE)) {
252 $verb = urldecode(substr($item['verb'], strpos($item['verb'],'#') + 1));
254 $this->profiler->stopRecording();
257 if ($item['object-type'] == "" || $item['object-type'] !== Activity\ObjectType::PERSON) {
258 $this->profiler->stopRecording();
262 $obj = XML::parseString($xmlhead . $item['object']);
264 $Bname = $obj->title;
268 foreach ($obj->link as $l) {
269 $atts = $l->attributes();
270 switch ($atts['rel']) {
271 case "alternate": $Blink = $atts['href'];
272 case "photo": $Bphoto = $atts['href'];
276 $author = ['uid' => 0, 'id' => $item['author-id'],
277 'network' => $item['author-network'], 'url' => $item['author-link']];
278 $A = '[url=' . Contact::magicLinkByContact($author) . ']' . $item['author-name'] . '[/url]';
280 if (!empty($Blink)) {
281 $B = '[url=' . Contact::magicLink($Blink) . ']' . $Bname . '[/url]';
286 if ($Bphoto != "" && !empty($Blink)) {
287 $Bphoto = '[url=' . Contact::magicLink($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
291 * we can't have a translation string with three positions but no distinguishable text
292 * So here is the translate string.
294 $txt = $this->l10n->t('%1$s poked %2$s');
296 // now translate the verb
297 $poked_t = trim(sprintf($txt, '', ''));
298 $txt = str_replace($poked_t, $this->l10n->t($verb), $txt);
300 // then do the sprintf on the translation string
302 $item['body'] = sprintf($txt, $A, $B) . "\n\n\n" . $Bphoto;
306 if ($this->activity->match($item['verb'], Activity::TAG)) {
307 $fields = ['author-id', 'author-link', 'author-name', 'author-network',
308 'verb', 'object-type', 'resource-id', 'body', 'plink'];
309 $obj = Post::selectFirst($fields, ['uri' => $item['parent-uri']]);
310 if (!DBA::isResult($obj)) {
311 $this->profiler->stopRecording();
315 $author_arr = ['uid' => 0, 'id' => $item['author-id'],
316 'network' => $item['author-network'], 'url' => $item['author-link']];
317 $author = '[url=' . Contact::magicLinkByContact($author_arr) . ']' . $item['author-name'] . '[/url]';
319 $author_arr = ['uid' => 0, 'id' => $obj['author-id'],
320 'network' => $obj['author-network'], 'url' => $obj['author-link']];
321 $objauthor = '[url=' . Contact::magicLinkByContact($author_arr) . ']' . $obj['author-name'] . '[/url]';
323 switch ($obj['verb']) {
325 switch ($obj['object-type']) {
326 case Activity\ObjectType::EVENT:
327 $post_type = $this->l10n->t('event');
330 $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);
354 if (preg_match_all('/@\[url=(.*?)\]/is', $item['body'], $matches, PREG_SET_ORDER)) {
355 foreach ($matches as $mtch) {
356 if (!strpos($mtch[1], 'zrl=')) {
357 $item['body'] = str_replace($mtch[0], '@[url=' . Contact::magicLink($mtch[1]) . ']', $item['body']);
362 // add sparkle links to appropriate permalinks
363 // Only create a redirection to a magic link when logged in
364 if (!empty($item['plink']) && Session::isAuthenticated() && $item['private'] == ModelItem::PRIVATE) {
365 $author = ['uid' => 0, 'id' => $item['author-id'],
366 'network' => $item['author-network'], 'url' => $item['author-link']];
367 $item['plink'] = Contact::magicLinkByContact($author, $item['plink']);
369 $this->profiler->stopRecording();
372 public function photoMenu($item, string $formSecurityToken)
374 $this->profiler->startRecording('rendering');
385 if (local_user() && local_user() == $item['uid'] && $item['gravity'] == GRAVITY_PARENT && !$item['self'] && !$item['mention']) {
386 $sub_link = 'javascript:doFollowThread(' . $item['id'] . '); return false;';
389 $author = ['uid' => 0, 'id' => $item['author-id'],
390 'network' => $item['author-network'], 'url' => $item['author-link']];
391 $profile_link = Contact::magicLinkByContact($author, $item['author-link']);
392 $sparkle = (strpos($profile_link, 'redir/') === 0);
395 $pcid = $item['author-id'];
398 $condition = ['uid' => local_user(), 'nurl' => Strings::normaliseLink($item['author-link'])];
399 $contact = DBA::selectFirst('contact', ['id', 'network', 'rel'], $condition);
400 if (DBA::isResult($contact)) {
401 $cid = $contact['id'];
402 $network = $contact['network'];
403 $rel = $contact['rel'];
407 $status_link = $profile_link . '/status';
408 $photos_link = str_replace('/profile/', '/photos/', $profile_link);
409 $profile_link = $profile_link . '/profile';
413 $contact_url = 'contact/' . $pcid;
414 $posts_link = $contact_url . '/posts';
415 $block_link = $item['self'] ? '' : $contact_url . '/block?t=' . $formSecurityToken;
416 $ignore_link = $item['self'] ? '' : $contact_url . '/ignore?t=' . $formSecurityToken;
419 if ($cid && !$item['self']) {
420 $contact_url = 'contact/' . $cid;
421 $poke_link = $contact_url . '/poke';
422 $posts_link = $contact_url . '/posts';
424 if (in_array($network, [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA])) {
425 $pm_url = 'message/new/' . $cid;
431 $this->l10n->t('Follow Thread') => $sub_link,
432 $this->l10n->t('View Status') => $status_link,
433 $this->l10n->t('View Profile') => $profile_link,
434 $this->l10n->t('View Photos') => $photos_link,
435 $this->l10n->t('Network Posts') => $posts_link,
436 $this->l10n->t('View Contact') => $contact_url,
437 $this->l10n->t('Send PM') => $pm_url,
438 $this->l10n->t('Block') => $block_link,
439 $this->l10n->t('Ignore') => $ignore_link
442 if (!empty($item['language'])) {
443 $menu[$this->l10n->t('Languages')] = 'javascript:alert(\'' . ModelItem::getLanguageMessage($item) . '\');';
446 if ($network == Protocol::DFRN) {
447 $menu[$this->l10n->t("Poke")] = $poke_link;
450 if ((($cid == 0) || ($rel == Contact::FOLLOWER)) &&
451 in_array($item['network'], Protocol::FEDERATED)) {
452 $menu[$this->l10n->t('Connect/Follow')] = 'follow?url=' . urlencode($item['author-link']) . '&auto=1';
455 $menu = [$this->l10n->t('View Profile') => $item['author-link']];
458 $args = ['item' => $item, 'menu' => $menu];
460 Hook::callAll('item_photo_menu', $args);
462 $menu = $args['menu'];
465 foreach ($menu as $k => $v) {
466 if (strpos($v, 'javascript:') === 0) {
468 $o .= '<li role="menuitem"><a onclick="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
470 $o .= '<li role="menuitem"><a href="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
473 $this->profiler->stopRecording();
477 public function visibleActivity($item) {
479 if (empty($item['verb']) || $this->activity->isHidden($item['verb'])) {
483 // @TODO below if() block can be rewritten to a single line: $isVisible = allConditionsHere;
484 if ($this->activity->match($item['verb'], Activity::FOLLOW) &&
485 $item['object-type'] === Activity\ObjectType::NOTE &&
486 empty($item['self']) &&
487 $item['uid'] == local_user()) {