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