]> git.mxchange.org Git - friendica.git/blobdiff - include/dfrn.php
It is better this way
[friendica.git] / include / dfrn.php
index 83d76a0ae45cea961b58cd1ea60944fac1f10a0c..7e4d7af2bcceabe423bbe911fab8739b9eca6b2b 100644 (file)
@@ -7,6 +7,8 @@
  * https://github.com/friendica/friendica/blob/master/spec/dfrn2.pdf
  */
 
+use Friendica\App;
+
 require_once("include/Contact.php");
 require_once("include/ostatus.php");
 require_once("include/enotify.php");
@@ -370,7 +372,7 @@ class dfrn {
                $ext = Photo::supportedTypes();
 
                foreach ($rp as $p) {
-                       $photos[$p['scale']] = app::get_baseurl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']];
+                       $photos[$p['scale']] = App::get_baseurl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']];
                }
 
                unset($rp, $ext);
@@ -431,7 +433,7 @@ class dfrn {
                $root->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS);
                $root->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
 
-               xml::add_element($doc, $root, "id", app::get_baseurl()."/profile/".$owner["nick"]);
+               xml::add_element($doc, $root, "id", App::get_baseurl()."/profile/".$owner["nick"]);
                xml::add_element($doc, $root, "title", $owner["name"]);
 
                $attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION);
@@ -448,13 +450,13 @@ class dfrn {
                        // DFRN itself doesn't uses this. But maybe someone else wants to subscribe to the public feed.
                        ostatus::hublinks($doc, $root);
 
-                       $attributes = array("rel" => "salmon", "href" => app::get_baseurl()."/salmon/".$owner["nick"]);
+                       $attributes = array("rel" => "salmon", "href" => App::get_baseurl()."/salmon/".$owner["nick"]);
                        xml::add_element($doc, $root, "link", "", $attributes);
 
-                       $attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-replies", "href" => app::get_baseurl()."/salmon/".$owner["nick"]);
+                       $attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-replies", "href" => App::get_baseurl()."/salmon/".$owner["nick"]);
                        xml::add_element($doc, $root, "link", "", $attributes);
 
-                       $attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-mention", "href" => app::get_baseurl()."/salmon/".$owner["nick"]);
+                       $attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-mention", "href" => App::get_baseurl()."/salmon/".$owner["nick"]);
                        xml::add_element($doc, $root, "link", "", $attributes);
                }
 
@@ -511,7 +513,7 @@ class dfrn {
                }
 
                xml::add_element($doc, $author, "name", $owner["name"], $attributes);
-               xml::add_element($doc, $author, "uri", app::get_baseurl().'/profile/'.$owner["nickname"], $attributes);
+               xml::add_element($doc, $author, "uri", App::get_baseurl().'/profile/'.$owner["nickname"], $attributes);
                xml::add_element($doc, $author, "dfrn:handle", $owner["addr"], $attributes);
 
                $attributes = array("rel" => "photo", "type" => "image/jpeg",
@@ -554,8 +556,9 @@ class dfrn {
                        xml::add_element($doc, $author, "poco:displayName", $profile["name"]);
                        xml::add_element($doc, $author, "poco:updated", $namdate);
 
-                       if (trim($profile["dob"]) != "0000-00-00")
+                       if (trim($profile["dob"]) > '0001-01-01') {
                                xml::add_element($doc, $author, "poco:birthday", "0000-".date("m-d", strtotime($profile["dob"])));
+                       }
 
                        xml::add_element($doc, $author, "poco:note", $profile["about"]);
                        xml::add_element($doc, $author, "poco:preferredUsername", $profile["nickname"]);
@@ -811,11 +814,33 @@ class dfrn {
                        $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",
-                                               "href" => app::get_baseurl().'/display/'.$parent[0]['guid'],
+                                               "href" => App::get_baseurl().'/display/'.$parent[0]['guid'],
                                                "dfrn:diaspora_guid" => $parent[0]['guid']);
                        xml::add_element($doc, $entry, "thr:in-reply-to", "", $attributes);
                }
 
+               // Add conversation data. This is used for OStatus
+               $conversation_href = App::get_baseurl()."/display/".$owner["nick"]."/".$item["parent"];
+               $conversation_uri = $conversation_href;
+
+               if (isset($parent_item)) {
+                       $r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $item['parent-uri']);
+                       if (dbm::is_result($r)) {
+                               if ($r['conversation-uri'] != '') {
+                                       $conversation_uri = $r['conversation-uri'];
+                               }
+                               if ($r['conversation-href'] != '') {
+                                       $conversation_href = $r['conversation-href'];
+                               }
+                       }
+               }
+
+               $attributes = array(
+                               "href" => $conversation_href,
+                               "ref" => $conversation_uri);
+
+               xml::add_element($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes);
+
                xml::add_element($doc, $entry, "id", $item["uri"]);
                xml::add_element($doc, $entry, "title", $item["title"]);
 
@@ -831,7 +856,7 @@ class dfrn {
 
                // We save this value in "plink". Maybe we should read it from there as well?
                xml::add_element($doc, $entry, "link", "", array("rel" => "alternate", "type" => "text/html",
-                                                               "href" => app::get_baseurl()."/display/".$item["guid"]));
+                                                               "href" => App::get_baseurl()."/display/".$item["guid"]));
 
                // "comment-allow" is some old fashioned stuff for old Friendica versions.
                // It is included in the rewritten code for completeness
