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);
220 $item_id = $_REQUEST['item_id'];
221 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
222 if ($item['uid'] != 0) {
223 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
225 $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
227 $item_parent = $item['parent'];
228 $item_parent_uri = $item['parent-uri'];
230 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
231 $item_parent = $item_id;
235 $fields = ['id', 'parent', 'parent-uri', 'uid'];
238 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
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'];
245 } elseif (remote_user()) {
246 $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1]);
247 if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
248 $item_id = $item["id"];
249 $item_parent = $item["parent"];
250 $item_parent_uri = $item['parent-uri'];
254 if ($item_parent == 0) {
255 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
256 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
257 if (DBA::isResult($item)) {
258 $item_id = $item["id"];
259 $item_parent = $item["parent"];
260 $item_parent_uri = $item['parent-uri'];
267 System::httpExit(404);
270 // We are displaying an "alternate" link if that post was public. See issue 2864
271 $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
273 // For the atom feed the nickname doesn't matter at all, we only need the item id.
274 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
275 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
281 $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
282 ['$alternate' => $alternate,
283 '$conversation' => $conversation]);
287 $is_remote_contact = false;
288 $item_uid = local_user();
290 $parent = Item::selectFirst(['uid'], ['uri' => $item_parent_uri, 'wall' => true]);
291 if (DBA::isResult($parent)) {
292 $a->profile['uid'] = $parent['uid'];
293 $a->profile['profile_uid'] = $parent['uid'];
294 $is_remote_contact = Contact::isFollower(remote_user(), $a->profile['profile_uid']);
297 if ($is_remote_contact) {
298 $cdata = Contact::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);
299 if (!empty($cdata['user'])) {
300 $groups = Group::getIdsByContactId($cdata['user']);
301 $remote_cid = $cdata['user'];
302 $item_uid = $parent['uid'];
306 $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
307 if (DBA::isResult($page_contact)) {
308 $a->page_contact = $page_contact;
310 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
312 if (!empty($a->profile['hidewall']) && !$is_owner && !$is_remote_contact) {
313 notice(L10n::t('Access to this profile has been restricted.') . EOL);
317 // We need the editor here to be able to reshare an item.
321 'allow_location' => $a->user['allow_location'],
322 'default_location' => $a->user['default-location'],
323 'nickname' => $a->user['nickname'],
324 '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'),
325 'acl' => ACL::getFullSelectorHTML($a->user, true),
327 'visitor' => 'block',
328 'profile_uid' => local_user(),
330 $o .= status_editor($a, $x, 0, true);
332 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid'], $is_remote_contact, $groups, $remote_cid);
334 if (local_user() && (local_user() == $a->profile['profile_uid'])) {
335 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
336 $unseen = Item::exists($condition);
341 if ($update && !$unseen) {
345 $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, $item_uid];
346 $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink'];
347 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
349 if (!DBA::isResult($item)) {
350 System::httpExit(404);
353 $item['uri'] = $item['parent-uri'];
356 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
357 Item::update(['unseen' => false], $condition);
361 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
364 $o .= conversation($a, [$item], new Pager($a->query_string), 'display', $update_uid, false, 'commented', $item_uid);
366 // Preparing the meta header
367 $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
368 $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
369 $author_name = $item["author-name"];
371 $image = $a->removeBaseURL($item["author-avatar"]);
374 $title = $author_name;
377 // Limit the description to 160 characters
378 if (strlen($description) > 160) {
379 $description = substr($description, 0, 157) . '...';
382 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
383 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
384 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
386 //<meta name="keywords" content="">
387 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
388 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
389 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
390 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
392 // Schema.org microdata
393 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
394 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
395 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
396 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
399 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
400 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
401 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
402 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
403 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
406 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
407 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
410 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
411 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
412 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
413 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
414 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
415 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
421 function displayShowFeed($item_id, $conversation)
423 $xml = DFRN::itemFeed($item_id, $conversation);
425 System::httpExit(500);
427 header("Content-type: application/atom+xml");