3 * @file src/Worker/DiscoverPoCo.php
5 namespace Friendica\Worker;
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;
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 = '')
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
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 == "") {
47 $server_url = filter_var($server_url, FILTER_SANITIZE_URL);
48 if (substr(Strings::normaliseLink($server_url), 0, 7) != "http://") {
51 $result = "Checking server ".$server_url." - ";
52 $ret = GServer::check($server_url);
58 Logger::log($result, Logger::DEBUG);
59 } elseif ($command == "update_server") {
61 } elseif ($command == "load") {
62 if (!empty($param4)) {
67 PortableContact::load(intval($param1), intval($param2), intval($param3), $url);
68 } elseif ($command !== "") {
69 Logger::log("Unknown or missing parameter ".$command."\n");
73 Logger::log('start '.$search);
75 if (($mode == 0) && ($search == "") && (Config::get('system', 'poco_discovery') != PortableContact::DISABLED)) {
76 // Query Friendica and Hubzilla servers for their users
77 PortableContact::discover();
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();
85 Logger::log('end '.$search);
91 * @brief Updates the first 250 servers
94 private static function updateServer() {
95 $r = q("SELECT `url`, `created`, `last_failure`, `last_contact` FROM `gserver` ORDER BY rand()");
97 if (!DBA::isResult($r)) {
103 foreach ($r AS $server) {
104 if (!PortableContact::updateNeeded($server["created"], "", $server["last_failure"], $server["last_contact"])) {
107 Logger::log('Update server status for server '.$server["url"], Logger::DEBUG);
109 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server["url"]);
111 if (++$updated > 250) {
117 private static function discoverUsers() {
118 Logger::log("Discover users", Logger::DEBUG);
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));
134 foreach ($users AS $user) {
136 $urlparts = parse_url($user["url"]);
137 if (!isset($urlparts["scheme"])) {
138 DBA::update('gcontact', ['network' => Protocol::PHANTOM],
139 ['nurl' => Strings::normaliseLink($user["url"])]);
143 if (in_array($urlparts["host"], ["twitter.com", "identi.ca"])) {
144 $networks = ["twitter.com" => Protocol::TWITTER, "identi.ca" => Protocol::PUMPIO];
146 DBA::update('gcontact', ['network' => $networks[$urlparts["host"]]],
147 ['nurl' => Strings::normaliseLink($user["url"])]);
151 $server_url = Contact::getBasepath($user["url"]);
152 $force_update = false;
154 if ($user["server_url"] != "") {
156 $force_update = (Strings::normaliseLink($user["server_url"]) != Strings::normaliseLink($server_url));
158 $server_url = $user["server_url"];
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');
165 if (++$checked > 100) {
169 DBA::update('gcontact', ['last_failure' => DateTimeFormat::utcNow()],
170 ['nurl' => Strings::normaliseLink($user["url"])]);
173 // Quit the loop after 3 minutes
174 if (time() > ($starttime + 180)) {