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\Module\Objects;
23 use Friendica\Protocol\ActivityPub;
24 use Friendica\Protocol\DFRN;
25 use Friendica\Util\Strings;
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] : '');
40 if (substr($a->argv[2], -5) == '.atom') {
41 $item_id = substr($a->argv[2], 0, -5);
42 displayShowFeed($item_id, false);
47 if ($a->argv[3] == 'conversation.atom') {
48 $item_id = $a->argv[2];
49 displayShowFeed($item_id, true);
54 $item_user = local_user();
56 $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
58 // If there is only one parameter, then check if this parameter could be a guid
62 // Does the local user have this item?
64 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
65 if (DBA::isResult($item)) {
66 $nick = $a->user["nickname"];
68 // Is this item private but could be visible to the remove visitor?
69 } elseif (remote_user()) {
70 $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1]);
71 if (DBA::isResult($item)) {
72 if (!Contact::isFollower(remote_user(), $item['uid'])) {
75 $item_user = $item['uid'];
80 // Is it an item with uid=0?
81 if (!DBA::isResult($item)) {
82 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
84 } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
85 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => [0, 2], 'uid' => 0]);
88 if (!DBA::isResult($item)) {
89 System::httpExit(404);
92 if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
93 Logger::log('Directly serving XML for id '.$item["id"], Logger::DEBUG);
94 displayShowFeed($item["id"], false);
97 if ($item["id"] != $item["parent"]) {
98 $item = Item::selectFirstForUser($item_user, $fields, ['id' => $item["parent"]]);
101 $profiledata = display_fetchauthor($a, $item);
103 if (strstr(Strings::normaliseLink($profiledata["url"]), Strings::normaliseLink(System::baseUrl()))) {
104 $nickname = str_replace(Strings::normaliseLink(System::baseUrl())."/profile/", "", Strings::normaliseLink($profiledata["url"]));
106 if (($nickname != $a->user["nickname"])) {
107 $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
108 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
109 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
112 if (DBA::isResult($profile)) {
113 $profiledata = $profile;
115 $profiledata["network"] = Protocol::DFRN;
121 Profile::load($a, $nick, 0, $profiledata);
124 function display_fetchauthor($a, $item)
126 $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
129 $profiledata['uid'] = -1;
130 $profiledata['nickname'] = $author['nick'];
131 $profiledata['name'] = $author['name'];
132 $profiledata['picdate'] = '';
133 $profiledata['photo'] = $author['photo'];
134 $profiledata['url'] = $author['url'];
135 $profiledata['network'] = $author['network'];
137 // Check for a repeated message
139 $body = trim($item["body"]);
141 // Skip if it isn't a pure repeated messages
142 // Does it start with a share?
143 if (!$skip && strpos($body, "[share") > 0) {
146 // Does it end with a share?
147 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
151 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
152 // Skip if there is no shared message in there
153 if ($body == $attributes) {
159 preg_match("/author='(.*?)'/ism", $attributes, $matches);
160 if (!empty($matches[1])) {
161 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
163 preg_match('/author="(.*?)"/ism', $attributes, $matches);
164 if (!empty($matches[1])) {
165 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
167 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
168 if (!empty($matches[1])) {
169 $profiledata["url"] = $matches[1];
171 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
172 if (!empty($matches[1])) {
173 $profiledata["url"] = $matches[1];
175 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
176 if (!empty($matches[1])) {
177 $profiledata["photo"] = $matches[1];
179 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
180 if (!empty($matches[1])) {
181 $profiledata["photo"] = $matches[1];
183 $profiledata["nickname"] = $profiledata["name"];
184 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
186 $profiledata["address"] = "";
187 $profiledata["about"] = "";
190 $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
192 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
195 if (in_array($profiledata["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
196 $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
198 } elseif ($profiledata["network"] == Protocol::DFRN) {
199 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
200 $profiledata["remoteconnect"] = $connect;
203 return($profiledata);
206 function display_content(App $a, $update = false, $update_uid = 0)
208 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
209 notice(L10n::t('Public access denied.') . EOL);
216 $item_id = $_REQUEST['item_id'];
217 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
218 if ($item['uid'] != 0) {
219 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
221 $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
223 $item_parent = $item['parent'];
224 $item_parent_uri = $item['parent-uri'];
226 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
227 $item_parent = $item_id;
231 $fields = ['id', 'parent', 'parent-uri', 'uid'];
234 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
235 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
236 if (DBA::isResult($item)) {
237 $item_id = $item["id"];
238 $item_parent = $item["parent"];
239 $item_parent_uri = $item['parent-uri'];
241 } elseif (remote_user()) {
242 $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1]);
243 if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
244 $item_id = $item["id"];
245 $item_parent = $item["parent"];
246 $item_parent_uri = $item['parent-uri'];
250 if ($item_parent == 0) {
251 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
252 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
253 if (DBA::isResult($item)) {
254 $item_id = $item["id"];
255 $item_parent = $item["parent"];
256 $item_parent_uri = $item['parent-uri'];
263 System::httpExit(404);
266 // We are displaying an "alternate" link if that post was public. See issue 2864
267 $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
269 // For the atom feed the nickname doesn't matter at all, we only need the item id.
270 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
271 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
277 $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
278 ['$alternate' => $alternate,
279 '$conversation' => $conversation]);
283 $is_remote_contact = false;
284 $item_uid = local_user();
286 if (isset($item_parent_uri)) {
287 $parent = Item::selectFirst(['uid'], ['uri' => $item_parent_uri, 'wall' => true]);
288 if (DBA::isResult($parent)) {
289 $a->profile['uid'] = defaults($a->profile, 'uid', $parent['uid']);
290 $a->profile['profile_uid'] = defaults($a->profile, 'profile_uid', $parent['uid']);
291 $is_remote_contact = Contact::isFollower(remote_user(), $a->profile['profile_uid']);
293 if ($is_remote_contact) {
294 $cdata = Contact::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);
295 if (!empty($cdata['user'])) {
296 $groups = Group::getIdsByContactId($cdata['user']);
297 $remote_cid = $cdata['user'];
298 $item_uid = $parent['uid'];
305 $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
306 if (DBA::isResult($page_contact)) {
307 $a->page_contact = $page_contact;
309 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
311 if (!empty($a->profile['hidewall']) && !$is_owner && !$is_remote_contact) {
312 notice(L10n::t('Access to this profile has been restricted.') . EOL);
316 // We need the editor here to be able to reshare an item.
320 'allow_location' => $a->user['allow_location'],
321 'default_location' => $a->user['default-location'],
322 'nickname' => $a->user['nickname'],
323 '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'),
324 'acl' => ACL::getFullSelectorHTML($a->user, true),
326 'visitor' => 'block',
327 'profile_uid' => local_user(),
329 $o .= status_editor($a, $x, 0, true);
331 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid'], $is_remote_contact, $groups, $remote_cid);
333 if (local_user() && (local_user() == $a->profile['profile_uid'])) {
334 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
335 $unseen = Item::exists($condition);
340 if ($update && !$unseen) {
344 $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, $item_uid];
345 $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink'];
346 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
348 if (!DBA::isResult($item)) {
349 System::httpExit(404);
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', $item_uid);
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");