3 * @file mod/display.php
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Content\Text\HTML;
9 use Friendica\Core\ACL;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Group;
17 use Friendica\Model\Item;
18 use Friendica\Model\Profile;
19 use Friendica\Protocol\DFRN;
20 use Friendica\Protocol\ActivityPub;
22 function display_init(App $a)
24 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
28 $nick = (($a->argc > 1) ? $a->argv[1] : '');
32 if (substr($a->argv[2], -5) == '.atom') {
33 $item_id = substr($a->argv[2], 0, -5);
34 displayShowFeed($item_id, false);
39 if ($a->argv[3] == 'conversation.atom') {
40 $item_id = $a->argv[2];
41 displayShowFeed($item_id, true);
47 $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
49 // If there is only one parameter, then check if this parameter could be a guid
53 // Does the local user have this item?
55 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
56 if (DBA::isResult($item)) {
57 $nick = $a->user["nickname"];
61 // Is it an item with uid=0?
62 if (!DBA::isResult($item)) {
63 $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
65 } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
66 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => [0, 2], 'uid' => 0]);
69 if (!DBA::isResult($item)) {
71 notice(L10n::t('Item not found.') . EOL);
75 if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
76 logger('Directly serving XML for id '.$item["id"], LOGGER_DEBUG);
77 displayShowFeed($item["id"], false);
80 if (stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json')) {
81 $wall_item = Item::selectFirst(['id', 'uid'], ['guid' => $item['guid'], 'wall' => true]);
82 if ($wall_item['uid'] == 180) {
83 $data = ActivityPub::createActivityFromItem($wall_item['id']);
84 echo json_encode($data);
88 if ($item["id"] != $item["parent"]) {
89 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item["parent"]]);
92 $profiledata = display_fetchauthor($a, $item);
94 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
95 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
97 if (($nickname != $a->user["nickname"])) {
98 $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
99 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
100 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
103 if (DBA::isResult($profile)) {
104 $profiledata = $profile;
106 $profiledata["network"] = Protocol::DFRN;
112 Profile::load($a, $nick, 0, $profiledata);
115 function display_fetchauthor($a, $item)
117 $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
120 $profiledata['uid'] = -1;
121 $profiledata['nickname'] = $author['nick'];
122 $profiledata['name'] = $author['name'];
123 $profiledata['picdate'] = '';
124 $profiledata['photo'] = $author['photo'];
125 $profiledata['url'] = $author['url'];
126 $profiledata['network'] = $author['network'];
128 // Check for a repeated message
130 $body = trim($item["body"]);
132 // Skip if it isn't a pure repeated messages
133 // Does it start with a share?
134 if (!$skip && strpos($body, "[share") > 0) {
137 // Does it end with a share?
138 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
142 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
143 // Skip if there is no shared message in there
144 if ($body == $attributes) {
151 preg_match("/author='(.*?)'/ism", $attributes, $matches);
152 if (!empty($matches[1])) {
153 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
155 preg_match('/author="(.*?)"/ism', $attributes, $matches);
156 if (!empty($matches[1])) {
157 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
160 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
161 if (!empty($matches[1])) {
162 $profiledata["url"] = $matches[1];
164 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
165 if (!empty($matches[1])) {
166 $profiledata["url"] = $matches[1];
169 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
170 if (!empty($matches[1])) {
171 $profiledata["photo"] = $matches[1];
173 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
174 if (!empty($matches[1])) {
175 $profiledata["photo"] = $matches[1];
177 $profiledata["nickname"] = $profiledata["name"];
178 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
180 $profiledata["address"] = "";
181 $profiledata["about"] = "";
184 $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
186 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
189 if (in_array($profiledata["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
190 $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
192 } elseif ($profiledata["network"] == Protocol::DFRN) {
193 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
194 $profiledata["remoteconnect"] = $connect;
197 return($profiledata);
200 function display_content(App $a, $update = false, $update_uid = 0)
202 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
203 notice(L10n::t('Public access denied.') . EOL);
207 require_once 'include/security.php';
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'] .= replace_macros(get_markup_template('display-head.tpl'),
270 ['$alternate' => $alternate,
271 '$conversation' => $conversation]);
276 $is_remote_contact = false;
280 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
281 foreach ($_SESSION['remote'] as $v) {
282 if ($v['uid'] == $a->profile['uid']) {
283 $contact_id = $v['cid'];
290 $groups = Group::getIdsByContactId($contact_id);
291 $remote_contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $a->profile['uid']]);
292 if (DBA::isResult($remote_contact)) {
293 $contact = $remote_contact;
294 $is_remote_contact = true;
298 if (!$is_remote_contact) {
300 $contact_id = $_SESSION['cid'];
301 $contact = $a->contact;
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 (x($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);
332 $sql_extra = item_permissions_sql($a->profile['uid'], $is_remote_contact, $groups);
334 if (local_user() && (local_user() == $a->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, local_user()];
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 notice(L10n::t('Item not found.') . EOL);
354 $item['uri'] = $item['parent-uri'];
357 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
358 Item::update(['unseen' => false], $condition);
362 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
365 $o .= conversation($a, [$item], 'display', $update_uid, false, 'commented', local_user());
367 // Preparing the meta header
368 $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
369 $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
370 $author_name = $item["author-name"];
372 $image = $a->remove_baseurl($item["author-avatar"]);
375 $title = $author_name;
378 // Limit the description to 160 characters
379 if (strlen($description) > 160) {
380 $description = substr($description, 0, 157) . '...';
383 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
384 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
385 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
387 //<meta name="keywords" content="">
388 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
389 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
390 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
391 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
393 // Schema.org microdata
394 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
395 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
396 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
397 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
400 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
401 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
402 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
403 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
404 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
407 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
408 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
411 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
412 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
413 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
414 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
415 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
416 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
422 function displayShowFeed($item_id, $conversation)
424 $xml = DFRN::itemFeed($item_id, $conversation);
426 System::httpExit(500);
428 header("Content-type: application/atom+xml");