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\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\Protocol;
15 use Friendica\Core\Renderer;
16 use Friendica\Core\System;
17 use Friendica\Database\DBA;
18 use Friendica\Model\Contact;
19 use Friendica\Model\Group;
20 use Friendica\Model\Item;
21 use Friendica\Model\Profile;
22 use Friendica\Protocol\ActivityPub;
23 use Friendica\Protocol\DFRN;
25 function display_init(App $a)
27 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
31 $nick = (($a->argc > 1) ? $a->argv[1] : '');
35 if (substr($a->argv[2], -5) == '.atom') {
36 $item_id = substr($a->argv[2], 0, -5);
37 displayShowFeed($item_id, false);
42 if ($a->argv[3] == 'conversation.atom') {
43 $item_id = $a->argv[2];
44 displayShowFeed($item_id, true);
50 $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
52 // If there is only one parameter, then check if this parameter could be a guid
56 // Does the local user have this item?
58 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
59 if (DBA::isResult($item)) {
60 $nick = $a->user["nickname"];
64 // Is it an item with uid=0?
65 if (!DBA::isResult($item)) {
66 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
68 } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
69 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => [0, 2], 'uid' => 0]);
72 if (!DBA::isResult($item)) {
74 notice(L10n::t('Item not found.') . EOL);
78 if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
79 Logger::log('Directly serving XML for id '.$item["id"], Logger::DEBUG);
80 displayShowFeed($item["id"], false);
83 if (ActivityPub::isRequest()) {
84 $a->internalRedirect(str_replace('display/', 'objects/', $a->query_string));
87 if ($item["id"] != $item["parent"]) {
88 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item["parent"]]);
91 $profiledata = display_fetchauthor($a, $item);
93 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
94 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
96 if (($nickname != $a->user["nickname"])) {
97 $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
98 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
99 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
102 if (DBA::isResult($profile)) {
103 $profiledata = $profile;
105 $profiledata["network"] = Protocol::DFRN;
111 Profile::load($a, $nick, 0, $profiledata);
114 function display_fetchauthor($a, $item)
116 $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
119 $profiledata['uid'] = -1;
120 $profiledata['nickname'] = $author['nick'];
121 $profiledata['name'] = $author['name'];
122 $profiledata['picdate'] = '';
123 $profiledata['photo'] = $author['photo'];
124 $profiledata['url'] = $author['url'];
125 $profiledata['network'] = $author['network'];
127 // Check for a repeated message
129 $body = trim($item["body"]);
131 // Skip if it isn't a pure repeated messages
132 // Does it start with a share?
133 if (!$skip && strpos($body, "[share") > 0) {
136 // Does it end with a share?
137 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
141 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
142 // Skip if there is no shared message in there
143 if ($body == $attributes) {
150 preg_match("/author='(.*?)'/ism", $attributes, $matches);
151 if (!empty($matches[1])) {
152 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
154 preg_match('/author="(.*?)"/ism', $attributes, $matches);
155 if (!empty($matches[1])) {
156 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
159 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
160 if (!empty($matches[1])) {
161 $profiledata["url"] = $matches[1];
163 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
164 if (!empty($matches[1])) {
165 $profiledata["url"] = $matches[1];
168 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
169 if (!empty($matches[1])) {
170 $profiledata["photo"] = $matches[1];
172 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
173 if (!empty($matches[1])) {
174 $profiledata["photo"] = $matches[1];
176 $profiledata["nickname"] = $profiledata["name"];
177 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
179 $profiledata["address"] = "";
180 $profiledata["about"] = "";
183 $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
185 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
188 if (in_array($profiledata["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
189 $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
191 } elseif ($profiledata["network"] == Protocol::DFRN) {
192 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
193 $profiledata["remoteconnect"] = $connect;
196 return($profiledata);
199 function display_content(App $a, $update = false, $update_uid = 0)
201 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
202 notice(L10n::t('Public access denied.') . EOL);
206 require_once 'include/conversation.php';
211 $item_id = $_REQUEST['item_id'];
212 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
213 if ($item['uid'] != 0) {
214 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
216 $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
218 $item_parent = $item['parent'];
219 $item_parent_uri = $item['parent-uri'];
221 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
222 $item_parent = $item_id;
226 $fields = ['id', 'parent', 'parent-uri'];
229 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
230 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
231 if (DBA::isResult($item)) {
232 $item_id = $item["id"];
233 $item_parent = $item["parent"];
234 $item_parent_uri = $item['parent-uri'];
238 if ($item_parent == 0) {
239 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
240 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
241 if (DBA::isResult($item)) {
242 $item_id = $item["id"];
243 $item_parent = $item["parent"];
244 $item_parent_uri = $item['parent-uri'];
252 notice(L10n::t('Item not found.').EOL);
256 // We are displaying an "alternate" link if that post was public. See issue 2864
257 $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
259 // For the atom feed the nickname doesn't matter at all, we only need the item id.
260 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
261 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
267 $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
268 ['$alternate' => $alternate,
269 '$conversation' => $conversation]);
274 $is_remote_contact = false;
278 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
279 foreach ($_SESSION['remote'] as $v) {
280 if ($v['uid'] == $a->profile['uid']) {
281 $contact_id = $v['cid'];
288 $groups = Group::getIdsByContactId($contact_id);
289 $remote_contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $a->profile['uid']]);
290 if (DBA::isResult($remote_contact)) {
291 $contact = $remote_contact;
292 $is_remote_contact = true;
296 if (!$is_remote_contact) {
298 $contact_id = $_SESSION['cid'];
299 $contact = $a->contact;
303 $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
304 if (DBA::isResult($page_contact)) {
305 $a->page_contact = $page_contact;
307 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
309 if (x($a->profile, 'hidewall') && !$is_owner && !$is_remote_contact) {
310 notice(L10n::t('Access to this profile has been restricted.') . EOL);
314 // We need the editor here to be able to reshare an item.
318 'allow_location' => $a->user['allow_location'],
319 'default_location' => $a->user['default-location'],
320 'nickname' => $a->user['nickname'],
321 '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'),
322 'acl' => ACL::getFullSelectorHTML($a->user, true),
324 'visitor' => 'block',
325 'profile_uid' => local_user(),
327 $o .= status_editor($a, $x, 0, true);
330 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['uid'], $is_remote_contact, $groups);
332 if (local_user() && (local_user() == $a->profile['uid'])) {
333 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
334 $unseen = Item::exists($condition);
339 if ($update && !$unseen) {
343 $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, local_user()];
344 $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink'];
345 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
347 if (!DBA::isResult($item)) {
348 notice(L10n::t('Item not found.') . EOL);
352 $item['uri'] = $item['parent-uri'];
355 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
356 Item::update(['unseen' => false], $condition);
360 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
363 $o .= conversation($a, [$item], new Pager($a->query_string), 'display', $update_uid, false, 'commented', local_user());
365 // Preparing the meta header
366 $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
367 $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
368 $author_name = $item["author-name"];
370 $image = $a->removeBaseURL($item["author-avatar"]);
373 $title = $author_name;
376 // Limit the description to 160 characters
377 if (strlen($description) > 160) {
378 $description = substr($description, 0, 157) . '...';
381 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
382 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
383 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
385 //<meta name="keywords" content="">
386 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
387 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
388 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
389 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
391 // Schema.org microdata
392 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
393 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
394 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
395 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
398 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
399 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
400 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
401 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
402 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
405 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
406 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
409 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
410 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
411 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
412 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
413 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
414 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
420 function displayShowFeed($item_id, $conversation)
422 $xml = DFRN::itemFeed($item_id, $conversation);
424 System::httpExit(500);
426 header("Content-type: application/atom+xml");