+/*
+ $guid = notags(unxmlify($xml->guid));
+ $author = notags(unxmlify($xml->author));
+
+
+ if($author != $msg["author"]) {
+ logger('diaspora_post: Potential forgery. Message handle is not the same as envelope sender.');
+ return 202;
+ }
+
+ $contact = diaspora_get_contact_by_handle($importer["uid"],$author);
+ if(! $contact)
+ return;
+
+ if(! diaspora_post_allow($importer,$contact, false)) {
+ logger('diaspora_reshare: Ignoring this author: ' . $author . ' ' . print_r($xml,true));
+ return 202;
+ }
+
+ $message_id = $author . ':' . $guid;
+ $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
+ intval($importer["uid"]),
+ dbesc($guid)
+ );
+ if(count($r)) {
+ logger('diaspora_reshare: message exists: ' . $guid);
+ return;
+ }
+
+ $orig_author = notags(unxmlify($xml->root_diaspora_id));
+ $orig_guid = notags(unxmlify($xml->root_guid));
+ $orig_url = $a->get_baseurl()."/display/".$orig_guid;
+
+ $create_original_post = false;
+
+ // Do we already have this item?
+ $r = q("SELECT `body`, `tag`, `app`, `created`, `plink`, `object`, `object-type`, `uri` FROM `item` WHERE `guid` = '%s' AND `visible` AND NOT
+`deleted` AND `body` != '' LIMIT 1",
+ dbesc($orig_guid),
+ dbesc(NETWORK_DIASPORA)
+ );
+ if(count($r)) {
+ logger('reshared message '.$orig_guid." reshared by ".$guid.' already exists on system.');
+
+ // Maybe it is already a reshared item?
+ // Then refetch the content, since there can be many side effects with reshared posts from other networks or reshares from reshares
+ require_once('include/api.php');
+ if (api_share_as_retweet($r[0]))
+ $r = array();
+ else {
+ $body = $r[0]["body"];
+ $str_tags = $r[0]["tag"];
+ $app = $r[0]["app"];
+ $orig_created = $r[0]["created"];
+ $orig_plink = $r[0]["plink"];
+ $orig_uri = $r[0]["uri"];
+ $object = $r[0]["object"];
+ $objecttype = $r[0]["object-type"];
+ }
+ }
+
+ if (!count($r)) {
+ $body = "";
+ $str_tags = "";
+ $app = "";
+
+ $server = 'https://'.substr($orig_author,strpos($orig_author,'@')+1);
+ logger('1st try: reshared message '.$orig_guid." reshared by ".$guid.' will be fetched from original server: '.$server);
+ $item = diaspora_fetch_message($orig_guid, $server);
+
+ if (!$item) {
+ $server = 'https://'.substr($author,strpos($author,'@')+1);
+ logger('2nd try: reshared message '.$orig_guid." reshared by ".$guid." will be fetched from sharer's server: ".$server);
+ $item = diaspora_fetch_message($orig_guid, $server);
+ }
+ if (!$item) {
+ $server = 'http://'.substr($orig_author,strpos($orig_author,'@')+1);
+ logger('3rd try: reshared message '.$orig_guid." reshared by ".$guid.' will be fetched from original server: '.$server);
+ $item = diaspora_fetch_message($orig_guid, $server);
+ }
+ if (!$item) {
+ $server = 'http://'.substr($author,strpos($author,'@')+1);
+ logger('4th try: reshared message '.$orig_guid." reshared by ".$guid." will be fetched from sharer's server: ".$server);
+ $item = diaspora_fetch_message($orig_guid, $server);
+ }
+
+ if ($item) {
+ $body = $item["body"];
+ $str_tags = $item["tag"];
+ $app = $item["app"];
+ $orig_created = $item["created"];
+ $orig_author = $item["author"];
+ $orig_guid = $item["guid"];
+ $orig_plink = diaspora_plink($orig_author, $orig_guid);
+ $orig_uri = $orig_author.':'.$orig_guid;
+ $create_original_post = ($body != "");
+ $object = $item["object"];
+ $objecttype = $item["object-type"];
+ }
+ }
+
+ $plink = diaspora_plink($author, $guid);
+
+ $person = find_diaspora_person_by_handle($orig_author);
+
+ $created = unxmlify($xml->created_at);
+ $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
+
+ $datarray = array();
+
+ $datarray["uid"] = $importer["uid"];
+ $datarray["contact-id"] = $contact["id"];
+ $datarray["wall"] = 0;
+ $datarray["network"] = NETWORK_DIASPORA;
+ $datarray["guid"] = $guid;
+ $datarray["uri"] = $datarray["parent-uri"] = $message_id;
+ $datarray["changed"] = $datarray["created"] = $datarray["edited"] = datetime_convert('UTC','UTC',$created);
+ $datarray["private"] = $private;
+ $datarray["parent"] = 0;
+ $datarray["plink"] = $plink;
+ $datarray["owner-name"] = $contact["name"];
+ $datarray["owner-link"] = $contact["url"];
+ $datarray["owner-avatar"] = ((x($contact,'thumb')) ? $contact["thumb"] : $contact["photo"]);
+ $prefix = share_header($person["name"], $person["url"], ((x($person,'thumb')) ? $person["thumb"] : $person["photo"]), $orig_guid, $orig_created, $orig_url);
+
+ $datarray["author-name"] = $contact["name"];
+ $datarray["author-link"] = $contact["url"];
+ $datarray["author-avatar"] = $contact["thumb"];
+ $datarray["body"] = $prefix.$body."[/share]";
+
+ $datarray["object"] = json_encode($xml);
+ $datarray["object-type"] = $objecttype;
+
+ $datarray["tag"] = $str_tags;
+ $datarray["app"] = $app;
+
+ // if empty content it might be a photo that hasn't arrived yet. If a photo arrives, we'll make it visible. (testing)
+ $datarray["visible"] = ((strlen($body)) ? 1 : 0);
+
+ // Store the original item of a reshare
+ if ($create_original_post) {
+ require_once("include/Contact.php");
+
+ $datarray2 = $datarray;
+
+ $datarray2["uid"] = 0;
+ $datarray2["contact-id"] = get_contact($person["url"], 0);
+ $datarray2["guid"] = $orig_guid;
+ $datarray2["uri"] = $datarray2["parent-uri"] = $orig_uri;
+ $datarray2["changed"] = $datarray2["created"] = $datarray2["edited"] = $datarray2["commented"] = $datarray2["received"] = datetime_convert('UTC','UTC',$orig_created);
+ $datarray2["parent"] = 0;
+ $datarray2["plink"] = $orig_plink;
+
+ $datarray2["author-name"] = $person["name"];
+ $datarray2["author-link"] = $person["url"];
+ $datarray2["author-avatar"] = ((x($person,'thumb')) ? $person["thumb"] : $person["photo"]);
+ $datarray2["owner-name"] = $datarray2["author-name"];
+ $datarray2["owner-link"] = $datarray2["author-link"];
+ $datarray2["owner-avatar"] = $datarray2["author-avatar"];
+ $datarray2["body"] = $body;
+ $datarray2["object"] = $object;
+
+ DiasporaFetchGuid($datarray2);
+ $message_id = item_store($datarray2);
+
+ logger("Store original item ".$orig_guid." under message id ".$message_id);
+ }
+
+ DiasporaFetchGuid($datarray);
+ $message_id = item_store($datarray);
+*/