#!/usr/local/bin/perl
# domain_form.cgi
# Display a form for setting up a new virtual domain

require './virtual-server-lib.pl';
&ReadParse();

if ($in{'import'}) {
	# Redirect to the import form
	&redirect("import_form.cgi");
	return;
	}
elsif ($in{'migrate'}) {
	# Redirect to the migration form
	&redirect("migrate_form.cgi");
	return;
	}
elsif ($in{'batch'}) {
	# Redirect to the batch creation
	&redirect("mass_create_form.cgi");
	return;
	}
elsif ($in{'delete'}) {
	# Redirect to the mass server deletion form
	@d = split(/\0/, $in{'d'});
	&redirect("mass_delete_domains.cgi?".join("&", map { "d=$_" } @d));
	return;
	}
elsif ($in{'mass'}) {
	# Redirect to the mass server update form
	@d = split(/\0/, $in{'d'});
	&redirect("mass_domains_form.cgi?".join("&", map { "d=$_" } @d));
	return;
	}

# Get parent settings
&can_create_master_servers() || &can_create_sub_servers() ||
	&error($text{'form_ecannot'});
if ($in{'to'}) {
	# Creating an alias domain
	$aliasdom = &get_domain($in{'to'});
	$parentdom = $aliasdom->{'parent'} ?
		&get_domain($aliasdom->{'parent'}) : $aliasdom;
	$parentuser = $parentdom->{'user'};
	}
elsif (!&can_create_master_servers()) {
	# This user can only create a sub-server
	if ($access{'admin'}) {
		$parentdom = &get_domain($access{'admin'});
		$parentuser = $parentdom->{'user'};
		}
	else {
		$parentuser = $remote_user;
		}
	}
elsif ($in{'parentuser1'} || $in{'parentuser2'}) {
	# Creating sub-server explicitly
	$parentuser = $in{'add1'} ? $in{'parentuser1'} : $in{'parentuser2'};
	}
if ($parentuser && !$parentdom) {
	$parentdom = &get_domain_by("user", $parentuser, "parent", "");
	$parentdom || &error(&text('form_eparent', $parentuser));
	}
if ($in{'subdom'}) {
	# Creating a sub-domain
	$subdom = &get_domain($in{'subdom'});
	$subdom || &error(&text('form_esubdom', $in{'subdom'}));
	}

&ui_print_header(undef, $aliasdom ? $text{'form_title3'} :
	$parentdom ? $text{'form_title2'} : $text{'form_title'}, "");

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

print "<tr> <td><b>",&hlink($text{'form_domain'}, "domainname"),"</b></td>";
if ($subdom) {
	print "<td colspan=3>",&ui_textbox("dom", undef, 20),
			       ".$subdom->{'dom'}","</td> </tr>\n";
	}
else {
	print "<td colspan=3>",
	      &ui_textbox("dom", $access{'forceunder'} && $parentdom ? ".$parentdom->{'dom'}" : undef, 50),"</td> </tr>\n";
	}

print "<tr> <td><b>", &hlink($text{'form_owner'}, "ownersname"), "</b></td>";
print "<td colspan=3><input name=owner size=50></td> </tr>\n";

# Generate Javascript for template change
print "<script>\n";
print "function select_template(num)\n";
print "{\n";
foreach $t (&list_available_templates($parentdom, $aliasdom)) {
	local $tmpl = &get_template($t->{'id'});
	print "if (num == $tmpl->{'id'}) {\n";
	if (!$parentdom) {
		# Set group for unix user
		if (&can_choose_ugroup()) {
			$num = $tmpl->{'ugroup'} eq "none" ? 0 : 1;
			$val = $tmpl->{'ugroup'} eq "none" ? "" : $tmpl->{'ugroup'};
			print "    document.forms[0].group_def[$num].checked = true;\n";
			print "    document.forms[0].group.value = \"$val\";\n";
			}

		# Set quotas
		print &quota_javascript("quota", $tmpl->{'quota'}, "home", 0);
		print &quota_javascript("uquota", $tmpl->{'uquota'}, "home", 0);

		# Set limits
		print &quota_javascript("mailboxlimit", $tmpl->{'mailboxlimit'},
					"none", 1);
		print &quota_javascript("aliaslimit", $tmpl->{'aliaslimit'},
					"none", 1);
		print &quota_javascript("dbslimit", $tmpl->{'dbslimit'},
					"none", 1);
		if ($config{'bw_active'}) {
			print &quota_javascript("bwlimit", $tmpl->{'bwlimit'},
						"bw", 1);
			}
		$num = $tmpl->{'domslimit'} eq "none" ? 1 :
		       $tmpl->{'domslimit'} eq "0" ? 0 : 2;
		$val = $num == 2 ? $tmpl->{'domslimit'} : "";
		print "    document.forms[0].domslimit_def[$num].checked = true;\n";
		print "    document.forms[0].domslimit.value = \"$val\";\n";
		}
	print "    }\n";
	}
