#!/usr/local/bin/perl
# edit_domain.cgi
# Display details of a domain for editing

require './virtual-server-lib.pl';
use POSIX;
&ReadParse();
$d = &get_domain($in{'dom'});
$d || &error($text{'edit_egone'});
&can_config_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{'edit_title'}, "");

print "<form action=save_domain.cgi method=post>\n";
print "<input type=hidden name=dom value='$in{'dom'}'>\n";
print "<table border width=100%>\n";
print "<tr $tb> <td><b>$text{'edit_header'}</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";

# Domain name, with link
print "<tr> <td><b>$text{'edit_domain'}</b></td>\n";
if ($d->{'web'}) {
	print "<td><tt><a href=http://$d->{'dom'}/>$d->{'dom'}</a></tt></td>\n";
	}
else {
	print "<td><tt>$d->{'dom'}</tt></td>\n";
	}

print "<td><b>$text{'edit_user'}</b></td>\n";
print "<td><tt>$d->{'user'}</tt></td> </tr>\n";

print "<tr> <td><b>$text{'edit_group'}</b></td>\n";
if (($d->{'unix'} || $d->{'parent'}) && $d->{'group'}) {
	print "<td><tt>$d->{'group'}</tt></td>\n";
	}
else {
	print "<td>$text{'edit_nogroup'}</td>\n";
	}

if (!$aliasdom) {
	# Only show database name for non-alias domains
	print "<td><b>$text{'edit_dbs'}</b></td>\n";
	@dbs = &domain_databases($d);
	if (@dbs > 0) {
		local @mysqldbs = grep { $_->{'type'} eq "mysql" } @dbs;
		local @postgresdbs = grep { $_->{'type'} eq "postgres" } @dbs;
		#print "<td>",&text('edit_dbtypes', scalar(@mysqldbs), scalar(@postgresdbs)),"</td> </tr>\n";
		print "<td>",scalar(@dbs),"</td>\n";
		}
	else {
		print "<td>$text{'edit_nodbs'}</td> </tr>\n";
		}
	}
else {
	print "</tr>\n";
	}

# Show username prefix, with option to change
if (!$aliasdom) {
	@users = &list_domain_users($d, 1, 1, 1, 1);
	print "<tr> <td><b>$text{'edit_prefix'}</b></td> <td colspan=3>\n";
	if (@users) {
		print "<tt>$d->{'prefix'}</tt>",
		      " (",&text('edit_noprefix', scalar(@users)),")\n";
		}
	else {
		print &ui_textbox("prefix", $d->{'prefix'}, 30);
		}
	print "</td> </tr>\n";
	}

# Show active template
foreach $t (&list_templates()) {
	next if ($t->{'deleted'});
	next if (($d->{'parent'} && !$d->{'alias'}) && !$t->{'for_sub'});
	next if (!$d->{'parent'} && !$t->{'for_parent'});
	next if (!&master_admin() && !&reseller_admin() && !$t->{'for_users'});
	next if ($d->{'alias'} && !$t->{'for_alias'});
	next if (!&can_use_template($t));
	push(@cantmpls, $t);
	$gottmpl = 1 if ($t->{'id'} == $tmpl->{'id'});
	}
push(@cantmpls, $tmpl) if (!$gottmpl);
print "<tr> <td><b>$text{'edit_tmpl'}</b></td>\n";
print "<td>",&ui_select("template", $tmpl->{'id'},
	[ map { [ $_->{'id'}, $_->{'name'} ] } @cantmpls ]),"</td> </tr>\n";

# Show reseller
print "<td><b>$text{'edit_reseller'}</b></td>\n";
print "<td>",$d->{'reseller'} ? "<tt>$d->{'reseller'}</tt>"
			      : $text{'edit_noreseller'},"</tt></td> </tr>\n";

if (!$aliasdom) {
	# Show IP-related options
	print "<tr> <td><b>$text{'edit_ip'}</b></td>\n";
	print "<td colspan=3><tt>$d->{'ip'}</tt>\n";
	print $d->{'virt'} ? $text{'edit_private'} : $text{'edit_shared'};
	print "</td> </tr>\n";

	print "<tr> <td><b>$text{'edit_virt'}</b></td>\n";
	print "<td colspan=3>\n";
	if ($d->{'virt'}) {
		# Got a virtual IP .. show option to remove
		local $iface = &get_address_iface($d->{'ip'});
		print &ui_radio("virt", 1,
		    [ [ 0, $text{'edit_virtoff'} ],
		      [ 1, &text('edit_virton', "<tt>$iface</tt>") ] ]);
		}
	elsif ($config{'all_namevirtual'}) {
		# Always name-based, but IP can be changed
		print "<input name=ip size=15 value='$d->{'ip'}'>\n";
		}
	elsif (!&can_use_feature("virt")) {
		# Not allowed to add virtual IP
		print $text{'edit_virtnone'};
		}
	else {
		# No IP .. show option to add
		print "<input type=radio name=virt value=0 checked> ",
		      "$text{'edit_virtnone'}\n";
		if ($tmpl->{'ranges'} ne "none") {
			# Can do automatic allocation
			local %racl = $d->{'reseller'} ?
				&get_reseller_acl($d->{'reseller'}) : ();
			local $alloc = $racl{'ranges'} ?
				&free_ip_address(\%racl) :
				&free_ip_address($tmpl);
			if ($alloc) {
				print "<input type=radio name=virt value=1> ",
				      &text('edit_alloc', $alloc);
				}
			else {
				# None left!
				print $text{'form_noalloc'};
				}
			}
		else {
			# Use must enter IP
			print "<input type=radio name=virt value=1> ",
			      "$text{'edit_virtalloc'}\n";
			print "<input name=ip size=15>\n";
			}
		}
	print "</td> </tr>\n";
	}

