]> git.mxchange.org Git - friendica.git/blob - include/discover_poco.php
Class file relocations
[friendica.git] / include / discover_poco.php
1 <?php
2
3 use Friendica\Core\Config;
4 use Friendica\Core\Worker;
5 use Friendica\Database\DBM;
6 use Friendica\Network\Probe;
7
8 require_once 'include/socgraph.php';
9 require_once 'include/datetime.php';
10
11 function discover_poco_run(&$argv, &$argc) {
12
13         /*
14         This function can be called in these ways:
15         - dirsearch <search pattern>: Searches for "search pattern" in the directory. "search pattern" is url encoded.
16         - checkcontact: Updates gcontact entries
17         - suggestions: Discover other servers for their contacts.
18         - server <poco url>: Searches for the poco server list. "poco url" is base64 encoded.
19         - update_server: Frequently check the first 250 servers for vitality.
20         - update_server_directory: Discover the given server id for their contacts
21         - poco_load: Load POCO data from a given POCO address
22         - check_profile: Update remote profile data
23         */
24
25         if (($argc > 2) && ($argv[1] == "dirsearch")) {
26                 $search = urldecode($argv[2]);
27                 $mode = 1;
28         } elseif (($argc == 2) && ($argv[1] == "checkcontact")) {
29                 $mode = 2;
30         } elseif (($argc == 2) && ($argv[1] == "suggestions")) {
31                 $mode = 3;
32         } elseif (($argc == 3) && ($argv[1] == "server")) {
33                 $mode = 4;
34         } elseif (($argc == 2) && ($argv[1] == "update_server")) {
35                 $mode = 5;
36         } elseif (($argc == 3) && ($argv[1] == "update_server_directory")) {
37                 $mode = 6;
38         } elseif (($argc > 5) && ($argv[1] == "poco_load")) {
39                 $mode = 7;
40         } elseif (($argc == 3) && ($argv[1] == "check_profile")) {
41                 $mode = 8;
42         } elseif ($argc == 1) {
43                 $search = "";
44                 $mode = 0;
45         } else {
46                 die("Unknown or missing parameter ".$argv[1]."\n");
47         }
48
49         logger('start '.$search);
50
51         if ($mode == 8) {
52                 if ($argv[2] != "") {
53                         poco_last_updated($argv[2], true);
54                 }
55         } elseif ($mode == 7) {
56                 if ($argc == 6) {
57                         $url = $argv[5];
58                 } else {
59                         $url = '';
60                 }
61                 poco_load_worker(intval($argv[2]), intval($argv[3]), intval($argv[4]), $url);
62         } elseif ($mode == 6) {
63                 poco_discover_single_server(intval($argv[2]));
64         } elseif ($mode == 5) {
65                 update_server();
66         } elseif ($mode == 4) {
67                 $server_url = $argv[2];
68                 if ($server_url == "") {
69                         return;
70                 }
71                 $server_url = filter_var($server_url, FILTER_SANITIZE_URL);
72                 if (substr(normalise_link($server_url), 0, 7) != "http://") {
73                         return;
74                 }
75                 $result = "Checking server ".$server_url." - ";
76                 $ret = poco_check_server($server_url);
77                 if ($ret) {
78                         $result .= "success";
79                 } else {
80                         $result .= "failed";
81                 }
82                 logger($result, LOGGER_DEBUG);
83         } elseif ($mode == 3) {
84                 update_suggestions();
85         } elseif (($mode == 2) && Config::get('system','poco_completion')) {
86                 discover_users();
87         } elseif (($mode == 1) && ($search != "") && Config::get('system','poco_local_search')) {
88                 discover_directory($search);
89                 gs_search_user($search);
90         } elseif (($mode == 0) && ($search == "") && (Config::get('system','poco_discovery') > 0)) {
91                 // Query Friendica and Hubzilla servers for their users
92                 poco_discover();
93
94                 // Query GNU Social servers for their users ("statistics" addon has to be enabled on the GS server)
95                 if (!Config::get('system','ostatus_disabled'))
96                         gs_discover();
97         }
98
99         logger('end '.$search);
100
101         return;
102 }
103
104 /**
105  * @brief Updates the first 250 servers
106  *
107  */
108 function update_server() {
109         $r = q("SELECT `url`, `created`, `last_failure`, `last_contact` FROM `gserver` ORDER BY rand()");
110
111         if (!DBM::is_result($r)) {
112                 return;
113         }
114
115         $updated = 0;
116
117         foreach ($r AS $server) {
118                 if (!poco_do_update($server["created"], "", $server["last_failure"], $server["last_contact"])) {
119                         continue;
120                 }
121                 logger('Update server status for server '.$server["url"], LOGGER_DEBUG);
122
123                 Worker::add(PRIORITY_LOW, "discover_poco", "server", $server["url"]);
124
125                 if (++$updated > 250) {
126                         return;
127                 }
128         }
129 }
130
131 function discover_users() {
132         logger("Discover users", LOGGER_DEBUG);
133
134         $starttime = time();
135
136         $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url`, `network` FROM `gcontact`
137                         WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
138                                 `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
139                                 `network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()",
140                         dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA),
141                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_FEED));
142
143         if (!$users) {
144                 return;
145         }
146         $checked = 0;
147
148         foreach ($users AS $user) {
149
150                 $urlparts = parse_url($user["url"]);
151                 if (!isset($urlparts["scheme"])) {
152                         q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
153                                 dbesc(NETWORK_PHANTOM), dbesc(normalise_link($user["url"])));
154                         continue;
155                  }
156
157                 if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
158                                                         "identi.ca", "alpha.app.net"))) {
159                         $networks = array("www.facebook.com" => NETWORK_FACEBOOK,
160                                         "facebook.com" => NETWORK_FACEBOOK,
161                                         "twitter.com" => NETWORK_TWITTER,
162                                         "identi.ca" => NETWORK_PUMPIO,
163                                         "alpha.app.net" => NETWORK_APPNET);
164
165                         q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
166                                 dbesc($networks[$urlparts["host"]]), dbesc(normalise_link($user["url"])));
167                         continue;
168                 }
169
170                 $server_url = poco_detect_server($user["url"]);
171                 $force_update = false;
172
173                 if ($user["server_url"] != "") {
174
175                         $force_update = (normalise_link($user["server_url"]) != normalise_link($server_url));
176
177                         $server_url = $user["server_url"];
178                 }
179
180                 if ((($server_url == "") && ($user["network"] == NETWORK_FEED)) || $force_update || poco_check_server($server_url, $user["network"])) {
181                         logger('Check profile '.$user["url"]);
182                         Worker::add(PRIORITY_LOW, "discover_poco", "check_profile", $user["url"]);
183
184                         if (++$checked > 100) {
185                                 return;
186                         }
187                 } else {
188                         q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
189                                 dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
190                 }
191
192                 // Quit the loop after 3 minutes
193                 if (time() > ($starttime + 180)) {
194                         return;
195                 }
196         }
197 }
198
199 function discover_directory($search) {
200
201         $data = Cache::get("dirsearch:".$search);
202         if (!is_null($data)) {
203                 // Only search for the same item every 24 hours
204                 if (time() < $data + (60 * 60 * 24)) {
205                         logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
206                         return;
207                 }
208         }
209
210         $x = fetch_url(get_server()."/lsearch?p=1&n=500&search=".urlencode($search));
211         $j = json_decode($x);
212
213         if (count($j->results)) {
214                 foreach ($j->results as $jj) {
215                         // Check if the contact already exists
216                         $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
217                         if (DBM::is_result($exists)) {
218                                 logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
219
220                                 if (($exists[0]["last_contact"] < $exists[0]["last_failure"]) &&
221                                         ($exists[0]["updated"] < $exists[0]["last_failure"])) {
222                                         continue;
223                                 }
224                                 // Update the contact
225                                 poco_last_updated($jj->url);
226                                 continue;
227                         }
228
229                         $server_url = poco_detect_server($jj->url);
230                         if ($server_url != '') {
231                                 if (!poco_check_server($server_url)) {
232                                         logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG);
233                                         continue;
234                                 }
235                                 logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
236                         }
237
238                         $data = Probe::uri($jj->url);
239                         if ($data["network"] == NETWORK_DFRN) {
240                                 logger("Profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
241                                 logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
242
243                                 if ($jj->tags != "") {
244                                         $data["keywords"] = $jj->tags;
245                                 }
246
247                                 $data["server_url"] = $data["baseurl"];
248
249                                 update_gcontact($data);
250                         } else {
251                                 logger("Profile ".$jj->url." is not responding or no Friendica contact - but network ".$data["network"], LOGGER_DEBUG);
252                         }
253                 }
254         }
255         Cache::set("dirsearch:".$search, time(), CACHE_DAY);
256 }
257
258 /**
259  * @brief Search for GNU Social user with gstools.org
260  *
261  * @param str $search User name
262  */
263 function gs_search_user($search) {
264
265         // Currently disabled, since the service isn't available anymore.
266         // It is not removed since I hope that there will be a successor.
267         return false;
268
269         $a = get_app();
270
271         $url = "http://gstools.org/api/users_search/".urlencode($search);
272
273         $result = z_fetch_url($url);
274         if (!$result["success"]) {
275                 return false;
276         }
277
278         $contacts = json_decode($result["body"]);
279
280         if ($contacts->status == 'ERROR') {
281                 return false;
282         }
283
284         /// @TODO AS is considered as a notation for constants (as they usually being written all upper-case)
285         /// @TODO find all those and convert to all lower-case which is a keyword then
286         foreach ($contacts->data AS $user) {
287                 $contact = Probe::uri($user->site_address."/".$user->name);
288                 if ($contact["network"] != NETWORK_PHANTOM) {
289                         $contact["about"] = $user->description;
290                         update_gcontact($contact);
291                 }
292         }
293 }