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