if (!$aliasdom && $d->{'dir'}) {
	# Show home directory
	print "<tr> <td><b>$text{'edit_home'}</b></td>\n";
	print "<td colspan=3><tt>$d->{'home'}</tt></td> </tr>\n";
	}

if ($d->{'proxy_pass_mode'} && $d->{'proxy_pass'} && $d->{'web'}) {
	# Show forwarding / proxy destination
	print "<tr> <td><b>",$text{'edit_proxy'.$d->{'proxy_pass_mode'}},
	      "</b></td>\n";
	print "<td colspan=3><tt>$d->{'proxy_pass'}</tt></td> </tr>\n";
	}

# Show description
print "<tr> <td><b>$text{'edit_owner'}</b></td>\n";
print "<td colspan=3><input name=owner size=50 value='",
	&html_escape($d->{'owner'}),"'></td> </tr>\n";

if ($aliasdom) {
	# Show link to aliased domain
	print "<tr> <td><b>$text{'edit_aliasto'}</b></td>\n";
	print "<td colspan=3><a href='edit_domain.cgi?dom=$d->{'alias'}'>",
	      "$aliasdom->{'dom'}</a></td> </tr>\n";
	}
elsif (!$parentdom) {
	# Show owner's email address and password
	print "<tr> <td><b>$text{'edit_email'}</b></td> <td colspan=3>\n";
	if ($d->{'unix'}) {
		printf "<input type=radio name=email_def value=1 %s> %s\n",
			$d->{'email'} ? "" : "checked", $text{'edit_email_def'};
		printf "<input type=radio name=email_def value=0 %s>\n",
			$d->{'email'} ? "checked" : "";
		}
	print "<input name=email size=30 value='",
		&html_escape($d->{'email'}),"'></td> </tr>\n";

	print "<tr> <td><b>$text{'edit_passwd'}</b></td> <td colspan=3>\n";
	print "<input type=radio name=passwd_def value=1 checked> $text{'edit_lv'}\n";
	print "<input type=radio name=passwd_def value=0> $text{'edit_set'}\n";
	print "<input type=password name=passwd size=20></td> </tr>\n";
	}
else {
	# Show link to parent domain
	print "<tr> <td><b>$text{'edit_parent'}</b></td>\n";
	print "<td colspan=3><a href='edit_domain.cgi?dom=$d->{'parent'}'>",
	      "$parentdom->{'dom'}</a></td> </tr>\n";
	}

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

# Show user and group quota editing inputs
if ($config{'home_quotas'} && !$parentdom && &can_edit_quotas()) {
	print "<tr> <td colspan=4><hr></td> </tr>\n";

	print "<tr> <td><b>$text{'edit_quota'}</b></td> <td colspan=3>\n";
	printf "<input type=radio name=quota_def value=1 %s> %s\n",
		$d->{'quota'} ? "" : "checked", $text{'form_unlimit'};
	printf "<input type=radio name=quota_def value=0 %s>\n",
		$d->{'quota'} ? "checked" : "";
	print &quota_input("quota", $d->{'quota'}, $config{'home_quotas'}),
	      "</td> </tr>\n";

	print "<tr> <td><b>$text{'edit_uquota'}</b></td> <td colspan=3>\n";
	printf "<input type=radio name=uquota_def value=1 %s> %s\n",
		$d->{'uquota'} ? "" : "checked", $text{'form_unlimit'};
	printf "<input type=radio name=uquota_def value=0 %s>\n",
		$d->{'uquota'} ? "checked" : "";
	print &quota_input("uquota", $d->{'uquota'}, $config{'home_quotas'}),
	      "</td> </tr>\n";
	}

# Show total disk usage, broken down into unix user and mail users
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");
	if (&has_mail_quotas()) {
		print "<tr> <td><b>$text{'edit_allquotah'}</b></td>\n";
		}
	else {
		print "<tr> <td><b>$text{'edit_allquota'}</b></td>\n";
		}
	print "<td colspan=3>\n";
	print &text('edit_quotaby',
		&nice_size($totalhomequota*$bsize),
		&nice_size($duser->{'uquota'}*$bsize),
		&nice_size(($homequota+$subhomequota)*$bsize));
	print "</td> </tr>\n";

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

	# Show DB usage
	if ($dbquota+$subdbquota) {
		print "<tr> <td><b>$text{'edit_dbquota'}</b></td>\n";
		print "<td colspan=3>\n";
		print &text('edit_quotabysubs',
			&nice_size($dbquota+$subdbquota),
			&nice_size($dbquota),
			&nice_size($subdbquota));
		print "</td> </tr>\n";
		}
	}

