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\System;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Group;
19 use Friendica\Model\Item;
20 use Friendica\Model\Profile;
21 use Friendica\Protocol\ActivityPub;
22 use Friendica\Protocol\DFRN;
24 function display_init(App $a)
26 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
30 $nick = (($a->argc > 1) ? $a->argv[1] : '');
34 if (substr($a->argv[2], -5) == '.atom') {
35 $item_id = substr($a->argv[2], 0, -5);
36 displayShowFeed($item_id, false);
41 if ($a->argv[3] == 'conversation.atom') {
42 $item_id = $a->argv[2];
43 displayShowFeed($item_id, true);
49 $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
51 // If there is only one parameter, then check if this parameter could be a guid
55 // Does the local user have this item?
57 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
58 if (DBA::isResult($item)) {
59 $nick = $a->user["nickname"];
63 // Is it an item with uid=0?
64 if (!DBA::isResult($item)) {
65 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
67 } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
68 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => [0, 2], 'uid' => 0]);
71 if (!DBA::isResult($item)) {
73 notice(L10n::t('Item not found.') . EOL);
77 if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
78 Logger::log('Directly serving XML for id '.$item["id"], LOGGER_DEBUG);
79 displayShowFeed($item["id"], false);
82 if (ActivityPub::isRequest()) {
83 $a->internalRedirect(str_replace('display/', 'objects/', $a->query_string));
86 if ($item["id"] != $item["parent"]) {
87 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item["parent"]]);
90 $profiledata = display_fetchauthor($a, $item);
92 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
93 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
95 if (($nickname != $a->user["nickname"])) {
96 $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
97 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
98 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
101 if (DBA::isResult($profile)) {
102 $profiledata = $profile;
104 $profiledata["network"] = Protocol::DFRN;
110 Profile::load($a, $nick, 0, $profiledata);
113 function display_fetchauthor($a, $item)
115 $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
118 $profiledata['uid'] = -1;
119 $profiledata['nickname'] = $author['nick'];
120 $profiledata['name'] = $author['name'];
121 $profiledata['picdate'] = '';
122 $profiledata['photo'] = $author['photo'];
123 $profiledata['url'] = $author['url'];
124 $profiledata['network'] = $author['network'];
126 // Check for a repeated message
128 $body = trim($item["body"]);
130 // Skip if it isn't a pure repeated messages
131 // Does it start with a share?
132 if (!$skip && strpos($body, "[share") > 0) {
135 // Does it end with a share?
136 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
140 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
141 // Skip if there is no shared message in there
142 if ($body == $attributes) {
149 preg_match("/author='(.*?)'/ism", $attributes, $matches);
150 if (!empty($matches[1])) {
151 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
153 preg_match('/author="(.*?)"/ism', $attributes, $matches);
154 if (!empty($matches[1])) {
155 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
158 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
159 if (!empty($matches[1])) {
160 $profiledata["url"] = $matches[1];
162 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
163 if (!empty($matches[1])) {
164 $profiledata["url"] = $matches[1];
167 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
168 if (!empty($matches[1])) {
169 $profiledata["photo"] = $matches[1];
171 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
172 if (!empty($matches[1])) {
173 $profiledata["photo"] = $matches[1];
175 $profiledata["nickname"] = $profiledata["name"];
176 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
178 $profiledata["address"] = "";
179 $profiledata["about"] = "";
182 $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
184 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
187 if (in_array($profiledata["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
188 $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
190 } elseif ($profiledata["network"] == Protocol::DFRN) {
191 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
192 $profiledata["remoteconnect"] = $connect;
195 return($profiledata);
198 function display_content(App $a, $update = false, $update_uid = 0)
200 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
201 notice(L10n::t('Public access denied.') . EOL);
205 require_once 'include/conversation.php';
210 $item_id = $_REQUEST['item_id'];
211 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
212 if ($item['uid'] != 0) {
213 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
215 $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
217 $item_parent = $item['parent'];
218 $item_parent_uri = $item['parent-uri'];
220 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
221 $item_parent = $item_id;
225 $fields = ['id', 'parent', 'parent-uri'];
228 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
229 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
230 if (DBA::isResult($item)) {
231 $item_id = $item["id"];
232 $item_parent = $item["parent"];
233 $item_parent_uri = $item['parent-uri'];
237 if ($item_parent == 0) {
238 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
239 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
240 if (DBA::isResult($item)) {
241 $item_id = $item["id"];
242 $item_parent = $item["parent"];
243 $item_parent_uri = $item['parent-uri'];
251 notice(L10n::t('Item not found.').EOL);
255 // We are displaying an "alternate" link if that post was public. See issue 2864
256 $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
258 // For the atom feed the nickname doesn't matter at all, we only need the item id.
259 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
260 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
266 $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
267 ['$alternate' => $alternate,
268 '$conversation' => $conversation]);
273 $is_remote_contact = false;
277 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
278 foreach ($_SESSION['remote'] as $v) {
279 if ($v['uid'] == $a->profile['uid']) {
280 $contact_id = $v['cid'];
287 $groups = Group::getIdsByContactId($contact_id);
288 $remote_contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $a->profile['uid']]);
289 if (DBA::isResult($remote_contact)) {
290 $contact = $remote_contact;
291 $is_remote_contact = true;
295 if (!$is_remote_contact) {
297 $contact_id = $_SESSION['cid'];
298 $contact = $a->contact;
302 $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
303 if (DBA::isResult($page_contact)) {
304 $a->page_contact = $page_contact;
306 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
308 if (x($a->profile, 'hidewall') && !$is_owner && !$is_remote_contact) {
309 notice(L10n::t('Access to this profile has been restricted.') . EOL);
313 // We need the editor here to be able to reshare an item.
317 'allow_location' => $a->user['allow_location'],
318 'default_location' => $a->user['default-location'],
319 'nickname' => $a->user['nickname'],
320 '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'),
321 'acl' => ACL::getFullSelectorHTML($a->user, true),
323 'visitor' => 'block',
324 'profile_uid' => local_user(),
326 $o .= status_editor($a, $x, 0, true);
329 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['uid'], $is_remote_contact, $groups);
331 if (local_user() && (local_user() == $a->profile['uid'])) {
332 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
333 $unseen = Item::exists($condition);
338 if ($update && !$unseen) {
342 $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, local_user()];
343 $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink'];
344 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
346 if (!DBA::isResult($item)) {
347 notice(L10n::t('Item not found.') . EOL);
351 $item['uri'] = $item['parent-uri'];
354 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
355 Item::update(['unseen' => false], $condition);
359 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
362 $o .= conversation($a, [$item], new Pager($a->query_string), 'display', $update_uid, false, 'commented', local_user());
364 // Preparing the meta header
365 $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
366 $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
367 $author_name = $item["author-name"];
369 $image = $a->removeBaseURL($item["author-avatar"]);
372 $title = $author_name;
375 // Limit the description to 160 characters
376 if (strlen($description) > 160) {
377 $description = substr($description, 0, 157) . '...';
380 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
381 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
382 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
384 //<meta name="keywords" content="">
385 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
386 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
387 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
388 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
390 // Schema.org microdata
391 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
392 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
393 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
394 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
397 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
398 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
399 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
400 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
401 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
404 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
405 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
408 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
409 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
410 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
411 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
412 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
413 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
419 function displayShowFeed($item_id, $conversation)
421 $xml = DFRN::itemFeed($item_id, $conversation);
423 System::httpExit(500);
425 header("Content-type: application/atom+xml");