3 * @copyright Copyright (C) 2010-2021, 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 string $inform a comma-seperated string containing everybody to inform
139 * @param integer $profile_uid the user id to replace the tag for (0 = anyone)
140 * @param string $tag the tag to replace
141 * @param string $network The network of the post
143 * @return array|bool ['replaced' => $replaced, 'contact' => $contact];
144 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
145 * @throws \ImagickException
147 public static function replaceTag(&$body, &$inform, $profile_uid, $tag, $network = '')
151 //is it a person tag?
152 if (Tag::isType($tag, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION)) {
153 $tag_type = substr($tag, 0, 1);
154 //is it already replaced?
155 if (strpos($tag, '[url=')) {
159 //get the person's name
160 $name = substr($tag, 1);
162 // Sometimes the tag detection doesn't seem to work right
163 // This is some workaround
164 $nameparts = explode(' ', $name);
165 $name = $nameparts[0];
167 // Try to detect the contact in various ways
168 if (strpos($name, 'http://') || strpos($name, '@')) {
169 $contact = Contact::getByURLForUser($name, $profile_uid);
172 $fields = ['id', 'url', 'nick', 'name', 'alias', 'network', 'forum', 'prv'];
174 if (strrpos($name, '+')) {
175 // Is it in format @nick+number?
176 $tagcid = intval(substr($name, strrpos($name, '+') + 1));
177 $contact = DBA::selectFirst('contact', $fields, ['id' => $tagcid, 'uid' => $profile_uid]);
180 // select someone by nick in the current network
181 if (!DBA::isResult($contact) && ($network != '')) {
182 $condition = ["`nick` = ? AND `network` = ? AND `uid` = ?",
183 $name, $network, $profile_uid];
184 $contact = DBA::selectFirst('contact', $fields, $condition);
187 // select someone by attag in the current network
188 if (!DBA::isResult($contact) && ($network != '')) {
189 $condition = ["`attag` = ? AND `network` = ? AND `uid` = ?",
190 $name, $network, $profile_uid];
191 $contact = DBA::selectFirst('contact', $fields, $condition);
194 //select someone by name in the current network
195 if (!DBA::isResult($contact) && ($network != '')) {
196 $condition = ['name' => $name, 'network' => $network, 'uid' => $profile_uid];
197 $contact = DBA::selectFirst('contact', $fields, $condition);
200 // select someone by nick in any network
201 if (!DBA::isResult($contact)) {
202 $condition = ["`nick` = ? AND `uid` = ?", $name, $profile_uid];
203 $contact = DBA::selectFirst('contact', $fields, $condition);
206 // select someone by attag in any network
207 if (!DBA::isResult($contact)) {
208 $condition = ["`attag` = ? AND `uid` = ?", $name, $profile_uid];
209 $contact = DBA::selectFirst('contact', $fields, $condition);
212 // select someone by name in any network
213 if (!DBA::isResult($contact)) {
214 $condition = ['name' => $name, 'uid' => $profile_uid];
215 $contact = DBA::selectFirst('contact', $fields, $condition);
219 // Check if $contact has been successfully loaded
220 if (DBA::isResult($contact)) {
221 if (strlen($inform) && (isset($contact['notify']) || isset($contact['id']))) {
225 if (isset($contact['id'])) {
226 $inform .= 'cid:' . $contact['id'];
227 } elseif (isset($contact['notify'])) {
228 $inform .= $contact['notify'];
231 $profile = $contact['url'];
232 $newname = ($contact['name'] ?? '') ?: $contact['nick'];
235 //if there is an url for this persons profile
236 if (isset($profile) && ($newname != '')) {
238 // create profile link
239 $profile = str_replace(',', '%2c', $profile);
240 $newtag = $tag_type.'[url=' . $profile . ']' . $newname . '[/url]';
241 $body = str_replace($tag_type . $name, $newtag, $body);
245 return ['replaced' => $replaced, 'contact' => $contact];
249 * Render actions localized
252 * @throws ImagickException
253 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
255 public function localize(&$item)
257 $this->profiler->startRecording('rendering');
258 /// @todo The following functionality needs to be cleaned up.
259 if (!empty($item['verb'])) {
260 $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
262 if (stristr($item['verb'], Activity::POKE)) {
263 $verb = urldecode(substr($item['verb'], strpos($item['verb'],'#') + 1));
265 $this->profiler->stopRecording();
268 if ($item['object-type'] == "" || $item['object-type'] !== Activity\ObjectType::PERSON) {
269 $this->profiler->stopRecording();
273 $obj = XML::parseString($xmlhead . $item['object']);
275 $Bname = $obj->title;
279 foreach ($obj->link as $l) {
280 $atts = $l->attributes();
281 switch ($atts['rel']) {
282 case "alternate": $Blink = $atts['href'];
283 case "photo": $Bphoto = $atts['href'];
287 $author = ['uid' => 0, 'id' => $item['author-id'],
288 'network' => $item['author-network'], 'url' => $item['author-link']];
289 $A = '[url=' . Contact::magicLinkByContact($author) . ']' . $item['author-name'] . '[/url]';
291 if (!empty($Blink)) {
292 $B = '[url=' . Contact::magicLink($Blink) . ']' . $Bname . '[/url]';
297 if ($Bphoto != "" && !empty($Blink)) {
298 $Bphoto = '[url=' . Contact::magicLink($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
302 * we can't have a translation string with three positions but no distinguishable text
303 * So here is the translate string.
305 $txt = $this->l10n->t('%1$s poked %2$s');
307 // now translate the verb
308 $poked_t = trim(sprintf($txt, '', ''));
309 $txt = str_replace($poked_t, $this->l10n->t($verb), $txt);
311 // then do the sprintf on the translation string
313 $item['body'] = sprintf($txt, $A, $B) . "\n\n\n" . $Bphoto;
317 if ($this->activity->match($item['verb'], Activity::TAG)) {
318 $fields = ['author-id', 'author-link', 'author-name', 'author-network',
319 'verb', 'object-type', 'resource-id', 'body', 'plink'];
320 $obj = Post::selectFirst($fields, ['uri' => $item['parent-uri']]);
321 if (!DBA::isResult($obj)) {
322 $this->profiler->stopRecording();
326 $author_arr = ['uid' => 0, 'id' => $item['author-id'],
327 'network' => $item['author-network'], 'url' => $item['author-link']];
328 $author = '[url=' . Contact::magicLinkByContact($author_arr) . ']' . $item['author-name'] . '[/url]';
330 $author_arr = ['uid' => 0, 'id' => $obj['author-id'],
331 'network' => $obj['author-network'], 'url' => $obj['author-link']];
332 $objauthor = '[url=' . Contact::magicLinkByContact($author_arr) . ']' . $obj['author-name'] . '[/url]';
334 switch ($obj['verb']) {
336 switch ($obj['object-type']) {
337 case Activity\ObjectType::EVENT:
338 $post_type = $this->l10n->t('event');
341 $post_type = $this->l10n->t('status');
345 if ($obj['resource-id']) {
346 $post_type = $this->l10n->t('photo');
347 $m=[]; preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
348 $rr['plink'] = $m[1];
350 $post_type = $this->l10n->t('status');
352 // Let's break everthing ... ;-)
355 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
357 $parsedobj = XML::parseString($xmlhead . $item['object']);
359 $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
360 $item['body'] = $this->l10n->t('%1$s tagged %2$s\'s %3$s with %4$s', $author, $objauthor, $plink, $tag);
365 if (preg_match_all('/@\[url=(.*?)\]/is', $item['body'], $matches, PREG_SET_ORDER)) {
366 foreach ($matches as $mtch) {
367 if (!strpos($mtch[1], 'zrl=')) {
368 $item['body'] = str_replace($mtch[0], '@[url=' . Contact::magicLink($mtch[1]) . ']', $item['body']);
373 // add sparkle links to appropriate permalinks
374 // Only create a redirection to a magic link when logged in
375 if (!empty($item['plink']) && Session::isAuthenticated() && $item['private'] == ModelItem::PRIVATE) {
376 $author = ['uid' => 0, 'id' => $item['author-id'],
377 'network' => $item['author-network'], 'url' => $item['author-link']];
378 $item['plink'] = Contact::magicLinkByContact($author, $item['plink']);
380 $this->profiler->stopRecording();
383 public function photoMenu($item, string $formSecurityToken)
385 $this->profiler->startRecording('rendering');
396 if (local_user() && local_user() == $item['uid'] && $item['gravity'] == GRAVITY_PARENT && !$item['self'] && !$item['mention']) {
397 $sub_link = 'javascript:doFollowThread(' . $item['id'] . '); return false;';
400 $author = ['uid' => 0, 'id' => $item['author-id'],
401 'network' => $item['author-network'], 'url' => $item['author-link']];
402 $profile_link = Contact::magicLinkByContact($author, $item['author-link']);
403 $sparkle = (strpos($profile_link, 'redir/') === 0);
406 $pcid = $item['author-id'];
409 $condition = ['uid' => local_user(), 'nurl' => Strings::normaliseLink($item['author-link'])];
410 $contact = DBA::selectFirst('contact', ['id', 'network', 'rel'], $condition);
411 if (DBA::isResult($contact)) {
412 $cid = $contact['id'];
413 $network = $contact['network'];
414 $rel = $contact['rel'];
418 $status_link = $profile_link . '/status';
419 $photos_link = str_replace('/profile/', '/photos/', $profile_link);
420 $profile_link = $profile_link . '/profile';
424 $contact_url = 'contact/' . $pcid;
425 $posts_link = $contact_url . '/posts';
426 $block_link = $item['self'] ? '' : $contact_url . '/block?t=' . $formSecurityToken;
427 $ignore_link = $item['self'] ? '' : $contact_url . '/ignore?t=' . $formSecurityToken;
430 if ($cid && !$item['self']) {
431 $contact_url = 'contact/' . $cid;
432 $poke_link = $contact_url . '/poke';
433 $posts_link = $contact_url . '/posts';
435 if (in_array($network, [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA])) {
436 $pm_url = 'message/new/' . $cid;
442 $this->l10n->t('Follow Thread') => $sub_link,
443 $this->l10n->t('View Status') => $status_link,
444 $this->l10n->t('View Profile') => $profile_link,
445 $this->l10n->t('View Photos') => $photos_link,
446 $this->l10n->t('Network Posts') => $posts_link,
447 $this->l10n->t('View Contact') => $contact_url,
448 $this->l10n->t('Send PM') => $pm_url,
449 $this->l10n->t('Block') => $block_link,
450 $this->l10n->t('Ignore') => $ignore_link
453 if (!empty($item['language'])) {
454 $menu[$this->l10n->t('Languages')] = 'javascript:alert(\'' . ModelItem::getLanguageMessage($item) . '\');';
457 if ($network == Protocol::DFRN) {
458 $menu[$this->l10n->t("Poke")] = $poke_link;
461 if ((($cid == 0) || ($rel == Contact::FOLLOWER)) &&
462 in_array($item['network'], Protocol::FEDERATED)) {
463 $menu[$this->l10n->t('Connect/Follow')] = 'follow?url=' . urlencode($item['author-link']) . '&auto=1';
466 $menu = [$this->l10n->t('View Profile') => $item['author-link']];
469 $args = ['item' => $item, 'menu' => $menu];
471 Hook::callAll('item_photo_menu', $args);
473 $menu = $args['menu'];
476 foreach ($menu as $k => $v) {
477 if (strpos($v, 'javascript:') === 0) {
479 $o .= '<li role="menuitem"><a onclick="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
481 $o .= '<li role="menuitem"><a href="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
484 $this->profiler->stopRecording();
488 public function visibleActivity($item) {
490 if (empty($item['verb']) || $this->activity->isHidden($item['verb'])) {
494 // @TODO below if() block can be rewritten to a single line: $isVisible = allConditionsHere;
495 if ($this->activity->match($item['verb'], Activity::FOLLOW) &&
496 $item['object-type'] === Activity\ObjectType::NOTE &&
497 empty($item['self']) &&
498 $item['uid'] == local_user()) {