if ($config{'bw_active'} && !$d->{'parent'}) {
	# Show bandwidth limit and usage
	print "<tr> <td><b>$text{'edit_bw'}</b></td> <td colspan=3>\n";
	if (&can_edit_bandwidth()) {
		print &bandwidth_input("bw", $d->{'bw_limit'});
		}
	else {
		print $d->{'bw_limit'} ?
		    &text('edit_bwpast_'.$config{'bw_past'},
		        &nice_size($d->{'bw_limit'}), $config{'bw_period'}) :
		    $text{'edit_bwnone'};
		}
	print "</td> </tr>\n";

	# Show usage over current period
	if (defined($d->{'bw_usage'})) {
		print "<tr> <td><b>",&text('edit_bwusage',
			strftime("%d/%m/%Y", localtime($d->{'bw_start'}*(24*60*60)))),
		      "</b></td> <td colspan=3>\n";
		if ($d->{'bw_limit'} && $d->{'bw_usage'} > $d->{'bw_limit'}) {
			print "<font color=#ff0000>".&nice_size($d->{'bw_usage'})."</font>\n";
			if ($d->{'bw_notify'}) {
				$notify = localtime($d->{'bw_notify'});
				print &text('edit_bwnotify', $notify);
				}
			}
		else {
			print &nice_size($d->{'bw_usage'});
			}
		print "</td> </tr>\n";
		}
	}

# Show section for custom fields, if any
$fields = &show_custom_fields($d);
if ($fields) {
	print "<tr> <td colspan=4><hr></td> </tr>\n";
	print $fields;
	}

# Show buttons for turning features on and off (if allowed)
print "<tr> <td colspan=4><hr></td> </tr>\n";
if ($d->{'disabled'}) {
	# Disabled, so tell the user that features cannot be changed
	print "<tr> <td colspan=4 align=center>&nbsp;<br><font color=#ff0000>",
	      "<b>",$text{'edit_disabled_'.$d->{'disabled_reason'}},"\n",
	      $text{'edit_disabled'},
	      "</b></font><br>&nbsp;</td> </tr>\n";
	}
else {
	# Show features for this domain
	print "<tr> <td colspan=4><table width=100%>\n";
	$i = 0;
	@dom_features = $aliasdom ? @opt_alias_features : @opt_features;
	foreach $f (@dom_features) {
		# Webmin feature is not needed for subdomains
		next if ($d->{'parent'} && $f eq "webmin");

		# Unix feature is not needed for subdomains
		next if ($d->{'parent'} && $f eq "unix");

		# Cannot enable features not in alias
		next if ($aliasdom && !$aliasdom->{$f});

		# Don't show features that are always enabled
		if ($config{$f} == 3 && $d->{$f}) {
			print &ui_hidden($f, 1),"\n";
			next;
			}

		print "<tr>\n" if ($i%2 == 0);
		local $txt = $parentdom ? $text{'edit_sub'.$f} : undef;
		$txt ||= $text{'edit_'.$f};
		if (!&can_use_feature($f)) {
			print "<td>",&ui_checkbox($f."_dis", 1, "", $d->{$f},
						  undef, 1),"</td>\n";
			print &ui_hidden($f, $d->{$f}),"\n";
			}
		else {
			print "<td>",&ui_checkbox($f, 1, "", $d->{$f},
				undef, !$config{$f} && defined($config{$f})),
				"</td>\n";
			}
		print "<td><b>",&hlink($txt, $f),"</b></td>\n";
		print "</tr>\n" if ($i++%2 == 1);
		}

	foreach $f (@feature_plugins) {
		# Cannot enable features not in alias
		next if ($aliasdom && !$aliasdom->{$f});

		print "<tr>\n" if ($i%2 == 0);
		$label = &plugin_call($f, "feature_label", 1);

		if (!&can_use_feature($f)) {
			print "<td>",&ui_checkbox($f."_dis", 1, "", $d->{$f},
						  undef, 1),"</td>\n";
			print &ui_hidden($f, $d->{$f}),"\n";
			}
		else {
			print "<td>",&ui_checkbox($f, 1, "", $d->{$f}),"</td>\n";
			}
		print "<td><b>$label</b></td>\n";
		print "</tr>\n" if ($i++%2 == 1);
		}

	print "</table></td></tr>\n";
	}

print "</table></td></tr></table>\n";

# Save changes button
print "<input type=submit value='$text{'edit_save'}'>\n";
print "</form>\n";

&show_domain_buttons($d);

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

