+function poco_reachable($profile, $server = "", $network = "", $force = false) {
+
+ if ($server == "")
+ $server = poco_detect_server($profile);
+
+ if ($server == "")
+ return true;
+
+ return poco_check_server($server, $network, $force);
+}
+
+function poco_detect_server($profile) {
+
+ // Try to detect the server path based upon some known standard paths
+ $server_url = "";
+
+ if ($server_url == "") {
+ $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
+ if ($friendica != $profile) {
+ $server_url = $friendica;
+ $network = NETWORK_DFRN;
+ }
+ }
+
+ if ($server_url == "") {
+ $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
+ if ($diaspora != $profile) {
+ $server_url = $diaspora;
+ $network = NETWORK_DIASPORA;
+ }
+ }
+
+ if ($server_url == "") {
+ $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
+ if ($red != $profile) {
+ $server_url = $red;
+ $network = NETWORK_DIASPORA;
+ }
+ }
+
+ return $server_url;
+}
+
+function poco_alternate_ostatus_url($url) {
+ return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
+}
+
+function poco_last_updated($profile, $force = false) {
+
+ $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
+ dbesc(normalise_link($profile)));
+
+ if ($gcontacts[0]["created"] == "0000-00-00 00:00:00")
+ q("UPDATE `gcontact` SET `created` = '%s' WHERE `nurl` = '%s'",
+ dbesc(datetime_convert()), dbesc(normalise_link($profile)));
+
+ if ($gcontacts[0]["server_url"] != "")
+ $server_url = $gcontacts[0]["server_url"];
+ else
+ $server_url = poco_detect_server($profile);
+
+ if ($server_url != "") {
+ if (!poco_check_server($server_url, $gcontacts[0]["network"], $force)) {
+
+ if ($force)
+ q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
+ dbesc(datetime_convert()), dbesc(normalise_link($profile)));
+
+ return false;
+ }
+
+ q("UPDATE `gcontact` SET `server_url` = '%s' WHERE `nurl` = '%s'",
+ dbesc($server_url), dbesc(normalise_link($profile)));
+ }
+
+ if (in_array($gcontacts[0]["network"], array("", NETWORK_FEED))) {
+ $server = q("SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
+ dbesc(normalise_link($server_url)));
+
+ if ($server)
+ q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
+ dbesc($server[0]["network"]), dbesc(normalise_link($profile)));
+ else
+ return;
+ }
+
+ // noscrape is really fast so we don't cache the call.
+ if (($gcontacts[0]["server_url"] != "") AND ($gcontacts[0]["nick"] != "")) {
+
+ // Use noscrape if possible
+ $server = q("SELECT `noscrape` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($gcontacts[0]["server_url"])));
+
+ if ($server) {
+ $noscraperet = z_fetch_url($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
+
+ if ($noscraperet["success"] AND ($noscraperet["body"] != "")) {
+
+ $noscrape = json_decode($noscraperet["body"], true);
+
+ if (($noscrape["fn"] != "") AND ($noscrape["fn"] != $gcontacts[0]["name"]))
+ q("UPDATE `gcontact` SET `name` = '%s' WHERE `nurl` = '%s'",
+ dbesc($noscrape["fn"]), dbesc(normalise_link($profile)));
+
+ if (($noscrape["photo"] != "") AND ($noscrape["photo"] != $gcontacts[0]["photo"]))
+ q("UPDATE `gcontact` SET `photo` = '%s' WHERE `nurl` = '%s'",
+ dbesc($noscrape["photo"]), dbesc(normalise_link($profile)));
+
+ if (($noscrape["updated"] != "") AND ($noscrape["updated"] != $gcontacts[0]["updated"]))
+ q("UPDATE `gcontact` SET `updated` = '%s' WHERE `nurl` = '%s'",
+ dbesc($noscrape["updated"]), dbesc(normalise_link($profile)));
+
+ if (($noscrape["gender"] != "") AND ($noscrape["gender"] != $gcontacts[0]["gender"]))
+ q("UPDATE `gcontact` SET `gender` = '%s' WHERE `nurl` = '%s'",
+ dbesc($noscrape["gender"]), dbesc(normalise_link($profile)));
+
+ if (($noscrape["pdesc"] != "") AND ($noscrape["pdesc"] != $gcontacts[0]["about"]))
+ q("UPDATE `gcontact` SET `about` = '%s' WHERE `nurl` = '%s'",
+ dbesc($noscrape["pdesc"]), dbesc(normalise_link($profile)));
+
+ if (($noscrape["about"] != "") AND ($noscrape["about"] != $gcontacts[0]["about"]))
+ q("UPDATE `gcontact` SET `about` = '%s' WHERE `nurl` = '%s'",
+ dbesc($noscrape["about"]), dbesc(normalise_link($profile)));
+
+ if (isset($noscrape["comm"]) AND ($noscrape["comm"] != $gcontacts[0]["community"]))
+ q("UPDATE `gcontact` SET `community` = %d WHERE `nurl` = '%s'",
+ intval($noscrape["comm"]), dbesc(normalise_link($profile)));
+
+ if (isset($noscrape["tags"]))
+ $keywords = implode(" ", $noscrape["tags"]);
+ else
+ $keywords = "";
+
+ if (($keywords != "") AND ($keywords != $gcontacts[0]["keywords"]))
+ q("UPDATE `gcontact` SET `keywords` = '%s' WHERE `nurl` = '%s'",
+ dbesc($keywords), dbesc(normalise_link($profile)));
+
+ $location = $noscrape["locality"];
+
+ if ($noscrape["region"] != "") {
+ if ($location != "")
+ $location .= ", ";
+
+ $location .= $noscrape["region"];
+ }
+
+ if ($noscrape["country-name"] != "") {
+ if ($location != "")
+ $location .= ", ";
+
+ $location .= $noscrape["country-name"];
+ }
+
+ if (($location != "") AND ($location != $gcontacts[0]["location"]))
+ q("UPDATE `gcontact` SET `location` = '%s' WHERE `nurl` = '%s'",
+ dbesc($location), dbesc(normalise_link($profile)));
+
+ // If we got data from noscrape then mark the contact as reachable
+ if (is_array($noscrape) AND count($noscrape))
+ q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'",
+ dbesc(datetime_convert()), dbesc(normalise_link($profile)));
+
+ return $noscrape["updated"];
+ }
+ }
+ }
+
+ // If we only can poll the feed, then we only do this once a while
+ if (!$force AND !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"]))
+ return $gcontacts[0]["updated"];
+