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