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 if ($item['uid'] != 0) {
215 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
217 $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
219 $item_parent = $item['parent'];
221 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
227 $r = dba::fetch_first("SELECT `id`, `parent` FROM `item`
228 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
229 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
230 if (DBM::is_result($r)) {
232 $item_parent = $r["parent"];
236 if ($item_parent == 0) {
237 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent` FROM `item`
238 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
239 AND NOT `item`.`private` AND `item`.`uid` = 0
240 AND `item`.`guid` = ?", $a->argv[1]);
241 if (DBM::is_result($r)) {
243 $item_parent = $r["parent"];
251 notice(L10n::t('Item not found.').EOL);
255 // We are displaying an "alternate" link if that post was public. See issue 2864
256 $is_public = dba::exists('item', ['id' => $item_id, 'private' => false]);
258 // For the atom feed the nickname doesn't matter at all, we only need the item id.
259 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
260 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
266 $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
267 ['$alternate' => $alternate,
268 '$conversation' => $conversation]);
273 $remote_contact = false;
277 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
278 foreach ($_SESSION['remote'] as $v) {
279 if ($v['uid'] == $a->profile['uid']) {
280 $contact_id = $v['cid'];
287 $groups = Group::getIdsByContactId($contact_id);
288 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
292 if (DBM::is_result($r)) {
294 $remote_contact = true;
298 if (!$remote_contact) {
300 $contact_id = $_SESSION['cid'];
301 $contact = $a->contact;
305 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
306 if (DBM::is_result($r)) {
307 $a->page_contact = $r;
309 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
311 if (x($a->profile, 'hidewall') && !$is_owner && !$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' => populate_acl($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'], $remote_contact, $groups);
335 $r = dba::p("SELECT `id` FROM `item` WHERE
336 `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
337 $sql_extra AND `unseen`",
341 if (dba::num_rows($r) == 0) {
346 $r = dba::p(item_query()."AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
348 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
352 if (!DBM::is_result($r)) {
353 notice(L10n::t('Item not found.') . EOL);
357 $s = dba::inArray($r);
359 if (local_user() && (local_user() == $a->profile['uid'])) {
360 $unseen = dba::selectFirst('item', ['id'], ['parent' => $s[0]['parent'], 'unseen' => true]);
361 if (DBM::is_result($unseen)) {
362 dba::update('item', ['unseen' => false], ['parent' => $s[0]['parent'], 'unseen' => true]);
366 $items = conv_sort($s, "`commented`");
369 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
371 $o .= conversation($a, $items, 'display', $update_uid);
373 // Preparing the meta header
374 require_once 'include/html2plain.php';
376 $description = trim(html2plain(BBCode::convert($s[0]["body"], false), 0, true));
377 $title = trim(html2plain(BBCode::convert($s[0]["title"], false), 0, true));
378 $author_name = $s[0]["author-name"];
380 $image = $a->remove_baseurl($s[0]["author-thumb"]);
383 $title = $author_name;
386 // Limit the description to 160 characters
387 if (strlen($description) > 160) {
388 $description = substr($description, 0, 157) . '...';
391 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
392 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
393 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
395 //<meta name="keywords" content="">
396 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
397 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
398 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
399 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
401 // Schema.org microdata
402 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
403 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
404 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
405 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
408 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
409 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
410 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
411 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
412 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
415 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
416 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
419 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
420 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
421 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
422 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
423 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
424 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
430 function displayShowFeed($item_id, $conversation) {
431 $xml = DFRN::itemFeed($item_id, $conversation);
433 System::httpExit(500);
435 header("Content-type: application/atom+xml");