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-id`, `body`, `uid`
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-id`, `body`, `uid`
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-id`, `body`, `uid`
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-id`, `body`, `uid` 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) {
117 $author = dba::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
120 $profiledata['uid'] = -1;
121 $profiledata['nickname'] = $author['nick'];
122 $profiledata['name'] = $author['name'];
123 $profiledata['picdate'] = '';
124 $profiledata['photo'] = $author['photo'];
125 $profiledata['url'] = $author['url'];
126 $profiledata['network'] = $author['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', 'parent-uri'], ['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'];
220 $item_parent_uri = $item['parent-uri'];
222 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
228 $r = dba::fetch_first("SELECT `id`, `parent`, `parent-uri` FROM `item`
229 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
230 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
231 if (DBM::is_result($r)) {
233 $item_parent = $r["parent"];
234 $item_parent_uri = $r['parent-uri'];
238 if ($item_parent == 0) {
239 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent`, `item`.`parent-uri` FROM `item`
240 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
241 AND NOT `item`.`private` AND `item`.`uid` = 0
242 AND `item`.`guid` = ?", $a->argv[1]);
243 if (DBM::is_result($r)) {
245 $item_parent = $r["parent"];
246 $item_parent_uri = $r['parent-uri'];
254 notice(L10n::t('Item not found.').EOL);
258 // We are displaying an "alternate" link if that post was public. See issue 2864
259 $is_public = dba::exists('item', ['id' => $item_id, 'private' => false]);
261 // For the atom feed the nickname doesn't matter at all, we only need the item id.
262 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
263 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
269 $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
270 ['$alternate' => $alternate,
271 '$conversation' => $conversation]);
276 $remote_contact = false;
280 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
281 foreach ($_SESSION['remote'] as $v) {
282 if ($v['uid'] == $a->profile['uid']) {
283 $contact_id = $v['cid'];
290 $groups = Group::getIdsByContactId($contact_id);
291 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
295 if (DBM::is_result($r)) {
297 $remote_contact = true;
301 if (!$remote_contact) {
303 $contact_id = $_SESSION['cid'];
304 $contact = $a->contact;
308 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
309 if (DBM::is_result($r)) {
310 $a->page_contact = $r;
312 $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
314 if (x($a->profile, 'hidewall') && !$is_owner && !$remote_contact) {
315 notice(L10n::t('Access to this profile has been restricted.') . EOL);
319 // We need the editor here to be able to reshare an item.
323 'allow_location' => $a->user['allow_location'],
324 'default_location' => $a->user['default-location'],
325 'nickname' => $a->user['nickname'],
326 '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'),
327 'acl' => ACL::getFullSelectorHTML($a->user, true),
329 'visitor' => 'block',
330 'profile_uid' => local_user(),
332 $o .= status_editor($a, $x, 0, true);
335 $sql_extra = item_permissions_sql($a->profile['uid'], $remote_contact, $groups);
337 if (local_user() && (local_user() == $a->profile['uid'])) {
338 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
339 $unseen = dba::exists('item', $condition);
344 if ($update && !$unseen) {
348 $r = dba::p(item_query(local_user())."AND `item`.`parent-uri` = (SELECT `parent-uri` FROM `item` WHERE `id` = ?)
349 AND `item`.`uid` IN (0, ?) $sql_extra
350 ORDER BY `item`.`uid` ASC, `parent` DESC, `gravity` ASC, `id` ASC",
351 $item_id, local_user()
354 if (!DBM::is_result($r)) {
355 notice(L10n::t('Item not found.') . EOL);
359 $s = dba::inArray($r);
362 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
363 dba::update('item', ['unseen' => false], $condition);
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, false, 'commented', local_user());
373 // Preparing the meta header
374 $description = trim(HTML::toPlaintext(BBCode::convert($s[0]["body"], false), 0, true));
375 $title = trim(HTML::toPlaintext(BBCode::convert($s[0]["title"], false), 0, true));
376 $author_name = $s[0]["author-name"];
378 $image = $a->remove_baseurl($s[0]["author-thumb"]);
381 $title = $author_name;
384 // Limit the description to 160 characters
385 if (strlen($description) > 160) {
386 $description = substr($description, 0, 157) . '...';
389 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
390 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
391 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
393 //<meta name="keywords" content="">
394 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
395 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
396 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
397 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
399 // Schema.org microdata
400 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
401 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
402 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
403 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
406 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
407 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
408 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
409 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
410 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
413 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
414 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
417 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
418 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
419 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
420 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
421 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
422 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
428 function displayShowFeed($item_id, $conversation) {
429 $xml = DFRN::itemFeed($item_id, $conversation);
431 System::httpExit(500);
433 header("Content-type: application/atom+xml");