3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
23 use Friendica\Content\Text\BBCode;
24 use Friendica\Content\Text\HTML;
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;
32 use Friendica\Model\Contact;
33 use Friendica\Model\Item;
34 use Friendica\Model\Post;
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;
42 function display_init(App $a)
44 if (ActivityPub::isRequest()) {
45 Objects::rawContent(['guid' => $a->argv[1] ?? null]);
48 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
52 $nick = (($a->argc > 1) ? $a->argv[1] : '');
55 $item_user = local_user();
57 $fields = ['uri-id', 'parent-uri-id', 'author-id', 'body', 'uid', 'guid', 'gravity'];
59 // If there is only one parameter, then check if this parameter could be a guid
63 // Does the local user have this item?
65 $item = Post::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
66 if (DBA::isResult($item)) {
67 $nick = $a->user["nickname"];
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'])) {
78 $item_user = $item['uid'];
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]);
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);
92 $item = Post::selectFirstForUser(local_user(), $fields, ['uri-id' => $uri_id, 'private' => [Item::PUBLIC, Item::UNLISTED], 'uid' => 0]);
95 if (!DBA::isResult($item)) {
99 if ($a->argc >= 3 && $nick == 'feed-item') {
100 displayShowFeed($item['uri-id'], $item['uid'], $a->argc > 3 && $a->argv[3] == 'conversation.atom');
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);
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;
113 $profiledata = display_fetchauthor($a, $item);
115 if (strstr(Strings::normaliseLink($profiledata['url']), Strings::normaliseLink(DI::baseUrl()))) {
116 $nickname = str_replace(Strings::normaliseLink(DI::baseUrl()) . '/profile/', '', Strings::normaliseLink($profiledata['url']));
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;
123 $profiledata["network"] = Protocol::DFRN;
129 Profile::load($a, $nick, $profiledata);
132 function display_fetchauthor($a, $item)
134 $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
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'];
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'];
152 if (!empty($shared['profile'])) {
153 $profiledata['url'] = $shared['profile'];
156 if (!empty($shared['avatar'])) {
157 $profiledata['photo'] = $shared['avatar'];
160 $profiledata["nickname"] = $profiledata["name"];
161 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
163 $profiledata["address"] = "";
164 $profiledata["about"] = "";
167 $profiledata = Contact::getByURLForUser($profiledata["url"], local_user()) ?: $profiledata;
169 if (!empty($profiledata["photo"])) {
170 $profiledata["photo"] = DI::baseUrl()->remove($profiledata["photo"]);
176 function display_content(App $a, $update = false, $update_uid = 0)
178 if (DI::config()->get('system','block_public') && !Session::isAuthenticated()) {
179 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
186 $force = (bool)($_REQUEST['force'] ?? false);
189 $uri_id = $_REQUEST['uri_id'];
190 $item = Post::selectFirst(['uid', 'parent-uri-id'], ['uri-id' => $uri_id, 'uid' => $update_uid]);
191 if ($item['uid'] != 0) {
192 $a->profile = ['uid' => intval($item['uid'])];
194 $a->profile = ['uid' => intval($update_uid)];
196 $parent_uri_id = $item['parent-uri-id'];
198 $uri_id = (($a->argc > 2) ? $a->argv[2] : 0);
199 $parent_uri_id = $uri_id;
202 $fields = ['uri-id', 'parent-uri-id', 'uid'];
205 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
206 $item = Post::selectFirstForUser(local_user(), $fields, $condition);
207 if (DBA::isResult($item)) {
208 $uri_id = $item['uri-id'];
209 $parent_uri_id = $item['parent-uri-id'];
213 if (($parent_uri_id == 0) && remote_user()) {
214 $item = Post::selectFirst($fields, ['guid' => $a->argv[1], 'private' => Item::PRIVATE, 'origin' => true]);
215 if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
216 $uri_id = $item['uri-id'];
217 $parent_uri_id = $item['parent-uri-id'];
221 if ($parent_uri_id == 0) {
222 $condition = ['private' => [Item::PUBLIC, Item::UNLISTED], 'guid' => $a->argv[1], 'uid' => 0];
223 $item = Post::selectFirstForUser(local_user(), $fields, $condition);
224 if (DBA::isResult($item)) {
225 $uri_id = $item['uri-id'];
226 $parent_uri_id = $item['parent-uri-id'];
233 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
236 if (!DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
237 DBA::update('notify', ['seen' => true], ['parent-uri-id' => $item['parent-uri-id'], 'uid' => local_user()]);
240 // We are displaying an "alternate" link if that post was public. See issue 2864
241 $is_public = Post::exists(['uri-id' => $uri_id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
243 // For the atom feed the nickname doesn't matter at all, we only need the item id.
244 $alternate = DI::baseUrl().'/display/feed-item/'.$uri_id.'.atom';
245 $conversation = DI::baseUrl().'/display/feed-item/' . $parent_uri_id . '/conversation.atom';
251 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
252 ['$alternate' => $alternate,
253 '$conversation' => $conversation]);
255 $is_remote_contact = false;
256 $item_uid = local_user();
259 if (!empty($parent_uri_id)) {
260 $parent = Post::selectFirst(['uid'], ['uri-id' => $parent_uri_id, 'wall' => true]);
263 if (DBA::isResult($parent)) {
264 $a->profile['uid'] = ($a->profile['uid'] ?? 0) ?: $parent['uid'];
265 $is_remote_contact = Session::getRemoteContactID($a->profile['uid']);
266 if ($is_remote_contact) {
267 $item_uid = $parent['uid'];
270 $a->profile = ['uid' => intval($item['uid'])];
273 $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
274 if (DBA::isResult($page_contact)) {
275 $a->page_contact = $page_contact;
278 $is_owner = (local_user() && (in_array($a->profile['uid'], [local_user(), 0])) ? true : false);
280 if (!empty($a->profile['hidewall']) && !$is_owner && !$is_remote_contact) {
281 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
284 // We need the editor here to be able to reshare an item.
285 if ($is_owner && !$update) {
288 'allow_location' => $a->user['allow_location'],
289 'default_location' => $a->user['default-location'],
290 'nickname' => $a->user['nickname'],
291 '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'),
292 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
294 'visitor' => 'block',
295 'profile_uid' => local_user(),
297 $o .= status_editor($a, $x, 0, true);
299 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['uid']);
301 if (local_user() && (local_user() == $a->profile['uid'])) {
302 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => local_user(), 'unseen' => true];
303 $unseen = Post::exists($condition);
308 if ($update && !$unseen && !$force) {
312 $condition = ["`uri-id` = ? AND `uid` IN (0, ?) " . $sql_extra, $uri_id, $item_uid];
313 $fields = ['parent-uri-id', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id', 'owner-id', 'contact-id'];
314 $item = Post::selectFirstForUser($a->profile['uid'], $fields, $condition);
316 if (!DBA::isResult($item)) {
317 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
320 $item['uri-id'] = $item['parent-uri-id'];
323 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => local_user(), 'unseen' => true];
324 Item::update(['unseen' => false], $condition);
327 if (!$update && local_user()) {
328 $o .= "<script> var netargs = '?uri_id=" . $item['uri-id'] . "'; </script>";
331 $o .= conversation($a, [$item], 'display', $update_uid, false, 'commented', $item_uid);
333 // Preparing the meta header
334 $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
335 $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
336 $author_name = $item["author-name"];
338 $image = DI::baseUrl()->remove($item["author-avatar"]);
341 $title = $author_name;
344 // Limit the description to 160 characters
345 if (strlen($description) > 160) {
346 $description = substr($description, 0, 157) . '...';
349 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
350 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
351 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
355 if (DBA::exists('contact', ['unsearchable' => true, 'id' => [$item['contact-id'], $item['author-id'], $item['owner-id']]])) {
356 $page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
359 DI::page()['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
360 $page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
361 $page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
362 $page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
364 // Schema.org microdata
365 $page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
366 $page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
367 $page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
368 $page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
371 $page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
372 $page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
373 $page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
374 $page['htmlhead'] .= '<meta name="twitter:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
375 $page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
378 $page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
379 $page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
382 $page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
383 $page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
384 $page['htmlhead'] .= '<meta property="og:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
385 $page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
386 $page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
387 $page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
393 function displayShowFeed(int $uri_id, int $uid, bool $conversation)
395 $xml = DFRN::itemFeed($uri_id, $uid, $conversation);
397 throw new HTTPException\InternalServerErrorException(DI::l10n()->t('The feed for this item is unavailable.'));
399 header("Content-type: application/atom+xml");