#!/usr/local/bin/perl
# edit_user.cgi
# Display a form for editing or adding a user. This can be a local user,
# or a domain mailbox user

require './virtual-server-lib.pl';
&ReadParse();
if ($in{'dom'}) {
	$d = &get_domain($in{'dom'});
	&can_edit_domain($d) || &error($text{'users_ecannot'});
	}
else {
	&can_edit_local() || &error($text{'users_ecannot2'});
	}
&can_edit_users() || &error($text{'users_ecannot'});
$din = $d ? &domain_in($d) : undef;
$tmpl = $d ? &get_template($d->{'template'}) : &get_template(0);
if ($in{'new'}) {
	&ui_print_header($din, $text{'user_create'}, "");
	$user = &create_initial_user($d, undef, $in{'web'});
	}
else {
	&ui_print_header($din, $text{'user_edit'}, "");
	@users = &list_domain_users($d);
	($user) = grep { $_->{'user'} eq $in{'user'} &&
			 $_->{'unix'} == $in{'unix'} } @users;
	$mailbox = $d && $d->{'user'} eq $user->{'user'} && $user->{'unix'};
	}

print &check_clicks_function();
print "<form action=save_user.cgi>\n";
print "<input type=hidden name=new value='$in{'new'}'>\n";
print "<input type=hidden name=dom value='$in{'dom'}'>\n";
print "<input type=hidden name=old value='$in{'user'}'>\n";
print "<input type=hidden name=unix value='$in{'unix'}'>\n";
print "<input type=hidden name=web value='$in{'web'}'>\n";

print "<table border>\n";
print "<tr $tb> <td><b>",$mailbox ? $text{'user_mheader'} :
			 $user->{'webowner'} ? $text{'user_wheader'} :
			 $d ? $text{'user_header'} : $text{'user_lheader'},
      "</b></td> </tr>\n";
print "<tr $cb> <td><table>\n";

print "<tr> <td><b>",&hlink($text{'user_user'}, "username"),"</b></td>\n";
if ($mailbox) {
	print "<td><tt>$user->{'user'}</tt></td> </tr>\n";
	$pop3 = $user->{'user'};
	}
else {
	$pop3 = $d && !$user->{'noappend'} ?
		&remove_userdom($user->{'user'}, $d) : $user->{'user'};
	print "<td><input name=mailuser size=13 value='$pop3'>",
		$d ? "\@$d->{'dom'}\n" : "\n";
	if ($pop3 ne $user->{'user'}) {
		print " ",&text('user_pop3', "<tt>$user->{'user'}</tt>");
		}
	print "</td> </tr>\n";
	print "<input type=hidden name=oldpop3 value='$pop3'>\n";
	}

# Real name - only for true Unix users or LDAP persons
if ($user->{'person'}) {
	print "<tr> <td><b>",&hlink($text{'user_real'}, "realname"),"</b></td>\n";
	if ($mailbox) {
		print "<td>$user->{'real'}</td> </tr>\n";
		}
	else {
		print "<td><input name=real size=25 value='$user->{'real'}'></td> </tr>\n";
		}
	}

# Password cannot be edited for domain owners (because it is the domain pass)
if (!$mailbox) {
	print "<tr> <td valign=top><b>",&hlink($text{'user_pass'}, "password"),
	      "</b></td> <td>\n";
	if ($in{'new'}) {
		print &new_password_input("mailpass");
		}
	else {
		# For an existing user, offer to change password
		print "<input type=radio name=mailpass_def value=1 checked> ",
		      "$text{'user_passdef'}\n";
		if (defined($user->{'plainpass'})) {
			if ($user->{'plainpass'}) {
				print "($user->{'plainpass'})\n";
				}
			else {
				print "(<i>$text{'user_none'}</i>)\n";
				}
			}
		print "<input type=radio name=mailpass_def value=0> $text{'user_passset'}\n";
		print "<input name=mailpass type=password size=13>\n";
		if ($user->{'unix'} && $user->{'change'}) {
			local $tm =timelocal(gmtime($user->{'change'} * 60*60*24));
			print "<br>",&text('user_lastch', &make_date($tm, 1));
			}
		}
	if (!$user->{'alwaysplain'}) {
		# Option to disable
		print "<br>",&ui_checkbox("disable", 1, $text{'user_disabled'},
					  $user->{'pass'} =~ /^\!/ ? 1 : 0);
		}
	print "</td> </tr>\n";
	}

if (&can_mailbox_home() && $d && $d->{'home'} &&
    !$mailbox && !$user->{'fixedhome'}) {
	# Show home directory editing field
	print "<tr> <td><b>", &hlink($text{'user_home'}, "userhome"), "</b></td>\n";
	if (!$user->{'brokenhome'}) {
		# Home is under server root, and so can be edited
		local $auto = $in{'new'} || $user->{'home'} eq "$d->{'home'}/$config{'homes_dir'}/$pop3";
		printf "<td><input type=radio name=home_def value=1 %s> %s\n",
			$auto ? "checked" : "", $text{'user_home1'};
		printf "<input type=radio name=home_def value=0 %s> %s\n",
			$auto ? "" : "checked", &text('user_homeunder');
		printf "<input name=home size=20 value='%s'></td> </tr>\n",
			$auto ? "" : substr($user->{'home'}, length($d->{'home'})+1);
		}
	else {
		# Home is elsewhere .. perhaps an imported user?
		print "<td><tt>$user->{'home'}</tt></td> </tr>\n";
		print &ui_hidden("brokenhome", 1),"\n";
		}
	}

