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 - update_server_directory: Discover the given server id for their contacts
34 - PortableContact::load: Load POCO data from a given POCO address
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 == "") {
48 $server_url = filter_var($server_url, FILTER_SANITIZE_URL);
49 if (substr(Strings::normaliseLink($server_url), 0, 7) != "http://") {
52 $result = "Checking server ".$server_url." - ";
53 $ret = GServer::check($server_url);
59 Logger::log($result, Logger::DEBUG);
60 } elseif ($command == "update_server") {
62 } elseif ($command == "update_server_directory") {
63 PortableContact::discoverSingleServer(intval($param1));
64 } elseif ($command == "load") {
65 if (!empty($param4)) {
70 PortableContact::load(intval($param1), intval($param2), intval($param3), $url);
71 } elseif ($command !== "") {
72 Logger::log("Unknown or missing parameter ".$command."\n");
76 Logger::log('start '.$search);
78 if (($mode == 0) && ($search == "") && (Config::get('system', 'poco_discovery') != PortableContact::DISABLED)) {
79 // Query Friendica and Hubzilla servers for their users
80 PortableContact::discover();
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();
88 Logger::log('end '.$search);
94 * @brief Updates the first 250 servers
97 private static function updateServer() {
98 $r = q("SELECT `url`, `created`, `last_failure`, `last_contact` FROM `gserver` ORDER BY rand()");
100 if (!DBA::isResult($r)) {
106 foreach ($r AS $server) {
107 if (!PortableContact::updateNeeded($server["created"], "", $server["last_failure"], $server["last_contact"])) {
110 Logger::log('Update server status for server '.$server["url"], Logger::DEBUG);
112 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server["url"]);
114 if (++$updated > 250) {
120 private static function discoverUsers() {
121 Logger::log("Discover users", Logger::DEBUG);
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));
137 foreach ($users AS $user) {
139 $urlparts = parse_url($user["url"]);
140 if (!isset($urlparts["scheme"])) {
141 DBA::update('gcontact', ['network' => Protocol::PHANTOM],
142 ['nurl' => Strings::normaliseLink($user["url"])]);
146 if (in_array($urlparts["host"], ["twitter.com", "identi.ca"])) {
147 $networks = ["twitter.com" => Protocol::TWITTER, "identi.ca" => Protocol::PUMPIO];
149 DBA::update('gcontact', ['network' => $networks[$urlparts["host"]]],
150 ['nurl' => Strings::normaliseLink($user["url"])]);
154 $server_url = Contact::getBasepath($user["url"]);
155 $force_update = false;
157 if ($user["server_url"] != "") {
159 $force_update = (Strings::normaliseLink($user["server_url"]) != Strings::normaliseLink($server_url));
161 $server_url = $user["server_url"];
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');
168 if (++$checked > 100) {
172 DBA::update('gcontact', ['last_failure' => DateTimeFormat::utcNow()],
173 ['nurl' => Strings::normaliseLink($user["url"])]);
176 // Quit the loop after 3 minutes
177 if (time() > ($starttime + 180)) {