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;
24 use Friendica\Util\Strings;
25 use Friendica\Module\Objects;
27 function display_init(App $a)
29 if (ActivityPub::isRequest()) {
30 Objects::rawContent();
33 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
37 $nick = (($a->argc > 1) ? $a->argv[1] : '');
41 if (substr($a->argv[2], -5) == '.atom') {
42 $item_id = substr($a->argv[2], 0, -5);
43 displayShowFeed($item_id, false);
48 if ($a->argv[3] == 'conversation.atom') {
49 $item_id = $a->argv[2];
50 displayShowFeed($item_id, true);
55 $item_user = local_user();
57 $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
59 // If there is only one parameter, then check if this parameter could be a guid
63 // Does the local user have this item?
65 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
66 if (DBA::isResult($item)) {
67 $nick = $a->user["nickname"];
69 // Is this item private but could be visible to the remove visitor?
70 } elseif (remote_user()) {
71 $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1]);
72 if (DBA::isResult($item)) {
73 if (!Contact::isFollower(remote_user(), $item['uid'])) {
76 $item_user = $item['uid'];
81 // Is it an item with uid=0?
82 if (!DBA::isResult($item)) {
83 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
85 } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
86 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => [0, 2], 'uid' => 0]);
89 if (!DBA::isResult($item)) {
90 System::httpExit(404);
93 if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
94 Logger::log('Directly serving XML for id '.$item["id"], Logger::DEBUG);
95 displayShowFeed($item["id"], false);
98 if ($item["id"] != $item["parent"]) {
99 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item["parent"]]);
102 $profiledata = display_fetchauthor($a, $item);
104 if (strstr(Strings::normaliseLink($profiledata["url"]), Strings::normaliseLink(System::baseUrl()))) {
105 $nickname = str_replace(Strings::normaliseLink(System::baseUrl())."/profile/", "", Strings::normaliseLink($profiledata["url"]));
107 if (($nickname != $a->user["nickname"])) {
108 $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
109 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
110 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
113 if (DBA::isResult($profile)) {
114 $profiledata = $profile;
116 $profiledata["network"] = Protocol::DFRN;
122 Profile::load($a, $nick, 0, $profiledata);
125 function display_fetchauthor($a, $item)
127 $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
130 $profiledata['uid'] = -1;
131 $profiledata['nickname'] = $author['nick'];
132 $profiledata['name'] = $author['name'];
133 $profiledata['picdate'] = '';
134 $profiledata['photo'] = $author['photo'];
135 $profiledata['url'] = $author['url'];
136 $profiledata['network'] = $author['network'];
138 // Check for a repeated message
140 $body = trim($item["body"]);
142 // Skip if it isn't a pure repeated messages
143 // Does it start with a share?
144 if (!$skip && strpos($body, "[share") > 0) {
147 // Does it end with a share?
148 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
152 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
153 // Skip if there is no shared message in there
154 if ($body == $attributes) {
161 preg_match("/author='(.*?)'/ism", $attributes, $matches);
162 if (!empty($matches[1])) {
163 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
165 preg_match('/author="(.*?)"/ism', $attributes, $matches);
166 if (!empty($matches[1])) {
167 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
170 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
171 if (!empty($matches[1])) {
172 $profiledata["url"] = $matches[1];
174 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
175 if (!empty($matches[1])) {
176 $profiledata["url"] = $matches[1];
179 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
180 if (!empty($matches[1])) {
181 $profiledata["photo"] = $matches[1];
183 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
184 if (!empty($matches[1])) {
185 $profiledata["photo"] = $matches[1];
187 $profiledata["nickname"] = $profiledata["name"];
188 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
190 $profiledata["address"] = "";
191 $profiledata["about"] = "";
194 $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
196 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
199 if (in_array($profiledata["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
200 $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
202 } elseif ($profiledata["network"] == Protocol::DFRN) {
203 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
204 $profiledata["remoteconnect"] = $connect;
207 return($profiledata);
210 function display_content(App $a, $update = false, $update_uid = 0)
212 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
213 notice(L10n::t('Public access denied.') . EOL);
217 require_once 'include/conversation.php';
222 $item_id = $_REQUEST['item_id'];
223 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
224 if ($item['uid'] != 0) {
225 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
227 $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
229 $item_parent = $item['parent'];
230 $item_parent_uri = $item['parent-uri'];
232 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
233 $item_parent = $item_id;
237 $fields = ['id', 'parent', 'parent-uri', 'uid'];
240 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
241 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
242 if (DBA::isResult($item)) {
243 $item_id = $item["id"];
244 $item_parent = $item["parent"];
245 $item_parent_uri = $item['parent-uri'];
247 } elseif (remote_user()) {
248 $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1]);
249 if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
250 $item_id = $item["id"];
251 $item_parent = $item["parent"];
252 $item_parent_uri = $item['parent-uri'];
256 if ($item_parent == 0) {
257 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
258 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
259 if (DBA::isResult($item)) {
260 $item_id = $item["id"];
261 $item_parent = $item["parent"];
262 $item_parent_uri = $item['parent-uri'];
269 System::httpExit(404);
272 // We are displaying an "alternate" link if that post was public. See issue 2864
273 $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
275 // For the atom feed the nickname doesn't matter at all, we only need the item id.
276 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
277 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
283 $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
284 ['$alternate' => $alternate,
285 '$conversation' => $conversation]);
289 $is_remote_contact = false;
290 $item_uid = local_user();
292 $parent = Item::selectFirst(['uid'], ['uri' => $item_parent_uri, 'wall' => true]);
293 if (DBA::isResult($parent)) {
294 $a->profile['uid'] = $parent['uid'];
295 $a->profile['profile_uid'] = $parent['uid'];
296 $is_remote_contact = Contact::isFollower(remote_user(), $a->profile['profile_uid']);
299 if ($is_remote_contact) {
300 $cdata = Contact::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);
301 if (!empty($cdata['user'])) {
302 $groups = Group::getIdsByContactId($cdata['user']);
303 $remote_cid = $cdata['user'];
304 $item_uid = $parent['uid'];
308 $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
309 if (DBA::isResult($page_contact)) {
310 $a->page_contact = $page_contact;
312 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
314 if (!empty($a->profile['hidewall']) && !$is_owner && !$is_remote_contact) {
315 notice(L10n::t('Access to this profile has been restricted.') . EOL);
319 // We need the editor here to be able to reshare an item.
323 'allow_location' => $a->user['allow_location'],
324 'default_location' => $a->user['default-location'],
325 'nickname' => $a->user['nickname'],
326 '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'),
327 'acl' => ACL::getFullSelectorHTML($a->user, true),
329 'visitor' => 'block',
330 'profile_uid' => local_user(),
332 $o .= status_editor($a, $x, 0, true);
334 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid'], $is_remote_contact, $groups, $remote_cid);
336 if (local_user() && (local_user() == $a->profile['profile_uid'])) {
337 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
338 $unseen = Item::exists($condition);
343 if ($update && !$unseen) {
347 $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, $item_uid];
348 $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink'];
349 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
351 if (!DBA::isResult($item)) {
352 System::httpExit(404);
355 $item['uri'] = $item['parent-uri'];
358 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
359 Item::update(['unseen' => false], $condition);
363 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
366 $o .= conversation($a, [$item], new Pager($a->query_string), 'display', $update_uid, false, 'commented', $item_uid);
368 // Preparing the meta header
369 $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
370 $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
371 $author_name = $item["author-name"];
373 $image = $a->removeBaseURL($item["author-avatar"]);
376 $title = $author_name;
379 // Limit the description to 160 characters
380 if (strlen($description) > 160) {
381 $description = substr($description, 0, 157) . '...';
384 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
385 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
386 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
388 //<meta name="keywords" content="">
389 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
390 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
391 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
392 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
394 // Schema.org microdata
395 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
396 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
397 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
398 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
401 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
402 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
403 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
404 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
405 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
408 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
409 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
412 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
413 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
414 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
415 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
416 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
417 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
423 function displayShowFeed($item_id, $conversation)
425 $xml = DFRN::itemFeed($item_id, $conversation);
427 System::httpExit(500);
429 header("Content-type: application/atom+xml");