]> git.mxchange.org Git - friendica.git/blobdiff - include/socgraph.php
Revert DB update
[friendica.git] / include / socgraph.php
index ec0f3854da0fc190cb9689f878e471bd280c1820..1d9f2adebabe9f5b2c9688ba5474597157815de7 100644 (file)
@@ -7,6 +7,8 @@
  * @todo Detect if it is a forum
  */
 
+use \Friendica\Core\Config;
+
 require_once('include/datetime.php');
 require_once("include/Scrape.php");
 require_once("include/network.php");
@@ -172,10 +174,13 @@ function poco_load_worker($cid, $uid, $zcid, $url) {
                                "contact-type" => $contact_type,
                                "generation" => $generation);
 
-               if (sanitized_gcontact($gcontact)) {
+               try {
+                       $gcontact = sanitize_gcontact($gcontact);
                        $gcid = update_gcontact($gcontact);
 
                        link_gcontact($gcid, $uid, $cid, $zcid);
+               } catch (Exception $e) {
+                       logger($e->getMessage(), LOGGER_DEBUG);
                }
        }
        logger("poco_load: loaded $total entries",LOGGER_DEBUG);
@@ -191,6 +196,7 @@ function poco_load_worker($cid, $uid, $zcid, $url) {
  * @brief Sanitize the given gcontact data
  *
  * @param array $gcontact array with gcontact data
+ * @throw Exception
  *
  * Generation:
  *  0: No definition
@@ -200,20 +206,20 @@ function poco_load_worker($cid, $uid, $zcid, $url) {
  *  4: ...
  *
  */
-function sanitized_gcontact(&$gcontact) {
+function sanitize_gcontact($gcontact) {
 
        if ($gcontact['url'] == "") {
-               return false;
+               throw new Exception('URL is empty');
        }
 
        $urlparts = parse_url($gcontact['url']);
        if (!isset($urlparts["scheme"])) {
-               return false;
+               throw new Exception("This (".$gcontact['url'].") doesn't seem to be an url.");
        }
 
        if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
                                                "identi.ca", "alpha.app.net"))) {
-               return false;
+               throw new Exception('Contact from a non federated network ignored. ('.$gcontact['url'].')');
        }
 
        // Don't store the statusnet connector as network
@@ -279,7 +285,7 @@ function sanitized_gcontact(&$gcontact) {
                $data = Probe::uri($gcontact['url']);
 
                if ($data["network"] == NETWORK_PHANTOM) {
-                       return false;
+                       throw new Exception('Probing for URL '.$gcontact['url'].' failed');
                }
 
                $orig_profile = $gcontact['url'];
@@ -299,11 +305,11 @@ function sanitized_gcontact(&$gcontact) {
        }
 
        if (!isset($gcontact['name']) OR !isset($gcontact['photo'])) {
-               return false;
+               throw new Exception('No name and photo for URL '.$gcontact['url']);
        }
 
        if (!in_array($gcontact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
-               return false;
+               throw new Exception('No federated network ('.$gcontact['network'].') detected for URL '.$gcontact['url']);
        }
 
        if (!isset($gcontact['server_url'])) {
@@ -321,7 +327,7 @@ function sanitized_gcontact(&$gcontact) {
                $gcontact['server_url'] = "";
        }
 
-       return true;
+       return $gcontact;
 }
 
 /**
@@ -606,10 +612,13 @@ function poco_last_updated($profile, $force = false) {
 
                $gcontact["server_url"] = $data["baseurl"];
 
-               if (sanitized_gcontact($gcontact)) {
+               try {
+                       $gcontact = sanitize_gcontact($gcontact);
                        update_gcontact($gcontact);
 
                        poco_last_updated($data["url"], $force);
+               } catch (Exception $e) {
+                       logger($e->getMessage(), LOGGER_DEBUG);
                }
 
                logger("Profile ".$profile." was deleted", LOGGER_DEBUG);
@@ -999,6 +1008,7 @@ function poco_check_server($server_url, $network = "", $force = false) {
        if (dbm::is_result($servers) AND ($orig_server_url == $server_url) AND
                ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
                logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
+               dba::p("UPDATE `gserver` SET `last_failure` = ? WHERE `nurl` = ?", datetime_convert(), normalise_link($server_url));
                return false;
        }
 
@@ -1013,6 +1023,7 @@ function poco_check_server($server_url, $network = "", $force = false) {
                // Quit if there is a timeout
                if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
                        logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
+                       dba::p("UPDATE `gserver` SET `last_failure` = ? WHERE `nurl` = ?", datetime_convert(), normalise_link($server_url));
                        return false;
                }
 
@@ -1022,12 +1033,10 @@ function poco_check_server($server_url, $network = "", $force = false) {
        if (!$serverret["success"] OR ($serverret["body"] == "") OR (sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
                // Workaround for bad configured servers (known nginx problem)
                if (!in_array($serverret["debug"]["http_code"], array("403", "404"))) {
-                       $last_failure = datetime_convert();
                        $failure = true;
                }
                $possible_failure = true;
-       } elseif ($network == NETWORK_DIASPORA)
-               $last_contact = datetime_convert();
+       }
 
        // If the server has no possible failure we reset the cached data
        if (!$possible_failure) {
@@ -1045,8 +1054,6 @@ function poco_check_server($server_url, $network = "", $force = false) {
                        $data = json_decode($serverret["body"]);
                        if (isset($data->totalResults)) {
                                $poco = $server_url."/poco";
-                               $last_contact = datetime_convert();
-
                                $server = poco_detect_poco_data($data);
                                if ($server) {
                                        $platform = $server['platform'];
@@ -1063,7 +1070,6 @@ function poco_check_server($server_url, $network = "", $force = false) {
                $serverret = z_fetch_url($server_url);
 
                if (!$serverret["success"] OR ($serverret["body"] == "")) {
-                       $last_failure = datetime_convert();
                        $failure = true;
                } else {
                        $server = poco_detect_server_type($serverret["body"]);
@@ -1072,7 +1078,6 @@ function poco_check_server($server_url, $network = "", $force = false) {
                                $network = $server['network'];
                                $version = $server['version'];
                                $site_name = $server['site_name'];
-                               $last_contact = datetime_convert();
                        }
 
                        $lines = explode("\n",$serverret["header"]);
@@ -1086,15 +1091,11 @@ function poco_check_server($server_url, $network = "", $force = false) {
                                                $network = NETWORK_DIASPORA;
                                                $versionparts = explode("-", $version);
                                                $version = $versionparts[0];
-                                               $last_contact = datetime_convert();
                                        }
 
                                        if(stristr($line,'Server: Mastodon')) {
                                                $platform = "Mastodon";
                                                $network = NETWORK_OSTATUS;
-                                               // Mastodon doesn't reveal version numbers
-                                               $version = "";
-                                               $last_contact = datetime_convert();
                                        }
                                }
                        }
@@ -1113,7 +1114,6 @@ function poco_check_server($server_url, $network = "", $force = false) {
                        $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
                        $version = trim($version, '"');
                        $network = NETWORK_OSTATUS;
-                       $last_contact = datetime_convert();
                }
 
                // Test for GNU Social
@@ -1125,7 +1125,19 @@ function poco_check_server($server_url, $network = "", $force = false) {
                        $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
                        $version = trim($version, '"');
                        $network = NETWORK_OSTATUS;
-                       $last_contact = datetime_convert();
+               }
+
+               // Test for Mastodon
+               $serverret = z_fetch_url($server_url."/api/v1/instance");
+               if ($serverret["success"] AND ($serverret["body"] != '')) {
+                       $data = json_decode($serverret["body"]);
+                       if (isset($data->version)) {
+                               $platform = "Mastodon";
+                               $version = $data->version;
+                               $site_name = $data->title;
+                               $info = $data->description;
+                               $network = NETWORK_OSTATUS;
+                       }
                }
        }
 
@@ -1135,8 +1147,6 @@ function poco_check_server($server_url, $network = "", $force = false) {
                if ($serverret["success"]) {
                        $data = json_decode($serverret["body"]);
                        if (isset($data->site->server)) {
-                               $last_contact = datetime_convert();
-
                                if (isset($data->site->platform)) {
                                        $platform = $data->site->platform->PLATFORM_NAME;
                                        $version = $data->site->platform->STD_VERSION;
@@ -1183,7 +1193,6 @@ function poco_check_server($server_url, $network = "", $force = false) {
                }
        }
 
-
        // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
        if (!$failure) {
                $serverret = z_fetch_url($server_url."/statistics.json");
@@ -1211,9 +1220,6 @@ function poco_check_server($server_url, $network = "", $force = false) {
                        } else {
                                $register_policy = REGISTER_CLOSED;
                        }
-
-                       if (isset($data->version))
-                               $last_contact = datetime_convert();
                }
        }
 
@@ -1238,8 +1244,6 @@ function poco_check_server($server_url, $network = "", $force = false) {
                        if (isset($server['site_name'])) {
                                $site_name = $server['site_name'];
                        }
-
-                       $last_contact = datetime_convert();
                }
        }
 
@@ -1255,7 +1259,6 @@ function poco_check_server($server_url, $network = "", $force = false) {
                        $data = json_decode($serverret["body"]);
 
                        if (isset($data->version)) {
-                               $last_contact = datetime_convert();
                                $network = NETWORK_DFRN;
 
                                $noscrape = $data->no_scrape_url;
@@ -1281,13 +1284,14 @@ function poco_check_server($server_url, $network = "", $force = false) {
        }
 
        if ($possible_failure AND !$failure) {
-               $last_failure = datetime_convert();
                $failure = true;
        }
 
        if ($failure) {
                $last_contact = $orig_last_contact;
+               $last_failure = datetime_convert();
        } else {
+               $last_contact = datetime_convert();
                $last_failure = $orig_last_failure;
        }
 
@@ -1649,6 +1653,20 @@ function poco_discover_federation() {
                }
        }
 
+       // Disvover Mastodon servers
+       if (!Config::get('system','ostatus_disabled')) {
+               $serverdata = fetch_url("https://instances.mastodon.xyz/instances.json");
+
+               if ($serverdata) {
+                       $servers = json_decode($serverdata);
+
+                       foreach ($servers AS $server) {
+                               $url = (is_null($server->https_score) ? 'http' : 'https').'://'.$server->name;
+                               proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", base64_encode($url));
+                       }
+               }
+       }
+
        // Currently disabled, since the service isn't available anymore.
        // It is not removed since I hope that there will be a successor.
        // Discover GNU Social Servers.
@@ -1888,8 +1906,11 @@ function poco_discover_server($data, $default_generation = 0) {
                                        "contact-type" => $contact_type,
                                        "generation" => $generation);
 
-                       if (sanitized_gcontact($gcontact)) {
+                       try {
+                               $gcontact = sanitize_gcontact($gcontact);
                                update_gcontact($gcontact);
+                       } catch (Exception $e) {
+                               logger($e->getMessage(), LOGGER_DEBUG);
                        }
 
                        logger("Done for profile ".$profile_url, LOGGER_DEBUG);
@@ -2086,7 +2107,7 @@ function update_gcontact($contact) {
        fix_alternate_contact_address($contact);
 
        if (!isset($contact["updated"]))
-               $contact["updated"] = datetime_convert();
+               $contact["updated"] = dbm::date();
 
        if ($contact["server_url"] == "") {
                $server_url = $contact["url"];
@@ -2141,7 +2162,7 @@ function update_gcontact($contact) {
                        dbesc($contact["gender"]), dbesc($contact["keywords"]), intval($contact["hide"]),
                        intval($contact["nsfw"]), intval($contact["contact-type"]), dbesc($contact["alias"]),
                        dbesc($contact["notify"]), dbesc($contact["url"]), dbesc($contact["location"]),
-                       dbesc($contact["about"]), intval($contact["generation"]), dbesc($contact["updated"]),
+                       dbesc($contact["about"]), intval($contact["generation"]), dbesc(dbm::date($contact["updated"])),
                        dbesc($contact["server_url"]), dbesc($contact["connect"]),
                        dbesc(normalise_link($contact["url"])), intval($contact["generation"]));