]> git.mxchange.org Git - friendica.git/blobdiff - include/socgraph.php
Merge remote-tracking branch 'upstream/develop' into 1702-null-date
[friendica.git] / include / socgraph.php
index 3fe1227f313c46037a6b2b2b2d8e519c97c0b536..152f713c10a5441978f2d8935ad119da724c3478 100644 (file)
@@ -81,7 +81,7 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
                $connect_url = '';
                $name = '';
                $network = '';
-               $updated = '0000-00-00 00:00:00';
+               $updated = NULL_DATE;
                $location = '';
                $about = '';
                $keywords = '';
@@ -91,8 +91,8 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
 
                $name = $entry->displayName;
 
-               if(isset($entry->urls)) {
-                       foreach($entry->urls as $url) {
+               if (isset($entry->urls)) {
+                       foreach ($entry->urls as $url) {
                                if ($url->type == 'profile') {
                                        $profile_url = $url->value;
                                        continue;
@@ -104,7 +104,7 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
                        }
                }
                if (isset($entry->photos)) {
-                       foreach($entry->photos as $photo) {
+                       foreach ($entry->photos as $photo) {
                                if ($photo->type == 'profile') {
                                        $profile_photo = $photo->value;
                                        continue;
@@ -112,29 +112,37 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
                        }
                }
 
-               if(isset($entry->updated))
+               if (isset($entry->updated)) {
                        $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
+               }
 
-               if(isset($entry->network))
+               if (isset($entry->network)) {
                        $network = $entry->network;
+               }
 
-               if(isset($entry->currentLocation))
+               if (isset($entry->currentLocation)) {
                        $location = $entry->currentLocation;
+               }
 
-               if(isset($entry->aboutMe))
+               if (isset($entry->aboutMe)) {
                        $about = html2bbcode($entry->aboutMe);
+               }
 
-               if(isset($entry->gender))
+               if (isset($entry->gender)) {
                        $gender = $entry->gender;
+               }
 
-               if(isset($entry->generation) AND ($entry->generation > 0))
+               if (isset($entry->generation) AND ($entry->generation > 0)) {
                        $generation = ++$entry->generation;
+               }
 
-               if(isset($entry->tags))
-                       foreach($entry->tags as $tag)
+               if (isset($entry->tags)) {
+                       foreach($entry->tags as $tag) {
                                $keywords = implode(", ", $tag);
+                       }
+               }
 
-               if(isset($entry->contactType) AND ($entry->contactType >= 0))
+               if (isset($entry->contactType) AND ($entry->contactType >= 0))
                        $contact_type = $entry->contactType;
 
                // If you query a Friendica server for its profiles, the network has to be Friendica
@@ -171,8 +179,6 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
 
 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) {
 
-       $a = get_app();
-
        // Generation:
        //  0: No definition
        //  1: Profiles on this server
@@ -214,8 +220,9 @@ function poco_check($profile_url, $name, $network, $profile_photo, $about, $loca
        $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
                dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
        );
-       if (dbm::is_result($r))
+       if (dbm::is_result($r)) {
                $network = $r[0]["network"];
+       }
 
        if (($network == "") OR ($network == NETWORK_OSTATUS)) {
                $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
@@ -235,7 +242,7 @@ function poco_check($profile_url, $name, $network, $profile_photo, $about, $loca
                if (($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET))
                        $network = $x[0]["network"];
 
-               if ($updated == "0000-00-00 00:00:00")
+               if ($updated <= NULL_DATE)
                        $updated = $x[0]["updated"];
 
                $created = $x[0]["created"];
@@ -245,7 +252,7 @@ function poco_check($profile_url, $name, $network, $profile_photo, $about, $loca
                $alias =  $x[0]["alias"];
                $notify =  $x[0]["notify"];
        } else {
-               $created = "0000-00-00 00:00:00";
+               $created = NULL_DATE;
                $server_url = "";
 
                $urlparts = parse_url($profile_url);
@@ -413,7 +420,7 @@ 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")
+       if ($gcontacts[0]["created"] <= NULL_DATE)
                q("UPDATE `gcontact` SET `created` = '%s' WHERE `nurl` = '%s'",
                        dbesc(datetime_convert()), dbesc(normalise_link($profile)));
 
@@ -612,10 +619,10 @@ function poco_last_updated($profile, $force = false) {
        // Maybe there aren't any entries. Then check if it is a valid feed
        if ($last_updated == "")
                if ($xpath->query('/atom:feed')->length > 0)
-                       $last_updated = "0000-00-00 00:00:00";
+                       $last_updated = NULL_DATE;
 
        q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'",
-               dbesc($last_updated), dbesc(datetime_convert()), dbesc(normalise_link($profile)));
+               dbesc(dbm::date($last_updated)), dbesc(dbm::date()), dbesc(normalise_link($profile)));
 
        if (($gcontacts[0]["generation"] == 0))
                q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'",
@@ -683,9 +690,9 @@ function poco_check_server($server_url, $network = "", $force = false) {
                return false;
 
        $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
-       if ($servers) {
+       if (dbm::is_result($servers)) {
 
-               if ($servers[0]["created"] == "0000-00-00 00:00:00")
+               if ($servers[0]["created"] <= NULL_DATE)
                        q("UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'",
                                dbesc(datetime_convert()), dbesc(normalise_link($server_url)));
 
@@ -716,8 +723,8 @@ function poco_check_server($server_url, $network = "", $force = false) {
                $info = "";
                $register_policy = -1;
 
-               $last_contact = "0000-00-00 00:00:00";
-               $last_failure = "0000-00-00 00:00:00";
+               $last_contact = NULL_DATE;
+               $last_failure = NULL_DATE;
        }
        logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$servers[0]["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
 
@@ -725,21 +732,40 @@ function poco_check_server($server_url, $network = "", $force = false) {
        $orig_last_failure = $last_failure;
 
        // Check if the page is accessible via SSL.
+       $orig_server_url = $server_url;
        $server_url = str_replace("http://", "https://", $server_url);
-       $serverret = z_fetch_url($server_url."/.well-known/host-meta");
+
+       // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
+       $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
+
+       // Quit if there is a timeout.
+       // But we want to make sure to only quit if we are mostly sure that this server url fits.
+       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);
+               return false;
+       }
 
        // Maybe the page is unencrypted only?
        $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
        if (!$serverret["success"] OR ($serverret["body"] == "") OR (@sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
                $server_url = str_replace("https://", "http://", $server_url);
-               $serverret = z_fetch_url($server_url."/.well-known/host-meta");
+
+               // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
+               $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
+
+               // Quit if there is a timeout
+               if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
+                       logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
+                       return false;
+               }
 
                $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
        }
 
        if (!$serverret["success"] OR ($serverret["body"] == "") OR (sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
                // Workaround for bad configured servers (known nginx problem)
-               if ($serverret["debug"]["http_code"] != "403") {
+               if (!in_array($serverret["debug"]["http_code"], array("403", "404"))) {
                        $last_failure = datetime_convert();
                        $failure = true;
                }
@@ -1121,7 +1147,7 @@ function suggestion_query($uid, $start = 0, $limit = 80) {
                where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
                AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
                AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
-               AND `gcontact`.`updated` != '0000-00-00 00:00:00'
+               AND `gcontact`.`updated` >= '%s'
                AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
                AND `gcontact`.`network` IN (%s)
                GROUP BY `glink`.`gcid` ORDER BY `gcontact`.`updated` DESC,`total` DESC LIMIT %d, %d",
@@ -1129,6 +1155,7 @@ function suggestion_query($uid, $start = 0, $limit = 80) {
                intval($uid),
                intval($uid),
                intval($uid),
+               dbesc(NULL_DATE),
                $sql_network,
                intval($start),
                intval($limit)
@@ -1147,13 +1174,14 @@ function suggestion_query($uid, $start = 0, $limit = 80) {
                WHERE `glink`.`uid` = 0 AND `glink`.`cid` = 0 AND `glink`.`zcid` = 0 AND NOT `gcontact`.`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = %d)
                AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
                AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
-               AND `gcontact`.`updated` != '0000-00-00 00:00:00'
+               AND `gcontact`.`updated` >= '%s'
                AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
                AND `gcontact`.`network` IN (%s)
                ORDER BY rand() LIMIT %d, %d",
                intval($uid),
                intval($uid),
                intval($uid),
+               dbesc(NULL_DATE),
                $sql_network,
                intval($start),
                intval($limit)
@@ -1181,23 +1209,24 @@ function update_suggestions() {
 
        $done = array();
 
-       /// TODO Check if it is really neccessary to poll the own server
+       /// @TODO Check if it is really neccessary to poll the own server
        poco_load(0,0,0,App::get_baseurl() . '/poco');
 
        $done[] = App::get_baseurl() . '/poco';
 
-       if(strlen(get_config('system','directory'))) {
+       if (strlen(get_config('system','directory'))) {
                $x = fetch_url(get_server()."/pubsites");
-               if($x) {
+               if ($x) {
                        $j = json_decode($x);
-                       if($j->entries) {
-                               foreach($j->entries as $entry) {
+                       if ($j->entries) {
+                               foreach ($j->entries as $entry) {
 
                                        poco_check_server($entry->url);
 
                                        $url = $entry->url . '/poco';
-                                       if(! in_array($url,$done))
+                                       if (! in_array($url,$done)) {
                                                poco_load(0,0,0,$entry->url . '/poco');
+                                       }
                                }
                        }
                }
@@ -1209,7 +1238,7 @@ function update_suggestions() {
        );
 
        if (dbm::is_result($r)) {
-               foreach($r as $rr) {
+               foreach ($r as $rr) {
                        $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
                        if(! in_array($base,$done))
                                poco_load(0,0,0,$base);
@@ -1220,7 +1249,7 @@ function update_suggestions() {
 function poco_discover_federation() {
        $last = get_config('poco','last_federation_discovery');
 
-       if($last) {
+       if ($last) {
                $next = $last + (24 * 60 * 60);
                if($next > time())
                        return;
@@ -1236,18 +1265,20 @@ function poco_discover_federation() {
                        poco_check_server("https://".$server->host);
        }
 
-       // Discover GNU Social Servers
-       if (!get_config('system','ostatus_disabled')) {
-               $serverdata = "http://gstools.org/api/get_open_instances/";
+       // 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.
+       //if (!get_config('system','ostatus_disabled')) {
+       //      $serverdata = "http://gstools.org/api/get_open_instances/";
 
-               $result = z_fetch_url($serverdata);
-               if ($result["success"]) {
-                       $servers = json_decode($result["body"]);
+       //      $result = z_fetch_url($serverdata);
+       //      if ($result["success"]) {
+       //              $servers = json_decode($result["body"]);
 
-                       foreach($servers->data AS $server)
-                               poco_check_server($server->instance_address);
-               }
-       }
+       //              foreach($servers->data AS $server)
+       //                      poco_check_server($server->instance_address);
+       //      }
+       //}
 
        set_config('poco','last_federation_discovery', time());
 }
@@ -1366,7 +1397,7 @@ function poco_discover_server($data, $default_generation = 0) {
                $connect_url = '';
                $name = '';
                $network = '';
-               $updated = '0000-00-00 00:00:00';
+               $updated = NULL_DATE;
                $location = '';
                $about = '';
                $keywords = '';
@@ -1376,7 +1407,7 @@ function poco_discover_server($data, $default_generation = 0) {
 
                $name = $entry->displayName;
 
-               if(isset($entry->urls)) {
+               if (isset($entry->urls)) {
                        foreach($entry->urls as $url) {
                                if ($url->type == 'profile') {
                                        $profile_url = $url->value;
@@ -1389,39 +1420,48 @@ function poco_discover_server($data, $default_generation = 0) {
                        }
                }
 
-               if(isset($entry->photos)) {
-                       foreach($entry->photos as $photo) {
-                               if($photo->type == 'profile') {
+               if (isset($entry->photos)) {
+                       foreach ($entry->photos as $photo) {
+                               if ($photo->type == 'profile') {
                                        $profile_photo = $photo->value;
                                        continue;
                                }
                        }
                }
 
-               if(isset($entry->updated))
+               if (isset($entry->updated)) {
                        $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
+               }
 
-               if(isset($entry->network))
+               if(isset($entry->network)) {
                        $network = $entry->network;
+               }
 
-               if(isset($entry->currentLocation))
+               if(isset($entry->currentLocation)) {
                        $location = $entry->currentLocation;
+               }
 
-               if(isset($entry->aboutMe))
+               if(isset($entry->aboutMe)) {
                        $about = html2bbcode($entry->aboutMe);
+               }
 
-               if(isset($entry->gender))
+               if(isset($entry->gender)) {
                        $gender = $entry->gender;
+               }
 
-               if(isset($entry->generation) AND ($entry->generation > 0))
+               if(isset($entry->generation) AND ($entry->generation > 0)) {
                        $generation = ++$entry->generation;
+               }
 
-               if(isset($entry->contactType) AND ($entry->contactType >= 0))
+               if(isset($entry->contactType) AND ($entry->contactType >= 0)) {
                        $contact_type = $entry->contactType;
+               }
 
-               if(isset($entry->tags))
-                       foreach($entry->tags as $tag)
+               if(isset($entry->tags)) {
+                       foreach ($entry->tags as $tag) {
                                $keywords = implode(", ", $tag);
+                       }
+               }
 
                if ($generation > 0) {
                        $success = true;
@@ -1512,7 +1552,7 @@ function get_gcontact_id($contact) {
        if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
                $contact["url"] = clean_contact_url($contact["url"]);
 
-       $r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
+       $r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 2",
                dbesc(normalise_link($contact["url"])));
 
        if ($r) {
@@ -1575,6 +1615,11 @@ function get_gcontact_id($contact) {
  */
 function update_gcontact($contact) {
 
+       // Check for invalid "contact-type" value
+       if (isset($contact['contact-type']) AND (intval($contact['contact-type']) < 0)) {
+               $contact['contact-type'] = 0;
+       }
+
        /// @todo update contact table as well
 
        $gcontact_id = get_gcontact_id($contact);