# FILE: src-board-subs-102
#-------------------------------------------------------------------------------
# DISCUS VERSION 3.10 COPYRIGHT NOTICE
#
# Discus 3.10 is copyright (c) 2000 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.
#-------------------------------------------------------------------------------
#
# --------------------------------------------------------------------------------
# ATTENTION!  THIS FILE IS PART OF THE DISCUS PRO DISTRIBUTION AND IS COMMERCIAL
# SOFTWARE.  REDISTRIBUTION OF THIS FILE IS STRICTLY PROHIBITED AND WILL RESULT IN
# PROSECUTION TO THE MAXIMUM EXTENT OF THE LAW.  YOU ARE NOT PERMITTED TO REMOVE
# THIS NOTICE UNDER ANY CIRCUMSTANCES.
# --------------------------------------------------------------------------------

#---SEPARATOR---#

sub define_mime_types {
	undef %MIME_icon; undef %MIME_extension;
	if (open(ATTACH, "$admin_dir/attachments.conf")) {
		@attach = <ATTACH>;
		close (ATTACH);
		$flag = 0;
		foreach $line (@attach) {
			next if $line =~ m|^#|;
			next if $line !~ m|\S|;
			if ($line =~ m|<Recognized>|i) {
				$flag = 1;
			} elsif ($line =~ m|</Recognized>|i) {
				$flag = 0;
			} elsif ($line =~ m|<Valid>|i) {
				$flag = 2;
			} elsif ($line =~ m|</Valid>|i) {
				$flag = 0;
			} elsif ($line =~ m|<SSI>|i) {
				$flag = 3;
			} elsif ($line =~ m|</SSI>|i) {
				$flag = 0;
			} elsif ($flag == 1) {
				if ($line =~ m|(\S+)\s+(\S+)\s+(\S+)|) {
					$MIME_icon{$1} = $2;
					$MIME_ext{$1} = $3 if $3 ne "";
				}
			} elsif ($flag == 2) {
				$line =~ s/\W//g;
				push (@valid_ext, $line) if $line =~ /\S/;
			} elsif ($flag == 3) {
				$line =~ s/\W//g;
				push (@remove_ssis, $line) if $line =~ /\S/;
			}
		}
	} else {
		&error_message("Attachment Configuration Error", "Could not read attachment configuration file.");
	}

	# If the extension is not recognized, this is the extension that should be used.
	# We recommend 'unk' because this will not be defined on most servers or browsers.
	# If your server does not properly handle this (like if it gives plain text) then
	# you will need to either reconfigure your server or change this to something like
	# 'zip' which will be downloaded to the user.  The following line in your srm.conf
	# file (Apache or NCSA) will prompt the user for what to do with the file.
   #
	# SRM.CONF -- ADD THE FOLLOWING LINE:
	#  AddType application/x-unknown .unk

	$default_MIME_extension = "unk";

}

#---SEPARATOR---#

sub parse_filename {
	my ($line_in) = @_;
	if ($line_in =~ m|^(\w+):\\|) {
		$line_in =~ m|(.*)\\|;
		return $';
	}
	if ($line_in =~ m|^/|) {
		$line_in =~ m|(.*)/|;
		return $';
	}
	if ($line_in =~ m|:|) {
		$line_in =~ m|(.*):|;
		return $';
	}
	return $line_in;
}

#---SEPARATOR---#
#REQ:define_mime_types
#REQ:get_number

