4 use Friendica\Core\System;
6 require_once('include/dfrn.php');
8 function display_init(App $a) {
10 if ((get_config('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);
24 // If there is only one parameter, then check if this parameter could be a guid
30 // Does the local user have this item?
32 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
33 `author-avatar`, `network`, `body`, `uid`, `owner-link`
34 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
35 AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
36 if (dbm::is_result($r)) {
37 $nick = $a->user["nickname"];
38 $itemuid = local_user();
42 // Is it an item with uid=0?
43 if (!dbm::is_result($r)) {
44 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
45 `author-avatar`, `network`, `body`, `uid`, `owner-link`
46 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
47 AND `allow_cid` = '' AND `allow_gid` = ''
48 AND `deny_cid` = '' AND `deny_gid` = ''
49 AND NOT `private` AND `uid` = 0
50 AND `guid` = ? LIMIT 1", $a->argv[1]);
53 // Or is it anywhere on the server?
54 if (!dbm::is_result($r)) {
55 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent`, `item`.`author-name`, `item`.`author-link`,
56 `item`.`author-avatar`, `item`.`network`, `item`.`body`, `item`.`uid`, `item`.`owner-link`
57 FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
58 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
59 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
60 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
61 AND NOT `item`.`private` AND NOT `user`.`hidewall`
62 AND `item`.`guid` = ? LIMIT 1", $a->argv[1]);
65 if (dbm::is_result($r)) {
67 if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
68 logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
69 displayShowFeed($r["id"]);
72 if ($r["id"] != $r["parent"]) {
73 $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
74 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
75 AND `id` = ?", $r["parent"]);
78 $profiledata = display_fetchauthor($a, $r);
80 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
81 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
83 if (($nickname != $a->user["nickname"])) {
84 $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
85 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
86 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
89 if (dbm::is_result($r)) {
92 $profiledata["network"] = NETWORK_DFRN;
94 $profiledata = array();
99 notice(t('Item not found.') . EOL);
104 profile_load($a, $nick, 0, $profiledata);
107 function display_fetchauthor($a, $item) {
109 require_once("include/Contact.php");
111 $profiledata = array();
112 $profiledata["uid"] = -1;
113 $profiledata["nickname"] = $item["author-name"];
114 $profiledata["name"] = $item["author-name"];
115 $profiledata["picdate"] = "";
116 $profiledata["photo"] = $item["author-avatar"];
117 $profiledata["url"] = $item["author-link"];
118 $profiledata["network"] = $item["network"];
120 // Check for a repeated message
122 $body = trim($item["body"]);
124 // Skip if it isn't a pure repeated messages
125 // Does it start with a share?
126 if (!$skip && strpos($body, "[share") > 0) {
129 // Does it end with a share?
130 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
134 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
135 // Skip if there is no shared message in there
136 if ($body == $attributes) {
143 preg_match("/author='(.*?)'/ism", $attributes, $matches);
144 if ($matches[1] != "") {
145 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
147 preg_match('/author="(.*?)"/ism', $attributes, $matches);
148 if ($matches[1] != "") {
149 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
152 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
153 if ($matches[1] != "") {
154 $profiledata["url"] = $matches[1];
156 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
157 if ($matches[1] != "") {
158 $profiledata["url"] = $matches[1];
161 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
162 if ($matches[1] != "") {
163 $profiledata["photo"] = $matches[1];
165 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
166 if ($matches[1] != "") {
167 $profiledata["photo"] = $matches[1];
169 $profiledata["nickname"] = $profiledata["name"];
170 $profiledata["network"] = GetProfileUsername($profiledata["url"], "", false, true);
172 $profiledata["address"] = "";
173 $profiledata["about"] = "";
176 $profiledata = get_contact_details_by_url($profiledata["url"], local_user(), $profiledata);
178 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
181 if (in_array($profiledata["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
182 $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
184 } elseif ($profiledata["network"] == NETWORK_DFRN) {
185 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
186 $profiledata["remoteconnect"] = $connect;
189 return($profiledata);
192 function display_content(App $a, $update = 0) {
194 if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
195 notice(t('Public access denied.') . EOL);
199 require_once('include/security.php');
200 require_once('include/conversation.php');
201 require_once('include/acl_selectors.php');
207 $nick = $_REQUEST['nick'];
209 $nick = (($a->argc > 1) ? $a->argv[1] : '');
213 $item_id = $_REQUEST['item_id'];
214 $a->profile = array('uid' => intval($update), 'profile_uid' => intval($update));
216 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
222 $r = dba::fetch_first("SELECT `id` 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 $nick = $a->user["nickname"];
232 $r = dba::fetch_first("SELECT `user`.`nickname`, `item`.`id` FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
233 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
234 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
235 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
236 AND NOT `item`.`private` AND NOT `user`.`hidewall`
237 AND `item`.`guid` = ?", $a->argv[1]);
238 if (dbm::is_result($r)) {
240 $nick = $r["nickname"];
244 $r = dba::fetch_first("SELECT `item`.`id` FROM `item`
245 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
246 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
247 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
248 AND NOT `item`.`private` AND `item`.`uid` = 0
249 AND `item`.`guid` = ?", $a->argv[1]);
250 if (dbm::is_result($r)) {
257 if ($item_id && !is_numeric($item_id)) {
258 $r = dba::select('item', array('id'), array('uri' => $item_id, 'uid' => $a->profile['uid']), array('limit' => 1));
259 if (dbm::is_result($r)) {
268 notice(t('Item not found.').EOL);
272 // We are displaying an "alternate" link if that post was public. See issue 2864
273 $is_public = dba::exists('item', array('id' => $item_id, 'private' => false));
275 $alternate = System::baseUrl().'/display/'.$nick.'/'.$item_id.'.atom';
280 $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
281 array('$alternate' => $alternate));
286 $remote_contact = false;
290 if (is_array($_SESSION['remote'])) {
291 foreach ($_SESSION['remote'] as $v) {
292 if ($v['uid'] == $a->profile['uid']) {
293 $contact_id = $v['cid'];
300 $groups = init_groups_visitor($contact_id);
301 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
305 if (dbm::is_result($r)) {
307 $remote_contact = true;
311 if (!$remote_contact) {
313 $contact_id = $_SESSION['cid'];
314 $contact = $a->contact;
318 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
319 if (dbm::is_result($r)) {
320 $a->page_contact = $r;
322 $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
324 if ($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
325 notice(t('Access to this profile has been restricted.') . EOL);
329 // We need the editor here to be able to reshare an item.
334 'allow_location' => $a->user['allow_location'],
335 'default_location' => $a->user['default-location'],
336 'nickname' => $a->user['nickname'],
337 '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'),
338 'acl' => populate_acl($a->user, true),
340 'visitor' => 'block',
341 'profile_uid' => local_user(),
342 'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
344 $o .= status_editor($a,$x,0,true);
347 $sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
350 $r = dba::p("SELECT `id` FROM `item` WHERE `item`.`uid` = ?
351 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
352 $sql_extra AND `unseen`",
357 if (dba::num_rows($r) == 0) {
362 $r = dba::p(item_query()."AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
364 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
368 if (!dbm::is_result($r) && local_user()) {
369 // Check if this is another person's link to a post that we have
370 $r = dba::fetch_first("SELECT `item`.uri FROM `item`
371 WHERE (`item`.`id` = ? OR `item`.`uri` = ?)
376 if (dbm::is_result($r)) {
377 $item_uri = $r['uri'];
379 $r = dba::p(item_query()." AND `item`.`uid` = ?
380 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `uri` = ? AND uid = ?)
381 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
389 if (dbm::is_result($r)) {
390 $s = dba::inArray($r);
392 if ((local_user()) && (local_user() == $a->profile['uid'])) {
393 $unseen = dba::select('item', array('id'), array('parent' => $s[0]['parent'], 'unseen' => true), array('limit' => 1));
394 if (dbm::is_result($unseen)) {
395 dba::update('item', array('unseen' => false), array('parent' => $s[0]['parent'], 'unseen' => true));
399 $items = conv_sort($s, "`commented`");
402 $o .= "<script> var netargs = '?f=&nick=" . $nick . "&item_id=" . $item_id . "'; </script>";
404 $o .= conversation($a, $items, 'display', $update);
406 // Preparing the meta header
407 require_once('include/bbcode.php');
408 require_once("include/html2plain.php");
409 $description = trim(html2plain(bbcode($s[0]["body"], false, false), 0, true));
410 $title = trim(html2plain(bbcode($s[0]["title"], false, false), 0, true));
411 $author_name = $s[0]["author-name"];
413 $image = $a->remove_baseurl($s[0]["author-thumb"]);
416 $title = $author_name;
419 // Limit the description to 160 characters
420 if (strlen($description) > 160) {
421 $description = substr($description, 0, 157) . '...';
424 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
425 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
426 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
428 //<meta name="keywords" content="">
429 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
430 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
431 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
432 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
434 // Schema.org microdata
435 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
436 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
437 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
438 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
441 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
442 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
443 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
444 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.$image.'" />'."\n";
445 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
448 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
449 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
452 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
453 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
454 $a->page['htmlhead'] .= '<meta property="og:image" content="'.$image.'" />'."\n";
455 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
456 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
457 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
462 $r = dba::fetch_first("SELECT `id`,`deleted` FROM `item` WHERE `id` = ? OR `uri` = ? LIMIT 1",
466 if (dbm::is_result($r)) {
468 notice(t('Item has been removed.') . EOL);
470 notice(t('Permission denied.') . EOL);
473 notice(t('Item not found.') . EOL);
479 function displayShowFeed($item_id) {
480 $xml = dfrn::itemFeed($item_id);
482 http_status_exit(500);
484 header("Content-type: application/atom+xml");