]> git.mxchange.org Git - friendica.git/blob - src/Worker/DiscoverPoCo.php
Added UpdateServerDirectory
[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                 - suggestions: Discover other servers for their contacts.
31                 - server <poco url>: Searches for the poco server list. "poco url" is base64 encoded.
32                 - update_server: Frequently check the first 250 servers for vitality.
33                 - PortableContact::load: Load POCO data from a given POCO address
34                 */
35
36                 $search = "";
37                 $mode = 0;
38                 if (($command == "checkcontact") && Config::get('system', 'poco_completion')) {
39                         self::discoverUsers();
40                 } elseif ($command == "suggestions") {
41                         GContact::updateSuggestions();
42                 } elseif ($command == "server") {
43                         $server_url = $param1;
44                         if ($server_url == "") {
45                                 return;
46                         }
47                         $server_url = filter_var($server_url, FILTER_SANITIZE_URL);
48                         if (substr(Strings::normaliseLink($server_url), 0, 7) != "http://") {
49                                 return;
50                         }
51                         $result = "Checking server ".$server_url." - ";
52                         $ret = GServer::check($server_url);
53                         if ($ret) {
54                                 $result .= "success";
55                         } else {
56                                 $result .= "failed";
57                         }
58                         Logger::log($result, Logger::DEBUG);
59                 } elseif ($command == "update_server") {
60                         self::updateServer();
61                 } elseif ($command == "load") {
62                         if (!empty($param4)) {
63                                 $url = $param4;
64                         } else {
65                                 $url = '';
66                         }
67                         PortableContact::load(intval($param1), intval($param2), intval($param3), $url);
68                 } elseif ($command !== "") {
69                         Logger::log("Unknown or missing parameter ".$command."\n");
70                         return;
71                 }
72
73                 Logger::log('start '.$search);
74
75                 if (($mode == 0) && ($search == "") && (Config::get('system', 'poco_discovery') != PortableContact::DISABLED)) {
76                         // Query Friendica and Hubzilla servers for their users
77                         PortableContact::discover();
78
79                         // Query GNU Social servers for their users ("statistics" addon has to be enabled on the GS server)
80                         if (!Config::get('system', 'ostatus_disabled')) {
81                                 GContact::discoverGsUsers();
82                         }
83                 }
84
85                 Logger::log('end '.$search);
86
87                 return;
88         }
89
90         /**
91          * @brief Updates the first 250 servers
92          *
93          */
94         private static function updateServer() {
95                 $r = q("SELECT `url`, `created`, `last_failure`, `last_contact` FROM `gserver` ORDER BY rand()");
96
97                 if (!DBA::isResult($r)) {
98                         return;
99                 }
100
101                 $updated = 0;
102
103                 foreach ($r AS $server) {
104                         if (!PortableContact::updateNeeded($server["created"], "", $server["last_failure"], $server["last_contact"])) {
105                                 continue;
106                         }
107                         Logger::log('Update server status for server '.$server["url"], Logger::DEBUG);
108
109                         Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server["url"]);
110
111                         if (++$updated > 250) {
112                                 return;
113                         }
114                 }
115         }
116
117         private static function discoverUsers() {
118                 Logger::log("Discover users", Logger::DEBUG);
119
120                 $starttime = time();
121
122                 $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url`, `network` FROM `gcontact`
123                                 WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
124                                         `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
125                                         `network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()",
126                                 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA),
127                                 DBA::escape(Protocol::OSTATUS), DBA::escape(Protocol::FEED));
128
129                 if (!$users) {
130                         return;
131                 }
132                 $checked = 0;
133
134                 foreach ($users AS $user) {
135
136                         $urlparts = parse_url($user["url"]);
137                         if (!isset($urlparts["scheme"])) {
138                                 DBA::update('gcontact', ['network' => Protocol::PHANTOM],
139                                         ['nurl' => Strings::normaliseLink($user["url"])]);
140                                 continue;
141                          }
142
143                         if (in_array($urlparts["host"], ["twitter.com", "identi.ca"])) {
144                                 $networks = ["twitter.com" => Protocol::TWITTER, "identi.ca" => Protocol::PUMPIO];
145
146                                 DBA::update('gcontact', ['network' => $networks[$urlparts["host"]]],
147                                         ['nurl' => Strings::normaliseLink($user["url"])]);
148                                 continue;
149                         }
150
151                         $server_url = Contact::getBasepath($user["url"]);
152                         $force_update = false;
153
154                         if ($user["server_url"] != "") {
155
156                                 $force_update = (Strings::normaliseLink($user["server_url"]) != Strings::normaliseLink($server_url));
157
158                                 $server_url = $user["server_url"];
159                         }
160
161                         if ((($server_url == "") && ($user["network"] == Protocol::FEED)) || $force_update || GServer::check($server_url, $user["network"])) {
162                                 Logger::log('Check profile '.$user["url"]);
163                                 Worker::add(PRIORITY_LOW, 'UpdateGContact', $user['url'], 'force');
164
165                                 if (++$checked > 100) {
166                                         return;
167                                 }
168                         } else {
169                                 DBA::update('gcontact', ['last_failure' => DateTimeFormat::utcNow()],
170                                         ['nurl' => Strings::normaliseLink($user["url"])]);
171                         }
172
173                         // Quit the loop after 3 minutes
174                         if (time() > ($starttime + 180)) {
175                                 return;
176                         }
177                 }
178         }
179 }