]> git.mxchange.org Git - friendica.git/blobdiff - include/dfrn.php
Merge pull request #2975 from annando/1611-frontend-worker
[friendica.git] / include / dfrn.php
index f6f43660296c39a555cfee7a7150fa37ea013eda..67cef59d956ccbe037a060e272ba9f83a1439c97 100644 (file)
@@ -68,10 +68,11 @@ class dfrn {
         * @param string $owner_nick Owner nick name
         * @param string $last_update Date of the last update
         * @param int $direction Can be -1, 0 or 1.
+        * @param boolean $onlyheader Output only the header without content? (Default is "no")
         *
         * @return string DFRN feed entries
         */
-       public static function feed($dfrn_id, $owner_nick, $last_update, $direction = 0) {
+       public static function feed($dfrn_id, $owner_nick, $last_update, $direction = 0, $onlyheader = false) {
 
                $a = get_app();
 
@@ -97,9 +98,9 @@ class dfrn {
 
                $sql_extra = " AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' ";
 
-               $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
+               $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`, `user`.`account-type`
                        FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
-                       WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
+                       WHERE `contact`.`self` AND `user`.`nickname` = '%s' LIMIT 1",
                        dbesc($owner_nick)
                );
 
@@ -111,7 +112,6 @@ class dfrn {
                $owner_nick = $owner['nickname'];
 
                $sql_post_table = "";
-               $visibility = "";
 
                if(! $public_feed) {
 
@@ -134,7 +134,7 @@ class dfrn {
                                        break; // NOTREACHED
                        }
 
-                       $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
+                       $r = q("SELECT * FROM `contact` WHERE NOT `blocked` AND `contact`.`uid` = %d $sql_extra LIMIT 1",
                                intval($owner_id)
                        );
 
@@ -170,9 +170,6 @@ class dfrn {
                else
                        $sort = 'ASC';
 
-               $date_field = "`changed`";
-               $sql_order = "`item`.`parent` ".$sort.", `item`.`created` ASC";
-
                if(! strlen($last_update))
                        $last_update = 'now -30 days';
 
@@ -189,23 +186,19 @@ class dfrn {
 
                $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
 
-               //      AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
-               //      dbesc($check_date),
-
-               $r = q("SELECT STRAIGHT_JOIN `item`.*, `item`.`id` AS `item_id`,
+               $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
                        `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
                        `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
                        `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
-                       `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
                        `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
-                       FROM `item` $sql_post_table
-                       INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
-                       AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
+                       FROM `item` USE INDEX (`uid_wall_changed`, `uid_type_changed`) $sql_post_table
+                       STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+                       AND NOT `contact`.`blocked`
                        LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
-                       WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0
-                       AND ((`item`.`wall` = 1) $visibility) AND `item`.$date_field > '%s'
+                       WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`moderated` AND `item`.`parent` != 0
+                       AND `item`.`wall` AND `item`.`changed` > '%s'
                        $sql_extra
-                       ORDER BY $sql_order LIMIT 0, 300",
+                       ORDER BY `item`.`parent` ".$sort.", `item`.`created` ASC LIMIT 0, 300",
                        intval($owner_id),
                        dbesc($check_date),
                        dbesc($sort)
@@ -234,7 +227,7 @@ class dfrn {
                // This hook can't work anymore
                //      call_hooks('atom_feed', $atom);
 
-               if(! count($items)) {
+               if (!count($items) OR $onlyheader) {
                        $atom = trim($doc->saveXML());
 
                        call_hooks('atom_feed_end', $atom);
@@ -368,6 +361,8 @@ class dfrn {
 
                xml::add_element($doc, $relocate, "dfrn:url", $owner['url']);
                xml::add_element($doc, $relocate, "dfrn:name", $owner['name']);
+               xml::add_element($doc, $relocate, "dfrn:addr", $owner['addr']);
+               xml::add_element($doc, $relocate, "dfrn:avatar", $owner['avatar']);
                xml::add_element($doc, $relocate, "dfrn:photo", $photos[4]);
                xml::add_element($doc, $relocate, "dfrn:thumb", $photos[5]);
                xml::add_element($doc, $relocate, "dfrn:micro", $photos[6]);
@@ -438,9 +433,13 @@ class dfrn {
                        xml::add_element($doc, $root, "link", "", $attributes);
                }
 
+               // For backward compatibility we keep this element
                if ($owner['page-flags'] == PAGE_COMMUNITY)
                        xml::add_element($doc, $root, "dfrn:community", 1);
 
+               // The former element is replaced by this one
+               xml::add_element($doc, $root, "dfrn:account_type", $owner["account-type"]);
+
                /// @todo We need a way to transmit the different page flags like "PAGE_PRVGROUP"
 
                xml::add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
@@ -462,47 +461,64 @@ class dfrn {
         */
        private 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`
+                               WHERE (`hidewall` OR NOT `net-publish`) AND `user`.`uid` = %d",
+                       intval($owner['uid']));
+               if ($r)
+                       $hidewall = true;
+               else
+                       $hidewall = false;
+
                $author = $doc->createElement($authorelement);
 
-               $namdate = datetime_convert('UTC', 'UTC', $owner['name-date'].'+00:00' , ATOM_TIME);
+               $namdate = datetime_convert('UTC', 'UTC', $owner['name-date'].'+00:00', ATOM_TIME);
                $uridate = datetime_convert('UTC', 'UTC', $owner['uri-date'].'+00:00', ATOM_TIME);
                $picdate = datetime_convert('UTC', 'UTC', $owner['avatar-date'].'+00:00', ATOM_TIME);
 
-               $attributes = array("dfrn:updated" => $namdate);
-               xml::add_element($doc, $author, "name", $owner["name"], $attributes);
+               if (!$public OR !$hidewall)
+                       $attributes = array("dfrn:updated" => $namdate);
+               else
+                       $attributes = array();
 
-               $attributes = array("dfrn:updated" => $namdate);
+               xml::add_element($doc, $author, "name", $owner["name"], $attributes);
                xml::add_element($doc, $author, "uri", app::get_baseurl().'/profile/'.$owner["nickname"], $attributes);
-
-               $attributes = array("dfrn:updated" => $namdate);
                xml::add_element($doc, $author, "dfrn:handle", $owner["addr"], $attributes);
 
-               $attributes = array("rel" => "photo", "type" => "image/jpeg", "dfrn:updated" => $picdate,
+               $attributes = array("rel" => "photo", "type" => "image/jpeg",
                                        "media:width" => 175, "media:height" => 175, "href" => $owner['photo']);
+
+               if (!$public OR !$hidewall)
+                       $attributes["dfrn:updated"] = $picdate;
+
                xml::add_element($doc, $author, "link", "", $attributes);
 
-               $attributes = array("rel" => "avatar", "type" => "image/jpeg", "dfrn:updated" => $picdate,
-                                       "media:width" => 175, "media:height" => 175, "href" => $owner['photo']);
+               $attributes["rel"] = "avatar";
                xml::add_element($doc, $author, "link", "", $attributes);
 
+               if ($hidewall)
+                       xml::add_element($doc, $author, "dfrn:hide", "true");
+
+               // The following fields will only be generated if the data isn't meant for a public feed
+               if ($public)
+                       return $author;
+
                $birthday = feed_birthday($owner['uid'], $owner['timezone']);
 
                if ($birthday)
                        xml::add_element($doc, $author, "dfrn:birthday", $birthday);
 
-               // The following fields will only be generated if this isn't for a public feed
-               if ($public)
-                       return $author;
-
                // Only show contact details when we are allowed to
-               $r = q("SELECT `profile`.`about`, `profile`.`name`, `profile`.`homepage`, `user`.`nickname`, `user`.`timezone`,
-                               `profile`.`locality`, `profile`.`region`, `profile`.`country-name`, `profile`.`pub_keywords`, `profile`.`dob`
+               $r = q("SELECT `profile`.`about`, `profile`.`name`, `profile`.`homepage`, `user`.`nickname`,
+                               `user`.`timezone`, `profile`.`locality`, `profile`.`region`, `profile`.`country-name`,
+                               `profile`.`pub_keywords`, `profile`.`xmpp`, `profile`.`dob`
                        FROM `profile`
                                INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
                                WHERE `profile`.`is-default` AND NOT `user`.`hidewall` AND `user`.`uid` = %d",
                        intval($owner['uid']));
                if ($r) {
                        $profile = $r[0];
+
                        xml::add_element($doc, $author, "poco:displayName", $profile["name"]);
                        xml::add_element($doc, $author, "poco:updated", $namdate);
 
@@ -533,12 +549,10 @@ class dfrn {
 
                        }
 
-                       /// @todo When we are having the XMPP address in the profile we should propagate it here
-                       $xmpp = "";
-                       if (trim($xmpp) != "") {
+                       if (trim($profile["xmpp"]) != "") {
                                $ims = $doc->createElement("poco:ims");
                                xml::add_element($doc, $ims, "poco:type", "xmpp");
-                               xml::add_element($doc, $ims, "poco:value", $xmpp);
+                               xml::add_element($doc, $ims, "poco:value", $profile["xmpp"]);
                                xml::add_element($doc, $ims, "poco:primary", "true");
                                $author->appendChild($ims);
                        }
@@ -1126,7 +1140,7 @@ class dfrn {
                $author["link"] = $xpath->evaluate($element."/atom:uri/text()", $context)->item(0)->nodeValue;
 
                $r = q("SELECT `id`, `uid`, `url`, `network`, `avatar-date`, `name-date`, `uri-date`, `addr`,
-                               `name`, `nick`, `about`, `location`, `keywords`, `bdyear`, `bd`
+                               `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 ($r) {
@@ -1202,14 +1216,28 @@ class dfrn {
                        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 != "")
+                               $poco["xmpp"] = $value;
+
                        /// @todo Add support for the following fields that we don't support by now in the contact table:
                        /// - poco:utcOffset
-                       /// - poco:ims
                        /// - poco:urls
                        /// - poco:locality
                        /// - poco:region
                        /// - poco:country
 
+                       // If the "hide" element is present then the profile isn't searchable.
+                       $hide = intval($xpath->evaluate($element."/dfrn:hide/text()", $context)->item(0)->nodeValue == "true");
+
+                       logger("Hidden status for contact ".$contact["url"].": ".$hide, LOGGER_DEBUG);
+
+                       // If the contact isn't searchable then set the contact to "hidden".
+                       // Problem: This can be manually overridden by the user.
+                       if ($hide)
+                               $contact["hidden"] = true;
+
                        // Save the keywords into the contact table
                        $tags = array();
                        $tagelements = $xpath->evaluate($element."/poco:tags/text()", $context);
@@ -1262,17 +1290,17 @@ class dfrn {
                        unset($fields["name-date"]);
                        unset($fields["uri-date"]);
 
-                        // Update check for this field has to be done differently
+                       // Update check for this field has to be done differently
                        $datefields = array("name-date", "uri-date");
                        foreach ($datefields AS $field)
                                if (strtotime($contact[$field]) > strtotime($r[0][$field])) {
-                                       logger("Difference for contact ".$contact["id"]." in field '".$field."'. Old value: '".$contact[$field]."', new value '".$r[0][$field]."'", LOGGER_DEBUG);
+                                       logger("Difference for contact ".$contact["id"]." in field '".$field."'. New value: '".$contact[$field]."', old value '".$r[0][$field]."'", LOGGER_DEBUG);
                                        $update = true;
                                }
 
                        foreach ($fields AS $field => $data)
                                if ($contact[$field] != $r[0][$field]) {
-                                       logger("Difference for contact ".$contact["id"]." in field '".$field."'. Old value: '".$contact[$field]."', new value '".$r[0][$field]."'", LOGGER_DEBUG);
+                                       logger("Difference for contact ".$contact["id"]." in field '".$field."'. New value: '".$contact[$field]."', old value '".$r[0][$field]."'", LOGGER_DEBUG);
                                        $update = true;
                                }
 
@@ -1280,12 +1308,13 @@ class dfrn {
                                logger("Update contact data for contact ".$contact["id"]." (".$contact["nick"].")", LOGGER_DEBUG);
 
                                q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `about` = '%s', `location` = '%s',
-                                       `addr` = '%s', `keywords` = '%s', `bdyear` = '%s', `bd` = '%s',
-                                       `name-date`  = '%s', `uri-date` = '%s'
+                                       `addr` = '%s', `keywords` = '%s', `bdyear` = '%s', `bd` = '%s', `hidden` = %d,
+                                       `xmpp` = '%s', `name-date`  = '%s', `uri-date` = '%s'
                                        WHERE `id` = %d AND `network` = '%s'",
                                        dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["about"]), dbesc($contact["location"]),
                                        dbesc($contact["addr"]), dbesc($contact["keywords"]), dbesc($contact["bdyear"]),
-                                       dbesc($contact["bd"]), dbesc($contact["name-date"]), dbesc($contact["uri-date"]),
+                                       dbesc($contact["bd"]), intval($contact["hidden"]), dbesc($contact["xmpp"]),
+                                       dbesc($contact["name-date"]), dbesc($contact["uri-date"]),
                                        intval($contact["id"]), dbesc($contact["network"]));
                        }
 
@@ -1299,6 +1328,8 @@ class dfrn {
 
                        $poco["generation"] = 2;
                        $poco["photo"] = $author["avatar"];
+                       $poco["hide"] = $hide;
+                       $poco["contact-type"] = $contact["contact-type"];
                        update_gcontact($poco);
                }
 
@@ -1519,7 +1550,9 @@ class dfrn {
                $relocate["uid"] = $importer["importer_uid"];
                $relocate["cid"] = $importer["id"];
                $relocate["url"] = $xpath->query("dfrn:url/text()", $relocation)->item(0)->nodeValue;
+               $relocate["addr"] = $xpath->query("dfrn:addr/text()", $relocation)->item(0)->nodeValue;
                $relocate["name"] = $xpath->query("dfrn:name/text()", $relocation)->item(0)->nodeValue;
+               $relocate["avatar"] = $xpath->query("dfrn:avatar/text()", $relocation)->item(0)->nodeValue;
                $relocate["photo"] = $xpath->query("dfrn:photo/text()", $relocation)->item(0)->nodeValue;
                $relocate["thumb"] = $xpath->query("dfrn:thumb/text()", $relocation)->item(0)->nodeValue;
                $relocate["micro"] = $xpath->query("dfrn:micro/text()", $relocation)->item(0)->nodeValue;
@@ -1529,6 +1562,12 @@ class dfrn {
                $relocate["poll"] = $xpath->query("dfrn:poll/text()", $relocation)->item(0)->nodeValue;
                $relocate["sitepubkey"] = $xpath->query("dfrn:sitepubkey/text()", $relocation)->item(0)->nodeValue;
 
+               if (($relocate["avatar"] == "") AND ($relocate["photo"] != ""))
+                       $relocate["avatar"] = $relocate["photo"];
+
+               if ($relocate["addr"] == "")
+                       $relocate["addr"] = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$3@$2", $relocate["url"]);
+
                // update contact
                $r = q("SELECT `photo`, `url` FROM `contact` WHERE `id` = %d AND `uid` = %d;",
                        intval($importer["id"]),
@@ -1538,51 +1577,83 @@ class dfrn {
 
                $old = $r[0];
 
-               $x = q("UPDATE `contact` SET
+               // Update the gcontact entry
+               $relocate["server_url"] = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $relocate["url"]);
+
+               $x = q("UPDATE `gcontact` SET
                                        `name` = '%s',
                                        `photo` = '%s',
-                                       `thumb` = '%s',
-                                       `micro` = '%s',
                                        `url` = '%s',
                                        `nurl` = '%s',
+                                       `addr` = '%s',
+                                       `connect` = '%s',
+                                       `notify` = '%s',
+                                       `server_url` = '%s'
+                       WHERE `nurl` = '%s';",
+                                       dbesc($relocate["name"]),
+                                       dbesc($relocate["avatar"]),
+                                       dbesc($relocate["url"]),
+                                       dbesc(normalise_link($relocate["url"])),
+                                       dbesc($relocate["addr"]),
+                                       dbesc($relocate["addr"]),
+                                       dbesc($relocate["notify"]),
+                                       dbesc($relocate["server_url"]),
+                                       dbesc(normalise_link($old["url"])));
+
+               // Update the contact table. We try to find every entry.
+               $x = q("UPDATE `contact` SET
+                                       `name` = '%s',
+                                       `avatar` = '%s',
+                                       `url` = '%s',
+                                       `nurl` = '%s',
+                                       `addr` = '%s',
                                        `request` = '%s',
                                        `confirm` = '%s',
                                        `notify` = '%s',
                                        `poll` = '%s',
                                        `site-pubkey` = '%s'
-                       WHERE `id` = %d AND `uid` = %d;",
+                       WHERE (`id` = %d AND `uid` = %d) OR (`nurl` = '%s');",
                                        dbesc($relocate["name"]),
-                                       dbesc($relocate["photo"]),
-                                       dbesc($relocate["thumb"]),
-                                       dbesc($relocate["micro"]),
+                                       dbesc($relocate["avatar"]),
                                        dbesc($relocate["url"]),
                                        dbesc(normalise_link($relocate["url"])),
+                                       dbesc($relocate["addr"]),
                                        dbesc($relocate["request"]),
                                        dbesc($relocate["confirm"]),
                                        dbesc($relocate["notify"]),
                                        dbesc($relocate["poll"]),
                                        dbesc($relocate["sitepubkey"]),
                                        intval($importer["id"]),
-                                       intval($importer["importer_uid"]));
+                                       intval($importer["importer_uid"]),
+                                       dbesc(normalise_link($old["url"])));
+
+               update_contact_avatar($relocate["avatar"], $importer["importer_uid"], $importer["id"], true);
 
                if ($x === false)
                        return false;
 
                // update items
+               /// @todo This is an extreme performance killer
                $fields = array(
                        'owner-link' => array($old["url"], $relocate["url"]),
                        'author-link' => array($old["url"], $relocate["url"]),
-                       'owner-avatar' => array($old["photo"], $relocate["photo"]),
-                       'author-avatar' => array($old["photo"], $relocate["photo"]),
+                       //'owner-avatar' => array($old["photo"], $relocate["photo"]),
+                       //'author-avatar' => array($old["photo"], $relocate["photo"]),
                        );
-               foreach ($fields as $n=>$f){
-                       $x = q("UPDATE `item` SET `%s` = '%s' WHERE `%s` = '%s' AND `uid` = %d",
-                                       $n, dbesc($f[1]),
+               foreach ($fields as $n=>$f) {
+                       $r = q("SELECT `id` FROM `item` WHERE `%s` = '%s' AND `uid` = %d LIMIT 1",
                                        $n, dbesc($f[0]),
                                        intval($importer["importer_uid"]));
-                               if ($x === false)
-                                       return false;
+
+                       if ($r) {
+                               $x = q("UPDATE `item` SET `%s` = '%s' WHERE `%s` = '%s' AND `uid` = %d",
+                                               $n, dbesc($f[1]),
+                                               $n, dbesc($f[0]),
+                                               intval($importer["importer_uid"]));
+                                       if ($x === false)
+                                               return false;
                        }
+               }
 
                /// @TODO
                /// merge with current record, current contents have priority
@@ -1624,7 +1695,7 @@ class dfrn {
                        $changed = true;
 
                        if ($entrytype == DFRN_REPLY_RC)
-                               proc_run("php", "include/notifier.php","comment-import", $current["id"]);
+                               proc_run(PRIORITY_HIGH, "include/notifier.php","comment-import", $current["id"]);
                }
 
                // update last-child if it changes
@@ -2184,7 +2255,7 @@ class dfrn {
 
                                if($posted_id AND $parent AND ($entrytype == DFRN_REPLY_RC)) {
                                        logger("Notifying followers about comment ".$posted_id, LOGGER_DEBUG);
-                                       proc_run("php", "include/notifier.php", "comment-import", $posted_id);
+                                       proc_run(PRIORITY_HIGH, "include/notifier.php", "comment-import", $posted_id);
                                }
 
                                return true;
@@ -2355,7 +2426,7 @@ class dfrn {
 
                                if($entrytype == DFRN_REPLY_RC) {
                                        logger("Notifying followers about deletion of post ".$item["id"], LOGGER_DEBUG);
-                                       proc_run("php", "include/notifier.php","drop", $item["id"]);
+                                       proc_run(PRIORITY_HIGH, "include/notifier.php","drop", $item["id"]);
                                }
                        }
                }
@@ -2415,7 +2486,19 @@ class dfrn {
 
                logger("Import DFRN message for user ".$importer["uid"]." from contact ".$importer["id"], LOGGER_DEBUG);
 
-               // is it a public forum? Private forums aren't supported by now with this method
+               // The account type is new since 3.5.1
+               if ($xpath->query("/atom:feed/dfrn:account_type")->length > 0) {
+                       $accounttype = intval($xpath->evaluate("/atom:feed/dfrn:account_type/text()", $context)->item(0)->nodeValue);
+
+                       if ($accounttype != $importer["contact-type"])
+                               q("UPDATE `contact` SET `contact-type` = %d WHERE `id` = %d",
+                                       intval($accounttype),
+                                       intval($importer["id"])
+                               );
+               }
+
+               // is it a public forum? Private forums aren't supported with this method
+               // This is deprecated since 3.5.1
                $forum = intval($xpath->evaluate("/atom:feed/dfrn:community/text()", $context)->item(0)->nodeValue);
 
                if ($forum != $importer["forum"])