sub attachment_upload {
	my ($srcline, $topic_number, $queue, $poster_type, $poster_binary) = @_;
	&define_mime_types;
	while ($srcline =~ m|\\attachment_notuploaded\{(\d+),([^\}]*)\}|) {
		($counter, $description, $before, $after) = ($1, $2, $`, $');
		$descr{$counter} = $description;
		$FORMNAME{$counter} = "Attachment$counter";
		push (@statusorder, $counter);
		$formname = "Attachment$counter"; $content = $CONTENT{$formname};
		if ($FILE{$formname} eq "") {
			$status{$counter} = $FILENAME{$formname};
			$statuscode{$counter} = 2;
			$srcline = join("", $before, "\\attachment_notuploaded*{$counter,$description}", $after);
		} elsif (substr($poster_binary, 1, 1) eq "1" && length($FILE{$formname}) > ($GLOBAL_OPTIONS{'registered_mod_maxsize'} * 1000) && $GLOBAL_OPTIONS{'registered_mod_maxsize'} != 0) {
			$statuscode{$counter} = 3;
			$srcline = join("", $before, "\\attachment_notuploaded*{$counter,$description}", $after);
		} elsif (substr($poster_binary, 0, 1) eq "1" && substr($poster_binary, 1, 1) eq "0" && length($FILE{$formname}) > ($GLOBAL_OPTIONS{'registered_maxsize'} * 1000) && $GLOBAL_OPTIONS{'registered_maxsize'} != 0) {
			$statuscode{$counter} = 3;
			$srcline = join("", $before, "\\attachment_notuploaded*{$counter,$description}", $after);
		} elsif (substr($poster_binary, 2, 2) =~ m|1| && substr($poster_binary, 0, 2) !~ m|1| && length($FILE{$formname}) > ($GLOBAL_OPTIONS{'public_maxsize'} * 1000) && $GLOBAL_OPTIONS{'public_maxsize'} != 0) {
			$statuscode{$counter} = 3;
			$srcline = join("", $before, "\\attachment_notuploaded*{$counter,$description}", $after);
		} else {
			$newtag = "attachment_alreadyuploaded";
			$file = $FILENAME{$formname};
			$file =~ s/[^\w\-\.\s]//g;
			$file =~ s/\s/_/g;
			$file =~ tr/A-Z/a-z/;
			if ($file =~ m|(.*)\.(\w+)|) {
				($name, $extension) = ($1, $2);
			} else {
				$name = $file; $extension = "";
			}
			if (length($name) > 31) {
				$name = substr($name, 0, 31);
			}
			if (length($extension) > 5) {
				$extension = substr($extension, 0, 5);
			}
			$file_disp = $file;
			$c = $CONTENT{$formname};
			if ($MIME_ext{$c} ne "") {
				$file = "$name.$MIME_ext{$c}"; $extension = $MIME_ext{$c};
				$file_disp = $file;
			} elsif (!grep(/^$extension$/, @valid_ext)) {
				if ($extension ne "") {
					$file = "$name" . "_" . "$extension.$default_MIME_extension";
				} else {
					$file = "$name.$default_MIME_extension";
				}
				$extension = $default_MIME_extension;
			}
			if (grep(/^$extension$/, @remove_ssis)) {
				$FILE{$formname} =~ s/<!--#(\w*)(\s*)(\w*)(\s*)=(\s*)"([^"]*)"(.*)-->//g;
			}
			$filename = "$message_dir/$topic_number/$file" if (!$queue && -e "$message_dir/$topic_number");
			$filename = "$secdir/$topic_number/$file" if (!$queue && !-e "$message_dir/$topic_number");
			$filename = "$admin_dir/queue/$file" if $queue;
			if (-e "$admin_dir/queue/$file" || -e "$secdir/$topic_number/$file" || -e "$message_dir/$topic_number/$file") {
				($num) = &get_number;
				$file = "$num" . "_$file";
				$filename = "$message_dir/$topic_number/$file" if (!$queue && -e "$message_dir/$topic_number");
				$filename = "$secdir/$topic_number/$file" if (!$queue && !-e "$message_dir/$topic_number");
				$filename = "$admin_dir/queue/$file" if $queue;
			}
			if ($MIME_icon{$c}) {
				$icon = $MIME_icon{$c};
			} else {
				$icon = "attachment_icon.gif";
			}
			push (@newattach, $file);
			$length = length($FILE{$formname}); $length /= 1000;
			if ($length =~ m|\.|) {
				$length = $`;
			}
			if ($length == 0) {
				$length = "<1";
			}
			$description =~ s/,//g;
			$srcline = join("", $before, "\\", $newtag, "{$file,$description,$icon,$length,$file_disp,$c}", $after);
			open (FILE, ">$filename");
			binmode(FILE);
			print FILE $FILE{$formname};
			close (FILE);
			chmod(oct($perms0644), $filename);
			$status{$counter} = length($FILE{$formname});
			$statuscode{$counter} = 0;
		}
	}
	while ($srcline =~ m|\\attachment_notuploaded\*\{(\d+),([^\}]*)\}|) {
		($counter, $description, $before, $after) = ($1, $2, $`, $');
		$srcline = join("", $before, "\\attachment_notuploaded{$counter,$description}", $after);
	}
	return $srcline;
}

#---SEPARATOR---#

sub manage_attachments {
	my ($source_in, $topic_number, $queue, $action) = @_;
	my (@file);
	while ($source_in =~ m|\\attachment_alreadyuploaded\{([^,]+)|) {
		push (@file, "$1");
		$source_in = join("", $`, $');
	}
	if ($queue != 0) {
		$dir = "";
	} else {
		if (-e "$message_dir/$topic_number") {
			$dir = "$message_dir/$topic_number";
		} else {
			$dir = "$secdir/$topic_number";
		}
	}
	if ($action eq "delete") {
		foreach $line (@file) {
			(unlink("$dir/$line"));
		}
	} elsif ($action eq "list") {
		foreach $line (@file) {
			$line = "$dir/$line";
		}
		return @file;
	}
}

#---SEPARATOR---#

sub email_list {
	my ($param) = @_;
	&header;
	print "<HTML><HEAD><TITLE>E-mail List</TITLE>\n";
	print '<BODY BGCOLOR="ffffff" TEXT="000000" LINK="0000FF" VLINK="0000FF" ';
	print "onLoad=\"window.defaultStatus = 'E-mail List'\">$fs\n";
	print "<FONT SIZE=3><CENTER><B>E-mail List</B></CENTER></FONT>\n";
	print "<HR>\n";
	print "<B><A HREF=\"$cgiurlm?cmd=options_mgr&username=$superuser\" onMouseOver=\"";
	print "window.status = 'Return to the Options Manager'; return true\">Options Manager";
	print "</A>: E-mail List</B>\n<HR>\n";
	undef @addresses; undef %seen;
	open (USERS, "$admin_dir/users.txt"); @users = <USERS>; close (USERS);
	open (PASSWD, "$admin_dir/passwd.txt"); @passwd = <PASSWD>; close (PASSWD);
	if ($param == 1) {
		@array = @users;
	} elsif ($param == 2) {
		@array = @passwd;
	} elsif ($param == 3) {
		@array = @users;
		foreach $line (@passwd) {
			push (@array, $line);
		}
	}
	foreach $line (@array) {
		($user, $pass, $email) = split(/:/, $line);
		next if $seen{$email};
		if ($email =~ m|^([\w\+\-\.]+)\@([\w\+\-\.]+)$|) {
			$seen{$email} = 1;
			push (@addresses, $email);
		}
	}
	if (scalar(@addresses) == 0) {
		print "Sorry, there are no addresses with the parameters you specified.\n";
	} else {
		print "<TABLE BORDER=1 WIDTH=100% BGCOLOR=#ffffcc><TR><TD>$fs";
		print "The following text box contains a list of e-mail addresses, one per\n";
		print "line, that were requested.  This can be copied into most mail programs\n";
		print "as a mailing list.<P>\n";
		print "<FORM><TABLE BORDER=1 BGCOLOR=#ffffcc><TR><TD>";
		if (scalar(@addresses) <= 500) {
			print "<TEXTAREA ROWS=10 COLS=50>";
		} else {
			print "<PRE>";
		}
		foreach $line (sort(@addresses)) {
			print "$line\n";
		}
		if (scalar(@addresses) <= 500) {
			print "</TEXTAREA>";
		} else {
			print "</PRE>";
		}
		print "</TD></TR></TABLE></FORM></TD></TR></TABLE>\n";
	}
	print "</BODY></HTML>\n";
	exit(0);
}

