3 * @file mod/display.php
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Protocol;
11 use Friendica\Core\System;
12 use Friendica\Database\DBM;
13 use Friendica\Model\Contact;
14 use Friendica\Model\Group;
15 use Friendica\Model\Profile;
16 use Friendica\Protocol\DFRN;
18 function display_init(App $a)
20 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
24 $nick = (($a->argc > 1) ? $a->argv[1] : '');
28 if (substr($a->argv[2], -5) == '.atom') {
29 $item_id = substr($a->argv[2], 0, -5);
30 displayShowFeed($item_id, false);
35 if ($a->argv[3] == 'conversation.atom') {
36 $item_id = $a->argv[2];
37 displayShowFeed($item_id, true);
43 // If there is only one parameter, then check if this parameter could be a guid
48 // Does the local user have this item?
50 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
51 `author-avatar`, `network`, `body`, `uid`, `owner-link`
52 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
53 AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
54 if (DBM::is_result($r)) {
55 $nick = $a->user["nickname"];
59 // Is it an item with uid=0?
60 if (!DBM::is_result($r)) {
61 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
62 `author-avatar`, `network`, `body`, `uid`, `owner-link`
63 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
64 AND NOT `private` AND `uid` = 0
65 AND `guid` = ? LIMIT 1", $a->argv[1]);
68 if (!DBM::is_result($r)) {
70 notice(L10n::t('Item not found.') . EOL);
73 } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
74 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
75 `author-avatar`, `network`, `body`, `uid`, `owner-link`
76 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
77 AND NOT `private` AND `uid` = 0
78 AND `id` = ? LIMIT 1", $a->argv[2]);
81 if (DBM::is_result($r)) {
82 if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
83 logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
84 displayShowFeed($r["id"], false);
87 if ($r["id"] != $r["parent"]) {
88 $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
89 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
90 AND `id` = ?", $r["parent"]);
93 $profiledata = display_fetchauthor($a, $r);
95 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
96 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
98 if (($nickname != $a->user["nickname"])) {
99 $r = dba::fetch_first("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 (DBM::is_result($r)) {
107 $profiledata["network"] = NETWORK_DFRN;
114 Profile::load($a, $nick, 0, $profiledata);
117 function display_fetchauthor($a, $item) {
119 $profiledata["uid"] = -1;
120 $profiledata["nickname"] = $item["author-name"];
121 $profiledata["name"] = $item["author-name"];
122 $profiledata["picdate"] = "";
123 $profiledata["photo"] = $item["author-avatar"];
124 $profiledata["url"] = $item["author-link"];
125 $profiledata["network"] = $item["network"];
127 // Check for a repeated message
129 $body = trim($item["body"]);
131 // Skip if it isn't a pure repeated messages
132 // Does it start with a share?
133 if (!$skip && strpos($body, "[share") > 0) {
136 // Does it end with a share?
137 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
141 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
142 // Skip if there is no shared message in there
143 if ($body == $attributes) {
150 preg_match("/author='(.*?)'/ism", $attributes, $matches);
151 if ($matches[1] != "") {
152 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
154 preg_match('/author="(.*?)"/ism', $attributes, $matches);
155 if ($matches[1] != "") {
156 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
159 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
160 if ($matches[1] != "") {
161 $profiledata["url"] = $matches[1];
163 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
164 if ($matches[1] != "") {
165 $profiledata["url"] = $matches[1];
168 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
169 if ($matches[1] != "") {
170 $profiledata["photo"] = $matches[1];
172 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
173 if ($matches[1] != "") {
174 $profiledata["photo"] = $matches[1];
176 $profiledata["nickname"] = $profiledata["name"];
177 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
179 $profiledata["address"] = "";
180 $profiledata["about"] = "";
183 $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
185 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
188 if (in_array($profiledata["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS])) {
189 $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
191 } elseif ($profiledata["network"] == NETWORK_DFRN) {
192 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
193 $profiledata["remoteconnect"] = $connect;
196 return($profiledata);
199 function display_content(App $a, $update = false, $update_uid = 0) {
200 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
201 notice(L10n::t('Public access denied.') . EOL);
205 require_once 'include/security.php';
206 require_once 'include/conversation.php';
207 require_once 'include/acl_selectors.php';
212 $item_id = $_REQUEST['item_id'];
213 $item = dba::selectFirst('item', ['uid', 'parent'], ['id' => $item_id]);
214 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
215 $item_parent = $item['parent'];
217 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
223 $r = dba::fetch_first("SELECT `id`, `parent` FROM `item`
224 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
225 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
226 if (DBM::is_result($r)) {
228 $item_parent = $r["parent"];
232 if ($item_parent == 0) {
233 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent` FROM `item`
234 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
235 AND NOT `item`.`private` AND `item`.`uid` = 0
236 AND `item`.`guid` = ?", $a->argv[1]);
237 if (DBM::is_result($r)) {
239 $item_parent = $r["parent"];
247 notice(L10n::t('Item not found.').EOL);
251 // We are displaying an "alternate" link if that post was public. See issue 2864
252 $is_public = dba::exists('item', ['id' => $item_id, 'private' => false]);
254 // For the atom feed the nickname doesn't matter at all, we only need the item id.
255 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
256 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
262 $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
263 ['$alternate' => $alternate,
264 '$conversation' => $conversation]);
269 $remote_contact = false;
273 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
274 foreach ($_SESSION['remote'] as $v) {
275 if ($v['uid'] == $a->profile['uid']) {
276 $contact_id = $v['cid'];
283 $groups = Group::getIdsByContactId($contact_id);
284 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
288 if (DBM::is_result($r)) {
290 $remote_contact = true;
294 if (!$remote_contact) {
296 $contact_id = $_SESSION['cid'];
297 $contact = $a->contact;
301 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
302 if (DBM::is_result($r)) {
303 $a->page_contact = $r;
305 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
307 if (x($a->profile, 'hidewall') && !$is_owner && !$remote_contact) {
308 notice(L10n::t('Access to this profile has been restricted.') . EOL);
312 // We need the editor here to be able to reshare an item.
316 'allow_location' => $a->user['allow_location'],
317 'default_location' => $a->user['default-location'],
318 'nickname' => $a->user['nickname'],
319 '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'),
320 'acl' => populate_acl($a->user, true),
322 'visitor' => 'block',
323 'profile_uid' => local_user(),
325 $o .= status_editor($a, $x, 0, true);
328 $sql_extra = item_permissions_sql($a->profile['uid'], $remote_contact, $groups);
331 $r = dba::p("SELECT `id` FROM `item` WHERE
332 `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
333 $sql_extra AND `unseen`",
337 if (dba::num_rows($r) == 0) {
342 $r = dba::p(item_query()."AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
344 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
348 if (!DBM::is_result($r)) {
349 notice(L10n::t('Item not found.') . EOL);
353 $s = dba::inArray($r);
355 if (local_user() && (local_user() == $a->profile['uid'])) {
356 $unseen = dba::selectFirst('item', ['id'], ['parent' => $s[0]['parent'], 'unseen' => true]);
357 if (DBM::is_result($unseen)) {
358 dba::update('item', ['unseen' => false], ['parent' => $s[0]['parent'], 'unseen' => true]);
362 $items = conv_sort($s, "`commented`");
365 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
367 $o .= conversation($a, $items, 'display', $update_uid);
369 // Preparing the meta header
370 require_once 'include/html2plain.php';
372 $description = trim(html2plain(BBCode::convert($s[0]["body"], false), 0, true));
373 $title = trim(html2plain(BBCode::convert($s[0]["title"], false), 0, true));
374 $author_name = $s[0]["author-name"];
376 $image = $a->remove_baseurl($s[0]["author-thumb"]);
379 $title = $author_name;
382 // Limit the description to 160 characters
383 if (strlen($description) > 160) {
384 $description = substr($description, 0, 157) . '...';
387 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
388 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
389 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
391 //<meta name="keywords" content="">
392 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
393 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
394 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
395 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
397 // Schema.org microdata
398 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
399 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
400 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
401 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
404 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
405 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
406 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
407 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
408 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
411 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
412 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
415 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
416 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
417 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
418 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
419 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
420 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
426 function displayShowFeed($item_id, $conversation) {
427 $xml = DFRN::itemFeed($item_id, $conversation);
429 System::httpExit(500);
431 header("Content-type: application/atom+xml");