3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Widget;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session;
28 use Friendica\Core\System;
29 use Friendica\Database\DBA;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Item;
33 use Friendica\Model\Post;
34 use Friendica\Model\User;
35 use Friendica\Module\ActivityPub\Objects;
36 use Friendica\Module\Response;
37 use Friendica\Network\HTTPException;
38 use Friendica\Protocol\ActivityPub;
39 use Friendica\Protocol\DFRN;
40 use Friendica\Protocol\Diaspora;
41 use Friendica\Util\DateTimeFormat;
43 function display_init(App $a)
45 if (ActivityPub::isRequest()) {
46 (new Objects(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER, ['guid' => DI::args()->getArgv()[1] ?? null]))->run();
49 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
53 $nick = ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : '');
56 $item_user = Session::getLocalUser();
58 $fields = ['uri-id', 'parent-uri-id', 'author-id', 'author-link', 'body', 'uid', 'guid', 'gravity'];
60 // If there is only one parameter, then check if this parameter could be a guid
61 if (DI::args()->getArgc() == 2) {
64 // Does the local user have this item?
65 if (Session::getLocalUser()) {
66 $item = Post::selectFirstForUser(Session::getLocalUser(), $fields, ['guid' => DI::args()->getArgv()[1], 'uid' => Session::getLocalUser()]);
67 if (DBA::isResult($item)) {
68 $nick = $a->getLoggedInUserNickname();
72 // Is this item private but could be visible to the remove visitor?
73 if (!DBA::isResult($item) && Session::getRemoteUser()) {
74 $item = Post::selectFirst($fields, ['guid' => DI::args()->getArgv()[1], 'private' => Item::PRIVATE, 'origin' => true]);
75 if (DBA::isResult($item)) {
76 if (!Contact::isFollower(Session::getRemoteUser(), $item['uid'])) {
79 $item_user = $item['uid'];
84 // Is it an item with uid=0?
85 if (!DBA::isResult($item)) {
86 $item = Post::selectFirstForUser(Session::getLocalUser(), $fields, ['guid' => DI::args()->getArgv()[1], 'private' => [Item::PUBLIC, Item::UNLISTED], 'uid' => 0]);
88 } elseif (DI::args()->getArgc() >= 3 && $nick == 'feed-item') {
89 $uri_id = DI::args()->getArgv()[2];
90 if (substr($uri_id, -5) == '.atom') {
91 $uri_id = substr($uri_id, 0, -5);
93 $item = Post::selectFirstForUser(Session::getLocalUser(), $fields, ['uri-id' => $uri_id, 'private' => [Item::PUBLIC, Item::UNLISTED], 'uid' => 0]);
96 if (!DBA::isResult($item)) {
100 if (DI::args()->getArgc() >= 3 && $nick == 'feed-item') {
101 displayShowFeed($item['uri-id'], $item['uid'], DI::args()->getArgc() > 3 && DI::args()->getArgv()[3] == 'conversation.atom');
104 if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
105 Logger::debug('Directly serving XML', ['uri-id' => $item['uri-id']]);
106 displayShowFeed($item['uri-id'], $item['uid'], false);
109 if ($item['gravity'] != Item::GRAVITY_PARENT) {
110 $parent = Post::selectFirstForUser($item_user, $fields, ['uid' => [0, $item_user], 'uri-id' => $item['parent-uri-id']], ['order' => ['uid' => true]]);
111 $item = $parent ?: $item;
114 $author = display_fetchauthor($item);
116 if (\Friendica\Util\Network::isLocalLink($author['url'])) {
117 \Friendica\Model\Profile::load(DI::app(), $author['nick'], false);
119 DI::page()['aside'] = Widget\VCard::getHTML($author);
121 $a->setProfileOwner($item['uid']);
124 function display_fetchauthor($item)
126 $shared = Item::getShareArray($item);
127 if (empty($shared['comment']) && !empty($shared['guid']) && !empty($shared['profile'])) {
128 $contact = Contact::getByURLForUser($shared['profile'], Session::getLocalUser());
131 if (empty($contact)) {
132 $contact = Contact::getById($item['author-id']);
138 function display_content(App $a, $update = false, $update_uid = 0)
140 if (DI::config()->get('system','block_public') && !Session::isAuthenticated()) {
141 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
148 $force = (bool)($_REQUEST['force'] ?? false);
151 $uri_id = $_REQUEST['uri_id'];
152 $item = Post::selectFirst(['uid', 'parent-uri-id'], ['uri-id' => $uri_id, 'uid' => [0, $update_uid]], ['order' => ['uid' => true]]);
154 if ($item['uid'] != 0) {
155 $a->setProfileOwner($item['uid']);
157 $a->setProfileOwner($update_uid);
159 $parent_uri_id = $item['parent-uri-id'];
161 if (empty($_REQUEST['force'])) {
162 $browser_update = intval(DI::pConfig()->get($update_uid, 'system', 'update_interval'));
163 if (!empty($browser_update)) {
164 $update_date = date(DateTimeFormat::MYSQL, time() - ($browser_update / 500));
165 if (!Post::exists(["`parent-uri-id` = ? AND `uid` IN (?, ?) AND `received` > ?", $parent_uri_id, 0, $update_uid, $update_date])) {
166 Logger::debug('No updated content', ['uri-id' => $uri_id, 'uid' => $update_uid, 'updated' => $update_date]);
169 Logger::debug('Updated content found', ['uri-id' => $uri_id, 'uid' => $update_uid, 'updated' => $update_date]);
173 Logger::debug('Forced content update', ['uri-id' => $uri_id, 'uid' => $update_uid]);
176 $uri_id = ((DI::args()->getArgc() > 2) ? DI::args()->getArgv()[2] : 0);
177 $parent_uri_id = $uri_id;
179 if (DI::args()->getArgc() == 2) {
180 $fields = ['uri-id', 'parent-uri-id', 'uid'];
182 if (Session::getLocalUser()) {
183 $condition = ['guid' => DI::args()->getArgv()[1], 'uid' => [0, Session::getLocalUser()]];
184 $item = Post::selectFirstForUser(Session::getLocalUser(), $fields, $condition, ['order' => ['uid' => true]]);
185 if (DBA::isResult($item)) {
186 $uri_id = $item['uri-id'];
187 $parent_uri_id = $item['parent-uri-id'];
191 if (($parent_uri_id == 0) && Session::getRemoteUser()) {
192 $item = Post::selectFirst($fields, ['guid' => DI::args()->getArgv()[1], 'private' => Item::PRIVATE, 'origin' => true]);
193 if (DBA::isResult($item) && Contact::isFollower(Session::getRemoteUser(), $item['uid'])) {
194 $uri_id = $item['uri-id'];
195 $parent_uri_id = $item['parent-uri-id'];
199 if ($parent_uri_id == 0) {
200 $condition = ['private' => [Item::PUBLIC, Item::UNLISTED], 'guid' => DI::args()->getArgv()[1], 'uid' => 0];
201 $item = Post::selectFirstForUser(Session::getLocalUser(), $fields, $condition);
202 if (DBA::isResult($item)) {
203 $uri_id = $item['uri-id'];
204 $parent_uri_id = $item['parent-uri-id'];
211 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
214 if (!DI::pConfig()->get(Session::getLocalUser(), 'system', 'detailed_notif')) {
215 DI::notification()->setAllSeenForUser(Session::getLocalUser(), ['parent-uri-id' => $item['parent-uri-id']]);
216 DI::notify()->setAllSeenForUser(Session::getLocalUser(), ['parent-uri-id' => $item['parent-uri-id']]);
219 // We are displaying an "alternate" link if that post was public. See issue 2864
220 $is_public = Post::exists(['uri-id' => $uri_id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
222 // For the atom feed the nickname doesn't matter at all, we only need the item id.
223 $alternate = DI::baseUrl().'/display/feed-item/'.$uri_id.'.atom';
224 $conversation = DI::baseUrl().'/display/feed-item/' . $parent_uri_id . '/conversation.atom';
230 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
231 ['$alternate' => $alternate,
232 '$conversation' => $conversation]);
234 $is_remote_contact = false;
235 $item_uid = Session::getLocalUser();
239 if (!Session::getLocalUser() && !empty($parent_uri_id)) {
240 $parent = Post::selectFirst(['uid'], ['uri-id' => $parent_uri_id, 'wall' => true]);
243 if (DBA::isResult($parent)) {
244 $page_uid = $page_uid ?? 0 ?: $parent['uid'];
245 $is_remote_contact = Session::getRemoteContactID($page_uid);
246 if ($is_remote_contact) {
247 $item_uid = $parent['uid'];
250 $page_uid = $item['uid'];
253 if (!empty($page_uid) && ($page_uid != Session::getLocalUser())) {
254 $page_user = User::getById($page_uid);
257 $is_owner = Session::getLocalUser() && (in_array($page_uid, [Session::getLocalUser(), 0]));
259 if (!empty($page_user['hidewall']) && !$is_owner && !$is_remote_contact) {
260 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
263 // We need the editor here to be able to reshare an item.
264 if ($is_owner && !$update) {
265 $o .= DI::conversation()->statusEditor([], 0, true);
267 $sql_extra = Item::getPermissionsSQLByUserId($page_uid);
269 if (Session::getLocalUser() && (Session::getLocalUser() == $page_uid)) {
270 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => Session::getLocalUser(), 'unseen' => true];
271 $unseen = Post::exists($condition);
276 if ($update && !$unseen && !$force) {
280 $condition = ["`uri-id` = ? AND `uid` IN (0, ?) " . $sql_extra, $uri_id, $item_uid];
281 $fields = ['parent-uri-id', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id', 'owner-id', 'contact-id'];
282 $item = Post::selectFirstForUser($page_uid, $fields, $condition);
284 if (!DBA::isResult($item)) {
285 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
288 $item['uri-id'] = $item['parent-uri-id'];
291 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => Session::getLocalUser(), 'unseen' => true];
292 Item::update(['unseen' => false], $condition);
295 if (!$update && Session::getLocalUser()) {
296 $o .= "<script> var netargs = '?uri_id=" . $item['uri-id'] . "'; </script>";
299 $o .= DI::conversation()->create([$item], 'display', $update_uid, false, 'commented', $item_uid);
301 // Preparing the meta header
302 $description = trim(BBCode::toPlaintext($item['body']));
303 $title = trim(BBCode::toPlaintext($item['title'] ?? ''));
304 $author_name = $item['author-name'];
306 $image = DI::baseUrl()->remove($item['author-avatar']);
309 $title = $author_name;
312 // Limit the description to 160 characters
313 if (strlen($description) > 160) {
314 $description = substr($description, 0, 157) . '...';
317 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
318 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
319 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
323 if (DBA::exists('contact', ['unsearchable' => true, 'id' => [$item['contact-id'], $item['author-id'], $item['owner-id']]])) {
324 $page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
327 DI::page()['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
328 $page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
329 $page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
330 $page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
332 // Schema.org microdata
333 $page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
334 $page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
335 $page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
336 $page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
339 $page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
340 $page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
341 $page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
342 $page['htmlhead'] .= '<meta name="twitter:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
343 $page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
346 $page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
347 $page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
350 $page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
351 $page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
352 $page['htmlhead'] .= '<meta property="og:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
353 $page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
354 $page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
355 $page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
361 function displayShowFeed(int $uri_id, int $uid, bool $conversation)
363 $xml = DFRN::itemFeed($uri_id, $uid, $conversation);
365 throw new HTTPException\InternalServerErrorException(DI::l10n()->t('The feed for this item is unavailable.'));
368 System::httpExit($xml, Response::TYPE_ATOM);