<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Functions used by view_table.cgi and siblings, and shared between the
# MySQL and PostgreSQL modules

# get_search_args(&amp;in)
# Returns the search SQL, search URL arguments and search hidden fields
sub get_search_args
{
local %in = %{$_[0]};
local ($search, $searchhids, $searchargs, @adv);
if ($in{'field'}) {
	# A simple search
	$search = "where ".&amp;quotestr($in{'field'})." ".
		   &amp;make_like($in{'match'}, $in{'for'});
	$searchargs = "&amp;field=".&amp;urlize($in{'field'}).
		      "&amp;for=".&amp;urlize($in{'for'}).
		      "&amp;match=".&amp;urlize($in{'match'});
	$searchhids = &amp;ui_hidden("field", $in{'field'})."\n".
		      &amp;ui_hidden("for", $in{'for'})."\n".
		      &amp;ui_hidden("match", $in{'match'})."\n";
	}
elsif ($in{'advanced'}) {
	# An advanced search
	for(my $i=0; defined($in{"field_$i"}); $i++) {
		if ($in{"field_$i"}) {
			push(@adv, &amp;quotestr($in{"field_$i"})." ".
				   &amp;make_like($in{"match_$i"}, $in{"for_$i"}));
			$searchargs .= "&amp;field_$i=".&amp;urlize($in{"field_$i"}).
				       "&amp;for_$i=".&amp;urlize($in{"for_$i"}).
				       "&amp;match_$i=".&amp;urlize($in{"match_$i"});
			$searchhids .=
				&amp;ui_hidden("field_$i", $in{"field_$i"})."\n".
				&amp;ui_hidden("for_$i", $in{"for_$i"})."\n".
				&amp;ui_hidden("match_$i", $in{"match_$i"})."\n";
			}
		}
	if (@adv) {
		$search = "where (".join($in{'and'} ? " and " : " or ",
					@adv).")";
		$searchhids .= &amp;ui_hidden("and", $in{'and'})."\n".
			       &amp;ui_hidden("advanced", 1)."\n";
		$searchargs .= "&amp;and=".$in{'and'}.
			       "&amp;advanced=1";
		}
	}
return ($search, $searchhids, $searchargs, scalar(@adv));
}

# get_search_limit(&amp;in)
# Build limiting expression
sub get_search_limit
{
local %in = %{$_[0]};
$in{'start'} ||= 0;
if ($module_name eq "mysql") {
	return "limit $in{'start'},$displayconfig{'perpage'}";
	}
else {
	return "limit $displayconfig{'perpage'} offset $in{'start'}";
	}
}

# get_search_limit(&amp;in)
# Returns an SQL expression for sorting, hidden fields for the current sort order, and
# URL args for the current order
sub get_search_sort
{
local %in = %{$_[0]};
if ($in{'sortfield'}) {
	local ($sort, $sorthids, $sortargs);
	$sort = "order by ".&amp;quotestr($in{'sortfield'})." ".($in{'sortdir'} ? "asc" : "desc");
	$sorthids = &amp;ui_hidden("sortfield", $in{'sortfield'})."\n".
		    &amp;ui_hidden("sortdir", $in{'sortdir'})."\n";
	$sortargs = "&amp;sortfield=".&amp;urlize($in{'sortfield'}).
		    "&amp;sortdir=$in{'sortdir'}";
	return ($sort, $sorthids, $sortargs);
	}
else {
	return ( undef, undef, undef );
	}
}

# make_like(mode, for)
sub make_like
{
local ($match, $for) = @_;
local $qu = $module_name eq "mysql" ? '"' : "'";
return $match == 0 ? "like $qu%$for%$qu" :
       $match == 1 ? "like $qu$for$qu" :
       $match == 2 ? "not like $qu%$for%$qu" :
       $match == 3 ? "not like $qu$for$qu" :
       $match == 4 ? "&gt; $for" :
       $match == 5 ? "&lt; $for" :
		     " = \"\"";
}

# get_databases_return_link(db-name)
# Returns a link back to the DB list, which may be in Virtualmin
sub get_databases_return_link
{
my ($dbname) = @_;
if (&amp;foreign_check("virtual-server") &amp;&amp;
    &amp;foreign_available("virtual-server")) {
	$virtual_server::no_virtualmin_plugins = 1;
	&amp;foreign_require("virtual-server", "virtual-server-lib.pl");
	if (!&amp;virtual_server::master_admin()) {
		my $type = $module_name =~ /^mysql/ ? 'mysql' : 'postgres';
		foreach my $d (grep { &amp;virtual_server::can_edit_domain($_) }
				    &amp;virtual_server::list_domains()) {
			my @dbs = &amp;virtual_server::domain_databases(
					$d, [ $type ]);
			my ($got) = grep { $_-&gt;{'name'} eq $dbname } @dbs;
			if ($got) {
				return "../virtual-server/list_databases.cgi?".
				       "dom=$d-&gt;{'id'}";
				}
			}
		}
	}
return "";
}

1;

</pre></body></html>