if (&can_mailbox_ftp() && !$mailbox && $user->{'unix'}) {
	# Show FTP shell field
	print "<tr> <td><b>", &hlink($text{'user_ftp'}, "uftp"), "</b></td>\n";
	$ftp = $user->{'qmail'} && !$user->{'shell'} ? -2 :
	       $user->{'shell'} eq $config{'ftp_shell'} ? 1 :
	       $config{'jail_shell'} &&
		$user->{'shell'} eq $config{'jail_shell'} ? 2 :
	       $in{'new'} || $user->{'shell'} eq $config{'shell'} ? 0 : -1;
	print "<td>";
	if ($ftp == -1) {
		print &text('user_shell', "<tt>$user->{'shell'}</tt>");
		}
	elsif ($ftp == -2) {
		print &text('user_qmail');
		}
	else {
		printf "<input type=radio name=ftp value=1 %s> %s\n",
			$ftp == 1 ? "checked" : "", $text{'yes'};
		if ($config{'jail_shell'}) {
			printf "<input type=radio name=ftp value=2 %s> %s\n",
				$ftp == 2 ? "checked" : "", $text{'user_jail'};
			}
		printf "<input type=radio name=ftp value=0 %s> %s\n",
			$ftp == 0 ? "checked" : "", $text{'no'};
		}
	print "</td> </tr>\n";
	}

# Find and show all plugin features
foreach $f (@mail_plugins) {
	$input = &plugin_call($f, "mailbox_inputs", $user, $in{'new'}, $d);
	print $input;
	}

if (!$user->{'webowner'}) {
	print "<tr> <td colspan=2><hr></td> </tr>\n";
	}

if ($d && !$user->{'noprimary'} && $d->{'mail'}) {
	# Show primary email address field
	print "<tr> <td><b>", &hlink($text{'user_mailbox'}, "mailbox"), "</b></td>\n";
	printf "<td><input type=radio name=mailbox value=1 %s> %s\n",
		$user->{'email'} || $in{'new'} ? "checked" : "", $text{'yes'};
	printf "<input type=radio name=mailbox value=0 %s> %s</td> </tr>\n",
		$user->{'email'} || $in{'new'} ? "" : "checked", $text{'no'};
	}

if ($user->{'mailquota'}) {
	# Show Qmail/VPOPMail quota field
	print "<tr> <td><b>",&hlink($text{'user_qquota'},"qmailquota"),"</b></td>\n";
	$user->{'qquota'} = "" if ($user->{'qquota'} eq "none");
	print "<td>",&ui_radio("qquota_def", $user->{'qquota'} ? 0 : 1,
			       [ [ 1, $text{'form_unlimit'} ],
				 [ 0, " " ] ]);
	print &ui_textbox("qquota", $user->{'qquota'} || "", 10)," ",
	      $text{'form_bytes'},"</td> </tr>\n";
	}

if (!$mailbox && $user->{'unix'} && !$user->{'noquota'}) {
	# Show quotas field(s)
	if (&has_home_quotas()) {
		print "<tr> <td><b>", &hlink($qsame ? $text{'user_umquota'} : $text{'user_uquota'}, "diskquota"), "</b></td>\n";
		&quota_field("quota", $user->{'quota'}, $user->{'uquota'},
			     $config{'home_quotas'});
		}
	if (&has_mail_quotas()) {
		print "<tr> <td><b>", &hlink($text{'user_mquota'}, "diskmquota"), "</b></td>\n";
		&quota_field("mquota", $user->{'mquota'}, $user->{'umquota'},
			     $config{'mail_quotas'});
		}
	}

if (!$in{'new'} && ($user->{'email'} || @{$user->{'extraemail'}}) &&
    !$user->{'nomailfile'}) {
	# Show the user's mail file
	local ($sz, $umf, $lastmod) = &mail_file_size($user);
	print "<tr> <td valign=top>",
	      &hlink("<b>$text{'user_mail'}</b>", "mailfile"),"</td>\n";
	local $link = &read_mail_link($user, $d);
	print "<td><tt>$umf</tt>\n";
	if ($link) { print "<a href='$link'>$text{'user_read'}</a>\n"; }
	if ($lastmod) {
		print "<br>",&text('user_lastmod', &make_date($lastmod));
		}
	print "</td></tr>\n";
	}

if (!$user->{'noextra'}) {
	# Show extra email addresses
	print "<tr> <td valign=top>",
	      &hlink("<b>$text{'user_extra'}</b>", "extraemail"),"</td>\n";
	print "<td><textarea name=extra rows=5 cols=30>\n";
	foreach $e (@{$user->{'extraemail'}}) {
		print "$e\n";
		}
	print "</textarea></td> </tr>\n";
	}