#---SEPARATOR---#

sub access_mgr_2_pro {
	my ($topic, $username) = @_;
	if (!-w "$admin_dir/posting.txt") {
		&error_message("Save Settings Error", "The posting.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then reload this page.", 0, 1);
	}
	if (!-w "$admin_dir/queue.txt") {
		&error_message("Save Settings Error", "The queue.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then reload this page.", 0, 1);
	}
	if (!-w "$admin_dir/security.txt") {
		&error_message("Save Settings Error", "The security.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then reload this page.", 0, 1);
	}
	if (!-w "$admin_dir/postoptions.txt") {
		&error_message("Save Settings Error", "The postoptions.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then reload this page.", 0, 1);
	}
	my (@read, @post, $rl, $pl);
	&extract("//$topic/$topic.$ext");
	open (SECURITY, "$admin_dir/security.txt"); @post = <SECURITY>; close (SECURITY);
	($pl{'reading'}) = grep(/^$topic:/, @post);
	if ($pl{'reading'} eq "") {
		&lock("access_mgr_2_pro", "$admin_dir/security.txt");
		open (SECURITY, ">>$admin_dir/security.txt");
		print SECURITY "$topic:~:::\n";
		close (SECURITY);
		&unlock("access_mgr_2_pro", "$admin_dir/security.txt");
		$pl{'reading'} = "$topic:~:::\n";
	}
	open (SECURITY, "$admin_dir/posting.txt"); @post = <SECURITY>; close (SECURITY);
	($pl{'posting'}) = grep(/^$topic:/, @post);
	if ($pl{'posting'} eq "") {
		&lock("access_mgr_2_pro", "$admin_dir/posting.txt");
		open (SECURITY, ">>$admin_dir/posting.txt");
		print SECURITY "$topic:~:::\n";
		close (SECURITY);
		&unlock("access_mgr_2_pro", "$admin_dir/posting.txt");
		$pl{'posting'} = "$topic:~:::\n";
	}
	open (SECURITY, "$admin_dir/postoptions.txt");
	@options = <SECURITY>;
	close (SECURITY);
	($ps) = grep(/^$topic:/, @options);
	if ($ps eq "") {
		&lock("access_mgr_2_pro", "$admin_dir/postoptions.txt");
		open (SECURITY, ">>$admin_dir/postoptions.txt");
		print SECURITY "$topic:0:0:\n";
		close (SECURITY);
		&unlock("access_mgr_2_pro", "$admin_dir/postoptions.txt");
		$ps = "$topic:0:0:\n";
		&log_error("src-board-subs-102", "access_mgr_2_pro", "Post index option for $topic is not in file! [$ps]");
	}
	$POST_OPTIONS = $ps;
	open (GROUPS, "$admin_dir/groups.txt"); @grp = <GROUPS>; close (GROUPS);
	foreach $grp (@grp) {
		$grp =~ m|^(\w+):|; $grp = $1;
	}
	&header;
	print "<HTML><HEAD><TITLE>Access Manager</TITLE>\n";
	print "<SCRIPT LANGUAGE=\"JavaScript\"> <!-- Hide\n";
	print "function setStatus(msg) {\n";
	print "  window.status=msg\n";
	print "  return true\n";
	print "}\n";
	print "//-->\n";
	print "</SCRIPT></HEAD>\n";
	print '<BODY BGCOLOR="ffffff" TEXT="000000" LINK="0000FF" VLINK="0000FF" ';
	print "onLoad=\"window.defaultStatus = 'Access Editor: ";
	print &JavaScript_prepare($topic_name);
	print "'\">$fs\n";
	print "<FONT SIZE=3><CENTER><B>Access Editor: $topic_name</B></CENTER></FONT>\n";
	print "<HR><B><A HREF=$cgiurlm?action=access_mgr_1&username=$username onMouseOver=\"return setStatus('Return to topic selection screen')\">Access Manager</A>:\n";
	print "Access Editor</B>\n";
	print "<HR>You can edit <A HREF=#posting>Posting Privileges</A> and <A HREF=#reading>Reading Privileges</A>.\n";
	foreach $w ('posting', 'reading') {
		$doit = "document.$w.acc[2].checked = true; ";
		$pl{$w} =~ s/\s+$//;
		($topic, $ip, $usrgrp, $modgrp, $pass) = split(/:/, $pl{$w});
		print "<HR><A NAME=$w><H3>\u$w Privileges</H3>";
		print "<FORM ACTION=\"$cgiurl\" METHOD=POST NAME=\"$w\">\n";
		print "<INPUT TYPE=HIDDEN NAME=topic VALUE=$topic_number>\n";
		print "<INPUT TYPE=HIDDEN NAME=username VALUE=$username>\n";
		print "<INPUT TYPE=HIDDEN NAME=action VALUE=access_edit_pro>\n";
		print "<INPUT TYPE=HIDDEN NAME=privs VALUE=$w>\n";
		print "<TABLE BGCOLOR=#ffffcc BORDER=1 WIDTH=100%><TR><TD>$fs\n";
		print "<INPUT TYPE=RADIO NAME=\"acc\" VALUE=dis ";
		print "CHECKED" if ($ip eq "" && $usrgrp eq "" && $modgrp eq "" && $pass eq "");
		print " onClick=\"document.$w.modgrpbox.checked = false; document.$w.modgrpbased[0].checked = 0; document.$w.modgrpbased[1].checked = 0; document.$w.modgrpbased[2].checked = 0; ";
		print "document.$w.usrgrpbox.checked = 0; document.$w.usrgrpbased[0].checked = 0; document.$w.usrgrpbased[1].checked = 0; document.$w.usrgrpbased[2].checked = 0; ";
		print "document.$w.globalbox.checked = 0; document.$w.IPbox.checked = 0;\"";
		print "><B>All \u$w Disabled</B><P>\n";
		print "<INPUT TYPE=RADIO NAME=\"acc\" VALUE=pub ";
		print "CHECKED" if ($ip eq "~" && $usrgrp eq "" && $modgrp eq "" && $pass eq "");
		print " onClick=\"document.$w.modgrpbox.checked = false; document.$w.modgrpbased[0].checked = 0; document.$w.modgrpbased[1].checked = 0; document.$w.modgrpbased[2].checked = 0; ";
		print "document.$w.usrgrpbox.checked = 0; document.$w.usrgrpbased[0].checked = 0; document.$w.usrgrpbased[1].checked = 0; document.$w.usrgrpbased[2].checked = 0; ";
		print "document.$w.globalbox.checked = 0; document.$w.IPbox.checked = 0;\"";
		print "><B>Public \u$w (No Restrictions)</B><P>\n";
		print "<INPUT TYPE=RADIO NAME=\"acc\" VALUE=res ";
		print "CHECKED" if (($ip ne "" && $ip ne "~") || $usrgrp ne "" || $modgrp ne "" || $pass ne "");
		print "><B>Restricted \u$w:</B><BR>\n";
		print "&nbsp;&nbsp;&nbsp;";
		print "<INPUT TYPE=CHECKBOX NAME=modgrpbox VALUE=1 ";
		print "CHECKED " if $modgrp ne "";
		print "onClick=\"$doit if (document.$w.modgrpbox.checked == 0) {document.$w.modgrpbased[0].checked = 0; document.$w.modgrpbased[1].checked = 0; document.$w.modgrpbased[2].checked = 0; document.$w.acc[2].checked = 1;}\"";
		print "> Group-based access for moderators:<BR>\n";
		print "&nbsp;&nbsp;&nbsp;";
		print "&nbsp;&nbsp;&nbsp;";
		print "<INPUT TYPE=RADIO NAME=modgrpbased VALUE=1 ";
		print "CHECKED " if $modgrp eq $owner;
		print "onClick=\"$doit if (document.$w.modgrpbased[0].checked != 0) {document.$w.modgrpbox.checked = true; document.$w.acc[2].checked = 1;}\"";
		print "> Moderators in this group (\u$owner)<BR>\n";
		print "&nbsp;&nbsp;&nbsp;";
		print "&nbsp;&nbsp;&nbsp;";
		print "<INPUT TYPE=RADIO NAME=modgrpbased VALUE=2 ";
		print "CHECKED " if $modgrp eq "~";
		print "onClick=\"$doit if (document.$w.modgrpbased[1].checked != 0) {document.$w.modgrpbox.checked = true; document.$w.acc[2].checked = 1;}\"";
		print "> Moderators in all groups<BR>\n";
		print "&nbsp;&nbsp;&nbsp;";
		print "&nbsp;&nbsp;&nbsp;";
		print "<INPUT TYPE=RADIO NAME=modgrpbased VALUE=3 ";
		print "CHECKED " if ($modgrp ne $owner && $modgrp ne "~" && $modgrp ne "");
		print "onClick=\"$doit if (document.$w.modgrpbased[2].checked != 0) {document.$w.modgrpbox.checked = true; document.$w.acc[2].checked = 1;}\"";
		print "> Moderators in selected groups ";
		print "(<A HREF=\"$cgiurl?username=$username&action=access_pro&word=$w&do=mod&topic=$topic\">Configure</A>)";
		print "<P>\n";
		print "&nbsp;&nbsp;&nbsp;";
		print "<INPUT TYPE=CHECKBOX NAME=usrgrpbox VALUE=1 ";
		print "CHECKED " if $usrgrp ne "";
		print "onClick=\"$doit if (document.$w.usrgrpbox.checked == 0) {document.$w.usrgrpbased[0].checked = 0; document.$w.usrgrpbased[1].checked = 0; document.$w.usrgrpbased[2].checked = 0; document.$w.acc[2].checked = 1;}\"";
		print "> Group-based access for users:<BR>\n";
		print "&nbsp;&nbsp;&nbsp;";
		print "&nbsp;&nbsp;&nbsp;";
		print "<INPUT TYPE=RADIO NAME=usrgrpbased VALUE=1 ";
		print "CHECKED " if $usrgrp eq $owner;
		print "onClick=\"$doit if (document.$w.usrgrpbased[0].checked != 0) {document.$w.usrgrpbox.checked = true; document.$w.acc[2].checked = 1;}\"";
		print "> Users in this group (\u$owner)<BR>\n";
		print "&nbsp;&nbsp;&nbsp;";
		print "&nbsp;&nbsp;&nbsp;";
		print "<INPUT TYPE=RADIO NAME=usrgrpbased VALUE=2 ";
		print "CHECKED " if $usrgrp eq "~";
		print "onClick=\"$doit if (document.$w.usrgrpbased[1].checked != 0) {document.$w.usrgrpbox.checked = true; document.$w.acc[2].checked = 1;}\"";
		print "> Users in all groups<BR>\n";
		print "&nbsp;&nbsp;&nbsp;";
		print "&nbsp;&nbsp;&nbsp;";
		print "<INPUT TYPE=RADIO NAME=usrgrpbased VALUE=3 ";
		print "CHECKED " if ($usrgrp ne $owner && $usrgrp ne "~" && $usrgrp ne "");
		print "onClick=\"$doit if (document.$w.usrgrpbased[2].checked != 0) {document.$w.usrgrpbox.checked = true; document.$w.acc[2].checked = 1;}\"";
		print "> Users in selected groups ";
		print "(<A HREF=\"$cgiurl?username=$username&action=access_pro&word=$w&do=usr&topic=$topic\">Configure</A>)";
		print "<P>\n";
		print "&nbsp;&nbsp;&nbsp;";
		print "<INPUT TYPE=CHECKBOX NAME=globalbox VALUE=1 ";
		print "CHECKED " if $pass =~ /\S/;
		print "onClick=\"$doit\"" if $doit ne "";
		print "> Global password access (common password) \n";
		print "(<A HREF=\"$cgiurl?username=$username&action=access_pro&word=$w&do=pw&topic=$topic\">Configure</A>)";
		if ($FORM{'globalbox'} && $FORM{'privs'} eq $w && $pass !~ /\S/ && $FORM{'acc'} ne "pub") {
			print "<BR><FONT SIZE=1 COLOR=#ff0000>You tried to enable the global password setting without specifying a global password.\n";
			print "Please click the \"configure\" link above to set up the global password.</FONT>\n";
		}
		print "<P>\n";
		print "&nbsp;&nbsp;&nbsp;";
		print "<INPUT TYPE=CHECKBOX NAME=IPbox VALUE=1 ";
		print "CHECKED " if ($ip ne "~" && $ip ne "");
		print "onClick=\"$doit\"" if $doit ne "";
		print "> IP-based authentication/rejection (<A HREF=\"$cgiurl?username=$username&action=access_pro&word=$w&do=IP&topic=$topic\">Configure</A>)\n";
		if ($FORM{'IPbox'} && $FORM{'privs'} eq $w && ($ip eq "~" || $ip eq "") && $FORM{'acc'} ne "pub") {
			print "<BR><FONT SIZE=1 COLOR=#ff0000>You tried to enable the IP based settings without specifying the IP addresses.\n";
			print "Please click the \"configure\" link above to set up the IP addresses.</FONT>\n";
		}
		if ($w eq "posting") {
			$POST_OPTIONS =~ s/\s+$//;
			($topic, $anon, $full, $email) = split(/:/, $POST_OPTIONS);
			print "<P>\n";
			print "<B>Additional Settings:</B><BR>\n";
			print "<input type=hidden name=save_options value=1>\n";
			print "&nbsp;&nbsp;&nbsp;";
			print "<INPUT TYPE=CHECKBOX NAME=anon VALUE=1 ";
			print "CHECKED" if $anon;
			print "> Disable \"Anonymous\" posts<BR>\n";
			print "&nbsp;&nbsp;&nbsp;";
			print "<INPUT TYPE=CHECKBOX NAME=full VALUE=1 ";
			print "CHECKED" if $full;
			print "> Disable full names on posts<BR>\n";
			print "&nbsp;&nbsp;&nbsp;";
			print "<INPUT TYPE=CHECKBOX NAME=email VALUE=1 ";
			print "CHECKED" if $email;
			print "> Disable e-mail addresses / links to profiles posts<BR>\n";
			open (MOD, "$admin_dir/queue.txt"); @q = <MOD>; close (MOD);
			($line) = grep(/^$topic_number:/, @q);
			$line =~ m|^(\d+):(\d+):(\d+):(\d+):(\d+)|;
			($user, $moderator, $special, $public) = ($2, $3, $4, $5);
			print "&nbsp;&nbsp;&nbsp;";
			print "<INPUT TYPE=CHECKBOX NAME=queue VALUE=1 ";
			print "CHECKED" if ($user+$moderator+$special+$public);
			print "> Use Message Queue (\"Moderated posting\")\n";
			print "(<A HREF=\"$cgiurl?username=$username&action=access_pro&word=$w&do=Q&topic=$topic\">Configure</A>)";
		}
		print "<P>\n";
		print "<INPUT TYPE=SUBMIT VALUE=\"Save Settings\">\n";
		print "<INPUT TYPE=RESET VALUE=\"Revert to Saved\">\n";
		print "</TD></TR></TABLE>\n";
		print "</FORM>\n";
	}
	print "</FONT></BODY></HTML>\n";
	exit(0);
}

