5 require_once('include/dfrn.php');
7 function display_init(App $a) {
9 if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
13 $nick = (($a->argc > 1) ? $a->argv[1] : '');
14 $profiledata = array();
17 if (substr($a->argv[2], -5) == '.atom') {
18 $item_id = substr($a->argv[2], 0, -5);
19 $xml = dfrn::itemFeed($item_id);
21 http_status_exit(500);
23 header("Content-type: application/atom+xml");
29 // If there is only one parameter, then check if this parameter could be a guid
34 // Does the local user have this item?
36 $r = qu("SELECT `id`, `parent`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
37 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
38 AND `guid` = '%s' AND `uid` = %d", dbesc($a->argv[1]), local_user());
39 if (dbm::is_result($r)) {
40 $nick = $a->user["nickname"];
41 $itemuid = local_user();
45 // Or is it anywhere on the server?
47 $r = qu("SELECT `user`.`nickname`, `item`.`id`, `item`.`parent`, `item`.`author-name`,
48 `item`.`author-link`, `item`.`author-avatar`, `item`.`network`, `item`.`uid`, `item`.`owner-link`, `item`.`body`
49 FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
50 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
51 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
52 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
53 AND NOT `item`.`private` AND NOT `user`.`hidewall`
54 AND `item`.`guid` = '%s'", dbesc($a->argv[1]));
55 if (dbm::is_result($r)) {
56 $nick = $r[0]["nickname"];
57 $itemuid = $r[0]["uid"];
61 // Is it an item with uid=0?
63 $r = qu("SELECT `item`.`id`, `item`.`parent`, `item`.`author-name`, `item`.`author-link`,
64 `item`.`author-avatar`, `item`.`network`, `item`.`uid`, `item`.`owner-link`, `item`.`body`
65 FROM `item` 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 `item`.`uid` = 0
69 AND `item`.`guid` = '%s'", dbesc($a->argv[1]));
71 if (dbm::is_result($r)) {
72 if ($r[0]["id"] != $r[0]["parent"]) {
73 $r = qu("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` = %d", $r[0]["parent"]);
77 if (($itemuid != local_user()) && local_user()) {
78 // Do we know this contact but we haven't got this item?
79 // Copy the wohle thread to our local storage so that we can interact.
80 // We really should change this need for the future since it scales very bad.
81 $contactid = get_contact($r[0]['owner-link'], local_user());
83 $items = qu("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id`", intval($r[0]["id"]));
84 foreach ($items AS $item) {
85 $itemcontactid = get_contact($item['owner-link'], local_user());
86 if (!$itemcontactid) {
87 $itemcontactid = $contactid;
90 $item['uid'] = local_user();
92 $item['contact-id'] = $itemcontactid;
93 $local_copy = item_store($item, false, false, true);
94 logger("Stored local copy for post ".$item['guid']." under id ".$local_copy, LOGGER_DEBUG);
99 $profiledata = display_fetchauthor($a, $r[0]);
101 if (strstr(normalise_link($profiledata["url"]), normalise_link(App::get_baseurl()))) {
102 $nickname = str_replace(normalise_link(App::get_baseurl())."/profile/", "", normalise_link($profiledata["url"]));
104 if (($nickname != $a->user["nickname"])) {
105 $r = qu("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
106 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
107 WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
110 if (dbm::is_result($r)) {
111 $profiledata = $r[0];
113 $profiledata["network"] = NETWORK_DFRN;
115 $profiledata = array();
120 notice(t('Item not found.') . EOL);
125 profile_load($a, $nick, 0, $profiledata);
128 function display_fetchauthor($a, $item) {
130 require_once("include/Contact.php");
132 $profiledata = array();
133 $profiledata["uid"] = -1;
134 $profiledata["nickname"] = $item["author-name"];
135 $profiledata["name"] = $item["author-name"];
136 $profiledata["picdate"] = "";
137 $profiledata["photo"] = $item["author-avatar"];
138 $profiledata["url"] = $item["author-link"];
139 $profiledata["network"] = $item["network"];
141 // Check for a repeated message
143 $body = trim($item["body"]);
145 // Skip if it isn't a pure repeated messages
146 // Does it start with a share?
147 if (!$skip && strpos($body, "[share") > 0) {
150 // Does it end with a share?
151 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
155 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
156 // Skip if there is no shared message in there
157 if ($body == $attributes) {
164 preg_match("/author='(.*?)'/ism", $attributes, $matches);
165 if ($matches[1] != "") {
166 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
168 preg_match('/author="(.*?)"/ism', $attributes, $matches);
169 if ($matches[1] != "") {
170 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
173 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
174 if ($matches[1] != "") {
175 $profiledata["url"] = $matches[1];
177 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
178 if ($matches[1] != "") {
179 $profiledata["url"] = $matches[1];
182 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
183 if ($matches[1] != "") {
184 $profiledata["photo"] = $matches[1];
186 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
187 if ($matches[1] != "") {
188 $profiledata["photo"] = $matches[1];
190 $profiledata["nickname"] = $profiledata["name"];
191 $profiledata["network"] = GetProfileUsername($profiledata["url"], "", false, true);
193 $profiledata["address"] = "";
194 $profiledata["about"] = "";
197 $profiledata = get_contact_details_by_url($profiledata["url"], local_user(), $profiledata);
199 $profiledata["photo"] = App::remove_baseurl($profiledata["photo"]);
202 if (in_array($profiledata["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
203 $profiledata["remoteconnect"] = App::get_baseurl()."/follow?url=".urlencode($profiledata["url"]);
205 } elseif ($profiledata["network"] == NETWORK_DFRN) {
206 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
207 $profiledata["remoteconnect"] = $connect;
210 return($profiledata);
213 function display_content(App $a, $update = 0) {
215 if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
216 notice(t('Public access denied.') . EOL);
220 require_once('include/security.php');
221 require_once('include/conversation.php');
222 require_once('include/acl_selectors.php');
228 $nick = $_REQUEST['nick'];
230 $nick = (($a->argc > 1) ? $a->argv[1] : '');
234 $item_id = $_REQUEST['item_id'];
235 $a->profile = array('uid' => intval($update), 'profile_uid' => intval($update));
237 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
243 $r = qu("SELECT `id` FROM `item`
244 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
245 AND `guid` = '%s' AND `uid` = %d", dbesc($a->argv[1]), local_user());
246 if (dbm::is_result($r)) {
247 $item_id = $r[0]["id"];
248 $nick = $a->user["nickname"];
253 $r = qu("SELECT `user`.`nickname`, `item`.`id` FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
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 NOT `user`.`hidewall`
258 AND `item`.`guid` = '%s'", dbesc($a->argv[1]));
259 // AND NOT `item`.`private` AND `item`.`wall`
260 if (dbm::is_result($r)) {
261 $item_id = $r[0]["id"];
262 $nick = $r[0]["nickname"];
266 $r = qu("SELECT `item`.`id` FROM `item`
267 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
268 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
269 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
270 AND NOT `item`.`private` AND `item`.`uid` = 0
271 AND `item`.`guid` = '%s'", dbesc($a->argv[1]));
272 // AND NOT `item`.`private` AND `item`.`wall`
273 if (dbm::is_result($r)) {
274 $item_id = $r[0]["id"];
280 if ($item_id && !is_numeric($item_id)) {
281 $r = qu("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
282 dbesc($item_id), intval($a->profile['uid']));
283 if (dbm::is_result($r)) {
284 $item_id = $r[0]["id"];
292 notice(t('Item not found.').EOL);
296 // We are displaying an "alternate" link if that post was public. See issue 2864
297 $items = q("SELECT `id` FROM `item` WHERE `id` = %d AND NOT `private` AND `wall`", intval($item_id));
298 if (dbm::is_result($items)) {
299 $alternate = App::get_baseurl().'/display/'.$nick.'/'.$item_id.'.atom';
304 $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
305 array('$alternate' => $alternate));
310 $remote_contact = false;
314 if (is_array($_SESSION['remote'])) {
315 foreach ($_SESSION['remote'] as $v) {
316 if ($v['uid'] == $a->profile['uid']) {
317 $contact_id = $v['cid'];
324 $groups = init_groups_visitor($contact_id);
325 $r = qu("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
327 intval($a->profile['uid'])
329 if (dbm::is_result($r)) {
331 $remote_contact = true;
335 if (!$remote_contact) {
337 $contact_id = $_SESSION['cid'];
338 $contact = $a->contact;
342 $r = qu("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
343 intval($a->profile['uid'])
345 if (dbm::is_result($r)) {
346 $a->page_contact = $r[0];
348 $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
350 if ($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
351 notice(t('Access to this profile has been restricted.') . EOL);
355 // We need the editor here to be able to reshare an item.
360 'allow_location' => $a->user['allow_location'],
361 'default_location' => $a->user['default-location'],
362 'nickname' => $a->user['nickname'],
363 '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'),
364 'acl' => populate_acl($a->user, true),
366 'visitor' => 'block',
367 'profile_uid' => local_user(),
368 'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
370 $o .= status_editor($a,$x,0,true);
373 $sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
377 $r = qu("SELECT `id` FROM `item` WHERE `item`.`uid` = %d
378 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = %d)
379 $sql_extra AND `unseen`",
380 intval($a->profile['uid']),
389 $r = qu(item_query()." AND `item`.`uid` = %d
390 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = %d)
392 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
393 intval($a->profile['uid']),
398 if (!$r && local_user()) {
399 // Check if this is another person's link to a post that we have
400 $r = qu("SELECT `item`.uri FROM `item`
401 WHERE (`item`.`id` = %d OR `item`.`uri` = '%s')
406 if (dbm::is_result($r)) {
407 $item_uri = $r[0]['uri'];
409 $r = qu(item_query()." AND `item`.`uid` = %d
410 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `uri` = '%s' AND uid = %d)
411 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC ",
412 intval(local_user()),
421 if ((local_user()) && (local_user() == $a->profile['uid'])) {
422 $unseen = q("SELECT `id` FROM `item` WHERE `unseen` AND `parent` = %d",
423 intval($r[0]['parent']));
426 q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d AND `unseen`",
427 intval($r[0]['parent'])
432 $items = conv_sort($r,"`commented`");
435 $o .= "<script> var netargs = '?f=&nick=" . $nick . "&item_id=" . $item_id . "'; </script>";
437 $o .= conversation($a,$items,'display', $update);
439 // Preparing the meta header
440 require_once('include/bbcode.php');
441 require_once("include/html2plain.php");
442 $description = trim(html2plain(bbcode($r[0]["body"], false, false), 0, true));
443 $title = trim(html2plain(bbcode($r[0]["title"], false, false), 0, true));
444 $author_name = $r[0]["author-name"];
446 $image = $a->remove_baseurl($r[0]["author-thumb"]);
449 $title = $author_name;
452 // Limit the description to 160 characters
453 if (strlen($description) > 160) {
454 $description = substr($description, 0, 157) . '...';
457 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
458 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
459 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
461 //<meta name="keywords" content="">
462 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
463 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
464 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
465 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
467 // Schema.org microdata
468 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
469 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
470 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
471 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
474 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
475 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
476 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
477 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.$image.'" />'."\n";
478 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$r[0]["plink"].'" />'."\n";
481 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
482 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
485 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
486 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
487 $a->page['htmlhead'] .= '<meta property="og:image" content="'.$image.'" />'."\n";
488 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$r[0]["plink"].'" />'."\n";
489 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
490 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
496 $r = qu("SELECT `id`,`deleted` FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
501 if ($r[0]['deleted']) {
502 notice(t('Item has been removed.') . EOL );
504 notice(t('Permission denied.') . EOL );
507 notice(t('Item not found.') . EOL );