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