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 (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
33 $nick = (($a->argc > 1) ? $a->argv[1] : '');
37 if (substr($a->argv[2], -5) == '.atom') {
38 $item_id = substr($a->argv[2], 0, -5);
39 displayShowFeed($item_id, false);
44 if ($a->argv[3] == 'conversation.atom') {
45 $item_id = $a->argv[2];
46 displayShowFeed($item_id, true);
52 $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
54 // If there is only one parameter, then check if this parameter could be a guid
58 // Does the local user have this item?
60 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
61 if (DBA::isResult($item)) {
62 $nick = $a->user["nickname"];
66 // Is it an item with uid=0?
67 if (!DBA::isResult($item)) {
68 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
70 } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
71 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => [0, 2], 'uid' => 0]);
74 if (!DBA::isResult($item)) {
76 notice(L10n::t('Item not found.') . EOL);
80 if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
81 Logger::log('Directly serving XML for id '.$item["id"], Logger::DEBUG);
82 displayShowFeed($item["id"], false);
85 if (ActivityPub::isRequest()) {
86 Objects::rawContent();
89 if ($item["id"] != $item["parent"]) {
90 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item["parent"]]);
93 $profiledata = display_fetchauthor($a, $item);
95 if (strstr(Strings::normaliseLink($profiledata["url"]), Strings::normaliseLink(System::baseUrl()))) {
96 $nickname = str_replace(Strings::normaliseLink(System::baseUrl())."/profile/", "", Strings::normaliseLink($profiledata["url"]));
98 if (($nickname != $a->user["nickname"])) {
99 $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
100 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
101 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
104 if (DBA::isResult($profile)) {
105 $profiledata = $profile;
107 $profiledata["network"] = Protocol::DFRN;
113 Profile::load($a, $nick, 0, $profiledata);
116 function display_fetchauthor($a, $item)
118 $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
121 $profiledata['uid'] = -1;
122 $profiledata['nickname'] = $author['nick'];
123 $profiledata['name'] = $author['name'];
124 $profiledata['picdate'] = '';
125 $profiledata['photo'] = $author['photo'];
126 $profiledata['url'] = $author['url'];
127 $profiledata['network'] = $author['network'];
129 // Check for a repeated message
131 $body = trim($item["body"]);
133 // Skip if it isn't a pure repeated messages
134 // Does it start with a share?
135 if (!$skip && strpos($body, "[share") > 0) {
138 // Does it end with a share?
139 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
143 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
144 // Skip if there is no shared message in there
145 if ($body == $attributes) {
152 preg_match("/author='(.*?)'/ism", $attributes, $matches);
153 if (!empty($matches[1])) {
154 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
156 preg_match('/author="(.*?)"/ism', $attributes, $matches);
157 if (!empty($matches[1])) {
158 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
161 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
162 if (!empty($matches[1])) {
163 $profiledata["url"] = $matches[1];
165 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
166 if (!empty($matches[1])) {
167 $profiledata["url"] = $matches[1];
170 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
171 if (!empty($matches[1])) {
172 $profiledata["photo"] = $matches[1];
174 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
175 if (!empty($matches[1])) {
176 $profiledata["photo"] = $matches[1];
178 $profiledata["nickname"] = $profiledata["name"];
179 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
181 $profiledata["address"] = "";
182 $profiledata["about"] = "";
185 $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
187 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
190 if (in_array($profiledata["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
191 $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
193 } elseif ($profiledata["network"] == Protocol::DFRN) {
194 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
195 $profiledata["remoteconnect"] = $connect;
198 return($profiledata);
201 function display_content(App $a, $update = false, $update_uid = 0)
203 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
204 notice(L10n::t('Public access denied.') . EOL);
208 require_once 'include/conversation.php';
213 $item_id = $_REQUEST['item_id'];
214 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
215 if ($item['uid'] != 0) {
216 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
218 $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
220 $item_parent = $item['parent'];
221 $item_parent_uri = $item['parent-uri'];
223 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
224 $item_parent = $item_id;
228 $fields = ['id', 'parent', 'parent-uri'];
231 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
232 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
233 if (DBA::isResult($item)) {
234 $item_id = $item["id"];
235 $item_parent = $item["parent"];
236 $item_parent_uri = $item['parent-uri'];
240 if ($item_parent == 0) {
241 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
242 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
243 if (DBA::isResult($item)) {
244 $item_id = $item["id"];
245 $item_parent = $item["parent"];
246 $item_parent_uri = $item['parent-uri'];
254 notice(L10n::t('Item not found.').EOL);
258 // We are displaying an "alternate" link if that post was public. See issue 2864
259 $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
261 // For the atom feed the nickname doesn't matter at all, we only need the item id.
262 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
263 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
269 $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
270 ['$alternate' => $alternate,
271 '$conversation' => $conversation]);
275 $parent = Item::selectFirst(['uid'], ['uri' => $item_parent_uri, 'wall' => true]);
276 if (DBA::isResult($parent)) {
277 $a->profile['profile_uid'] = $parent['uid'];
280 $is_remote_contact = Contact::isFollower(remote_user(), $a->profile['profile_uid']);
282 if ($is_remote_contact) {
283 $cdata = Contact::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);
284 if (!empty($cdata['user'])) {
285 $groups = Group::getIdsByContactId($cdata['user']);
289 $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
290 if (DBA::isResult($page_contact)) {
291 $a->page_contact = $page_contact;
293 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
295 if (!empty($a->profile['hidewall']) && !$is_owner && !$is_remote_contact) {
296 notice(L10n::t('Access to this profile has been restricted.') . EOL);
300 // We need the editor here to be able to reshare an item.
304 'allow_location' => $a->user['allow_location'],
305 'default_location' => $a->user['default-location'],
306 'nickname' => $a->user['nickname'],
307 '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'),
308 'acl' => ACL::getFullSelectorHTML($a->user, true),
310 'visitor' => 'block',
311 'profile_uid' => local_user(),
313 $o .= status_editor($a, $x, 0, true);
316 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['uid'], $is_remote_contact, $groups);
318 if (local_user() && (local_user() == $a->profile['uid'])) {
319 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
320 $unseen = Item::exists($condition);
325 if ($update && !$unseen) {
329 $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, local_user()];
330 $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink'];
331 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
333 if (!DBA::isResult($item)) {
334 notice(L10n::t('Item not found.') . EOL);
338 $item['uri'] = $item['parent-uri'];
341 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
342 Item::update(['unseen' => false], $condition);
346 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
349 $o .= conversation($a, [$item], new Pager($a->query_string), 'display', $update_uid, false, 'commented', local_user());
351 // Preparing the meta header
352 $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
353 $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
354 $author_name = $item["author-name"];
356 $image = $a->removeBaseURL($item["author-avatar"]);
359 $title = $author_name;
362 // Limit the description to 160 characters
363 if (strlen($description) > 160) {
364 $description = substr($description, 0, 157) . '...';
367 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
368 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
369 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
371 //<meta name="keywords" content="">
372 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
373 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
374 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
375 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
377 // Schema.org microdata
378 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
379 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
380 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
381 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
384 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
385 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
386 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
387 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
388 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
391 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
392 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
395 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
396 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
397 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
398 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
399 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
400 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
406 function displayShowFeed($item_id, $conversation)
408 $xml = DFRN::itemFeed($item_id, $conversation);
410 System::httpExit(500);
412 header("Content-type: application/atom+xml");