print "}\n";
print "</script>\n";

# Show template selection field
print "<tr> <td><b>",&hlink($text{'form_template'},"template"),"</b></td>\n";
if ($config{'template_auto'}) {
	print "<td><select name=template>\n";
	}
else {
	print "<td><select name=template onChange='select_template(options[selectedIndex].value)'>\n";
	}
foreach $t (&list_available_templates($parentdom, $aliasdom)) {
	$firsttemplate ||= $t;
	print "<option value=$t->{'id'}>$t->{'name'}\n";
	push(@cantmpls, $t);
	}
print "</select></td> </tr>\n";

# These settings are not needed for a sub-domain, as they come from the owner
if (!$parentuser) {
	print "<tr> <td colspan=4><hr></td> </tr>\n";


	# Contact email address
	print "<tr> <td><b>", &hlink($text{'form_email'}, "ownersemail"), "</b></td>";
	print "<td colspan=3><input type=radio name=email_def value=1 checked> ",
	      $text{'form_email_def'};
	print "<input type=radio name=email_def value=0> $text{'form_email_set'}\n";
	print "<input name=email size=30></td> </tr>\n";

	# Unix username
	print "<tr> <td><b>", &hlink($text{'form_user'}, "unixusername"), "</b></td>";
	print "<td nowrap colspan=3><input type=radio name=vuser_def value=1 checked> ",
	      "$text{'form_auto'}";
	print "<input type=radio name=vuser_def value=0> $text{'form_nwuser'} ";
	print "<input name=vuser size=15></td> </tr>\n";

	print "<tr> <td><b>", &hlink($text{'form_pass'}, "password"), "</b></td>";
	print "<td colspan=3>",&new_password_input("vpass"),"</td> </tr>\n";

	print "<tr> <td><b>",&hlink($text{'form_mgroup'}, "mailgroupname"),"</b></td>";
	print "<td colspan=3><input type=radio name=mgroup_def value=1 checked> ",
	      "$text{'form_auto'}";
	print "<input type=radio name=mgroup_def value=0> $text{'form_nwgroup'} ",
	      "<input name=mgroup size=13></td> </tr>\n";

	if (&can_choose_ugroup()) {
		print "<tr> <td><b>",&hlink($text{'form_group'}, "unixgroupname"),"</b></td>";
		print "<td colspan=3><input type=radio name=group_def value=1 checked> ",
		      "$text{'form_crgroup'}";
		print "<input type=radio name=group_def value=0> $text{'form_exgroup'}";
		print &unix_group_input("group"),"</td> </tr>\n";
		}
	}
elsif ($aliasdom) {
	# Show destination of alias
	print "<tr> <td><b>", &hlink($text{'form_aliasdom'}, "aliasdom"), "</b></td>";
	print "<td><a href='edit_domain.cgi?dom=$parentdom->{'id'}'>$aliasdom->{'dom'}</a></td> </tr>\n";
	}
else {
	# Show parent domain
	print "<tr> <td><b>", &hlink($text{'form_parentdom'}, "parentdom"), "</b></td>";
	print "<td><a href='edit_domain.cgi?dom=$parentdom->{'id'}'>$parentdom->{'dom'}</a> (<tt>$parentuser</tt>)</td> </tr>\n";
	}

