]> git.mxchange.org Git - friendica.git/blob - src/Module/Item/Display.php
Update function / rearrange tab order
[friendica.git] / src / Module / Item / Display.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Item;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Content\Conversation;
27 use Friendica\Content\Item as ContentItem;
28 use Friendica\Content\Text\BBCode;
29 use Friendica\Core\Config\Capability\IManageConfigValues;
30 use Friendica\Core\L10n;
31 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
32 use Friendica\Core\Renderer;
33 use Friendica\Core\Session\Capability\IHandleUserSessions;
34 use Friendica\Model\Contact;
35 use Friendica\Model\Item;
36 use Friendica\Model\Post;
37 use Friendica\Model\Profile;
38 use Friendica\Model\User;
39 use Friendica\Module\Response;
40 use Friendica\Module\Special\DisplayNotFound;
41 use Friendica\Navigation\Notifications\Repository\Notification;
42 use Friendica\Navigation\Notifications\Repository\Notify;
43 use Friendica\Protocol\ActivityPub;
44 use Friendica\Util\Network;
45 use Friendica\Util\Profiler;
46 use Friendica\Network\HTTPException;
47 use Friendica\Content\Widget;
48 use Psr\Log\LoggerInterface;
49
50 /**
51  * Controller to display one item and its conversation
52  */
53 class Display extends BaseModule
54 {
55         /** @var App\Page */
56         protected $page;
57         /** @var IManageConfigValues */
58         protected $config;
59         /** @var IManagePersonalConfigValues */
60         protected $pConfig;
61         /** @var IHandleUserSessions */
62         protected $session;
63         /** @var App */
64         protected $app;
65         /** @var ContentItem */
66         protected $contentItem;
67         /** @var Conversation */
68         protected $conversation;
69         /** @var Notification */
70         protected $notification;
71         /** @var Notify */
72         protected $notify;
73
74         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManageConfigValues $config, IManagePersonalConfigValues $pConfig, IHandleUserSessions $session, App $app, App\Page $page, ContentItem $contentItem, Conversation $conversation, Notification $notification, Notify $notify, array $server, array $parameters = [])
75         {
76                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
77
78                 $this->page         = $page;
79                 $this->config       = $config;
80                 $this->pConfig      = $pConfig;
81                 $this->session      = $session;
82                 $this->app          = $app;
83                 $this->contentItem  = $contentItem;
84                 $this->conversation = $conversation;
85                 $this->notification = $notification;
86                 $this->notify       = $notify;
87         }
88
89         protected function content(array $request = []): string
90         {
91                 if (ActivityPub::isRequest()) {
92                         $this->baseUrl->redirect(str_replace('display/', 'objects/', $this->args->getQueryString()));
93                 }
94
95                 if ($this->config->get('system', 'block_public') && !$this->session->isAuthenticated()) {
96                         throw new HTTPException\UnauthorizedException($this->t('Access denied.'));
97                 }
98
99                 $guid = $this->parameters['guid'] ?? 0;
100
101                 $item    = null;
102                 $itemUid = $this->session->getLocalUserId();
103
104                 $fields = ['uri-id', 'parent-uri-id', 'author-id', 'author-link', 'body', 'uid', 'guid', 'gravity'];
105
106                 // Does the local user have this item?
107                 if ($this->session->getLocalUserId()) {
108                         $item = Post::selectFirstForUser($this->session->getLocalUserId(), $fields, [
109                                 'guid' => $guid,
110                                 'uid'  => $this->session->getLocalUserId()
111                         ]);
112                 }
113
114                 // Is this item private but could be visible to the remove visitor?
115                 if (empty($item) && $this->session->getRemoteUserId()) {
116                         $item = Post::selectFirst($fields, ['guid' => $guid, 'private' => Item::PRIVATE, 'origin' => true]);
117                         if (!empty($item)) {
118                                 if (!Contact::isFollower($this->session->getRemoteUserId(), $item['uid'])) {
119                                         $item = null;
120                                 } else {
121                                         $itemUid = $item['uid'];
122                                 }
123                         }
124                 }
125
126                 // Is it an item with uid = 0?
127                 if (empty($item)) {
128                         $item = Post::selectFirstForUser($this->session->getLocalUserId(), $fields, [
129                                 'guid'    => $guid,
130                                 'private' => [Item::PUBLIC, Item::UNLISTED],
131                                 'uid'     => 0
132                         ]);
133                 }
134
135                 if (empty($item)) {
136                         $this->page['aside'] = '';
137                         $displayNotFound = new DisplayNotFound($this->l10n, $this->baseUrl, $this->args, $this->logger, $this->profiler, $this->response, $this->server, $this->parameters);
138                         return $displayNotFound->content();
139                 }
140
141                 if ($item['gravity'] != Item::GRAVITY_PARENT) {
142                         $parent = Post::selectFirst($fields, [
143                                 'uid'    => [0, $itemUid],
144                                 'uri-id' => $item['parent-uri-id']
145                         ], ['order' => ['uid' => true]]);
146
147                         $item = $parent ?: $item;
148                 }
149
150                 if (!$this->pConfig->get($this->session->getLocalUserId(), 'system', 'detailed_notif')) {
151                         $this->notification->setAllSeenForUser($this->session->getLocalUserId(), ['parent-uri-id' => $item['parent-uri-id']]);
152                         $this->notify->setAllSeenForUser($this->session->getLocalUserId(), ['parent-uri-id' => $item['parent-uri-id']]);
153                 }
154
155                 $this->displaySidebar($item);
156                 $this->displayHead($item['uri-id'], $item['parent-uri-id']);
157
158                 $output = '';
159
160                 // add the uri-id to the update_display parameter
161                 if ($this->session->getLocalUserId()) {
162                         $output .= "<script> var netargs = '?uri_id=" . $item['uri-id'] . "'; </script>";
163                 }
164
165                 $output .= $this->getDisplayData($item);
166
167                 return $output;
168         }
169
170         /**
171          * Loads the content for the sidebar of the display page
172          *
173          * @param array $item The current item
174          *
175          * @return void
176          * @throws HTTPException\InternalServerErrorException
177          * @throws HTTPException\NotFoundException
178          * @throws \ImagickException
179          */
180         protected function displaySidebar(array $item)
181         {
182                 $shared = $this->contentItem->getSharedPost($item, ['author-link']);
183                 if (!empty($shared) && empty($shared['comment'])) {
184                         $author = Contact::getByURLForUser($shared['post']['author-link'], $this->session->getLocalUserId());
185                 }
186
187                 if (empty($contact)) {
188                         $author = Contact::getById($item['author-id']);
189                 }
190
191                 if (Network::isLocalLink($author['url'])) {
192                         Profile::load($this->app, $author['nick'], false);
193                 } else {
194                         $this->page['aside'] = Widget\VCard::getHTML($author);
195                 }
196
197                 $this->app->setProfileOwner($item['uid']);
198         }
199
200         protected function getDisplayData(array $item, bool $update = false, int $updateUid = 0, bool $force = false): string
201         {
202                 $itemUid = $this->session->getLocalUserId();
203
204                 $parent = null;
205                 if (!$this->session->getLocalUserId() && !empty($item['parent-uri-id'])) {
206                         $parent = Post::selectFirst(['uid'], ['uri-id' => $item['parent-uri-id'], 'wall' => true]);
207                 }
208
209                 if (!empty($parent)) {
210                         $pageUid         = $parent['uid'];
211                         if ($this->session->getRemoteContactID($pageUid)) {
212                                 $itemUid = $parent['uid'];
213                         }
214                 } else {
215                         $pageUid = $item['uid'];
216                 }
217
218                 if (!empty($pageUid) && ($pageUid != $this->session->getLocalUserId())) {
219                         $page_user = User::getById($pageUid, ['nickname', 'hidewall']);
220                 }
221
222                 if (!empty($page_user['hidewall']) && !$this->session->isAuthenticated()) {
223                         $this->baseUrl->redirect('profile/' . $page_user['nickname'] . '/restricted');
224                 }
225
226                 $sql_extra = Item::getPermissionsSQLByUserId($pageUid);
227
228                 if ($this->session->getLocalUserId() && ($this->session->getLocalUserId() == $pageUid)) {
229                         $unseen = Post::exists([
230                                 'parent-uri-id' => $item['parent-uri-id'],
231                                 'uid'           => $this->session->getLocalUserId(),
232                                 'unseen'        => true
233                         ]);
234                 } else {
235                         $unseen = false;
236                 }
237
238                 if ($update && !$unseen && !$force) {
239                         return '';
240                 }
241
242                 $condition = ["`uri-id` = ? AND `uid` IN (0, ?) " . $sql_extra, $item['uri-id'], $itemUid];
243                 $fields    = [
244                         'parent-uri-id', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id',
245                         'owner-id', 'contact-id'
246                 ];
247
248                 $item = Post::selectFirstForUser($pageUid, $fields, $condition);
249
250                 if (empty($item)) {
251                         $this->page['aside'] = '';
252                         $displayNotFound = new DisplayNotFound($this->l10n, $this->baseUrl, $this->args, $this->logger, $this->profiler, $this->response, $this->server, $this->parameters);
253                         return $displayNotFound->content();
254                 }
255
256                 $item['uri-id'] = $item['parent-uri-id'];
257
258                 if ($unseen) {
259                         $condition = [
260                                 'parent-uri-id' => $item['parent-uri-id'],
261                                 'uid'           => $this->session->getLocalUserId(),
262                                 'unseen'        => true
263                         ];
264                         Item::update(['unseen' => false], $condition);
265                 }
266
267                 $this->addMetaTags($item);
268
269                 $output = '';
270
271                 $is_owner = $this->session->getLocalUserId() && (in_array($pageUid, [$this->session->getLocalUserId(), 0]));
272
273                 // We need the editor here to be able to reshare an item.
274                 if ($is_owner && !$update) {
275                         $output .= $this->conversation->statusEditor([], 0, true);
276                 }
277
278                 $output .= $this->conversation->render([$item], Conversation::MODE_DISPLAY, $updateUid, false, 'commented', $itemUid);
279
280                 return $output;
281         }
282
283         // We are displaying an "alternate" link if that post was public. See issue 2864
284         protected function displayHead(string $uriId, string $parentUriId)
285         {
286                 if (Post::exists(['uri-id' => $uriId, 'private' => [Item::PUBLIC, Item::UNLISTED]])) {
287                         // For the atom feed the nickname doesn't matter at all, we only need the item id.
288                         $this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'), [
289                                 '$alternate'    => sprintf('display/feed-item/%s.atom', $uriId),
290                                 '$conversation' => sprintf('display/feed-item/%s/conversation.atom', $parentUriId)
291                         ]);
292                 }
293         }
294
295         /**
296          * Adds <meta> tags to the HTML output based on an item
297          *
298          * @param array $item The item with the information for the <meta> tags
299          *
300          * @return void
301          * @throws \Exception
302          */
303         protected function addMetaTags(array $item)
304         {
305                 // Preparing the meta header
306                 $description = trim(BBCode::toPlaintext($item['body']));
307                 $title       = trim(BBCode::toPlaintext($item['title'] ?? ''));
308                 $author_name = $item['author-name'];
309
310                 $image = $this->baseUrl->remove($item['author-avatar']);
311
312                 if ($title === '') {
313                         $title = $author_name;
314                 }
315
316                 // Limit the description to 160 characters
317                 if (strlen($description) > 160) {
318                         $description = substr($description, 0, 157) . '...';
319                 }
320
321                 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
322                 $title       = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
323                 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
324
325                 $page = $this->page;
326
327                 if (Contact::exists([
328                         'unsearchable' => true, 'id' => [$item['contact-id'], $item['author-id'], $item['owner-id']]
329                 ])) {
330                         $page['htmlhead'] .= "<meta content=\"noindex, noarchive\" name=\"robots\" />\n";
331                 }
332
333                 $page['htmlhead'] .= sprintf("<meta name=\"author\" content=\"%s\" />\n", $author_name);
334                 $page['htmlhead'] .= sprintf("<meta name=\"title\" content=\"%s\" />\n", $title);
335                 $page['htmlhead'] .= sprintf("<meta name=\"fulltitle\" content=\"%s\" />\n", $title);
336                 $page['htmlhead'] .= sprintf("<meta name=\"description\" content=\"%s\" />\n", $description);
337
338                 // Schema.org microdata
339                 $page['htmlhead'] .= sprintf("<meta itemprop=\"name\" content=\"%s\" />\n", $title);
340                 $page['htmlhead'] .= sprintf("<meta itemprop=\"description\" content=\"%s\" />\n", $description);
341                 $page['htmlhead'] .= sprintf("<meta itemprop=\"image\" content=\"%s\" />\n", $image);
342                 $page['htmlhead'] .= sprintf("<meta itemprop=\"author\" content=\"%s\" />\n", $author_name);
343
344                 // Twitter cards
345                 $page['htmlhead'] .= "<meta name=\"twitter:card\" content=\"summary\" />\n";
346                 $page['htmlhead'] .= sprintf("<meta name=\"twitter:title\" content=\"%s\" />\n", $title);
347                 $page['htmlhead'] .= sprintf("<meta name=\"twitter:description\" content=\"%s\" />\n", $description);
348                 $page['htmlhead'] .= sprintf("<meta name=\"twitter:image\" content=\"%s/%s\" />\n", $this->baseUrl, $image);
349                 $page['htmlhead'] .= sprintf("<meta name=\"twitter:url\" content=\"%s\" />\n", $item["plink"]);
350
351                 // Dublin Core
352                 $page['htmlhead'] .= sprintf("<meta name=\"DC.title\" content=\"%s\" />\n", $title);
353                 $page['htmlhead'] .= sprintf("<meta name=\"DC.description\" content=\"%s\" />\n", $description);
354
355                 // Open Graph
356                 $page['htmlhead'] .= "<meta property=\"og:type\" content=\"website\" />\n";
357                 $page['htmlhead'] .= sprintf("<meta property=\"og:title\" content=\"%s\" />\n", $title);
358                 $page['htmlhead'] .= sprintf("<meta property=\"og:image\" content=\"%s/%s\" />\n", $this->baseUrl, $image);
359                 $page['htmlhead'] .= sprintf("<meta property=\"og:url\" content=\"%s\" />\n", $item["plink"]);
360                 $page['htmlhead'] .= sprintf("<meta property=\"og:description\" content=\"%s\" />\n", $description);
361                 $page['htmlhead'] .= sprintf("<meta name=\"og:article:author\" content=\"%s\" />\n", $author_name);
362                 // article:tag
363         }
364 }