3 use Friendica\Core\Config;
5 require_once 'include/probe.php';
6 require_once 'include/socgraph.php';
7 require_once 'include/datetime.php';
9 function discover_poco_run(&$argv, &$argc) {
12 This function can be called in these ways:
13 - dirsearch <search pattern>: Searches for "search pattern" in the directory. "search pattern" is url encoded.
14 - checkcontact: Updates gcontact entries
15 - suggestions: Discover other servers for their contacts.
16 - server <poco url>: Searches for the poco server list. "poco url" is base64 encoded.
17 - update_server: Frequently check the first 250 servers for vitality.
18 - update_server_directory: Discover the given server id for their contacts
19 - poco_load: Load POCO data from a given POCO address
20 - check_profile: Update remote profile data
23 if (($argc > 2) && ($argv[1] == "dirsearch")) {
24 $search = urldecode($argv[2]);
26 } elseif (($argc == 2) && ($argv[1] == "checkcontact")) {
28 } elseif (($argc == 2) && ($argv[1] == "suggestions")) {
30 } elseif (($argc == 3) && ($argv[1] == "server")) {
32 } elseif (($argc == 2) && ($argv[1] == "update_server")) {
34 } elseif (($argc == 3) && ($argv[1] == "update_server_directory")) {
36 } elseif (($argc > 5) && ($argv[1] == "poco_load")) {
38 } elseif (($argc == 3) && ($argv[1] == "check_profile")) {
40 } elseif ($argc == 1) {
44 die("Unknown or missing parameter ".$argv[1]."\n");
47 logger('start '.$search);
50 $profile_url = base64_decode($argv[2]);
51 if ($profile_url != "") {
52 poco_last_updated($profile_url, true);
54 } elseif ($mode == 7) {
56 $url = base64_decode($argv[5]);
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) {
65 } elseif ($mode == 4) {
66 $server_url = base64_decode($argv[2]);
67 if ($server_url == "") {
70 $server_url = filter_var($server_url, FILTER_SANITIZE_URL);
71 if (substr(normalise_link($server_url), 0, 7) != "http://") {
74 $result = "Checking server ".$server_url." - ";
75 $ret = poco_check_server($server_url);
81 logger($result, LOGGER_DEBUG);
82 } elseif ($mode == 3) {
84 } elseif (($mode == 2) && get_config('system','poco_completion')) {
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
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'))
98 logger('end '.$search);
104 * @brief Updates the first 250 servers
107 function update_server() {
108 $r = q("SELECT `url`, `created`, `last_failure`, `last_contact` FROM `gserver` ORDER BY rand()");
110 if (!dbm::is_result($r)) {
116 foreach ($r AS $server) {
117 if (!poco_do_update($server["created"], "", $server["last_failure"], $server["last_contact"])) {
120 logger('Update server status for server '.$server["url"], LOGGER_DEBUG);
122 proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", base64_encode($server["url"]));
124 if (++$updated > 250) {
130 function discover_users() {
131 logger("Discover users", LOGGER_DEBUG);
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));
147 foreach ($users AS $user) {
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"])));
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);
164 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
165 dbesc($networks[$urlparts["host"]]), dbesc(normalise_link($user["url"])));
169 $server_url = poco_detect_server($user["url"]);
170 $force_update = false;
172 if ($user["server_url"] != "") {
174 $force_update = (normalise_link($user["server_url"]) != normalise_link($server_url));
176 $server_url = $user["server_url"];
179 if ((($server_url == "") && ($user["network"] == NETWORK_FEED)) || $force_update || poco_check_server($server_url, $user["network"])) {
180 logger('Check profile '.$user["url"]);
181 proc_run(PRIORITY_LOW, "include/discover_poco.php", "check_profile", base64_encode($user["url"]));
183 if (++$checked > 100) {
187 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
188 dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
191 // Quit the loop after 3 minutes
192 if (time() > ($starttime + 180)) {
198 function discover_directory($search) {
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);
209 $x = fetch_url(get_server()."/lsearch?p=1&n=500&search=".urlencode($search));
210 $j = json_decode($x);
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);
219 if (($exists[0]["last_contact"] < $exists[0]["last_failure"]) &&
220 ($exists[0]["updated"] < $exists[0]["last_failure"])) {
223 // Update the contact
224 poco_last_updated($jj->url);
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);
234 logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
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);
242 if ($jj->tags != "") {
243 $data["keywords"] = $jj->tags;
246 $data["server_url"] = $data["baseurl"];
248 update_gcontact($data);
250 logger("Profile ".$jj->url." is not responding or no Friendica contact - but network ".$data["network"], LOGGER_DEBUG);
254 Cache::set("dirsearch:".$search, time(), CACHE_DAY);
258 * @brief Search for GNU Social user with gstools.org
260 * @param str $search User name
262 function gs_search_user($search) {
264 // Currently disabled, since the service isn't available anymore.
265 // It is not removed since I hope that there will be a successor.
270 $url = "http://gstools.org/api/users_search/".urlencode($search);
272 $result = z_fetch_url($url);
273 if (!$result["success"]) {
277 $contacts = json_decode($result["body"]);
279 if ($contacts->status == 'ERROR') {
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);