]> 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 fef9ae2fedf07611858821eb6afb8ff2e2afd444..4002bb4268de07a2dca041789dfac033ff557666 100644 (file)
@@ -194,7 +194,7 @@ class dfrn {
                        `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
                        `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
                        `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
-                       FROM `item` USE INDEX (`uid_wall_changed`, `uid_type_changed`) $sql_post_table
+                       FROM `item` USE INDEX (`uid_wall_changed`) $sql_post_table
                        STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
                        AND (NOT `contact`.`blocked` OR `contact`.`pending`)
                        LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
@@ -391,7 +391,7 @@ class dfrn {
         *
         * @return object XML root object
         */
-       private function add_header($doc, $owner, $authorelement, $alternatelink = "", $public = false) {
+       private static function add_header($doc, $owner, $authorelement, $alternatelink = "", $public = false) {
 
                if ($alternatelink == "")
                        $alternatelink = $owner['url'];
@@ -462,7 +462,7 @@ class dfrn {
         *
         * @return object XML author object
         */
-       private function add_author($doc, $owner, $authorelement, $public) {
+       private static function add_author($doc, $owner, $authorelement, $public) {
 
                // Is the profile hidden or shouldn't be published in the net? Then add the "hide" element
                $r = q("SELECT `id` FROM `profile` INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
@@ -591,7 +591,7 @@ class dfrn {
         *
         * @return object XML author object
         */
-       private function add_entry_author($doc, $element, $contact_url, $item) {
+       private static function add_entry_author($doc, $element, $contact_url, $item) {
 
                $contact = get_contact_details_by_url($contact_url, $item["uid"]);
 
@@ -631,7 +631,7 @@ class dfrn {
         *
         * @return object XML activity object
         */
-       private function create_activity($doc, $element, $activity) {
+       private static function create_activity($doc, $element, $activity) {
 
                if($activity) {
                        $entry = $doc->createElement($element);
@@ -685,7 +685,7 @@ class dfrn {
         *
         * @return object XML attachment object
         */
-       private function get_attachment($doc, $root, $item) {
+       private static function get_attachment($doc, $root, $item) {
                $arr = explode('[/attach],',$item['attach']);
                if(count($arr)) {
                        foreach($arr as $r) {
@@ -720,7 +720,7 @@ class dfrn {
         *
         * @return object XML entry object
         */
-       private function entry($doc, $type, $item, $owner, $comment = false, $cid = 0) {
+       private static function entry($doc, $type, $item, $owner, $comment = false, $cid = 0) {
 
                $mentioned = array();
 
@@ -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);
 
@@ -913,11 +935,18 @@ class dfrn {
 
                logger('dfrn_deliver: ' . $url);
 
-               $xml = fetch_url($url);
+               $ret = z_fetch_url($url);
+
+               if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
+                       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);
 
@@ -1008,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
@@ -1017,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;
@@ -1068,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');
@@ -1085,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);
@@ -1103,14 +1134,24 @@ class dfrn {
         * @param string $birthday Birthday of the contact
         *
         */
-       private function birthday_event($contact, $birthday) {
+       private static function birthday_event($contact, $birthday) {
+
+               // Check for duplicates
+               $r = q("SELECT `id` FROM `event` WHERE `uid` = %d AND `cid` = %d AND `start` = '%s' AND `type` = '%s' LIMIT 1",
+                       intval($contact["uid"]),
+                       intval($contact["id"]),
+                       dbesc(datetime_convert("UTC","UTC", $birthday)),
+                       dbesc("birthday"));
+
+               if (dbm::is_result($r)) {
+                       return;
+               }
 
                logger("updating birthday: ".$birthday." for contact ".$contact["id"]);
 
                $bdtext = sprintf(t("%s\'s birthday"), $contact["name"]);
                $bdtext2 = sprintf(t("Happy Birthday %s"), " [url=".$contact["url"]."]".$contact["name"]."[/url]") ;
 
-
                $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`)
                        VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s') ",
                        intval($contact["uid"]),
