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