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\Config;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Protocol;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\Session;
16 use Friendica\Database\DBA;
18 use Friendica\Model\Contact;
19 use Friendica\Model\Item;
20 use Friendica\Model\Profile;
21 use Friendica\Module\Objects;
22 use Friendica\Network\HTTPException;
23 use Friendica\Protocol\ActivityPub;
24 use Friendica\Protocol\DFRN;
25 use Friendica\Util\Strings;
27 function display_init(App $a)
29 if (ActivityPub::isRequest()) {
30 Objects::rawContent();
33 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
37 $nick = (($a->argc > 1) ? $a->argv[1] : '');
40 $item_user = local_user();
42 $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
44 // If there is only one parameter, then check if this parameter could be a guid
48 // Does the local user have this item?
50 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
51 if (DBA::isResult($item)) {
52 $nick = $a->user["nickname"];
56 // Is this item private but could be visible to the remove visitor?
57 if (!DBA::isResult($item) && remote_user()) {
58 $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1, 'origin' => true]);
59 if (DBA::isResult($item)) {
60 if (!Contact::isFollower(remote_user(), $item['uid'])) {
63 $item_user = $item['uid'];
68 // Is it an item with uid=0?
69 if (!DBA::isResult($item)) {
70 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
72 } elseif ($a->argc >= 3 && $nick == 'feed-item') {
73 $item_id = $a->argv[2];
74 if (substr($item_id, -5) == '.atom') {
75 $item_id = substr($item_id, 0, -5);
77 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item_id, 'private' => [0, 2], 'uid' => 0]);
80 if (!DBA::isResult($item)) {
84 if ($a->argc >= 3 && $nick == 'feed-item') {
85 displayShowFeed($item['id'], $a->argc > 3 && $a->argv[3] == 'conversation.atom');
88 if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
89 Logger::log('Directly serving XML for id '.$item["id"], Logger::DEBUG);
90 displayShowFeed($item["id"], false);
93 if ($item["id"] != $item["parent"]) {
94 $parent = Item::selectFirstForUser($item_user, $fields, ['id' => $item["parent"]]);
95 $item = $parent ?: $item;
98 $profiledata = display_fetchauthor($a, $item);
100 if (strstr(Strings::normaliseLink($profiledata["url"]), Strings::normaliseLink(DI::baseUrl()))) {
101 $nickname = str_replace(Strings::normaliseLink(DI::baseUrl())."/profile/", "", Strings::normaliseLink($profiledata["url"]));
103 if ($nickname != $a->user["nickname"]) {
104 $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
105 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
106 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
109 if (DBA::isResult($profile)) {
110 $profiledata = $profile;
112 $profiledata["network"] = Protocol::DFRN;
118 Profile::load($a, $nick, 0, $profiledata);
121 function display_fetchauthor($a, $item)
123 $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
126 $profiledata['uid'] = -1;
127 $profiledata['nickname'] = $author['nick'];
128 $profiledata['name'] = $author['name'];
129 $profiledata['picdate'] = '';
130 $profiledata['photo'] = $author['photo'];
131 $profiledata['url'] = $author['url'];
132 $profiledata['network'] = $author['network'];
134 // Check for a repeated message
135 $shared = Item::getShareArray($item);
136 if (!empty($shared) && empty($shared['comment'])) {
137 if (!empty($shared['author'])) {
138 $profiledata['name'] = $shared['author'];
141 if (!empty($shared['profile'])) {
142 $profiledata['url'] = $shared['profile'];
145 if (!empty($shared['avatar'])) {
146 $profiledata['photo'] = $shared['avatar'];
149 $profiledata["nickname"] = $profiledata["name"];
150 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
152 $profiledata["address"] = "";
153 $profiledata["about"] = "";
156 $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
158 if (!empty($profiledata["photo"])) {
159 $profiledata["photo"] = DI::baseUrl()->remove($profiledata["photo"]);
165 function display_content(App $a, $update = false, $update_uid = 0)
167 if (Config::get('system','block_public') && !Session::isAuthenticated()) {
168 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
176 $item_id = $_REQUEST['item_id'];
177 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
178 if ($item['uid'] != 0) {
179 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
181 $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
183 $item_parent = $item['parent'];
184 $item_parent_uri = $item['parent-uri'];
186 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
187 $item_parent = $item_id;
191 $fields = ['id', 'parent', 'parent-uri', 'uid'];
194 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
195 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
196 if (DBA::isResult($item)) {
197 $item_id = $item["id"];
198 $item_parent = $item["parent"];
199 $item_parent_uri = $item['parent-uri'];
203 if (($item_parent == 0) && remote_user()) {
204 $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1, 'origin' => true]);
205 if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
206 $item_id = $item["id"];
207 $item_parent = $item["parent"];
208 $item_parent_uri = $item['parent-uri'];
212 if ($item_parent == 0) {
213 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
214 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
215 if (DBA::isResult($item)) {
216 $item_id = $item["id"];
217 $item_parent = $item["parent"];
218 $item_parent_uri = $item['parent-uri'];
225 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
228 // We are displaying an "alternate" link if that post was public. See issue 2864
229 $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
231 // For the atom feed the nickname doesn't matter at all, we only need the item id.
232 $alternate = DI::baseUrl().'/display/feed-item/'.$item_id.'.atom';
233 $conversation = DI::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
239 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
240 ['$alternate' => $alternate,
241 '$conversation' => $conversation]);
243 $is_remote_contact = false;
244 $item_uid = local_user();
247 if (!empty($item_parent_uri)) {
248 $parent = Item::selectFirst(['uid'], ['uri' => $item_parent_uri, 'wall' => true]);
251 if (DBA::isResult($parent)) {
252 $a->profile['uid'] = ($a->profile['uid'] ?? 0) ?: $parent['uid'];
253 $a->profile['profile_uid'] = ($a->profile['profile_uid'] ?? 0) ?: $parent['uid'];
254 $is_remote_contact = Session::getRemoteContactID($a->profile['profile_uid']);
255 if ($is_remote_contact) {
256 $item_uid = $parent['uid'];
259 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
262 $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
263 if (DBA::isResult($page_contact)) {
264 $a->page_contact = $page_contact;
267 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
269 if (!empty($a->profile['hidewall']) && !$is_owner && !$is_remote_contact) {
270 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
273 // We need the editor here to be able to reshare an item.
277 'allow_location' => $a->user['allow_location'],
278 'default_location' => $a->user['default-location'],
279 'nickname' => $a->user['nickname'],
280 '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'),
281 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
283 'visitor' => 'block',
284 'profile_uid' => local_user(),
286 $o .= status_editor($a, $x, 0, true);
288 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid']);
290 if (local_user() && (local_user() == $a->profile['profile_uid'])) {
291 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
292 $unseen = Item::exists($condition);
297 if ($update && !$unseen) {
301 $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, $item_uid];
302 $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id', 'owner-id', 'contact-id'];
303 $item = Item::selectFirstForUser($a->profile['profile_uid'], $fields, $condition);
305 if (!DBA::isResult($item)) {
306 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
309 $item['uri'] = $item['parent-uri'];
312 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
313 Item::update(['unseen' => false], $condition);
317 $o .= "<script> var netargs = '?item_id=" . $item_id . "'; </script>";
320 $o .= conversation($a, [$item], new Pager(DI::args()->getQueryString()), 'display', $update_uid, false, 'commented', $item_uid);
322 // Preparing the meta header
323 $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
324 $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
325 $author_name = $item["author-name"];
327 $image = DI::baseUrl()->remove($item["author-avatar"]);
330 $title = $author_name;
333 // Limit the description to 160 characters
334 if (strlen($description) > 160) {
335 $description = substr($description, 0, 157) . '...';
338 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
339 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
340 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
344 if (DBA::exists('contact', ['unsearchable' => true, 'id' => [$item['contact-id'], $item['author-id'], $item['owner-id']]])) {
345 $page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
348 DI::page()['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
349 $page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
350 $page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
351 $page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
353 // Schema.org microdata
354 $page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
355 $page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
356 $page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
357 $page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
360 $page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
361 $page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
362 $page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
363 $page['htmlhead'] .= '<meta name="twitter:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
364 $page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
367 $page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
368 $page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
371 $page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
372 $page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
373 $page['htmlhead'] .= '<meta property="og:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
374 $page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
375 $page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
376 $page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
382 function displayShowFeed($item_id, $conversation)
384 $xml = DFRN::itemFeed($item_id, $conversation);
386 throw new HTTPException\InternalServerErrorException(DI::l10n()->t('The feed for this item is unavailable.'));
388 header("Content-type: application/atom+xml");