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