# FILE: src-board-subs-common
#-------------------------------------------------------------------------------
# This script is copyright (c) 1998 by DiscusWare, LLC, all rights reserved.
# Its use is subject to the license agreement that can be found at the following
# URL:  http://www.chem.hope.edu/discus/license
#-------------------------------------------------------------------------------
#
# The following indicates the version information for this distribution.
# This is available to you through the Version Manager.

$release_version = "3.00";
$free_revision = "3";
$pro_revision = "p5";

#-------------------------------------------------------------------------------

$url_to_default_image = "$html_url/clipart/your_image.gif";
$titlej = &JavaScript_prepare($title);
$message_dir = "$html_dir/messages";
$message_url = "$html_url/messages";
$cgiurl = "$script_url/board-admin.$cgi_extension";
$cgiurl1 = "$script_url/board-admin-1.$cgi_extension";
$cgiurl2 = "$script_url/board-admin-2.$cgi_extension";
$cgiurl3 = "$script_url/board-admin-3.$cgi_extension";
$cgiurlm = "$script_url/board-admin-menuonly.$cgi_extension";
$secdir = "$admin_dir/secure";
sub header {
	print "Content-type: text/html\n\n";
}
$fs = "<FONT FACE=\"Verdana,Arial,Helvetica\" SIZE=2>";
undef %GLOBAL_OPTIONS;
undef %FORM;
undef %COOKIE;
open (OPT, "$admin_dir/options.txt"); @opt = <OPT>; close (OPT);
foreach $line (@opt) {
	if ($line =~ m|^(\w+)=(.*)|) {
		$GLOBAL_OPTIONS{$1} = $2;
	}
}
&read_declarations;
undef %ev;
sub ex {
	local ($function_name) = shift (@_);
	local (@params, @file, $o);
	@params = @_;
	local ($tmp, $file, $num, $sub, @sub, @s1, @s2, $tmpvar);
	if (!$ev{$function_name}) {
		$tmp = $/;
		$/ = "#---SEPARATOR---#";
		($file, $num) = split(/-/, $funct{$function_name});
		$file .= "_$pro_fileid" if $file >= 100;
		open (FILE, "$admin_dir/source/src-board-subs-$file"); @file = <FILE>; close (FILE);
		$sub = $file[$num];
		$/ = $tmp;
		@sub = split(/\n/, $sub); @s1 = grep(/^#REQ:/, @sub); @s2 = grep(!/^#/, @sub);
		foreach $tempvar (@s1) {
			$tempvar =~ m|^#REQ:(\w+)|; $o = $1; &ex($o);
		}
		$tempvar = join("\n", @s2); eval $tempvar;
		$ev{$function_name} = $1;
	}
	if (scalar(@params) > 0) {
		$tempvar = "\@result = &$function_name(\@params);"; eval $tempvar; return @result;
	} else {
		return 0;
	}
}
sub lock {
	local ($filename) = @_;
	open (LOCK, "$admin_dir/locks.txt") || &error_message("File Locking Error", "Could not open file $admin_dir/locks.txt.  Make sure $admin_dir/locks.txt exists and is world writable.");
	@LOCK = <LOCK>;
	close (LOCK);
	$time = time;
	if (($ll) = grep(/\*/, @LOCK)) {
		($timest) = split(/,/, $ll);
		if ((time - $timest) <= 180) {
			$es = $L{'FILELOCKREGEN'};
			$t = (180 - (time - $timest));
			$es =~ s/\%sec/$t/g;
			&error_message("$L{'FILELOCKERROR'}", "$es");
		} else {
			@LOCK = grep(!/\*/, @LOCK);
		}
	}
	if (!grep(/,$filename\s*$/, @LOCK)) {
		open (LOCK, ">>$admin_dir/locks.txt") || &error_message("File Locking Error", "Could not append file $admin_dir/locks.txt.  Make sure $admin_dir/locks.txt exists and is world writable.");
		print LOCK "$time,$filename\n";
		close (LOCK);
		return 1;
	} else {
		$ctr = 0;
		($line) = grep(/,$filename\s*$/, @LOCK);
		($time_l) = split(/,/, $line);
		if ($time > ($time_l + 5)) {
			@LOCK = grep(!/,$filename\s*$/, @LOCK);
			push (@LOCK, "$time,$filename\n");
			open (LOCK, ">$admin_dir/locks.txt") || &error_message("File Locking Error", "Could not write file $admin_dir/locks.txt.  Make sure $admin_dir/locks.txt exists and is world writable.");
			print LOCK @LOCK;
			close (LOCK);
			return 1;
		} else {
			for ($ctr = 0; $ctr <= 100; $ctr++) {
				open (LOCK, "$admin_dir/locks.txt");
				@LOCK = <LOCK>;
				close (LOCK);
				$time = time;
				if (!grep(/,$filename\s*$/, @LOCK)) {
					open (LOCK, ">>$admin_dir/locks.txt") || &error_message("File Locking Error", "Could not append file $admin_dir/locks.txt.  Make sure $admin_dir/locks.txt exists and is world writable.");
					print LOCK "$time,$filename\n";
					close (LOCK);
					return 1;
				}
			}
			&error_message("$L{'FILELOCKERROR'}", "$L{FILEISLOCKED}", 0, 1);
		}
	}
}
sub unlock {
	local ($filename) = @_;
	open (LOCK, "$admin_dir/locks.txt") || &error_message("File Unlocking Error", "Could not open file $admin_dir/locks.txt.  Make sure $admin_dir/locks.txt exists and is world writable.");
	@LOCK = <LOCK>;
	close (LOCK);
	@LOCK = grep(!/,$filename\s*$/, @LOCK) if $filename ne "*";
	@LOCK = grep(!/\*/, @LOCK) if $filename eq "*";
	if (scalar(@LOCK) == 0) {
		push (@LOCK, "#\n");
	}
	open (LOCK, ">$admin_dir/locks.txt") || &error_message("File Unlocking Error", "Could not open file $admin_dir/locks.txt (System returned error: $!).  Make sure $admin_dir/locks.txt exists and is world writable.");
	print LOCK @LOCK;
	close (LOCK);
}
sub parse_form {
	undef %FORM;
	if ($ENV{'CONTENT_LENGTH'} != 0) {
		read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
		@pairs = split(/&/, $buffer);
		foreach $pair (@pairs) {
			($name, $value) = split(/=/, $pair);
			$value =~ tr/+/ /;
			$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
			$value =~ s/<!--#(\w*)(\s*)(\w*)(\s*)=(\s*)"([^"]*)"(.*)-->//g;
			$value =~ s/\r//g;
			if ($FORM{$name} eq "") {
				$FORM{$name} = $value;
			} else {
				$FORM{$name} .= ",$value";
			}
		}
	}
	$ENV{'QUERY_STRING'} =~ s/#(.*)$//;
	if ($ENV{'QUERY_STRING'} ne "") {
		$command = $ENV{'QUERY_STRING'};
		@pairs = split(/&/, $command);
		foreach $pair (@pairs) {
			($name, $value) = split(/=/, $pair);
			$value = &unescape($value);
			$FORM{$name} = $value if $FORM{$name} eq "";
		}
	}
}
sub error_message {
	local ($reason, $explanation, $flag, $noerr) = @_;
	$excl_pt = $!;
	&header;
	&ex('printuntil', 1, 1, 0, "$reason") if !$flag;
	print "<!-Discus Program Error Message-!>\n";
	print "$fs<FONT SIZE=4 COLOR=#ff0000><CENTER><B>$reason</B></CENTER></FONT>\n";
	print "<HR><P>\n";
	print "<!-Begin Error Message-!>\n";
	print "<FONT FACE='Courier New' SIZE=2>$explanation<P>\n";
	if ($excl_pt && !$noerr) {
		print "\$!: <FONT COLOR=#ff0000><B>$excl_pt</B></FONT><P>";
	}
	if ($@ && !$noerr) {
		print "\$\@: <FONT COLOR=#ff0000><B>$@</B></FONT><P>";
	}
	print "</FONT>\n";
	print "<!-End Error Message-!>\n";
	print "<P>Please contact <B>$contact</B> if this problem persists.\n";
	&ex('printuntil',3, 17, 0, "$reason") if !$flag;
	exit(0);
}
sub extract {
	local ($strin, $error_trapping) = @_;
	local ($line, $filename, @file,$key);
	undef %level_number;
	$_ = "/$strin";
	$secure = 0;
	/.*\/(\d+)\/(\d+)\.$ext/;
	$f1 = $1;
	$f2 = $2;
	&error_message('Undefined Error',"Undefined Error extracting file!  Input file $_ is not a valid file!", 0, 1) if $f1 eq "";
	$filename = "$message_dir/$f1/$f2.$ext"; $filename2 = "";
	if (!-e "$message_dir/$f1") {
		$filename2 = "$secdir/$f1/$f2.$ext";
		$secure = 1;
	}
	$filename = $filename2 if $filename2 ne "";
	if (!-e $filename) {
		if (-e "$filename.NEW") {
			&lock($filename);
			if (!-e $filename && -e "$filename.NEW") {
				if ($platform eq "NT" || $NT || $platform =~ m|NT|i) {
					$code = 0;
				} else {
					$code = rename("$filename.NEW", "$filename");
				}
				if ($code != 1 || !-e $filename) {
					open (FILE, "$filename.NEW");
					@file = <FILE>;
					close (FILE);
					open (FILE, ">$filename");
					print FILE @file;
					close (FILE);
					unlink ("$filename.NEW");
				}
				chmod (0666, "$filename");
			}
			&unlock($filename);
		}
	}
	if (open (FILE_EXTRACT, $filename)) {
		@file = <FILE_EXTRACT>;
		close (FILE_EXTRACT);
		foreach $line (@file) {
			$_ = $line;
			$topic_name = $2 if /<!--Topic: (\d+)\/(.*)-->/;
			$topic_number = $1 if /<!--Topic: (\d+)\/(.*)-->/;
			$owner = $1 if /<!--Owner: (\w+)-->/;
			if (/<!--Level (\d+): (\d+)\/(.*)-->/) {
				$key = $1;
				$level_name{$key} = $3;
				$level_number{$key} = $2;
			}
			$me_name = $2 if /<!--Me: (\d+)\/(.*)-->/;
			$me_number = $1 if /<!--Me: (\d+)\/(.*)-->/;
			$parent_number = $1 if /<!--Parent: (\d+)/;
			if (/<!--Param: (\w+)-->/) {
				$param = $1;
			}
		}
		return 1;
	} else {
		&error_message('Undefined Error',"Undefined Error extracting file!  <I>$filename</I> could not be opened!") if $error_trapping == 0;
		$topic_name = "<B>Corrupted Topic File</B>";
		$topic_number = $f1;
		$owner = "unknown";
		return 0;
	}	
}
sub JavaScript_prepare {
	local ($str, $param) = @_;
	$str =~ s/<IMG SRC="[^"]*" ALT="([^"]*)">/\[$1\]/g;
	$str =~ s/<([^>]*)>//g;
	$str =~ s/\n//g;
	if ($param == 1) {
		$str =~ s/([^\w&#; ])/&makeord($1)/ge;
	} else {
		$str =~ s/&#(\d+);//g;
		$str =~ s/'//g;
		$str =~ s/"//g;
		$str =~ s/&quot;//g;
		$str =~ s/&amp;//g;
	}
	return $str;
}
sub makeord {
	local ($o, $num) = @_;
	if (ord($o) <= 126) {
		$num = ord($o);
		return "&#$num;";
	} else {
		return $o;
	}
}
sub remove_links {
	local ($string) = @_;
	local ($str);
	$str = $string;
	$str =~ s/<A\s([^>]*)>//g;
	$str =~ s/<\/A>//g;
	return $str;
}
sub read_cookie {
	$buffer = $ENV{'HTTP_COOKIE'};
	@pairs = split(/; /, $buffer);
	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$COOKIE{$name} = $value;
	}
}
sub escape {
	local ($input) = @_;
	local ($string);
	$string = $input;
	$string =~ s/([^\w ])/sprintf("%%%02lx", ord($1))/eg;
	$string =~ tr/ /+/;
	return $string;
}
sub unescape {
	local ($input) = @_;
	local ($string);
	$string = $input;
	$string =~ tr/+/ /;
	$string =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	return $string;
}
sub remove_html {
	local ($string, $flag) = @_;
	local ($str);
	$str = $string;
	$str =~ s/<IMG SRC="[^"]*" ALT="([^"]*)">/\[$1\]/g;
	$str =~ s/<[^>]*>//g;
	$str =~ s/&#(\d+);//g if $flag == 0;
	return $str;
}
sub seturl {
	local ($targeturl) = @_;
	if (!($nph_server)) {
		print "Location: $targeturl\n\n";
		exit(0);
	} else {
		&header;
		print "<HTML><HEAD><TITLE>Document Moved</TITLE>\n";
		print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=$targeturl\">\n";
		print "</HEAD>\n";
		print "<BODY>\n";
		print "<NOSCRIPT>\n";
		print "<A HREF=\"$targeturl\">Please click here</A>\n";
		print "</NOSCRIPT>\n";
		print "</BODY></HTML>\n";
		exit(0);
	}
}
sub getfn {
	local ($input) = @_;
	local ($before, $after, $m, $o);
	if ($input =~ m|(.*)/|) {
		$before = "$1/";
		$after = $';
	} else {
		$after = $input;
	}
	while ($after =~ m|(.)|g) {
		$m = $1; $o = ord($m);
		if ($m eq "." || $m =~ /\w/ || $m eq "-") {
			$before .= $m;
		} else {
			$before .= "$o";
		}
	}
	return $before;
}
sub print_cookie_string {
	local ($username_in, $password_in, $password_to_crypt_in) = @_;
	$username_in = "" if $ALREADY_SET_USERNAME;
	$password_in = "" if $ALREADY_SET_PWIN;
	$password_to_crypt_in = "" if $ALREADY_SET_PWCIN;
	$store_cookies = 1 if $GLOBAL_OPTIONS{'cookies'};
	if ($pro) {
		&ex('pro_cookies', $username_in, $password_in, $password_to_crypt_in);
		return 1;
	} else {
		return 0 if !$store_cookies;
		print "Set-cookie: user$COOKIE_ID=$username_in; path=/\n" if $username_in;
		$ALREADY_SET_USERNAME = 1 if $username_in;
		print "Set-cookie: rpwd$COOKIE_ID=", "x" x $password_in, "; path=/\n" if $password_in;
		$ALREADY_SET_PWIN = 1 if $password_in;
		print "Set-cookie: cpwd$COOKIE_ID=" if $password_to_crypt_in;
		print crypt($password_to_crypt_in, "cookie") if $password_to_crypt_in;
		print "; path=/\n" if $password_to_crypt_in;
		$ALREADY_SET_PWCIN = 1 if $password_to_crypt_in;
		return 1;
	}
}
sub get_page {
	local ($topic, $page, $meta_override) = @_;
	local ($head, $announce, $announce_src, $sublist, $about, $about_src, $message, $message_src, $flag, $color, $lm);
	local ($save, @file);
	if (-e "$html_dir/messages/$topic/$page.$ext") {
		open (FILE, "$html_dir/messages/$topic/$page.$ext");
		$secure = 0;
	} else {
		open (FILE, "$secdir/$topic/$page.$ext");
		$secure = 1;
	}
	@file = <FILE>;
	close (FILE);
	@file_last_got = @file;
	foreach $_ (@file) {
		if (/^<!--Topic/ || /^<!--Level/ || /^<!--Me:/ || /^<!--Param/ || /^<!--Parent/ || /^<!--Owner/) {
			$head .= $_;
		} elsif (/^\s*$/) {
			next;
		} elsif (/^<BODY BGCOLOR="([^"]*)" TEXT="([^"]*)" LINK="([^"]*)" VLINK="([^"]*)" ALINK="([^"]*)" BACKGROUND="([^"]*)"/i) {
			$color = join("\t", $1, $2, $3, $4, $5, $6);
		} elsif (/^<BODY BGCOLOR="([^"]*)" TEXT="([^"]*)" LINK="([^"]*)" VLINK="([^"]*)" ALINK="([^"]*)"/i) {
			$color = join("\t", $1, $2, $3, $4, $5, "");
		} elsif (/^<BASEFONT SIZE="([^"]*)"><FONT FACE="([^"]*)">/i) {
			$color .= "\t$1\t$2";
		} elsif (/^<A NAME="([^P].*)">/) {
			$lm = $1;
		} elsif (/^<!-Top: (\d+)-!>/) {
			$sublist .= $_;
		} elsif (/^<!-URL: (\d+)-!>/) {
			$sublist .= $_;
		} elsif (/^<!--About-->/) {
			$flag = 1;
		} elsif (/^<!--Source:/) {
			$flag = 2;
		} elsif ($flag == 1) {
			$about .= $_ if (!/^<HR>/ && !/^-->/);
		} elsif ($flag == 2 && m|^<!--/About-->|) {
			$flag = 0;
		} elsif ($flag == 2) {
			$about_src .= $_ if !/^-->/;
		} elsif (/^<!--Announcement-->/) {
			$flag = 15;
		} elsif (/^<!--Announcement Source:/) {
			$flag = 16;
		} elsif ($flag == 15) {
			$announce .= $_ if (!/^<HR>/ && !/^-->/);
		} elsif ($flag == 16 && m|^<!--/Announcement-->|) {
			$flag = 0;
		} elsif ($flag == 16) {
			$announce_src .= $_ if !/^-->/;
		} elsif (m|<!-Post: (\d+)-!>|) {
			$flag = 3; $save = $1;
			$message .= $_;
		} elsif (m|<!-/Post: $save-!>|) {
			$flag = 0;
			$message .= $_;
		} elsif ($flag == 3) {
			$message .= $_;
		} elsif (/^<!--Message Source/) {
			$flag = 4;
		} elsif ($flag == 4 && /^-->/) {
			$flag = 0;
		} elsif ($flag == 4) {
			$message_src .= $_;
		} elsif (m|^<META NAME="DESCRIPTION" CONTENT="([^"]*)">|) {
			$meta_description = $1 if !$meta_override;
		} elsif (m|^<META NAME="KEYWORDS" CONTENT="([^"]*)">|) {
			$meta_keywords = $1 if !$meta_override;
		} elsif (m|^<META NAME="ROBOTS" CONTENT="([^"]*)">|) {
			$meta_robots = $1 if !$meta_override;
		}
	}
	if ($color eq "") {
		$color = "ffffff\t000000\t0000ff\t800080\tff0000\t\t2\tVerdana,Arial,Helvetica";
	}
	return ($head, $color, $lm, $announce, $announce_src, $sublist, $about, $about_src, $message, $message_src);
}
sub set_page {
	local ($topic, $page, $head, $color, $timestr, $announcement_variable, $announcement_source_variable, $subtopic_variable, $about_variable, $about_source_variable, $message_variable, $message_source_variable, $templatefile, $addfile, $timesaver) = @_;
	return 0 if $page eq "";
	local (@tfile, $line, $param, $owner, $levelj, $navline, $navbar, $str, $file, @file);
	$message_variable = &add_message_icons($message_variable);
	if ($templatefile) {
		@tfile = split(/\n/, $templatefile);
		foreach $line (@tfile) {
			$line .= "\n";
		}
		@tfile = grep(/\S/, @tfile);
	} else {
		if (open (TFILE, "$html_dir/messages/$topic/newpage.conf")) {
			@tfile = <TFILE>;
			close (TFILE);
		} elsif (open (TFILE, "$secdir/$topic/newpage.conf")) {
			@tfile = <TFILE>;
			close (TFILE);
		} else {
			open (TFILE, "$admin_dir/newpage.conf");
			@tfile = <TFILE>;
			close (TFILE);
		}
	}
	if (!(grep(/<!--Start-->/, @tfile))) {
		&error_message("Save Error", "The newpage.conf template has been corrupted; unable to save your change", 0, 1);
	}
	if (!(grep(/\$subtopic_variable/, @tfile)) || !(grep(/\$about_variable/, @tfile)) || !(grep(/\$message_variable/, @tfile)) || !(grep(/\$message_source_variable/, @tfile)) || !(grep(/\$about_source_variable/, @tfile)) || !(grep(/\$head/, @tfile))) {
		&error_message("Save Error", "The newpage.conf template has been corrupted; unable to save your change", 0, 1);
	}
	local ($bgcolor, $text, $link, $vlink, $alink, $image, $size, $face) = split(/\t/, $color);
	local (%level_number, %level_name, $topic_number, $topic_name, $me_number, $me_name);
	foreach $line (split(/\n/, $head)) {
		if ($line =~ m|<!--Topic: (\d+)/(.*)-->|) {
			($topic_number, $topic_name) = ($1, $2);
		} elsif ($line =~ m|<!--Level (\d+): (\d+)/(.*)-->|) {
			$level_number{$1} = $2; $level_name{$1} = $3;
		} elsif ($line =~ m|<!--Me: (\d+)/(.*)-->|) {
			$me_number = $1; $me_name = $2;
		} elsif ($line =~ m|<!--Param: (.*)-->|) {
			$param = $1;
		} elsif ($line =~ m|<!--Owner: (.*)-->|) {
			$owner = $1;
		}
	}
	$navline = "<A HREF=\"$html_url/messages/board-topics.html\" onMouseOver=\"return setStatus('$L{NBRETURN} $titlej main page')\">$title</A>: ";
	if ($topic_number != $me_number) {
		$levelj = &JavaScript_prepare($topic_name);
		$navline .= "<A HREF=\"$html_url/messages/$topic_number/$topic_number.$ext\"";
		$navline .= " onMouseOver=\"return setStatus('$L{NBRETURN} $levelj')\">";
		$navline .= "$topic_name</A>: ";
	} else {
		$navline .= "$topic_name";
	}
	foreach $line (sort by_number keys(%level_number)) {
		if ($level_number{$line} != $me_number) {
			$levelj = &JavaScript_prepare($level_name{$line});
			$navline .= "<A HREF=\"$html_url/messages/$topic_number/$level_number{$line}.$ext\"";
			$navline .= " onMouseOver=\"return setStatus('$L{NBRETURN} $levelj')\">";
			$navline .= "$level_name{$line}</A>:\n";
		} else {
			$navline .= "$level_name{$line}";
		}
	}
	$str = &JavaScript_prepare($navline);
	$navbar = $navline;
	local (@users, @addfile, $line_2, @valid, $privpub);
	if ($addfile) {
		@addfile = split(/\n/, $addfile);
		foreach $line (@addfile) {
			$line .= "\n";
			if ($line =~ m|<!-/Identification Tag - LEAVE THIS HERE-!>|) {
				$line .= "<INPUT TYPE=HIDDEN NAME=HTTP_REFERER VALUE=//$topic/$page.$ext>\n";
			}
		}
		@addfile = grep(/\S/, @addfile);
	} else {
		open (POST, "$admin_dir/posting.txt"); @post = <POST>; close (POST);
		($line) = grep(/^$topic:/, @post);
		($t, $ip) = split(/:/, $line);
		if ($ip eq "") {
			$privpub = "private";
		} else {
			$privpub = "public";
		}
		open (POST, "$admin_dir/postoptions.txt"); @post = <POST>; close (POST);
		($line) = grep(/^$topic:/, @post);
		($t, $an) = split(/:/, $line);
		$anflag = $an;
		$anflag = 1 if ($GLOBAL_OPTIONS{'anonymous'} == 0 && $GLOBAL_OPTIONS{'options_used'} == 1);
		if (open(FILExx, "$message_dir/$topic/addmessage.conf")) {
			@addfile = <FILExx>;
			close (FILExx);
		} elsif (open(FILExx, "$secdir/$topic/addmessage.conf")) {
			@addfile = <FILExx>;
			close (FILExx);
		} elsif ($line =~ m|::::\s*$| && !-e "$message_dir/$topic/addmessage.conf" && !-e "$secdir/$topic/addmessage.conf") {
			$l = $L{BPPOSTINGDISABLEDDESCR};
			$l =~ s|\%aopen|<A HREF="$script_url/board-contact.$cgi_extension">|g;
			$l =~ s|\%aclose|</A>|g;
			@addfile = ("<HR><CENTER><TABLE BGCOLOR=#ffcccc><TR><TD><FONT FACE=\"$face\" SIZE=\"$size\" COLOR=000000><B>$l</B></FONT></TD></TR></TABLE>");
		} else {		
			open (FILExx, "$admin_dir/addmessage-$privpub.conf");
			@addfile = <FILExx>;
			close (FILExx);
		}
		foreach $line_2 (@addfile) {
			if ($line_2 =~ m|<!-/Identification Tag - LEAVE THIS HERE-!>|) {
				$line_2 .= "<INPUT TYPE=HIDDEN NAME=HTTP_REFERER VALUE=//$topic/$page.$ext>\n";
			}
			$line_2 =~ s/<!-FONT-!>/<FONT FACE="$face" SIZE="$size">/g;
			$line_2 =~ s/<!-POST TO CGI-!>/$script_url\/board-post.$cgi_extension/g;
		}
		@addfile = grep(!/^<!-Anon-!>/, @addfile) if $anflag;
	}
	undef @file;
	local ($flag, $navflag, $flag2, $pagetitle, @array);
	@array = split(/\n/, $subtopic_variable); @array = grep(/\S/, @array); $subtopic_variable = join("\n", @array);
	@array = split(/\n/, $about_variable); @array = grep(/\S/, @array); $about_variable = join("\n", @array);
	@array = split(/\n/, $about_source_variable); @array = grep(/\S/, @array); $about_source_variable = join("\n", @array);
	@array = split(/\n/, $message_variable); @array = grep(/\S/, @array); $message_variable = join("\n", @array);
	@array = split(/\n/, $message_source_variable); @array = grep(/\S/, @array); $message_source_variable = join("\n", @array);
	$pagetitle = $me_name;
	$flag = 0; $navflag = 0; $flag2 = 0;
	foreach $line (@tfile) {
		if ($line =~ /<!--Start-->/) {
			$flag = 1;
		} elsif ($flag == 1) {
			if ($line =~ m|<!--Navbar|) {
				$navflag = 1;
				push (@file, "<!--Navbar-->\n<B>\n$navbar\n</B>\n<!--/Navbar-->\n");
			} elsif ($navflag == 1) {
				$navflag = 0 if $line =~ m|<!--/Navbar|;
			} else {
				if ($line =~ m|\$(\w+)|) {
					$line =~ s/\\/\\\\/g;
					$line =~ s/"/\\"/g;
					$line = eval("\"$line\"");
				}
				if ($line =~ /\[Read addmessage.txt\]/ || $line =~ /\[Read addmessage.conf\]/) {
					push (@file, @addfile);
				} else {
					if ($line =~ m|<!--Sublist-->|) {
						push (@file, "<!--Sublist--><!--Off\n") if $param !~ m|Sublist|;
						push (@file, "<!--Sublist-->\n") if $param =~ m|Sublist|;
					} elsif ($line =~ m|<!--/Sublist-->|) {
						push (@file, "-->\n<!--/Sublist-->\n") if $param !~ m|Sublist|;
						push (@file, "<!--/Sublist-->\n") if $param =~ m|Sublist|;
					} elsif ($line =~ m|<!--Create-->|) {
						push (@file, "<!--Create--><!--Off\n") if $param !~ m|Create|;
						push (@file, "<!--Create-->\n") if $param =~ m|Create|;
					} elsif ($line =~ m|<!--/Create-->|) {
						push (@file, "-->\n<!--/Create-->\n") if $param !~ m|Create|;
						push (@file, "<!--/Create-->\n") if $param =~ m|Create|;
					} elsif ($line =~ m|<!--About-->|) {
						push (@file, "<!--About--><!--Off\n") if $param !~ m|About|;
						push (@file, "<!--About-->\n") if $param =~ m|About|;
					} elsif ($line =~ m|<!--Source:|) {
						push (@file, "-->\n<!--Source:\n") if $param !~ m|About|;
						push (@file, "<!--Source:\n") if $param =~ m|About|;
					} elsif ($line =~ m|<!--Messages-->|) {
						push (@file, "<!--Messages--><!--Off\n") if $param !~ m|Messages|;
						push (@file, "<!--Messages-->\n") if $param =~ m|Messages|;
					} elsif ($line =~ m|<!--/Messages-->|) {
						push (@file, "-->\n<!--/Messages-->\n") if $param !~ m|Messages|;
						push (@file, "<!--/Messages-->\n") if $param =~ m|Messages|;
					} elsif ($line =~ m|<!--Add-->|) {
						push (@file, "<!--Add--><!--Off\n") if $param !~ m|Add|;
						push (@file, "<!--Add-->\n") if $param =~ m|Add|;
					} elsif ($line =~ m|<!--/Add-->|) {
						push (@file, "-->\n<!--/Add-->\n") if $param !~ m|Add|;
						push (@file, "<!--/Add-->\n") if $param =~ m|Add|;
					} elsif ($line =~ m|<!--Announcement-->|) {
						push (@file, "<!--Announcement--><!--Off\n") if $param !~ m|Announcement|;
						push (@file, "<!--Announcement-->\n") if $param =~ m|Announcement|;
					} elsif ($line =~ m|<!--Announcement Source:|) {
						push (@file, "-->\n<!--Announcement Source:\n") if $param !~ m|Announcement|;
						push (@file, "<!--Announcement Source:\n") if $param =~ m|Announcement|;
					} else {
						push (@file, $line);
					}
				}
			}
		}
	}
	@file = grep(/\S/, @file);
	if ($timesaver && join("", @file) eq join("", @file_last_got)) {
		return 0;
	}	
	if (-e "$html_dir/messages/$topic") {
		$filename = "$html_dir/messages/$topic/$page.$ext";
	} else {
		$filename = "$secdir/$topic/$page.$ext";
	}
	open (FILE, ">$filename.NEW") || &error_message("File Save - Write Error", "Could not open $filename.NEW for writing.");
	print FILE @file;
	close (FILE);
	if (-e "$filename.NEW") {
		$fsize_desired = scalar(@file);
		$fsize = -s "$filename.NEW";
		if ($fsize < $fsize_desired) {
			undef $!;
			unlink ("$filename.NEW");
			&error_message("File Save - Write Error", "Error trying to write to $filename -- source file has wrong length (Desired: $fsize_desired; actual: $fsize).  Check your disk quota!");
		}	
		if ($platform eq "NT" || $NT || $platform =~ m|NT|i) {
			$code = 0;
		} else {
			$code = rename("$filename.NEW", "$filename");
		}
		if ($code != 1 || !-e $filename) {
			open (FILE, ">$filename");
			print FILE @file;
			close (FILE);
			$fsize = -s "$filename";
			if ($fsize < $fsize_desired) {
				undef $!;
				&error_message("File Save - Write Error", "Error trying to write to $filename -- source file has wrong length (Desired: $fsize_desired; actual: $fsize).  Check your disk quota!");
			}	
			unlink ("$filename.NEW");
		}
		chmod (0666, "$filename");
	} else {
		&error_message("File Save - Write Error", "Could not open $filename.NEW for reading.");
	}
	return 1;
}
sub add_message_icons {
	local ($message_in) = @_;
	local (@M, @C, @P);
	@M = split(/\n/, $message_in);
	@C = grep(/^<!-Post: (\d+)/, @M);
	undef @P;
	foreach $c (@C) {
		$c =~ m|<!-Post: (\d+)|;
		push (@P, $1);
	}
	$ctr = 0; $flag = 0;
	foreach $line (@M) {
		if ($line =~ m|<!-Post: (\d+)|) {
			$pid = $1;
			$line = "<!-Post: $pid-!><A NAME=\"POST$pid\">\n";
			$flag = 1;
		} elsif ($flag == 1 && $line =~ m|^<HR>|) {
			$flag = 2;
		} elsif ($flag == 2) {
			if ($line =~ m|<!-Icons-!>(.*)<!-/Icons-!>|) {
				$line = $';
			}
			if ($GLOBAL_OPTIONS{'message_icons'} == 1) {
				$line2 = "<!-Icons-!>";
				if ($GLOBAL_OPTIONS{'message_icon_top'} eq "") {
					$GLOBAL_OPTIONS{'message_icon_top'} = "mark_top.gif";
					$GLOBAL_OPTIONS{'message_icon_up'} = "mark_up.gif";
					$GLOBAL_OPTIONS{'message_icon_down'} = "mark_down.gif";
					$GLOBAL_OPTIONS{'message_icon_bottom'} = "mark_bottom.gif";
					$GLOBAL_OPTIONS{'message_icon_link'} = "tree_m.gif";
				}
				$line2 .= "<A HREF=\"#MT\"><IMG SRC=\"$html_url/icons/$GLOBAL_OPTIONS{'message_icon_top'}\" HEIGHT=12 WIDTH=12 BORDER=0 ALT=\"$L{MESSAGE_ICON_TOP}\"></A>" if $GLOBAL_OPTIONS{'message_icon_top'};
				$line2 .= "<A HREF=\"#POST" . $P[$ctr-1] . "\"><IMG SRC=\"$html_url/icons/$GLOBAL_OPTIONS{'message_icon_up'}\" HEIGHT=12 WIDTH=12 BORDER=0 ALT=\"$L{MESSAGE_ICON_UP}\"></A>" if ($ctr > 0 && $GLOBAL_OPTIONS{'message_icon_up'});
				$line2 .= "<A HREF=\"#MT\"><IMG SRC=\"$html_url/icons/$GLOBAL_OPTIONS{'message_icon_up'}\" HEIGHT=12 WIDTH=12 BORDER=0 ALT=\"$L{MESSAGE_ICON_UP}\"></A>" if ($ctr == 0 && $GLOBAL_OPTIONS{'message_icon_up'});
				$line2 .= "<A HREF=\"#POST" . $P[$ctr+1] . "\"><IMG SRC=\"$html_url/icons/$GLOBAL_OPTIONS{'message_icon_down'}\" HEIGHT=12 WIDTH=12 BORDER=0 ALT=\"$L{MESSAGE_ICON_DOWN}\"></A>" if ($ctr < (scalar(@P) - 1) && $GLOBAL_OPTIONS{'message_icon_down'});
				$line2 .= "<A HREF=\"#MB\"><IMG SRC=\"$html_url/icons/$GLOBAL_OPTIONS{'message_icon_down'}\" HEIGHT=12 WIDTH=12 BORDER=0 ALT=\"$L{MESSAGE_ICON_DOWN}\"></A>" if ($ctr == (scalar(@P) - 1) && $GLOBAL_OPTIONS{'message_icon_down'});
				$line2 .= "<A HREF=\"#MB\"><IMG SRC=\"$html_url/icons/$GLOBAL_OPTIONS{'message_icon_bottom'}\" HEIGHT=12 WIDTH=12 BORDER=0 ALT=\"$L{MESSAGE_ICON_BOTTOM}\"></A>" if $GLOBAL_OPTIONS{'message_icon_bottom'};
				$line2 .= "<A HREF=\"#POST" . $P[$ctr] . "\"><IMG SRC=\"$html_url/icons/$GLOBAL_OPTIONS{'message_icon_link'}\" HEIGHT=16 WIDTH=20 BORDER=0 ALT=\"$L{MESSAGE_ICON_LINK}\"></A>" if $GLOBAL_OPTIONS{'message_icon_link'};
				$line = "$line2&nbsp;&nbsp;<!-/Icons-!>$line";
			}
			$ctr += 1;
			$flag = 0;
		}
	}
	return join("\n", @M);
}

