Commits

Anonymous committed 1be30bb

a bunch of updates.

Comments (0)

Files changed (24)

+<?php
+
+require_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");
+        admin_gatekeeper();
+        echo "go...<br/>";
+        elgg_set_ignore_access(true);
+	$clear = get_input('clear', 0);
+        $options = array('metadata_name'=>'foreign', 'metadata_value' => 1, 'owner_guid'=>ELGG_ENTITIES_ANY_VALUE, 'limit'=>0);
+        $ents = elgg_get_entities_from_metadata($options);
+	echo "<h2>delete objects</h2>";
+        foreach ($ents as $ent) {
+		if ($ent->name)
+			$title = $ent->name;
+		elseif ($ent->title)
+			$title = $ent->title;
+		else
+			$title = $ent->guid.':'.$ent->getSubtype();
+                echo "delete ".$title."<br/>";
+		if ($clear) {
+                	$ent->delete();
+		}
+        }
+
+	echo "<h2>foreign_notifications</h2>";
+        $options = array('types'=>'object','subtypes'=>'foreign_notification','limit'=>0);
+        $ents = elgg_get_entities($options);
+        foreach ($ents as $ent) {
+                echo "delete ".$ent->guid."<br/>";
+		if ($clear) {
+                	$ent->delete();
+		}
+	}
+	echo "<h2>salmon_notifications</h2>";
+        $options = array('types'=>'object','subtypes'=>'salmon_notification','limit'=>0);
+        $ents = elgg_get_entities($options);
+        foreach ($ents as $ent) {
+                echo "delete ".$ent->guid."<br/>";
+		if ($clear) {
+                	$ent->delete();
+		}
+        }
+
+
+	echo "go...<br/>";
+/*
+        $options = array('limit'=>0,'types'=>'','subtypes'=>'');
+        $ents = elgg_get_entities($options);
+        foreach ($ents as $ent) {
+                if ($ent->foreign) {
+                        echo "delete ".$ent->guid."<br/>";
+                        $ent->delete();
+                }
+	}
+        $options = array('types'=>'group','limit'=>0);
+        $ents = elgg_get_entities($options);
+        foreach ($ents as $ent) {
+                if ($ent->foreign || $ent->type == 'file') {
+                        echo "delete ".$ent->guid."<br/>";
+                        $ent->delete();
+                }
+        }
+*/
+	elgg_set_ignore_access(false);
+?>

graphics/foreign.png

Added
New image
+<?php
+
+// Generate By translationbrowser. 
+
+$catalan = array( 
+	 'foreign_objects:foreigngroup'  =>  "grup remot" , 
+	 'foreign_objects:remote'  =>  "federat, prem aquí per anar a la web original" , 
+	 'item:object:foreign_notification'  =>  "notificacions remotes"
+); 
+
+add_translation('ca', $catalan); 
+
+?>
          */
 
 	$english = array(
+		"item:user:ostatus" => 'Ostatus users',
 		"foreign_objects:foreigngroup" => 'remote group',
+		"foreign_objects:remote" => 'federated, click to go to original site',
 		"item:object:foreign_notification" => 'remote notifications'
 	);
 	
