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