4 use Friendica\Core\Config;
5 use Friendica\Core\System;
6 use Friendica\Protocol\Dfrn;
8 function display_init(App $a) {
10 if ((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
14 $nick = (($a->argc > 1) ? $a->argv[1] : '');
15 $profiledata = array();
18 if (substr($a->argv[2], -5) == '.atom') {
19 $item_id = substr($a->argv[2], 0, -5);
20 displayShowFeed($item_id, false);
25 if ($a->argv[3] == 'conversation.atom') {
26 $item_id = $a->argv[2];
27 displayShowFeed($item_id, true);
31 // If there is only one parameter, then check if this parameter could be a guid
37 // Does the local user have this item?
39 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
40 `author-avatar`, `network`, `body`, `uid`, `owner-link`
41 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
42 AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
43 if (dbm::is_result($r)) {
44 $nick = $a->user["nickname"];
45 $itemuid = local_user();
49 // Is it an item with uid=0?
50 if (!dbm::is_result($r)) {
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 `allow_cid` = '' AND `allow_gid` = ''
55 AND `deny_cid` = '' AND `deny_gid` = ''
56 AND NOT `private` AND `uid` = 0
57 AND `guid` = ? LIMIT 1", $a->argv[1]);
60 // Or is it anywhere on the server?
61 if (!dbm::is_result($r)) {
62 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent`, `item`.`author-name`, `item`.`author-link`,
63 `item`.`author-avatar`, `item`.`network`, `item`.`body`, `item`.`uid`, `item`.`owner-link`
64 FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
65 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
66 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
67 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
68 AND NOT `item`.`private` AND NOT `user`.`hidewall`
69 AND `item`.`guid` = ? LIMIT 1", $a->argv[1]);
72 if (dbm::is_result($r)) {
74 if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
75 logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
76 displayShowFeed($r["id"], false);
79 if ($r["id"] != $r["parent"]) {
80 $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
81 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
82 AND `id` = ?", $r["parent"]);
85 $profiledata = display_fetchauthor($a, $r);
87 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
88 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
90 if (($nickname != $a->user["nickname"])) {
91 $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
92 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
93 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
96 if (dbm::is_result($r)) {
99 $profiledata["network"] = NETWORK_DFRN;
101 $profiledata = array();
106 notice(t('Item not found.') . EOL);
111 profile_load($a, $nick, 0, $profiledata);
114 function display_fetchauthor($a, $item) {
116 require_once("include/Contact.php");
118 $profiledata = array();
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"] = GetProfileUsername($profiledata["url"], "", false, true);
179 $profiledata["address"] = "";
180 $profiledata["about"] = "";
183 $profiledata = get_contact_details_by_url($profiledata["url"], local_user(), $profiledata);
185 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
188 if (in_array($profiledata["network"], array(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 = 0) {
201 if ((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
202 notice(t('Public access denied.') . EOL);
206 require_once('include/security.php');
207 require_once('include/conversation.php');
208 require_once('include/acl_selectors.php');
214 $nick = $_REQUEST['nick'];
216 $nick = (($a->argc > 1) ? $a->argv[1] : '');
220 $item_id = $_REQUEST['item_id'];
221 $a->profile = array('uid' => intval($update), 'profile_uid' => intval($update));
223 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
229 $r = dba::fetch_first("SELECT `id`, `parent` FROM `item`
230 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
231 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
232 if (dbm::is_result($r)) {
234 $item_parent = $r["parent"];
235 $nick = $a->user["nickname"];
240 $r = dba::fetch_first("SELECT `user`.`nickname`, `item`.`id`, `item`.`parent` FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
241 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
242 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
243 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
244 AND NOT `item`.`private` AND NOT `user`.`hidewall`
245 AND `item`.`guid` = ?", $a->argv[1]);
246 if (dbm::is_result($r)) {
248 $item_parent = $r["parent"];
249 $nick = $r["nickname"];
253 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent` FROM `item`
254 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
255 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
256 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
257 AND NOT `item`.`private` AND `item`.`uid` = 0
258 AND `item`.`guid` = ?", $a->argv[1]);
259 if (dbm::is_result($r)) {
261 $item_parent = $r["parent"];
267 if ($item_id && !is_numeric($item_id)) {
268 $r = dba::select('item', array('id', 'parent'), array('uri' => $item_id, 'uid' => $a->profile['uid']), array('limit' => 1));
269 if (dbm::is_result($r)) {
271 $item_parent = $r["parent"];
279 notice(t('Item not found.').EOL);
283 // We are displaying an "alternate" link if that post was public. See issue 2864
284 $is_public = dba::exists('item', array('id' => $item_id, 'private' => false));
286 $alternate = System::baseUrl().'/display/'.$nick.'/'.$item_id.'.atom';
287 $conversation = System::baseUrl().'/display/'.$nick.'/'.$item_parent.'/conversation.atom';
293 $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
294 array('$alternate' => $alternate,
295 '$conversation' => $conversation));
300 $remote_contact = false;
304 if (is_array($_SESSION['remote'])) {
305 foreach ($_SESSION['remote'] as $v) {
306 if ($v['uid'] == $a->profile['uid']) {
307 $contact_id = $v['cid'];
314 $groups = init_groups_visitor($contact_id);
315 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
319 if (dbm::is_result($r)) {
321 $remote_contact = true;
325 if (!$remote_contact) {
327 $contact_id = $_SESSION['cid'];
328 $contact = $a->contact;
332 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
333 if (dbm::is_result($r)) {
334 $a->page_contact = $r;
336 $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
338 if ($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
339 notice(t('Access to this profile has been restricted.') . EOL);
343 // We need the editor here to be able to reshare an item.
348 'allow_location' => $a->user['allow_location'],
349 'default_location' => $a->user['default-location'],
350 'nickname' => $a->user['nickname'],
351 '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'),
352 'acl' => populate_acl($a->user, true),
354 'visitor' => 'block',
355 'profile_uid' => local_user(),
356 'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
358 $o .= status_editor($a,$x,0,true);
361 $sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
364 $r = dba::p("SELECT `id` FROM `item` WHERE `item`.`uid` = ?
365 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
366 $sql_extra AND `unseen`",
371 if (dba::num_rows($r) == 0) {
376 $r = dba::p(item_query()."AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
378 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
382 if (!dbm::is_result($r) && local_user()) {
383 // Check if this is another person's link to a post that we have
384 $r = dba::fetch_first("SELECT `item`.uri FROM `item`
385 WHERE (`item`.`id` = ? OR `item`.`uri` = ?)
390 if (dbm::is_result($r)) {
391 $item_uri = $r['uri'];
393 $r = dba::p(item_query()." AND `item`.`uid` = ?
394 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `uri` = ? AND uid = ?)
395 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
403 if (dbm::is_result($r)) {
404 $s = dba::inArray($r);
406 if ((local_user()) && (local_user() == $a->profile['uid'])) {
407 $unseen = dba::select('item', array('id'), array('parent' => $s[0]['parent'], 'unseen' => true), array('limit' => 1));
408 if (dbm::is_result($unseen)) {
409 dba::update('item', array('unseen' => false), array('parent' => $s[0]['parent'], 'unseen' => true));
413 $items = conv_sort($s, "`commented`");
416 $o .= "<script> var netargs = '?f=&nick=" . $nick . "&item_id=" . $item_id . "'; </script>";
418 $o .= conversation($a, $items, 'display', $update);
420 // Preparing the meta header
421 require_once('include/bbcode.php');
422 require_once("include/html2plain.php");
423 $description = trim(html2plain(bbcode($s[0]["body"], false, false), 0, true));
424 $title = trim(html2plain(bbcode($s[0]["title"], false, false), 0, true));
425 $author_name = $s[0]["author-name"];
427 $image = $a->remove_baseurl($s[0]["author-thumb"]);
430 $title = $author_name;
433 // Limit the description to 160 characters
434 if (strlen($description) > 160) {
435 $description = substr($description, 0, 157) . '...';
438 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
439 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
440 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
442 //<meta name="keywords" content="">
443 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
444 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
445 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
446 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
448 // Schema.org microdata
449 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
450 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
451 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
452 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
455 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
456 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
457 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
458 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
459 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
462 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
463 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
466 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
467 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
468 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
469 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
470 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
471 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
476 $r = dba::fetch_first("SELECT `id`,`deleted` FROM `item` WHERE `id` = ? OR `uri` = ? LIMIT 1",
480 if (dbm::is_result($r)) {
482 notice(t('Item has been removed.') . EOL);
484 notice(t('Permission denied.') . EOL);
487 notice(t('Item not found.') . EOL);
493 function displayShowFeed($item_id, $conversation) {
494 $xml = Dfrn::itemFeed($item_id, $conversation);
496 http_status_exit(500);
498 header("Content-type: application/atom+xml");