]> 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 3017357516681c4b1a9a14af210bfef02da2261f..4002bb4268de07a2dca041789dfac033ff557666 100644 (file)
@@ -637,24 +637,18 @@ class dfrn {
                        $entry = $doc->createElement($element);
 
                        $r = parse_xml_string($activity, false);
-                       if (!$r) {
+                       if(!$r)
                                return false;
-                       }
-                       if ($r->type) {
+                       if($r->type)
                                xml::add_element($doc, $entry, "activity:object-type", $r->type);
-                       }
-                       if ($r->id) {
+                       if($r->id)
                                xml::add_element($doc, $entry, "id", $r->id);
-                       }
-                       if ($r->title) {
+                       if($r->title)
                                xml::add_element($doc, $entry, "title", $r->title);
-                       }
-
-                       if ($r->link) {
-                               if (substr($r->link,0,1) == '<') {
-                                       if (strstr($r->link,'&') && (! strstr($r->link,'&amp;'))) {
+                       if($r->link) {
+                               if(substr($r->link,0,1) == '<') {
+                                       if(strstr($r->link,'&') && (! strstr($r->link,'&amp;')))
                                                $r->link = str_replace('&','&amp;', $r->link);
-                                       }
 
                                        $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
 
@@ -663,9 +657,8 @@ class dfrn {
                                        if (is_object($data)) {
                                                foreach ($data->link AS $link) {
                                                        $attributes = array();
-                                                       foreach ($link->attributes() AS $parameter => $value) {
+                                                       foreach ($link->attributes() AS $parameter => $value)
                                                                $attributes[$parameter] = $value;
-                                                       }
                                                        xml::add_element($doc, $entry, "link", "", $attributes);
                                                }
                                        }
@@ -674,9 +667,8 @@ class dfrn {
                                        xml::add_element($doc, $entry, "link", "", $attributes);
                                }
                        }
-                       if ($r->content) {
+                       if($r->content)
                                xml::add_element($doc, $entry, "content", bbcode($r->content), array("type" => "html"));
-                       }
 
                        return $entry;
                }
@@ -695,22 +687,20 @@ class dfrn {
         */
        private static function get_attachment($doc, $root, $item) {
                $arr = explode('[/attach],',$item['attach']);
-               if (count($arr)) {
-                       foreach ($arr as $r) {
+               if(count($arr)) {
+                       foreach($arr as $r) {
                                $matches = false;
                                $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|',$r,$matches);
-                               if ($cnt) {
+                               if($cnt) {
                                        $attributes = array("rel" => "enclosure",
                                                        "href" => $matches[1],
                                                        "type" => $matches[3]);
 
-                                       if (intval($matches[2])) {
+                                       if(intval($matches[2]))
                                                $attributes["length"] = intval($matches[2]);
-                                       }
 
-                                       if (trim($matches[4]) != "") {
+                                       if(trim($matches[4]) != "")
                                                $attributes["title"] = trim($matches[4]);
-                                       }
 
                                        xml::add_element($doc, $root, "link", "", $attributes);
                                }
@@ -734,22 +724,20 @@ class dfrn {
 
                $mentioned = array();
 
-               if (!$item['parent']) {
+               if(!$item['parent'])
                        return;
-               }
 
-               if ($item['deleted']) {
+               if($item['deleted']) {
                        $attributes = array("ref" => $item['uri'], "when" => datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME));
                        return xml::create_element($doc, "at:deleted-entry", "", $attributes);
                }
 
                $entry = $doc->createElement("entry");
 
-               if ($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid']) {
+               if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
                        $body = fix_private_photos($item['body'],$owner['uid'],$item,$cid);
-               } else {
+               else
                        $body = $item['body'];
-               }
 
                // Remove the abstract element. It is only locally important.
                $body = remove_abstract($body);
@@ -757,9 +745,8 @@ class dfrn {
                if ($type == 'html') {
                        $htmlbody = $body;
 
-                       if ($item['title'] != "") {
+                       if ($item['title'] != "")
                                $htmlbody = "[b]".$item['title']."[/b]\n\n".$htmlbody;
-                       }
 
                        $htmlbody = bbcode($htmlbody, false, false, 7);
                }
@@ -770,7 +757,7 @@ class dfrn {
                $dfrnowner = self::add_entry_author($doc, "dfrn:owner", $item["owner-link"], $item);
                $entry->appendChild($dfrnowner);
 
-               if (($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
+               if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
                        $parent = q("SELECT `guid` FROM `item` WHERE `id` = %d", intval($item["parent"]));
                        $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
                        $attributes = array("ref" => $parent_item, "type" => "text/html",
@@ -798,33 +785,26 @@ class dfrn {
 
                // "comment-allow" is some old fashioned stuff for old Friendica versions.
                // It is included in the rewritten code for completeness
-               if ($comment) {
+               if ($comment)
                        xml::add_element($doc, $entry, "dfrn:comment-allow", intval($item['last-child']));
-               }
 
-               if ($item['location']) {
+               if($item['location'])
                        xml::add_element($doc, $entry, "dfrn:location", $item['location']);
-               }
 
-               if ($item['coord']) {
+               if($item['coord'])
                        xml::add_element($doc, $entry, "georss:point", $item['coord']);
-               }
 
-               if (($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) {
+               if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
                        xml::add_element($doc, $entry, "dfrn:private", (($item['private']) ? $item['private'] : 1));
-               }
 
-               if ($item['extid']) {
+               if($item['extid'])
                        xml::add_element($doc, $entry, "dfrn:extid", $item['extid']);
-               }
 
-               if ($item['bookmark']) {
+               if($item['bookmark'])
                        xml::add_element($doc, $entry, "dfrn:bookmark", "true");
-               }
 
-               if ($item['app']) {
+               if($item['app'])
                        xml::add_element($doc, $entry, "statusnet:notice_info", "", array("local_id" => $item['id'], "source" => $item['app']));
-               }
 
                xml::add_element($doc, $entry, "dfrn:diaspora_guid", $item["guid"]);
 
@@ -837,56 +817,46 @@ class dfrn {
 
                xml::add_element($doc, $entry, "activity:verb", construct_verb($item));
 
-               if ($item['object-type'] != "") {
+               if ($item['object-type'] != "")
                        xml::add_element($doc, $entry, "activity:object-type", $item['object-type']);
-               } elseif ($item['id'] == $item['parent']) {
+               elseif ($item['id'] == $item['parent'])
                        xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
-               } else {
+               else
                        xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_COMMENT);
-               }
 
                $actobj = self::create_activity($doc, "activity:object", $item['object']);
-               if ($actobj) {
+               if ($actobj)
                        $entry->appendChild($actobj);
-               }
 
                $actarg = self::create_activity($doc, "activity:target", $item['target']);
-               if ($actarg) {
+               if ($actarg)
                        $entry->appendChild($actarg);
-               }
 
                $tags = item_getfeedtags($item);
 
-               if( count($tags)) {
-                       foreach ($tags as $t) {
-                               if (($type != 'html') OR ($t[0] != "@")) {
+               if(count($tags)) {
+                       foreach($tags as $t)
+                               if (($type != 'html') OR ($t[0] != "@"))
                                        xml::add_element($doc, $entry, "category", "", array("scheme" => "X-DFRN:".$t[0].":".$t[1], "term" => $t[2]));
-                               }
-                       }
                }
 
-               if (count($tags)) {
-                       foreach($tags as $t) {
-                               if ($t[0] == "@") {
+               if(count($tags))
+                       foreach($tags as $t)
+                               if ($t[0] == "@")
                                        $mentioned[$t[1]] = $t[1];
-                               }
-                       }
-               }
 
                foreach ($mentioned AS $mention) {
                        $r = q("SELECT `forum`, `prv` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
                                intval($owner["uid"]),
                                dbesc(normalise_link($mention)));
-
-                       if ($r[0]["forum"] OR $r[0]["prv"]) {
+                       if ($r[0]["forum"] OR $r[0]["prv"])
                                xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
                                                                                        "ostatus:object-type" => ACTIVITY_OBJ_GROUP,
                                                                                        "href" => $mention));
-                       } else {
+                       else
                                xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
                                                                                        "ostatus:object-type" => ACTIVITY_OBJ_PERSON,
                                                                                        "href" => $mention));
-                       }
                }
 
                self::get_attachment($doc, $entry, $item);
@@ -894,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
         *
@@ -910,21 +904,15 @@ class dfrn {
 
                $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
 
-               if ($contact['duplex'] && $contact['dfrn-id']) {
+               if($contact['duplex'] && $contact['dfrn-id'])
                        $idtosend = '0:' . $orig_id;
-               }
-               if ($contact['duplex'] && $contact['issued-id']) {
+               if($contact['duplex'] && $contact['issued-id'])
                        $idtosend = '1:' . $orig_id;
-               }
+
 
                $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);
 
                $ssl_val = intval(get_config('system','ssl_policy'));
@@ -962,11 +950,10 @@ class dfrn {
 
                logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
 
-               if (! $xml) {
+               if(! $xml)
                        return 3;
-               }
 
-               if (strpos($xml,'<?xml') === false) {
+               if(strpos($xml,'<?xml') === false) {
                        logger('dfrn_deliver: no valid XML returned');
                        logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
                        return 3;
@@ -974,9 +961,8 @@ class dfrn {
 
                $res = parse_xml_string($xml);
 
-               if ((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id))) {
+               if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
                        return (($res->status) ? $res->status : 3);
-               }
 
                $postvars     = array();
                $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
@@ -988,14 +974,13 @@ class dfrn {
 
                logger("Remote rino version: ".$rino_remote_version." for ".$contact["url"], LOGGER_DEBUG);
 
-               if ($owner['page-flags'] == PAGE_PRVGROUP) {
+               if($owner['page-flags'] == PAGE_PRVGROUP)
                        $page = 2;
-               }
 
                $final_dfrn_id = '';
 
-               if ($perm) {
-                       if ((($perm == 'rw') && (! intval($contact['writable'])))
+               if($perm) {
+                       if((($perm == 'rw') && (! intval($contact['writable'])))
                                || (($perm == 'r') && (intval($contact['writable'])))) {
                                q("update contact set writable = %d where id = %d",
                                        intval(($perm == 'rw') ? 1 : 0),
@@ -1005,7 +990,7 @@ class dfrn {
                        }
                }
 
-               if (($contact['duplex'] && strlen($contact['pubkey']))
+               if(($contact['duplex'] && strlen($contact['pubkey']))
                        || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
                        || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
                        openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
@@ -1017,11 +1002,10 @@ class dfrn {
 
                $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
 
-               if (strpos($final_dfrn_id,':') == 1) {
+               if(strpos($final_dfrn_id,':') == 1)
                        $final_dfrn_id = substr($final_dfrn_id,2);
-               }
 
-               if ($final_dfrn_id != $orig_id) {
+               if($final_dfrn_id != $orig_id) {
                        logger('dfrn_deliver: wrong dfrn_id.');
                        // did not decode properly - cannot trust this site
                        return 3;
@@ -1029,12 +1013,11 @@ class dfrn {
 
                $postvars['dfrn_id']      = $idtosend;
                $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
-               if ($dissolve) {
+               if($dissolve)
                        $postvars['dissolve'] = '1';
-               }
 
 
-               if ((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
+               if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
                        $postvars['data'] = $atom;
                        $postvars['perm'] = 'rw';
                } else {
@@ -1044,19 +1027,18 @@ class dfrn {
 
                $postvars['ssl_policy'] = $ssl_policy;
 
-               if ($page) {
+               if($page)
                        $postvars['page'] = $page;
-               }
 
 
-               if ($rino>0 && $rino_remote_version>0 && (! $dissolve)) {
+               if($rino>0 && $rino_remote_version>0 && (! $dissolve)) {
                        logger('rino version: '. $rino_remote_version);
 
                        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
@@ -1087,25 +1069,23 @@ class dfrn {
                        $postvars['rino'] = $rino_remote_version;
                        $postvars['data'] = bin2hex($data);
 
-                       //logger('rino: sent key = ' . $key, LOGGER_DEBUG);
+                       #logger('rino: sent key = ' . $key, LOGGER_DEBUG);
 
 
-                       if ($dfrn_version >= 2.1) {
-                               if (($contact['duplex'] && strlen($contact['pubkey']))
+                       if($dfrn_version >= 2.1) {
+                               if(($contact['duplex'] && strlen($contact['pubkey']))
                                        || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
-                                       || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
+                                       || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey'])))
 
                                        openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
-                               } else {
+                               else
                                        openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
-                               }
 
                        } else {
-                               if (($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
+                               if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY))
                                        openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
-                               } else {
+                               else
                                        openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
-                               }
 
                        }
 
@@ -1130,13 +1110,13 @@ class dfrn {
                        return -10;
                }
 
-               if (strpos($xml,'<?xml') === false) {
+               if(strpos($xml,'<?xml') === false) {
                        logger('dfrn_deliver: phase 2: no valid XML returned');
                        logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
                        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);
@@ -1207,8 +1187,7 @@ class dfrn {
                                `name`, `nick`, `about`, `location`, `keywords`, `xmpp`, `bdyear`, `bd`, `hidden`, `contact-type`
                                FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
                        intval($importer["uid"]), dbesc(normalise_link($author["link"])), dbesc(NETWORK_STATUSNET));
-
-               if (dbm::is_result($r)) {
+               if ($r) {
                        $contact = $r[0];
                        $author["contact-id"] = $r[0]["id"];
                        $author["network"] = $r[0]["network"];
@@ -1229,19 +1208,15 @@ class dfrn {
                        $href = "";
                        $width = 0;
                        foreach($avatar->attributes AS $attributes) {
-                               if ($attributes->name == "href") {
+                               if ($attributes->name == "href")
                                        $href = $attributes->textContent;
-                               }
-                               if ($attributes->name == "width") {
+                               if ($attributes->name == "width")
                                        $width = $attributes->textContent;
-                               }
-                               if ($attributes->name == "updated") {
+                               if ($attributes->name == "updated")
                                        $contact["avatar-date"] = $attributes->textContent;
-                               }
                        }
-                       if (($width > 0) AND ($href != "")) {
+                       if (($width > 0) AND ($href != ""))
                                $avatarlist[$width] = $href;
-                       }
                }
                if (count($avatarlist) > 0) {
                        krsort($avatarlist);
@@ -1255,50 +1230,40 @@ class dfrn {
 
                        // When was the last change to name or uri?
                        $name_element = $xpath->query($element."/atom:name", $context)->item(0);
-                       foreach ($name_element->attributes AS $attributes) {
-                               if ($attributes->name == "updated") {
+                       foreach($name_element->attributes AS $attributes)
+                               if ($attributes->name == "updated")
                                        $poco["name-date"] = $attributes->textContent;
-                               }
-                       }
 
                        $link_element = $xpath->query($element."/atom:link", $context)->item(0);
-                       foreach ($link_element->attributes AS $attributes) {
-                               if ($attributes->name == "updated") {
+                       foreach($link_element->attributes AS $attributes)
+                               if ($attributes->name == "updated")
                                        $poco["uri-date"] = $attributes->textContent;
-                               }
-                       }
 
                        // Update contact data
                        $value = $xpath->evaluate($element."/dfrn:handle/text()", $context)->item(0)->nodeValue;
-                       if ($value != "") {
+                       if ($value != "")
                                $poco["addr"] = $value;
-                       }
 
                        $value = $xpath->evaluate($element."/poco:displayName/text()", $context)->item(0)->nodeValue;
-                       if ($value != "") {
+                       if ($value != "")
                                $poco["name"] = $value;
-                       }
 
                        $value = $xpath->evaluate($element."/poco:preferredUsername/text()", $context)->item(0)->nodeValue;
-                       if ($value != "") {
+                       if ($value != "")
                                $poco["nick"] = $value;
-                       }
 
                        $value = $xpath->evaluate($element."/poco:note/text()", $context)->item(0)->nodeValue;
-                       if ($value != "") {
+                       if ($value != "")
                                $poco["about"] = $value;
-                       }
 
                        $value = $xpath->evaluate($element."/poco:address/poco:formatted/text()", $context)->item(0)->nodeValue;
-                       if ($value != "") {
+                       if ($value != "")
                                $poco["location"] = $value;
-                       }
 
                        /// @todo Only search for elements with "poco:type" = "xmpp"
                        $value = $xpath->evaluate($element."/poco:ims/poco:value/text()", $context)->item(0)->nodeValue;
-                       if ($value != "") {
+                       if ($value != "")
                                $poco["xmpp"] = $value;
-                       }
 
                        /// @todo Add support for the following fields that we don't support by now in the contact table:
                        /// - poco:utcOffset
@@ -1314,20 +1279,17 @@ class dfrn {
 
                        // If the contact isn't searchable then set the contact to "hidden".
                        // Problem: This can be manually overridden by the user.
-                       if ($hide) {
+                       if ($hide)
                                $contact["hidden"] = true;
-                       }
 
                        // Save the keywords into the contact table
                        $tags = array();
                        $tagelements = $xpath->evaluate($element."/poco:tags/text()", $context);
-                       foreach ($tagelements AS $tag) {
+                       foreach($tagelements AS $tag)
                                $tags[$tag->nodeValue] = $tag->nodeValue;
-                       }
 
-                       if (count($tags)) {
+                       if (count($tags))
                                $poco["keywords"] = implode(", ", $tags);
-                       }
 
                        // "dfrn:birthday" contains the birthday converted to UTC
                        $old_bdyear = $contact["bdyear"];
@@ -1357,15 +1319,13 @@ class dfrn {
 
                        $contact = array_merge($contact, $poco);
 
-                       if ($old_bdyear != $contact["bdyear"]) {
+                       if ($old_bdyear != $contact["bdyear"])
                                self::birthday_event($contact, $birthday);
-                       }
 
                        // Get all field names
                        $fields = array();
-                       foreach ($r[0] AS $field => $data) {
+                       foreach ($r[0] AS $field => $data)
                                $fields[$field] = $data;
-                       }
 
                        unset($fields["id"]);
                        unset($fields["uid"]);
@@ -1414,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);
@@ -1430,9 +1392,8 @@ class dfrn {
         * @return string XML string
         */
        private static function transform_activity($xpath, $activity, $element) {
-               if (!is_object($activity)) {
+               if (!is_object($activity))
                        return "";
-               }
 
                $obj_doc = new DOMDocument("1.0", "utf-8");
                $obj_doc->formatOutput = true;