if (!$aliasdom && &max_username_length()) {
	# Show input for mail username prefix, if needed
	print "<tr> <td><b>",&hlink($text{'form_prefix'}, "prefixname"),"</b></td>";
	print "<td colspan=3><input type=radio name=prefix_def value=1 checked> ",
	      "$text{'form_auto'}";
	print "<input type=radio name=prefix_def value=0> ",
	      "<input name=prefix size=13></td> </tr>\n";
	}
else {
	print "<input type=hidden name=prefix_def value=1>\n";
	}

if (!$aliasdom && &database_feature() && &can_edit_databases() && !$subdom) {
	# Show database name field, iff this is not an alias domain
	print "<tr> <td><b>",&hlink($text{'form_dbname'},"dbname"),"</b></td>\n";
	print "<td colspan=3><input type=radio name=db_def value=1 checked> ",
	      "$text{'form_auto'}";
	print "<input type=radio name=db_def value=0> ",
	      "<input name=db size=13></td> </tr>\n";
	}

# Only display quota inputs if enabled, and if not creating a subdomain
if ($config{'home_quotas'} && !$parentuser && !$config{'template_auto'}) {
	print "<tr> <td colspan=4><hr></td> </tr>\n";
	print "<tr> <td><b>", &hlink($text{'form_quota'}, "websitequota"), "</b></td>\n";
	@defquota = split (/ /,$config{'defquota'});
	if ( !@defquota or $#defquota eq 0 ) {
	    print "<td>",&quota_input("quota", $config{'defquota'},
				      "home"),"</td>\n";
	} else {
	    print "<td><select name=quota>\n";
	    foreach $i (@defquota) {
		chomp $i;
		print "<option value=\"$i\">$i</option>\n";
	    }
	    print "<option value=\"-1\">$text{'other'}</option></select>\n";
	    print &quota_input("otherquota", "",
			       "home"),"</td>\n";
	}
	print "</tr>\n";

	print "<tr> <td><b>", &hlink($text{'form_uquota'}, "unixuserquota"), "</b></td>\n";
	@defuquota = split (/ /,$config{'defuquota'});
	if ( !@defuquota or $#defuquota eq 0 ) {
	    print "<td>",&quota_input("uquota", $config{'defuquota'},
				      "home"),"</td>\n";
	} else {
	    print "<td><select name=uquota>\n";
	    foreach $i (@defuquota) {
		chomp $i;
		print "<option value=\"$i\">$i</option>\n";
	    }
	    print "<option value=\"-1\">$text{'other'}</option></select>\n";
	    print &quota_input("otheruquota", "", "home"),"</td>\n";
	}
	print "</tr>\n";
}

if (!$parentdom && $config{'bw_active'} && !$config{'template_auto'}) {
	# Show bandwidth limit field
	print "<tr> <td><b>",&hlink($text{'edit_bw'},
			    "bwlimit"),"</b></td> <td colspan=3>\n";
	print &bandwidth_input("bwlimit", 0);
	print "</td> </tr>\n";
	}

if (!$parentuser && !$config{'template_auto'}) {
	# Show input for limit on number of mailboxes
	print "<tr> <td><b>",&hlink($text{'form_mailboxlimit'},
			    "mailboxlimit"),"</b></td> <td colspan=2>\n";
	print &ui_opt_textbox("mailboxlimit", $config{'defmailboxlimit'}, 4,
	       $text{'form_unlimit'}, $text{'form_atmost'}),"</td> </tr>\n";

	# Show input for limit on number of aliases
	print "<tr> <td><b>",&hlink($text{'form_aliaslimit'},
			    "aliaslimit"),"</b></td> <td colspan=2>\n";
	print &ui_opt_textbox("aliaslimit", $config{'defaliaslimit'}, 4,
	       $text{'form_unlimit'}, $text{'form_atmost'}),"</td> </tr>\n";

	# And databases
	print "<tr> <td><b>",&hlink($text{'form_dbslimit'},
			    "dbslimit"),"</b></td> <td colspan=2>\n";
	print &ui_opt_textbox("dbslimit", $config{'defdbslimit'}, 4,
	       $text{'form_unlimit'}, $text{'form_atmost'}),"</td> </tr>\n";

	# Show input for restriction of number of sub-domains this domain
	# owner can create
	local $dlm = $config{'defdomslimit'} eq "" ? 1 :
		     $config{'defdomslimit'} eq "*" ? 2 : 0;
	print "<tr> <td><b>",&hlink($text{'form_domslimit'},
			    "domslimit"),"</b></td> <td colspan=2>\n";
	printf "<input type=radio name=domslimit_def value=1 %s> %s\n",
		$dlm == 1 ? "checked" : "",
		$text{'form_nocreate'};
	printf "<input type=radio name=domslimit_def value=2 %s> %s\n",
		$dlm == 2 ? "checked" : "",
		$text{'form_unlimit'};
	printf "<input type=radio name=domslimit_def value=0 %s> %s\n",
		$dlm == 0 ? "checked" : "",
		$text{'form_atmost'};
	printf "<input name=domslimit value='%s' size=4></td></tr>\n",
		$dlm == 0 ? $config{'defdomslimit'} : "";

	# Show input for default database name limit
	print "<tr> <td><b>$text{'limits_nodbname'}</b></td>\n";
	print "<td>",&ui_radio("nodbname", $config{'defnodbname'} ? 1 : 0,
	       [ [ 0, $text{'yes'} ], [ 1, $text{'no'} ] ]),"</td> </tr>\n";
	}

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

