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