]> git.mxchange.org Git - friendica.git/blob - mod/display.php
Merge remote-tracking branch 'upstream/develop' into private-forums
[friendica.git] / mod / 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 use Friendica\App;
23 use Friendica\Content\Text\BBCode;
24 use Friendica\Content\Widget;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Item;
32 use Friendica\Model\Post;
33 use Friendica\Model\User;
34 use Friendica\Module\ActivityPub\Objects;
35 use Friendica\Network\HTTPException;
36 use Friendica\Protocol\ActivityPub;
37 use Friendica\Protocol\DFRN;
38 use Friendica\Protocol\Diaspora;
39
40 function display_init(App $a)
41 {
42         if (ActivityPub::isRequest()) {
43                 (new Objects(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER, ['guid' => DI::args()->getArgv()[1] ?? null]))->run();
44         }
45
46         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
47                 return;
48         }
49
50         $nick = ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : '');
51
52         $item = null;
53         $item_user = local_user();
54
55         $fields = ['uri-id', 'parent-uri-id', 'author-id', 'author-link', 'body', 'uid', 'guid', 'gravity'];
56
57         // If there is only one parameter, then check if this parameter could be a guid
58         if (DI::args()->getArgc() == 2) {
59                 $nick = '';
60
61                 // Does the local user have this item?
62                 if (local_user()) {
63                         $item = Post::selectFirstForUser(local_user(), $fields, ['guid' => DI::args()->getArgv()[1], 'uid' => local_user()]);
64                         if (DBA::isResult($item)) {
65                                 $nick = $a->getLoggedInUserNickname();
66                         }
67                 }
68
69                 // Is this item private but could be visible to the remove visitor?
70                 if (!DBA::isResult($item) && remote_user()) {
71                         $item = Post::selectFirst($fields, ['guid' => DI::args()->getArgv()[1], 'private' => Item::PRIVATE, 'origin' => true]);
72                         if (DBA::isResult($item)) {
73                                 if (!Contact::isFollower(remote_user(), $item['uid'])) {
74                                         $item = null;
75                                 } else {
76                                         $item_user = $item['uid'];
77                                 }
78                         }
79                 }
80
81                 // Is it an item with uid=0?
82                 if (!DBA::isResult($item)) {
83                         $item = Post::selectFirstForUser(local_user(), $fields, ['guid' => DI::args()->getArgv()[1], 'private' => [Item::PUBLIC, Item::UNLISTED], 'uid' => 0]);
84                 }
85         } elseif (DI::args()->getArgc() >= 3 && $nick == 'feed-item') {
86                 $uri_id = DI::args()->getArgv()[2];
87                 if (substr($uri_id, -5) == '.atom') {
88                         $uri_id = substr($uri_id, 0, -5);
89                 }
90                 $item = Post::selectFirstForUser(local_user(), $fields, ['uri-id' => $uri_id, 'private' => [Item::PUBLIC, Item::UNLISTED], 'uid' => 0]);
91         }
92
93         if (!DBA::isResult($item)) {
94                 return;
95         }
96
97         if (DI::args()->getArgc() >= 3 && $nick == 'feed-item') {
98                 displayShowFeed($item['uri-id'], $item['uid'], DI::args()->getArgc() > 3 && DI::args()->getArgv()[3] == 'conversation.atom');
99         }
100
101         if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
102                 Logger::info('Directly serving XML for uri-id '.$item['uri-id']);
103                 displayShowFeed($item['uri-id'], $item['uid'], false);
104         }
105
106         if ($item['gravity'] != GRAVITY_PARENT) {
107                 $parent = Post::selectFirstForUser($item_user, $fields, ['uid' => $item['uid'], 'uri-id' => $item['parent-uri-id']]);
108                 $item = $parent ?: $item;
109         }
110
111         DI::page()['aside'] = Widget\VCard::getHTML(display_fetchauthor($item));
112 }
113
114 function display_fetchauthor($item)
115 {
116         if (Diaspora::isReshare($item['body'], true)) {
117                 $shared = Item::getShareArray($item);
118                 if (!empty($shared['profile'])) {
119                         $contact = Contact::getByURLForUser($shared['profile'], local_user());
120                 }
121         }
122
123         if (empty($contact)) {
124                 $contact = Contact::getById($item['author-id']);
125         }
126
127         return $contact;
128 }
129
130 function display_content(App $a, $update = false, $update_uid = 0)
131 {
132         if (DI::config()->get('system','block_public') && !Session::isAuthenticated()) {
133                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
134         }
135
136         $o = '';
137
138         $item = null;
139
140         $force = (bool)($_REQUEST['force'] ?? false);
141
142         if ($update) {
143                 $uri_id = $_REQUEST['uri_id'];
144                 $item = Post::selectFirst(['uid', 'parent-uri-id'], ['uri-id' => $uri_id, 'uid' => [0, $update_uid]], ['order' => ['uid' => true]]);
145                 if (!empty($item)) {
146                         if ($item['uid'] != 0) {
147                                 $a->setProfileOwner($item['uid']);
148                         } else {
149                                 $a->setProfileOwner($update_uid);
150                         }
151                         $parent_uri_id = $item['parent-uri-id'];
152                 }
153         } else {
154                 $uri_id = ((DI::args()->getArgc() > 2) ? DI::args()->getArgv()[2] : 0);
155                 $parent_uri_id = $uri_id;
156
157                 if (DI::args()->getArgc() == 2) {
158                         $fields = ['uri-id', 'parent-uri-id', 'uid'];
159
160                         if (local_user()) {
161                                 $condition = ['guid' => DI::args()->getArgv()[1], 'uid' => [0, local_user()]];
162                                 $item = Post::selectFirstForUser(local_user(), $fields, $condition, ['order' => ['uid' => true]]);
163                                 if (DBA::isResult($item)) {
164                                         $uri_id = $item['uri-id'];
165                                         $parent_uri_id = $item['parent-uri-id'];
166                                 }
167                         }
168
169                         if (($parent_uri_id == 0) && remote_user()) {
170                                 $item = Post::selectFirst($fields, ['guid' => DI::args()->getArgv()[1], 'private' => Item::PRIVATE, 'origin' => true]);
171                                 if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
172                                         $uri_id = $item['uri-id'];
173                                         $parent_uri_id = $item['parent-uri-id'];
174                                 }
175                         }
176
177                         if ($parent_uri_id == 0) {
178                                 $condition = ['private' => [Item::PUBLIC, Item::UNLISTED], 'guid' => DI::args()->getArgv()[1], 'uid' => 0];
179                                 $item = Post::selectFirstForUser(local_user(), $fields, $condition);
180                                 if (DBA::isResult($item)) {
181                                         $uri_id = $item['uri-id'];
182                                         $parent_uri_id = $item['parent-uri-id'];
183                                 }
184                         }
185                 }
186         }
187
188         if (empty($item)) {
189                 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
190         }
191
192         if (!DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
193                 DI::notification()->setAllSeenForUser(local_user(), ['parent-uri-id' => $item['parent-uri-id']]);
194                 DI::notify()->setAllSeenForUser(local_user(), ['parent-uri-id' => $item['parent-uri-id']]);
195         }
196
197         // We are displaying an "alternate" link if that post was public. See issue 2864
198         $is_public = Post::exists(['uri-id' => $uri_id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
199         if ($is_public) {
200                 // For the atom feed the nickname doesn't matter at all, we only need the item id.
201                 $alternate = DI::baseUrl().'/display/feed-item/'.$uri_id.'.atom';
202                 $conversation = DI::baseUrl().'/display/feed-item/' . $parent_uri_id . '/conversation.atom';
203         } else {
204                 $alternate = '';
205                 $conversation = '';
206         }
207
208         DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
209                                 ['$alternate' => $alternate,
210                                         '$conversation' => $conversation]);
211
212         $is_remote_contact = false;
213         $item_uid = local_user();
214         $page_uid = 0;
215
216         $parent = null;
217         if (!local_user() && !empty($parent_uri_id)) {
218                 $parent = Post::selectFirst(['uid'], ['uri-id' => $parent_uri_id, 'wall' => true]);
219         }
220
221         if (DBA::isResult($parent)) {
222                 $page_uid = $page_uid ?? 0 ?: $parent['uid'];
223                 $is_remote_contact = Session::getRemoteContactID($page_uid);
224                 if ($is_remote_contact) {
225                         $item_uid = $parent['uid'];
226                 }
227         } else {
228                 $page_uid = $item['uid'];
229         }
230
231         if (!empty($page_uid) && ($page_uid != local_user())) {
232                 $page_user = User::getById($page_uid);
233         }
234
235         $is_owner = local_user() && (in_array($page_uid, [local_user(), 0]));
236
237         if (!empty($page_user['hidewall']) && !$is_owner && !$is_remote_contact) {
238                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
239         }
240
241         // We need the editor here to be able to reshare an item.
242         if ($is_owner && !$update) {
243                 $o .= DI::conversation()->statusEditor([], 0, true);
244         }
245         $sql_extra = Item::getPermissionsSQLByUserId($page_uid);
246
247         if (local_user() && (local_user() == $page_uid)) {
248                 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => local_user(), 'unseen' => true];
249                 $unseen = Post::exists($condition);
250         } else {
251                 $unseen = false;
252         }
253
254         if ($update && !$unseen && !$force) {
255                 return '';
256         }
257
258         $condition = ["`uri-id` = ? AND `uid` IN (0, ?) " . $sql_extra, $uri_id, $item_uid];
259         $fields = ['parent-uri-id', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id', 'owner-id', 'contact-id'];
260         $item = Post::selectFirstForUser($page_uid, $fields, $condition);
261
262         if (!DBA::isResult($item)) {
263                 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
264         }
265
266         $item['uri-id'] = $item['parent-uri-id'];
267
268         if ($unseen) {
269                 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => local_user(), 'unseen' => true];
270                 Item::update(['unseen' => false], $condition);
271         }
272
273         if (!$update && local_user()) {
274                 $o .= "<script> var netargs = '?uri_id=" . $item['uri-id'] . "'; </script>";
275         }
276
277         $o .= DI::conversation()->create([$item], 'display', $update_uid, false, 'commented', $item_uid);
278
279         // Preparing the meta header
280         $description = trim(BBCode::toPlaintext($item['body']));
281         $title = trim(BBCode::toPlaintext($item['title']));
282         $author_name = $item['author-name'];
283
284         $image = DI::baseUrl()->remove($item['author-avatar']);
285
286         if ($title == '') {
287                 $title = $author_name;
288         }
289
290         // Limit the description to 160 characters
291         if (strlen($description) > 160) {
292                 $description = substr($description, 0, 157) . '...';
293         }
294
295         $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
296         $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
297         $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
298
299         $page = DI::page();
300
301         if (DBA::exists('contact', ['unsearchable' => true, 'id' => [$item['contact-id'], $item['author-id'], $item['owner-id']]])) {
302                 $page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
303         }
304
305         DI::page()['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
306         $page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
307         $page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
308         $page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
309
310         // Schema.org microdata
311         $page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
312         $page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
313         $page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
314         $page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
315
316         // Twitter cards
317         $page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
318         $page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
319         $page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
320         $page['htmlhead'] .= '<meta name="twitter:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
321         $page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
322
323         // Dublin Core
324         $page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
325         $page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
326
327         // Open Graph
328         $page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
329         $page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
330         $page['htmlhead'] .= '<meta property="og:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
331         $page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
332         $page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
333         $page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
334         // article:tag
335
336         return $o;
337 }
338
339 function displayShowFeed(int $uri_id, int $uid, bool $conversation)
340 {
341         $xml = DFRN::itemFeed($uri_id, $uid, $conversation);
342         if ($xml == '') {
343                 throw new HTTPException\InternalServerErrorException(DI::l10n()->t('The feed for this item is unavailable.'));
344         }
345         header("Content-type: application/atom+xml");
346         echo $xml;
347         exit();
348 }