# Show checkboxes for features
print "<tr> <td colspan=4><hr></td> </tr>\n";
print "<tr> <td colspan=4><table width=100%>\n";
$i = 0;
foreach $f ($aliasdom ? @opt_alias_features :
	    $subdom ? @opt_subdom_features : @opt_features) {
	# Don't allow access to features that this user hasn't been
	# granted for his subdomains.
	next if (!&can_use_feature($f));
	next if ($parentdom && $f eq "webmin");
	next if ($parentdom && $f eq "unix");
	next if ($aliasdom && !$aliasdom->{$f});
	$can_feature{$f}++;

	if ($config{$f} == 3) {
		# This feature is always on, so don't show it
		print &ui_hidden($f, 1),"\n";
		next;
		}

	local $txt = $parentdom ? $text{'form_sub'.$f} : undef;
	$txt ||= $text{'form_'.$f};
	print "<tr>\n" if ($i%2 == 0);
	print "<td width=5>",&ui_checkbox($f, 1, "", $config{$f} == 1, undef,
			  !$config{$f} && defined($config{$f})),"</td>\n";
	print "<td width=50% align=left><b>",&hlink($txt, $f),"</b></td>";
	print "</tr>\n" if ($i++%2 == 1);
	}

# Show checkboxes for plugins
foreach $f (@feature_plugins) {
	next if (!&plugin_call($f, "feature_suitable",
				$parentdom, $aliasdom, $subdom));
	next if (!&can_use_feature($f));
	next if ($aliasdom && !$aliasdom->{$f});

	print "<tr>\n" if ($i%2 == 0);
	$label = &plugin_call($f, "feature_label", 0);
	print "<td>",&ui_checkbox($f, 1, "", 1),"</td>\n";
	print "<td><b>$label</b></td>\n";
	print "</tr>\n" if ($i++%2 == 1);
	}
print "</table></td> </tr>\n";

# Show inputs for setting up a proxy-only virtual server
if ($config{'proxy_pass'} && !$aliasdom) {
	print &frame_fwd_input();
	}

# Show field for mail forwarding
if ($can_feature{'mail'} && !$aliasdom && !$subdom) {
	print "<tr> <td><b>",&hlink($text{'form_fwdto'}, "fwdto"),"</b></td>\n";
	print "<td colspan=3>",&ui_opt_textbox("fwdto", undef, 30, $text{'form_fwdto_none'}),"</td> </tr>\n";
	}

# Show IP address allocation section
if (!$aliasdom && &can_use_feature("virt")) {
	$defip = &get_default_ip();
	print "<tr> <td><b>",&hlink($text{'form_iface'},"iface"),"</b></td>\n";
	print "<td colspan=3>",&virtual_ip_input(\@cantmpls),"</td> </tr>\n";
	}

print "</table></td></tr></table>\n";
print "<input type=submit value='$text{'form_ok'}'></form>\n";
if (!$config{'template_auto'}) {
	print "<script>select_template($firsttemplate->{'id'});</script>\n";
	}

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

