Some work at the retractions.

This commit is contained in:
Michael Vogel 2016-03-02 23:28:20 +01:00
parent 1f7f964bb7
commit 66919761ab
2 changed files with 230 additions and 159 deletions

View File

@ -116,13 +116,13 @@ function diaspora_dispatch($importer,$msg,$attempt=1) {
$ret = diaspora_retraction($importer,$xmlbase->retraction,$msg); $ret = diaspora_retraction($importer,$xmlbase->retraction,$msg);
} }
elseif($xmlbase->signed_retraction) { elseif($xmlbase->signed_retraction) {
//$tempfile = tempnam(get_temppath(), "diaspora-signed_retraction"); $tempfile = tempnam(get_temppath(), "diaspora-signed_retraction");
//file_put_contents($tempfile, json_encode($data)); file_put_contents($tempfile, json_encode($data));
$ret = diaspora_signed_retraction($importer,$xmlbase->signed_retraction,$msg); $ret = diaspora_signed_retraction($importer,$xmlbase->signed_retraction,$msg);
} }
elseif($xmlbase->relayable_retraction) { elseif($xmlbase->relayable_retraction) {
//$tempfile = tempnam(get_temppath(), "diaspora-relayable_retraction"); $tempfile = tempnam(get_temppath(), "diaspora-relayable_retraction");
//file_put_contents($tempfile, json_encode($data)); file_put_contents($tempfile, json_encode($data));
$ret = diaspora_signed_retraction($importer,$xmlbase->relayable_retraction,$msg); $ret = diaspora_signed_retraction($importer,$xmlbase->relayable_retraction,$msg);
} }
elseif($xmlbase->photo) { elseif($xmlbase->photo) {

View File

@ -95,11 +95,11 @@ class diaspora {
$type = $fields->getName(); $type = $fields->getName();
switch ($type) { switch ($type) {
case "account_deletion": case "account_deletion": // Done
return true; return true;
//return self::import_account_deletion($importer, $fields); //return self::import_account_deletion($importer, $fields);
case "comment": case "comment": // Done
return true; return true;
//return self::import_comment($importer, $sender, $fields); //return self::import_comment($importer, $sender, $fields);
@ -107,40 +107,40 @@ class diaspora {
return true; return true;
//return self::import_conversation($importer, $fields); //return self::import_conversation($importer, $fields);
case "like": case "like": // Done
return true; return true;
//return self::import_like($importer, $sender, $fields); //return self::import_like($importer, $sender, $fields);
case "message": case "message": // Done
return true; return true;
//return self::import_message($importer, $fields); //return self::import_message($importer, $fields);
case "participation": // Not implemented case "participation": // Not implemented
return self::import_participation($importer, $fields); return self::import_participation($importer, $fields);
case "photo": case "photo": // Not needed
return self::import_photo($importer, $fields); return self::import_photo($importer, $fields);
case "poll_participation": // Not implemented case "poll_participation": // Not implemented
return self::import_poll_participation($importer, $fields); return self::import_poll_participation($importer, $fields);
case "profile": case "profile": // Done
return true; return true;
//return self::import_profile($importer, $fields); //return self::import_profile($importer, $fields);
case "request": case "request":
return self::import_request($importer, $fields); return self::import_request($importer, $fields);
case "reshare": case "reshare": // Done
return true; return true;
//return self::import_reshare($importer, $fields); //return self::import_reshare($importer, $fields);
case "retraction": case "retraction":
return self::import_retraction($importer, $fields); return self::import_retraction($importer, $fields);
case "status_message": case "status_message": // Done
//return true; return true;
return self::import_status_message($importer, $fields); //return self::import_status_message($importer, $fields);
default: default:
logger("Unknown message type ".$type); logger("Unknown message type ".$type);
@ -567,25 +567,25 @@ class diaspora {
} }
private function plink($addr, $guid) { private function plink($addr, $guid) {
$r = q("SELECT `url`, `nick`, `network` FROM `fcontact` WHERE `addr`='%s' LIMIT 1", dbesc($addr)); $r = q("SELECT `url`, `nick`, `network` FROM `fcontact` WHERE `addr`='%s' LIMIT 1", dbesc($addr));
// Fallback // Fallback
if (!$r) if (!$r)
return "https://".substr($addr,strpos($addr,"@")+1)."/posts/".$guid; return "https://".substr($addr,strpos($addr,"@")+1)."/posts/".$guid;
// Friendica contacts are often detected as Diaspora contacts in the "fcontact" table // Friendica contacts are often detected as Diaspora contacts in the "fcontact" table
// So we try another way as well. // So we try another way as well.
$s = q("SELECT `network` FROM `gcontact` WHERE `nurl`='%s' LIMIT 1", dbesc(normalise_link($r[0]["url"]))); $s = q("SELECT `network` FROM `gcontact` WHERE `nurl`='%s' LIMIT 1", dbesc(normalise_link($r[0]["url"])));
if ($s) if ($s)
$r[0]["network"] = $s[0]["network"]; $r[0]["network"] = $s[0]["network"];
if ($r[0]["network"] == NETWORK_DFRN) if ($r[0]["network"] == NETWORK_DFRN)
return(str_replace("/profile/".$r[0]["nick"]."/", "/display/".$guid, $r[0]["url"]."/")); return(str_replace("/profile/".$r[0]["nick"]."/", "/display/".$guid, $r[0]["url"]."/"));
if (self::is_redmatrix($r[0]["url"])) if (self::is_redmatrix($r[0]["url"]))
return $r[0]["url"]."/?f=&mid=".$guid; return $r[0]["url"]."/?f=&mid=".$guid;
return "https://".substr($addr,strpos($addr,"@")+1)."/posts/".$guid; return "https://".substr($addr,strpos($addr,"@")+1)."/posts/".$guid;
} }
private function import_account_deletion($importer, $data) { private function import_account_deletion($importer, $data) {
@ -681,158 +681,158 @@ class diaspora {
print_r($data); print_r($data);
die(); die();
/* /*
$guid = notags(unxmlify($xml->guid)); $guid = notags(unxmlify($xml->guid));
$subject = notags(unxmlify($xml->subject)); $subject = notags(unxmlify($xml->subject));
$diaspora_handle = notags(unxmlify($xml->diaspora_handle)); $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
$participant_handles = notags(unxmlify($xml->participant_handles)); $participant_handles = notags(unxmlify($xml->participant_handles));
$created_at = datetime_convert('UTC','UTC',notags(unxmlify($xml->created_at))); $created_at = datetime_convert('UTC','UTC',notags(unxmlify($xml->created_at)));
$parent_uri = $diaspora_handle . ':' . $guid; $parent_uri = $diaspora_handle . ':' . $guid;
$messages = $xml->message; $messages = $xml->message;
if(! count($messages)) { if(! count($messages)) {
logger('empty conversation'); logger('empty conversation');
return; return;
} }
$contact = self::get_allowed_contact_by_handle($importer, $sender, true) $contact = self::get_allowed_contact_by_handle($importer, $sender, true)
if (!$contact) if (!$contact)
return false; return false;
$conversation = null; $conversation = null;
$c = q("select * from conv where uid = %d and guid = '%s' limit 1", $c = q("select * from conv where uid = %d and guid = '%s' limit 1",
intval($importer['uid']), intval($importer["uid"]),
dbesc($guid) dbesc($guid)
); );
if(count($c)) if(count($c))
$conversation = $c[0]; $conversation = $c[0];
else { else {
$r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ", $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
intval($importer['uid']), intval($importer["uid"]),
dbesc($guid), dbesc($guid),
dbesc($diaspora_handle), dbesc($diaspora_handle),
dbesc(datetime_convert('UTC','UTC',$created_at)), dbesc(datetime_convert('UTC','UTC',$created_at)),
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc($subject), dbesc($subject),
dbesc($participant_handles) dbesc($participant_handles)
); );
if($r) if($r)
$c = q("select * from conv where uid = %d and guid = '%s' limit 1", $c = q("select * from conv where uid = %d and guid = '%s' limit 1",
intval($importer['uid']), intval($importer["uid"]),
dbesc($guid) dbesc($guid)
); );
if(count($c)) if(count($c))
$conversation = $c[0]; $conversation = $c[0];
} }
if(! $conversation) { if(! $conversation) {
logger('diaspora_conversation: unable to create conversation.'); logger('diaspora_conversation: unable to create conversation.');
return; return;
} }
foreach($messages as $mesg) { foreach($messages as $mesg) {
$reply = 0; $reply = 0;
$msg_guid = notags(unxmlify($mesg->guid)); $msg_guid = notags(unxmlify($mesg->guid));
$msg_parent_guid = notags(unxmlify($mesg->parent_guid)); $msg_parent_guid = notags(unxmlify($mesg->parent_guid));
$msg_parent_author_signature = notags(unxmlify($mesg->parent_author_signature)); $msg_parent_author_signature = notags(unxmlify($mesg->parent_author_signature));
$msg_author_signature = notags(unxmlify($mesg->author_signature)); $msg_author_signature = notags(unxmlify($mesg->author_signature));
$msg_text = unxmlify($mesg->text); $msg_text = unxmlify($mesg->text);
$msg_created_at = datetime_convert('UTC','UTC',notags(unxmlify($mesg->created_at))); $msg_created_at = datetime_convert('UTC','UTC',notags(unxmlify($mesg->created_at)));
$msg_diaspora_handle = notags(unxmlify($mesg->diaspora_handle)); $msg_diaspora_handle = notags(unxmlify($mesg->diaspora_handle));
$msg_conversation_guid = notags(unxmlify($mesg->conversation_guid)); $msg_conversation_guid = notags(unxmlify($mesg->conversation_guid));
if($msg_conversation_guid != $guid) { if($msg_conversation_guid != $guid) {
logger('diaspora_conversation: message conversation guid does not belong to the current conversation. ' . $xml); logger('diaspora_conversation: message conversation guid does not belong to the current conversation. ' . $xml);
continue; continue;
} }
$body = diaspora2bb($msg_text); $body = diaspora2bb($msg_text);
$message_id = $msg_diaspora_handle . ':' . $msg_guid; $message_id = $msg_diaspora_handle . ':' . $msg_guid;
$author_signed_data = $msg_guid . ';' . $msg_parent_guid . ';' . $msg_text . ';' . unxmlify($mesg->created_at) . ';' . $msg_diaspora_handle . ';' . $msg_conversation_guid; $author_signed_data = $msg_guid . ';' . $msg_parent_guid . ';' . $msg_text . ';' . unxmlify($mesg->created_at) . ';' . $msg_diaspora_handle . ';' . $msg_conversation_guid;
$author_signature = base64_decode($msg_author_signature); $author_signature = base64_decode($msg_author_signature);
if(strcasecmp($msg_diaspora_handle,$msg['author']) == 0) { if(strcasecmp($msg_diaspora_handle,$msg["author"]) == 0) {
$person = $contact; $person = $contact;
$key = $msg['key']; $key = $msg["key"];
} }
else { else {
$person = find_diaspora_person_by_handle($msg_diaspora_handle); $person = find_diaspora_person_by_handle($msg_diaspora_handle);
if(is_array($person) && x($person,'pubkey')) if(is_array($person) && x($person,'pubkey'))
$key = $person['pubkey']; $key = $person["pubkey"];
else { else {
logger('diaspora_conversation: unable to find author details'); logger('diaspora_conversation: unable to find author details');
continue; continue;
} }
} }
if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) { if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) {
logger('diaspora_conversation: verification failed.'); logger('diaspora_conversation: verification failed.');
continue; continue;
} }
if($msg_parent_author_signature) { if($msg_parent_author_signature) {
$owner_signed_data = $msg_guid . ';' . $msg_parent_guid . ';' . $msg_text . ';' . unxmlify($mesg->created_at) . ';' . $msg_diaspora_handle . ';' . $msg_conversation_guid; $owner_signed_data = $msg_guid . ';' . $msg_parent_guid . ';' . $msg_text . ';' . unxmlify($mesg->created_at) . ';' . $msg_diaspora_handle . ';' . $msg_conversation_guid;
$parent_author_signature = base64_decode($msg_parent_author_signature); $parent_author_signature = base64_decode($msg_parent_author_signature);
$key = $msg['key']; $key = $msg["key"];
if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) { if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) {
logger('diaspora_conversation: owner verification failed.'); logger('diaspora_conversation: owner verification failed.');
continue; continue;
} }
} }
$r = q("select id from mail where `uri` = '%s' limit 1", $r = q("select id from mail where `uri` = '%s' limit 1",
dbesc($message_id) dbesc($message_id)
); );
if(count($r)) { if(count($r)) {
logger('diaspora_conversation: duplicate message already delivered.', LOGGER_DEBUG); logger('diaspora_conversation: duplicate message already delivered.', LOGGER_DEBUG);
continue; continue;
} }
q("insert into mail ( `uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`) values ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')", q("insert into mail ( `uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`) values ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
intval($importer['uid']), intval($importer["uid"]),
dbesc($msg_guid), dbesc($msg_guid),
intval($conversation['id']), intval($conversation["id"]),
dbesc($person['name']), dbesc($person["name"]),
dbesc($person['photo']), dbesc($person["photo"]),
dbesc($person['url']), dbesc($person["url"]),
intval($contact['id']), intval($contact["id"]),
dbesc($subject), dbesc($subject),
dbesc($body), dbesc($body),
0, 0,
0, 0,
dbesc($message_id), dbesc($message_id),
dbesc($parent_uri), dbesc($parent_uri),
dbesc($msg_created_at) dbesc($msg_created_at)
); );
q("update conv set updated = '%s' where id = %d", q("update conv set updated = '%s' where id = %d",
dbesc(datetime_convert()), dbesc(datetime_convert()),
intval($conversation['id']) intval($conversation["id"])
); );
notification(array( notification(array(
'type' => NOTIFY_MAIL, 'type' => NOTIFY_MAIL,
'notify_flags' => $importer['notify-flags'], 'notify_flags' => $importer["notify-flags"],
'language' => $importer['language'], 'language' => $importer["language"],
'to_name' => $importer['username'], 'to_name' => $importer["username"],
'to_email' => $importer['email'], 'to_email' => $importer["email"],
'uid' =>$importer['uid'], 'uid' =>$importer["uid"],
'item' => array('subject' => $subject, 'body' => $body), 'item' => array('subject' => $subject, 'body' => $body),
'source_name' => $person['name'], 'source_name' => $person["name"],
'source_link' => $person['url'], 'source_link' => $person["url"],
'source_photo' => $person['thumb'], 'source_photo' => $person["thumb"],
'verb' => ACTIVITY_POST, 'verb' => ACTIVITY_POST,
'otype' => 'mail' 'otype' => 'mail'
)); ));
} }
*/ */
return true; return true;
} }
@ -1420,7 +1420,78 @@ print_r($data);
return $message_id; return $message_id;
} }
private function item_retraction($importer, $contact, $data) {
$target_guid = notags(unxmlify($data->target_guid));
$r = q("SELECT `id`, `parent`, `parent-uri`, `author-link` FROM `item` WHERE `guid` = '%s' AND `uid` = %d AND NOT `file` LIKE '%%[%%' LIMIT 1",
dbesc($target_guid),
intval($importer["uid"])
);
if (!$r)
return false;
// Only delete it if the author really fits
if (!link_compare($r[0]["author-link"],$contact["url"]))
return false;
// Currently we don't have a central deletion function that we could use in this case. The function "item_drop" doesn't work for that case
q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' , `title` = '' WHERE `id` = %d",
dbesc(datetime_convert()),
dbesc(datetime_convert()),
intval($r[0]["id"])
);
delete_thread($r[0]["id"], $r[0]["parent-uri"]);
// Now check if the retraction needs to be relayed by us
//
// The first item in the `item` table with the parent id is the parent. However, MySQL doesn't always
// return the items ordered by `item`.`id`, in which case the wrong item is chosen as the parent.
// The only item with `parent` and `id` as the parent id is the parent item.
$p = q("SELECT `origin` FROM `item` WHERE `parent` = %d AND `id` = %d LIMIT 1",
intval($r[0]["parent"]),
intval($r[0]["parent"])
);
if(count($p)) {
if($p[0]["origin"]) {
// Formerly we stored the signed text, the signature and the author in different fields.
// The new Diaspora protocol can have variable fields. We now store the data in correct order in a single field.
q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
intval($r[0]["id"]),
dbesc(json_encode($data))
);
// the existence of parent_author_signature would have meant the parent_author or owner
// is already relaying.
logger("relaying retraction");
proc_run("php", "include/notifier.php", "drop", $r[0]["id"]);
}
}
}
private function import_retraction($importer, $data) { private function import_retraction($importer, $data) {
$target_type = notags(unxmlify($data->target_type));
$author = notags(unxmlify($data->author));
$contact = self::get_contact_by_handle($importer["uid"], $author);
if (!$contact) {
logger("cannot find contact for author: ".$author);
return false;
}
switch ($target_type) {
case "Comment": case "Like": case "StatusMessage":
self::item_retraction($importer, $contact, $data);
break;
case "Person":
contact_remove($contact["id"]);
return true;
default:
logger("Unknown target type ".$target_type);
}
return true; return true;
} }
@ -1434,11 +1505,11 @@ print_r($data);
$provider_display_name = notags(unxmlify($data->provider_display_name)); $provider_display_name = notags(unxmlify($data->provider_display_name));
/// @todo enable support for polls /// @todo enable support for polls
if ($data->poll) { //if ($data->poll) {
foreach ($data->poll AS $poll) // foreach ($data->poll AS $poll)
print_r($poll); // print_r($poll);
die("poll!\n"); // die("poll!\n");
} //}
$contact = self::get_allowed_contact_by_handle($importer, $author, false); $contact = self::get_allowed_contact_by_handle($importer, $author, false);
if (!$contact) if (!$contact)
return false; return false;