if ($in{'new'} && &will_send_user_email($d)) {
	# Show address for confirmation email (for the mailbox itself)
	print "<tr> <td><b>",&hlink($text{'user_newmail'},"newmail"),"</b></td>\n";
	printf "<td><input type=radio name=newmail_def value=1 %s> %s\n",
		"checked", $user->{'email'} ? $text{'user_newmail1'}
					    : $text{'user_newmail2'};
	printf "<input type=radio name=newmail_def value=0 %s> %s\n",
		"", $text{'user_newmail0'};
	print "<input name=newmail size=20></td> </tr>\n";
	}
elsif (!$in{'new'}) {
	# Show option to re-send info email
	print "<tr> <td><b>",&hlink($text{'user_remail'},"remail"),"</b></td>\n";
	print "<td>",&ui_radio("remail_def", 1,
	  [ [ 1, $text{'user_remail1'} ], [ 0, $text{'user_remail0'} ] ]),"\n",
	  &ui_textbox("remail", $user->{'email'}, 20),"</td> </tr>\n";
	}

# Show forwarding setup for this user
if (($user->{'email'} || $user->{'noprimary'}) && !$user->{'noalias'}) {
	print "<tr> <td colspan=2><hr></td> </tr>\n";
	&alias_form($user->{'to'},
		    &hlink("<b>$text{'user_aliases'}</b>", "userdest"),
		    $d, "user", $in{'user'});
	}

# Show allowed databases
if ($d && !$mailbox) {
	@dbs = grep { $_->{'users'} } &domain_databases($d);
	}
if (@dbs) {
	print "<tr> <td colspan=2><hr></td> </tr>\n";
	@userdbs = map { $_->{'type'}."_".$_->{'name'} } @{$user->{'dbs'}};
	print "<tr> <td valign=top><b>",
	      &hlink($text{'user_dbs'},"userdbs"),"</b></td>\n";
	print "<td>",&ui_select("dbs", \@userdbs,
	    [ map { [ $_->{'type'}."_".$_->{'name'},
		      $_->{'name'}." ($_->{'desc'})" ] }
		  @dbs ], 5, 1),
	    ($user->{'mysql_user'} &&
	     $user->{'mysql_user'} ne $user->{'user'} ?
		"<br>".&text('user_mysqluser',
			     "<tt>$user->{'mysql_user'}</tt>") : ""),
	      "</td> </tr>\n";
	}

# Show secondary groups
@sgroups = &allowed_secondary_groups($d);
if (@sgroups && $user->{'unix'}) {
	print "<tr> <td colspan=2><hr></td> </tr>\n";
	print "<tr> <td valign=top><b>",
	      &hlink($text{'user_groups'},"usergroups"),"</b></td>\n";
	print "<td>",&ui_select("groups", $user->{'secs'},
			[ map { [ $_ ] } @sgroups ], 5, 1, 1),"</td> </tr>\n";
	}

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

print "<table><tr>\n";
if ($in{'new'}) {
	print "<td><input type=submit value='$text{'create'}'></td>\n";
	}
else {
	print "<td><input type=submit value='$text{'save'}'></td>\n";
	print "<td><input type=submit name=delete ",
	      "value='$text{'delete'}' onClick='check_clicks(form)'></td>\n"
		if (!$mailbox);
	}
print "</table></form>\n";

if ($d) {
	if ($single_domain_mode) {
		&ui_print_footer("list_users.cgi?dom=$in{'dom'}", $text{'users_return'},
			"", $text{'index_return2'});
		}
	else {
		&ui_print_footer("list_users.cgi?dom=$in{'dom'}", $text{'users_return'},
			&domain_footer_link($d),
			"", $text{'index_return'});
		}
	}
else {
	&ui_print_footer("", $text{'index_return'});
	}

# quota_field(name, value, used, filesystem)
sub quota_field
{
if (&can_mailbox_quota()) {
	# Show inputs for editing quotas
	local $quota = $_[1];
	$quota = undef if ($quota eq "none");
	printf "<td><input type=radio name=%s_def value=1 %s> %s\n",
		$_[0], $quota ? "" : "checked", $text{'form_unlimit'};
	printf "<input type=radio name=%s_def value=0 %s>\n",
		$_[0], $quota ? "checked" : "";
	print &quota_input($_[0], $quota || "", $_[3]),"\n";
	if (!$in{'new'}) {
		print &text('user_used', &quota_show($_[2], $_[3])),"\n";
		}
	print "</td> </tr>\n";
	}
else {
	# Just show current settings, or default
	print "<td>";
	local $q = $in{'new'} ? $defmquota[0] : $_[1];
	print ($q ? &quota_show($q, $_[3]) : $text{'form_unlimit'}),"\n";
	print &text('user_used', &quota_show($_[2], $_[3])) if (!$in{'new'});
	print "</td> </tr>\n";
	}
}

