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