#---SEPARATOR---#

sub access_edit_configure {
	my ($word, $do) = @_;
	if (!-w "$admin_dir/posting.txt") {
		&error_message("Save Settings Error", "The posting.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then re-attempt to save the settings.", 0, 1);
	}
	if (!-w "$admin_dir/queue.txt") {
		&error_message("Save Settings Error", "The queue.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then re-attempt to save the settings.", 0, 1);
	}
	if (!-w "$admin_dir/security.txt") {
		&error_message("Save Settings Error", "The security.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then re-attempt to save the settings.", 0, 1);
	}
	if (!-w "$admin_dir/postoptions.txt") {
		&error_message("Save Settings Error", "The postoptions.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then re-attempt to save the settings.", 0, 1);
	}

	&header;
	print "<HTML><HEAD><TITLE>Access Manager</TITLE>\n";
	print "<SCRIPT LANGUAGE=\"JavaScript\"> <!-- Hide\n";
	print "function setStatus(msg) {\n";
	print "  window.status=msg\n";
	print "  return true\n";
	print "}\n";
	print "//-->\n";
	print "</SCRIPT></HEAD>\n";
	print '<BODY BGCOLOR="ffffff" TEXT="000000" LINK="0000FF" VLINK="0000FF" ';
	print "onLoad=\"window.defaultStatus = 'Access Editor'\">$fs\n";
	print "<FONT SIZE=3><CENTER><B>Advanced Access Editor: $topic_name</B></CENTER></FONT>\n";
	print "<HR><B><A HREF=$cgiurlm?action=access_mgr_1&username=$username onMouseOver=\"return setStatus('Return to topic selection screen')\">Access Manager</A>:\n";
	print "<A HREF=$cgiurlm?action=access_mgr_2&username=$username&HTTP_REFERER=//$topic_number/$topic_number.$ext onMouseOver=\"return setStatus('Access Editor')\">Access Editor</A>:\n";
	print "Advanced Access Editor</B>\n";
	print "<HR><FORM ACTION=$cgiurl METHOD=POST>\n";
	print "<INPUT TYPE=HIDDEN NAME=topic VALUE=$topic_number>\n";
	print "<INPUT TYPE=HIDDEN NAME=username VALUE=$username>\n";
	print "<INPUT TYPE=HIDDEN NAME=action VALUE=access_edit_advanced>\n";
	print "<INPUT TYPE=HIDDEN NAME=privtype VALUE=$word>\n";
	print "<INPUT TYPE=HIDDEN NAME=do VALUE=$do>\n";
	print "<H3>\u$word Privileges</H3>\n";
	print "<TABLE BGCOLOR=#ffffcc BORDER=1 WIDTH=100%><TR><TD>$fs\n";
	open (SECURITY, "$admin_dir/security.txt"); @post = <SECURITY>; close (SECURITY);
	($pl{'reading'}) = grep(/^$topic_number:/, @post);
	open (SECURITY, "$admin_dir/posting.txt"); @post = <SECURITY>; close (SECURITY);
	($pl{'posting'}) = grep(/^$topic_number:/, @post);
	($topic_number, $ip, $usrgrp, $modgrp, $pass) = split(/:/, $pl{$word});
	&ip_restrictions($word) if $do eq "IP";
	&groups($word, $do) if ($do eq "mod" || $do eq "usr");
	&globalpw if $do eq "pw";
	&queue if $do eq "Q";
	print "</TD></TR></TABLE>\n";
	print "</FORM>\n";
	print "</BODY></HTML>\n";
	exit(0);
}

