# FILE: admin-pl.pl
# DESCRIPTION: Administration function control
#-------------------------------------------------------------------------------
# DISCUS COPYRIGHT NOTICE
#
# Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
#    http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------

use strict;
use vars qw($GLOBAL_OPTIONS $DCONF $PARAMS);

###
### admin_control
###
### Delegates responsibility for all requests coming in to administration
### program.
###

sub admin_control {
	dreq("authpass", "template");
	my $FORMref = {};
	if ($ENV{'CONTENT_TYPE'} =~ m|multipart|) {
		dreq("fcn-mtp");
		$FORMref = parse_multipart($ENV{'CONTENT_LENGTH'});
	} else {
		$FORMref = parse_form($ENV{'QUERY_STRING'}, $ENV{'CONTENT_LENGTH'});
	}
	if ($GLOBAL_OPTIONS->{'admin_actionlog'} == 1 && $FORMref->{username} ne "") {
		admin_action_log($FORMref);
	}
	if ($FORMref->{'action'} eq "" || $FORMref->{'action'} eq "qmenu" || $FORMref->{'action'} eq "menu" || $FORMref->{'action'} eq "first_pass" || $FORMref->{'action'} eq "firstpass") {
		dreq("adm-menu");
		MENU_admin($FORMref);
	}
	if ($FORMref->{action} =~ m|^upgrade|) {
		dreq("upgr31");
		upgrade_version_310($FORMref, undef);
	}
	if ($FORMref->{'action'} =~ m|^acc|) {
		dreq("adm-accs");
		ACCESS_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m|^arch|) {
		dreq("adm-arch-PRO");
		ARCHIVE_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m|^user| || $FORMref->{action} =~ m|^um-|) {
		header();
		dreq("adm-user");
		USER_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m|^pm-|) {
		dreq("adm-page");
		PAGE_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m|^mod_|) {
		dreq("adm-mod");
		MODERATOR_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m|^grp_|) {
		dreq("adm-grp");
		GROUP_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m|^database|) {
		dreq("adm-db");
		DATABASE_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m%^(version|first|quota|logout|color|taskman)%) {
		dreq("adm-misc");
		MISC_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m%^topic%) {
		dreq("adm-tpc");
		TOPIC_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m%^appear%) {
		dreq("adm-appr");
		APPEAR_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m%^data_% || $FORMref->{'action'} =~ m%^dr_%) {
		dreq("adm-dr");
		DR_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m%^log_%) {
		dreq("loganaly-PRO");
		log_analysis_handler($FORMref);
	}
	if ($FORMref->{'action'} =~ m%^options%) {
		dreq("adm-opts");
		OPTS_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m%^queue%) {
		dreq("queue2-PRO");
		QUEUE_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m|^update_|) {
		dreq("adm-upd");
		UPDATE_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m|^schedule_|) {
		dreq("schedule-PRO");
		schedule_mgr_admin_interface($FORMref);
	}
	if ($FORMref->{'action'} =~ m|^back|) {
		dreq("adm-bkup-PRO");
		BACKUP_admin($FORMref);
	}
	if ($FORMref->{'action'} =~ m|^regen(\d+)$|) {
		dreq("fcn-regn");
		REGEN_admin($FORMref);
	}
	if ($FORMref->{action} =~ m|^debug|) {
		dreq("prgdebug");
		program_debug_admin_interface($FORMref);
	}
	if ($FORMref->{action} =~ m|^selfreg|) {
		dreq("selfreg-PRO");
		selfreg_administration($FORMref);
	}
	if ($FORMref->{action} =~ m|^email_admin|) {
		dreq("em-admin-PRO");
		email_admin_administration($FORMref);
	}
	if ($FORMref->{action} eq 'quick') {
		dreq("quickadm-PRO");
		quick_admin_handler($FORMref);
	}
	fatal_error($FORMref);
}

###
### admin_action_log
###
### Keeps a log of everything that everyone did to your administration program
### if you've turned this on in Options Manager.
###

sub admin_action_log {
	my ($FORMref) = @_;
	if (open(LOG, ">>$DCONF->{admin_dir}/msg_index/admin-actionlog.txt")) {
		print LOG "$FORMref->{'username'}\t$ENV{'REMOTE_ADDR'}\t", time, "\t";
		if ($0 =~ m|([\w\-]+)\.$DCONF->{cgi_extension}|) {
			print LOG $1, "\t";
		} else {
			print LOG "?\t";
		}
		foreach my $key (keys(%{$FORMref})) {
			next if $key eq "COOKIE";
			print LOG escape($key), "=";
			if ($key =~ m|^pass|) {
				print LOG escape("*") if $FORMref->{$key} ne "";
				print LOG "BLANK" if $FORMref->{$key} eq "";
			} else {
				print LOG escape($FORMref->{$key});
			}
			print LOG ",";
		}
		print LOG "\n";
		close (LOG);
	} else {
		log_error("src-board-subs-admin", "log_admin_action", "Could not append action log file: $!");
	}
}

###
### fatal_error
###
### Hopefully this will never be seen.  It happens if there is a serious program
### error (mainly if the program falls through the admin_control routine, with a
### malfunctioning function handler).
###

sub fatal_error {
	my ($FORMref) = @_;
	header();
	my $is_pro = "";
	$is_pro = "PRO" if $DCONF->{pro};
	print <<End_Error_Message;
<html><head><title>Internal Program Error</title>
<style>
body { font-family: arial,helvetica }
pre { font-family: courier new; font-size: 11pt; }
</style>
</head>
<body bgcolor=#ffffff text=#000000 link=#0000ff>
<font size=4 color=#ff0000><b>Internal Program Error</b></font><hr>
This error was generated by the Discus program after receiving invalid input or
experiencing an execution error.  If you experienced this error when performing
a normal operation within the Discus program, first check the
<a href=diagnose.$DCONF->{cgi_extension} target=_blank>program diagnostics</a>.
If that does not reveal any error conditions, please contact DiscusWare with the
following information, along with a description of what you were doing when you
received this error message.
<p><pre>
<b>URL to board:</b>   $DCONF->{html_url}
<b>Discus version:</b> $PARAMS->{release}.$PARAMS->{revision} $is_pro
<b>Program running:</b> $0
End_Error_Message
	foreach my $key (keys(%{$FORMref})) {
		print "<FONT COLOR=#0000ff>$key</FONT>=<FONT COLOR=#ff0000><B>$FORMref->{$key}</B></FONT>\n";
	}
	print "</pre></body></html>\n";
	program_exit(0);
}

###
### bad_login
###
### Complaint if someone shouldn't be here
###

sub bad_login {
	my ($arg) = @_;
	my $subst = {};
	$subst->{general}->{reason} = 0 if $arg->{bad_menu_username};
	$subst->{general}->{reason} = 1 if $arg->{username} || $arg->{bad_username};
	$subst->{general}->{reason} = 2 if $arg->{bad_database};
	$subst->{general}->{reason} = 3 if $arg->{bad_backup};
	$subst->{general}->{reason} = 4 if $arg->{superuser} || $arg->{superuser_required};
	$subst->{general}->{reason} = 5 if $arg->{privilege};
	$subst->{general}->{reason} = 6 if $arg->{group_privilege};
	screen_out("badlogin", $subst);
}

###
### admin_error
###
### Miscellaneous Error Messages
###

sub admin_error {
	my ($code) = @_;
	undef my $subst;
	$subst->{c}->{e} = $code;
	screen_out("admerror", $subst);
}

1;
