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