3 * @file mod/display.php
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\ACL;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Protocol;
12 use Friendica\Core\System;
13 use Friendica\Database\DBM;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Group;
16 use Friendica\Model\Profile;
17 use Friendica\Protocol\DFRN;
19 function display_init(App $a)
21 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
25 $nick = (($a->argc > 1) ? $a->argv[1] : '');
29 if (substr($a->argv[2], -5) == '.atom') {
30 $item_id = substr($a->argv[2], 0, -5);
31 displayShowFeed($item_id, false);
36 if ($a->argv[3] == 'conversation.atom') {
37 $item_id = $a->argv[2];
38 displayShowFeed($item_id, true);
44 // If there is only one parameter, then check if this parameter could be a guid
49 // Does the local user have this item?
51 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
52 `author-avatar`, `network`, `body`, `uid`, `owner-link`
53 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
54 AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
55 if (DBM::is_result($r)) {
56 $nick = $a->user["nickname"];
60 // Is it an item with uid=0?
61 if (!DBM::is_result($r)) {
62 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
63 `author-avatar`, `network`, `body`, `uid`, `owner-link`
64 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
65 AND NOT `private` AND `uid` = 0
66 AND `guid` = ? LIMIT 1", $a->argv[1]);
69 if (!DBM::is_result($r)) {
71 notice(L10n::t('Item not found.') . EOL);
74 } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
75 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
76 `author-avatar`, `network`, `body`, `uid`, `owner-link`
77 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
78 AND NOT `private` AND `uid` = 0
79 AND `id` = ? LIMIT 1", $a->argv[2]);
82 if (DBM::is_result($r)) {
83 if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
84 logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
85 displayShowFeed($r["id"], false);
88 if ($r["id"] != $r["parent"]) {
89 $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
90 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
91 AND `id` = ?", $r["parent"]);
94 $profiledata = display_fetchauthor($a, $r);
96 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
97 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
99 if (($nickname != $a->user["nickname"])) {
100 $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
101 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
102 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
105 if (DBM::is_result($r)) {
108 $profiledata["network"] = NETWORK_DFRN;
115 Profile::load($a, $nick, 0, $profiledata);
118 function display_fetchauthor($a, $item) {
120 $profiledata["uid"] = -1;
121 $profiledata["nickname"] = $item["author-name"];
122 $profiledata["name"] = $item["author-name"];
123 $profiledata["picdate"] = "";
124 $profiledata["photo"] = $item["author-avatar"];
125 $profiledata["url"] = $item["author-link"];
126 $profiledata["network"] = $item["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 ($matches[1] != "") {
153 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
155 preg_match('/author="(.*?)"/ism', $attributes, $matches);
156 if ($matches[1] != "") {
157 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
160 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
161 if ($matches[1] != "") {
162 $profiledata["url"] = $matches[1];
164 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
165 if ($matches[1] != "") {
166 $profiledata["url"] = $matches[1];
169 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
170 if ($matches[1] != "") {
171 $profiledata["photo"] = $matches[1];
173 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
174 if ($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"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS])) {
190 $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
192 } elseif ($profiledata["network"] == NETWORK_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) {
201 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
202 notice(L10n::t('Public access denied.') . EOL);
206 require_once 'include/security.php';
207 require_once 'include/conversation.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' => ACL::getFullSelectorHTML($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");