]> git.mxchange.org Git - friendica.git/blobdiff - include/socgraph.php
added more spaces #3254
[friendica.git] / include / socgraph.php
index 4dedd33d398f22a600c2b1112580204236c963d0..f43ad62d088c71a829bb0342ed18775145333cc7 100644 (file)
@@ -14,8 +14,13 @@ require_once("include/html2bbcode.php");
 require_once("include/Contact.php");
 require_once("include/Photo.php");
 
-/*
- * poco_load
+/**
+ * @brief Fetch POCO data
+ *
+ * @param integer $cid Contact ID
+ * @param integer $uid User ID
+ * @param integer $zcid Global Contact ID
+ * @param integer $url POCO address that should be polled
  *
  * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
  * and add the entries to the gcontact (Global Contact) table, or update existing entries
@@ -27,16 +32,25 @@ require_once("include/Photo.php");
  * pointing to the same global contact id.
  *
  */
+function poco_load($cid, $uid = 0, $zcid = 0, $url = null) {
+       // Call the function "poco_load_worker" via the worker
+       proc_run(PRIORITY_LOW, "include/discover_poco.php", "poco_load", $cid, $uid, $zcid, base64_encode($url));
+}
 
-
-
-
-function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
-
+/**
+ * @brief Fetch POCO data from the worker
+ *
+ * @param integer $cid Contact ID
+ * @param integer $uid User ID
+ * @param integer $zcid Global Contact ID
+ * @param integer $url POCO address that should be polled
+ *
+ */
+function poco_load_worker($cid, $uid, $zcid, $url) {
        $a = get_app();
 
-       if ($cid) {
-               if ((! $url) || (! $uid)) {
+       if($cid) {
+               if((! $url) || (! $uid)) {
                        $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
                                intval($cid)
                        );
@@ -45,11 +59,11 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
                                $uid = $r[0]['uid'];
                        }
                }
-               if (! $uid)
+               if(! $uid)
                        return;
        }
 
-       if (! $url)
+       if(! $url)
                return;
 
        $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation') ;
@@ -62,18 +76,18 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
 
        logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
 
-       if (($a->get_curl_code() > 299) || (! $s))
+       if(($a->get_curl_code() > 299) || (! $s))
                return;
 
        $j = json_decode($s);
 
        logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
 
-       if (! isset($j->entry))
+       if(! isset($j->entry))
                return;
 
        $total = 0;
-       foreach ($j->entry as $entry) {
+       foreach($j->entry as $entry) {
 
                $total ++;
                $profile_url = '';
@@ -137,7 +151,7 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
                }
 
                if (isset($entry->tags)) {
-                       foreach ($entry->tags as $tag) {
+                       foreach($entry->tags as $tag) {
                                $keywords = implode(", ", $tag);
                        }
                }
@@ -340,7 +354,7 @@ function poco_check($profile_url, $name, $network, $profile_photo, $about, $loca
 
        $gcid = update_gcontact($gcontact);
 
-       if (!$gcid)
+       if(!$gcid)
                return $gcid;
 
        $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
@@ -1077,10 +1091,10 @@ function poco_check_server($server_url, $network = "", $force = false) {
                        }
 
                        $lines = explode("\n",$serverret["header"]);
-                       if (count($lines)) {
-                               foreach ($lines as $line) {
+                       if(count($lines)) {
+                               foreach($lines as $line) {
                                        $line = trim($line);
-                                       if (stristr($line,'X-Diaspora-Version:')) {
+                                       if(stristr($line,'X-Diaspora-Version:')) {
                                                $platform = "Diaspora";
                                                $version = trim(str_replace("X-Diaspora-Version:", "", $line));
                                                $version = trim(str_replace("x-diaspora-version:", "", $version));
@@ -1090,7 +1104,7 @@ function poco_check_server($server_url, $network = "", $force = false) {
                                                $last_contact = datetime_convert();
                                        }
 
-                                       if (stristr($line,'Server: Mastodon')) {
+                                       if(stristr($line,'Server: Mastodon')) {
                                                $platform = "Mastodon";
                                                $network = NETWORK_OSTATUS;
                                                // Mastodon doesn't reveal version numbers
@@ -1369,7 +1383,7 @@ function count_common_friends($uid,$cid) {
 
 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
 
-       if ($shuffle)
+       if($shuffle)
                $sql_extra = " order by rand() ";
        else
                $sql_extra = " order by `gcontact`.`name` asc ";
@@ -1414,7 +1428,7 @@ function count_common_friends_zcid($uid,$zcid) {
 
 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
 
-       if ($shuffle)
+       if($shuffle)
                $sql_extra = " order by rand() ";
        else
                $sql_extra = " order by `gcontact`.`name` asc ";
@@ -1597,7 +1611,7 @@ function update_suggestions() {
        if (dbm::is_result($r)) {
                foreach ($r as $rr) {
                        $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
-                       if (! in_array($base,$done))
+                       if(! in_array($base,$done))
                                poco_load(0,0,0,$base);
                }
        }
@@ -1635,7 +1649,7 @@ function poco_discover_federation() {
 
        if ($last) {
                $next = $last + (24 * 60 * 60);
-               if ($next > time())
+               if($next > time())
                        return;
        }
 
@@ -1650,100 +1664,118 @@ function poco_discover_federation() {
                }
        }
 
-       /*
-        * 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/";
+       // 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());
 }
 
-function poco_discover($complete = false) {
+function poco_discover_single_server($id) {
+       $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
+       if (!dbm::is_result($r)) {
+               return false;
+       }
 
-       // Update the server list
-       poco_discover_federation();
+       $server = $r[0];
 
-       $no_of_queries = 5;
+       // Discover new servers out there (Works from Friendica version 3.5.2)
+       poco_fetch_serverlist($server["poco"]);
 
-       $requery_days = intval(get_config("system", "poco_requery_days"));
+       // Fetch all users from the other server
+       $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
 
-       if ($requery_days == 0)
-               $requery_days = 7;
+       logger("Fetch all users from the server ".$server["url"], LOGGER_DEBUG);
 
-       $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
+       $retdata = z_fetch_url($url);
+       if ($retdata["success"]) {
+               $data = json_decode($retdata["body"]);
 
-       $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update));
-       if ($r)
-               foreach ($r AS $server) {
+               poco_discover_server($data, 2);
 
-                       if (!poco_check_server($server["url"], $server["network"])) {
-                               // The server is not reachable? Okay, then we will try it later
-                               q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
-                               continue;
+               if (get_config('system','poco_discovery') > 1) {
+
+                       $timeframe = get_config('system','poco_discovery_since');
+                       if ($timeframe == 0) {
+                               $timeframe = 30;
                        }
 
-                       // Discover new servers out there
-                       poco_fetch_serverlist($server["poco"]);
+                       $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400);
 
-                       // Fetch all users from the other server
-                       $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
+                       // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
+                       $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
 
-                       logger("Fetch all users from the server ".$server["nurl"], LOGGER_DEBUG);
+                       $success = false;
 
                        $retdata = z_fetch_url($url);
                        if ($retdata["success"]) {
-                               $data = json_decode($retdata["body"]);
+                               logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
+                               $success = poco_discover_server(json_decode($retdata["body"]));
+                       }
 
-                               poco_discover_server($data, 2);
+                       if (!$success AND (get_config('system','poco_discovery') > 2)) {
+                               logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
+                               poco_discover_server_users($data, $server);
+                       }
+               }
 
-                               if (get_config('system','poco_discovery') > 1) {
+               q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
 
-                                       $timeframe = get_config('system','poco_discovery_since');
-                                       if ($timeframe == 0)
-                                               $timeframe = 30;
+               return true;
+       } else {
+               // If the server hadn't replied correctly, then force a sanity check
+               poco_check_server($server["url"], $server["network"], true);
 
-                                       $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400);
+               // If we couldn't reach the server, we will try it some time later
+               q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
 
-                                       // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
-                                       $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
+               return false;
+       }
+}
 
-                                       $success = false;
+function poco_discover($complete = false) {
 
-                                       $retdata = z_fetch_url($url);
-                                       if ($retdata["success"]) {
-                                               logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
-                                               $success = poco_discover_server(json_decode($retdata["body"]));
-                                       }
+       // Update the server list
+       poco_discover_federation();
 
-                                       if (!$success AND (get_config('system','poco_discovery') > 2)) {
-                                               logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
-                                               poco_discover_server_users($data, $server);
-                                       }
-                               }
+       $no_of_queries = 5;
 
-                               q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
-                               if (!$complete AND (--$no_of_queries == 0))
-                                       break;
-                       } else {
-                               // If the server hadn't replied correctly, then force a sanity check
-                               poco_check_server($server["url"], $server["network"], true);
+       $requery_days = intval(get_config("system", "poco_requery_days"));
+
+       if ($requery_days == 0) {
+               $requery_days = 7;
+       }
+       $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
 
-                               // If we couldn't reach the server, we will try it some time later
+       $r = q("SELECT `id`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update));
+       if (dbm::is_result($r)) {
+               foreach ($r AS $server) {
+
+                       if (!poco_check_server($server["url"], $server["network"])) {
+                               // The server is not reachable? Okay, then we will try it later
                                q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
+                               continue;
+                       }
+
+                       logger('Update directory from server '.$server['url'].' with ID '.$server['id'], LOGGER_DEBUG);
+                       proc_run(PRIORITY_LOW, "include/discover_poco.php", "update_server_directory", $server['id']);
+
+                       if (!$complete AND (--$no_of_queries == 0)) {
+                               break;
                        }
                }
+       }
 }
 
 function poco_discover_server_users($data, $server) {
@@ -1754,7 +1786,7 @@ function poco_discover_server_users($data, $server) {
        foreach ($data->entry AS $entry) {
                $username = "";
                if (isset($entry->urls)) {
-                       foreach ($entry->urls as $url)
+                       foreach($entry->urls as $url)
                                if ($url->type == 'profile') {
                                        $profile_url = $url->value;
                                        $urlparts = parse_url($profile_url);
@@ -1798,7 +1830,7 @@ function poco_discover_server($data, $default_generation = 0) {
                $name = $entry->displayName;
 
                if (isset($entry->urls)) {
-                       foreach ($entry->urls as $url) {
+                       foreach($entry->urls as $url) {
                                if ($url->type == 'profile') {
                                        $profile_url = $url->value;
                                        continue;
@@ -1823,31 +1855,31 @@ function poco_discover_server($data, $default_generation = 0) {
                        $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)) {
+               if(isset($entry->tags)) {
                        foreach ($entry->tags as $tag) {
                                $keywords = implode(", ", $tag);
                        }