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