@@ -1136,7 +1177,7 @@ class dfrn {
         *
         * @return Returns an array with relevant data of the author
         */
-       private function fetchauthor($xpath, $context, $importer, $element, $onlyfetch, $xml = "") {
+       private static function fetchauthor($xpath, $context, $importer, $element, $onlyfetch, $xml = "") {
 
                $author = array();
                $author["name"] = $xpath->evaluate($element."/atom:name/text()", $context)->item(0)->nodeValue;
@@ -1333,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);
@@ -1348,7 +1391,7 @@ class dfrn {
         *
         * @return string XML string
         */
-       private function transform_activity($xpath, $activity, $element) {
+       private static function transform_activity($xpath, $activity, $element) {
                if (!is_object($activity))
                        return "";
 
@@ -1393,7 +1436,7 @@ class dfrn {
         * @param object $mail mail elements
         * @param array $importer Record of the importer user mixed with contact of the content
         */
-       private function process_mail($xpath, $mail, $importer) {
+       private static function process_mail($xpath, $mail, $importer) {
 
                logger("Processing mails");
 
@@ -1411,9 +1454,9 @@ class dfrn {
                $msg["seen"] = 0;
                $msg["replied"] = 0;
 
-               dbesc_array($msg);
+               dbm::esc_array($msg, true);
 
-               $r = dbq("INSERT INTO `mail` (`".implode("`, `", array_keys($msg))."`) VALUES ('".implode("', '", array_values($msg))."')");
+               $r = dbq("INSERT INTO `mail` (`".implode("`, `", array_keys($msg))."`) VALUES (".implode(", ", array_values($msg)).")");
 
                // send notifications.
 
@@ -1444,7 +1487,7 @@ class dfrn {
         * @param object $suggestion suggestion elements
         * @param array $importer Record of the importer user mixed with contact of the content
         */
-       private function process_suggestion($xpath, $suggestion, $importer) {
+       private static function process_suggestion($xpath, $suggestion, $importer) {
                $a = get_app();
 
                logger("Processing suggestions");
@@ -1546,7 +1589,7 @@ class dfrn {
         * @param object $relocation relocation elements
         * @param array $importer Record of the importer user mixed with contact of the content
         */
-       private function process_relocation($xpath, $relocation, $importer) {
+       private static function process_relocation($xpath, $relocation, $importer) {
 
                logger("Processing relocations");
 
@@ -1675,7 +1718,7 @@ class dfrn {
         * @param array $importer Record of the importer user mixed with contact of the content
         * @param int $entrytype Is it a toplevel entry, a comment or a relayed comment?
         */
-       private function update_content($current, $item, $importer, $entrytype) {
+       private static function update_content($current, $item, $importer, $entrytype) {
                $changed = false;
 
                if (edited_timestamp_is_newer($current, $item)) {
@@ -1727,7 +1770,7 @@ class dfrn {
         *
         * @return int Is it a toplevel entry, a comment or a relayed comment?
         */
-       private function get_entry_type($importer, $item) {
+       private static function get_entry_type($importer, $item) {
                if ($item["parent-uri"] != $item["uri"]) {
                        $community = false;
 
@@ -1793,7 +1836,7 @@ class dfrn {
         * @param array $importer Record of the importer user mixed with contact of the content
         * @param int $posted_id The record number of item record that was just posted
         */
-       private function do_poke($item, $importer, $posted_id) {
+       private static function do_poke($item, $importer, $posted_id) {
                $verb = urldecode(substr($item["verb"],strpos($item["verb"], "#")+1));
                if(!$verb)
                        return;
@@ -1848,7 +1891,7 @@ class dfrn {
         *
         * @return bool Should the processing of the entries be continued?
         */
-       private function process_verbs($entrytype, $importer, &$item, &$is_like) {
+       private static function process_verbs($entrytype, $importer, &$item, &$is_like) {
 
                logger("Process verb ".$item["verb"]." and object-type ".$item["object-type"]." for entrytype ".$entrytype, LOGGER_DEBUG);
 
@@ -1948,7 +1991,7 @@ class dfrn {
         * @param object $links link elements
         * @param array $item the item record
         */
-       private function parse_links($links, &$item) {
+       private static function parse_links($links, &$item) {
                $rel = "";
                $href = "";
                $type = "";
@@ -1991,7 +2034,7 @@ class dfrn {
         * @param object $entry entry elements
         * @param array $importer Record of the importer user mixed with contact of the content
         */
-       private function process_entry($header, $xpath, $entry, $importer) {
+       private static function process_entry($header, $xpath, $entry, $importer) {
 
                logger("Processing entries");
 
@@ -2000,6 +2043,20 @@ class dfrn {
                // Get the uri
                $item["uri"] = $xpath->query("atom:id/text()", $entry)->item(0)->nodeValue;
 
+               $item["edited"] = $xpath->query("atom:updated/text()", $entry)->item(0)->nodeValue;
+
+               $current = q("SELECT `id`, `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                       dbesc($item["uri"]),
+                       intval($importer["importer_uid"])
+               );
+
+               // Is there an existing item?
+               if (dbm::is_result($current) AND edited_timestamp_is_newer($current[0], $item) AND
+                       (datetime_convert("UTC","UTC",$item["edited"]) < $current[0]["edited"])) {
+                       logger("Item ".$item["uri"]." already existed.", LOGGER_DEBUG);
+                       return;
+               }
+
                // Fetch the owner
                $owner = self::fetchauthor($xpath, $entry, $importer, "dfrn:owner", true);
 
@@ -2017,7 +2074,6 @@ class dfrn {
                $item["title"] = $xpath->query("atom:title/text()", $entry)->item(0)->nodeValue;
 
                $item["created"] = $xpath->query("atom:published/text()", $entry)->item(0)->nodeValue;
-               $item["edited"] = $xpath->query("atom:updated/text()", $entry)->item(0)->nodeValue;
 
                $item["body"] = $xpath->query("dfrn:env/text()", $entry)->item(0)->nodeValue;
                $item["body"] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$item["body"]);
@@ -2205,18 +2261,13 @@ class dfrn {
                        }
                }
 
-               $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
-                       dbesc($item["uri"]),
-                       intval($importer["importer_uid"])
-               );
-
                if (!self::process_verbs($entrytype, $importer, $item, $is_like)) {
                        logger("Exiting because 'process_verbs' told us so", LOGGER_DEBUG);
                        return;
                }
 
                // Update content if 'updated' changes
-               if (dbm::is_result($r)) {
+               if (dbm::is_result($current)) {
                        if (self::update_content($r[0], $item, $importer, $entrytype))
                                logger("Item ".$item["uri"]." was updated.", LOGGER_DEBUG);
                        else
@@ -2301,7 +2352,7 @@ class dfrn {
         * @param object $deletion deletion elements
         * @param array $importer Record of the importer user mixed with contact of the content
         */
-       private function process_deletion($xpath, $deletion, $importer) {
+       private static function process_deletion($xpath, $deletion, $importer) {
 
                logger("Processing deletions");
 
@@ -2355,7 +2406,7 @@ class dfrn {
                                                dbesc($xt->id),
                                                intval($importer["importer_uid"])
                                        );
-                                       if(count($i)) {
+                                       if (dbm::is_result($i)) {
 
                                                // For tags, the owner cannot remove the tag on the author's copy of the post.