sub by_number {
	$a <=> $b;
}
sub read_declarations {
	$funct{'add_topic'} = "1-1";
	$funct{'remove_topic'} = "1-2";
	$funct{'rename_topic'} = "1-3";
	$funct{'reorder_topics'} = "1-4";
	$funct{'change_main_message'} = "1-5";
	$funct{'register_discus'} = "1-6";
	$funct{'change_topic_group'} = "1-7";
	$funct{'preview_board_colors'} = "1-8";
	$funct{'change_board_colors'} = "1-9";
	$funct{'change_board_toppage'} = "1-10";
	$funct{'add_page'} = "2-1";
	$funct{'rename_topic_form'} = "2-2";
	$funct{'change_topic_group_form'} = "2-3";
	$funct{'add_moderator'} = "2-4";
	$funct{'delete_moderator'} = "2-5";
	$funct{'save_top_meta_tags'} = "2-6";
	$funct{'save_topic_meta_tags'} = "2-7";
	$funct{'upgrade_file_structure'} = "2-8";
	$funct{'rename_subtopic_form'} = "2-9";
	$funct{'rename_link_form'} = "2-10";
	$funct{'rename_subtopic'} = "2-11";
	$funct{'rename_link'} = "2-12";
	$funct{'reorder_subtopics'} = "2-13";
	$funct{'email_configuration'} = "2-14";
	$funct{'html_to_webtags'} = "2-15";
	$funct{'page_mgr_1'} = "3-1";
	$funct{'page_mgr_2'} = "3-2";
	$funct{'board_manager'} = "4-1";
	$funct{'build_graphical_tree'} = "4-2";
	$funct{'admin_main_menu'} = "4-3";
	$funct{'front_page'} = "4-4";
	$funct{'viewmessage'} = "4-5";
	$funct{'moderator_mgr'} = "4-6";
	$funct{'save_profile_information'} = "4-7";
	$funct{'save_description'} = "4-8";
	$funct{'remove_message'} = "4-9";
	$funct{'first_pass'} = "4-10";
	$funct{'notification_message'} = "4-11";
	$funct{'preview_admin_message'} = "5-1";
	$funct{'change_about_message'} = "5-2";
	$funct{'remove_page'} = "5-3";
	$funct{'move_page'} = "5-4";
	$funct{'printuntil'} = "5-5";
	$funct{'remove_swearing'} = "5-6";
	$funct{'access_mgr_2'} = "5-7";
	$funct{'send_email_message'} = "5-8";
	$funct{'change_announce_message'} = "5-9";
	$funct{'manage_images'} = "5-10";
	$funct{'reapply_webtags'} = "5-11";
	$funct{'profile_editor_screen'} = "5-12";
	$funct{'verify_owner'} = "5-13";
	$funct{'profanity_editor'} = "5-14";
	$funct{'cuss_save'} = "5-15";
	$funct{'group_mgr'} = "6-1";
	$funct{'add_group'} = "6-2";
	$funct{'delete_group'} = "6-3";
	$funct{'edit_group'} = "6-4";
	$funct{'delete_moderator_from_group'} = "6-5";
	$funct{'add_to_group'} = "6-6";
	$funct{'move_message_form'} = "6-7";
	$funct{'move_message'} = "6-8";
	$funct{'edit_message_form'} = "6-9";
	$funct{'save_edited_message'} = "6-10";
	$funct{'reorder_messages'} = "6-11";
	$funct{'email_notification'} = "7-1";
	$funct{'user_mgr_1'} = "7-2";
	$funct{'user_mgr_2'} = "7-3";
	$funct{'add_user'} = "7-4";
	$funct{'version_manager'} = "7-5";
	$funct{'preview_user_list'} = "7-6";
	$funct{'modify_list'} = "7-7";
	$funct{'add_user_list'} = "7-8";
	$funct{'delete_user'} = "7-9";
	$funct{'posting_options'} = "7-10";
	$funct{'change_title_message'} = "7-11";
	$funct{'change_addmessage'} = "7-12";
	$funct{'change_layout'} = "8-1";
	$funct{'options_mgr'} = "8-2";
	$funct{'extract_lastmodified'} = "8-3";
	$funct{'prepare_navbar'} = "8-4";
	$funct{'webtags'} = "8-5";
	$funct{'change_private_and_public'} = "8-6";
	$funct{'page_manager_navbar'} = "8-7";
	$funct{'graphic_browser_select'} = "8-8";
	$funct{'move_subtopic_form'} = "8-9";
	$funct{'recurse'} = "8-10";
	$funct{'new_file'} = "8-11";
	$funct{'access_edit_simple'} = "8-12";
	$funct{'determine_addmessage'} = "8-13";
	$funct{'post_message'} = "9-1";
	$funct{'get_date_time'} = "9-2";
	$funct{'get_number'} = "9-3";
	$funct{'check_owner'} = "9-4";
	$funct{'extract_colorsonly'} = "9-5";
	$funct{'verify_user_password'} = "9-6";
	$funct{'update_time'} = "9-7";
	$funct{'verify_postread_privileges'} = "9-8";
	$funct{'options_save'} = "9-9";
	$funct{'email_configurator'} = "9-10";
	$funct{'compare_profiles'} = "10-1";
	$funct{'display_profile_differences'} = "10-2";
	$funct{'data_recovery'} = "10-3";
	$funct{'data_recovery_action'} = "10-4";
	$funct{'data_recovery_top'} = "10-5";
	$funct{'auto_configure_email'} = "10-6";
	$funct{'admin_from_here'} = "10-7";
		
#  --------- DISCUS PRO	---------
	
	$funct{'log_authorization'} = "100-1";
	$funct{'get_authorized_topics'} = "100-2";
	$funct{'userapp_result'} = "100-3";
	$funct{'topic_security'} = "100-4";
	$funct{'access_edit'} = "100-5";
	$funct{'email_notification_pro'} = "100-6";
	$funct{'log_analysis_1'} = "100-7";
	$funct{'log_analysis_access'} = "100-8";
	$funct{'log_analysis_by_user'} = "100-9";
	$funct{'show_secure_image'} = "101-1";
	$funct{'check_queue_status'} = "101-2";
	$funct{'send_message_to_queue'} = "101-3";
	$funct{'queue_submitted'} = "101-4";
	$funct{'check_queue'} = "101-5";
	$funct{'register_1'} = "101-6";
	$funct{'register_2'} = "101-7";
	$funct{'_get_group_info'} = "101-8";
	$funct{'register_thanks'} = "101-9";
	$funct{'user_selfreg_config'} = "101-10";
	$funct{'user_selfreg_config_save'} = "101-11";
	$funct{'userapp_1'} = "101-12";
	$funct{'userapp_framesetter'} = "101-13";
	$funct{'userapp_2'} = "101-14";
	$funct{'userapp_disp'} = "101-15";
	$funct{'userapp_action'} = "101-16";
	$funct{'reg_configurator'} = "101-17";
	$funct{'save_selfreg_config'} = "101-18";
	$funct{'save_selfreg_config_rs'} = "101-19";
	$funct{'save_selfreg_config_welcome'} = "101-20";
	$funct{'define_mime_types'} = "102-1";
	$funct{'parse_filename'} = "102-2";
	$funct{'attachment_upload'} = "102-3";
	$funct{'manage_attachments'} = "102-4";
	$funct{'compare_source'} = "102-5";
	$funct{'email_list'} = "102-6";
	$funct{'access_mgr_2_pro'} = "102-7";
	$funct{'access_edit_configure'} = "102-8";
	$funct{'aedit_pro'} = "102-9";
	$funct{'advanced_access_editor'} = "102-10";
	$funct{'display_profile'} = "103-1";
	$funct{'display_picture'} = "103-2";
	$funct{'compare_profiles_PRO'} = "103-3";
	$funct{'display_profile_differences_PRO'} = "103-4";
	$funct{'read_profile'} = "103-5";
	$funct{'synchronize_PRO'} = "103-6";
	$funct{'profile_editor_screen_PRO'} = "103-7";
	$funct{'image_handler_PRO'} = "103-8";
	$funct{'create_blank_profile_file'} = "103-9";
	$funct{'notify_save_pro'} = "103-10";
	$funct{'prefs_save_pro'} = "103-11";
	$funct{'info_save_pro'} = "103-12";
	$funct{'clear_picture'} = "103-13";
	$funct{'edit_post_form'} = "103-14";
	$funct{'edit_message_action'} = "103-15";
	$funct{'get_preferences'} = "103-16";
	$funct{'pro_cookies'} = "103-17";
	$funct{'validate_auths'} = "103-18";
	$funct{'select_by_subtopics'} = "103-19";
	$funct{'archive_mgr'} = "104-1";
	$funct{'prune_messages'} = "104-2";
	$funct{'ip_ban_form'} = "104-3";
	$funct{'check_ban_ip'} = "104-4";
	$funct{'save_archive_options'} = "104-5";
	$funct{'auto_prune'} = "104-6";
	$funct{'rtf_to_webtags'} = "105-1";
	$funct{'spellcheck'} = "105-2";
	$funct{'profile_fields'} = "105-3";
	$funct{'profile_fields_save'} = "105-4";
	$funct{'queue_mgr_1'} = "106-1";
	$funct{'queue_frames'} = "106-2";
	$funct{'queue_mgr_disp'} = "106-3";
	$funct{'queue_edit'} = "106-4";
	$funct{'queue_action'} = "106-5";
	$funct{'backup_mgr'} = "106-6";
	$funct{'create_backup'} = "106-7";
	$funct{'bumgr'} = "106-8";
	$funct{'restore_action'} = "106-9";
	$funct{'compress_uncompress'} = "106-10";
	$funct{'open_archive_interface'} = "106-11";
	$funct{'manual_restore'} = "106-12";
	undef %evaluated;
}
open (LANG, "$admin_dir/language.conf");
@lang = <LANG>;
close (LANG);
if (-e "$admin_dir/language_pro.conf") {
	open (LANG, "$admin_dir/language_pro.conf");
	@lang2 = <LANG>;
	close (LANG);
	push (@lang, @lang2);
}
undef %lang;
@lang = grep(!/^#/, @lang);
@lang = grep(/\S/, @lang);
$cur = "";
foreach $line (@lang) {
	$line =~ s/##(.*)//;
	$line =~ s/^\s+//;
	$line =~ s/\s+$//;
	if ($line =~ m|^\$(\w+)|) {
		$cur = $1; $del = "";
	} elsif ($line =~ m|^\@(\w+)\((.)\)|) {
		$cur = $1; $del = $2;
	} elsif ($del ne "") {
		@arr = split(/$del/, $line);
		foreach $a (@arr) {
			$a =~ s/^\s+//;
			$a =~ s/\s+$//;
		}
		$evst = "\@$cur = (";
		foreach $a (@arr) {
			$evst .= "'$a',";
		}
		chop ($evst); $evst .= ");";
		eval $evst;
	} elsif ($cur ne "") {
		$L{$cur} .= $line . " ";
	}
}
foreach $key (keys(%L)) {
	$L{$key} =~ s/^\s+//;
	$L{$key} =~ s/\s+$//;
}
&ex('check_ban_ip', 1) if $pro;
1;
