]> git.mxchange.org Git - friendica.git/blobdiff - include/diaspora.php
Fix wrong class name case
[friendica.git] / include / diaspora.php
index 6f189f9c0709c389dc4a9153f93b45917b10c7c1..21081dd6a7250d7e4891984c47889ad633e4b065 100644 (file)
@@ -8,19 +8,20 @@
  * This will change in the future.
  */
 
-use \Friendica\Core\Config;
-
-require_once("include/items.php");
-require_once("include/bb2diaspora.php");
-require_once("include/Scrape.php");
-require_once("include/Contact.php");
-require_once("include/Photo.php");
-require_once("include/socgraph.php");
-require_once("include/group.php");
-require_once("include/xml.php");
-require_once("include/datetime.php");
-require_once("include/queue_fn.php");
-require_once("include/cache.php");
+use Friendica\App;
+use Friendica\Core\Config;
+
+require_once 'include/items.php';
+require_once 'include/bb2diaspora.php';
+require_once 'include/Scrape.php';
+require_once 'include/Contact.php';
+require_once 'include/Photo.php';
+require_once 'include/socgraph.php';
+require_once 'include/group.php';
+require_once 'include/xml.php';
+require_once 'include/datetime.php';
+require_once 'include/queue_fn.php';
+require_once 'include/cache.php';
 
 /**
  * @brief This class contain functions to create and send Diaspora XML files
@@ -45,7 +46,7 @@ class Diaspora {
 
                $servers = explode(",", $serverdata);
 
-               foreach($servers AS $server) {
+               foreach ($servers AS $server) {
                        $server = trim($server);
                        $addr = "relay@".str_replace("http://", "", normalise_link($server));
                        $batch = $server."/receive/public";
@@ -160,6 +161,32 @@ class Diaspora {
                return $data;
        }
 
+       /**
+        * @brief encrypts data via AES
+        *
+        * @param string $key The AES key
+        * @param string $iv The IV (is used for CBC encoding)
+        * @param string $data The data that is to be encrypted
+        *
+        * @return string encrypted data
+        */
+       private static function aes_encrypt($key, $iv, $data) {
+               return openssl_encrypt($data, 'aes-256-cbc', str_pad($key, 32, "\0"), OPENSSL_RAW_DATA, str_pad($iv, 16, "\0"));
+       }
+
+       /**
+        * @brief decrypts data via AES
+        *
+        * @param string $key The AES key
+        * @param string $iv The IV (is used for CBC encoding)
+        * @param string $encrypted The encrypted data
+        *
+        * @return string decrypted data
+        */
+       private static function aes_decrypt($key, $iv, $encrypted) {
+               return openssl_decrypt($encrypted,'aes-256-cbc', str_pad($key, 32, "\0"), OPENSSL_RAW_DATA,str_pad($iv, 16, "\0"));
+       }
+
        /**
         * @brief: Decodes incoming Diaspora message
         *
@@ -199,10 +226,7 @@ class Diaspora {
                        $outer_iv = base64_decode($j_outer_key_bundle->iv);
                        $outer_key = base64_decode($j_outer_key_bundle->key);
 
-                       $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv);
-
-
-                       $decrypted = pkcs5_unpad($decrypted);
+                       $decrypted = self::aes_decrypt($outer_key, $outer_iv, $ciphertext);
 
                        logger('decrypted: '.$decrypted, LOGGER_DEBUG);
                        $idom = parse_xml_string($decrypted,false);
@@ -261,8 +285,7 @@ class Diaspora {
                        // Decode the encrypted blob
 
                        $inner_encrypted = base64_decode($data);
-                       $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
-                       $inner_decrypted = pkcs5_unpad($inner_decrypted);
+                       $inner_decrypted = self::aes_decrypt($inner_aes_key, $inner_iv, $inner_encrypted);
                }
 
                if (!$author_link) {
@@ -325,15 +348,10 @@ class Diaspora {
                                self::dispatch($rr,$msg);
                        }
                } else {
-                       $social_relay = (bool)Config::get('system', 'relay_subscribe', false);
-
                        // Use a dummy importer to import the data for the public copy
-                       if ($social_relay) {
-                               $importer = array("uid" => 0, "page-flags" => PAGE_FREELOVE);
-                               $message_id = self::dispatch($importer,$msg);
-                       } else {
-                               logger("Unwanted message from ".$msg["author"]." send by ".$_SERVER["REMOTE_ADDR"]." with ".$_SERVER["HTTP_USER_AGENT"].": ".print_r($msg, true), LOGGER_DEBUG);
-                       }
+                       // or for comments from unknown people
+                       $importer = array("uid" => 0, "page-flags" => PAGE_FREELOVE);
+                       $message_id = self::dispatch($importer,$msg);
                }
 
                return $message_id;
@@ -360,6 +378,11 @@ class Diaspora {
 
                $type = $fields->getName();
 
+               $social_relay = Config::get('system', 'relay_subscribe', false);
+               if (!$social_relay AND ($type == 'message')) {
+                       logger("Unwanted message from ".$sender." send by ".$_SERVER["REMOTE_ADDR"]." with ".$_SERVER["HTTP_USER_AGENT"].": ".print_r($msg, true), LOGGER_DEBUG);
+               }
+
                logger("Received message type ".$type." from ".$sender." for user ".$importer["uid"], LOGGER_DEBUG);
 
                switch ($type) {
@@ -1205,6 +1228,27 @@ class Diaspora {
                }
        }
 
+       /**
+        * @brief Find the best importer for a comment
+        *
+        * @param array $importer Array of the importer user
+        * @param string $guid The guid of the item
+        *
+        * @return array the importer that fits the best
+        */
+       private static function importer_for_comment($importer, $guid) {
+               $item = dba::fetch_first("SELECT `uid` FROM `item` WHERE `origin` AND `guid` = ? LIMIT 1", $guid);
+
+               if (dbm::is_result($item)) {
+                       logger("Found user ".$item['uid']." as owner of item ".$guid, LOGGER_DEBUG);
+                       $contact = dba::fetch_first("SELECT * FROM `contact` WHERE `self` AND `uid` = ?", $item['uid']);
+                       if (dbm::is_result($contact)) {
+                               $importer = $contact;
+                       }
+               }
+               return $importer;
+       }
+
        /**
         * @brief Processes an incoming comment
         *
@@ -1234,6 +1278,11 @@ class Diaspora {
                        $thr_uri = "";
                }
 
+               // Find the best importer when there was no importer found
+               if ($importer["uid"] == 0) {
+                       $importer = self::importer_for_comment($importer, $parent_guid);
+               }
+
                $contact = self::allowed_contact_by_handle($importer, $sender, true);
                if (!$contact) {
                        return false;
@@ -1286,7 +1335,9 @@ class Diaspora {
                }
 
                $datarray["object-type"] = ACTIVITY_OBJ_COMMENT;
-               $datarray["object"] = $xml;
+
+               $datarray["protocol"] = PROTOCOL_DIASPORA;
+               $datarray["source"] = $xml;
 
                $datarray["changed"] = $datarray["created"] = $datarray["edited"] = $created_at;
 
@@ -1507,7 +1558,7 @@ class Diaspora {
                        return;
                }
 
-               foreach($messages as $mesg)
+               foreach ($messages as $mesg)
                        self::receive_conversation_message($importer, $contact, $data, $msg, $mesg, $conversation);
 
                return true;
@@ -1606,6 +1657,8 @@ class Diaspora {
 
                $datarray = array();
 
+               $datarray["protocol"] = PROTOCOL_DIASPORA;
+
                $datarray["uid"] = $importer["uid"];
                $datarray["contact-id"] = $author_contact["cid"];
                $datarray["network"]  = $author_contact["network"];
@@ -1848,18 +1901,15 @@ class Diaspora {
                        intval($importer["uid"])
                );
 
-               if ($searchable) {
-                       poco_check($contact["url"], $name, NETWORK_DIASPORA, $image_url, $about, $location, $gender, $keywords, "",
-                               datetime_convert(), 2, $contact["id"], $importer["uid"]);
-               }
-
                $gcontact = array("url" => $contact["url"], "network" => NETWORK_DIASPORA, "generation" => 2,
                                        "photo" => $image_url, "name" => $name, "location" => $location,
                                        "about" => $about, "birthday" => $birthday, "gender" => $gender,
                                        "addr" => $author, "nick" => $nick, "keywords" => $keywords,
                                        "hide" => !$searchable, "nsfw" => $nsfw);
 
-               update_gcontact($gcontact);
+               $gcid = update_gcontact($gcontact);
+
+               link_gcontact($gcid, $importer["uid"], $contact["id"]);
 
                logger("Profile of contact ".$contact["id"]." stored for user ".$importer["uid"], LOGGER_DEBUG);
 
@@ -1900,6 +1950,7 @@ class Diaspora {
                        if ($self && $contact["rel"] == CONTACT_IS_FOLLOWER) {
 
                                $arr = array();
+                               $arr["protocol"] = PROTOCOL_DIASPORA;
                                $arr["uri"] = $arr["parent-uri"] = item_new_uri($a->get_hostname(), $importer["uid"]);
                                $arr["uid"] = $importer["uid"];
                                $arr["contact-id"] = $self[0]["id"];
@@ -2259,7 +2310,8 @@ class Diaspora {
                $datarray["verb"] = ACTIVITY_POST;
                $datarray["gravity"] = GRAVITY_PARENT;
 
-               $datarray["object"] = $xml;
+               $datarray["protocol"] = PROTOCOL_DIASPORA;
+               $datarray["source"] = $xml;
 
                $prefix = share_header($original_item["author-name"], $original_item["author-link"], $original_item["author-avatar"],
                                        $original_item["guid"], $original_item["created"], $orig_url);
@@ -2462,7 +2514,8 @@ class Diaspora {
                $datarray["verb"] = ACTIVITY_POST;
                $datarray["gravity"] = GRAVITY_PARENT;
 
-               $datarray["object"] = $xml;
+               $datarray["protocol"] = PROTOCOL_DIASPORA;
+               $datarray["source"] = $xml;
 
                $datarray["body"] = self::replace_people_guid($body, $contact["url"]);
 
@@ -2621,20 +2674,19 @@ class Diaspora {
                        return false;
                }
 
-               $inner_aes_key = random_string(32);
+               $inner_aes_key = openssl_random_pseudo_bytes(32);
                $b_inner_aes_key = base64_encode($inner_aes_key);
-               $inner_iv = random_string(16);
+               $inner_iv = openssl_random_pseudo_bytes(16);
                $b_inner_iv = base64_encode($inner_iv);
 
-               $outer_aes_key = random_string(32);
+               $outer_aes_key = openssl_random_pseudo_bytes(32);
                $b_outer_aes_key = base64_encode($outer_aes_key);
-               $outer_iv = random_string(16);
+               $outer_iv = openssl_random_pseudo_bytes(16);
                $b_outer_iv = base64_encode($outer_iv);
 
                $handle = self::my_handle($user);
 
-               $padded_data = pkcs5_pad($msg,16);
-               $inner_encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $padded_data, MCRYPT_MODE_CBC, $inner_iv);
+               $inner_encrypted = self::aes_encrypt($inner_aes_key, $inner_iv, $msg);
 
                $b64_data = base64_encode($inner_encrypted);
 
@@ -2656,9 +2708,8 @@ class Diaspora {
                                                        "author_id" => $handle));
 
                $decrypted_header = xml::from_array($xmldata, $xml, true);
-               $decrypted_header = pkcs5_pad($decrypted_header,16);
 
-               $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $outer_aes_key, $decrypted_header, MCRYPT_MODE_CBC, $outer_iv);
+               $ciphertext = self::aes_encrypt($outer_aes_key, $outer_iv, $decrypted_header);
 
                $outer_json = json_encode(array("iv" => $b_outer_iv, "key" => $b_outer_aes_key));
 
@@ -3094,7 +3145,7 @@ class Diaspora {
                                $cnt = preg_match_all('/href=\"(.*?)\"(.*?)title=\"(.*?)\"/ism', $item["attach"], $matches, PREG_SET_ORDER);
                                if (cnt) {
                                        $body .= "\n".t("Attachments:")."\n";
-                                       foreach($matches as $mtch)
+                                       foreach ($matches as $mtch)
                                                $body .= "[".$mtch[3]."](".$mtch[1].")\n";
                                }
                        }
@@ -3574,7 +3625,7 @@ class Diaspora {
                if ($searchable === 'true') {
                        $dob = '1000-00-00';
 
-                       if (($profile['dob']) && ($profile['dob'] != '0000-00-00'))
+                       if (($profile['dob']) && ($profile['dob'] > '0001-01-01'))
                                $dob = ((intval($profile['dob'])) ? intval($profile['dob']) : '1000') .'-'. datetime_convert('UTC','UTC',$profile['dob'],'m-d');
 
                        $about = $profile['about'];
@@ -3587,7 +3638,7 @@ class Diaspora {
                                $kw = str_replace('  ',' ',$kw);
                                $arr = explode(' ',$profile['pub_keywords']);
                                if (count($arr)) {
-                                       for($x = 0; $x < 5; $x ++) {
+                                       for ($x = 0; $x < 5; $x ++) {
                                                if (trim($arr[$x]))
                                                        $tags .= '#'. trim($arr[$x]) .' ';
                                        }
@@ -3609,7 +3660,7 @@ class Diaspora {
                                "searchable" => $searchable,
                                "tag_string" => $tags);
 
-               foreach($recips as $recip) {
+               foreach ($recips as $recip) {
                        logger("Send updated profile data for user ".$uid." to contact ".$recip["id"], LOGGER_DEBUG);
                        self::build_and_transmit($profile, $recip, "profile", $message, false, "", true);
                }