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