3 * @file mod/display.php
7 use Friendica\Content\Pager;
8 use Friendica\Content\Text\BBCode;
9 use Friendica\Content\Text\HTML;
10 use Friendica\Core\ACL;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\Session;
15 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Item;
19 use Friendica\Model\Profile;
20 use Friendica\Module\Objects;
21 use Friendica\Network\HTTPException;
22 use Friendica\Protocol\ActivityPub;
23 use Friendica\Protocol\DFRN;
24 use Friendica\Util\Strings;
26 function display_init(App $a)
28 if (ActivityPub::isRequest()) {
29 Objects::rawContent();
32 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
36 $nick = (($a->argc > 1) ? $a->argv[1] : '');
39 $item_user = local_user();
41 $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
43 // If there is only one parameter, then check if this parameter could be a guid
47 // Does the local user have this item?
49 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
50 if (DBA::isResult($item)) {
51 $nick = $a->user["nickname"];
55 // Is this item private but could be visible to the remove visitor?
56 if (!DBA::isResult($item) && remote_user()) {
57 $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1, 'origin' => true]);
58 if (DBA::isResult($item)) {
59 if (!Contact::isFollower(remote_user(), $item['uid'])) {
62 $item_user = $item['uid'];
67 // Is it an item with uid=0?
68 if (!DBA::isResult($item)) {
69 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
71 } elseif ($a->argc >= 3 && $nick == 'feed-item') {
72 $item_id = $a->argv[2];
73 if (substr($item_id, -5) == '.atom') {
74 $item_id = substr($item_id, 0, -5);
76 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item_id, 'private' => [0, 2], 'uid' => 0]);
79 if (!DBA::isResult($item)) {
83 if ($a->argc >= 3 && $nick == 'feed-item') {
84 displayShowFeed($item['id'], $a->argc > 3 && $a->argv[3] == 'conversation.atom');
87 if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
88 Logger::log('Directly serving XML for id '.$item["id"], Logger::DEBUG);
89 displayShowFeed($item["id"], false);
92 if ($item["id"] != $item["parent"]) {
93 $parent = Item::selectFirstForUser($item_user, $fields, ['id' => $item["parent"]]);
94 $item = $parent ?: $item;
97 $profiledata = display_fetchauthor($a, $item);
99 if (strstr(Strings::normaliseLink($profiledata['url']), Strings::normaliseLink(DI::baseUrl()))) {
100 $nickname = str_replace(Strings::normaliseLink(DI::baseUrl()) . '/profile/', '', Strings::normaliseLink($profiledata['url']));
102 if (!empty($a->user['nickname']) && $nickname != $a->user['nickname']) {
103 $profile = DBA::fetchFirst("SELECT `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
104 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
105 WHERE `user`.`nickname` = ? AND `contact`.`self` LIMIT 1",
108 if (DBA::isResult($profile)) {
109 $profiledata = $profile;
111 $profiledata["network"] = Protocol::DFRN;
117 Profile::load($a, $nick, $profiledata);
120 function display_fetchauthor($a, $item)
122 $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
125 $profiledata['uid'] = -1;
126 $profiledata['nickname'] = $author['nick'];
127 $profiledata['name'] = $author['name'];
128 $profiledata['picdate'] = '';
129 $profiledata['photo'] = $author['photo'];
130 $profiledata['url'] = $author['url'];
131 $profiledata['network'] = $author['network'];
133 // Check for a repeated message
134 $shared = Item::getShareArray($item);
135 if (!empty($shared) && empty($shared['comment'])) {
136 if (!empty($shared['author'])) {
137 $profiledata['name'] = $shared['author'];
140 if (!empty($shared['profile'])) {
141 $profiledata['url'] = $shared['profile'];
144 if (!empty($shared['avatar'])) {
145 $profiledata['photo'] = $shared['avatar'];
148 $profiledata["nickname"] = $profiledata["name"];
149 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
151 $profiledata["address"] = "";
152 $profiledata["about"] = "";
155 $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
157 if (!empty($profiledata["photo"])) {
158 $profiledata["photo"] = DI::baseUrl()->remove($profiledata["photo"]);
164 function display_content(App $a, $update = false, $update_uid = 0)
166 if (DI::config()->get('system','block_public') && !Session::isAuthenticated()) {
167 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
175 $item_id = $_REQUEST['item_id'];
176 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
177 if ($item['uid'] != 0) {
178 $a->profile = ['uid' => intval($item['uid'])];
180 $a->profile = ['uid' => intval($update_uid)];
182 $item_parent = $item['parent'];
183 $item_parent_uri = $item['parent-uri'];
185 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
186 $item_parent = $item_id;
190 $fields = ['id', 'parent', 'parent-uri', 'uid'];
193 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
194 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
195 if (DBA::isResult($item)) {
196 $item_id = $item["id"];
197 $item_parent = $item["parent"];
198 $item_parent_uri = $item['parent-uri'];
202 if (($item_parent == 0) && remote_user()) {
203 $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1, 'origin' => true]);
204 if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
205 $item_id = $item["id"];
206 $item_parent = $item["parent"];
207 $item_parent_uri = $item['parent-uri'];
211 if ($item_parent == 0) {
212 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
213 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
214 if (DBA::isResult($item)) {
215 $item_id = $item["id"];
216 $item_parent = $item["parent"];
217 $item_parent_uri = $item['parent-uri'];
224 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
227 // We are displaying an "alternate" link if that post was public. See issue 2864
228 $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
230 // For the atom feed the nickname doesn't matter at all, we only need the item id.
231 $alternate = DI::baseUrl().'/display/feed-item/'.$item_id.'.atom';
232 $conversation = DI::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
238 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
239 ['$alternate' => $alternate,
240 '$conversation' => $conversation]);
242 $is_remote_contact = false;
243 $item_uid = local_user();
246 if (!empty($item_parent_uri)) {
247 $parent = Item::selectFirst(['uid'], ['uri' => $item_parent_uri, 'wall' => true]);
250 if (DBA::isResult($parent)) {
251 $a->profile['uid'] = ($a->profile['uid'] ?? 0) ?: $parent['uid'];
252 $is_remote_contact = Session::getRemoteContactID($a->profile['uid']);
253 if ($is_remote_contact) {
254 $item_uid = $parent['uid'];
257 $a->profile = ['uid' => intval($item['uid'])];
260 $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
261 if (DBA::isResult($page_contact)) {
262 $a->page_contact = $page_contact;
265 $is_owner = (local_user() && (in_array($a->profile['uid'], [local_user(), 0])) ? true : false);
267 if (!empty($a->profile['hidewall']) && !$is_owner && !$is_remote_contact) {
268 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
271 // We need the editor here to be able to reshare an item.
275 'allow_location' => $a->user['allow_location'],
276 'default_location' => $a->user['default-location'],
277 'nickname' => $a->user['nickname'],
278 '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'),
279 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
281 'visitor' => 'block',
282 'profile_uid' => local_user(),
284 $o .= status_editor($a, $x, 0, true);
286 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['uid']);
288 if (local_user() && (local_user() == $a->profile['uid'])) {
289 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
290 $unseen = Item::exists($condition);
295 if ($update && !$unseen) {
299 $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, $item_uid];
300 $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id', 'owner-id', 'contact-id'];
301 $item = Item::selectFirstForUser($a->profile['uid'], $fields, $condition);
303 if (!DBA::isResult($item)) {
304 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
307 $item['uri'] = $item['parent-uri'];
310 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
311 Item::update(['unseen' => false], $condition);
315 $o .= "<script> var netargs = '?item_id=" . $item_id . "'; </script>";
318 $o .= conversation($a, [$item], new Pager(DI::args()->getQueryString()), 'display', $update_uid, false, 'commented', $item_uid);
320 // Preparing the meta header
321 $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
322 $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
323 $author_name = $item["author-name"];
325 $image = DI::baseUrl()->remove($item["author-avatar"]);
328 $title = $author_name;
331 // Limit the description to 160 characters
332 if (strlen($description) > 160) {
333 $description = substr($description, 0, 157) . '...';
336 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
337 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
338 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
342 if (DBA::exists('contact', ['unsearchable' => true, 'id' => [$item['contact-id'], $item['author-id'], $item['owner-id']]])) {
343 $page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
346 DI::page()['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
347 $page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
348 $page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
349 $page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
351 // Schema.org microdata
352 $page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
353 $page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
354 $page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
355 $page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
358 $page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
359 $page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
360 $page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
361 $page['htmlhead'] .= '<meta name="twitter:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
362 $page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
365 $page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
366 $page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
369 $page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
370 $page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
371 $page['htmlhead'] .= '<meta property="og:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
372 $page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
373 $page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
374 $page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
380 function displayShowFeed($item_id, $conversation)
382 $xml = DFRN::itemFeed($item_id, $conversation);
384 throw new HTTPException\InternalServerErrorException(DI::l10n()->t('The feed for this item is unavailable.'));
386 header("Content-type: application/atom+xml");