3 * @file mod/display.php
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Network;
10 use Friendica\Core\System;
11 use Friendica\Database\DBM;
12 use Friendica\Model\Contact;
13 use Friendica\Model\Group;
14 use Friendica\Model\Profile;
15 use Friendica\Protocol\DFRN;
17 function display_init(App $a)
19 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
23 $nick = (($a->argc > 1) ? $a->argv[1] : '');
27 if (substr($a->argv[2], -5) == '.atom') {
28 $item_id = substr($a->argv[2], 0, -5);
29 displayShowFeed($item_id, false);
34 if ($a->argv[3] == 'conversation.atom') {
35 $item_id = $a->argv[2];
36 displayShowFeed($item_id, true);
42 // If there is only one parameter, then check if this parameter could be a guid
47 // Does the local user have this item?
49 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
50 `author-avatar`, `network`, `body`, `uid`, `owner-link`
51 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
52 AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
53 if (DBM::is_result($r)) {
54 $nick = $a->user["nickname"];
58 // Is it an item with uid=0?
59 if (!DBM::is_result($r)) {
60 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
61 `author-avatar`, `network`, `body`, `uid`, `owner-link`
62 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
63 AND NOT `private` AND `uid` = 0
64 AND `guid` = ? LIMIT 1", $a->argv[1]);
67 if (!DBM::is_result($r)) {
69 notice(L10n::t('Item not found.') . EOL);
72 } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
73 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
74 `author-avatar`, `network`, `body`, `uid`, `owner-link`
75 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
76 AND NOT `private` AND `uid` = 0
77 AND `id` = ? LIMIT 1", $a->argv[2]);
80 if (DBM::is_result($r)) {
81 if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
82 logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
83 displayShowFeed($r["id"], false);
86 if ($r["id"] != $r["parent"]) {
87 $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
88 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
89 AND `id` = ?", $r["parent"]);
92 $profiledata = display_fetchauthor($a, $r);
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 $r = dba::fetch_first("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 (DBM::is_result($r)) {
106 $profiledata["network"] = NETWORK_DFRN;
113 Profile::load($a, $nick, 0, $profiledata);
116 function display_fetchauthor($a, $item) {
118 $profiledata["uid"] = -1;
119 $profiledata["nickname"] = $item["author-name"];
120 $profiledata["name"] = $item["author-name"];
121 $profiledata["picdate"] = "";
122 $profiledata["photo"] = $item["author-avatar"];
123 $profiledata["url"] = $item["author-link"];
124 $profiledata["network"] = $item["network"];
126 // Check for a repeated message
128 $body = trim($item["body"]);
130 // Skip if it isn't a pure repeated messages
131 // Does it start with a share?
132 if (!$skip && strpos($body, "[share") > 0) {
135 // Does it end with a share?
136 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
140 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
141 // Skip if there is no shared message in there
142 if ($body == $attributes) {
149 preg_match("/author='(.*?)'/ism", $attributes, $matches);
150 if ($matches[1] != "") {
151 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
153 preg_match('/author="(.*?)"/ism', $attributes, $matches);
154 if ($matches[1] != "") {
155 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
158 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
159 if ($matches[1] != "") {
160 $profiledata["url"] = $matches[1];
162 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
163 if ($matches[1] != "") {
164 $profiledata["url"] = $matches[1];
167 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
168 if ($matches[1] != "") {
169 $profiledata["photo"] = $matches[1];
171 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
172 if ($matches[1] != "") {
173 $profiledata["photo"] = $matches[1];
175 $profiledata["nickname"] = $profiledata["name"];
176 $profiledata["network"] = Network::matchByProfileUrl($profiledata["url"]);
178 $profiledata["address"] = "";
179 $profiledata["about"] = "";
182 $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
184 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
187 if (in_array($profiledata["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS])) {
188 $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
190 } elseif ($profiledata["network"] == NETWORK_DFRN) {
191 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
192 $profiledata["remoteconnect"] = $connect;
195 return($profiledata);
198 function display_content(App $a, $update = false, $update_uid = 0) {
199 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
200 notice(L10n::t('Public access denied.') . EOL);
204 require_once 'include/security.php';
205 require_once 'include/conversation.php';
206 require_once 'include/acl_selectors.php';
211 $item_id = $_REQUEST['item_id'];
212 $item = dba::selectFirst('item', ['uid', 'parent'], ['id' => $item_id]);
213 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
214 $item_parent = $item['parent'];
216 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
222 $r = dba::fetch_first("SELECT `id`, `parent` FROM `item`
223 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
224 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
225 if (DBM::is_result($r)) {
227 $item_parent = $r["parent"];
231 if ($item_parent == 0) {
232 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent` FROM `item`
233 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
234 AND NOT `item`.`private` AND `item`.`uid` = 0
235 AND `item`.`guid` = ?", $a->argv[1]);
236 if (DBM::is_result($r)) {
238 $item_parent = $r["parent"];
246 notice(L10n::t('Item not found.').EOL);
250 // We are displaying an "alternate" link if that post was public. See issue 2864
251 $is_public = dba::exists('item', ['id' => $item_id, 'private' => false]);
253 // For the atom feed the nickname doesn't matter at all, we only need the item id.
254 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
255 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
261 $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
262 ['$alternate' => $alternate,
263 '$conversation' => $conversation]);
268 $remote_contact = false;
272 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
273 foreach ($_SESSION['remote'] as $v) {
274 if ($v['uid'] == $a->profile['uid']) {
275 $contact_id = $v['cid'];
282 $groups = Group::getIdsByContactId($contact_id);
283 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
287 if (DBM::is_result($r)) {
289 $remote_contact = true;
293 if (!$remote_contact) {
295 $contact_id = $_SESSION['cid'];
296 $contact = $a->contact;
300 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
301 if (DBM::is_result($r)) {
302 $a->page_contact = $r;
304 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
306 if (x($a->profile, 'hidewall') && !$is_owner && !$remote_contact) {
307 notice(L10n::t('Access to this profile has been restricted.') . EOL);
311 // We need the editor here to be able to reshare an item.
315 'allow_location' => $a->user['allow_location'],
316 'default_location' => $a->user['default-location'],
317 'nickname' => $a->user['nickname'],
318 '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'),
319 'acl' => populate_acl($a->user, true),
321 'visitor' => 'block',
322 'profile_uid' => local_user(),
324 $o .= status_editor($a, $x, 0, true);
327 $sql_extra = item_permissions_sql($a->profile['uid'], $remote_contact, $groups);
330 $r = dba::p("SELECT `id` FROM `item` WHERE
331 `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
332 $sql_extra AND `unseen`",
336 if (dba::num_rows($r) == 0) {
341 $r = dba::p(item_query()."AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
343 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
347 if (!DBM::is_result($r)) {
348 notice(L10n::t('Item not found.') . EOL);
352 $s = dba::inArray($r);
354 if (local_user() && (local_user() == $a->profile['uid'])) {
355 $unseen = dba::selectFirst('item', ['id'], ['parent' => $s[0]['parent'], 'unseen' => true]);
356 if (DBM::is_result($unseen)) {
357 dba::update('item', ['unseen' => false], ['parent' => $s[0]['parent'], 'unseen' => true]);
361 $items = conv_sort($s, "`commented`");
364 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
366 $o .= conversation($a, $items, 'display', $update_uid);
368 // Preparing the meta header
369 require_once 'include/bbcode.php';
370 require_once 'include/html2plain.php';
371 $description = trim(html2plain(bbcode($s[0]["body"], false, false), 0, true));
372 $title = trim(html2plain(bbcode($s[0]["title"], false, false), 0, true));
373 $author_name = $s[0]["author-name"];
375 $image = $a->remove_baseurl($s[0]["author-thumb"]);
378 $title = $author_name;
381 // Limit the description to 160 characters
382 if (strlen($description) > 160) {
383 $description = substr($description, 0, 157) . '...';
386 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
387 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
388 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
390 //<meta name="keywords" content="">
391 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
392 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
393 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
394 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
396 // Schema.org microdata
397 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
398 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
399 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
400 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
403 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
404 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
405 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
406 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
407 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
410 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
411 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
414 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
415 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
416 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
417 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
418 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
419 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
425 function displayShowFeed($item_id, $conversation) {
426 $xml = DFRN::itemFeed($item_id, $conversation);
428 System::httpExit(500);
430 header("Content-type: application/atom+xml");