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