]> git.mxchange.org Git - friendica.git/blobdiff - include/dfrn.php
Merge pull request #3281 from annando/issue-3206-2957
[friendica.git] / include / dfrn.php
index fbf6aea5615774b3f7cccac17fdd442f19598095..4002bb4268de07a2dca041789dfac033ff557666 100644 (file)
@@ -864,6 +864,30 @@ class dfrn {
                return $entry;
        }
 
+       /**
+        * @brief encrypts data via AES
+        *
+        * @param string $data The data that is to be encrypted
+        * @param string $key The AES key
+        *
+        * @return string encrypted data
+        */
+       private static function aes_encrypt($data, $key) {
+               return openssl_encrypt($data, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
+       }
+
+       /**
+        * @brief decrypts data via AES
+        *
+        * @param string $encrypted The encrypted data
+        * @param string $key The AES key
+        *
+        * @return string decrypted data
+        */
+       public static function aes_decrypt($encrypted, $key) {
+               return openssl_decrypt($encrypted, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
+       }
+
        /**
         * @brief Delivers the atom content to the contacts
         *
@@ -888,8 +912,6 @@ class dfrn {
 
                $rino = get_config('system','rino_encrypt');
                $rino = intval($rino);
-               // use RINO1 if mcrypt isn't installed and RINO2 was selected
-               if ($rino==2 and !function_exists('mcrypt_create_iv')) $rino=1;
 
                logger("Local rino version: ". $rino, LOGGER_DEBUG);
 
@@ -916,14 +938,15 @@ class dfrn {
                $ret = z_fetch_url($url);
 
                if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
-                       return(-1); // timed out
+                       return -2; // timed out
                }
 
                $xml = $ret['body'];
 
                $curl_stat = $a->get_curl_code();
-               if(! $curl_stat)
-                       return(-1); // timed out
+               if (!$curl_stat) {
+                       return -3; // timed out
+               }
 
                logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
 
@@ -1014,8 +1037,8 @@ class dfrn {
                        switch($rino_remote_version) {
                                case 1:
                                        // Deprecated rino version!
-                                       $key = substr(random_string(),0,16);
-                                       $data = aes_encrypt($postvars['data'],$key);
+                                       $key = openssl_random_pseudo_bytes(16);
+                                       $data = self::aes_encrypt($postvars['data'], $key);
                                        break;
                                case 2:
                                        // RINO 2 based on php-encryption
@@ -1023,24 +1046,24 @@ class dfrn {
                                                $key = Crypto::createNewRandomKey();
                                        } catch (CryptoTestFailed $ex) {
                                                logger('Cannot safely create a key');
-                                               return -1;
+                                               return -4;
                                        } catch (CannotPerformOperation $ex) {
                                                logger('Cannot safely create a key');
-                                               return -1;
+                                               return -5;
                                        }
                                        try {
                                                $data = Crypto::encrypt($postvars['data'], $key);
                                        } catch (CryptoTestFailed $ex) {
                                                logger('Cannot safely perform encryption');
-                                               return -1;
+                                               return -6;
                                        } catch (CannotPerformOperation $ex) {
                                                logger('Cannot safely perform encryption');
-                                               return -1;
+                                               return -7;
                                        }
                                        break;
                                default:
                                        logger("rino: invalid requested verision '$rino_remote_version'");
-                                       return -1;
+                                       return -8;
                        }
 
                        $postvars['rino'] = $rino_remote_version;
@@ -1074,16 +1097,18 @@ class dfrn {
 
                logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
 
-               $xml = post_url($contact['notify'],$postvars);
+               $xml = post_url($contact['notify'], $postvars);
 
                logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
 
                $curl_stat = $a->get_curl_code();
-               if((! $curl_stat) || (! strlen($xml)))
-                       return(-1); // timed out
+               if ((!$curl_stat) || (!strlen($xml))) {
+                       return -9; // timed out
+               }
 
-               if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))
-                       return(-1);
+               if (($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after'))) {
+                       return -10;
+               }
 
                if(strpos($xml,'<?xml') === false) {
                        logger('dfrn_deliver: phase 2: no valid XML returned');
@@ -1091,7 +1116,7 @@ class dfrn {
                        return 3;
                }
 
-               if($contact['term-date'] != '0000-00-00 00:00:00') {
+               if ($contact['term-date'] > NULL_DATE) {
                        logger("dfrn_deliver: $url back from the dead - removing mark for death");
                        require_once('include/Contact.php');
                        unmark_for_death($contact);
@@ -1349,7 +1374,9 @@ class dfrn {
                        $poco["photo"] = $author["avatar"];
                        $poco["hide"] = $hide;
                        $poco["contact-type"] = $contact["contact-type"];
-                       update_gcontact($poco);
+                       $gcid = update_gcontact($poco);
+
+                       link_gcontact($gcid, $importer["uid"], $contact["id"]);
                }
 
                return($author);