]> git.mxchange.org Git - friendica.git/blobdiff - include/socgraph.php
Merge pull request #2347 from annando/1602-dfrn-forum
[friendica.git] / include / socgraph.php
index 4b4b3587879cc6e2bd11fc4ba09710d5d98fa75b..3b8e9140f8eb476b36d30f553eedb81679d03dba 100644 (file)
@@ -139,15 +139,16 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
                poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
 
                // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
-               if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
-                       q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
-                               WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
-                               dbesc($location),
-                               dbesc($about),
-                               dbesc($keywords),
-                               dbesc($gender),
-                               dbesc(normalise_link($profile_url)),
-                               dbesc(NETWORK_DFRN));
+               // Deactivated because we now update Friendica contacts in dfrn.php
+               //if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
+               //      q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
+               //              WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
+               //              dbesc($location),
+               //              dbesc($about),
+               //              dbesc($keywords),
+               //              dbesc($gender),
+               //              dbesc(normalise_link($profile_url)),
+               //              dbesc(NETWORK_DFRN));
        }
        logger("poco_load: loaded $total entries",LOGGER_DEBUG);
 
@@ -289,93 +290,25 @@ function poco_check($profile_url, $name, $network, $profile_photo, $about, $loca
 
        poco_check_server($server_url, $network);
 
-       if(count($x)) {
-               $gcid = $x[0]['id'];
-
-               if (($location == "") AND ($x[0]['location'] != ""))
-                       $location = $x[0]['location'];
-
-               if (($about == "") AND ($x[0]['about'] != ""))
-                       $about = $x[0]['about'];
-
-               if (($gender == "") AND ($x[0]['gender'] != ""))
-                       $gender = $x[0]['gender'];
-
-               if (($keywords == "") AND ($x[0]['keywords'] != ""))
-                       $keywords = $x[0]['keywords'];
-
-               if (($addr == "") AND ($x[0]['addr'] != ""))
-                       $addr = $x[0]['addr'];
-
-               if (($alias == "") AND ($x[0]['alias'] != ""))
-                       $alias = $x[0]['alias'];
-
-               if (($notify == "") AND ($x[0]['notify'] != ""))
-                       $notify = $x[0]['notify'];
-
-               if (($generation == 0) AND ($x[0]['generation'] > 0))
-                       $generation = $x[0]['generation'];
-
-               if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
-                       q("UPDATE `gcontact` SET `name` = '%s', `addr` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `server_url` = '%s',
-                               `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s', `generation` = %d,
-                               `alias` = '$s', `notify` = '%s'
-                               WHERE (`generation` >= %d OR `generation` = 0) AND `nurl` = '%s'",
-                               dbesc($name),
-                               dbesc($addr),
-                               dbesc($network),
-                               dbesc($profile_photo),
-                               dbesc($connect_url),
-                               dbesc($profile_url),
-                               dbesc($server_url),
-                               dbesc($updated),
-                               dbesc($location),
-                               dbesc($about),
-                               dbesc($keywords),
-                               dbesc($gender),
-                               dbesc($alias),
-                               dbesc($notify),
-                               intval($generation),
-                               intval($generation),
-                               dbesc(normalise_link($profile_url))
-                       );
-               }
-       } else {
-               // Maybe another process had inserted the entry after the first check, so it again
-               $x = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
-                       dbesc(normalise_link($profile_url))
-               );
-               if(!$x) {
-                       q("INSERT INTO `gcontact` (`name`, `nick`, `addr`, `network`, `url`, `nurl`, `photo`, `connect`, `server_url`, `created`, `updated`, `location`, `about`, `keywords`, `gender`, `alias`, `notify`, `generation`)
-                               VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)",
-                               dbesc($name),
-                               dbesc($nick),
-                               dbesc($addr),
-                               dbesc($network),
-                               dbesc($profile_url),
-                               dbesc(normalise_link($profile_url)),
-                               dbesc($profile_photo),
-                               dbesc($connect_url),
-                               dbesc($server_url),
-                               dbesc(datetime_convert()),
-                               dbesc($updated),
-                               dbesc($location),
-                               dbesc($about),
-                               dbesc($keywords),
-                               dbesc($gender),
-                               dbesc($alias),
-                               dbesc($notify),
-                               intval($generation)
-                       );
-                       $x = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
-                               dbesc(normalise_link($profile_url))
-                       );
-               }
-               if(count($x))
-                       $gcid = $x[0]['id'];
-       }
-
-       if(! $gcid)
+       $gcontact = array("url" => $profile_url,
+                       "addr" => $addr,
+                       "alias" => $alias,
+                       "name" => $name,
+                       "network" => $network,
+                       "photo" => $profile_photo,
+                       "about" => $about,
+                       "location" => $location,
+                       "gender" => $gender,
+                       "keywords" => $keywords,
+                       "server_url" => $server_url,
+                       "connect" => $connect_url,
+                       "notify" => $notify,
+                       "updated" => $updated,
+                       "generation" => $generation);
+
+       $gcid = update_gcontact($gcontact);
+
+       if(!$gcid)
                return $gcid;
 
        $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
@@ -402,13 +335,6 @@ function poco_check($profile_url, $name, $network, $profile_photo, $about, $loca
                );
        }
 
-       // For unknown reasons there are sometimes duplicates
-       //q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
-       //      NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
-       //      dbesc(normalise_link($profile_url)),
-       //      intval($gcid)
-       //);
-
        return $gcid;
 }
 
@@ -796,19 +722,23 @@ function poco_check_server($server_url, $network = "", $force = false) {
                // Test for Diaspora
                $serverret = z_fetch_url($server_url);
 
-               $lines = explode("\n",$serverret["header"]);
-               if(count($lines))
-                       foreach($lines as $line) {
-                               $line = trim($line);
-                               if(stristr($line,'X-Diaspora-Version:')) {
-                                       $platform = "Diaspora";
-                                       $version = trim(str_replace("X-Diaspora-Version:", "", $line));
-                                       $version = trim(str_replace("x-diaspora-version:", "", $version));
-                                       $network = NETWORK_DIASPORA;
-                                       $versionparts = explode("-", $version);
-                                       $version = $versionparts[0];
+               if (!$serverret["success"] OR ($serverret["body"] == ""))
+                       $failure = true;
+               else {
+                       $lines = explode("\n",$serverret["header"]);
+                       if(count($lines))
+                               foreach($lines as $line) {
+                                       $line = trim($line);
+                                       if(stristr($line,'X-Diaspora-Version:')) {
+                                               $platform = "Diaspora";
+                                               $version = trim(str_replace("X-Diaspora-Version:", "", $line));
+                                               $version = trim(str_replace("x-diaspora-version:", "", $version));
+                                               $network = NETWORK_DIASPORA;
+                                               $versionparts = explode("-", $version);
+                                               $version = $versionparts[0];
+                                       }
                                }
-                       }
+               }
        }
 
        if (!$failure) {
@@ -1536,7 +1466,7 @@ function get_gcontact_id($contact) {
        if ($contact["network"] == NETWORK_STATUSNET)
                $contact["network"] = NETWORK_OSTATUS;
 
-       $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
+       $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
                dbesc(normalise_link($contact["url"])));
 
        if ($r)
@@ -1558,13 +1488,18 @@ function get_gcontact_id($contact) {
                        intval($contact["generation"])
                );
 
-               $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
+               $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
                        dbesc(normalise_link($contact["url"])));
 
                if ($r)
                        $gcontact_id = $r[0]["id"];
        }
 
+       if ((count($r) > 1) AND ($gcontact_id > 0) AND ($contact["url"] != ""))
+        q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d",
+               dbesc(normalise_link($contact["url"])),
+               intval($gcontact_id));
+
        return $gcontact_id;
 }
 
@@ -1583,78 +1518,55 @@ function update_gcontact($contact) {
        if (!$gcontact_id)
                return false;
 
-       $r = q("SELECT `name`, `nick`, `photo`, `location`, `about`, `addr`, `generation`, `birthday`, `gender`, `keywords`, `hide`, `nsfw`, `network`, `alias`, `notify`, `url`
+       $r = q("SELECT `name`, `nick`, `photo`, `location`, `about`, `addr`, `generation`, `birthday`, `gender`, `keywords`,
+                       `hide`, `nsfw`, `network`, `alias`, `notify`, `server_url`, `connect`, `updated`, `url`
                FROM `gcontact` WHERE `id` = %d LIMIT 1",
                intval($gcontact_id));
 
-       if ($contact["generation"] == 0)
-               $contact["generation"] = $r[0]["generation"];
-
-       if ($contact["photo"] == "")
-               $contact["photo"] = $r[0]["photo"];
-
-       if ($contact["name"] == "")
-               $contact["name"] = $r[0]["name"];
-
-       if ($contact["nick"] == "")
-               $contact["nick"] = $r[0]["nick"];
-
-       if ($contact["addr"] == "")
-               $contact["addr"] = $r[0]["addr"];
+       // Get all field names
+       $fields = array();
+       foreach ($r[0] AS $field => $data)
+               $fields[$field] = $data;
 
-       if ($contact["location"] =="")
-               $contact["location"] = $r[0]["location"];
+       unset($fields["url"]);
+       unset($fields["updated"]);
 
-       if ($contact["about"] =="")
-               $contact["about"] = $r[0]["about"];
+       // assign all unassigned fields from the database entry
+       foreach ($fields AS $field => $data)
+               if (!isset($contact[$field]))
+                       $contact[$field] = $r[0][$field];
 
-       if ($contact["birthday"] =="")
-               $contact["birthday"] = $r[0]["birthday"];
-
-       if ($contact["gender"] =="")
-               $contact["gender"] = $r[0]["gender"];
-
-       if ($contact["keywords"] =="")
-               $contact["keywords"] = $r[0]["keywords"];
-
-       if (!isset($contact["hide"]))
-               $contact["hide"] = $r[0]["hide"];
-
-       if (!isset($contact["nsfw"]))
-               $contact["nsfw"] = $r[0]["nsfw"];
-
-       if ($contact["network"] =="")
-               $contact["network"] = $r[0]["network"];
-
-       if ($contact["alias"] =="")
-               $contact["alias"] = $r[0]["alias"];
+       if ($contact["network"] == NETWORK_STATUSNET)
+               $contact["network"] = NETWORK_OSTATUS;
 
-       if ($contact["url"] =="")
-               $contact["url"] = $r[0]["url"];
+       if (!isset($contact["updated"]))
+               $contact["updated"] = datetime_convert();
 
-       if ($contact["notify"] =="")
-               $contact["notify"] = $r[0]["notify"];
+       // Check if any field changed
+       $update = false;
+       unset($fields["generation"]);
 
-       if ($contact["network"] == NETWORK_STATUSNET)
-               $contact["network"] = NETWORK_OSTATUS;
+       foreach ($fields AS $field => $data)
+               if ($contact[$field] != $r[0][$field])
+                       $update = true;
 
-       if (($contact["photo"] != $r[0]["photo"]) OR ($contact["name"] != $r[0]["name"]) OR ($contact["nick"] != $r[0]["nick"]) OR ($contact["addr"] != $r[0]["addr"]) OR
-               ($contact["birthday"] != $r[0]["birthday"]) OR ($contact["gender"] != $r[0]["gender"]) OR ($contact["keywords"] != $r[0]["keywords"]) OR
-               ($contact["hide"] != $r[0]["hide"]) OR ($contact["nsfw"] != $r[0]["nsfw"]) OR ($contact["network"] != $r[0]["network"]) OR
-               ($contact["alias"] != $r[0]["alias"]) OR ($contact["notify"] != $r[0]["notify"]) OR ($contact["url"] != $r[0]["url"]) OR
-               ($contact["location"] != $r[0]["location"]) OR ($contact["about"] != $r[0]["about"]) OR ($contact["generation"] < $r[0]["generation"])) {
+       if ($contact["generation"] < $r[0]["generation"])
+               $update = true;
 
+       if ($update) {
                q("UPDATE `gcontact` SET `photo` = '%s', `name` = '%s', `nick` = '%s', `addr` = '%s', `network` = '%s',
-                                       `birthday` = '%s', `gender` = '%s', `keywords` = %d, `hide` = %d, `nsfw` = %d,
+                                       `birthday` = '%s', `gender` = '%s', `keywords` = '%s', `hide` = %d, `nsfw` = %d,
                                        `alias` = '%s', `notify` = '%s', `url` = '%s',
-                                       `location` = '%s', `about` = '%s', `generation` = %d, `updated` = '%s'
+                                       `location` = '%s', `about` = '%s', `generation` = %d, `updated` = '%s',
+                                       `server_url` = '%s', `connect` = '%s'
                                WHERE `nurl` = '%s' AND (`generation` = 0 OR `generation` >= %d)",
                        dbesc($contact["photo"]), dbesc($contact["name"]), dbesc($contact["nick"]),
                        dbesc($contact["addr"]), dbesc($contact["network"]), dbesc($contact["birthday"]),
                        dbesc($contact["gender"]), dbesc($contact["keywords"]), intval($contact["hide"]),
                        intval($contact["nsfw"]), dbesc($contact["alias"]), dbesc($contact["notify"]),
                        dbesc($contact["url"]), dbesc($contact["location"]), dbesc($contact["about"]),
-                       intval($contact["generation"]), dbesc(datetime_convert()),
+                       intval($contact["generation"]), dbesc($contact["updated"]),
+                       dbesc($contact["server_url"]), dbesc($contact["connect"]),
                        dbesc(normalise_link($contact["url"])), intval($contact["generation"]));
        }
 
@@ -1714,18 +1626,19 @@ function gs_fetch_users($server) {
                $hostname = $statistics->instance_address;
        }
 
-       foreach ($statistics->users AS $nick => $user) {
-               $profile_url = $server."/".$user->nickname;
-
-               $contact = array("url" => $profile_url,
-                               "name" => $user->fullname,
-                               "addr" => $user->nickname."@".$hostname,
-                               "nick" => $user->nickname,
-                               "about" => $user->bio,
-                               "network" => NETWORK_OSTATUS,
-                               "photo" => $a->get_baseurl()."/images/person-175.jpg");
-               get_gcontact_id($contact);
-       }
+       if (is_object($statistics->users))
+               foreach ($statistics->users AS $nick => $user) {
+                       $profile_url = $server."/".$user->nickname;
+
+                       $contact = array("url" => $profile_url,
+                                       "name" => $user->fullname,
+                                       "addr" => $user->nickname."@".$hostname,
+                                       "nick" => $user->nickname,
+                                       "about" => $user->bio,
+                                       "network" => NETWORK_OSTATUS,
+                                       "photo" => $a->get_baseurl()."/images/person-175.jpg");
+                       get_gcontact_id($contact);
+               }
 }
 
 /**