-<?php
-/**
-	 * Elgg spotlight lorea
-	 * 
-	 * @package
-	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
-	 * @author lorea
-	 * @copyright lorea
-	 * @link http://lorea.cc
-	 */
-
-	$spanish = array(
-	                "foreign_objects:foreigngroup" => 'grupo remoto',
-	                "item:object:foreign_notification" => 'notificaciones remotas');
-	
-	add_translation("es",$spanish);
-
-?>
+<?php
+
+// Generado por translationbrowser 
+
+$spanish = array( 
+	 'foreign_objects:foreigngroup'  =>  "grupo remoto" , 
+	 'foreign_objects:remote'  =>  "este elemento esta federado desde otra red, pincha para ir a la red original" , 
+	 'item:object:foreign_notification'  =>  "notificaciones remotas"
+); 
+
+add_translation('es', $spanish); 
+
+?>
+<?php
+
+// Xerado por translationbrowser 
+
+$galician = array( 
+	 'foreign_objects:foreigngroup'  =>  "grupo remoto" , 
+	 'foreign_objects:remote'  =>  "Este elemento está federado dende outra rede, preme para ir á rede orixinal" , 
+	 'item:object:foreign_notification'  =>  "Notificacións remotas"
+); 
+
+add_translation('gl', $galician); 
+
+?>
+<?php
+
+// Gerado pelo browser de tradução. 
+
+$portuguese = array( 
+	 'foreign_objects:foreigngroup'  =>  "Grupo remoto" , 
+	 'foreign_objects:remote'  =>  "Federado, clique para ir ao original" , 
+	 'item:object:foreign_notification'  =>  "Notificações remotas"
+); 
+
+add_translation('pt', $portuguese); 
+
+?>
+<?php
+
+function foreign_object_disable_permissions($entity_type) {
+	global $CONFIG;
+	$oldhooks = $CONFIG->hooks['permissions_check'][$entity_type];
+	$oldhooks2 = $CONFIG->hooks['permissions_check:metadata'][$entity_type];
+	$oldhooks3 = $CONFIG->hooks['container_permissions_check'][$entity_type];
+	$CONFIG->hooks['permissions_check'][$entity_type] = array("foreign_objects_yes_perms");
+	$CONFIG->hooks['container_permissions_check'][$entity_type] = array("foreign_objects_yes_perms");
+	$CONFIG->hooks['permissions_check:metadata'][$entity_type] = array("foreign_objects_yes_perms");
+	return array($oldhooks, $oldhooks2, $oldhooks3);
+}
+
+function foreign_object_restore_permissions($entity_type, $backup) {
+	global $CONFIG;
+	$CONFIG->hooks['permissions_check'][$entity_type] = $backup[0];
+	$CONFIG->hooks['permissions_check:metadata'][$entity_type] = $backup[1];
+	$CONFIG->hooks['container_permissions_check'][$entity_type] = $backup[2];
+}
+
+function foreign_object_create_object($notification, $subtype, $group, $params, $entry, $description=false) {
+	global $CONFIG;
+	if ($params['provenance'] && !in_array($subtype, full_foreign_objects()))
+		return;
+	$query = array('atom_id'=>$params['object_id']);
+	$objs = elgg_get_entities_from_metadata(array('types'=>'object','subtypes'=>$subtype,'metadata_name_value_pairs'=>$query));
+	if (empty($objs)) {
+		$objs = elgg_get_entities_from_metadata(array('types'=>'object','subtypes'=>ELGG_ENTITIES_ANY_VALUE,'metadata_name_value_pairs'=>$query));
+	}
+	if ($objs) {
+		//error_log("foreign objects: object exists".$params['object_id']);
+		$newObject = $objs[0];
+		//return @current($objs);
+	} else {
+		//error_log("foreign objects: create".$params['object_id'].":".$description);
+		$newObject = new ElggObject();
+		$newObject->subtype = $subtype;
+		if ($params['user']) {
+			//error_log("ser user");
+			$newObject->owner_guid = $params['user']->getGUID();
+		}
+		else
+			$newObject->owner_guid = 0;
+
+	}
+	$newObject->access_id = foreign_objects_get_accessid($entry);
+	$newObject->title = $params['object_name'];
+	if (isset($description)) {
+		//error_log("save description");
+		$newObject->description = $description;
+	}
+	//error_log("foreign objects: create2".$newObject->title.":".$newObject->description);
+	$newObject->link = $params['object_link'];
+	$newObject->atom_id = $params['object_id'];
+	$newObject->icon_link = $params['object_icon'];
+	$newObject->notification = $notification->guid;
+	//$newObject->time_created = $params['published'];
+	//$newObject->time_updated = $params['updated'];
+
+	if ($group) {
+		if ($group->foreign) {
+			switch($subtype) {
+				case 'bookmarks':
+					$group->bookmarks_enable = 'yes';
+					break;
+				case 'groupforumpost':
+				case 'groupforumtopic':
+					$group->threaded_forums_enable = 'yes';
+					break;
+				case 'blog':
+					$group->blog_enable = 'yes';
+					break;
+				case 'file':
+					$group->files_enable = 'yes';
+					break;
+				case 'page_top':
+				case 'page':
+					$group->pages_enable = 'yes';
+					break;
+				case 'tasks':
+					$group->tasks_enable = 'yes';
+					break;
+				case 'event_calendar':
+					$group->event_calendar_enable = 'yes';
+					break;
+				case 'thewire':
+				case 'messages':
+					break;
+			}
+		}
+		$newObject->container_guid = $group->getGUID();
+	}
+	elseif ($params['user']) {
+		//error_log("set container to ".$params['user']->getGUID());
+		$newObject->container_guid = $params['user']->getGUID();
+	}
+	$newObject->save();
+	// change time for the object directly on the db
+	$res = update_data("UPDATE {$CONFIG->dbprefix}entities set time_created={$params['published']} where guid={$newObject->getGUID()}");
+	        $res = update_data("UPDATE {$CONFIG->dbprefix}entities set time_updated={$params['updated']} where guid={$newObject->getGUID()}");
+
+	/*if ($params['salmon_link']) {
+		$newObject->salmon_link = $params['salmon_link'];
+		// XXX will be loaded later on since we can't trust
+                // the salmon this comes from
+	}*/
+	if ($params['provenance'])
+		$newObject->salmon_provenance = $params['provenance'];
+	else {
+		if ($params['notification_provenance'])
+			$newObject->salmon_provenance = $params['notification_provenance'];
+		$newObject->foreign = true;
+	}
+	return $newObject;
+}
+function foreign_object_create_group($params, $target = 'container') {
+	//error_log("create group");
+	$newGroup = new ElggGroup();
+	$newGroup->name = $params[$target.'_name'];
+	$newGroup->access_id = ACCESS_PUBLIC;
+	$newGroup->owner_guid = 0;
+	//$newObject->salmon_link = $notification->salmon_link;
+	$newGroup->save();
+	//$newGroup->salmon_link = $params['salmon_link'];
+	$newGroup->link = $params[$target.'_link'];
+	$newGroup->atom_id = $params[$target.'_id'];
+	$newGroup->icon_link = $params[$target.'_icon'];
+	$newGroup->foreign = true;
+	$group_options = array('files', 'pages', 'threaded_forums', 'forum', 'polls', 'tasks', 'videolist', 'event_calendar', 'blog', 'dokuwiki', 'izap_videos', 'photos', 'bookmarks');
+	foreach($group_options as $option) {
+		$par = $option."_enable";
+		$newGroup->$par = 'no';
+	}
+	//error_log("group created");
+	return $newGroup;
+}
+
+function foreign_objects_get_group($entry, $params, $target = 'container') {
+	// look for a local group
+	$webid = $params[$target.'_id'];
+	$type = $params[$target.'_type'];
+	$group = foreign_objects_get_object_from_webid($webid, $type);
+	if (empty($group)) {
+		// look for a remote group
+		$query = array('atom_id'=>$params[$target.'_id']);
+		//error_log("about to create group");
+		$groups = elgg_get_entities_from_metadata(array('types'=>'group','metadata_name_value_pairs'=>$query));
+		if ($groups) {
+			$group = @current($groups);
+			//error_log("foreign objects: group exists");
+		} else {
+			$group = foreign_object_create_group($params, $target);
+			//error_log("foreign objects: created group");
+		}
+	}
+	if ($group && $group->foreign) {
+		if ($target == 'container')
+			$target = 'target';
+		foreign_objects_create_icons($entry, $group, 'activity:'.$target);
+		$group->icon_link = $params[$target.'_icon'];
+		$group->access_id = foreign_objects_get_accessid($entry, $target);
+	}
+	return $group;
+}
+
+function foreign_objects_parse_atom($entry) {
+	$title = @current($entry->xpath("atom:title"));
+	// atom published and updated
+	$published = @current($entry->xpath("atom:published"));
+	$updated = @current($entry->xpath("atom:updated"));
+	// author name
+	$author_name = @current($entry->xpath("atom:author/atom:name"));
+	if (!$author_name)
+		$author_name = @current($entry->xpath("//atom:author/atom:name"));
+
+	$object_name = @current($entry->xpath("activity:object/atom:title"));
+	if (!$object_name) {
+		$object_name = @current($entry->xpath('atom:title'));
+	}
+	// container
+	$container_id = @current($entry->xpath("activity:target/atom:id"));
+	$container_type = @current($entry->xpath("activity:target/activity:object-type"));
+	$container_icon = @current($entry->xpath("activity:target/atom:link[attribute::rel='preview']/@href"));
+	$container_name = @current($entry->xpath("activity:target/atom:title"));
+	$container_link = @current($entry->xpath("activity:target/atom:link[attribute::rel='alternate']/@href"));
+
+	// extra object stuff
+	$object_icon = @current($entry->xpath("activity:object/atom:link[attribute::rel='preview']/@href"));
+	$object_link = @current($entry->xpath("activity:object/atom:link[attribute::rel='alternate']/@href"));
+	$object_type = @current($entry->xpath("activity:object/activity:object-type"));
+	if (!$object_type)
+		$object_type = 'http://activitystrea.ms/schema/1.0/note';
+	$object_id = @current($entry->xpath("activity:object/atom:id"));
+	if (!$object_id)
+		$object_id = @current($entry->xpath("atom:id"));
+
+
+	// subject
+	$subject= @current($entry->xpath("atom:author/atom:id"));
+	if (!$subject)
+		$subject= @current($entry->xpath("//atom:author/atom:id"));
+	if (!$subject)
+		$subject= @current($entry->xpath("//atom:author/atom:uri"));
+	$subject_link= @current($entry->xpath("atom:author/atom:link[attribute::rel='alternate']/@href"));
+	if (!$subject_link)
+		$subject_link= @current($entry->xpath("atom:link[attribute::rel='alternate']/@href"));
+	if (!$subject_link)
+		$subject_link= @current($entry->xpath("//activity:subject/atom:link[attribute::rel='alternate']/@href"));
+	if (!$subject_link)
+		$subject_link = $subject;
+	$subject_icon= @current($entry->xpath("//atom:author/atom:link[attribute::rel='preview']/@href"));
+	if (!$subject_icon)
+		$subject_icon = @current($entry->xpath("activity:subject/atom:link[attribute::media:width='48']/@href"));
+	if (!$subject_icon)
+		$subject_icon = @current($entry->xpath("//activity:subject/atom:link[attribute::media:width='48']/@href"));
+	$verbs = $entry->xpath("activity:verb");
+	$id = @current($entry->xpath("atom:id"));
+	$last_verb = $verbs?array_pop($verbs):false;
+	if (!$last_verb) {
+		$last_verb = 'http://activitystrea.ms/schema/1.0/post';
+	}
+	if (empty($published)) {
+		$published = time();
+	}
+	else {
+		$published = strtotime($published);
+	}
+	if (empty($updated)) {
+		$updated = time();
+	}
+	else {
+		$updated = strtotime($updated);
+	}
+	$params = array('subject'=>$subject_link,
+                                'object'=>$object_link,
+                                'object_type'=>$object_type,
+                                'verb'=>$last_verb,
+                                'subject_name'=>$author_name,
+                                'subject_icon'=>$subject_icon,
+                                'container_type'=>$container_type,
+                                'container_icon'=>$container_icon,
+                                'object_icon'=>$object_icon,
+                                'object_id'=>$object_id,
+                                'object_link'=>$object_link,
+                                'container_name'=>$container_name,
+                                'container_id'=>$container_id,
+                                'container_link'=>$container_link,
+                                'object_name'=>$object_name,
+                                'title'=>$title,
+                                'id'=>$id,
+                                'published'=>$published,
+                                'updated'=>$updated);
+	return $params;
+}
+
+
+function fo_getdate($str_time) {
+	return strtotime($str_time);
+}
+
+/*
+ * pshb_river_update_hook
+ *
+ * hook to intercept river updates and distribute them on pshb
+ */
+
+function foreign_objects_get_object_from_webid($webid, $as_type) {
+	global $CONFIG;
+	if (strpos($webid, $CONFIG->wwwroot) === 0) {
+		// internal entity
+		$base_url = $CONFIG->wwwroot.'pg/entities/';
+		$root_len = strlen($base_url);
+		$entity_guid = substr($webid, $root_len, strlen($webid)-$root_len);
+		$entity = get_entity($entity_guid);
+		if ($entity)
+			return $entity;
+	}
+	else {
+		// foreign entity, we should have the atom_id stored on it (otherwise its not created yet)
+		$options = array('metadata_name'=>'atom_id', 'metadata_value' => $webid, 'owner_guid'=>ELGG_ENTITIES_ANY_VALUE);
+		$entities = elgg_get_entities_from_metadata($options);
+		if ($entities) {
+			return $entities[0];
+		}
+	}
+}
+
+function foreign_objects_get_user_from_webid($webid) {
+	global $CONFIG;
+	$base_url = $CONFIG->wwwroot."pg/profile/";
+	if (strpos($webid, $base_url) === 0) {
+		$root_len = strlen($base_url);
+		$username = substr($webid, $root_len, strlen($webid)-$root_len);
+		$user = get_user_by_username($username);
+	}
+	if ($user) {
+		return $user;
+	}
+	$options = array('metadata_name'=>'webid', 'metadata_value' => $webid, 'owner_guid'=>ELGG_ENTITIES_ANY_VALUE, 'types'=>'user', 'subtypes'=>'ostatus');
+        $users = elgg_get_entities_from_metadata($options);
+	if ($users) {
+		$user = $users[0];
+	}
+	return $user;
+}
+
+function fo_randomString($length)
+{
+    // Generate random 32 charecter string
+    $string = md5(time());
+
+    // Position Limiting
+    $highest_startpoint = 32-$length;
+
+    // Take a random starting point in the randomly
+    // Generated String, not going any higher then $highest_startpoint
+    $randomString = substr($string,rand(0,$highest_startpoint),$length);
+
+    return $randomString;
+
+}
+
+
+function foreign_objects_get_author($entry) {
+	$webid = @current($entry->xpath("atom:author/atom:id"));
+
+	if (!$webid) {
+		$webid = @current($entry->xpath("atom:author/atom:uri"));
+	}
+	if (!$webid) {
+		$webid = @current($entry->xpath("//atom:author/atom:uri"));
+	}
+	if (!$webid) {
+		$webid = @current($entry->xpath("//atom:author/atom:id"));
+	}
+	$name = @current($entry->xpath("atom:author/atom:name"));
+	if (!$name)
+		$name = @current($entry->xpath("//atom:author/atom:name"));
+	$icon = @current($entry->xpath("atom:author/atom:link[attribute::rel='preview']/@href"));
+	if (!$icon)
+		$icon = @current($entry->xpath("//atom:author/atom:link[attribute::media:width='48']/@href"));
+	if (!$icon)
+		$icon = @current($entry->xpath("activity:subject/atom:link[attribute::media:width='48']/@href"));
+	if (!$icon)
+		$icon = @current($entry->xpath("//activity:subject/atom:link[attribute::media:width='48']/@href"));
+	$link = @current($entry->xpath("atom:author/atom:link[attribute::rel='alternate']/@href"));
+	if (!$link)
+		$link = $webid;
+	$user = foreign_objects_get_user_from_webid($webid);
+	if ($user) {
+		//error_log("found user:".$user->username.":".$name);
+		if (!$user->foreign)
+			return $user; // internal user
+	}
+	else {
+		//error_log("creating user:".$webid);
+		$user = new ElggUser();
+		$user->owner_guid = 0;
+		$user->container_guid = 0;
+		$user->subtype = "ostatus";
+		$user->username = fo_randomString(8);
+		$user->save();
+		$user->username = 'ostatus_'.$user->getGUID();
+		$user->access_id = ACCESS_PUBLIC;
+	}
+	$user->name = $name;
+	$user->save();
+
+	$user->link = $link;
+	$user->foreign = true;
+	$user->webid = $webid;
+	$user->atom_id = $webid;
+	if ($icon)
+		$user->icon_link = $icon;
+	// icons
+	foreign_objects_create_icons($entry, $user);
+	return $user;
+}
+function foreign_objects_create_icons($entry, $entity, $target='atom:author') {
+	$icons = array(array('topbar','16'),
+			array('tiny','25'),
+			array('small','40'),
+			array('medium','100'),
+			array('large','200'),
+			array('master','500'));
+	foreach($icons as $icon) {
+		$icon_href = @current($entry->xpath($target."/atom:link[attribute::media:width='".$icon[1]."']/@href"));
+		if (empty($icon_href)) {
+			$icon_href = @current($entry->xpath("activity:subject/atom:link[attribute::media:width='".$icon[1]."']/@href"));
+		}
+		if (!empty($icon_href)) {
+			$var_name = 'oicon_'.$icon[0];
+			$entity->$var_name = $icon_href;
+		}
+	}
+}
+
+function foreign_objects_save_comment_provenance($annotation_id, $provenance, $atom_id) {
+	global $CONFIG;
+	$provenance = sanitise_string($provenance);
+	foreign_objects_updatedb();
+	insert_data("INSERT into {$CONFIG->dbprefix}annotations_fat (id, provenance) values ('{$annotation_id}', '{$provenance}')");
+}
+
+function foreign_objects_is_localurl($notification_id) {
+	global $CONFIG;
+        if (strpos($notification_id, $CONFIG->wwwroot) === 0) {
+		return true;
+	}
+	return false;
+}
+
+
+function foreign_objects_check_provenance($atom_provenance) {
+        $xml = salmon_decode_update_xml($atom_provenance);
+        if (isset($xml)) { 
+                $notification_id = @current($xml->xpath('atom:id'));
+		//error_log("decoded".$notification_id);
+                if (foreign_objects_is_localurl($notification_id)) {
+                        return false; // our own update coming back
+		}
+                return $xml;
+        } 
+        else {
+                // the signature is not valid but we keep it anyways for now..
+                // we should probably discard it in this case but lets see
+                return false;
+        }
+}
+
+function foreign_objects_get_tags($entry, $type = 'object') {
+	$tags = $entry->xpath("activity:".$type."/atom:category/@term");
+	if ($tags)
+		return string_to_tag_array(implode(", ", $tags));
+	return "";
+	
+}
+
+function foreign_objects_get_accessid($entry, $type= 'object') {
+	$access_id = ACCESS_PUBLIC;
+	$entry->registerXPathNamespace('privacy','http://lorea.org/ns/privacy');
+	$privacy_url = @current($entry->xpath("activity:".$type."/privacy:privacylevel/@url"));
+	$privacy_level = @current($entry->xpath("activity:".$type."/privacy:privacylevel"));
+	switch($privacy_level) {
+		case 'network':
+			$access_id = ACCESS_LOGGED_IN;
+			break;
+		case 'friends':
+			$access_id = ACCESS_FRIENDS;
+			break;
+		case 'group':
+			$group = foreign_objects_get_object_from_webid($privacy_url, 'group');
+			$access_id = $group->group_acl;
+			break;
+		case 'private':
+			$access_id = ACCESS_PRIVATE;
+			break;
+		default:
+			$access_id = ACCESS_PUBLIC;
+	}
+	return $access_id;
+}
+
+
+function foreign_objects_notification_arrived($hook, $entity_type, $returnvalue, $params) {
+	global $CONFIG;
+	if ($hook === 'foreign_notification' && $entity_type === 'foreign_notification') {
+	elgg_set_ignore_access(true);
+	set_context('foreign_notification');
+	$provenance = $params['provenance']; // not foreign
+	//error_log('**::'.$CONFIG->wwwroot);
+	/*if ($provenance)
+		error_log('**::PROVENANCE!');*/
+	$internal = ($params['internal'] == true);
+	$entry = $params['entry'];
+	$entry->registerXPathNamespace('me','http://salmon-protocol.org/ns/magic-env');
+	$entry->registerXPathNamespace('media','http://purl.org/syndication/atommedia');
+	
+	$atom_prov = $entry->xpath("me:provenance");
+	if (is_array($atom_prov)) {
+		//error_log("ping provenance".count($atom_prov));
+		$has_atom_provenance = $atom_prov[0];
+	}
+	if (isset($has_atom_provenance)) {
+		//error_log("ping provenance");
+		$prov_xml = foreign_objects_check_provenance($has_atom_provenance);
+		if (empty($prov_xml)) { // maybe original from here
+			//error_log("has provenance bail out");
+			elgg_set_ignore_access(false);
+			return null;
+		}
+		$params['notification_provenance'] = $has_atom_provenance->asXml();
+	}
+	$salmon_link = $params['salmon_link'];
+	$params = foreign_objects_parse_atom($entry);
+	$params['salmon_link'] = $salmon_link;
+	$params['provenance'] = $provenance;
+	//foreign_objects_parse_atom($entry);
+	$annotation_id = 0;
+	$subject_link = $params['subject'];
+	$object_link = $params['object'];
+	$published = $params['published'];
+	$updated = $params['updated'];
+	$verb = $params['verb'];
+	$title = $params['title'];
+	$subscriber = $params['subscriber'];
+	$query = array('atom_id'=>$params['id']);
+	$notifications = elgg_get_entities_from_metadata(array('types'=>'object','subtypes'=>array('foreign_notification','salmon_notification'),'metadata_name_value_pairs'=>$query));
+	if ($notifications) {
+		//error_log("notification already received".$params['id']);
+		elgg_set_ignore_access(false);
+		return null;
+	}
+	$newObject = new ElggObject();
+	$newObject->access_id = ACCESS_PUBLIC;
+	if ($provenance)
+		$newObject->subtype = 'salmon_notification';
+	else
+		$newObject->subtype = 'foreign_notification';
+	//error_log($params['container_type']);
+	//error_log($params['container_link']);
+	//error_log("saving");
+	if ($newObject->save()) {
+	//error_log("saved".$params['object_type']);
+		$newObject->atom_id = $params['id'];
+	}
+	//error_log('foreign_notification:'.$params['id']);
+	//error_log("notification new received".$params['id']);
+	$user = foreign_objects_get_author($entry);
+	//login($user);
+	$params['user'] = $user;
+	//error_log("messages arriving:".$verb);
+
+	// special actions
+	if ($verb == "http://elgg.org/activitystreams/actions/requestfriendship" && $provenance) {
+		//error_log("friends".$verb);
+		$target_user = $params['container_id'];
+		$toUser = foreign_objects_get_user_from_webid($target_user);
+                if (!check_entity_relationship($user->guid, "friendrequest", $toUser->guid))
+			add_entity_relationship($user->guid, "friendrequest", $toUser->guid);
+		elgg_set_ignore_access(false);
+		return null;
+	}
+	elseif ($verb == "http://elgg.org/activitystreams/actions/approvefriendship" && $provenance) {
+		//error_log("friends".$verb);
+		$target_user = $params['container_id'];
+		$friend = foreign_objects_get_user_from_webid($target_user);
+		if (remove_entity_relationship($friend->guid, "friendrequest", $user->guid)) {
+			if(isset($CONFIG->events['create']['friend'])) {
+		                $oldEventHander = $CONFIG->events['create']['friend'];
+		                $CONFIG->events['create']['friend'] = array();                  //Removes any event handlers
+		        }
+			$user->addFriend($friend->guid);
+			$friend->addFriend($user->guid);
+			if(isset($CONFIG->events['create']['friend'])) {
+		                $CONFIG->events['create']['friend'] = $oldEventHander;
+		        }
+			add_to_river('friends/river/create','friend',$user->guid,$friend->guid,ACCESS_FRIENDS);
+		}
+		elgg_set_ignore_access(false);
+		return null;
+	}
+
+	elseif ($verb == "http://elgg.org/activitystreams/actions/declinefriendship" && $provenance) {
+		//error_log("friends".$verb);
+		$target_user = $params['container_id'];
+		$friend = foreign_objects_get_user_from_webid($target_user);
+		if (check_entity_relationship($friend->guid, "friendrequest", $user->guid))
+			remove_entity_relationship($friend->guid, "friendrequest", $user->guid);
+		elgg_set_ignore_access(false);
+		return null;
+	}
+	elseif ($verb == "http://elgg.org/activitystreams/actions/removefriendship" && $provenance) {
+		//error_log("friends".$verb);
+		$target_user = $params['container_id'];
+		$friend = foreign_objects_get_user_from_webid($target_user);
+                if ($user->isFriendsWith($friend->guid))
+			$user->removeFriend($friend->guid);
+		try { 
+                	if ($friend->isFriendsWith($user->guid))
+				$friend->removeFriend($user->guid);
+		} catch (Exception $e) {}
+		elgg_set_ignore_access(false);
+		return null;
+	}
+	elseif ($verb == "http://activitystrea.ms/schema/1.0/follow" && $provenance) {
+		//error_log("friends".$verb);
+		if (isset($params['target_entity']) && $params['target_entity']) {
+			$friend = $params['target_entity'];
+		}
+		else {
+			$target_user = $params['container_id'];
+			$friend = foreign_objects_get_user_from_webid($target_user);
+		}
+		// $user->startFollow($friend);
+		$user_guid = $user->guid;
+		$entity_guid = $friend->guid;
+		if (!check_entity_relationship($user_guid ,'flags_content', $entity_guid))
+			add_entity_relationship($user_guid ,'flags_content', $entity_guid);
+		elgg_set_ignore_access(false);
+		return null;
+	}
+	elseif (($verb == "http://activitystrea.ms/schema/1.0/unfollow" || $verb == "http://ostatus.org/schema/1.0/unfollow") && $provenance) {
+		//error_log("friends".$verb);
+		if (isset($params['target_entity']) && $params['target_entity']) {
+			$friend = $params['target_entity'];
+		}
+		else {
+			$target_user = $params['container_id'];
+			$friend = foreign_objects_get_user_from_webid($target_user);
+		}
+		$user_guid = $user->guid;
+		$entity_guid = $friend->guid;
+		if (check_entity_relationship($user_guid ,'flags_content', $entity_guid))
+			remove_entity_relationship($user_guid ,'flags_content', $entity_guid);
+		// $user->startFollow($friend);
+		elgg_set_ignore_access(false);
+		return null;
+	}
+
+	elseif ($verb == "http://elgg.org/activitystreams/actions/joinrequest" && $provenance) {
+		//error_log("joinrequest");
+		$group = foreign_objects_get_group($entry, $params);
+		set_input('user_guid', $user->guid);
+		set_input('group_guid', $group->guid);
+		include($CONFIG->pluginspath.'groups/actions/joinrequest.php');
+		elgg_set_ignore_access(false);
+		return null;
+	}
+	elseif ($verb == "http://elgg.org/activitystreams/actions/killrequest" && $provenance) {
+		$group = foreign_objects_get_group($entry, $params);
+		set_input('user_guid', $user->guid);
+		set_input('group_guid', $group->guid);
+		include($CONFIG->pluginspath.'groups/actions/groupskillrequest.php');
+		elgg_set_ignore_access(false);
+		return null;
+	}
+	elseif ($verb == "http://elgg.org/activitystreams/actions/addtogroup" && $provenance) {
+		$group = foreign_objects_get_group($entry, $params);
+		set_input('user_guid', $user->guid);
+		set_input('group_guid', $group->guid);
+		include($CONFIG->pluginspath.'groups/actions/addtogroup.php');
+		elgg_set_ignore_access(false);
+		return null;
+	}
+
+	// normal process for receiving notifications and creating objects
+	if ($params['object_type'] == 'http://elgg.org/activitystreams/types/object/messages' && $provenance) {
+		$target_user = $params['container_id'];
+		$toUser = foreign_objects_get_user_from_webid($target_user);
+		$description = @current($entry->xpath("atom:content"));
+		$obj = foreign_object_create_object(null, 'messages', $group, $params, $entry, $description);
+		$obj->owner_guid = $toUser->guid;
+		$obj->container_guid = $toUser->guid;
+		$obj->toId = $toUser->guid;
+		$obj->fromId = $user->guid;
+		$obj->readYet = 0;
+		$obj->hiddenFrom = 0;
+		$obj->hiddenTo = 0;
+		$obj->msg = 1;
+		$obj->save();
+		elgg_set_ignore_access(false);
+		return null;
+	}
+
+	$newObject->title = $title;
+	$container_type = $params['container_type'];
+	//error_log($params['container_type']);
+	//error_log($params['container_link']);
+	//error_log("saving");
+	if ($newObject->save()) {
+	//error_log("saved".$params['object_type']);
+		$newObject->subject_link = $subject_link;
+		$newObject->object_link = $object_link;
+		$newObject->atom_id = $params['id'];
+		$newObject->object_name = $params['object_name'];
+		$newObject->object_type = $params['object_type'];
+		$newObject->object_icon = $params['object_icon'];
+		$newObject->subject_name = $params['subject_name'];
+		$newObject->subject_icon = $params['subject_icon'];
+		$newObject->container_name = $params['container_name'];
+		$newObject->container_link = $params['container_link'];
+		$newObject->container_icon = $params['container_icon'];
+		$newObject->container_type = $params['container_type'];
+		$newObject->salmon_link = $params['salmon_link'];
+		$newObject->published = $published;
+		$newObject->verb = $verb;
+		//error_log("check to create group");
+		$group = false;
+		// check target for group
+		if (($container_type == "http://elgg.org/activitystreams/types/group/" || $container_type == "http://activitystrea.ms/schema/1.0/group") && $params['container_link']) {
+			$entry->registerXPathNamespace('elgg', 'http://elgg.org/activitystreams/');
+			$description = @current($entry->xpath("activity:target/atom:summary"));
+			$content = @current($entry->xpath("activity:target/atom:content"));
+			$membership = @current($entry->xpath("activity:target/elgg:groupmembership"));
+			$group = foreign_objects_get_group($entry, $params);
+			if ($group->foreign) {
+				$group->interests = foreign_objects_get_tags($entry, "target");
+				$group->briefdescription = $description;
+				$group->description = $content;
+				$group->save();
+				if ($membership == "open")
+					$group->membership = ACCESS_PUBLIC;
+			}
+		}
+
+		// objects where both local and remote actions are allowed (only group for now)
+		if (trim($params['object_type']) == "http://activitystrea.ms/schema/1.0/group") {
+			//error_log("LOOK FOR GROUP");
+			$description = @current($entry->xpath("activity:object/atom:summary"));
+			$content = @current($entry->xpath("activity:object/atom:content"));
+			$membership = @current($entry->xpath("activity:object/elgg:groupmembership"));
+			$obj = foreign_objects_get_group($entry, $params, 'object');
+			if ($obj->foreign) {
+				$obj->briefdescription = $description;
+				$obj->description = $content;
+				$obj->interests = foreign_objects_get_tags($entry);
+				$obj->save();
+				if ($membership == "open")
+					$obj->membership = ACCESS_PUBLIC;
+				if ($group)
+					$obj->container_guid  = $group->getGUID();
+				else
+					$obj->container_guid  = 0;
+			}
+			//error_log("LOOK FOR GROUP".$obj->name);
+		}
+		if ($provenance) {
+			// dont create objects as it should be an action over a local one
+			// TODO find destination object here
+		} else {
+				if ($params['object_type'] == "http://elgg.org/activitystreams/types/object/file" || $params['object_type'] == "http://activitystrea.ms/schema/1.0/file") {
+			$description = @current($entry->xpath("activity:object/atom:summary"));
+			$download_link = @current($entry->xpath("activity:object/atom:link[attribute::rel='enclosure']/@href"));
+			$download_mime = @current($entry->xpath("activity:object/atom:link[attribute::rel='enclosure']/@type"));
+			$obj = foreign_object_create_object($newObject, 'file', $group, $params, $entry, $description);
+			$obj->tags = foreign_objects_get_tags($entry);
+			$obj->download_link = $download_link;
+			$obj->mimetype = $download_mime;
+		}
+
+		} // !$provenance
+
+		if ($params['object_type'] == "http://elgg.org/activitystreams/types/object/groupforumpost") {
+			// groupforumpost
+			$summary = @current($entry->xpath("activity:object/atom:summary"));
+			$description = @current($entry->xpath("activity:object/atom:content"));
+			$entry->registerXPathNamespace('thr', 'http://purl.org/syndication/thread/1.0');
+			$parent_uri = @current($entry->xpath("activity:object/thr:in-reply-to/@ref")); // for top level it will get topic or first post :-p
+			if ($parent_uri) {
+				$parent = foreign_objects_get_object_from_webid($parent_uri, 'groupforumpost');
+				if ($parent) {
+					$obj = foreign_object_create_object($newObject, 'groupforumpost', $group, $params, $entry, $description);
+					$annotation_id = $obj->annotate('group_topic_post',$description, $obj->access_id, $user->guid, 'text');
+					if ($parent->getSubtype() == 'groupforumpost') {
+						$topics = elgg_get_entities_from_relationship(array('relationship'=>'group_discussion_top_level_post', 'relationship_guid'=>$parent->guid, 'inverse_relationship'=>true, 'limit'=>1));
+						if ($topics)
+							$topic = $topics[0];
+						else {
+							$topics = elgg_get_entities_from_relationship(array('relationship'=>'group_discussion_topic_link', 'relationship_guid'=>$parent->guid, 'inverse_relationship'=>true, 'limit'=>1));
+							if ($topics) {
+								$topic = $topics[0];
+							}
+								
+						}
+						if ($topic)
+							add_entity_relationship($topic->guid, 'group_discussion_topic_link', $obj->guid);
+						add_entity_relationship($parent->guid, 'group_discussion_reply_post', $obj->guid); // must be the first operation
+						$action = 'create';
+						$action_view = 'river/forum/create';
+					}
+					elseif ($parent->getSubtype() == 'groupforumtopic') {
+						add_entity_relationship($parent->guid, 'group_discussion_top_level_post', $obj->guid);
+						$action = 'create';
+						$action_view = 'river/forum/create';
+					}
+					$obj->tags = foreign_objects_get_tags($entry);
+				}
+			}
+		}
+		elseif ($params['object_type'] == "http://activitystrea.ms/schema/1.0/note") {
+			$description = @current($entry->xpath("/activity:object/atom:content"));
+			if (!$description) {
+				$description = $entry->xpath("atom:content");
+				if (is_array($description))
+					$description = @current($description);
+				if ($description)
+					$description = $description->asXML();
+			}
+			error_log("pshb:create note ".$description);
+			$obj = foreign_object_create_object($newObject, 'thewire', $group, $params, $entry, $description);
+			$obj->method = 'ostatus';
+			$parent_uri = @current($entry->xpath("activity:object/thr:in-reply-to/@ref")); // for top level it will get topic or first post :-p
+			if ($parent_uri) {
+				$parent = foreign_objects_get_object_from_webid($parent_uri, 'thewire');
+				if ($parent) {
+					$obj->parent = $parent->guid;
+				}
+			}
+
+			$action = 'create';
+			$action_view = 'river/object/thewire/create';
+		}
+
+		elseif ($params['object_type'] == "http://activitystrea.ms/schema/1.0/event") {
+			$entry->registerXPathNamespace('cal', 'urn:ietf:params:xml:ns:xcal');
+			$start_date = @current($entry->xpath("activity:object/cal:dtstart"));
+			$end_date = @current($entry->xpath("activity:object/cal:dtend"));
+			$description = @current($entry->xpath("activity:object/atom:summary"));
+			if ($verb == "http://elgg.org/activitystreams/actions/create") {
+			    $action = 'create';
+			    $action_view = 'river/object/event_calendar/create';
+			} elseif ($verb == "http://elgg.org/activitystreams/actions/update") {
+			    $action = 'update';
+			    $action_view = 'river/object/event_calendar/update';
+			}
+			$obj = foreign_object_create_object($newObject, 'event_calendar', $group, $params, $entry, $description);
+			$obj->tags = foreign_objects_get_tags($entry);
+			if (isset($start_date))
+				$obj->start_date = fo_getdate($start_date);
+			if (isset($end_date))
+				$obj->end_date = fo_getdate($end_date);
+			$start_date = getdate($obj->start_date);
+			$end_date = getdate($obj->end_date);
+			$obj->start_time = ($start_date['hours']*60)+$start_date['minutes'];
+			$obj->end_time = ($end_date['hours']*60)+$end_date['minutes'];
+			if (($obj->start_time == 0) && ($obj->end_time == 0))
+				$obj->allday = true;
+		}
+
+		elseif ($params['object_type'] == "http://elgg.org/activitystreams/types/object/blog" || $params['object_type'] == "http://activitystrea.ms/schema/1.0/article") {
+			$description = @current($entry->xpath("activity:object/atom:content"));
+			$obj = foreign_object_create_object($newObject, 'blog', $group, $params, $entry, $description);
+			$obj->tags = foreign_objects_get_tags($entry);
+			if ($verb == "http://elgg.org/activitystreams/actions/create") {
+			    $action = 'create';
+			    $action_view = 'river/object/blog/create';
+			} elseif ($verb == "http://elgg.org/activitystreams/actions/update") {
+			    $action = 'update';
+			    $action_view = 'river/object/blog/update';
+			}
+
+		}
+		elseif ($params['object_type'] == "http://elgg.org/activitystreams/types/object/bookmarks" || $params['object_type'] == "http://activitystrea.ms/schema/1.0/bookmark") {
+			$description = @current($entry->xpath("activity:object/atom:summary"));
+			$obj = foreign_object_create_object($newObject, 'bookmarks', $group, $params, $entry, $description);
+			//error_log("BOOKMARK ARRIVED:".$description.":".$obj->description);
+			$obj->address = @current($entry->xpath("activity:object/atom:link[attribute::rel='related']/@href"));
+			$obj->tags = foreign_objects_get_tags($entry);
+			if ($verb == "http://elgg.org/activitystreams/actions/create") {
+			    $action = 'create';
+			    $action_view = 'river/object/bookmarks/create';
+			} elseif ($verb == "http://elgg.org/activitystreams/actions/update") {
+			    $action = 'update';
+			    $action_view = 'river/object/bookmarks/update';
+			}
+
+
+		}
+		elseif ($params['object_type'] == "http://elgg.org/activitystreams/types/object/tasks") {
+			$description = @current($entry->xpath("activity:object/atom:content"));
+
+			$entry->registerXPathNamespace('elgg', 'http://elgg.org/activitystreams/');
+			$done = @current($entry->xpath("activity:object/elgg:done/text()"));
+			$active = @current($entry->xpath("activity:object/elgg:active/text()"));
+			$active_arr = $entry->xpath("activity:object/elgg:active");
+				
+			$obj = foreign_object_create_object($newObject, 'tasks', $group, $params, $entry, $description);
+			$obj->tags = foreign_objects_get_tags($entry);
+			$obj->long_description = $description;
+			$obj->done = $done=="yes"?"1":"0";
+                        $obj->active = $active=="yes"?"1":0;
+                        $obj->startdate = time();
+                        $obj->enddate = time();
+                        $obj->priority = 'Medium';
+			if ($verb == "http://elgg.org/activitystreams/actions/create") {
+			    $action = 'create';
+			    $action_view = 'river/tasks/update';
+			} elseif ($verb == "http://elgg.org/activitystreams/actions/update") {
+			    $action = 'update';
+			    $action_view = 'river/tasks/update';
+			} elseif ($verb == "http://elgg.org/activitystreams/actions/done") {
+			    $action = 'done';
+			    $action_view = 'river/tasks/update';
+			} elseif ($verb == "http://elgg.org/activitystreams/actions/undone") {
+			    $action = 'undone';
+			    $action_view = 'river/tasks/update';
+			}
+
+			$entry->registerXPathNamespace('thr', 'http://purl.org/syndication/thread/1.0');
+			$parent_uri = @current($entry->xpath("activity:object/thr:in-reply-to/@ref"));
+			$obj->parent_guid = 0;
+			if (!empty($parent_uri)) {
+				$parent = foreign_objects_get_object_from_webid($parent_uri, 'tasks');
+				if (!empty($parent)) {
+					$obj->parent_guid = "".$parent->getGUID();
+				} else {
+					//error_log(" task:parent not found!");
+				}
+			}
+			$obj->save();
+		}
+
+		elseif ($params['object_type'] == "http://elgg.org/activitystreams/types/object/page") {
+			//error_log("go page");
+			$entity_subtype = 'page';
+			$entity_type = 'all';
+	//		$oldhooks = foreign_object_disable_permissions('all');
+	//		$oldhooks2 = foreign_object_disable_permissions('object');
+			$description = @current($entry->xpath("activity:object/atom:content"));
+			//error_log("go page 2".$user->guid);
+
+			$obj = foreign_object_create_object($newObject, $entity_subtype, $group, $params, $entry, $description);
+			//error_log("go page 3");
+			$obj->tags = foreign_objects_get_tags($entry);
+			//error_log("go page 4");
+			$aid = $obj->annotate('page',$description, $obj->access_id, $user->guid);
+			$entry->registerXPathNamespace('thr', 'http://purl.org/syndication/thread/1.0');
+			$parent_uri = @current($entry->xpath("activity:object/thr:in-reply-to/@ref"));
+			//error_log("go page 4");
+			if (!empty($parent_uri)) {
+				$parent = foreign_objects_get_object_from_webid($parent_uri, $entity_subtype);
+				if (!empty($parent)) {
+					//error_log(" pages:found parent".$parent->getGUID());
+					$obj->parent_guid = $parent->getGUID();
+				}
+				else {
+					//error_log("could not find parent");
+				}
+			}
+			if ($verb == "http://elgg.org/activitystreams/actions/create") {
+			    $action = 'create';
+			    $action_view = 'river/object/page/create';
+			} elseif ($verb == "http://elgg.org/activitystreams/actions/update") {
+			    $action = 'update';
+			    $action_view = 'river/object/page/update';
+			}
+
+			//error_log("end page");
+	//		foreign_object_restore_permissions('all', $oldhooks);
+	//		foreign_object_restore_permissions('object', $oldhooks2);
+			
+		}
+		elseif ($params['object_type'] == "http://elgg.org/activitystreams/types/object/page_top") {
+			$entity_type = 'object';
+			$entity_subtype = 'page_top';
+			$oldhooks = $CONFIG->hooks['permissions_check'][$entity_type];
+			$oldhooks2 = $CONFIG->hooks['permissions_check:metadata'][$entity_type];
+			$CONFIG->hooks['permissions_check'][$entity_type] = array();
+			$CONFIG->hooks['permissions_check:metadata'][$entity_type] = array();
+
+			$description = @current($entry->xpath("activity:object/atom:content"));
+			$obj = foreign_object_create_object($newObject, $entity_subtype, $group, $params, $entry, $description);
+			$obj->tags = foreign_objects_get_tags($entry);
+			$aid = $obj->annotate('page',$description, $obj->access_id, $user->guid);
+			$CONFIG->hooks['permissions_check'][$entity_type] = $oldhooks;
+			$CONFIG->hooks['permissions_check:metadata'][$entity_type] = $oldhooks2;
+			if ($verb == "http://elgg.org/activitystreams/actions/create") {
+			    $action = 'create';
+			    $action_view = 'river/object/page/create';
+			} elseif ($verb == "http://elgg.org/activitystreams/actions/update") {
+			    $action = 'update';
+			    $action_view = 'river/object/page/update';
+			}
+
+		}
+
+
+		elseif ($params['object_type'] == "http://elgg.org/activitystreams/types/object/groupforumtopic") {
+			// groupforumtopic
+			//error_log("groupforumtopic");
+			$summary = @current($entry->xpath("activity:object/atom:summary"));
+			$description = @current($entry->xpath("activity:object/atom:content"));
+			error_log("forum topic".$description);
+			$post = foreign_object_create_object($newObject, 'groupforumpost', $group, $params, $entry, $description);
+			$obj = foreign_object_create_object($newObject, 'groupforumtopic', $group, $params, $entry, $description);
+			//error_log("groupforumtopic".$post->guid.":".$obj->guid);
+			$obj->status = 'open';
+			$annotation_id = $post->annotate('group_topic_post',$description, $obj->access_id, $user->guid, 'text');
+			add_entity_relationship($group->guid, 'group_discussion', $obj->guid);
+			add_entity_relationship($obj->guid, 'group_discussion_top_level_post', $post->guid);
+			$obj->tags = foreign_objects_get_tags($entry);
+			$action = 'create';
+			$action_view = 'river/forum/topic/create';
+		}
+
+		elseif ($params['object_type'] == "http://activitystrea.ms/schema/1.0/comment") {
+			$entry->registerXPathNamespace('thr', 'http://purl.org/syndication/thread/1.0');
+			$description = @current($entry->xpath("activity:object/atom:content"));
+			$parent_uri = @current($entry->xpath("activity:object/thr:in-reply-to/@ref"));
+			$action = 'comment';
+			$action_view = 'annotation/annotate';
+			//error_log("comment for".$parent_uri);
+			if ($parent_uri) {
+				$entity = null;
+				$base_path = $CONFIG->wwwroot."pg/entities/";
+				if (strpos($parent_uri, $base_path) === 0) {
+					$parent_id = substr($parent_uri, strlen($base_path));
+					$entity = get_entity($parent_id);
+				}
+				else {
+					$options = array('metadata_name'=>'atom_id', 'metadata_value' => $parent_uri, 'owner_guid'=>ELGG_ENTITIES_ANY_VALUE);
+        				$candidates = elgg_get_entities_from_metadata($options);
+					if ($candidates) {
+						$entity = $candidates[0];
+						//error_log("really found:".count($candidates));
+					}
+
+				}
+				if ($entity) {
+					$annotation_id = create_annotation($entity->getGUID(), 'generic_comment', $description, '', $user->guid, $entity->access_id);
+					if ($provenance) {
+						foreign_objects_save_comment_provenance($annotation_id, $provenance, $params["object_id"]);
+					} elseif ($params["notification_provenance"])
+						foreign_objects_save_comment_provenance($annotation_id, $params["notification_provenance"], $params["object_id"]);
+					//error_log("found parent!".$entity->getGUID());
+					$obj = $entity;
+				}
+			}
+		}
+
+		if ($obj)
+			$newObject->access_id = $obj->access_id;
+
+		// group join and leave
+		if ($user && $obj) {
+			switch($params['verb']) {
+				case 'http://activitystrea.ms/schema/1.0/join':
+					//error_log("GROUP JOIN!!".$obj->name);
+					if ($obj instanceof ElggGroup) {
+						if ($obj->isPublicMembership())
+							$obj->join($user);
+						elgg_set_ignore_access(false);
+						return null;
+					}
+					break;
+				case 'http://activitystrea.ms/schema/1.0/leave':
+					if ($obj instanceof ElggGroup) {
+						$obj->leave($user);
+						elgg_set_ignore_access(false);
+						return null;
+					}
+					break;
+			}
+		}
+		// finish up
+		if (!$obj)
+			$obj = $newObject;
+		else {
+			$newObject->object_guid = $obj->getGUID();
+			$newObject->access_id = $obj->access_id;
+			$newObject->save();
+		}
+		if ($group) {
+			$newObject->container_guid = $group->guid;
+			$newObject->save();
+		}
+
+		//error_log("about to add to river".$user->getGUID().":".$user->name.":".$user->access_id.":".$obj->getGUID().":".$annotation_id);
+		// now add to river
+		//error_log("river");
+		if ($provenance && $action && $action_view) {
+			if (add_to_river($action_view, $action, $user->getGUID(), $obj->getGUID(), $obj->access_id, $updated, $annotation_id, true)) {
+				//error_log(" ** river2 ok:".$params['id']);
+			}
+		}
+		elseif (!$provenance) {
+		//error_log("about to add to river2");
+			if ($action && $action_view) {
+				$newObject->action_type = $action;
+				$newObject->view = $action_view;
+				if (add_to_river('foreign_objects/riverproxy', 'foreign_notification', $user->getGUID(), $newObject->getGUID(), $newObject->access_id, $updated, $annotation_id, true)) {
+					//error_log(" ** river ok:".$params['id']);
+				}
+			}
+			else {
+		//error_log("about to add to river3");
+				if (add_to_river('foreign_objects/river', 'foreign_notification', $user->getGUID(), $newObject->getGUID(), $newObject->access_id, $updated, $annotation_id, true)) {
+					//error_log(" ** river ok:".$params['id']);
+				}
+			}
+		}
+		elgg_set_ignore_access(false);
+		//error_log("end river");
+	}
+    }
+    return null;
+}
+
+//maps.lorea.org/dev/all/4/0/45/11.25/33.75
+?>

