]> git.mxchange.org Git - friendica.git/blob - src/Worker/DiscoverPoCo.php
Added "DiscoverContacts"
[friendica.git] / src / Worker / DiscoverPoCo.php
1 <?php
2 /**
3  * @file src/Worker/DiscoverPoCo.php
4  */
5 namespace Friendica\Worker;
6
7 use Friendica\Core\Cache;
8 use Friendica\Core\Config;
9 use Friendica\Core\Logger;
10 use Friendica\Core\Protocol;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBA;
13 use Friendica\Model\GContact;
14 use Friendica\Model\Contact;
15 use Friendica\Model\GServer;
16 use Friendica\Network\Probe;
17 use Friendica\Protocol\PortableContact;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Network;
20 use Friendica\Util\Strings;
21
22 class DiscoverPoCo
23 {
24         /// @todo Clean up this mess of a parameter hell and split it in several classes
25         public static function execute($command = '', $param1 = '', $param2 = '', $param3 = '', $param4 = '')
26         {
27                 /*
28                 This function can be called in these ways:
29                 - checkcontact: Updates gcontact entries
30                 - server <poco url>: Searches for the poco server list. "poco url" is base64 encoded.
31                 - update_server: Frequently check the first 250 servers for vitality.
32                 - PortableContact::load: Load POCO data from a given POCO address
33                 */
34
35                 $search = "";
36                 $mode = 0;
37                 if ($command == "server") {
38                         $server_url = $param1;
39                         if ($server_url == "") {
40                                 return;
41                         }
42                         $server_url = filter_var($server_url, FILTER_SANITIZE_URL);
43                         if (substr(Strings::normaliseLink($server_url), 0, 7) != "http://") {
44                                 return;
45                         }
46                         $result = "Checking server ".$server_url." - ";
47                         $ret = GServer::check($server_url);
48                         if ($ret) {
49                                 $result .= "success";
50                         } else {
51                                 $result .= "failed";
52                         }
53                         Logger::log($result, Logger::DEBUG);
54                 } elseif ($command == "update_server") {
55                         self::updateServer();
56                 } elseif ($command == "load") {
57                         if (!empty($param4)) {
58                                 $url = $param4;
59                         } else {
60                                 $url = '';
61                         }
62                         PortableContact::load(intval($param1), intval($param2), intval($param3), $url);
63                 } elseif ($command !== "") {
64                         Logger::log("Unknown or missing parameter ".$command."\n");
65                         return;
66                 }
67
68                 Logger::log('start '.$search);
69
70                 if (($mode == 0) && ($search == "") && (Config::get('system', 'poco_discovery') != PortableContact::DISABLED)) {
71                         // Query Friendica and Hubzilla servers for their users
72                         PortableContact::discover();
73
74                         // Query GNU Social servers for their users ("statistics" addon has to be enabled on the GS server)
75                         if (!Config::get('system', 'ostatus_disabled')) {
76                                 GContact::discoverGsUsers();
77                         }
78                 }
79
80                 Logger::log('end '.$search);
81
82                 return;
83         }
84
85         /**
86          * @brief Updates the first 250 servers
87          *
88          */
89         private static function updateServer() {
90                 $r = q("SELECT `url`, `created`, `last_failure`, `last_contact` FROM `gserver` ORDER BY rand()");
91
92                 if (!DBA::isResult($r)) {
93                         return;
94                 }
95
96                 $updated = 0;
97
98                 foreach ($r AS $server) {
99                         if (!PortableContact::updateNeeded($server["created"], "", $server["last_failure"], $server["last_contact"])) {
100                                 continue;
101                         }
102                         Logger::log('Update server status for server '.$server["url"], Logger::DEBUG);
103
104                         Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server["url"]);
105
106                         if (++$updated > 250) {
107                                 return;
108                         }
109                 }
110         }
111 }