]> git.mxchange.org Git - friendica.git/blob - include/discover_poco.php
7361721cff391f2195dd1bd752e67b3a6c9b265c
[friendica.git] / include / discover_poco.php
1 <?php
2
3 use Friendica\Core\Config;
4 use Friendica\Core\Worker;
5
6 require_once 'include/probe.php';
7 require_once 'include/socgraph.php';
8 require_once 'include/datetime.php';
9
10 function discover_poco_run(&$argv, &$argc) {
11
12         /*
13         This function can be called in these ways:
14         - dirsearch <search pattern>: Searches for "search pattern" in the directory. "search pattern" is url encoded.
15         - checkcontact: Updates gcontact entries
16         - suggestions: Discover other servers for their contacts.
17         - server <poco url>: Searches for the poco server list. "poco url" is base64 encoded.
18         - update_server: Frequently check the first 250 servers for vitality.
19         - update_server_directory: Discover the given server id for their contacts
20         - poco_load: Load POCO data from a given POCO address
21         - check_profile: Update remote profile data
22         */
23
24         if (($argc > 2) && ($argv[1] == "dirsearch")) {
25                 $search = urldecode($argv[2]);
26                 $mode = 1;
27         } elseif (($argc == 2) && ($argv[1] == "checkcontact")) {
28                 $mode = 2;
29         } elseif (($argc == 2) && ($argv[1] == "suggestions")) {
30                 $mode = 3;
31         } elseif (($argc == 3) && ($argv[1] == "server")) {
32                 $mode = 4;
33         } elseif (($argc == 2) && ($argv[1] == "update_server")) {
34                 $mode = 5;
35         } elseif (($argc == 3) && ($argv[1] == "update_server_directory")) {
36                 $mode = 6;
37         } elseif (($argc > 5) && ($argv[1] == "poco_load")) {
38                 $mode = 7;
39         } elseif (($argc == 3) && ($argv[1] == "check_profile")) {
40                 $mode = 8;
41         } elseif ($argc == 1) {
42                 $search = "";
43                 $mode = 0;
44         } else {
45                 die("Unknown or missing parameter ".$argv[1]."\n");
46         }
47
48         logger('start '.$search);
49
50         if ($mode == 8) {
51                 if ($argv[2] != "") {
52                         poco_last_updated($argv[2], true);
53                 }
54         } elseif ($mode == 7) {
55                 if ($argc == 6) {
56                         $url = $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 = $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                 Worker::add(PRIORITY_LOW, "discover_poco", "server", $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                         Worker::add(PRIORITY_LOW, "discover_poco", "check_profile", $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 }