4 use Friendica\Core\Config;
5 use Friendica\Core\System;
6 use Friendica\Database\DBM;
7 use Friendica\Object\Contact;
8 use Friendica\Protocol\DFRN;
10 function display_init(App $a) {
12 if ((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
16 $nick = (($a->argc > 1) ? $a->argv[1] : '');
17 $profiledata = array();
20 if (substr($a->argv[2], -5) == '.atom') {
21 $item_id = substr($a->argv[2], 0, -5);
22 displayShowFeed($item_id, false);
27 if ($a->argv[3] == 'conversation.atom') {
28 $item_id = $a->argv[2];
29 displayShowFeed($item_id, true);
33 // If there is only one parameter, then check if this parameter could be a guid
39 // Does the local user have this item?
41 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
42 `author-avatar`, `network`, `body`, `uid`, `owner-link`
43 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
44 AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
45 if (DBM::is_result($r)) {
46 $nick = $a->user["nickname"];
47 $itemuid = local_user();
51 // Is it an item with uid=0?
52 if (!DBM::is_result($r)) {
53 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
54 `author-avatar`, `network`, `body`, `uid`, `owner-link`
55 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
56 AND `allow_cid` = '' AND `allow_gid` = ''
57 AND `deny_cid` = '' AND `deny_gid` = ''
58 AND NOT `private` AND `uid` = 0
59 AND `guid` = ? LIMIT 1", $a->argv[1]);
62 // Or is it anywhere on the server?
63 if (!DBM::is_result($r)) {
64 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent`, `item`.`author-name`, `item`.`author-link`,
65 `item`.`author-avatar`, `item`.`network`, `item`.`body`, `item`.`uid`, `item`.`owner-link`
66 FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
67 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
68 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
69 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
70 AND NOT `item`.`private` AND NOT `user`.`hidewall`
71 AND `item`.`guid` = ? LIMIT 1", $a->argv[1]);
74 if (DBM::is_result($r)) {
76 if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
77 logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
78 displayShowFeed($r["id"], false);
81 if ($r["id"] != $r["parent"]) {
82 $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
83 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
84 AND `id` = ?", $r["parent"]);
87 $profiledata = display_fetchauthor($a, $r);
89 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
90 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
92 if (($nickname != $a->user["nickname"])) {
93 $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
94 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
95 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
98 if (DBM::is_result($r)) {
101 $profiledata["network"] = NETWORK_DFRN;
103 $profiledata = array();
108 notice(t('Item not found.') . EOL);
113 profile_load($a, $nick, 0, $profiledata);
116 function display_fetchauthor($a, $item) {
118 require_once("include/Contact.php");
120 $profiledata = array();
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"] = GetProfileUsername($profiledata["url"], "", false, true);
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"], array(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 = 0) {
203 if ((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
204 notice(t('Public access denied.') . EOL);
208 require_once('include/security.php');
209 require_once('include/conversation.php');
210 require_once('include/acl_selectors.php');
216 $nick = $_REQUEST['nick'];
218 $nick = (($a->argc > 1) ? $a->argv[1] : '');
222 $item_id = $_REQUEST['item_id'];
223 $a->profile = array('uid' => intval($update), 'profile_uid' => intval($update));
225 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
231 $r = dba::fetch_first("SELECT `id`, `parent` FROM `item`
232 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
233 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
234 if (DBM::is_result($r)) {
236 $item_parent = $r["parent"];
237 $nick = $a->user["nickname"];
242 $r = dba::fetch_first("SELECT `user`.`nickname`, `item`.`id`, `item`.`parent` FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
243 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
244 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
245 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
246 AND NOT `item`.`private` AND NOT `user`.`hidewall`
247 AND `item`.`guid` = ?", $a->argv[1]);
248 if (DBM::is_result($r)) {
250 $item_parent = $r["parent"];
251 $nick = $r["nickname"];
255 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent` FROM `item`
256 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
257 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
258 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
259 AND NOT `item`.`private` AND `item`.`uid` = 0
260 AND `item`.`guid` = ?", $a->argv[1]);
261 if (DBM::is_result($r)) {
263 $item_parent = $r["parent"];
269 if ($item_id && !is_numeric($item_id)) {
270 $r = dba::select('item', array('id', 'parent'), array('uri' => $item_id, 'uid' => $a->profile['uid']), array('limit' => 1));
271 if (DBM::is_result($r)) {
273 $item_parent = $r["parent"];
281 notice(t('Item not found.').EOL);
285 // We are displaying an "alternate" link if that post was public. See issue 2864
286 $is_public = dba::exists('item', array('id' => $item_id, 'private' => false));
288 $alternate = System::baseUrl().'/display/'.$nick.'/'.$item_id.'.atom';
289 $conversation = System::baseUrl().'/display/'.$nick.'/'.$item_parent.'/conversation.atom';
295 $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
296 array('$alternate' => $alternate,
297 '$conversation' => $conversation));
302 $remote_contact = false;
306 if (is_array($_SESSION['remote'])) {
307 foreach ($_SESSION['remote'] as $v) {
308 if ($v['uid'] == $a->profile['uid']) {
309 $contact_id = $v['cid'];
316 $groups = init_groups_visitor($contact_id);
317 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
321 if (DBM::is_result($r)) {
323 $remote_contact = true;
327 if (!$remote_contact) {
329 $contact_id = $_SESSION['cid'];
330 $contact = $a->contact;
334 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
335 if (DBM::is_result($r)) {
336 $a->page_contact = $r;
338 $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
340 if ($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
341 notice(t('Access to this profile has been restricted.') . EOL);
345 // We need the editor here to be able to reshare an item.
350 'allow_location' => $a->user['allow_location'],
351 'default_location' => $a->user['default-location'],
352 'nickname' => $a->user['nickname'],
353 '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'),
354 'acl' => populate_acl($a->user, true),
356 'visitor' => 'block',
357 'profile_uid' => local_user(),
358 'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
360 $o .= status_editor($a,$x,0,true);
363 $sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
366 $r = dba::p("SELECT `id` FROM `item` WHERE `item`.`uid` = ?
367 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
368 $sql_extra AND `unseen`",
373 if (dba::num_rows($r) == 0) {
378 $r = dba::p(item_query()."AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
380 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
384 if (!DBM::is_result($r) && local_user()) {
385 // Check if this is another person's link to a post that we have
386 $r = dba::fetch_first("SELECT `item`.uri FROM `item`
387 WHERE (`item`.`id` = ? OR `item`.`uri` = ?)
392 if (DBM::is_result($r)) {
393 $item_uri = $r['uri'];
395 $r = dba::p(item_query()." AND `item`.`uid` = ?
396 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `uri` = ? AND uid = ?)
397 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
405 if (DBM::is_result($r)) {
406 $s = dba::inArray($r);
408 if ((local_user()) && (local_user() == $a->profile['uid'])) {
409 $unseen = dba::select('item', array('id'), array('parent' => $s[0]['parent'], 'unseen' => true), array('limit' => 1));
410 if (DBM::is_result($unseen)) {
411 dba::update('item', array('unseen' => false), array('parent' => $s[0]['parent'], 'unseen' => true));
415 $items = conv_sort($s, "`commented`");
418 $o .= "<script> var netargs = '?f=&nick=" . $nick . "&item_id=" . $item_id . "'; </script>";
420 $o .= conversation($a, $items, 'display', $update);
422 // Preparing the meta header
423 require_once('include/bbcode.php');
424 require_once("include/html2plain.php");
425 $description = trim(html2plain(bbcode($s[0]["body"], false, false), 0, true));
426 $title = trim(html2plain(bbcode($s[0]["title"], false, false), 0, true));
427 $author_name = $s[0]["author-name"];
429 $image = $a->remove_baseurl($s[0]["author-thumb"]);
432 $title = $author_name;
435 // Limit the description to 160 characters
436 if (strlen($description) > 160) {
437 $description = substr($description, 0, 157) . '...';
440 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
441 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
442 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
444 //<meta name="keywords" content="">
445 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
446 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
447 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
448 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
450 // Schema.org microdata
451 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
452 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
453 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
454 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
457 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
458 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
459 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
460 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
461 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
464 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
465 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
468 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
469 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
470 $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
471 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
472 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
473 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
478 $r = dba::fetch_first("SELECT `id`,`deleted` FROM `item` WHERE `id` = ? OR `uri` = ? LIMIT 1",
482 if (DBM::is_result($r)) {
484 notice(t('Item has been removed.') . EOL);
486 notice(t('Permission denied.') . EOL);
489 notice(t('Item not found.') . EOL);
495 function displayShowFeed($item_id, $conversation) {
496 $xml = DFRN::itemFeed($item_id, $conversation);
498 http_status_exit(500);
500 header("Content-type: application/atom+xml");