#!/usr/local/bin/perl
# Show the details of one domain

require './virtual-server-lib.pl';
use POSIX;
&ReadParse();
$d = &get_domain($in{'dom'});
$d || &error($text{'edit_egone'});
&can_edit_domain($d) || &error($text{'edit_ecannot'});
if ($d->{'parent'}) {
	$parentdom = &get_domain($d->{'parent'});
	}
if ($d->{'alias'}) {
	$aliasdom = &get_domain($d->{'alias'});
	}
$tmpl = &get_template($d->{'template'});
&ui_print_header(&domain_in($d), $text{'view_title'}, "");
print &ui_table_start($text{'edit_header'}, "width=100%", 4);

# Domain name (with link), user and group
print "<tr> <td><b>$text{'edit_domain'}</b></td>\n";
if ($d->{'web'}) {
	print &ui_table_row($text{'edit_domain'},
				"<tt><a href=http://$d->{'dom'}/>$d->{'dom'}</a></tt>");
	}
else {
	print &ui_table_row($text{'edit_domain'},
			      "<tt>$d->{'dom'}</tt>");
	}
print &ui_table_row($text{'edit_user'}, "<tt>$d->{'user'}</tt>");
print &ui_table_row($text{'edit_group'},
		    $d->{'unix'} && $d->{'group'} ? "<tt>$d->{'group'}</tt>"
						  : $text{'edit_nogroup'});

# Databases
@dbs = &domain_databases($d);
@mysqldbs = grep { $_->{'type'} eq "mysql" } @dbs;
@postgresdbs = grep { $_->{'type'} eq "postgres" } @dbs;
print &ui_table_row($text{'edit_dbs'},
	    @dbs ? scalar(@dbs)
		 : $text{'edit_nodbs'});

# Template
print &ui_table_row($text{'edit_tmpl'}, $tmpl->{'name'});

# Reseller
print &ui_table_row($text{'edit_reseller'},
		    $d->{'reseller'} ? "<tt>$d->{'reseller'}</tt>"
				     : $text{'edit_noreseller'});

# IP-related options
if (!$aliasdom) {
	print &ui_table_row($text{'edit_ip'},
		"<tt>$d->{'ip'}</tt> ".
		($d->{'virt'} ? $text{'edit_private'} : $text{'edit_shared'}), 3);

	}

# Home directory
if (!$aliasdom && $d->{'dir'}) {
	print &ui_table_row($text{'edit_home'}, "<tt>$d->{'home'}</tt>");
	}

# Description
print &ui_table_row($text{'edit_owner'}, $d->{'owner'});

# Show forwarding / proxy destination
if ($d->{'proxy_pass_mode'} && $d->{'proxy_pass'} && $d->{'web'}) {
	print &ui_table_row($text{'edit_proxy'.$d->{'proxy_pass_mode'}},
		$d->{'proxy_pass'}, 3);
	}

if ($aliasdom) {
	# Alias destination
	print &ui_table_row($text{'edit_aliasto'},
	    "<a href='view_domain.cgi?dom=$d->{'alias'}'>$aliasdom->{'dom'}</a>",
	    3);
	}
elsif (!$parentdom) {
	# Contact email address
	print &ui_table_row($text{'edit_email'}, $d->{'emailto'}, 3);
	}
else {
	# Show link to parent domain
	print &ui_table_row($text{'edit_parent'},
	    "<a href='view_domain.cgi?dom=$d->{'parent'}'>$parentdom->{'dom'}</a>",
	    3);
	}

# Show any alias domains
@aliasdoms = &get_domain_by("alias", $d->{'id'});
if (@aliasdoms) {
	print &ui_table_row($text{'edit_aliasdoms'},
		scalar(@aliasdoms)." <a href='search.cgi?field=alias&what=$d->{'dom'}'>$text{'edit_sublist'}</a>", 3);
	}

# Show user and group quotas
if ($config{'home_quotas'} && !$parentdom) {
	print &ui_table_hr();
	print &ui_table_row($text{'edit_quota'},
	    $d->{'quota'} ? &quota_show($d->{'quota'}, "home")
			  : $text{'form_unlimit'});

	print &ui_table_row($text{'edit_uquota'},
	    $d->{'uquota'} ? &quota_show($d->{'uquota'}, "home")
			   : $text{'form_unlimit'});
	}

# Show disk usage
if ($config{'home_quotas'} && !$aliasdom && $d->{'unix'}) {
	# Get usage for mail users in the domain
	($homequota, $mailquota, $duser, $dbquota) =
		&get_domain_user_quotas($d);
	$homequota -= $duser->{'uquota'};
	$homequota = 0 if ($homequota < 0);
	$mailquota -= $duser->{'umquota'};
	$mailquota = 0 if ($mailquota < 0);

	# Get usage for sub-domain mail users
	@subs = &get_domain_by("parent", $d->{'id'});
	($subhomequota, $submailquota, $dummy, $subdbquota) =
		&get_domain_user_quotas(@subs);

	# Get group usage for the domain
	($totalhomequota, $totalmailquota) = &get_domain_quota($d);

	# Show file usage, for total, unix user and mail users
	$bsize = &quota_bsize("home");
	$msg = &has_mail_quotas() ? $text{'edit_allquotah'}
				  : $text{'edit_allquota'};
	print &ui_table_row($msg,
	   &text('edit_quotaby',
		&nice_size($totalhomequota*$bsize),
		&nice_size($duser->{'uquota'}*$bsize),
		&nice_size(($homequota+$subhomequota)*$bsize)), 3);

	# Show mail filesystem usage separately
	if (&has_mail_quotas()) {
		$mbsize = &quota_bsize("home");
		print &ui_table_row($text{'edit_allquotam'},
		  &text('edit_quotaby',
			&nice_size($totalmailquota*$mbsize),
			&nice_size($duser->{'umquota'}*$mbsize),
			&nice_size(($mailquota+$submailquota)*$mbsize)), 3);
		}

	# Show DB usage
	if ($dbquota+$subdbquota) {
		print &ui_table_row($text{'edit_dbquota'},
		    &text('edit_quotabysubs',
			&nice_size($dbquota+$subdbquota),
			&nice_size($dbquota),
			&nice_size($subdbquota)), 3);
		}
	}

# Show bandwidth limit and usage
if ($config{'bw_active'} && !$d->{'parent'}) {
	print &ui_table_row($text{'edit_bw'},
	    $d->{'bw_limit'} ?
		&text('edit_bwpast_'.$config{'bw_past'},
		      &nice_size($d->{'bw_limit'}), $config{'bw_period'}) :
		$text{'edit_bwnone'});

	print &ui_table_row(&text('edit_bwusage',
		strftime("%d/%m/%Y", localtime($d->{'bw_start'}*(24*60*60)))),
		&nice_size($d->{'bw_usage'}));
	}

# Show active features
print &ui_table_hr();
if ($d->{'disabled'}) {
	$featmsg = "<font color=#ff0000>".
		   $text{'edit_disabled_'.$d->{'disabled_reason'}}." ".
		   $text{'edit_disabled'}."</font>";
	}
else {
	foreach $f (@features, @feature_plugins) {
		if ($d->{$f}) {
			local $txt = $text{'feature_'.$f};
			$featmsg .= "$txt<br>\n";
			}
		}
	}
print &ui_table_row($text{'view_features'}, $featmsg);

print &ui_table_end();

&show_domain_buttons($d);

&ui_print_footer("", $text{'index_return'});

