]> git.mxchange.org Git - friendica.git/blob - mod/display.php
The "share" handling is improved
[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 = Session::getLocalUser();
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 (Session::getLocalUser()) {
66                         $item = Post::selectFirstForUser(Session::getLocalUser(), $fields, ['guid' => DI::args()->getArgv()[1], 'uid' => Session::getLocalUser()]);
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) && Session::getRemoteUser()) {
74                         $item = Post::selectFirst($fields, ['guid' => DI::args()->getArgv()[1], 'private' => Item::PRIVATE, 'origin' => true]);
75                         if (DBA::isResult($item)) {
76                                 if (!Contact::isFollower(Session::getRemoteUser(), $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(Session::getLocalUser(), $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(Session::getLocalUser(), $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'] != Item::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         $shared = Item::getShareArray($item);
127         if (empty($shared['comment']) && !empty($shared['guid']) && !empty($shared['profile'])) {
128                 $contact = Contact::getByURLForUser($shared['profile'], Session::getLocalUser());
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                 if (empty($_REQUEST['force'])) {
162                         $browser_update = intval(DI::pConfig()->get($update_uid, 'system', 'update_interval'));
163                         if (!empty($browser_update)) {
164                                 $update_date = date(DateTimeFormat::MYSQL, time() - ($browser_update / 500));
165                                 if (!Post::exists(["`parent-uri-id` = ? AND `uid` IN (?, ?) AND `received` > ?", $parent_uri_id, 0, $update_uid, $update_date])) {
166                                         Logger::debug('No updated content', ['uri-id' => $uri_id, 'uid' => $update_uid, 'updated' => $update_date]);
167                                         return '';
168                                 } else {
169                                         Logger::debug('Updated content found', ['uri-id' => $uri_id, 'uid' => $update_uid, 'updated' => $update_date]);
170                                 }
171                         }       
172                 } else {
173                         Logger::debug('Forced content update', ['uri-id' => $uri_id, 'uid' => $update_uid]);
174                 }
175         } else {
176                 $uri_id = ((DI::args()->getArgc() > 2) ? DI::args()->getArgv()[2] : 0);
177                 $parent_uri_id = $uri_id;
178
179                 if (DI::args()->getArgc() == 2) {
180                         $fields = ['uri-id', 'parent-uri-id', 'uid'];
181
182                         if (Session::getLocalUser()) {
183                                 $condition = ['guid' => DI::args()->getArgv()[1], 'uid' => [0, Session::getLocalUser()]];
184                                 $item = Post::selectFirstForUser(Session::getLocalUser(), $fields, $condition, ['order' => ['uid' => true]]);
185                                 if (DBA::isResult($item)) {
186                                         $uri_id = $item['uri-id'];
187                                         $parent_uri_id = $item['parent-uri-id'];
188                                 }
189                         }
190
191                         if (($parent_uri_id == 0) && Session::getRemoteUser()) {
192                                 $item = Post::selectFirst($fields, ['guid' => DI::args()->getArgv()[1], 'private' => Item::PRIVATE, 'origin' => true]);
193                                 if (DBA::isResult($item) && Contact::isFollower(Session::getRemoteUser(), $item['uid'])) {
194                                         $uri_id = $item['uri-id'];
195                                         $parent_uri_id = $item['parent-uri-id'];
196                                 }
197                         }
198
199                         if ($parent_uri_id == 0) {
200                                 $condition = ['private' => [Item::PUBLIC, Item::UNLISTED], 'guid' => DI::args()->getArgv()[1], 'uid' => 0];
201                                 $item = Post::selectFirstForUser(Session::getLocalUser(), $fields, $condition);
202                                 if (DBA::isResult($item)) {
203                                         $uri_id = $item['uri-id'];
204                                         $parent_uri_id = $item['parent-uri-id'];
205                                 }
206                         }
207                 }
208         }
209
210         if (empty($item)) {
211                 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
212         }
213
214         if (!DI::pConfig()->get(Session::getLocalUser(), 'system', 'detailed_notif')) {
215                 DI::notification()->setAllSeenForUser(Session::getLocalUser(), ['parent-uri-id' => $item['parent-uri-id']]);
216                 DI::notify()->setAllSeenForUser(Session::getLocalUser(), ['parent-uri-id' => $item['parent-uri-id']]);
217         }
218
219         // We are displaying an "alternate" link if that post was public. See issue 2864
220         $is_public = Post::exists(['uri-id' => $uri_id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
221         if ($is_public) {
222                 // For the atom feed the nickname doesn't matter at all, we only need the item id.
223                 $alternate = DI::baseUrl().'/display/feed-item/'.$uri_id.'.atom';
224                 $conversation = DI::baseUrl().'/display/feed-item/' . $parent_uri_id . '/conversation.atom';
225         } else {
226                 $alternate = '';
227                 $conversation = '';
228         }
229
230         DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
231                                 ['$alternate' => $alternate,
232                                         '$conversation' => $conversation]);
233
234         $is_remote_contact = false;
235         $item_uid = Session::getLocalUser();
236         $page_uid = 0;
237
238         $parent = null;
239         if (!Session::getLocalUser() && !empty($parent_uri_id)) {
240                 $parent = Post::selectFirst(['uid'], ['uri-id' => $parent_uri_id, 'wall' => true]);
241         }
242
243         if (DBA::isResult($parent)) {
244                 $page_uid = $page_uid ?? 0 ?: $parent['uid'];
245                 $is_remote_contact = Session::getRemoteContactID($page_uid);
246                 if ($is_remote_contact) {
247                         $item_uid = $parent['uid'];
248                 }
249         } else {
250                 $page_uid = $item['uid'];
251         }
252
253         if (!empty($page_uid) && ($page_uid != Session::getLocalUser())) {
254                 $page_user = User::getById($page_uid);
255         }
256
257         $is_owner = Session::getLocalUser() && (in_array($page_uid, [Session::getLocalUser(), 0]));
258
259         if (!empty($page_user['hidewall']) && !$is_owner && !$is_remote_contact) {
260                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
261         }
262
263         // We need the editor here to be able to reshare an item.
264         if ($is_owner && !$update) {
265                 $o .= DI::conversation()->statusEditor([], 0, true);
266         }
267         $sql_extra = Item::getPermissionsSQLByUserId($page_uid);
268
269         if (Session::getLocalUser() && (Session::getLocalUser() == $page_uid)) {
270                 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => Session::getLocalUser(), 'unseen' => true];
271                 $unseen = Post::exists($condition);
272         } else {
273                 $unseen = false;
274         }
275
276         if ($update && !$unseen && !$force) {
277                 return '';
278         }
279
280         $condition = ["`uri-id` = ? AND `uid` IN (0, ?) " . $sql_extra, $uri_id, $item_uid];
281         $fields = ['parent-uri-id', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id', 'owner-id', 'contact-id'];
282         $item = Post::selectFirstForUser($page_uid, $fields, $condition);
283
284         if (!DBA::isResult($item)) {
285                 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
286         }
287
288         $item['uri-id'] = $item['parent-uri-id'];
289
290         if ($unseen) {
291                 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => Session::getLocalUser(), 'unseen' => true];
292                 Item::update(['unseen' => false], $condition);
293         }
294
295         if (!$update && Session::getLocalUser()) {
296                 $o .= "<script> var netargs = '?uri_id=" . $item['uri-id'] . "'; </script>";
297         }
298
299         $o .= DI::conversation()->create([$item], 'display', $update_uid, false, 'commented', $item_uid);
300
301         // Preparing the meta header
302         $description = trim(BBCode::toPlaintext($item['body']));
303         $title = trim(BBCode::toPlaintext($item['title'] ?? ''));
304         $author_name = $item['author-name'];
305
306         $image = DI::baseUrl()->remove($item['author-avatar']);
307
308         if ($title == '') {
309                 $title = $author_name;
310         }
311
312         // Limit the description to 160 characters
313         if (strlen($description) > 160) {
314                 $description = substr($description, 0, 157) . '...';
315         }
316
317         $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
318         $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
319         $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
320
321         $page = DI::page();
322
323         if (DBA::exists('contact', ['unsearchable' => true, 'id' => [$item['contact-id'], $item['author-id'], $item['owner-id']]])) {
324                 $page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
325         }
326
327         DI::page()['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
328         $page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
329         $page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
330         $page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
331
332         // Schema.org microdata
333         $page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
334         $page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
335         $page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
336         $page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
337
338         // Twitter cards
339         $page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
340         $page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
341         $page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
342         $page['htmlhead'] .= '<meta name="twitter:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
343         $page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
344
345         // Dublin Core
346         $page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
347         $page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
348
349         // Open Graph
350         $page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
351         $page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
352         $page['htmlhead'] .= '<meta property="og:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
353         $page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
354         $page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
355         $page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
356         // article:tag
357
358         return $o;
359 }
360
361 function displayShowFeed(int $uri_id, int $uid, bool $conversation)
362 {
363         $xml = DFRN::itemFeed($uri_id, $uid, $conversation);
364         if ($xml == '') {
365                 throw new HTTPException\InternalServerErrorException(DI::l10n()->t('The feed for this item is unavailable.'));
366         }
367
368         System::httpExit($xml, Response::TYPE_ATOM);
369 }