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 = dba::fetch_first("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` = ? AND `uid` = ? LIMIT 1", $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 = dba::fetch_first("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` = ? LIMIT 1", $a->argv[1]);
55 if (dbm::is_result($r)) {
56 $nick = $r["nickname"];
61 // Is it an item with uid=0?
63 $r = dba::fetch_first("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` = ? LIMIT 1", $a->argv[1]);
71 if (dbm::is_result($r)) {
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"]);
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['owner-link'], local_user());
83 $items = dba::p("SELECT * FROM `item` WHERE `parent` = ? ORDER BY `id`", $r["id"]);
84 while ($item = dba::fetch($items)) {
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);
100 $profiledata = display_fetchauthor($a, $r);
102 if (strstr(normalise_link($profiledata["url"]), normalise_link(App::get_baseurl()))) {
103 $nickname = str_replace(normalise_link(App::get_baseurl())."/profile/", "", normalise_link($profiledata["url"]));
105 if (($nickname != $a->user["nickname"])) {
106 $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
107 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
108 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
111 if (dbm::is_result($r)) {
114 $profiledata["network"] = NETWORK_DFRN;
116 $profiledata = array();
121 notice(t('Item not found.') . EOL);
126 profile_load($a, $nick, 0, $profiledata);
129 function display_fetchauthor($a, $item) {
131 require_once("include/Contact.php");
133 $profiledata = array();
134 $profiledata["uid"] = -1;
135 $profiledata["nickname"] = $item["author-name"];
136 $profiledata["name"] = $item["author-name"];
137 $profiledata["picdate"] = "";
138 $profiledata["photo"] = $item["author-avatar"];
139 $profiledata["url"] = $item["author-link"];
140 $profiledata["network"] = $item["network"];
142 // Check for a repeated message
144 $body = trim($item["body"]);
146 // Skip if it isn't a pure repeated messages
147 // Does it start with a share?
148 if (!$skip && strpos($body, "[share") > 0) {
151 // Does it end with a share?
152 if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
156 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
157 // Skip if there is no shared message in there
158 if ($body == $attributes) {
165 preg_match("/author='(.*?)'/ism", $attributes, $matches);
166 if ($matches[1] != "") {
167 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
169 preg_match('/author="(.*?)"/ism', $attributes, $matches);
170 if ($matches[1] != "") {
171 $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
174 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
175 if ($matches[1] != "") {
176 $profiledata["url"] = $matches[1];
178 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
179 if ($matches[1] != "") {
180 $profiledata["url"] = $matches[1];
183 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
184 if ($matches[1] != "") {
185 $profiledata["photo"] = $matches[1];
187 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
188 if ($matches[1] != "") {
189 $profiledata["photo"] = $matches[1];
191 $profiledata["nickname"] = $profiledata["name"];
192 $profiledata["network"] = GetProfileUsername($profiledata["url"], "", false, true);
194 $profiledata["address"] = "";
195 $profiledata["about"] = "";
198 $profiledata = get_contact_details_by_url($profiledata["url"], local_user(), $profiledata);
200 $profiledata["photo"] = App::remove_baseurl($profiledata["photo"]);
203 if (in_array($profiledata["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
204 $profiledata["remoteconnect"] = App::get_baseurl()."/follow?url=".urlencode($profiledata["url"]);
206 } elseif ($profiledata["network"] == NETWORK_DFRN) {
207 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
208 $profiledata["remoteconnect"] = $connect;
211 return($profiledata);
214 function display_content(App $a, $update = 0) {
216 if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
217 notice(t('Public access denied.') . EOL);
221 require_once('include/security.php');
222 require_once('include/conversation.php');
223 require_once('include/acl_selectors.php');
229 $nick = $_REQUEST['nick'];
231 $nick = (($a->argc > 1) ? $a->argv[1] : '');
235 $item_id = $_REQUEST['item_id'];
236 $a->profile = array('uid' => intval($update), 'profile_uid' => intval($update));
238 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
244 $r = dba::fetch_first("SELECT `id` FROM `item`
245 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
246 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
247 if (dbm::is_result($r)) {
249 $nick = $a->user["nickname"];
254 $r = dba::fetch_first("SELECT `user`.`nickname`, `item`.`id` FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
255 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
256 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
257 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
258 AND NOT `item`.`private` AND NOT `user`.`hidewall`
259 AND `item`.`guid` = ?", $a->argv[1]);
260 if (dbm::is_result($r)) {
262 $nick = $r["nickname"];
266 $r = dba::fetch_first("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` = ?", $a->argv[1]);
272 if (dbm::is_result($r)) {
279 if ($item_id && !is_numeric($item_id)) {
280 $r = dba::select('item', array('id'), array('uri' => $item_id, 'uid' => $a->profile['uid']), array('limit' => 1));
281 if (dbm::is_result($r)) {
290 notice(t('Item not found.').EOL);
294 // We are displaying an "alternate" link if that post was public. See issue 2864
295 $is_public = dba::exists('item', array('id' => $item_id, 'private' => false, 'wall' => true));
297 $alternate = App::get_baseurl().'/display/'.$nick.'/'.$item_id.'.atom';
302 $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
303 array('$alternate' => $alternate));
308 $remote_contact = false;
312 if (is_array($_SESSION['remote'])) {
313 foreach ($_SESSION['remote'] as $v) {
314 if ($v['uid'] == $a->profile['uid']) {
315 $contact_id = $v['cid'];
322 $groups = init_groups_visitor($contact_id);
323 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
327 if (dbm::is_result($r)) {
329 $remote_contact = true;
333 if (!$remote_contact) {
335 $contact_id = $_SESSION['cid'];
336 $contact = $a->contact;
340 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
341 if (dbm::is_result($r)) {
342 $a->page_contact = $r;
344 $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
346 if ($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
347 notice(t('Access to this profile has been restricted.') . EOL);
351 // We need the editor here to be able to reshare an item.
356 'allow_location' => $a->user['allow_location'],
357 'default_location' => $a->user['default-location'],
358 'nickname' => $a->user['nickname'],
359 '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'),
360 'acl' => populate_acl($a->user, true),
362 'visitor' => 'block',
363 'profile_uid' => local_user(),
364 'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
366 $o .= status_editor($a,$x,0,true);
369 $sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
372 $r = dba::p("SELECT `id` FROM `item` WHERE `item`.`uid` = ?
373 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
374 $sql_extra AND `unseen`",
379 if (dba::num_rows($r) == 0) {
384 $r = dba::p(item_query()." AND `item`.`uid` = ?
385 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
387 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
392 if (!dbm::is_result($r) && local_user()) {
393 // Check if this is another person's link to a post that we have
394 $r = dba::fetch_first("SELECT `item`.uri FROM `item`
395 WHERE (`item`.`id` = ? OR `item`.`uri` = ?)
400 if (dbm::is_result($r)) {
401 $item_uri = $r['uri'];
403 $r = dba::p(item_query()." AND `item`.`uid` = ?
404 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `uri` = ? AND uid = ?)
405 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
413 if (dbm::is_result($r)) {
414 $s = dba::inArray($r);
416 if ((local_user()) && (local_user() == $a->profile['uid'])) {
417 $unseen = dba::select('item', array('id'), array('parent' => $s[0]['parent'], 'unseen' => true), array('limit' => 1));
418 if (dbm::is_result($unseen)) {
419 dba::update('item', array('unseen' => false), array('parent' => $s[0]['parent'], 'unseen' => true));
423 $items = conv_sort($s, "`commented`");
426 $o .= "<script> var netargs = '?f=&nick=" . $nick . "&item_id=" . $item_id . "'; </script>";
428 $o .= conversation($a, $items, 'display', $update);
430 // Preparing the meta header
431 require_once('include/bbcode.php');
432 require_once("include/html2plain.php");
433 $description = trim(html2plain(bbcode($s[0]["body"], false, false), 0, true));
434 $title = trim(html2plain(bbcode($s[0]["title"], false, false), 0, true));
435 $author_name = $s[0]["author-name"];
437 $image = $a->remove_baseurl($s[0]["author-thumb"]);
440 $title = $author_name;
443 // Limit the description to 160 characters
444 if (strlen($description) > 160) {
445 $description = substr($description, 0, 157) . '...';
448 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
449 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
450 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
452 //<meta name="keywords" content="">
453 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
454 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
455 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
456 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
458 // Schema.org microdata
459 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
460 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
461 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
462 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
465 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
466 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
467 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
468 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.$image.'" />'."\n";
469 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
472 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
473 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
476 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
477 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
478 $a->page['htmlhead'] .= '<meta property="og:image" content="'.$image.'" />'."\n";
479 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
480 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
481 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
487 $r = dba::fetch_first("SELECT `id`,`deleted` FROM `item` WHERE `id` = ? OR `uri` = ? LIMIT 1",
493 notice(t('Item has been removed.') . EOL);
495 notice(t('Permission denied.') . EOL);
498 notice(t('Item not found.') . EOL);