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\DBM;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Group;
17 use Friendica\Model\Profile;
18 use Friendica\Protocol\DFRN;
20 function display_init(App $a)
22 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
26 $nick = (($a->argc > 1) ? $a->argv[1] : '');
30 if (substr($a->argv[2], -5) == '.atom') {
31 $item_id = substr($a->argv[2], 0, -5);
32 displayShowFeed($item_id, false);
37 if ($a->argv[3] == 'conversation.atom') {
38 $item_id = $a->argv[2];
39 displayShowFeed($item_id, true);
45 // If there is only one parameter, then check if this parameter could be a guid
50 // Does the local user have this item?
52 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
53 `author-avatar`, `network`, `body`, `uid`, `owner-link`
54 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
55 AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
56 if (DBM::is_result($r)) {
57 $nick = $a->user["nickname"];
61 // Is it an item with uid=0?
62 if (!DBM::is_result($r)) {
63 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
64 `author-avatar`, `network`, `body`, `uid`, `owner-link`
65 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
66 AND NOT `private` AND `uid` = 0
67 AND `guid` = ? LIMIT 1", $a->argv[1]);
70 if (!DBM::is_result($r)) {
72 notice(L10n::t('Item not found.') . EOL);
75 } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
76 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
77 `author-avatar`, `network`, `body`, `uid`, `owner-link`
78 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
79 AND NOT `private` AND `uid` = 0
80 AND `id` = ? LIMIT 1", $a->argv[2]);
83 if (DBM::is_result($r)) {
84 if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
85 logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
86 displayShowFeed($r["id"], false);
89 if ($r["id"] != $r["parent"]) {
90 $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
91 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
92 AND `id` = ?", $r["parent"]);
95 $profiledata = display_fetchauthor($a, $r);
97 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
98 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
100 if (($nickname != $a->user["nickname"])) {
101 $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
102 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
103 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
106 if (DBM::is_result($r)) {
109 $profiledata["network"] = NETWORK_DFRN;
116 Profile::load($a, $nick, 0, $profiledata);
119 function display_fetchauthor($a, $item) {
121 $profiledata["uid"] = -1;
122 $profiledata["nickname"] = $item["author-name"];
123 $profiledata["name"] = $item["author-name"];
124 $profiledata["picdate"] = "";
125 $profiledata["photo"] = $item["author-avatar"];
126 $profiledata["url"] = $item["author-link"];
127 $profiledata["network"] = $item["network"];
129 // Check for a repeated message
131 $body = trim($item["body"]);
133 // Skip if it isn't a pure repeated messages
134 // Does it start with a share?
135 if (!$skip && strpos($body, "[share") > 0) {
138 // Does it end with a share?
139 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
143 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
144 // Skip if there is no shared message in there
145 if ($body == $attributes) {
152 preg_match("/author='(.*?)'/ism", $attributes, $matches);
153 if ($matches[1] != "") {
154 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
156 preg_match('/author="(.*?)"/ism', $attributes, $matches);
157 if ($matches[1] != "") {
158 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
161 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
162 if ($matches[1] != "") {
163 $profiledata["url"] = $matches[1];
165 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
166 if ($matches[1] != "") {
167 $profiledata["url"] = $matches[1];
170 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
171 if ($matches[1] != "") {
172 $profiledata["photo"] = $matches[1];
174 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
175 if ($matches[1] != "") {
176 $profiledata["photo"] = $matches[1];
178 $profiledata["nickname"] = $profiledata["name"];
179 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
181 $profiledata["address"] = "";
182 $profiledata["about"] = "";
185 $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
187 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
190 if (in_array($profiledata["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS])) {
191 $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
193 } elseif ($profiledata["network"] == NETWORK_DFRN) {
194 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
195 $profiledata["remoteconnect"] = $connect;
198 return($profiledata);
201 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 = dba::selectFirst('item', ['uid', 'parent'], ['id' => $item_id]);
215 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
216 $item_parent = $item['parent'];
218 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
224 $r = dba::fetch_first("SELECT `id`, `parent` FROM `item`
225 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
226 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
227 if (DBM::is_result($r)) {
229 $item_parent = $r["parent"];
233 if ($item_parent == 0) {
234 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent` FROM `item`
235 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
236 AND NOT `item`.`private` AND `item`.`uid` = 0
237 AND `item`.`guid` = ?", $a->argv[1]);
238 if (DBM::is_result($r)) {
240 $item_parent = $r["parent"];
248 notice(L10n::t('Item not found.').EOL);
252 // We are displaying an "alternate" link if that post was public. See issue 2864
253 $is_public = dba::exists('item', ['id' => $item_id, 'private' => false]);
255 // For the atom feed the nickname doesn't matter at all, we only need the item id.
256 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
257 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
263 $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
264 ['$alternate' => $alternate,
265 '$conversation' => $conversation]);
270 $remote_contact = false;
274 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
275 foreach ($_SESSION['remote'] as $v) {
276 if ($v['uid'] == $a->profile['uid']) {
277 $contact_id = $v['cid'];
284 $groups = Group::getIdsByContactId($contact_id);
285 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
289 if (DBM::is_result($r)) {
291 $remote_contact = true;
295 if (!$remote_contact) {
297 $contact_id = $_SESSION['cid'];
298 $contact = $a->contact;
302 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
303 if (DBM::is_result($r)) {
304 $a->page_contact = $r;
306 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
308 if (x($a->profile, 'hidewall') && !$is_owner && !$remote_contact) {
309 notice(L10n::t('Access to this profile has been restricted.') . EOL);
313 // We need the editor here to be able to reshare an item.
317 'allow_location' => $a->user['allow_location'],
318 'default_location' => $a->user['default-location'],
319 'nickname' => $a->user['nickname'],
320 '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'),
321 'acl' => ACL::getFullSelectorHTML($a->user, true),
323 'visitor' => 'block',
324 'profile_uid' => local_user(),
326 $o .= status_editor($a, $x, 0, true);
329 $sql_extra = item_permissions_sql($a->profile['uid'], $remote_contact, $groups);
332 $r = dba::p("SELECT `id` FROM `item` WHERE
333 `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
334 $sql_extra AND `unseen`",
338 if (dba::num_rows($r) == 0) {
343 $r = dba::p(item_query()."AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
345 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
349 if (!DBM::is_result($r)) {
350 notice(L10n::t('Item not found.') . EOL);
354 $s = dba::inArray($r);
356 if (local_user() && (local_user() == $a->profile['uid'])) {
357 $unseen = dba::selectFirst('item', ['id'], ['parent' => $s[0]['parent'], 'unseen' => true]);
358 if (DBM::is_result($unseen)) {
359 dba::update('item', ['unseen' => false], ['parent' => $s[0]['parent'], 'unseen' => true]);
363 $items = conv_sort($s, "`commented`");
366 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
368 $o .= conversation($a, $items, 'display', $update_uid);
370 // Preparing the meta header
371 require_once 'include/html2plain.php';
373 $description = trim(HTML::toPlaintext(BBCode::convert($s[0]["body"], false), 0, true));
374 $title = trim(HTML::toPlaintext(BBCode::convert($s[0]["title"], false), 0, true));
375 $author_name = $s[0]["author-name"];
377 $image = $a->remove_baseurl($s[0]["author-thumb"]);
380 $title = $author_name;
383 // Limit the description to 160 characters
384 if (strlen($description) > 160) {
385 $description = substr($description, 0, 157) . '...';
388 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
389 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
390 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
392 //<meta name="keywords" content="">
393 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
394 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
395 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
396 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
398 // Schema.org microdata
399 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
400 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
401 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
402 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
405 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
406 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
407 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
408 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
409 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
412 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
413 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
416 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
417 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
418 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
419 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
420 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
421 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
427 function displayShowFeed($item_id, $conversation) {
428 $xml = DFRN::itemFeed($item_id, $conversation);
430 System::httpExit(500);
432 header("Content-type: application/atom+xml");