diff --git a/include/discover_poco.php b/include/discover_poco.php index c69e27fef1..504d972873 100644 --- a/include/discover_poco.php +++ b/include/discover_poco.php @@ -36,9 +36,18 @@ function discover_poco_run(&$argv, &$argc){ } } + if(($argc > 2) && ($argv[1] == "dirsearch")) { + $search = urldecode($argv[2]); + $searchmode = 1; + } elseif ($argc == 1) { + $search = ""; + $searchmode = 0; + } else + die("Unknown or missing parameter ".$argv[1]."\n"); + $lockpath = get_lockpath(); if ($lockpath != '') { - $pidfile = new pidfile($lockpath, 'discover_poco'); + $pidfile = new pidfile($lockpath, 'discover_poco'.urlencode($search)); if($pidfile->is_already_running()) { logger("discover_poco: Already running"); if ($pidfile->running_time() > 19*60) { @@ -55,16 +64,67 @@ function discover_poco_run(&$argv, &$argc){ load_hooks(); - logger('start'); + logger('start '.$search); - if (get_config('system','poco_discovery') > 0) + if (($search != "") and get_config('system','poco_local_search')) + discover_directory($search); + elseif (($search == "") and get_config('system','poco_discovery') > 0) poco_discover(); - logger('end'); + logger('end '.$search); return; } +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("http://dir.friendica.com/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` 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"]) + continue; + + $last_updated = poco_last_updated($jj->url); + $last_contact = datetime_convert(); + + if ($last_updated) { + logger("Mark profile ".$jj->url." as accessible (".$search.")", LOGGER_DEBUG); + q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'", + dbesc($last_updated), dbesc($last_contact), dbesc(normalise_link($jj->url))); + } else { + logger("Mark profile ".$jj->url." as unaccessible (".$search.")", LOGGER_DEBUG); + q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", + dbesc($last_contact), dbesc(normalise_link($jj->url))); + } + continue; + } + + 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()); +} + if (array_search(__file__,get_included_files())===0){ discover_poco_run($_SERVER["argv"],$_SERVER["argc"]); killme(); diff --git a/mod/admin.php b/mod/admin.php index a079bdabf8..ea0196db83 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -360,6 +360,7 @@ function admin_page_site_post(&$a){ $maxloadavg_frontend = ((x($_POST,'maxloadavg_frontend')) ? intval(trim($_POST['maxloadavg_frontend'])) : 50); $poco_completion = ((x($_POST,'poco_completion')) ? intval(trim($_POST['poco_completion'])) : false); $poco_discovery = ((x($_POST,'poco_discovery')) ? intval(trim($_POST['poco_discovery'])) : 0); + $poco_local_search = ((x($_POST,'poco_local_search')) ? intval(trim($_POST['poco_local_search'])) : false); $dfrn_only = ((x($_POST,'dfrn_only')) ? True : False); $ostatus_disabled = !((x($_POST,'ostatus_disabled')) ? True : False); $ostatus_poll_interval = ((x($_POST,'ostatus_poll_interval')) ? intval(trim($_POST['ostatus_poll_interval'])) : 0); @@ -431,6 +432,7 @@ function admin_page_site_post(&$a){ set_config('system','maxloadavg_frontend',$maxloadavg_frontend); set_config('system','poco_completion',$poco_completion); set_config('system','poco_discovery',$poco_discovery); + set_config('system','poco_local_search',$poco_local_search); set_config('config','sitename',$sitename); set_config('config','hostname',$hostname); set_config('config','sender_email', $sender_email); @@ -701,6 +703,7 @@ function admin_page_site(&$a) { '$poco_completion' => array('poco_completion', t("Completion of incoming contacts"), get_config('system','poco_completion'), t("Complete data of incomplete incoming contacts that are provided by the 'portable contacts' functionality. (Useful when communicating with Redmatrix and friendica servers before 3.3)")), '$poco_discovery' => array('poco_discovery', t("Discover contacts from other servers"), (string) intval(get_config('system','poco_discovery')), t("Periodically query other servers for contacts. You can choose between 'users': the users on the remote system, 'Global Contacts': active contacts that are known on the system. The fallback is meant for Redmatrix servers and older friendica servers, where global contacts weren't available."), $poco_discovery_choices), + '$poco_local_search' => array('poco_local_search', t("Search the local directory"), get_config('system','poco_local_search'), t("Search the local directory instead of the global directory. When searching locally, every search will be executed on the global directory in the background. This improves the search results when the search is repeated.")), '$use_fulltext_engine' => array('use_fulltext_engine', t("Use MySQL full text engine"), get_config('system','use_fulltext_engine'), t("Activates the full text engine. Speeds up search - but can only search for four and more characters.")), '$suppress_language' => array('suppress_language', t("Suppress Language"), get_config('system','suppress_language'), t("Suppress language information in meta information about a posting.")), diff --git a/mod/dirfind.php b/mod/dirfind.php index c49d37e48d..996d2a4641 100644 --- a/mod/dirfind.php +++ b/mod/dirfind.php @@ -16,7 +16,7 @@ function dirfind_init(&$a) { function dirfind_content(&$a) { - $local = true; + $local = get_config('system','poco_local_search'); $search = notags(trim($_REQUEST['search'])); @@ -43,14 +43,18 @@ function dirfind_content(&$a) { dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search))); - $results = q("SELECT `url`, `name`, `photo`, `keywords` FROM `gcontact`WHERE `network` IN ('%s', '%s', '%s') AND - (`url` REGEXP '%s' OR `name` REGEXP '%s' OR `location` REGEXP '%s' OR `about` REGEXP '%s' OR - `keywords` REGEXP '%s') ORDER BY `name` ASC LIMIT %d, %d", - dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), + $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`keywords` + FROM `gcontact` + LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d + WHERE `gcontact`.`network` IN ('%s', '%s', '%s') AND `gcontact`.`last_contact` >= `gcontact`.`last_failure` AND + (`gcontact`.`url` REGEXP '%s' OR `gcontact`.`name` REGEXP '%s' OR `gcontact`.`location` REGEXP '%s' OR + `gcontact`.`about` REGEXP '%s' OR `gcontact`.`keywords` REGEXP '%s') + GROUP BY `gcontact`.`nurl` + ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d", + intval(local_user()), dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)), intval($startrec), intval($perpage)); - $j = new stdClass(); $j->total = $count[0]["total"]; $j->items_page = $perpage; @@ -62,6 +66,7 @@ function dirfind_content(&$a) { } $objresult = new stdClass(); + $objresult->cid = $result["cid"]; $objresult->name = $result["name"]; $objresult->url = $result["url"]; $objresult->photo = $result["photo"]; @@ -69,6 +74,9 @@ function dirfind_content(&$a) { $j->results[] = $objresult; } + + // Add found profiles from the global directory to the local directory + proc_run('php','include/discover_poco.php', "dirsearch", urlencode($search)); } else { $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : ''); @@ -89,11 +97,22 @@ function dirfind_content(&$a) { $tpl = get_markup_template('match.tpl'); foreach($j->results as $jj) { + // If We already know this contact then don't show the "connect" button + if ($jj->cid > 0) { + $connlnk = ""; + $conntxt = ""; + } else { + $connlnk = $a->get_baseurl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url); + $conntxt = t('Connect'); + } + $o .= replace_macros($tpl,array( '$url' => zrl($jj->url), '$name' => $jj->name, '$photo' => proxy_url($jj->photo), - '$tags' => $jj->tags + '$tags' => $jj->tags, + '$conntxt' => $conntxt, + '$connlnk' => $connlnk, )); } } diff --git a/view/templates/admin_site.tpl b/view/templates/admin_site.tpl index f0461e1b87..2f448d1b91 100644 --- a/view/templates/admin_site.tpl +++ b/view/templates/admin_site.tpl @@ -118,6 +118,7 @@

{{$portable_contacts}}

{{include file="field_checkbox.tpl" field=$poco_completion}} {{include file="field_select.tpl" field=$poco_discovery}} + {{include file="field_checkbox.tpl" field=$poco_local_search}}

{{$performance}}