+function DiasporaFetchGuid($item) {
+ preg_replace_callback("&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
+ function ($match) use ($item){
+ return(DiasporaFetchGuidSub($match, $item));
+ },$item["body"]);
+}
+
+function DiasporaFetchGuidSub($match, $item) {
+ $a = get_app();
+
+ $author = parse_url($item["author-link"]);
+ $authorserver = $author["scheme"]."://".$author["host"];
+
+ $owner = parse_url($item["owner-link"]);
+ $ownerserver = $owner["scheme"]."://".$owner["host"];
+
+ if (!diaspora_store_by_guid($match[1], $authorserver))
+ diaspora_store_by_guid($match[1], $ownerserver);
+}
+
+function diaspora_store_by_guid($guid, $server) {
+ require_once("include/Contact.php");
+
+ logger("fetching item ".$guid." from ".$server, LOGGER_DEBUG);
+
+ $item = diaspora_fetch_message($guid, $server);
+
+ if (!$item)
+ return false;
+
+ $body = $item["body"];
+ $str_tags = $item["tag"];
+ $app = $item["app"];
+ $created = $item["created"];
+ $author = $item["author"];
+ $guid = $item["guid"];
+ $private = $item["private"];
+
+ $message_id = $author.':'.$guid;
+ $r = q("SELECT `id` FROM `item` WHERE `uid` = 0 AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
+ dbesc($message_id),
+ dbesc($guid)
+ );
+ if(count($r))
+ return $r[0]["id"];
+
+ $person = find_diaspora_person_by_handle($author);
+
+ $datarray = array();
+ $datarray['uid'] = 0;
+ $datarray['contact-id'] = get_contact($person['url'], 0);
+ $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'] = diaspora_plink($author, $guid);
+ $datarray['author-name'] = $person['name'];
+ $datarray['author-link'] = $person['url'];
+ $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
+ $datarray['owner-name'] = $datarray['author-name'];
+ $datarray['owner-link'] = $datarray['author-link'];
+ $datarray['owner-avatar'] = $datarray['author-avatar'];
+ $datarray['body'] = $body;
+ $datarray['tag'] = $str_tags;
+ $datarray['app'] = $app;
+ $datarray['visible'] = ((strlen($body)) ? 1 : 0);
+
+ DiasporaFetchGuid($datarray);
+ $message_id = item_store($datarray);
+
+ // To-Do:
+ // Looking if there is some subscribe mechanism in Diaspora to get all comments for this post
+
+ return $message_id;
+}
+
+function diaspora_fetch_message($guid, $server, $level = 0) {
+
+ if ($level > 5)
+ return false;
+
+ $a = get_app();
+
+ // This will not work if the server is not a Diaspora server
+ $source_url = $server.'/p/'.$guid.'.xml';
+ $x = fetch_url($source_url);
+ if(!$x)
+ return false;
+
+ $x = str_replace(array('<activity_streams-photo>','</activity_streams-photo>'),array('<asphoto>','</asphoto>'),$x);
+ $source_xml = parse_xml_string($x,false);
+
+ $item = array();
+ $item["app"] = 'Diaspora';
+ $item["guid"] = $guid;
+ $body = "";
+
+ if ($source_xml->post->status_message->created_at)
+ $item["created"] = unxmlify($source_xml->post->status_message->created_at);
+
+ if ($source_xml->post->status_message->provider_display_name)
+ $item["app"] = unxmlify($source_xml->post->status_message->provider_display_name);
+
+ if ($source_xml->post->status_message->diaspora_handle)
+ $item["author"] = unxmlify($source_xml->post->status_message->diaspora_handle);
+
+ if ($source_xml->post->status_message->guid)
+ $item["guid"] = unxmlify($source_xml->post->status_message->guid);
+
+ $item["private"] = (unxmlify($source_xml->post->status_message->public) == 'false');
+
+ if(strlen($source_xml->post->asphoto->objectId) && ($source_xml->post->asphoto->objectId != 0) && ($source_xml->post->asphoto->image_url)) {
+ $body = '[url=' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '][img]' . notags(unxmlify($source_xml->post->asphoto->objectId)) . '[/img][/url]' . "\n";
+ $body = scale_external_images($body,false);
+ } elseif($source_xml->post->asphoto->image_url) {
+ $body = '[img]' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '[/img]' . "\n";
+ $body = scale_external_images($body);
+ } elseif($source_xml->post->status_message) {
+ $body = diaspora2bb($source_xml->post->status_message->raw_message);
+
+ // Checking for embedded pictures
+ if($source_xml->post->status_message->photo->remote_photo_path AND
+ $source_xml->post->status_message->photo->remote_photo_name) {
+
+ $remote_photo_path = notags(unxmlify($source_xml->post->status_message->photo->remote_photo_path));
+ $remote_photo_name = notags(unxmlify($source_xml->post->status_message->photo->remote_photo_name));
+
+ $body = '[img]'.$remote_photo_path.$remote_photo_name.'[/img]'."\n".$body;
+
+ logger('embedded picture link found: '.$body, LOGGER_DEBUG);
+ }
+
+ $body = scale_external_images($body);
+
+ // Add OEmbed and other information to the body
+ $body = add_page_info_to_body($body, false, true);
+ } elseif($source_xml->post->reshare) {
+ // Reshare of a reshare
+ return diaspora_fetch_message($source_xml->post->reshare->root_guid, $server, ++$level);
+ } else {
+ // Maybe it is a reshare of a photo that will be delivered at a later time (testing)
+ logger('no content found: '.print_r($source_xml,true));
+ $body = "";
+ }
+
+ if ($body == "")
+ return false;
+
+ $item["tag"] = '';
+ $item["body"] = $body;
+
+ return $item;
+}
+