+ /**
+ * @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);
+ }
+