@@ -867,7 +892,7 @@ class dfrn {
 
                // The signed text contains the content in Markdown, the sender handle and the signatur for the content
                // It is needed for relayed comments to Diaspora.
-               if($item['signed_text']) {
+               if ($item['signed_text']) {
                        $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
                        xml::add_element($doc, $entry, "dfrn:diaspora_signature", $sign);
                }
@@ -931,6 +956,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
         *
@@ -958,11 +1007,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);
 
                $ssl_val = intval(get_config('system','ssl_policy'));
@@ -1093,8 +1137,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
@@ -1268,7 +1312,7 @@ class dfrn {
                        $href = "";
                        $width = 0;
                        foreach ($avatar->attributes AS $attributes) {
-                               /// @TODO Rewrite these similar if() to one switch
+                               /// @TODO Rewrite these similar if () to one switch
                                if ($attributes->name == "href") {
                                        $href = $attributes->textContent;
                                }
@@ -1383,7 +1427,7 @@ class dfrn {
                        // "poco:birthday" is the birthday in the format "yyyy-mm-dd"
                        $value = $xpath->evaluate($element . "/poco:birthday/text()", $context)->item(0)->nodeValue;
 
-                       if (!in_array($value, array("", "0000-00-00"))) {
+                       if (!in_array($value, array("", "0000-00-00", "0001-01-01"))) {
                                $bdyear = date("Y");
                                $value = str_replace("0000", $bdyear, $value);
 
@@ -1440,7 +1484,7 @@ class dfrn {
                                        dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["about"]), dbesc($contact["location"]),
                                        dbesc($contact["addr"]), dbesc($contact["keywords"]), dbesc($contact["bdyear"]),
                                        dbesc($contact["bd"]), intval($contact["hidden"]), dbesc($contact["xmpp"]),
-                                       dbesc($contact["name-date"]), dbesc($contact["uri-date"]),
+                                       dbesc(dbm::date($contact["name-date"])), dbesc(dbm::date($contact["uri-date"])),
                                        intval($contact["id"]), dbesc($contact["network"]));
                        }
 
@@ -1458,7 +1502,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);
@@ -2143,7 +2189,7 @@ class dfrn {
                $title = "";
                foreach ($links AS $link) {
                        foreach ($link->attributes AS $attributes) {
-                               /// @TODO Rewrite these repeated (same) if() statements to a switch()
+                               /// @TODO Rewrite these repeated (same) if () statements to a switch()
                                if ($attributes->name == "href") {
                                        $href = $attributes->textContent;
                                }
@@ -2187,12 +2233,16 @@ class dfrn {
         * @param array $importer Record of the importer user mixed with contact of the content
         * @todo Add type-hints
         */
-       private static function process_entry($header, $xpath, $entry, $importer) {
+       private static function process_entry($header, $xpath, $entry, $importer, $xml) {
 
                logger("Processing entries");
 
                $item = $header;
 
+               $item["protocol"] = PROTOCOL_DFRN;
+
+               $item["source"] = $xml;
+
                // Get the uri
                $item["uri"] = $xpath->query("atom:id/text()", $entry)->item(0)->nodeValue;
 
@@ -2351,6 +2401,20 @@ class dfrn {
                        self::parse_links($links, $item);
                }
 
+               $item['conversation-uri'] = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
+
+               $conv = $xpath->query('ostatus:conversation', $entry);
+               if (is_object($conv->item(0))) {
+                       foreach ($conv->item(0)->attributes AS $attributes) {
+                               if ($attributes->name == "ref") {
+                                       $item['conversation-uri'] = $attributes->textContent;
+                               }
+                               if ($attributes->name == "href") {
+                                       $item['conversation-href'] = $attributes->textContent;
+                               }
+                       }
+               }
+
                // Is it a reply or a top level posting?
                $item["parent-uri"] = $item["uri"];
 
@@ -2393,7 +2457,7 @@ class dfrn {
                        // When activated, forums don't work.
                        // And: Why should we disallow commenting by followers?
                        // the behaviour is now similar to the Diaspora part.
-                       //if($importer["rel"] == CONTACT_IS_FOLLOWER) {
+                       //if ($importer["rel"] == CONTACT_IS_FOLLOWER) {
                        //      logger("Contact ".$importer["id"]." is only follower. Quitting", LOGGER_DEBUG);
                        //      return;
                        //}
@@ -2518,7 +2582,7 @@ class dfrn {
 
                        logger("Item was stored with id ".$posted_id, LOGGER_DEBUG);
 
-                       if(stristr($item["verb"],ACTIVITY_POKE))
+                       if (stristr($item["verb"],ACTIVITY_POKE))
                                self::do_poke($item, $importer, $posted_id);
                }
        }
@@ -2645,7 +2709,7 @@ class dfrn {
                                create_tags_from_itemuri($uri, $importer["uid"]);
                                create_files_from_itemuri($uri, $importer["uid"]);
                                update_thread_uri($uri, $importer["importer_uid"]);
-                               if($item["last-child"]) {
+                               if ($item["last-child"]) {
                                        // ensure that last-child is set in case the comment that had it just got wiped.
                                        q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
                                                dbesc(datetime_convert()),
@@ -2779,7 +2843,7 @@ class dfrn {
                if (!$sort_by_date) {
                        $entries = $xpath->query("/atom:feed/atom:entry");
                        foreach ($entries AS $entry) {
-                               self::process_entry($header, $xpath, $entry, $importer);
+                               self::process_entry($header, $xpath, $entry, $importer, $xml);
                        }
                } else {
                        $newentries = array();
@@ -2793,7 +2857,7 @@ class dfrn {
                        ksort($newentries);
 
                        foreach ($newentries AS $entry) {
-                               self::process_entry($header, $xpath, $entry, $importer);
+                               self::process_entry($header, $xpath, $entry, $importer, $xml);
                        }
                }
                logger("Import done for user " . $importer["uid"] . " from contact " . $importer["id"], LOGGER_DEBUG);