function discover_directory($search) { $data = Cache::get("dirsearch:" . $search); if (!is_null($data)) { // Only search for the same item every 24 hours if (time() < $data + 60 * 60 * 24) { logger("Already searched for " . $search . " in the last 24 hours", LOGGER_DEBUG); return; } } $x = fetch_url(get_server() . "/lsearch?p=1&n=500&search=" . urlencode($search)); $j = json_decode($x); if (count($j->results)) { foreach ($j->results as $jj) { // Check if the contact already exists $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url)); if ($exists) { logger("Profile " . $jj->url . " already exists (" . $search . ")", LOGGER_DEBUG); if ($exists[0]["last_contact"] < $exists[0]["last_failure"] and $exists[0]["updated"] < $exists[0]["last_failure"]) { continue; } // Update the contact poco_last_updated($jj->url); continue; } // Harcoded paths aren't so good. But in this case it is okay. // First: We only will get Friendica contacts (which always are using this url schema) // Second: There will be no further problems if we are doing a mistake $server_url = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "\$1\$2", $jj->url); if ($server_url != $jj->url) { if (!poco_check_server($server_url)) { logger("Friendica server " . $server_url . " doesn't answer.", LOGGER_DEBUG); continue; } } logger("Friendica server " . $server_url . " seems to be okay.", LOGGER_DEBUG); logger("Check if profile " . $jj->url . " is reachable (" . $search . ")", LOGGER_DEBUG); $data = probe_url($jj->url); if ($data["network"] == NETWORK_DFRN) { logger("Add profile " . $jj->url . " to local directory (" . $search . ")", LOGGER_DEBUG); poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0); } } } Cache::set("dirsearch:" . $search, time(), CACHE_DAY); }
function poco_discover($complete = false) { // Update the server list poco_discover_federation(); $no_of_queries = 5; $requery_days = intval(get_config("system", "poco_requery_days")); if ($requery_days == 0) { $requery_days = 7; } $last_update = date("c", time() - 60 * 60 * 24 * $requery_days); $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update)); if ($r) { foreach ($r as $server) { if (!poco_check_server($server["url"], $server["network"])) { // The server is not reachable? Okay, then we will try it later q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); continue; } // Fetch all users from the other server $url = $server["poco"] . "/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,generation"; logger("Fetch all users from the server " . $server["nurl"], LOGGER_DEBUG); $retdata = z_fetch_url($url); if ($retdata["success"]) { $data = json_decode($retdata["body"]); poco_discover_server($data, 2); if (get_config('system', 'poco_discovery') > 1) { $timeframe = get_config('system', 'poco_discovery_since'); if ($timeframe == 0) { $timeframe = 30; } $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400); // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3) $url = $server["poco"] . "/@global?updatedSince=" . $updatedSince . "&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,generation"; $success = false; $retdata = z_fetch_url($url); if ($retdata["success"]) { logger("Fetch all global contacts from the server " . $server["nurl"], LOGGER_DEBUG); $success = poco_discover_server(json_decode($retdata["body"])); } if (!$success and get_config('system', 'poco_discovery') > 2) { logger("Fetch contacts from users of the server " . $server["nurl"], LOGGER_DEBUG); poco_discover_server_users($data, $server); } } q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); if (!$complete and --$no_of_queries == 0) { break; } } else { // If the server hadn't replied correctly, then force a sanity check poco_check_server($server["url"], $server["network"], true); // If we couldn't reach the server, we will try it some time later q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); } } } }
function update_gcontact_run(&$argv, &$argc) { global $a, $db; if (is_null($a)) { $a = new App(); } if (is_null($db)) { @(include ".htconfig.php"); require_once "include/dba.php"; $db = new dba($db_host, $db_user, $db_pass, $db_data); unset($db_host, $db_user, $db_pass, $db_data); } require_once 'include/pidfile.php'; require_once 'include/Scrape.php'; require_once "include/socgraph.php"; load_config('config'); load_config('system'); $a->set_baseurl(get_config('system', 'url')); load_hooks(); logger('update_gcontact: start'); if ($argc > 1 && intval($argv[1])) { $contact_id = intval($argv[1]); } if (!$contact_id) { logger('update_gcontact: no contact'); return; } $lockpath = get_lockpath(); if ($lockpath != '') { $pidfile = new pidfile($lockpath, 'update_gcontact' . $contact_id); if ($pidfile->is_already_running()) { logger("update_gcontact: Already running for contact " . $contact_id); if ($pidfile->running_time() > 9 * 60) { $pidfile->kill(); logger("killed stale process"); } exit; } } $r = q("SELECT * FROM `gcontact` WHERE `id` = %d", intval($contact_id)); if (!$r) { return; } if (!in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) { return; } $data = probe_url($r[0]["url"]); if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) { if ($r[0]["server_url"] != "") { poco_check_server($r[0]["server_url"], $r[0]["network"]); } q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact_id)); return; } if ($data["name"] == "" and $r[0]['name'] != "") { $data["name"] = $r[0]['name']; } if ($data["nick"] == "" and $r[0]['nick'] != "") { $data["nick"] = $r[0]['nick']; } if ($data["addr"] == "" and $r[0]['addr'] != "") { $data["addr"] = $r[0]['addr']; } if ($data["photo"] == "" and $r[0]['photo'] != "") { $data["photo"] = $r[0]['photo']; } q("UPDATE `gcontact` SET `name` = '%s', `nick` = '%s', `addr` = '%s', `photo` = '%s'\n\t\t\t\tWHERE `id` = %d", dbesc($data["name"]), dbesc($data["nick"]), dbesc($data["addr"]), dbesc($data["photo"]), intval($contact_id)); q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `addr` = '%s', `photo` = '%s'\n\t\t\t\tWHERE `uid` = 0 AND `addr` = '' AND `nurl` = '%s'", dbesc($data["name"]), dbesc($data["nick"]), dbesc($data["addr"]), dbesc($data["photo"]), dbesc(normalise_link($data["url"]))); q("UPDATE `contact` SET `addr` = '%s'\n\t\t\t\tWHERE `uid` != 0 AND `addr` = '' AND `nurl` = '%s'", dbesc($data["addr"]), dbesc(normalise_link($data["url"]))); }