]> git.mxchange.org Git - friendica.git/blob - mod/display.php
Merge pull request #10543 from annando/app-variables
[friendica.git] / mod / display.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\ACL;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Session;
30 use Friendica\Database\DBA;
31 use Friendica\DI;
32 use Friendica\Model\Contact;
33 use Friendica\Model\Item;
34 use Friendica\Model\Post;
35 use Friendica\Model\User;
36 use Friendica\Module\ActivityPub\Objects;
37 use Friendica\Network\HTTPException;
38 use Friendica\Protocol\ActivityPub;
39 use Friendica\Protocol\DFRN;
40 use Friendica\Util\Strings;
41
42 function display_init(App $a)
43 {
44         if (ActivityPub::isRequest()) {
45                 Objects::rawContent(['guid' => $a->argv[1] ?? null]);
46         }
47
48         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
49                 return;
50         }
51
52         $nick = (($a->argc > 1) ? $a->argv[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 ($a->argc == 2) {
61                 $nick = '';
62
63                 // Does the local user have this item?
64                 if (local_user()) {
65                         $item = Post::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
66                         if (DBA::isResult($item)) {
67                                 $nick = $a->user['nickname'];
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' => $a->argv[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' => $a->argv[1], 'private' => [Item::PUBLIC, Item::UNLISTED], 'uid' => 0]);
86                 }
87         } elseif ($a->argc >= 3 && $nick == 'feed-item') {
88                 $uri_id = $a->argv[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 ($a->argc >= 3 && $nick == 'feed-item') {
100                 displayShowFeed($item['uri-id'], $item['uid'], $a->argc > 3 && $a->argv[3] == 'conversation.atom');
101         }
102
103         if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
104                 Logger::log('Directly serving XML for uri-id '.$item['uri-id'], Logger::DEBUG);
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         $profiledata = display_fetchauthor($item);
114
115         DI::page()['aside'] = Widget\VCard::getHTML($profiledata);
116 }
117
118 function display_fetchauthor($item)
119 {
120         $profiledata = Contact::getByURLForUser($item['author-link'], local_user());
121
122         // Check for a repeated message
123         $shared = Item::getShareArray($item);
124         if (!empty($shared) && empty($shared['comment'])) {
125                 $profiledata = [
126                         'uid' => -1,
127                         'id' => -1,
128                         'nickname' => '',
129                         'name' => '',
130                         'picdate' => '',
131                         'photo' => '',
132                         'url' => '',
133                         'network' => '',
134                 ];
135
136                 if (!empty($shared['author'])) {
137                         $profiledata['name'] = $shared['author'];
138                 }
139
140                 if (!empty($shared['profile'])) {
141                         $profiledata['url'] = $shared['profile'];
142                 }
143
144                 if (!empty($shared['avatar'])) {
145                         $profiledata['photo'] = $shared['avatar'];
146                 }
147
148                 $profiledata['nickname'] = $profiledata['name'];
149                 $profiledata['network'] = Protocol::matchByProfileUrl($profiledata['url']);
150
151                 $profiledata['address'] = '';
152                 $profiledata['about'] = '';
153
154                 $profiledata = Contact::getByURLForUser($profiledata['url'], local_user()) ?: $profiledata;
155         }
156
157         if (!empty($profiledata['photo'])) {
158                 $profiledata['photo'] = DI::baseUrl()->remove($profiledata['photo']);
159         }
160
161         return $profiledata;
162 }
163
164 function display_content(App $a, $update = false, $update_uid = 0)
165 {
166         if (DI::config()->get('system','block_public') && !Session::isAuthenticated()) {
167                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
168         }
169
170         $o = '';
171
172         $item = null;
173
174         $force = (bool)($_REQUEST['force'] ?? false);
175
176         if ($update) {
177                 $uri_id = $_REQUEST['uri_id'];
178                 $item = Post::selectFirst(['uid', 'parent-uri-id'], ['uri-id' => $uri_id, 'uid' => $update_uid]);
179                 if (!empty($item)) {
180                         if ($item['uid'] != 0) {
181                                 $a->setProfileOwner($item['uid']);
182                         } else {
183                                 $a->setProfileOwner($update_uid);
184                         }
185                         $parent_uri_id = $item['parent-uri-id'];
186                 }
187         } else {
188                 $uri_id = (($a->argc > 2) ? $a->argv[2] : 0);
189                 $parent_uri_id = $uri_id;
190
191                 if ($a->argc == 2) {
192                         $fields = ['uri-id', 'parent-uri-id', 'uid'];
193
194                         if (local_user()) {
195                                 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
196                                 $item = Post::selectFirstForUser(local_user(), $fields, $condition);
197                                 if (DBA::isResult($item)) {
198                                         $uri_id = $item['uri-id'];
199                                         $parent_uri_id = $item['parent-uri-id'];
200                                 }
201                         }
202
203                         if (($parent_uri_id == 0) && remote_user()) {
204                                 $item = Post::selectFirst($fields, ['guid' => $a->argv[1], 'private' => Item::PRIVATE, 'origin' => true]);
205                                 if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
206                                         $uri_id = $item['uri-id'];
207                                         $parent_uri_id = $item['parent-uri-id'];
208                                 }
209                         }
210
211                         if ($parent_uri_id == 0) {
212                                 $condition = ['private' => [Item::PUBLIC, Item::UNLISTED], 'guid' => $a->argv[1], 'uid' => 0];
213                                 $item = Post::selectFirstForUser(local_user(), $fields, $condition);
214                                 if (DBA::isResult($item)) {
215                                         $uri_id = $item['uri-id'];
216                                         $parent_uri_id = $item['parent-uri-id'];
217                                 }
218                         }
219                 }
220         }
221
222         if (empty($item)) {
223                 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
224         }
225
226         if (!DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
227                 DBA::update('notification', ['seen' => true], ['parent-uri-id' => $item['parent-uri-id'], 'uid' => local_user()]);
228                 DBA::update('notify', ['seen' => true], ['parent-uri-id' => $item['parent-uri-id'], 'uid' => local_user()]);
229         }
230
231         // We are displaying an "alternate" link if that post was public. See issue 2864
232         $is_public = Post::exists(['uri-id' => $uri_id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
233         if ($is_public) {
234                 // For the atom feed the nickname doesn't matter at all, we only need the item id.
235                 $alternate = DI::baseUrl().'/display/feed-item/'.$uri_id.'.atom';
236                 $conversation = DI::baseUrl().'/display/feed-item/' . $parent_uri_id . '/conversation.atom';
237         } else {
238                 $alternate = '';
239                 $conversation = '';
240         }
241
242         DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
243                                 ['$alternate' => $alternate,
244                                         '$conversation' => $conversation]);
245
246         $is_remote_contact = false;
247         $item_uid = local_user();
248         $page_uid = 0;
249
250         $parent = null;
251         if (!empty($parent_uri_id)) {
252                 $parent = Post::selectFirst(['uid'], ['uri-id' => $parent_uri_id, 'wall' => true]);
253         }
254
255         if (DBA::isResult($parent)) {
256                 $page_uid = $page_uid ?? 0 ?: $parent['uid'];
257                 $is_remote_contact = Session::getRemoteContactID($page_uid);
258                 if ($is_remote_contact) {
259                         $item_uid = $parent['uid'];
260                 }
261         } else {
262                 $page_uid = $item['uid'];
263         }
264
265         if (!empty($page_uid) && ($page_uid != local_user())) {
266                 $page_user = User::getById($page_uid);
267         }
268
269         $is_owner = local_user() && (in_array($page_uid, [local_user(), 0]));
270
271         if (!empty($page_user['hidewall']) && !$is_owner && !$is_remote_contact) {
272                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
273         }
274
275         // We need the editor here to be able to reshare an item.
276         if ($is_owner && !$update) {
277                 $x = [
278                         'is_owner' => true,
279                         'allow_location' => $a->user['allow_location'],
280                         'default_location' => $a->user['default-location'],
281                         'nickname' => $a->user['nickname'],
282                         'lockstate' => (is_array($a->user) && (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) || strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
283                         'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
284                         'bang' => '',
285                         'visitor' => 'block',
286                         'profile_uid' => local_user(),
287                 ];
288                 $o .= status_editor($a, $x, 0, true);
289         }
290         $sql_extra = Item::getPermissionsSQLByUserId($page_uid);
291
292         if (local_user() && (local_user() == $page_uid)) {
293                 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => local_user(), 'unseen' => true];
294                 $unseen = Post::exists($condition);
295         } else {
296                 $unseen = false;
297         }
298
299         if ($update && !$unseen && !$force) {
300                 return '';
301         }
302
303         $condition = ["`uri-id` = ? AND `uid` IN (0, ?) " . $sql_extra, $uri_id, $item_uid];
304         $fields = ['parent-uri-id', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id', 'owner-id', 'contact-id'];
305         $item = Post::selectFirstForUser($page_uid, $fields, $condition);
306
307         if (!DBA::isResult($item)) {
308                 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
309         }
310
311         $item['uri-id'] = $item['parent-uri-id'];
312
313         if ($unseen) {
314                 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => local_user(), 'unseen' => true];
315                 Item::update(['unseen' => false], $condition);
316         }
317
318         if (!$update && local_user()) {
319                 $o .= "<script> var netargs = '?uri_id=" . $item['uri-id'] . "'; </script>";
320         }
321
322         $o .= conversation($a, [$item], 'display', $update_uid, false, 'commented', $item_uid);
323
324         // Preparing the meta header
325         $description = trim(BBCode::toPlaintext($item['body']));
326         $title = trim(BBCode::toPlaintext($item['title']));
327         $author_name = $item['author-name'];
328
329         $image = DI::baseUrl()->remove($item['author-avatar']);
330
331         if ($title == '') {
332                 $title = $author_name;
333         }
334
335         // Limit the description to 160 characters
336         if (strlen($description) > 160) {
337                 $description = substr($description, 0, 157) . '...';
338         }
339
340         $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
341         $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
342         $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
343
344         $page = DI::page();
345
346         if (DBA::exists('contact', ['unsearchable' => true, 'id' => [$item['contact-id'], $item['author-id'], $item['owner-id']]])) {
347                 $page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
348         }
349
350         DI::page()['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
351         $page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
352         $page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
353         $page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
354
355         // Schema.org microdata
356         $page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
357         $page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
358         $page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
359         $page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
360
361         // Twitter cards
362         $page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
363         $page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
364         $page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
365         $page['htmlhead'] .= '<meta name="twitter:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
366         $page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
367
368         // Dublin Core
369         $page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
370         $page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
371
372         // Open Graph
373         $page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
374         $page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
375         $page['htmlhead'] .= '<meta property="og:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
376         $page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
377         $page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
378         $page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
379         // article:tag
380
381         return $o;
382 }
383
384 function displayShowFeed(int $uri_id, int $uid, bool $conversation)
385 {
386         $xml = DFRN::itemFeed($uri_id, $uid, $conversation);
387         if ($xml == '') {
388                 throw new HTTPException\InternalServerErrorException(DI::l10n()->t('The feed for this item is unavailable.'));
389         }
390         header("Content-type: application/atom+xml");
391         echo $xml;
392         exit();
393 }