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