manifest.xml

File contents unchanged.
+DROP TABLE prefix_annotations_fat;
 CREATE TABLE prefix_annotations_fat (
   id int(11) NOT NULL,
   provenance varchar(40000),
+  atom_id varchar(256),
   PRIMARY KEY (id)
 );
 
 <?php
 
-function foreign_object_create_object($notification, $subtype, $group, $params, $description=false) {
-	if ($params['provenance'])
-		return;
-	$query = array('atom_id'=>$notification->object_link);
-	$objs = elgg_get_entities_from_metadata(array('types'=>'object','subtypes'=>$subtype,'metadata_name_value_pairs'=>$query));
-	if ($objs) {
-		error_log("foreign objects: object exists".$notification->object_link);
-		$newObject = $objs[0];
-		//return @current($objs);
-	} else {
-		error_log("foreign objects: create".$notification->object_link.":".$description);
-		$newObject = new ElggObject();
-		$newObject->subtype = $subtype;
-	}
-	$newObject->access_id = ACCESS_PUBLIC;
-	if ($params['user']) {
-		error_log("ser user");
-		$newObject->owner_guid = $params['user']->getGUID();
-	//$newObject->owner_guid = 0;
-	}
-	$newObject->title = $notification->object_name;
-	if (isset($description)) {
-		error_log("save description");
-		$newObject->description = $description;
-	}
-	error_log("foreign objects: create2".$newObject->title.":".$newObject->description);
-	$newObject->link = $notification->object_link;
-	$newObject->atom_id = $params['object_id'];
-	$newObject->icon_link = $notification->object_icon;
-	$newObject->notification = $notification->getGUID();
-	if ($group) {
-		$newObject->container_guid = $group->getGUID();
-	}
-	elseif ($params['user']) {
-		error_log("set container to ".$params['user']->getGUID());
-		$newObject->container_guid = $params['user']->getGUID();
-	}
-	$newObject->save();
-	if ($notification->salmon_link) {
-		$newObject->salmon_link = $notification->salmon_link;
-	}
-	if ($params['provenance'])
-		$newObject->salmon_provenance = $params['provenance'];
-	else {
-		$newObject->foreign = true;
-	}
-	return $newObject;
-}
-function foreign_object_create_group($params, $target = 'container') {
-
-	//error_log("create group");
-	$newGroup = new ElggGroup();
-	$newGroup->name = $params[$target.'_name'];
-	$newGroup->access_id = ACCESS_PUBLIC;
-	$newGroup->owner_guid = 0;
-	//$newObject->salmon_link = $notification->salmon_link;
-	$newGroup->save();
-	$newGroup->salmon_link = $params['salmon_link'];
-	$newGroup->link = $params[$target.'_link'];
-	$newGroup->atom_id = $params[$target.'_link'];
-	$newGroup->icon_link = $params[$target.'_icon'];
-	$newGroup->foreign = true;
-	//error_log("group created");
-	return $newGroup;
+function full_foreign_objects() {
+	return array('groupforumpost', 'groupforumtopic', 'blog', 'bookmarks', 'messages', 'page_top', 'page', 'tasks', 'event_calendar', 'thewire');
 }
 
-function foreign_objects_get_group($params, $target = 'container') {
-	// look for a local group
-	$webid = $params[$target.'_link'];
-	$type = $params[$target.'_type'];
-	$group = foreign_objects_get_object_from_webid($webid, $type);
-	if ($group)
-		return $group;
-	// look for a remote group
-	$query = array('atom_id'=>$params[$target.'_link']);
-	//error_log("about to create group");
-	$groups = elgg_get_entities_from_metadata(array('types'=>'group','metadata_name_value_pairs'=>$query));
-	if ($groups) {
-		$group = @current($groups);
-		error_log("foreign objects: group exists");
-	} else {
-		$group = foreign_object_create_group($params, $target);
-		error_log("foreign objects: created group");
-	}
-	return $group;
-}
-
-function foreign_objects_parse_atom($entry) {
-	$title = @current($entry->xpath("atom:title"));
-	$object = @current($entry->xpath("activity:object/atom:id"));
-	$published = @current($entry->xpath("atom:published"));
-	$updated = @current($entry->xpath("atom:updated"));
-	$author_name = @current($entry->xpath("atom:author/atom:name"));
-	if (!$author_name)
-		$author_name = @current($entry->xpath("//atom:author/atom:name"));
-	$object_name = @current($entry->xpath("activity:object/atom:title"));
-	$container_type = @current($entry->xpath("activity:target/activity:object-type"));
-	$container_icon = @current($entry->xpath("activity:target/atom:link[attribute::rel='preview']/@href"));
-	$object_icon = @current($entry->xpath("activity:object/atom:link[attribute::rel='preview']/@href"));
-	$container_name = @current($entry->xpath("activity:target/atom:title"));
-	$container_link = @current($entry->xpath("activity:target/atom:link[attribute::rel='alternate']/@href"));
-	$object_link = @current($entry->xpath("activity:object/atom:link[attribute::rel='alternate']/@href"));
-	$object_type = @current($entry->xpath("activity:object/activity:object-type"));
-	$object_id = @current($entry->xpath("activity:object/atom:id"));
-	$subject= @current($entry->xpath("atom:author/atom:id"));
-	if (!$subject)
-		$subject= @current($entry->xpath("atom:id"));
-	$subject_link= @current($entry->xpath("atom:author/atom:link[attribute::rel='alternate']/@href"));
-	if (!$subject_link)
-		$subject_link= @current($entry->xpath("atom:link[attribute::rel='alternate']/@href"));
-	$subject_icon= @current($entry->xpath("//atom:author/atom:link[attribute::rel='preview']/@href"));
-	$verbs = $entry->xpath("activity:verb");
-	$id = @current($entry->xpath("atom:id"));
-	$last_verb = array_pop($verbs);
-	$params = array('subject'=>$subject_link,
-                                'object'=>$object_link,
-                                'object_type'=>$object_type,
-                                'verb'=>$last_verb,
-                                'subject_name'=>$author_name,
-                                'subject_icon'=>$subject_icon,
-                                'container_type'=>$container_type,
-                                'container_icon'=>$container_icon,
-                                'object_icon'=>$object_icon,
-                                'object_id'=>$object_id,
-                                'object_link'=>$object_id,
-                                'container_name'=>$container_name,
-                                'container_link'=>$container_link,
-                                'object_name'=>$object_name,
-                                'title'=>$title,
-                                'id'=>$id,
-                                'published'=>strtotime($published),
-                                'updated'=>strtotime($updated));
-	return $params;
-}
-
-
-function fo_getdate($str_time) {
-	return strtotime($str_time);
-	$ftime = strptime($str_time, DateTime::ATOM);
-	$unxTimestamp = mktime(
-                    $ftime['tm_hour'],
-                    $ftime['tm_min'],
-                    $ftime['tm_sec'],
-                    $ftime['tm_mon'] ,
-                    $ftime['tm_mday'],
-                   $ftime['tm_year']+1900
-                 ); 
-	return $unxTimestamp;
-}
-/*
- * pshb_river_update_hook
- *
- * hook to intercept river updates and distribute them on pshb
- */
-
-function foreign_objects_get_object_from_webid($webid, $as_type) {
+function foreign_objects_notification_hook($hook, $entity_type, $returnvalue, $params) {
 	global $CONFIG;
-	if (strpos($webid, $CONFIG->wwwroot) === 0) {
-		// internal entity
-		$base_url = $CONFIG->wwwroot.'pg/entities/';
-		$root_len = strlen($base_url);
-		$entity_guid = substr($webid, $root_len, strlen($webid)-$root_len);
-		$entity = get_entity($entity_guid);
-		if ($entity)
-			return $entity;
-	}
-	else {
-		// foreign entity, we should have the atom_id stored on it (otherwise its not created yet)
-		$options = array('metadata_name'=>'atom_id', 'metadata_value' => $webid, 'owner_guid'=>ELGG_ENTITIES_ANY_VALUE);
-		$entities = elgg_get_entities_from_metadata($options);
-		if ($entities) {
-			return $entities[0];
-		}
-	}
-}
-
-function foreign_objects_get_user_from_webid($webid) {
-	global $CONFIG;
-	$base_url = $CONFIG->wwwroot."pg/profile/";
-	if (strpos($webid, $base_url) === 0) {
-		$root_len = strlen($base_url);
-		$username = substr($webid, $root_len, strlen($webid)-$root_len);
-		$user = get_user_by_username($username);
-	}
-	if ($user) {
-		return $user;
-	}
-	$options = array('metadata_name'=>'webid', 'metadata_value' => $webid, 'owner_guid'=>ELGG_ENTITIES_ANY_VALUE, 'types'=>'user', 'subtypes'=>'ostatus');
-        $users = elgg_get_entities_from_metadata($options);
-	if ($users) {
-		$user = $users[0];
-	}
-	return $user;
-}
-
-function foreign_objects_get_author($entry) {
-	$webid = @current($entry->xpath("atom:author/atom:id"));
-	$name = @current($entry->xpath("atom:author/atom:name"));
-	$icon = @current($entry->xpath("atom:author/atom:link[attribute::rel='preview']"));
-	$user = foreign_objects_get_user_from_webid($webid);
-	if ($user) {
-		error_log("found user:".$user->username.":".$name);
-	}
-	else {
-		error_log("creating user:".$webid);
-		$user = new ElggUser();
-		$user->owner_guid = 0;
-		$user->container_guid = 0;
-		$user->subtype = "ostatus";
-	}
-	$user->access_id = ACCESS_PUBLIC;
-	$user->name = $name;
-	$user->username = 'ostatus_'.$name.'_'.$user->getGUID();
-	$user->save();
-	$user->webid = $webid;
-	if ($icon)
-		$user->icon_link = $icon;
-	return $user;
+	require_once($CONFIG->pluginspath."foreign_objects/lib.php");
+	foreign_objects_notification_arrived($hook, $entity_type, $returnvalue, $params);
 }
 
 function foreign_objects_updatedb() {
 	global $CONFIG;
 	$site = $CONFIG->site;
-	if (!$site->has_provenance_table) {
+	if ($site->has_provenance_table === 2 || $site->has_provenance_table === 1) {
+		try {
+		run_sql_script($CONFIG->path."mod/foreign_objects/sql/drop.sql");
+		} catch (Exception $e) {
+		}
+		$site->has_provenance_table = 2;	
+	}
+	if (!($site->has_provenance_table === 3)) {
+		error_log("creating provenance tables");
 		run_sql_script($CONFIG->path."mod/foreign_objects/sql/tables.sql");
-		$site->has_provenance_table = 1;	
+		$site->has_provenance_table = 3;	
 	}
-
 }
 
-function foreign_objects_save_comment_provenance($annotation_id, $provenance) {
+
+
+function foreign_object_url($entity) {
 	global $CONFIG;
-	$provenance = sanitise_string($provenance);
-	foreign_objects_updatedb();
-	insert_data("INSERT into {$CONFIG->dbprefix}annotations_fat (id, provenance) values ('{$annotation_id}', '{$provenance}')");
+	if ($entity->foreign)
+		return $entity->link;
+        if ($entity instanceof ElggUser && $entity->alias && get_plugin_usersetting("usealias", $entity->guid,"openid_client") && $entity->guid!=get_loggedin_userid())
+		return $entity->alias;
+	/*if ($entity->getSubtype() == 'page' || $entity->getSubtype() == 'page_top') {
+		return pages_url($entity);
+	}
+	if ($entity->getSubtype() == 'tasks') {
+		return tasks_url($entity);
+	}
+	if ($entity->getSubtype() == 'bookmarks') {
+		return bookmark_url($entity);
+	}*/
+	if ($entity->getSubtype() == 'event_calendar') {
+		return event_calendar_url($entity);
+	}
+	if ($entity->getSubtype() == 'file') {
+		return file_url($entity);
+	}
+	if ($entity->type == 'user') {
+		if (is_plugin_enabled("profile"))
+			return profile_url($entity);
+	}
 }
 
-function foreign_objects_check_provenance($has_atom_provenance) {
-	error_log("item with provenance! bailing out!!");
+function foreign_objects_icon_hook($hook, $entity_type, $returnvalue, $params) {
+	global $CONFIG;
+	if ($hook == 'entity:icon:url') {
+		$entity = $params['entity'];
+		if ($entity->foreign) {
+			$container = get_entity($vars['entity']->container_guid);
+                        if ($container)
+                             $icon = "<a href='".$container->getURL()."'><img src='".$container->getIcon()."' /></a>";
+                        else {
+                             $notification = get_entity($vars['entity']->notification);
+                             $icon = "<a href='".$notification->subject_link."'><img src='".$notification->subject_icon."' /></a>";
+			}
+
+		}
+	}
+	return null;
+}
+
+function foreign_objects_yes_perms($hook, $entity_type, $returnvalue, $params) {
 	return true;
 }
 
+function foreign_objects_perms_hook($hook, $entity_type, $returnvalue, $params) {
+	if (get_context() == 'foreign_notification')
+                return null;
+	if ($params['entity']->foreign && $params['entity'] instanceof ElggGroup)
+		return false;
+	if (in_array($params['entity']->getSubtype(), full_foreign_objects()))
+		return null;
+	return foreign_objects_container_perms_hook($hook, $entity_type, $returnvalue, array('container'=>$params['entity'], 'user'=>$params['user']));
+}
 
+function foreign_objects_container_perms_hook($hook, $entity_type, $returnvalue, $params) {
+	if ($params['container'] && $params['container']->getType() == 'user') {
+		return null;
+	}
+	if (get_context() == 'foreign_notification')
+                return null;
+	if (in_array(get_context(), array('threaded_forums', 'blog', 'bookmarks', 'pages', 'tasks', 'event_calendar')))
+		return null;
+	if ($params['user'] && $params['container']) {
+		if ($params['container']->foreign && !in_array($params['container']->getSubtype(), full_foreign_objects())) {
+			return false;
+		}
+	}
+	return null;
+}
 
-function foreign_objects_notification_hook($hook, $entity_type, $returnvalue, $params) {
+
+function foreign_objects_groupicon_hook($hook, $entity_type, $returnvalue, $params) {
 	global $CONFIG;
-	error_log('foreign_notification!');
-	if ($hook === 'foreign_notification' && $entity_type === 'foreign_notification') {
-	elgg_set_ignore_access(true);
-	$provenance = $params['provenance']; // not foreign
-	error_log('**::'.$CONFIG->wwwroot);
-	if ($provenance)
-		error_log('**::PROVENANCE!');
-	$internal = ($params['internal'] == true);
-	$entry = $params['entry'];
-	$entry->registerXPathNamespace('me','http://salmon-protocol.org/ns/magic-env');
-	$has_atom_provenance = @current($entry->xpath("me:provenance/me:data"));
-	if ($has_atom_provenance && foreign_objects_check_provenance($has_atom_provenance)) { // maybe original from here
-		return;
+	if ($hook == 'entity:icon:url') {
+		$entity = $params['entity'];
+		if ($entity->foreign) {
+			$var_name = 'oicon_'.$params['size'];
+			if ($entity->$var_name)
+				return $entity->$var_name;
+			if ($entity->icon_link)
+				return $entity->icon_link;
+		}
 	}
-	$salmon_link = $params['salmon_link'];
-	$params = foreign_objects_parse_atom($entry);
-	$params['salmon_link'] = $salmon_link;
-	$params['provenance'] = $provenance;
-	//foreign_objects_parse_atom($entry);
-	$annotation_id = 0;
-	$subject_link = $params['subject'];
-	$object_link = $params['object'];
-	$published = $params['published'];
-	$updated = $params['updated'];
-	$verb = $params['verb'];
-	$title = $params['title'];
-	$subscriber = $params['subscriber'];
-	$query = array('atom_id'=>$params['id']);
-	$notifications = elgg_get_entities_from_metadata(array('types'=>'object','subtypes'=>array('foreign_notification','salmon_notification'),'metadata_name_value_pairs'=>$query));
-	if ($notifications) {
-		error_log("notification already received".$params['id']);
-		return;
-	}
-	//error_log("notification new received".$params['id']);
-	$user = foreign_objects_get_author($entry);
-	$params['user'] = $user;
-	$newObject = new ElggObject();
-	$newObject->access_id = ACCESS_PUBLIC;
-	if ($provenance)
-		$newObject->subtype = 'salmon_notification';
-	else
-		$newObject->subtype = 'foreign_notification';
-	$newObject->title = $title;
-	$container_type = $params['container_type'];
-	//error_log($params['container_type']);
-	//error_log($params['container_link']);
-	error_log("saving");
-	if ($newObject->save()) {
-	error_log("saved".$params['object_type']);
-		$newObject->subject_link = $subject_link;
-		$newObject->object_link = $object_link;
-		$newObject->atom_id = $params['id'];
-		$newObject->object_name = $params['object_name'];
-		$newObject->object_type = $params['object_type'];
-		$newObject->object_icon = $params['object_icon'];
-		$newObject->subject_name = $params['subject_name'];
-		$newObject->subject_icon = $params['subject_icon'];
-		$newObject->container_name = $params['container_name'];
-		$newObject->container_link = $params['container_link'];
-		$newObject->container_icon = $params['container_icon'];
-		$newObject->container_type = $params['container_type'];
-		$newObject->salmon_link = $params['salmon_link'];
-		$newObject->published = $published;
-		$newObject->verb = $verb;
-		//error_log("check to create group");
-		$group = false;
-	error_log("saved".$params['object_type']);
-		if (trim($params['object_type']) == "http://activitystrea.ms/schema/1.0/group") {
-			error_log("LOOK FOR GROUP");
-			$description = @current($entry->xpath("//activity:object/atom:summary"));
-			$obj = foreign_objects_get_group($params, 'object');
-			error_log("LOOK FOR GROUP".$obj->name);
-		}
-		if ($provenance) {
-			// dont create objects as it should be an action over a local one
-			// TODO find destination object here
-		} else {
-		if (($container_type == "http://elgg.org/activitystreams/types/group/" || $container_type == "http://activitystrea.ms/schema/1.0/group") && $params['container_link']) {
-			$group = foreign_objects_get_group($params);
-		}
-		if ($params['object_type'] == "http://activitystrea.ms/schema/1.0/event") {
-			$entry->registerXPathNamespace('cal', 'urn:ietf:params:xml:ns:xcal');
-			$start_date = @current($entry->xpath("//activity:object/cal:dtstart"));
-			$end_date = @current($entry->xpath("//activity:object/cal:dtend"));
-			$description = @current($entry->xpath("//activity:object/atom:summary"));
-			$obj = foreign_object_create_object($newObject, 'event_calendar', $group, $params, $description);
-			if (isset($start_date))
-				$obj->start_date = fo_getdate($start_date);
-			if (isset($end_date))
-				$obj->end_date = fo_getdate($end_date);
-			$start_date = getdate($obj->start_date);
-			$end_date = getdate($obj->end_date);
-			$obj->start_time = ($start_date['hours']*60)+$start_date['minutes'];
-			$obj->end_time = ($end_date['hours']*60)+$end_date['minutes'];
-			if (($obj->start_time == 0) && ($obj->end_time == 0))
-				$obj->allday = true;
-		}
-		elseif ($params['object_type'] == "http://elgg.org/activitystreams/types/object/page") {
-			$description = @current($entry->xpath("//activity:object/atom:summary"));
-			$obj = foreign_object_create_object($newObject, 'page', $group, $params, $description);
-		}
-		elseif ($params['object_type'] == "http://elgg.org/activitystreams/types/object/file" || $params['object_type'] == "http://activitystrea.ms/schema/1.0/file") {
-			$description = @current($entry->xpath("//activity:object/atom:summary"));
-			$download_link = @current($entry->xpath("//activity:object/atom:link[attribute::rel='enclosure']/@href"));
-			$download_mime = @current($entry->xpath("//activity:object/atom:link[attribute::rel='enclosure']/@type"));
-			$obj = foreign_object_create_object($newObject, 'file', $group, $params, $description);
-			$obj->download_link = $download_link;
-			$obj->mimetype = $download_mime;
-		}
-
-		elseif ($params['object_type'] == "http://elgg.org/activitystreams/types/object/page_top") {
-			$description = @current($entry->xpath("//activity:object/atom:summary"));
-			$obj = foreign_object_create_object($newObject, 'page_top', $group, $params, $description);
-		}
-		elseif ($params['object_type'] == "http://elgg.org/activitystreams/types/object/bookmarks" || $params['object_type'] == "http://activitystrea.ms/schema/1.0/bookmark") {
-			$description = @current($entry->xpath("//activity:object/atom:summary"));
-			$obj = foreign_object_create_object($newObject, 'bookmarks', $group, $params, $description);
-			error_log("BOOKMARK ARRIVED:".$description.":".$obj->description);
-			$obj->address = @current($entry->xpath("//activity:object/atom:link[attribute::rel='related']/@href"));
-		}
-		elseif ($params['object_type'] == "http://elgg.org/activitystreams/types/object/blog" || $params['object_type'] == "http://activitystrea.ms/schema/1.0/article") {
-			$description = @current($entry->xpath("//activity:object/atom:content"));
-			$obj = foreign_object_create_object($newObject, 'blog', $group, $params, $description);
-		}
-		}
-		if ($params['object_type'] == "http://activitystrea.ms/schema/1.0/comment") {
-			$entry->registerXPathNamespace('thr', 'http://purl.org/syndication/thread/1.0');
-			$description = @current($entry->xpath("activity:object/atom:content"));
-			$parent_uri = @current($entry->xpath("//activity:object/thr:in-reply-to/@ref"));
-			$action = 'comment';
-			$action_view = 'annotation/annotate';
-			error_log("comment for".$parent_uri);
-			if ($parent_uri) {
-				$entity = null;
-				$base_path = $CONFIG->wwwroot."pg/entities/";
-				if (strpos($parent_uri, $base_path) === 0) {
-					$parent_id = substr($parent_uri, strlen($base_path));
-					$entity = get_entity($parent_id);
-				}
-				else {
-					$options = array('metadata_name'=>'atom_id', 'metadata_value' => $parent_uri, 'owner_guid'=>ELGG_ENTITIES_ANY_VALUE);
-        				$candidates = elgg_get_entities_from_metadata($options);
-					if ($candidates) {
-						$entity = $candidates[0];
-						error_log("really found");
-					}
-
-				}
-				if ($entity) {
-					$annotation_id = create_annotation($entity->getGUID(), 'generic_comment', $description, '', $user->guid, ACCESS_PUBLIC);
-					if ($provenance)
-						foreign_objects_save_comment_provenance($annotation_id, $provenance);
-					error_log("found parent!".$entity->getGUID());
-					$obj = $entity;
-				}
-			}
-	//		$obj = foreign_object_create_object($newObject, 'page', $group, $params, $description);
-		}
-
-
-		if ($user && $obj) {
-			switch($params['verb']) {
-				case 'http://activitystrea.ms/schema/1.0/join':
-					error_log("GROUP JOIN!!".$obj->name);
-					if ($obj instanceof ElggGroup) {
-						$obj->join($user);
-						elgg_set_ignore_access(false);
-						return;
-					}
-					break;
-				case 'http://activitystrea.ms/schema/1.0/leave':
-					if ($obj instanceof ElggGroup) {
-						$obj->leave($user);
-						elgg_set_ignore_access(false);
-						return;
-					}
-					break;
-			}
-		}
-		if (!$obj)
-			$obj = $newObject;
-		elgg_set_ignore_access(false);
-		//error_log(" ** notification:".$subject_link);
-		if ($provenance && $action && $action_view)
-			if (add_to_river($action_view, $action, $user->getGUID(), $obj->getGUID(), ACCESS_PUBLIC, $updated, $annotation_id, true))
-				error_log(" ** river2 ok:".$params['id']);
-		elseif (!$provenance)
-			if (add_to_river('foreign_objects/river', 'foreign_notification', $user->getGUID(), $obj->getGUID(), ACCESS_PUBLIC, $updated, $annotation_id, true))
-				error_log(" ** river ok:".$params['id']);
-	//	if (add_to_river('foreign_objects/river', 'salmon_notification', $newObject->getGUID(), $obj->getGUID(), ACCESS_PUBLIC, $updated, 0, true))
-	//		error_log(" ** river ok:".$params['id']);
-	}
-    }
-	
+	return null;
 }
 
 function foreign_objects_init() {
 	register_entity_type('object', 'foreign_notification');
 	register_entity_type('object', 'salmon_notification');
-	foreign_objects_updatedb();
         elgg_extend_view('css','foreign_objects/css');
 
 	register_plugin_hook('foreign_notification', 'foreign_notification', 'foreign_objects_notification_hook');
+	//register_entity_url_handler('foreign_object_url','object', 'page_top');
+        register_entity_url_handler('foreign_object_url','user', 'all');
+        //register_entity_url_handler('foreign_object_url','object', 'bookmarks');
+        register_entity_url_handler('foreign_object_url','object', 'event_calendar');
+        //register_entity_url_handler('foreign_object_url','object', 'file');
+
+	register_plugin_hook('entity:icon:url', 'object', 'foreign_objects_icon_hook');
+	register_plugin_hook('entity:icon:url', 'group', 'foreign_objects_groupicon_hook');
+	register_plugin_hook('entity:icon:url', 'user', 'foreign_objects_groupicon_hook');
+
+	//register_plugin_hook('container_permissions_check', 'object', 'foreign_objects_container_perms_hook');
+	register_plugin_hook('container_permissions_check', 'all', 'foreign_objects_container_perms_hook');
+	register_plugin_hook('permissions_check', 'object', 'foreign_objects_perms_hook');
+	register_plugin_hook('permissions_check', 'group', 'foreign_objects_perms_hook');
+
+	elgg_extend_view("tools/object", "foreign_objects/icon");
+
+
 	//register_page_handler('pshb','pshb_page_handler');
 	
 }

views/default/blog/listing.php

+<?php
+
+	/**
+	 * Elgg blog listing
+	 * 
+	 * @package ElggBlog
+	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
+	 * @author Curverider Ltd <info@elgg.com>
+	 * @copyright Curverider Ltd 2008-2010
+	 * @link http://elgg.com/
+	 */
+
+		$owner = get_entity($vars['entity']->container_guid);
+		if (!$owner)
+			$owner = get_entity($vars['entity']->owner_guid);
+		$friendlytime = friendly_time($vars['entity']->time_created);
+		$icon = elgg_view(
+				"profile/icon", array(
+										'entity' => $owner,
+										'size' => 'small',
+									  )
+			);
+		$info = "<p>" . elgg_echo('blog') . ": <b><a href=\"{$vars['entity']->getURL()}\">{$vars['entity']->title}</a>". elgg_view('tools/object', $vars) ."</b></p>";
+		if ($owner)
+			$info .= "<p class=\"owner_timestamp\"><a href=\"{$owner->getURL()}\">{$owner->name}</a> {$friendlytime}</p>";
+		echo elgg_view_listing($icon,$info);
+
+?>

views/default/foreign_objects/css.php

 
-.river_object_top_page_foreign_notification, .river_object_page_foreign_notification, .river_object_bookmarks_foreign_notification, .river_object_foreign_notification_foreign_notification {
+.river_object_blog_foreign_notification, .river_object_top_page_foreign_notification, .river_object_page_foreign_notification, .river_object_bookmarks_foreign_notification, .river_object_foreign_notification_foreign_notification {
       background: url(<?php echo $vars['url']; ?>mod/foreign_objects/graphics/foreign_notification.png) no-repeat left -1px;
 }

views/default/foreign_objects/icon.php

+<?php
+	$entity = $vars['entity'];
+	if ($entity->foreign) {
+		$link = $entity->link;
+		$image = '<a href="'.$link.'"><img title="'.elgg_echo("foreign_objects:remote").'" src=" '.$vars['url'].'mod/foreign_objects/graphics/foreign.png" /></a>';
+?>
+ <div style="display:inline-block">
+<?php
+        echo $image;
+?>
+ </div>
+<?php
+        }
+?>

views/default/foreign_objects/river.php

 <?php
-
-	$remote = get_entity($vars['item']->subject_guid); // $statement->getSubject();
+	$remote = get_entity($vars['item']->object_guid);
+	$subject = get_entity($vars['item']->subject_guid);
+	$object = get_entity($remote->object_guid);
+	error_log($remote->title.$vars['item']->subject_guid);
+	error_log($remote->description);
+	// get_entity($vars['item']->subject_guid); // $statement->getSubject();
 	$subject_name = $remote->subject_name;
 	$object_name = $remote->object_name;
 	$subject_link = $remote->subject_link;
 	//error_log("body:".$title);
 	$icon = '';
 	if ($remote->container_icon && ($remote->container_type == 'http://elgg.org/activitystreams/types/group/' || $remote->container_type == 'http://activitystrea.ms/schema/1.0/group')) {
-		$icon .= '<a href="'.$remote->container_link.'"><img style="right:0;top:0;position:relative;float:right;display:block;" src="'.$remote->container_icon.'" /></a>';
+		//$icon .= '<a href="'.$remote->container_link.'"><img style="right:0;top:0;position:relative;float:right;display:block;" src="'.$remote->container_icon.'" /></a>';
 	}
 	if (strpos($title, "href") === false) {
-		if ($subject_name && !empty($subject_name))
-			$title = $icon.str_replace($subject_name, '<a href="'.$subject_link.'">'.$subject_name.'</a>', $title);
-		if ($object_name && !empty($object_name) && $object_name != $subject_name)
-			$title = str_replace($object_name, '<a href="'.$object_link.'">'.$object_name.'</a><br />', $title);
+		if ($subject && $subject_name && !empty($subject_name))
+			$title = $icon.str_replace($subject_name, '<a href="'.$subject->getURL().'">'.$subject_name.'</a>', $title);
+		if ($object && $object_name && !empty($object_name) && $object_name != $subject_name)
+			$title = str_replace($object_name, '<a href="'.$object->getURL().'">'.$object_name.'</a><br />', $title);
 	}
-	if ($title == $remote->title) {
-		$title .= '  by <a href="'.$subject_link.'">'.$subject_name.'</a>';
-		$title .= '  to <a href="'.$object_link.'">'.$object_name.'</a>';
+	if ($object && $title == $remote->title) {
+		$title .= '  by <a href="'.$subject->getURL().'">'.$subject_name.'</a>';
+		$title .= '  to <a href="'.$object->getURL().'">'.$object_name.'</a>';
 	}
 	$title = str_replace('more', '', $title);
-	$title .= '  <a href="'.$object_link.'">more...</a>';
+	if ($object)
+		$title .= '  <a href="'.$object->getURL().'">more...</a>';
 	$title .= '<div class="clearfloat"></div>';
 	$string .= $title;
 	//$string .= $remote->container_type;

views/default/foreign_objects/riverproxy.php

+<?php
+	//echo elgg_view();
+	$item = $vars['item'];
+	$remote = get_entity($item->object_guid);
+	//error_log("riverproxy".$item->subject_guid.":".$item->object_guid.":".$remote->view);
+	$item->object_guid = $remote->object_guid;
+	$item->action_type = $remote->action_type;
+	if (!get_entity($item->subject_guid))
+		error_log("subject doesnt exist ".$item->subject_guid.$remote->subject_name."\n".$remote->title.":".$item->annotation_id);
+	if (get_entity($remote->object_guid) && get_entity($item->subject_guid))
+		echo elgg_view($remote->view, array('item'=>$item));
+	elseif (get_entity($item->subject_guid))
+		echo "error".$remote->object_guid;
+	else
+		echo "subject error".$item->subject_guid;
+?>

views/default/groups/grouplisting.php

 
 	//for admins display the feature or unfeature option
 	if($vars['entity']->featured_group == "yes"){
-		$url = elgg_add_action_tokens_to_url($vars['url'] . "action/groups/featured?group_guid=" . $vars['entity']->guid . "&action=unfeature");
+		$url = elgg_add_action_tokens_to_url($vars['url'] . "action/groups/featured?group_guid=" . $vars['entity']->guid . "&action_name=unfeature");
 		$wording = elgg_echo("groups:makeunfeatured");
 	}else{
-		$url = elgg_add_action_tokens_to_url($vars['url'] . "action/groups/featured?group_guid=" . $vars['entity']->guid . "&action=feature");
+		$url = elgg_add_action_tokens_to_url($vars['url'] . "action/groups/featured?group_guid=" . $vars['entity']->guid . "&action_name=feature");
 		$wording = elgg_echo("groups:makefeatured");
 	}
 

views/default/groups/groupprofile.php

File contents unchanged.

views/default/groups/icon.php

 
 <div class="groupicon">
 <?php 
-if ($vars['entity']->foreign) {
+/*if ($vars['entity']->foreign) {
 	$url = $vars['entity']->link;
 	$icon_url = str_replace('tiny',$vars['size'], $vars['entity']->icon_link);
 }
-else {
+else {*/
 	$url = $vars['entity']->getURL();
 	$icon_url = $vars['entity']->getIcon($vars['size']);
-}
+//}
 	
 ?>
-<a href="<?php echo $url; ?>" class="icon" ><img src="<?php echo $icon_url; ?>" border="0" <?php echo $align; ?> title="<?php echo $name; ?>" <?php echo $vars['js']; ?> /></a>
+<a href="<?php echo $url; ?>" class="icon" ><img src="<?php echo $icon_url; ?>" border="0" <?php echo $align; ?> title="<?php echo $vars['entity']->name; ?>" <?php echo $vars['js']; ?> /></a>
 </div>
 
 <?php

views/default/object/bookmarks.php

-<?php
-
-	/**
-	 * Elgg bookmark view
-	 * 
-	 * @package ElggBookmarks
-	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
-	 * @author Curverider <info@elgg.com>
-	 * @copyright Curverider Ltd 2008-2010
-	 * @link http://elgg.org/
-	 */
-
-	$owner = $vars['entity']->getOwnerEntity();
-	$friendlytime = friendly_time($vars['entity']->time_created);
-
-	if (get_context() == "search" || !($vars['full'])) {
-
-		if (get_input('search_viewtype') == "gallery") {
-
-			$parsed_url = parse_url($vars['entity']->address);
-			$faviconurl = $parsed_url['scheme'] . "://" . $parsed_url['host'] . "/favicon.ico";
-		
-			$info = "<p class=\"shares_gallery_title\">". elgg_echo("bookmarks:shared") .": <a href=\"{$vars['entity']->getURL()}\">{$vars['entity']->title}</a> (<a href=\"{$vars['entity']->address}\">".elgg_echo('bookmarks:visit')."</a>)" . elgg_view("tools/object", $vars) . "</p>";
-			$info .= "<p class=\"shares_gallery_user\">By: <a href=\"{$vars['url']}pg/bookmarks/{$owner->username}\">{$owner->name}</a> <span class=\"shared_timestamp\">{$friendlytime}</span></p>";
-			$numcomments = elgg_count_comments($vars['entity']);
-			if ($numcomments)
-				$info .= "<p class=\"shares_gallery_comments\"><a href=\"{$vars['entity']->getURL()}\">".sprintf(elgg_echo("comments")). " (" . $numcomments . ")</a></p>";
-			
-			//display 
-			echo "<div class=\"share_gallery_view\">";
-			echo "<div class=\"share_gallery_info\">" . $info . "</div>";
-			echo "</div>";
-
-
-		} else {
-
-			$parsed_url = parse_url($vars['entity']->address);
-			$faviconurl = $parsed_url['scheme'] . "://" . $parsed_url['host'] . "/favicon.ico";
-			if (@file_exists($faviconurl)) {
-				$icon = "<img src=\"{$faviconurl}\" />";
-			} else {
-				if ($vars['entity']->foreign) {
-					$container = get_entity($vars['entity']->container_guid);
-					$notification = get_entity($vars['entity']->notification);
-					if ($container)
-						$icon = "<a href='".$container->getURL()."'><img src='".$container->getIcon()."' /></a>";
-					else
-						$icon = "<a href='".$notification->subject_link."'><img src='".$notification->subject_icon."' /></a>";
-				}
-				else
-				$icon = elgg_view(
-					"profile/icon", array(
-										'entity' => $owner,
-										'size' => 'small',
-									  )
-				);
-
-			}
-		
-			if ($vars['entity']->foreign)
-				$info = "<p class=\"shares_gallery_title\">". elgg_echo("bookmarks:shared") .": <a href=\"{$vars['entity']->link}\">{$vars['entity']->title}</a> (<a href=\"{$vars['entity']->address}\">".elgg_echo('bookmarks:visit')."</a>) (remote)" . elgg_view("tools/object", $vars) . "</p>";
-			else
-				$info = "<p class=\"shares_gallery_title\">". elgg_echo("bookmarks:shared") .": <a href=\"{$vars['entity']->getURL()}\">{$vars['entity']->title}</a> (<a href=\"{$vars['entity']->address}\">".elgg_echo('bookmarks:visit')."</a>)" . elgg_view("tools/object", $vars) . "</p>";
-			$info .= "<p class=\"owner_timestamp\"><a href=\"{$vars['url']}pg/bookmarks/{$owner->username}\">{$owner->name}</a> {$friendlytime}";
-			$numcomments = elgg_count_comments($vars['entity']);
-			if ($numcomments)
-				$info .= ", <a href=\"{$vars['entity']->getURL()}\">".sprintf(elgg_echo("comments")). " (" . $numcomments . ")</a>";
-		    $info .= "</p>";
-			echo elgg_view_listing($icon, $info);
-
-		}
-		
-	} else {
-
-?>
-	<?php echo elgg_view_title(elgg_echo('bookmarks:shareditem'), false); ?>
-	<div class="contentWrapper">
-	<div class="sharing_item">
-	
-		<div class="sharing_item_title">
-			<h3>
-				<a href="<?php echo $vars['entity']->address; ?>"><?php echo $vars['entity']->title; ?></a>
-			</h3>
-		</div>
-		<div class="sharing_item_owner">
-			<p> 
-				<b><a href="<?php echo $vars['url']; ?>pg/bookmarks/<?php echo $owner->username; ?>"><?php echo $owner->name; ?></a></b> 
-				<?php echo $friendlytime; ?>
-			</p>
-		</div>
-		<div class="sharing_item_description">
-				<?php echo elgg_view('output/longtext', array('value' => $vars['entity']->description)); ?>
-		</div>
-<?php
-
-	$tags = $vars['entity']->tags;
-	if (!empty($tags)) {
-
-?>
-		<div class="sharing_item_tags">
-			<p>
-				<?php echo elgg_view('output/tags',array('value' => $vars['entity']->tags)); ?>
-			</p>
-		</div>
-<?php
-
-	}
-
-?>
-		<div class="sharing_item_address">
-			<p>
-				<?php 
-
-					//echo elgg_view('output/url',array('value' => $vars['entity']->address));
-				
-				?>
-				<a href="<?php echo $vars['entity']->address; ?>"><?php echo elgg_echo('bookmarks:visit'); ?></a>
-			</p>
-		</div>		
-		<?php
-
-			if ($vars['entity']->canEdit()) {
-		
-		?>
-		<div class="sharing_item_controls">
-			<p>
-				<a href="<?php echo $vars['url']; ?>mod/bookmarks/add.php?bookmark=<?php echo $vars['entity']->getGUID(); ?>"><?php echo elgg_echo('edit'); ?></a> &nbsp;