+ if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
+ return;
+ }
+
+ $nick = (($a->argc > 1) ? $a->argv[1] : '');
+ $profiledata = array();
+
+ // If there is only one parameter, then check if this parameter could be a guid
+ if ($a->argc == 2) {
+ $nick = "";
+ $itemuid = 0;
+
+ // Does the local user have this item?
+ if (local_user()) {
+ $r = qu("SELECT `id`, `parent`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
+ WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
+ AND `guid` = '%s' AND `uid` = %d", dbesc($a->argv[1]), local_user());
+ if (dbm::is_result($r)) {
+ $nick = $a->user["nickname"];
+ $itemuid = local_user();
+ }
+ }
+
+ // Or is it anywhere on the server?
+ if ($nick == "") {
+ $r = qu("SELECT `user`.`nickname`, `item`.`id`, `item`.`parent`, `item`.`author-name`,
+ `item`.`author-link`, `item`.`author-avatar`, `item`.`network`, `item`.`uid`, `item`.`owner-link`, `item`.`body`
+ FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
+ WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
+ AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
+ AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
+ AND NOT `item`.`private` AND NOT `user`.`hidewall`
+ AND `item`.`guid` = '%s'", dbesc($a->argv[1]));
+ if (dbm::is_result($r)) {
+ $nick = $r[0]["nickname"];
+ $itemuid = $r[0]["uid"];
+ }
+ }
+
+ // Is it an item with uid=0?
+ if ($nick == "") {
+ $r = qu("SELECT `item`.`id`, `item`.`parent`, `item`.`author-name`, `item`.`author-link`,
+ `item`.`author-avatar`, `item`.`network`, `item`.`uid`, `item`.`owner-link`, `item`.`body`
+ FROM `item` WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
+ AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
+ AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
+ AND NOT `item`.`private` AND `item`.`uid` = 0
+ AND `item`.`guid` = '%s'", dbesc($a->argv[1]));
+ }
+ if (dbm::is_result($r)) {
+ if ($r[0]["id"] != $r[0]["parent"]) {
+ $r = qu("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
+ WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
+ AND `id` = %d", $r[0]["parent"]);
+ }
+ if (($itemuid != local_user()) AND local_user()) {
+ // Do we know this contact but we haven't got this item?
+ // Copy the wohle thread to our local storage so that we can interact.
+ // We really should change this need for the future since it scales very bad.
+ $contactid = get_contact($r[0]['owner-link'], local_user());
+ if ($contactid) {
+ $items = qu("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id`", intval($r[0]["id"]));
+ foreach ($items AS $item) {
+ $itemcontactid = get_contact($item['owner-link'], local_user());
+ if (!$itemcontactid) {
+ $itemcontactid = $contactid;
+ }
+ unset($item['id']);
+ $item['uid'] = local_user();
+ $item['origin'] = 0;
+ $item['contact-id'] = $itemcontactid;
+ $local_copy = item_store($item, false, false, true);
+ logger("Stored local copy for post ".$item['guid']." under id ".$local_copy, LOGGER_DEBUG);
+ }
+ }
+ }
+
+ $profiledata = display_fetchauthor($a, $r[0]);
+
+ if (strstr(normalise_link($profiledata["url"]), normalise_link(App::get_baseurl()))) {
+ $nickname = str_replace(normalise_link(App::get_baseurl())."/profile/", "", normalise_link($profiledata["url"]));
+
+ if (($nickname != $a->user["nickname"])) {
+ $r = qu("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
+ INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
+ WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
+ dbesc($nickname)
+ );
+ if (dbm::is_result($r)) {
+ $profiledata = $r[0];
+ }
+ $profiledata["network"] = NETWORK_DFRN;
+ } else {
+ $profiledata = array();
+ }
+ }
+ } else {
+ $a->error = 404;
+ notice(t('Item not found.') . EOL);
+ return;
+ }
+ }
+
+ profile_load($a, $nick, 0, $profiledata);
+
+}
+
+function display_fetchauthor($a, $item) {
+
+ require_once("include/Contact.php");
+
+ $profiledata = array();
+ $profiledata["uid"] = -1;
+ $profiledata["nickname"] = $item["author-name"];
+ $profiledata["name"] = $item["author-name"];
+ $profiledata["picdate"] = "";
+ $profiledata["photo"] = $item["author-avatar"];
+ $profiledata["url"] = $item["author-link"];
+ $profiledata["network"] = $item["network"];
+
+ // Check for a repeated message
+ $skip = false;
+ $body = trim($item["body"]);