sub queue {
	print "<B><U>Message Queue</U></B><BR>\n";
	print "(Messages must be approved by a moderator prior to being posted)<P>\n";
	print "<TABLE WIDTH=100%><TR><TD WIDTH=50% VALIGN=TOP>\n";
	open (MOD, "$admin_dir/queue.txt"); @q = <MOD>; close (MOD);
	($line) = grep(/^$topic_number:/, @q);
	$line =~ m|^(\d+):(\d+):(\d+):(\d+):(\d+)|;
	($user, $moderator, $special, $public) = ($2, $3, $4, $5);
	print "$fs<INPUT TYPE=CHECKBOX NAME=QUEUEuser VALUE=1";
	print " CHECKED" if $user;
	print "> Posts by registered users<P>\n";
	print "<INPUT TYPE=CHECKBOX NAME=QUEUEmod VALUE=1";
	print " CHECKED" if $moderator;
	print "> Posts by moderators</TD>\n";
	print "<TD WIDTH=50% VALIGN=TOP>\n";
	print "$fs<INPUT TYPE=CHECKBOX NAME=QUEUEspec VALUE=1";
	print " CHECKED" if $special;
	print "> Posts with global password<P>\n";
	print "<INPUT TYPE=CHECKBOX NAME=QUEUEpublic VALUE=1";
	print " CHECKED" if $public;
	print "> Posts by the public</TD>\n";
	print "</TR></TABLE><P>\n";
	print "<P><INPUT TYPE=SUBMIT VALUE=\"Save Settings\">\n";
}

