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