require_once("include/group.php");
require_once("include/api.php");
+/**
+ * @brief This class contain functions to work with XML data
+ *
+ */
class xml {
function from_array($array, &$xml) {
}
}
}
+
/**
* @brief This class contain functions to create and send Diaspora XML files
*
*/
class diaspora {
+ /**
+ * @brief Dispatches public messages and find the fitting receivers
+ *
+ * @param array $msg The post that will be dispatched
+ *
+ * @return bool Was the message accepted?
+ */
public static function dispatch_public($msg) {
$enabled = intval(get_config("system", "diaspora_enabled"));
return $item_id;
}
+ /**
+ * @brief Dispatches the different message types to the different functions
+ *
+ * @param array $importer Array of the importer user
+ * @param array $msg The post that will be dispatched
+ *
+ * @return bool Was the message accepted?
+ */
public static function dispatch($importer, $msg) {
// The sender is the handle of the contact that sent the message.
//return self::import_comment($importer, $sender, $fields);
case "conversation":
- return true;
- //return self::import_conversation($importer, $fields);
+ //return true;
+ return self::import_conversation($importer, $fields);
case "like": // Done
return true;
//return self::import_profile($importer, $fields);
case "request":
+ //return true;
return self::import_request($importer, $fields);
case "reshare": // Done
return true;
//return self::import_reshare($importer, $fields);
- case "retraction":
- return self::import_retraction($importer, $fields);
-
- case "status_message": // Done
+ case "retraction": // Done
return true;
- //return self::import_status_message($importer, $fields);
+ //return self::import_retraction($importer, $sender, $fields);
+
+ case "status_message":
+ //return true;
+ return self::import_status_message($importer, $fields);
default:
logger("Unknown message type ".$type);
}
$type = $element->getName();
+ $orig_type = $type;
// All retractions are handled identically from now on.
// In the new version there will only be "retraction".
$signed_data .= $entry;
}
- if (!in_array($fieldname, array("parent_author_signature", "target_author_signature")))
+ if (!in_array($fieldname, array("parent_author_signature", "target_author_signature")) OR
+ ($orig_type == "relayable_retraction"))
xml::copy($entry, $fields, $fieldname);
}
return rsa_verify($signed_data, $author_signature, $key, "sha256");
}
+ /**
+ * @brief Fetches the public key for a given handle
+ *
+ * @param string $handle The handle
+ *
+ * @return string The public key
+ */
private function get_key($handle) {
logger("Fetching diaspora key for: ".$handle);
return "";
}
+ /**
+ * @brief Fetches data for a given handle
+ *
+ * @param string $handle The handle
+ *
+ * @return array the queried data
+ */
private function get_person_by_handle($handle) {
$r = q("SELECT * FROM `fcontact` WHERE `network` = '%s' AND `addr` = '%s' LIMIT 1",
return $person;
}
+ /**
+ * @brief Updates the fcontact table
+ *
+ * @param array $arr The fcontact data
+ * @param bool $update Update or insert?
+ *
+ * @return string The id of the fcontact entry
+ */
private function add_fcontact($arr, $update = false) {
/// @todo Remove this function from include/network.php
if ($level > 5)
return false;
- // This will not work if the server is not a Diaspora server
+ // This will work for Diaspora and newer Friendica servers
$source_url = $server."/p/".$guid.".xml";
$x = fetch_url($source_url);
if(!$x)
return false;
- /// @todo - should maybe solved by the dispatcher
$source_xml = parse_xml_string($x, false);
if (!is_object($source_xml))
if($message_id AND $parent_item["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.
+ // We now store the raw data so that we are more flexible.
q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
intval($message_id),
dbesc(json_encode($data))
}
private function import_conversation($importer, $data) {
+ // @todo
print_r($data);
die();
/*
$datarray["body"] = self::construct_like_body($contact, $parent_item, $guid);
$message_id = item_store($datarray);
- //print_r($datarray);
+ // print_r($datarray);
// If we are the origin of the parent we store the original data and notify our followers
if($message_id AND $parent_item["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.
+ // We now store the raw data so that we are more flexible.
q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
intval($message_id),
dbesc(json_encode($data))
}
private function import_request($importer, $data) {
-print_r($data);
+ // @todo
+ print_r($data);
/*
$author = unxmlify($xml->author);
$recipient = unxmlify($xml->recipient);
if (!$contact)
return false;
-// if (self::message_exists($importer["uid"], $guid))
-// return false;
+ if (self::message_exists($importer["uid"], $guid))
+ return false;
$original_item = self::get_original_item($root_guid, $root_author, $author);
if (!$original_item)
$datarray["object-type"] = $original_item["object-type"];
self::fetch_guid($datarray);
- //$message_id = item_store($datarray);
- print_r($datarray);
+ $message_id = item_store($datarray);
+ // print_r($datarray);
return $message_id;
}
private function item_retraction($importer, $contact, $data) {
+ $target_type = notags(unxmlify($data->target_type));
$target_guid = notags(unxmlify($data->target_guid));
+ $author = notags(unxmlify($data->author));
+
+ $person = self::get_person_by_handle($author);
+ if (!is_array($person)) {
+ logger("unable to find author detail for ".$author);
+ return false;
+ }
$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),
return false;
// Only delete it if the author really fits
- if (!link_compare($r[0]["author-link"],$contact["url"]))
+ if (!link_compare($r[0]["author-link"],$person["url"]))
+ return false;
+
+ // Check if the sender is the thread owner
+ $p = q("SELECT `author-link`, `origin` FROM `item` WHERE `id` = %d",
+ intval($r[0]["parent"]));
+
+ // Only delete it if the parent author really fits
+ if (!link_compare($p[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
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"]) {
+ 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");
+ // Formerly we stored the signed text, the signature and the author in different fields.
+ // We now store the raw data so that we are more flexible.
+ q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
+ intval($r[0]["id"]),
+ dbesc(json_encode($data))
+ );
- proc_run("php", "include/notifier.php", "drop", $r[0]["id"]);
- }
+ // notify others
+ proc_run("php", "include/notifier.php", "drop", $r[0]["id"]);
}
}
- private function import_retraction($importer, $data) {
+ private function import_retraction($importer, $sender, $data) {
$target_type = notags(unxmlify($data->target_type));
- $author = notags(unxmlify($data->author));
- $contact = self::get_contact_by_handle($importer["uid"], $author);
+ $contact = self::get_contact_by_handle($importer["uid"], $sender);
if (!$contact) {
- logger("cannot find contact for author: ".$author);
+ logger("cannot find contact for sender: ".$sender." and user ".$importer["uid"]);
return false;
}
switch ($target_type) {
- case "Comment": case "Like": case "StatusMessage":
- self::item_retraction($importer, $contact, $data);
- break;
+ case "Comment":
+ case "Like":
+ case "Post": // "Post" will be supported in a future version
+ case "Reshare":
+ case "StatusMessage":
+ return self::item_retraction($importer, $contact, $data);;
case "Person":
contact_remove($contact["id"]);
default:
logger("Unknown target type ".$target_type);
+ return false;
}
return true;
}
if (!$contact)
return false;
- //if (self::message_exists($importer["uid"], $guid))
- // return false;
+ if (self::message_exists($importer["uid"], $guid))
+ return false;
$address = array();
if ($data->location)
$body = add_page_info_to_body($body, false, true);
}
- $str_tags = "";
-
- // This doesn't work. @todo Check if the "tag" field is filled in the "item_store" function.
- $cnt = preg_match_all("/@\[url=(.*?)\[\/url\]/ism", $body, $matches, PREG_SET_ORDER);
- if($cnt) {
- foreach($matches as $mtch) {
- if(strlen($str_tags))
- $str_tags .= ",";
- $str_tags .= "@[url=".$mtch[1]."[/url]";
- }
- }
-
$datarray["uid"] = $importer["uid"];
$datarray["contact-id"] = $contact["id"];
$datarray["network"] = NETWORK_DIASPORA;
$datarray["body"] = $body;
- $datarray["tag"] = $str_tags;
if ($provider_display_name != "")
$datarray["app"] = $provider_display_name;
$datarray["coord"] = $address["lat"]." ".$address["lng"];
self::fetch_guid($datarray);
- //$message_id = item_store($datarray);
- print_r($datarray);
+ $message_id = item_store($datarray);
+ // print_r($datarray);
logger("Stored item with message id ".$message_id, LOGGER_DEBUG);