sub globalpw {
	print "<B><U>Grant access via a common, &quot;global&quot; password</U></B><P>\n";
	print "<INPUT TYPE=RADIO NAME=globalpw VALUE=off ";
	print "CHECKED" if $pass !~ /\S/;
	print "> This option is disabled<BR>\n";
	print "<INPUT TYPE=RADIO NAME=globalpw VALUE=new> Use this new password:\n";
	print "<UL>\n";
	print "<TABLE><TR><TD>$fs", "New Password:</TD><TD><INPUT TYPE=PASSWORD NAME=pass_1 VALUE='' SIZE=15 onChange=\"document.forms[0].globalpw[1].checked = true\"></TD></TR>\n";
	print "<TR><TD>$fs", "Verify New Password:</TD><TD><INPUT TYPE=PASSWORD NAME=pass_2 VALUE='' SIZE=15 onChange=\"document.forms[0].globalpw[1].checked = true\"></TD></TR>\n";
	print "</TABLE></UL>\n";
	if ($pass =~ /\S/) {
		print "<INPUT TYPE=RADIO NAME=globalpw VALUE=current ";
		print "CHECKED" if $pass =~ /\S/;
		print "> Use current password<BR>\n";
	}

	print "<P><INPUT TYPE=SUBMIT VALUE=\"Save Settings\">\n";
}

sub groups {
	my ($word, $do) = @_;
	@usrgrp = split(/,/, $usrgrp) if $do eq "usr";
	@usrgrp = split(/,/, $modgrp) if $do eq "mod";
	open (GROUPS, "$admin_dir/groups.txt"); @grp = <GROUPS>; close (GROUPS);
	foreach $grp (@grp) {
		$grp =~ m|^(\w+):|; $grp = $1;
	}
	print "<B><U>Grant access to ";
	print "users" if $do eq "usr";
	print "moderators" if $do eq "mod";
	print " by group</U></B><BR>\n";
	print "<FONT COLOR=#ff0000>*</FONT> Denotes the group that owns this topic.\n";
	print "<P>\n";
	$c = 0;
	print "<TABLE WIDTH=100%>\n";
	foreach $group (sort(@grp)) {
		print "<TR>" if ($c == 0);
		print "<TD WIDTH=33%>$fs<INPUT TYPE=CHECKBOX NAME=usergroup VALUE=$group ";
		print "CHECKED " if (grep(/^$group$/, @usrgrp) || grep(/^~$/, @usrgrp));
		print "> \u$group";
		print "<FONT COLOR=#ff0000>*</FONT>" if $group eq $owner;
		print "</FONT></TD>\n";
		print "</TR>" if $c == 2;
		$c += 1; $c = 0 if $c >= 3;
	}
	print "<TR>" if ($c == 0);
	print "<TD WIDTH=33%>$fs<INPUT TYPE=CHECKBOX NAME=usergroup VALUE=ALL ";
	print "CHECKED " if grep(/^~$/, @usrgrp);
	print "> <B>All&nbsp;Groups</B></FONT></TD>\n";
	print "</TR>" if $c == 2;
	$c += 1; $c = 0 if $c >= 3;
	if ($c != 0) {
		while ($c < 3) {
			print "<TD WIDTH=33%>&nbsp;</TD>\n";
			$c += 1;
		}
	}
	print "</TR></TABLE><P>\n";
	print "<INPUT TYPE=SUBMIT VALUE=\"Save Settings\"><P>\n";
}

