Cleaned up the code
This commit is contained in:
parent
2c7fd1cb3c
commit
99f0746660
3 changed files with 85 additions and 123 deletions
|
@ -2242,7 +2242,7 @@ class diaspora {
|
|||
*
|
||||
* @return string the handle in the format user@domain.tld
|
||||
*/
|
||||
function my_handle($contact) {
|
||||
private function my_handle($contact) {
|
||||
if ($contact["addr"] != "")
|
||||
return $contact["addr"];
|
||||
|
||||
|
@ -2256,6 +2256,40 @@ class diaspora {
|
|||
return $nick."@".substr(App::get_baseurl(), strpos(App::get_baseurl(),"://") + 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates the envelope for the "fetch" endpoint
|
||||
*
|
||||
* @param string $msg The message that is to be transmitted
|
||||
* @param array $user The record of the sender
|
||||
*
|
||||
* @return string The envelope
|
||||
*/
|
||||
|
||||
function build_magic_envelope($msg, $user) {
|
||||
|
||||
$b64url_data = base64url_encode($msg);
|
||||
$data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
|
||||
|
||||
$key_id = base64url_encode(diaspora::my_handle($user));
|
||||
$type = "application/xml";
|
||||
$encoding = "base64url";
|
||||
$alg = "RSA-SHA256";
|
||||
$signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
|
||||
$signature = rsa_sign($signable_data, $user["prvkey"]);
|
||||
$sig = base64url_encode($signature);
|
||||
|
||||
$xmldata = array("me:env" => array("me:data" => $data,
|
||||
"@attributes" => array("type" => $type),
|
||||
"me:encoding" => $encoding,
|
||||
"me:alg" => $alg,
|
||||
"me:sig" => $sig,
|
||||
"@attributes2" => array("key_id" => $key_id)));
|
||||
|
||||
$namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
|
||||
|
||||
return xml::from_array($xmldata, $xml, false, $namespaces);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates the envelope for a public message
|
||||
*
|
||||
|
@ -2647,16 +2681,16 @@ class diaspora {
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief Sends a post
|
||||
* @brief Create a post (status message or reshare)
|
||||
*
|
||||
* @param array $item The item that will be exported
|
||||
* @param array $owner the array of the item owner
|
||||
* @param array $contact Target of the communication
|
||||
* @param bool $public_batch Is it a public post?
|
||||
*
|
||||
* @return int The result of the transmission
|
||||
* @return array
|
||||
* 'type' -> Message type ("status_message" or "reshare")
|
||||
* 'message' -> Array of XML elements of the status
|
||||
*/
|
||||
public static function send_status($item, $owner, $contact, $public_batch = false) {
|
||||
public static function build_status($item, $owner) {
|
||||
|
||||
$myaddr = self::my_handle($owner);
|
||||
|
||||
|
@ -2719,8 +2753,24 @@ class diaspora {
|
|||
|
||||
$type = "status_message";
|
||||
}
|
||||
return array("type" => $type, "message" => $message);
|
||||
}
|
||||
|
||||
return self::build_and_transmit($owner, $contact, $type, $message, $public_batch, $item["guid"]);
|
||||
/**
|
||||
* @brief Sends a post
|
||||
*
|
||||
* @param array $item The item that will be exported
|
||||
* @param array $owner the array of the item owner
|
||||
* @param array $contact Target of the communication
|
||||
* @param bool $public_batch Is it a public post?
|
||||
*
|
||||
* @return int The result of the transmission
|
||||
*/
|
||||
public static function send_status($item, $owner, $contact, $public_batch = false) {
|
||||
|
||||
$status = diaspora::build_status($item, $owner);
|
||||
|
||||
return self::build_and_transmit($owner, $contact, $status["type"], $status["message"], $public_batch, $item["guid"]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -15,6 +15,7 @@ function fetch_init($a){
|
|||
|
||||
$guid = $a->argv[2];
|
||||
|
||||
// Fetch the item
|
||||
$item = q("SELECT `uid`, `title`, `body`, `guid`, `contact-id`, `private`, `created`, `app`, `location`, `coord`
|
||||
FROM `item` WHERE `wall` AND NOT `private` AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1",
|
||||
dbesc($guid), NETWORK_DFRN, NETWORK_DIASPORA);
|
||||
|
@ -22,49 +23,8 @@ function fetch_init($a){
|
|||
header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
|
||||
killme();
|
||||
}
|
||||
$post = array();
|
||||
|
||||
$reshared = diaspora::is_reshare($item[0]["body"]);
|
||||
|
||||
if ($reshared) {
|
||||
$nodename = "reshare";
|
||||
$post["root_diaspora_id"] = $reshared["root_handle"];
|
||||
$post["root_guid"] = $reshared["root_guid"];
|
||||
$post["guid"] = $item[0]["guid"];
|
||||
$post["diaspora_handle"] = diaspora::handle_from_contact($item[0]["contact-id"]);
|
||||
$post["public"] = (!$item[0]["private"] ? 'true':'false');
|
||||
$post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]);
|
||||
} else {
|
||||
|
||||
$location = array();
|
||||
|
||||
if ($item[0]["location"] != "")
|
||||
$location["address"] = $item[0]["location"];
|
||||
|
||||
if ($item[0]["coord"] != "") {
|
||||
$coord = explode(" ", $item[0]["coord"]);
|
||||
$location["lat"] = $coord[0];
|
||||
$location["lng"] = $coord[1];
|
||||
}
|
||||
|
||||
$body = bb2diaspora($item[0]["body"]);
|
||||
|
||||
if(strlen($item[0]["title"]))
|
||||
$body = "## ".html_entity_decode($item[0]["title"])."\n\n".$body;
|
||||
|
||||
$nodename = "status_message";
|
||||
$post["raw_message"] = str_replace("&", "&", $body);
|
||||
$post["location"] = $location;
|
||||
$post["guid"] = $item[0]["guid"];
|
||||
$post["diaspora_handle"] = diaspora::handle_from_contact($item[0]["contact-id"]);
|
||||
$post["public"] = (!$item[0]["private"] ? 'true':'false');
|
||||
$post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]);
|
||||
$post["provider_display_name"] = $item[0]["app"];
|
||||
}
|
||||
|
||||
$data = array("XML" => array("post" => array($nodename => $post)));
|
||||
$xml = xml::from_array($data, $xmlobj);
|
||||
|
||||
// Fetch some data from the author (We could combine both queries - but I think this is more readable)
|
||||
$r = q("SELECT `user`.`prvkey`, `contact`.`addr`, `user`.`nickname`, `contact`.`nick` FROM `user`
|
||||
INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid`
|
||||
WHERE `user`.`uid` = %d", intval($item[0]["uid"]));
|
||||
|
@ -72,35 +32,19 @@ function fetch_init($a){
|
|||
header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
|
||||
killme();
|
||||
}
|
||||
|
||||
$user = $r[0];
|
||||
|
||||
$b64url_data = base64url_encode($xml);
|
||||
$data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
|
||||
$status = diaspora::build_status($item[0], $user);
|
||||
$data = array("XML" => array("post" => array($status["type"] => $status["message"])));
|
||||
$xml = xml::from_array($data, $xmlobj);
|
||||
|
||||
$key_id = base64url_encode(diaspora::my_handle($user));
|
||||
$type = "application/xml";
|
||||
$encoding = "base64url";
|
||||
$alg = "RSA-SHA256";
|
||||
$signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
|
||||
$signature = rsa_sign($signable_data, $user["prvkey"]);
|
||||
$sig = base64url_encode($signature);
|
||||
|
||||
$xmldata = array("me:env" => array("me:data" => $data,
|
||||
"@attributes" => array("type" => $type),
|
||||
"me:encoding" => $encoding,
|
||||
"me:alg" => $alg,
|
||||
"me:sig" => $sig,
|
||||
"@attributes2" => array("key_id" => $key_id)));
|
||||
|
||||
$namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
|
||||
// Send the envelope
|
||||
header("Content-Type: application/magic-envelope+xml; charset=utf-8");
|
||||
echo diaspora::build_magic_envelope($xml, $user);
|
||||
|
||||
//header("Content-Type: application/xml; charset=utf-8");
|
||||
//echo $xml;
|
||||
//killme();
|
||||
|
||||
$envelope = xml::from_array($xmldata, $xml, false, $namespaces);
|
||||
header("Content-Type: application/magic-envelope+xml; charset=utf-8");
|
||||
echo $envelope;
|
||||
killme();
|
||||
}
|
||||
|
|
60
mod/p.php
60
mod/p.php
|
@ -19,62 +19,30 @@ function p_init($a){
|
|||
|
||||
$guid = strtolower(substr($guid, 0, -4));
|
||||
|
||||
$item = q("SELECT `title`, `body`, `guid`, `contact-id`, `private`, `created`, `app` FROM `item` WHERE `uid` = 0 AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1",
|
||||
// Fetch the item
|
||||
$item = q("SELECT `uid`, `title`, `body`, `guid`, `contact-id`, `private`, `created`, `app`, `location`, `coord`
|
||||
FROM `item` WHERE `wall` AND NOT `private` AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1",
|
||||
dbesc($guid), NETWORK_DFRN, NETWORK_DIASPORA);
|
||||
if (!$item) {
|
||||
header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
|
||||
killme();
|
||||
}
|
||||
|
||||
$post = array();
|
||||
|
||||
$reshared = diaspora::is_reshare($item[0]["body"]);
|
||||
|
||||
if ($reshared) {
|
||||
$nodename = "reshare";
|
||||
$post["root_diaspora_id"] = $reshared["root_handle"];
|
||||
$post["root_guid"] = $reshared["root_guid"];
|
||||
$post["guid"] = $item[0]["guid"];
|
||||
$post["diaspora_handle"] = diaspora::handle_from_contact($item[0]["contact-id"]);
|
||||
$post["public"] = (!$item[0]["private"] ? 'true':'false');
|
||||
$post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]);
|
||||
} else {
|
||||
|
||||
$body = bb2diaspora($item[0]["body"]);
|
||||
|
||||
if(strlen($item[0]["title"]))
|
||||
$body = "## ".html_entity_decode($item[0]["title"])."\n\n".$body;
|
||||
|
||||
$nodename = "status_message";
|
||||
$post["raw_message"] = str_replace("&", "&", $body);
|
||||
$post["guid"] = $item[0]["guid"];
|
||||
$post["diaspora_handle"] = diaspora::handle_from_contact($item[0]["contact-id"]);
|
||||
$post["public"] = (!$item[0]["private"] ? 'true':'false');
|
||||
$post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]);
|
||||
$post["provider_display_name"] = $item[0]["app"];
|
||||
// Fetch some data from the author (We could combine both queries - but I think this is more readable)
|
||||
$r = q("SELECT `user`.`prvkey`, `contact`.`addr`, `user`.`nickname`, `contact`.`nick` FROM `user`
|
||||
INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid`
|
||||
WHERE `user`.`uid` = %d", intval($item[0]["uid"]));
|
||||
if (!$r) {
|
||||
header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
|
||||
killme();
|
||||
}
|
||||
$user = $r[0];
|
||||
|
||||
$dom = new DOMDocument("1.0");
|
||||
$root = $dom->createElement("XML");
|
||||
$dom->appendChild($root);
|
||||
$postelement = $dom->createElement("post");
|
||||
$root->appendChild($postelement);
|
||||
$statuselement = $dom->createElement($nodename);
|
||||
$postelement->appendChild($statuselement);
|
||||
|
||||
foreach($post AS $index => $value) {
|
||||
$postnode = $dom->createElement($index, $value);
|
||||
$statuselement->appendChild($postnode);
|
||||
}
|
||||
$status = diaspora::build_status($item[0], $user);
|
||||
$data = array("XML" => array("post" => array($status["type"] => $status["message"])));
|
||||
$xml = xml::from_array($data, $xmlobj);
|
||||
|
||||
header("Content-Type: application/xml; charset=utf-8");
|
||||
$xml = $dom->saveXML();
|
||||
|
||||
// Diaspora doesn't send the XML header, so we remove them as well.
|
||||
// So we avoid possible compatibility problems.
|
||||
if (substr($xml, 0, 21) == '<?xml version="1.0"?>')
|
||||
$xml = trim(substr($xml, 21));
|
||||
|
||||
echo $xml;
|
||||
|
||||
killme();
|
||||
|
|
Loading…
Reference in a new issue