]> git.mxchange.org Git - friendica.git/blobdiff - include/diaspora.php
Small bugfix
[friendica.git] / include / diaspora.php
index 2ff7a90cfbe677cc4eb92a880a4fb7be39bfb09d..18206be8ed88bae758a1cf83a694bdc125851ff8 100644 (file)
@@ -360,8 +360,10 @@ class diaspora {
 
                $data = parse_xml_string($msg["message"], false);
 
-               if (!is_object($data))
+               if (!is_object($data)) {
+                       logger("No valid XML ".$msg["message"], LOGGER_DEBUG);
                        return false;
+               }
 
                $first_child = $data->getName();
 
@@ -422,11 +424,11 @@ class diaspora {
                                }
                        }
 
-                       if ($fieldname == "author_signature")
+                       if (($fieldname == "author_signature") AND ($entry != ""))
                                $author_signature = base64_decode($entry);
-                       elseif ($fieldname == "parent_author_signature")
+                       elseif (($fieldname == "parent_author_signature") AND ($entry != ""))
                                $parent_author_signature = base64_decode($entry);
-                       elseif ($fieldname != "target_author_signature") {
+                       elseif (!in_array($fieldname, array("author_signature", "parent_author_signature", "target_author_signature"))) {
                                if ($signed_data != "") {
                                        $signed_data .= ";";
                                        $signed_data_parent .= ";";
@@ -451,19 +453,27 @@ class diaspora {
                        return true;
 
                // No author_signature? This is a must, so we quit.
-               if (!isset($author_signature))
+               if (!isset($author_signature)) {
+                       logger("No author signature for type ".$type." - Message: ".$msg["message"], LOGGER_DEBUG);
                        return false;
+               }
 
                if (isset($parent_author_signature)) {
                        $key = self::key($msg["author"]);
 
-                       if (!rsa_verify($signed_data, $parent_author_signature, $key, "sha256"))
+                       if (!rsa_verify($signed_data, $parent_author_signature, $key, "sha256")) {
+                               logger("No valid parent author signature for author ".$msg["author"]. " in type ".$type." - signed data: ".$signed_data." - Message: ".$msg["message"]." - Signature ".$parent_author_signature, LOGGER_DEBUG);
                                return false;
+                       }
                }
 
                $key = self::key($fields->author);
 
-               return rsa_verify($signed_data, $author_signature, $key, "sha256");
+               if (!rsa_verify($signed_data, $author_signature, $key, "sha256")) {
+                       logger("No valid author signature for author ".$msg["author"]. " in type ".$type." - signed data: ".$signed_data." - Message: ".$msg["message"]." - Signature ".$author_signature, LOGGER_DEBUG);
+                       return false;
+               } else
+                       return true;
        }
 
        /**
@@ -981,6 +991,23 @@ class diaspora {
                return true;
        }
 
+       /**
+        * @brief Fetch the uri from our database if we already have this item (maybe from ourselves)
+        *
+        * @param string $author Author handle
+        * @param string $guid Message guid
+        *
+        * @return string The constructed uri or the one from our database
+        */
+       private function get_uri_from_guid($author, $guid) {
+
+               $r = q("SELECT `uri` FROM `item` WHERE `guid` = '%s' LIMIT 1", dbesc($guid));
+               if ($r)
+                       return $r[0]["uri"];
+               else
+                       return $author.":".$guid;
+       }
+
        /**
         * @brief Processes an incoming comment
         *
@@ -1033,7 +1060,7 @@ class diaspora {
                $datarray["owner-avatar"] = ((x($contact,"thumb")) ? $contact["thumb"] : $contact["photo"]);
 
                $datarray["guid"] = $guid;
-               $datarray["uri"] = $author.":".$guid;
+               $datarray["uri"] = self::get_uri_from_guid($author, $guid);
 
                $datarray["type"] = "remote-comment";
                $datarray["verb"] = ACTIVITY_POST;
@@ -1370,7 +1397,7 @@ class diaspora {
                $datarray["owner-avatar"] = ((x($contact,"thumb")) ? $contact["thumb"] : $contact["photo"]);
 
                $datarray["guid"] = $guid;
-               $datarray["uri"] = $author.":".$guid;
+               $datarray["uri"] = self::get_uri_from_guid($author, $guid);
 
                $datarray["type"] = "activity";
                $datarray["verb"] = $verb;
@@ -1792,6 +1819,8 @@ class diaspora {
                if(intval($def_gid))
                        group_add_member($importer["uid"], "", $contact_record["id"], $def_gid);
 
+               update_contact_avatar($ret["photo"], $importer['uid'], $contact_record["id"], true);
+
                if($importer["page-flags"] == PAGE_NORMAL) {
 
                        $hash = random_string().(string)time();   // Generate a confirm_key
@@ -1969,7 +1998,7 @@ class diaspora {
                $datarray["owner-avatar"] = $datarray["author-avatar"];
 
                $datarray["guid"] = $guid;
-               $datarray["uri"] = $datarray["parent-uri"] = $author.":".$guid;
+               $datarray["uri"] = $datarray["parent-uri"] = self::get_uri_from_guid($author, $guid);
 
                $datarray["verb"] = ACTIVITY_POST;
                $datarray["gravity"] = GRAVITY_PARENT;
@@ -2171,7 +2200,7 @@ class diaspora {
                $datarray["owner-avatar"] = $datarray["author-avatar"];
 
                $datarray["guid"] = $guid;
-               $datarray["uri"] = $datarray["parent-uri"] = $author.":".$guid;
+               $datarray["uri"] = $datarray["parent-uri"] = self::get_uri_from_guid($author, $guid);
 
                $datarray["verb"] = ACTIVITY_POST;
                $datarray["gravity"] = GRAVITY_PARENT;
@@ -2227,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
         *
@@ -2258,11 +2321,11 @@ class diaspora {
                $sig = base64url_encode($signature);
 
                $xmldata = array("diaspora" => array("header" => array("author_id" => $handle),
-                                               "me:env" => array("me:encoding" => "base64url",
-                                                               "me:alg" => "RSA-SHA256",
-                                                               "me:data" => $data,
-                                                               "@attributes" => array("type" => "application/xml"),
-                                                               "me:sig" => $sig)));
+                                                       "me:env" => array("me:encoding" => $encoding,
+                                                       "me:alg" => $alg,
+                                                       "me:data" => $data,
+                                                       "@attributes" => array("type" => $type),
+                                                       "me:sig" => $sig)));
 
                $namespaces = array("" => "https://joindiaspora.com/protocol",
                                "me" => "http://salmon-protocol.org/ns/magic-env");
@@ -2348,10 +2411,10 @@ class diaspora {
                $cipher_json = base64_encode($encrypted_header_json_object);
 
                $xmldata = array("diaspora" => array("encrypted_header" => $cipher_json,
-                                               "me:env" => array("me:encoding" => "base64url",
-                                                               "me:alg" => "RSA-SHA256",
+                                               "me:env" => array("me:encoding" => $encoding,
+                                                               "me:alg" => $alg,
                                                                "me:data" => $data,
-                                                               "@attributes" => array("type" => "application/xml"),
+                                                               "@attributes" => array("type" => $type),
                                                                "me:sig" => $sig)));
 
                $namespaces = array("" => "https://joindiaspora.com/protocol",
@@ -2469,6 +2532,20 @@ class diaspora {
        }
 
 
+       /**
+        * @brief Build the post xml
+        *
+        * @param string $type The message type
+        * @param array $message The message data
+        *
+        * @return string The post XML
+        */
+       public static function build_post_xml($type, $message) {
+
+               $data = array("XML" => array("post" => array($type => $message)));
+               return xml::from_array($data, $xml);
+       }
+
        /**
         * @brief Builds and transmit messages
         *
@@ -2484,9 +2561,7 @@ class diaspora {
         */
        private function build_and_transmit($owner, $contact, $type, $message, $public_batch = false, $guid = "", $spool = false) {
 
-               $data = array("XML" => array("post" => array($type => $message)));
-
-               $msg = xml::from_array($data, $xml);
+               $msg = self::build_post_xml($type, $message);
 
                logger('message: '.$msg, LOGGER_DATA);
                logger('send guid '.$guid, LOGGER_DEBUG);
@@ -2618,16 +2693,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);
 
@@ -2690,8 +2765,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"]);
        }
 
        /**