sub ip_restrictions {
	my ($word) = @_;
	print "<B><U>Configure IP Restrictions</U></B><P>\n";
	print "<P>\u$word by the public is <FONT COLOR=ff0000><B>expressly forbidden</B></FONT> from the following IP addresses (you may use '*' in your entries as a wildcard character to match any number of any character).  Settings in this section will override any permitted settings in the second list.<P>\n";
	print "<TABLE WIDTH=100%>\n";
	@ip = split(/,/, $ip);
	foreach $ip4 (@ip) {
		if ($ip4 =~ m|^!|) {
			$ip2 = $';
			$ip2 =~ s/~/\*/g;
			print "<TR><TD><INPUT TYPE=CHECKBOX NAME=bannedIP VALUE='$ip2' CHECKED> $fs$ip2</FONT></TD></TR>\n";
		}
	}
	print "<TR><TD><INPUT TYPE=CHECKBOX NAME=bannedIPnew VALUE=1> <INPUT TYPE=TEXT NAME=bannedIPtext SIZE=25 VALUE='' onChange='document.forms[0].bannedIPnew.checked = true'></TD></TR>\n";
	print "</TABLE><P>\n";
	print "\u$word by the public is <FONT COLOR=00aa00><B>permitted</B></FONT> from the following IP addresses (you may use '*' in your entries as a wildcard character to match any number of any character).  Note that if you make entries to this permitted list, IP addresses <I>not</I> matching any of the patterns below will be denied access.<P>\n";
	print "<TABLE WIDTH=100%>\n";
	foreach $ip4 (@ip) {
		if ($ip4 !~ m|^!|) {
			$ip4 =~ s/~/\*/g;
			print "<TR><TD><INPUT TYPE=CHECKBOX NAME=allowedIP VALUE='$ip4' CHECKED> $fs$ip4</FONT></TD></TR>\n";
		}
	}
	print "<TR><TD><INPUT TYPE=CHECKBOX NAME=allowedIPnew VALUE=1> <INPUT TYPE=TEXT NAME=allowedIPtext SIZE=25 VALUE='' onChange='document.forms[0].allowedIPnew.checked = true'></TD></TR>\n";
	print "</TABLE><P>";
	print "<INPUT TYPE=SUBMIT VALUE=\"Save Settings\">\n";
	print "<INPUT TYPE=HIDDEN NAME=option VALUE=IP>\n";
}

#---SEPARATOR---#
#REQ:access_edit
#REQ:change_private_and_public
#REQ:posting_options
#REQ:topic_security

sub aedit_pro {
	my ($topic, $file) = @_;
	if (!-w "$admin_dir/queue.txt") {
		&error_message("Save Settings Error", "The queue.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then re-attempt to save the settings.", 0, 1);
	}
	if (!-w "$admin_dir/$file.txt") {
		&error_message("Save Settings Error", "The $file.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then re-attempt to save the settings.", 0, 1);
	}
	open (FILE, "$admin_dir/$file.txt") || &error_message("File open error", "Could not open $admin_dir/$file.txt!");
	@file = <FILE>; close (FILE);
	@file = grep(/\S/, @file);
	if (grep(/^$topic:/, @file)) {
		($line) = grep(/^$topic:/, @file);
		@file = grep(!/^$topic:/, @file);
		($tn, $ip, $usr, $mod, $pas) = split(/:/, $line);
	} else {
		$line = "$topic::::"; $ip = ""; $usr = ""; $mod = ""; $pas = "\n";
		&log_error("src-board-subs-102", "aedit_pro", "Entry for $topic created in $file.txt file");
	}
	$was_public = 0;
	$was_public = 1 if $ip eq "~";
	$banned_ip = "***";
	$allowed_ip = "***";
	if ($FORM{'acc'} eq "dis") {
		&access_edit($topic, $file, "", "", "", "", "");
		$was_public = 2;
	} elsif ($FORM{'acc'} eq "pub") {
		&access_edit($topic, $file, "", "", "", "", "*");
		$public = 1;
	} elsif ($FORM{'acc'} eq "res") {
		$public = 0;
		if ($FORM{'modgrpbox'}) {
			if ($FORM{'modgrpbased'} == 1) {
				$mod = $owner;
			} elsif ($FORM{'modgrpbased'} == 2) {
				$mod = "~";
			} elsif ($FORM{'modgrpbased'} == 3) {
				if ($mod eq "") {
					$mod = $owner;
				} else {
					$mod = "***";
				}
			} else {
				$mod = $owner;
			}
		} else {
			$mod = "";
		}
		if ($FORM{'usrgrpbox'}) {
			if ($FORM{'usrgrpbased'} == 1) {
				$usr = $owner;
			} elsif ($FORM{'usrgrpbased'} == 2) {
				$usr = "~";
			} elsif ($FORM{'usrgrpbased'} == 3) {
				if ($usr eq "") {
					$usr = $owner;
				} else {
					$usr = "***";
				}
			} else {
				$usr = $owner;
			}
		} else {
			$usr = "";
		}
		if ($FORM{'globalbox'} != 1) {
			$pas = "\n";
		} else {
			$pas = "***";
		}
		if ($FORM{'IPbox'} != 1) {
			$banned_ip = "";
			$allowed_ip = "";
		} else {
			$banned_ip = "***";
			$allowed_ip = "***";
			@ip = split(/,/, $ip);
			$public = 1 if grep(/^~$/, @ip);
		}
		&access_edit($topic, $file, $mod, $usr, $pas, $banned_ip, $allowed_ip);
	}
	$code = &posting_options($FORM{'topic'}, $FORM{'anon'}, $FORM{'full'}, $FORM{'email'}) if $FORM{save_options};
	$was_public = 2 if $code == 2;
	if (!$FORM{'queue'}) {
		&lock("aedit_pro", "$admin_dir/queue.txt");
		open (MOD, "$admin_dir/queue.txt"); @q = <MOD>; close (MOD);
		@array = grep(!/^$topic:/, @q);
		open (MOD, ">$admin_dir/queue.txt"); print MOD @array; close (MOD);
		&unlock("aedit_pro", "$admin_dir/queue.txt");
	}
	$file_save = $file;
	&topic_security($topic);
	if ($file_save eq "posting") {
		&change_private_and_public($topic, $topic, "", $was_public);
	}
}

