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