#---SEPARATOR---#
#REQ:access_edit
#REQ:change_private_and_public
#REQ:access_edit_configure
#REQ:topic_security

sub advanced_access_editor {
	my ($file, $do, $topic) = @_;
	open (FILE, "$admin_dir/$file.txt") || &error_message("File open error", "Could not open $admin_dir/$file.txt!");
	@file = <FILE>; close (FILE);
	if (!-w "$admin_dir/$file.txt") {
		&error_message("Save Settings Error", "The $file.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then re-attempt to save the settings.", 0, 1);
	}
	if (!-w "$admin_dir/queue.txt") {
		&error_message("Save Settings Error", "The queue.txt file (in your administration directory) does not exist or does not have the proper file permissions to save your setting.  Make sure this file exists and that permissions on this file are 0777 (rwxrwxrwx).  Then re-attempt to save the settings.", 0, 1);
	}
	@file = grep(/\S/, @file);
	if (grep(/^$topic:/, @file)) {
		($line) = grep(/^$topic:/, @file);
		@file = grep(!/^$topic:/, @file);
		($tn, $ip, $usr, $mod, $pas) = split(/:/, $line);
	} else {
		$line = "$topic::::"; $ip = ""; $usr = ""; $mod = ""; $pas = "\n";
	}
	$waspublic = 0;
	@ip = split(/,/, $ip);
	$waspublic = 1 if grep(/^~$/, @ip);
	$banned_was_used = 0;
	$banned_was_used = 1 if ($ip ne "" && $ip ne "~");
	$public = 0;
	$banned_ip = "***";
	$allowed_ip = "***";
	if ($do eq "pw") {
		if ($FORM{'globalpw'} eq "off") {
			$pas = "\n";
		} elsif ($FORM{'globalpw'} eq "new") {
			$p1 = $FORM{'pass_1'}; $p1 =~ tr/A-Z/a-z/;
			$p2 = $FORM{'pass_2'}; $p2 =~ tr/A-Z/a-z/;
			while ($p1 =~ m|(\W)|g) {
				$o = ord($1); $m = $1;
				&error_message("Add User Error", "Password is invalid (password may not contain the character <B>$m</B>).") if $o < 126;
			}
			if ($p1 ne $p2) {
				&error_message("Password Error", "The two passwords you entered do not match!");
			} elsif (length($p1) < 1) {
				&error_message("Password Error", "Your password must be at least one character long!");
			}
			$pas = $p1;
			if (!$banned_was_used) {
				$banned_ip = ""; $allowed_ip = "";
				$public = 0;
			} else {
				$public = $waspublic;
			}
		} else {
			$pas = "***";
			if (!$banned_was_used) {
				$banned_ip = ""; $allowed_ip = "";
				$public = 0;
			} else {
				$public = $waspublic;
			}
		}
	} else {
		$pas = "***";
	}
	if ($do eq "mod" || $do eq "usr") {
		$mod = "***" if $do eq "usr";
		$usr = "***" if $do eq "mod";
		@grplist = split(/,/, $FORM{'usergroup'});
		if (grep(/^ALL$/, @grplist)) {
			$mod = "~" if $do eq "mod";
			$usr = "~" if $do eq "usr";
		} else {
			foreach $u (@grplist) {
				$u =~ s/\W//g;
			}
			$mod = join(",", @grplist) if $do eq "mod";
			$usr = join(",", @grplist) if $do eq "usr";
		}
		if (!$banned_was_used) {
			$banned_ip = ""; $allowed_ip = "";
			$public = 0;
		} else {
			$public = $waspublic;
		}
	} else {
		$mod = "***"; $usr = "***";
	}
	if ($do eq "IP") {
		$banned_ip = $FORM{'bannedIP'};
		$banned_ip .= ",$FORM{'bannedIPtext'}" if $FORM{'bannedIPnew'};
		$allowed_ip = $FORM{'allowedIP'};
		$allowed_ip .= ",$FORM{'allowedIPtext'}" if $FORM{'allowedIPnew'};
		$IPFLAG = 1;
	}
	if ($do eq "Q") {
		&lock("advanced_access_editor", "$admin_dir/queue.txt");
		open (MOD, "$admin_dir/queue.txt"); @q = <MOD>; close (MOD);
		@array = grep(!/^$topic_number:/, @q);
		($user, $moderator, $special, $public) = (0+$FORM{'QUEUEuser'}, 0+$FORM{'QUEUEmod'}, 0+$FORM{'QUEUEspec'}, 0+$FORM{'QUEUEpublic'});
		push (@array, join(":", $topic, $user, $moderator, $special, $public . "\n"));
		open (MOD, ">$admin_dir/queue.txt"); print MOD @array; close (MOD);
		&unlock("advanced_access_editor", "$admin_dir/queue.txt");
	}
	open (FILE, "$admin_dir/$file.txt") || &error_message("File open error", "Could not open $admin_dir/$file.txt!");
	@file = <FILE>; close (FILE);
	@file = grep(/\S/, @file);
	if (grep(/^$topic:/, @file)) {
		($line) = grep(/^$topic:/, @file);
		$line =~ s/\s+$//;
		@file = grep(!/^$topic:/, @file);
		($tn3, $ip3, $usr3, $mod3, $pas3) = split(/:/, $line);
	}
	if ($ip3 eq "" && $usr3 eq "" && $mod3 eq "" && $pas3 eq "") {
		$was_public = 2;
	}
	&access_edit($topic, $file, $mod, $usr, $pas, $banned_ip, $allowed_ip);
	if ($file eq "posting") {
		&change_private_and_public($topic, $topic, "", $was_public);
	}
	&topic_security($topic);
	if ($IPFLAG) {
		&extract("//$topic/$topic.$ext");
		$word = "posting" if $file eq "posting";
		$word = "reading" if $file eq "security";
		&access_edit_configure ($word, $do);
	}
}

# END - FILE IS CORRECTLY UPLOADED #
