# FILE: src-board-subs-105
#-------------------------------------------------------------------------------
# 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 rtf_to_webtags {
	my ($message_in) = @_;
	$tempreplace125 = "---!" . time . $$ . "!---125";
	$tempreplace123 = "---!" . time . $$ . "!---123";
	$tempreplace92 = "---!" . time . $$ . "!---92";
	$tempreplacetab = "---!" . time . $$ . "!---tab";
	$tempreplacen = "---!" . time . $$ . "!---n";

	$message_in =~ s/\\\}/$tempreplace125/g;
	$message_in =~ s/\\\\/$tempreplace92/g;
	$message_in =~ s/\\\{/$tempreplace123/g;
	$message_in =~ s/\\cell /$tempreplacetab/g;
	$message_in =~ s/\\row /$tempreplacen/g;
	$message_in =~ s/\n/ /g;

	if ($message_in =~ m|\{\\colortbl;([^\}]+)\}|) {
		$ct = $1;
		@ct = split(/;/, $ct);
		$ctr = 1;
		foreach $ct (@ct) {
			$ct =~ m|\\red(\d+)\\green(\d+)\\blue(\d+)|;
			$cl{$ctr} = sprintf("%02lx", $1) . sprintf("%02lx", $2) . sprintf("%02lx", $3);
			$ctr += 1;
		}
	}
	if ($message_in =~ m|\{\\fonttbl\{|) {
		$a = $';
		if ($a =~ m|\}\}|) {
			$m = $`; $m .= "}";
			@fd = split(/\}\s?\{/, $m);
			foreach $line (@fd) {
				if ($line =~ m|^\{?\\f(\d+).*\}(.*);|) {
					$ft{$1} = $2;
				}
			}
		}
	}
	$message_out = "";
	$ct = 0;
	if ($message_in =~ m|\{\\info\s*\{\\title ([^\}]+)\}|) {
		$titlemsg = $1; $message_in = $';
		$titlemsg =~ s/\s+$//g;
		if ($message_in =~ m|$titlemsg|) {
			$message_in = join("", $&, $');
		} else {
			&error_message("Title not found", "The RTF file supplied does not match the found title ($titlemsg).");
		}
	} else {
		&error_message("Title not found", "The RTF file supplied does not have a valid title.");
	}
	undef @brackets;
	while ($message_in =~ m|\{([^\}\{]+)[\}\{]|) {
		$b = $`; $a = $'; $m = $1;
		if ($b ne "") {
			$m = "$b $m";
		}
		push (@brackets, $m);
		$message_in = $a;
	}
	$message_out = ""; $general_closing = ""; $table = 0;
	foreach $b (@brackets) {
		$tableflag = 0;
		$tableflag = 1 if $b =~ /$tempreplacetab/;
		$tableflag = 1 if $b =~ /$tempreplacen/;

		$b =~ s/\\rquote /'/g;
		$b =~ s/\\lquote /'/g;
		$b =~ s/\\ldblquote /"/g;
		$b =~ s/\\rdblquote /"/g;
		$b =~ s/\\'(\d+)/pack("c", hex($1))/ge;
		$this_head = "";
		$this_message = "";
		$this_closing = "";
		@words = split(/\s+/, $b);
		foreach $w (@words) {
			next if $w eq "";
			if ($w =~ m|\\|) {
				if ($w eq '\par') {
					$this_message .= "\n";
				} else {
					($header, $footer) = &rtf_to_webtags_1($w);
					$this_head .= $header; $this_closing .= $footer;
				}
			} else {
				$this_message .= " $w";
			}
		}
		if ($table == 1 && $tableflag == 0) {
			$message_out .= "}";
			$table = 0;
		}
		if ($this_message =~ /\S/) {
			$message_out .= " $this_head";
			$this_message =~ s/^\s//;
			$message_out .= "$this_message$this_closing";
		}
	}
	$message_out =~ s/$tempreplace125/\\\}/g;
	$message_out =~ s/$tempreplace92/\\\\/g;
	$message_out =~ s/$tempreplace123/\\\{/g;
	$message_out =~ s/$tempreplacetab/\t/g;
	$message_out =~ s/$tempreplacen/\n/g;
	$message_out .= "}" if $table;
	$message_out = "\\font{$ft{'0'},$message_out" . "}" if $ft{'0'};
	return $message_out;
}

sub rtf_to_webtags_1 {
	my ($rtf_in) = @_;
	$rtf_out = ""; $rtf_out_close = "";
	$rtf_in .= "\\";
	if ($rtf_in =~ m|\\b\\|) {
		$rtf_out .= '\b{'; $rtf_out_close .= '}';
	}
	if ($rtf_in =~ m|\\i\\|) {
		$rtf_out .= '\i{'; $rtf_out_close .= '}';
	}
	if ($rtf_in =~ m|\\ul\\|) {
		$rtf_out .= '\u{'; $rtf_out_close .= '}';
	}
	if ($rtf_in =~ m|\\qc\\|) {
		$rtf_out .= '\c{'; $rtf_out_close .= '}';
	}
	if ($rtf_in =~ m|\\trowd\\|) {
		$rtf_out .= '\table{' if !$table;
		$table = 1; $tableflag = 1;
	}
	$tableflag = 1 if $rtf_in =~ m|\\intbl\\|;
	if ($rtf_in =~ m|\\f(\d+)\\|) {
		if ($ft{$1} ne "") {
			$rtf_out .= "\\font{$ft{$1},"; $rtf_out_close .= '}';
		}
	}
	if ($rtf_in =~ m|\\fs(\d+)\\|) {
		$s = $1;
		if ($s >= 32) {
			$rtf_out .= '\2{';
		} elsif ($s >= 24) {
			$rtf_out .= '\1{';
		} elsif ($s >= 20) {
			$rtf_out .= '\0{';
		} elsif ($s >= 16) {
			$rtf_out .= '\-1{';
		} else {
			$rtf_out .= '\-2{';
		}
		$rtf_out_close .= "}";
	}
	if ($rtf_in =~ m|\\cf(\d+)\\|) {
		if ($cl{$1} ne "") {
			$rtf_out .= "\\rgb{$cl{$1},"; $rtf_out_close .= '}';
		}
	}
	return ($rtf_out, $rtf_out_close);
}

#---SEPARATOR---#

sub spellcheck {
	my ($newmessage) = @_;
	$newmessage .= " ";
	my (@dictionaries, %seenwords, $dict, $line, @words);
	@dictionaries = split(/,/, $dictionary);
	undef %seenwords;
	$line = $newmessage;
	$line =~ s/<([^>]*)>//g;
	@words = split(/\s+/, $line);
	foreach $line (@words) {
		next if length($line) < 2;
		$line =~ tr/A-Z/a-z/; $line =~ s/&#(\d+);/pack("C", $1)/ge;
		next if $line =~ m|\d|;
		$line =~ s/[\?,\.!]$//g;
		$seenwords{$line} = 1 if $line ne "";
	}
	@mywords = sort keys(%seenwords);
	$lastword = $mywords[scalar(@mywords)-1];
O:	foreach $dict (@dictionaries) {
		if ($dict ne "") {
			if (open (DICT, "$dict")) {
I:				while (<DICT>) {
					s/\s*$//; tr/A-Z/a-z/;
					if ($seenwords{$_} == 1) {
						delete $seenwords{$_};
						last O if scalar(keys(%seenwords)) == 0;
					}
					last I if ($GLOBAL_OPTIONS{'dictionary_not_alphabetical'} != 1 && $_ gt $lastword);
				}
				close (DICT);
			} else {
				&log_error("src-board-subs-105", "spellcheck", "Could not open dictionary file $dict: $!");
			}
		}
	}
	foreach $key (keys(%seenwords)) {
		next if $seenwords{$key} == 0;
		next if $key eq "clipart";
		next if $key eq $L{ILCLIPTAG};
		$key =~ s/([^\w&#; ])/&makeord($1)/ge;
		$newmessage =~ s%\b($key)\b%<FONT COLOR=ff0000><U>$1</U></FONT>%gi;
	}
	if (scalar(keys(%seenwords)) != 0) {
		$scm = "$L{SPELL_ERRORS}\n";
	} else {
		$scm = "";
	}
	$newmessage =~ s/\s+$//;
	return ($newmessage, $scm);
}

#---SEPARATOR---#

sub profile_fields {
	&header;
	print "<HTML><HEAD><TITLE>Profile Field Configurator</TITLE>\n";
	print '<BODY BGCOLOR="ffffff" TEXT="000000" LINK="0000FF" VLINK="0000FF" ';
	print "onLoad=\"window.defaultStatus = 'Profile Field Configurator'\">$fs\n";
	print "<FONT SIZE=3><CENTER><B>Profile Field Configurator</B></CENTER></FONT>\n";
	print "<HR>\n";
	print "<B><A HREF=\"$cgiurlm?cmd=options_mgr&username=$superuser\" onMouseOver=\"window.status = 'Return to Options Manager'; return true\">Options Manager</A>: Profile Field Configurator</B><HR>\n";
	print "<H3>Profile Fields</H3>\n";
	print "<FORM ACTION=\"$cgiurl\" METHOD=POST>\n";
	print "<INPUT TYPE=HIDDEN NAME=username VALUE=$superuser>\n";
	print "<INPUT TYPE=HIDDEN NAME=action VALUE=profile_fields>\n";
	print "<TABLE BGCOLOR=#ffffcc WIDTH=100% BORDER=1><TR><TD>\n";
	open (CONFIG, "$admin_dir/profiles/config.txt"); @config = <CONFIG>; close (CONFIG);
	print "<TABLE>\n";
	print "<TR>\n";
	print "<TD>$fs", "<B>Shown</B></TD>\n";
	print "<TD>$fs", "<B>Field Description</B></TD>\n";
	print "<TD>$fs", "<B>Type</B></TD>\n";
	print "<TD>$fs", "<B>Size</B></TD>\n";
	print "<TD>$fs", "<B>Required</B></TD></TR>\n";
	foreach $line (@config) {
		chop $line if $line =~ m|\n$|;
		($var, $descr, $disp, $type, $size, $req) = split(/:/, $line);
		print "<TR><TD ALIGN=CENTER><INPUT TYPE=CHECKBOX VALUE=1 NAME=$var", "_on";
		print " CHECKED" if $disp;
		print "></TD><TD><INPUT TYPE=TEXT NAME=$var", "_val SIZE=25 VALUE='";
		$d = &unescape($descr);
		$d =~ s/'//g;
		print $d;
		print "'></TD><TD><SELECT NAME=$var", "_type SIZE=1>\n";
		print "<OPTION VALUE=1";
		print " SELECTED" if $type == 1;
		print ">Plain Text\n";
		print "<OPTION VALUE=2";
		print " SELECTED" if $type == 2;
		print ">URL\n";
		print "<OPTION VALUE=3";
		print " SELECTED" if $type == 3;
		print ">E-mail\n";
		print "</SELECT></TD>\n";
		print "<TD><SELECT NAME=$var", "_size SIZE=1>\n";
		print "<OPTION VALUE=1";
		print " SELECTED" if $size == 1;
		print ">1 row\n";
		print "<OPTION VALUE=3";
		print " SELECTED" if $size == 3;
		print ">3 rows\n";
		print "<OPTION VALUE=5";
		print " SELECTED" if $size == 5;
		print ">5 rows\n";
		print "</SELECT></TD>\n";
		print "<TD ALIGN=CENTER><INPUT TYPE=CHECKBOX VALUE=1 NAME=$var", "_req";
		print " CHECKED" if $req == 1;
		print "></TD></TR>\n";
	}
	print "<TR><TD ALIGN=CENTER><INPUT TYPE=CHECKBOX NAME=NEW_on VALUE=1></TD>";
	print "<TD><INPUT TYPE=TEXT NAME=NEW_val SIZE=25></TD>";
	print "<TD><SELECT NAME=NEW_type><OPTION VALUE=1>Plain Text\n<OPTION VALUE=2>URL\n<OPTION VALUE=3>E-mail</SELECT></TD>\n";
	print "<TD><SELECT NAME=NEW_size><OPTION VALUE=1>1 row\n<OPTION VALUE=3>3 rows\n<OPTION VALUE=5>5 rows</SELECT></TD>\n";
	print "<TD ALIGN=CENTER><INPUT TYPE=CHECKBOX NAME=NEW_req VALUE=1></TD></TR>";
	print "</TABLE><P>\n";
	print "<INPUT TYPE=SUBMIT VALUE=\"Save Profile Fields\">\n";
	print "<INPUT TYPE=HIDDEN NAME=save VALUE=1>\n";
	print "</TD></TR></TABLE>\n";
	print "</FORM></BODY></HTML>\n";
	exit(0);
}

#---SEPARATOR---#

sub profile_fields_save {
	&lock("profile_fields_save", "$admin_dir/profiles/config.txt");
	open (CONFIG, "$admin_dir/profiles/config.txt"); @config = <CONFIG>; close (CONFIG);
	foreach $line (@config) {
		chop $line if $line =~ m|\n$|;
		($var, $descr, $disp, $type, $size, $req) = split(/:/, $line);
		if ($FORM{"$var" . "_on"} == 1) {
			$disp = 1;
		} else {
			$disp = 0;
		}
		if ($FORM{"$var" . "_val"} eq "") {
			$line = "";
		} else {
			$descr = &escape($FORM{"$var" . "_val"});
		}
		if ($FORM{"$var" . "_type"} > 0) {
			$type = $FORM{"$var" . "_type"};
		}
		if ($FORM{"$var" . "_size"} > 0) {
			$size = $FORM{"$var" . "_size"};
		}
		if ($FORM{"$var" . "_req"} == 1) {
			$req = 1;
		} else {
			$req = 0;
		}
		if ($line ne "") {
			$line = join(":", $var, $descr, $disp, $type, $size, "$req\n");
		}
	}
	if ($FORM{'NEW_val'} ne "") {
		$var = "NEW";
		if ($FORM{"$var" . "_on"} == 1) {
			$disp = 1;
		} else {
			$disp = 0;
		}
		if ($FORM{"$var" . "_val"} eq "") {
			$line = "";
		} else {
			$descr = &escape($FORM{"$var" . "_val"});
		}
		if ($FORM{"$var" . "_type"} > 0) {
			$type = $FORM{"$var" . "_type"};
		}
		if ($FORM{"$var" . "_size"} > 0) {
			$size = $FORM{"$var" . "_size"};
		}
		if ($FORM{"$var" . "_req"} == 1) {
			$req = 1;
		} else {
			$req = 0;
		}
		$line = join(":", time, $descr, $disp, $type, $size, "$req\n");
		push (@config, $line);
	}
	@config = grep(/\S/, @config);
	open (CONFIG, ">$admin_dir/profiles/config.txt"); print CONFIG @config; close (CONFIG);
	&unlock("profile_fields_save", "$admin_dir/profiles/config.txt");
}

#---SEPARATOR---#

sub exporter {
	my ($format) = @_;
	&header;
	print "<HTML><HEAD><TITLE>Export Discus Data</TITLE></HEAD>\n";
	print "<BODY BGCOLOR=#ffffff TEXT=#000000 LINK=#0000ff VLINK=#0000ff ALINK=#0000ff>\n";
	print "$fs<FONT SIZE=3><CENTER><B>Export Discus Data</B></CENTER></FONT><HR><P>\n";
	if ($format eq "tab") {
		print "You have chosen to export your Discus message base in <B>tab delimited</B> format.\n";
		print "This is suitable for import into other programs, such as spreadsheets or databases.\n";
		print "This file will be created in your <B>backups</B> directory within your Discus administration\n";
		print "directory.  You can obtain this file by FTP or download it from your Backup Manager once\n";
		print "it has been created.<P>\n";
		print "<FORM ACTION=$cgiurl1 METHOD=POST>\n";
		print "<INPUT TYPE=HIDDEN NAME=username VALUE=$superuser>\n";
		print "<INPUT TYPE=HIDDEN NAME=action VALUE=tab_delimited>\n";
		print "<TABLE><TR><TD>$fs", "Destination File Name:</FONT></TD>\n";
		print "<TD><INPUT TYPE=TEXT NAME=filename SIZE=25 VALUE='export'></TD></TR></TABLE><P><UL>\n";
		open (TOP, "$message_dir/$board_topics_file");
		@top = <TOP>;
		close (TOP);
		foreach $line (@top) {
			if ($line =~ m|<!-Top: (\d+)-!>|) {
				$num = $1;
				&extract("//$num/$num.$ext");
				print "<INPUT TYPE=CHECKBOX NAME=topics VALUE=$num CHECKED> $topic_name<BR>\n";
			}
		}
		print "</UL>\n";
		print "<INPUT TYPE=SUBMIT VALUE=\"Export Data\">\n";
		print "<INPUT TYPE=HIDDEN NAME=first VALUE=1>\n";
		print "</FORM>\n";
	} elsif ($format eq "offline") {
		print "You have chosen to export your Discus message base in <B>offline viewing</B> format.\n";
		print "This is suitable for distribution of your message base on a CD-ROM or other media.\n";
		print "All Discus forms will be removed from the files, and any icons that are needed will be\n";
		print "included in the distribution.<P>\n";
		print "This file will be created in your <B>backups</B> directory within your Discus administration\n";
		print "directory.  You can obtain this file by FTP or download it from your Backup Manager once\n";
		print "it has been created.<P>\n";
		print "<FORM ACTION=$cgiurl1 METHOD=POST>\n";
		print "<INPUT TYPE=HIDDEN NAME=username VALUE=$superuser>\n";
		print "<INPUT TYPE=HIDDEN NAME=action VALUE=offline_view>\n";
		print "<TABLE><TR><TD>$fs", "Destination File Name:</FONT></TD>\n";
		print "<TD><INPUT TYPE=TEXT NAME=filename SIZE=25 VALUE='export'></TD></TR></TABLE><P><UL>\n";
		open (TOP, "$message_dir/$board_topics_file");
		@top = <TOP>;
		close (TOP);
		foreach $line (@top) {
			if ($line =~ m|<!-Top: (\d+)-!>|) {
				$num = $1;
				&extract("//$num/$num.$ext");
				print "<INPUT TYPE=CHECKBOX NAME=topics VALUE=$num CHECKED> $topic_name<BR>\n";
			}
		}
		print "</UL>\n";
		print "<INPUT TYPE=SUBMIT VALUE=\"Export Data\">\n";
		print "<INPUT TYPE=HIDDEN NAME=first VALUE=1>\n";
		print "</FORM>\n";
	}
	print "</BODY></HTML>\n";
	exit(0);
}

#---SEPARATOR---#
#REQ:regenerate_board
#REQ:show_gauge

sub tab_delim_export {
	my ($topics, $filename) = @_;
	$filename .= "-tab.exp" if $filename !~ m|\-tab\.exp$|;
	if ($filename =~ m|[^\w\s\.\-]| || $filename =~ m|\.\.|) {
		&error_message("Invalid File Name", "The export file name, $filename, is not valid, as it contains illegal characters");
	}
	if (!-w "$admin_dir/backups/$filename" && -e "$admin_dir/backups/$filename" || !-w "$admin_dir/backups") {
		&error_message("Invalid File Name", "Program cannot write to the requested file ($admin_dir/backups/$filename).");
	}
	if ($FORM{'first'} != 0) {
		unlink("$admin_dir/backups/$filename-tab.exp");
	}
	&regenerate_board("Exporting Message Base", "$cgiurl1?action=tab_delimited&running=1&topics=$topics&username=$superuser&filename=$filename", "$cgiurlm?cmd=importexport&username=$superuser", $topics, 15, $filename, "", "", "Exporting");
}

#---SEPARATOR---#
#REQ:backup_creator

sub pro_branch {
	my ($code, $topic, $page, $argument1, $argument2, $template, $addfile, $force_file) = @_;
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	if ($code == 15) {
		($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
		@msg = grep(/<!-Post: (\d+)-!>/, $message);
		if (scalar(@msg)) {
			@message = split(/\n/, $message); $flag = 0;
			open (FILES, ">>$admin_dir/backups/$argument1");
			foreach $line (@message) {
				if ($line =~ m|<!-Post: (\d+)-!>|) {
					$flag = 1;
					print FILES "$1\t$topic\t$page\t";
					if ($line =~ m|<!-Time: (\d+)-!>|) {
						print FILES "$1\t";
					} else {
						print FILES "0\t";
					}
				} elsif ($flag == 1) {
					$flag = 0;
					if ($line =~ m|<!-Name-!>(.*)<!-/Name-!>|) {
						print FILES "$1\t";
					} else {
						print FILES "\t";
					}
					if ($line =~ m|<!-Email-!>(.*)<!-/Email-!>|) {
						$x = $1;
						if ($x =~ m|<A HREF="mailto:(.*)">|) {
							print FILES "$1\t";
						} elsif ($x =~ m|profile=(.*)">|) {
							print FILES "$1\t";
						} else {
							print FILES "\t";
						}
					}
					if ($line =~ m|<!-Text-!>(.*)<!-/Text-!>|) {
						print FILES $1, "\n";
					} else {
						print FILES "\n";
					}
				}
			}
			close (FILES);
		}
	} elsif ($code == 16) {
		if ($force_file == 1) {
			open (FILE, "$message_dir/$board_topics_file"); @file = <FILE>;
			$filename = "$message_dir/$board_topics_file";
		} elsif (open(FILE, "$message_dir/$topic/$page.$ext")) {
			@file = <FILE>; $filename = "$message_dir/$topic/$page.$ext";
		} elsif (open(FILE, "$secdir/$topic/$page.$ext")) {
			@file = <FILE>; $filename = "$secdir/$topic/$page.$ext";
		} else {
			return 0;
		}
		close (FILE);
		$flag = 0;
		foreach $line (@file) {
			if ($line =~ m|<!--Create-->|) {
				$flag = 1; $line = "";
			} elsif ($line =~ m|<!--Add-->|) {
				$flag = 1; $line = "";
			} elsif ($line =~ m|<!--/(\w+)-->|) {
				$flag = 0; $line = "";
			} elsif ($line =~ m|<SCRIPT LANGUAGE="JavaScript"|i) {
				$flag = 1; $line = "";
			} elsif ($line =~ m|</SCRIPT>|i) {
				$flag = 0; $line = "";
			} elsif ($line =~ m| onLoad="[^"]+"|) {
				$line = join("", $`, $');
			} elsif ($line =~ m|<!--Off|) {
				$flag = 1; $line = "";
			} elsif ($line =~ m|<!--Source:|) {
				$flag = 1; $line = "";
			} elsif ($line =~ m|-->|) {
				$flag = 0; $line = "";
			} elsif ($line =~ m|<!--Message Source|) {
				$flag = 1; $line = "";
			} elsif ($line =~ m|<!--Description Source|) {
				$flag = 1; $line = "";
			} elsif ($flag == 1) {
				$line = "";
			} elsif ($line =~ m%<META NAME="(DESCRIPTION)|(KEYWORDS)|(ROBOTS)">%) {
				$line = "";
			} elsif ($line =~ m|<!-(.*?)-!>|) {
				while ($line =~ m|<!-(.*?)-!>|) {
					$line = join("", $`, $');
				}
			} elsif ($line =~ m|<!--|) {
				$line = "";
			}
			while ($line =~ m|$message_url/(\d+)/(\d+)\.$ext([^"]+)"|) {
				$line = join("", $`, "$2.$ext\"", $') if $1 == $topic;
				$line = join("", $`, "../$1/$2.$ext\"", $') if ($1 != $topic && $force_file != 1);
				$line = join("", $`, "$1/$2.$ext\"", $') if ($1 != $topic && $force_file == 1);
			}
			while ($line =~ m|$message_url|) {
				$line = join("", $`, "..", $') if $force_file != 1;
				$line = join("", $`, "", $') if $force_file == 1;
			}
			while ($line =~ m|$script_url/board-auth\.$cgi_extension\?file=/(\d+)/(\d+)\.$ext([^">]+)|) {
				$line = join("", $`, "$1/$2.$ext", $');
			}
			while ($line =~ m|<A HREF="$script_url/([^">]+)|i) {
				$line = join("", $`, "<A HREF=\"#", $');
			}
			while ($line =~ m|<A HREF=$script_url/([^\s>]+)|i) {
				$line = join("", $`, "<A HREF=\"#\"", $');
			}
			while ($line =~ m|$html_url/icons/|) {
				$line = join("", $`, "../../icons/", $') if $force_file != 1;
				$line = join("", $`, "../icons/", $') if $force_file == 1;
			}
			while ($line =~ m|$html_url/clipart/|) {
				$line = join("", $`, "../../clipart/", $') if $force_file != 1;
				$line = join("", $`, "../clipart/", $') if $force_file == 1;
			}
		}
		@file = grep(/\S/, @file);
		$file = join("", @file);
		($filetype, $content, $mode, $uid, $gid, $mtime) = &tar_format_file($filename);
		open (NEWFILE, ">>$admin_dir/backups/$argument1.exp");
		binmode(NEWFILE);
		if ($filename =~ m|/(\d+)/(\d+)\.$ext|) {
			$filenamenew = "messages/$1/$2.$ext";
		} elsif ($filename =~ m|$board_topics_file|) {
			$filenamenew = "messages/$board_topics_file";
		}
		print NEWFILE &format_tar_entry($filenamenew, $filetype, $file, $mode, $uid, $gid, $mtime);
		close (NEWFILE);
	}
}

#---SEPARATOR---#

sub adm_eml_config {
	&header;
	print "<HTML><HEAD><TITLE>Administration by e-mail: Configuration</TITLE>\n";
	print '<BODY BGCOLOR="ffffff" TEXT="000000" LINK="0000FF" VLINK="0000FF" ';
	print "onLoad=\"window.defaultStatus = 'Configuration of Administration by E-mail'\">$fs\n";
	print "<FONT SIZE=3><CENTER><B>Configuration of Administration by E-mail</B></CENTER></FONT>\n";
	print "<HR>\n";
	print "<B><A HREF=\"$cgiurlm?cmd=options_mgr&username=$superuser\" onMouseOver=\"window.status = 'Return to Options Manager'; return true\">Options Manager</A>:\n";
	print "<A HREF=\"$cgiurlm?username=$superuser&cmd=email_configurator\" onMouseOver=\"window.status = 'Return to E-mail Configurator'; return true\">E-mail Configurator</A>:\n";
	print "Configuration of Administration by E-mail</B><HR><P>\n";
	if (!-w "$admin_dir/email-admin.txt") {
		print "<B><FONT COLOR=#ff0000>Setup Error</FONT></B><P>\n";
		print "The file 'email-admin.txt' is not present or is not writable by your WWW server.\n";
		print "This is likely to occur if you are upgrading from a previous version of Discus 3.10 beta.\n";
		print "To resolve this problem, please do the following:<P>\n";
		print "1) Upload a blank file called <B>email-admin.txt</B> into your Discus administration directory.<P>\n";
		print "2) Change permissions on this file to 0777 (rwxrwxrwx) if on a unix server.<P>\n";
		print "3) <A HREF=$cgiurl3?action=adm_eml_config&username=$superuser>Click here</A> to reload this interface, and this message should be gone.<P>\n";
		print "</FONT></BODY></HTML>\n";
		exit(0);
	}
	print "<B>Documentation</B><P>\n";
	print "Please consult <A HREF=http://www.discusware.com/support/resources/reply TARGET=_blank>DiscusWare's resources</A>\n";
	print "regarding this feature.<P>\n";
	print "<B>Recognized Commands</B><P>\n";
	print "<FORM ACTION=$cgiurl3 METHOD=POST>\n";
	print "<TABLE BORDER=1><TR><TD>$fs<B>Delete</B></FONT></TD><TD>$fs<B>Command&nbsp;Keyword</B></FONT></TD><TD>$fs<B>Interpretation</B></FONT></TD><TD>$fs<B>Parameters</B></FONT></TD></TR>\n";
	open (CONFIG, "$admin_dir/email-admin.txt");
	@config = <CONFIG>;
	close (CONFIG);
	$counter = 0;
	@options = ('', "Delete Message", "Delete Conversation", "Approve Message", "Reject Message", "Move Message to ({topic}/{page})", "Close Conversation", "Annotate Message - Top", "Annotate Message - Bottom", "No E-mail Notification");
	foreach $line (@config) {
		chomp $line;
		($command, $interpretation, $parameters) = split(/\t/, $line);
		print "<TR><TD><INPUT TYPE=SUBMIT NAME=Delete_$counter VALUE=\"Delete\"></TD>\n";
		print "<TD><INPUT TYPE=TEXT NAME=command_$counter SIZE=16 VALUE=\"$command\"></TD>\n";
		print "<TD><SELECT NAME=interpretation_$counter SIZE=1>\n";
		for ($i = 1; $i < scalar(@options); $i++) {
			print "<OPTION VALUE=$i";
			print " SELECTED" if $interpretation == $i;
			print ">", $options[$i], "\n";
		}
		print "</SELECT></TD>\n";
		print "<TD><INPUT TYPE=TEXT NAME=param_$counter SIZE=10 VALUE=\"$parameters\"></TD></TR>\n";
		$counter += 1;
	}
	print "<TR><TD>&nbsp;</TD>\n";
	print "<TD><INPUT TYPE=TEXT NAME=command_$counter SIZE=16 VALUE=\"\"></TD>\n";
	print "<TD><SELECT NAME=interpretation_$counter SIZE=1>\n";
	for ($i = 1; $i < scalar(@options); $i++) {
		print "<OPTION VALUE=$i>", $options[$i], "\n";
	}
	print "</SELECT></TD>\n";
	print "<TD><INPUT TYPE=TEXT NAME=param_$counter SIZE=10 VALUE=\"\"></TD></TR>\n";
	print "</TABLE><P>\n";
	print "<INPUT TYPE=SUBMIT VALUE=\"Save Parameters\">\n";
	print "<INPUT TYPE=HIDDEN NAME=username VALUE=$superuser>\n";
	print "<INPUT TYPE=HIDDEN NAME=action VALUE=adm_eml_save>\n";
	print "</FORM></FONT></BODY></HTML>\n";
	exit(0);
}

#---SEPARATOR---#

sub adm_eml_save {
	if (!-w "$admin_dir/email-admin.txt") {
		&error_message("Save Options Error", "File $admin_dir/email-admin.txt is not writable!");
	}
	&lock("adm_eml_save", "$admin_dir/email-admin.txt");
	open (CONFIG, "$admin_dir/email-admin.txt");
	@config = <CONFIG>;
	close (CONFIG);
	if (grep(/^\s+$/, @config)) {
		@config = grep(/\S/, @config);
		open (CONFIG, ">$admin_dir/email-admin.txt");
		print CONFIG @config;
		close (CONFIG);
		&unlock("adm_eml_save", "$admin_dir/email-admin.txt");
		&error_message("Corrupted E-mail Administration File", "The email-admin.txt file was corrupted (edited by hand?).  The file has been repaired but your changes were not made.  <A HREF=$cgiurl3?action=adm_eml_config&username=$superuser>Click here</A> to try again.", 0, 1);
	}
	if (($keydel) = grep(/^Delete_/, keys(%FORM))) {
		$keydel =~ m|^Delete_(\d+)|; $keynum = $1;
		if ($keynum !~ m|^\d+$|) {
			&unlock("adm_eml_save", "$admin_dir/email-admin.txt");
			return 1;
		}
		$config[$keynum] = "";
		@config = grep(/\S/, @config);
		open (CONFIG, ">$admin_dir/email-admin.txt");
		print CONFIG @config;
		close (CONFIG);
		&unlock("adm_eml_save", "$admin_dir/email-admin.txt");
		return 1;
	}
	$counter = 0;
	$x = scalar(@config);
	if ($FORM{"command_$x"} ne "") {
		push (@config, "REPLACE ME\n");
	}
	foreach $line (@config) {
		chomp $line;
		($command, $interpretation, $parameters) = split(/\t/, $line);
		next if $FORM{"command_$counter"} eq "";
		$interpretation = $FORM{"interpretation_$counter"};
		if ($FORM{"command_$counter"} !~ m|^\{(\S+)\}$|) {
			&unlock("adm_eml_save", "$admin_dir/email-admin.txt");
			&error_message("Badly Formed Command", "The command " . $FORM{"command_$counter"} . " is not of the format <B>{command_string}</B> (with the curly braces}, or the command string has spaces.  Read that documentation!");
		}
		if ($interpretation < 1 || $interpretation > 9) {
			&unlock("adm_eml_save", "$admin_dir/email-admin.txt");
			&error_message("Bad Interpretation", "The command " . $FORM{"command_$counter"} . " does not have a valid interpretation associated with it.");
		}
		if ($interpretation == 5) {
			$param = $FORM{"param_$counter"};
			if ($param =~ m|^(\d+)/(\d+)$|) {
				($t, $p) = ($1, $2);
				if (!-e "$message_dir/$t/$p.$ext" && !-e "$secdir/$t/$p.$ext") {
					&unlock("adm_eml_save", "$admin_dir/email-admin.txt");
					&error_message("Bad Parameter", "The command " . $FORM{"command_$counter"} . " does not have a valid parameter (the page referenced by the topic and page number provided does not exist on this board).  Read that documentation!");
				}
			} else {
				&unlock("adm_eml_save", "$admin_dir/email-admin.txt");
				&error_message("Bad Parameter", "The command " . $FORM{"command_$counter"} . " does not have a valid parameter (must be of the format topic/page separated with a slash, and the topic and page must be numbers). Read that documentation!");
			}
		} else {
			$param = "";
		}
		$line = join("\t", $FORM{"command_$counter"}, $FORM{"interpretation_$counter"}, $FORM{"param_$counter"});
		$line .= "\n";
		$counter += 1;
	}
	open (CONFIG, ">$admin_dir/email-admin.txt");
	print CONFIG @config;
	close (CONFIG);
	&unlock("adm_eml_save", "$admin_dir/email-admin.txt");
}

#---SEPARATOR---#
#REQ:remove_message
#REQ:send_email_message
#REQ:remove_page
#REQ:queue_action
#REQ:change_layout
#REQ:move_message
#REQ:webtags
#REQ:read_source
#REQ:remove_swearing
#REQ:save_edited_message
#REQ:inverse_webtags

sub adm_eml_action {
	my ($command, $topic, $page, $postnum, $username, $passwd, $message_in, $donothing, $crypted_pw) = @_;
	require "$admin_dir/source/src-board-subs-admin";
	$FORM{'username'} = $username;
	$FORM{'password'} = $passwd;
	if ($crypted_pw ne "") {
		delete $FORM{"password"};
		$COOKIE{"pass$COOKIE_ID"} = $crypted_pw;
		print "Crypted password is $crypted_pw!\n" if $DEBUGGING;
		print "COOKIE: pass$COOKIE_ID = ", $COOKIE{"pass$COOKIE_ID"}, "\n" if $DEBUGGING;
		print "Username is $FORM{'username'}\n" if $DEBUGGING;
	}
	&check_passwd(0);
	$x_send_flag = 0;
	$x_send_flag = 1 if $EMAIL_MOD =~ m|^([\w\.\-\+]+)\@([\w\.\-\+]+)$|;
	&extract ("//$topic/$page.$ext");
	$reply_message =  "* Moderator username \U$username\E authenticated for commands.\n";
	$reply_message .= "* Moderator information: $FULLNAME_MOD.\n";
	$reply_message .= "* Action spot: $topic/$page.\n";
	$reply_message .= "* Action topic: $topic_name.\n";
	$reply_message .= "* Action page: $me_name.\n";
	if (&verify_owner($owner,$FORM{'username'})) {
		$reply_message .= "* Permission verified.\n";
	} else {
		$reply_message .= "* Permission denied.\n";
		&send_email_message($EMAIL_MOD, "Command: Permission Denied", "You do not have permission to edit this page!\n\n" . $reply_message) if $x_send_flag == 1;
		return 0;
	}
	open (CONFIG, "$admin_dir/email-admin.txt");
	@config = <CONFIG>;
	close (CONFIG);
	undef %PAR;
	undef %INT;
	foreach $line (@config) {
		chomp $line;
		($cmd, $par, $int) = split(/\t/, $line);
		$cmd =~ m|\{(\S+)\}|; $cmd = $1;
		$PAR{$cmd} = $int;
		$INT{$cmd} = $par;
	}
	while ($command =~ m|\{(\S+)\}|g) {
		$match = $1;
		print "Found command {$match} -- interpretation $INT{$match}\n" if $DEBUGGING;
		$reply_message .= "* Command {$match} found\n";
		$reply_message .= "* Command interpretation ";
		$reply_message .= "NOT FOUND!\n" if $INT{$match} == 0;
		$reply_message .= "has code $INT{$match}\n" if $INT{$match} != 0;
		next if $INT{$match} == 0;
		if ($INT{$match} == 1) {
			next if $donothing;
			$reply_message .= "* Command: Delete $postnum from $topic/$page\n";
			print "Attempting to remove post $postnum from $topic/$page\n" if $DEBUGGING;
			next if $postnum == 0;
			$status = &remove_message($topic, $page, $postnum);
			$reply_message .= "* Command successful!\n" if $status == 1;
			$reply_message .= "* Command FAILURE!\n" if $status != 1;
			print "Command successful!\n" if ($status == 1 && $DEBUGGING);
			print "Command failure!\n" if ($status != 1 && $DEBUGGING);
			next;
		}
		if ($INT{$match} == 2) {
			$reply_message .= "* Command: Delete $topic/$page conversation\n";
			next if $donothing;
			&extract("//$topic/$page.$ext");
			print "Attempting to remove entire page $topic/$page from $topic/$parent_number\n" if $DEBUGGING;
			if ($parent_number == 0) {
				$reply_message .= "* Parent number not found for page.\n* Delete command FAILED!\n";
				print "Parent number not found for $topic/$page; command FAILED\n" if $DEBUGGING;
				next;
			} elsif ($page == $topic) {
				$reply_message .= "* Page number is equal to topic number.\n* Moderators cannot delete topics!\n* Delete command FAILED!\n";
				print "Parent number ($parent_number) equals topic number for $topic/$page; permission DENIED\n" if $DEBUGGING;
				next;
			}
			print "Sending command...\n" if $DEBUGGING;
			$status = &remove_page($topic, $parent_number, $page);
			print "Page remove returned status ($status)\n" if $DEBUGGING;
			$reply_message .= "* Command successful!\n" if $status == 1;
			$reply_message .= "* Command FAILURE!\n" if $status != 1;
			print "Command successful!\n" if ($status == 1 && $DEBUGGING);
			print "Command failure!\n" if ($status != 1 && $DEBUGGING);
			next;
		}
		if ($INT{$match} == 3 || $INT{$match} == 4) {
			$reply_message .= "* Command: Approve message in queue\n" if $INT{$match} == 3;
			$reply_message .= "* Command: Reject message in queue\n" if $INT{$match} == 4;
			next if $donothing;
			print "Attempting to approve(3)/refuse(4) [$INT{$match}] message in queue\n" if $DEBUGGING;
			$status = &queue_action($username, $postnum, $INT{$match}, 0, 1, 1);
			if ($status == -1) {
				$reply_message .= "* Queue Error: Message not found!\n";
				$reply_message .= "> This could happen if another moderator has already approved or\n";
				$reply_message .= "> rejected this message.  Check the relevant page to see if this\n";
				$reply_message .= "> post was approved or rejected.\n";
				$reply_message .= "* Command FAILED!\n";
				print "Message not in queue; command FAILED\n" if $DEBUGGING;
				next;
			}
			if ($status == 1) {
				$reply_message .= "* Queue Manager reports message was successfully ";
				$reply_message .= "accepted!\n" if $INT{$match} == 3;
				$reply_message .= "rejected!\n" if $INT{$match} == 4;
				$reply_message .= "* Command successful!\n";
				print "Command successful!\n" if ($status == 1 && $DEBUGGING);
				next;
			} else {
				$reply_message .= "* Command FAILED for unknown reason!\n";
				print "Command failure!\n" if ($status != 1 && $DEBUGGING);
			}
		}
		if ($INT{$match} == 6) {
			next if $donothing;
			$reply_message .= "* Command: Close conversation (removes 'Add a Message' box)\n";
			print "Attempting to close conversation on $topic/$page\n" if $DEBUGGING;
			&extract("//$topic/$page.$ext");
			if ($param !~ m|Add|) {
				$reply_message .= "* Conversation is already closed.  Nothing to do.\n";
				print "Current parameters [$param] -- nothing to do.\n" if $DEBUGGING;
				next;
			}
			print "Current parameters [$param]\n" if $DEBUGGING;
			$param =~ s/Add//g;
			print "New parameters [$param]\n" if $DEBUGGING;
			$status = &change_layout($topic, $page, $param);
			if ($status == 1) {
				$reply_message .= "* 'Add a Message' box removed successfully.\n";
				$reply_message .= "* Command successful!\n";
				print "Command successful!\n" if $DEBUGGING;
			} else {
				$reply_message .= "* Command FAILED!\n";
				print "Command failed!\n" if $DEBUGGING;
			}
			next;
		}
		if ($INT{$match} == 5) {
			next if $donothing;
			$context = "message";
			$reply_message .= "* Command: move $context to new location $PAR{$match}\n";
			$reply_message .= "* Checking your permission to do so...\n";
			print "Command to move $context to $PAR{$match}\n" if $DEBUGGING;
			($new_topic, $new_page) = split(/\//, $PAR{$match});
			if ($new_topic !~ m|^\d+$| || $new_page !~ m|^\d+$|) {
				$reply_message .= "* Command FAILED: new destination is not valid!\n";
				print "FAILURE: $new_topic or $new_page not numeric!\n" if $DEBUGGING;
				next;
			}
			if (!-e "$message_dir/$new_topic/$new_page.$ext" && !-e "$secdir/$new_topic/$new_page.$ext") {
				$reply_message .= "* Command FAILED: new destination does not exist!\n";
				print "FAILURE: $new_topic/$new_page does not exist\n" if $DEBUGGING;
				next;
			}
			&extract("//$new_topic/$new_page.$ext");
			if (&verify_owner($owner,$username)) {
				$reply_message .= "* Permission verified to move $context.\n";
				print "Permission OK for user $username in group $owner\n" if $DEBUGGING;
			} else {
				$reply_message .= "* PERMISSION DENIED: User $username cannot move $context into topic $new_topic!\n";
				print "Permission DENIED for user $username in group $owner\n" if $DEBUGGING;
				next;
			}
			if ($INT{$match} == 5) {
				($np, $nt) = &move_message($topic, $page, $postnum, $new_topic, $new_page);
				if ($nt == $new_topic && $np == $new_page) {
					$reply_message .= "* Message $postnum successfully moved!\n";
					$reply_message .= "* Command successful!\n";
					print "Command to move message $postnum succeeded!\n" if $DEBUGGING;
				} else {
					$reply_message .= "* Command FAILED!\n";
					print "Command failed!\n" if $DEBUGGING;
				}
				next;
			}
		}
		if ($INT{$match} == 7 || $INT{$match} == 8) {
			return 1 if $donothing;
			$reply_message .= "* Command: Annotate message on top\n" if $INT{$match} == 7;
			$reply_message .= "* Command: Annotate message on bottom\n" if $INT{$match} == 8;
			if ($message_in eq "") {
				$reply_message .= "* Failed: You didn't enter any text!\n";
				$reply_message .= "* Command FAILED!\n";
				print "Command failed!\n" if $DEBUGGING;
				next;
			}
			&extract("//$topic/$page.$ext");
			if (&verify_owner($owner,$username)) {
				$reply_message .= "* Permission verified to edit message.\n";
				print "Permission OK for user $username in group $owner\n" if $DEBUGGING;
			} else {
				$reply_message .= "* PERMISSION DENIED: User $username cannot edit message in topic $new_topic!\n";
				print "Permission DENIED for user $username in group $owner\n" if $DEBUGGING;
				next;
			}
			$message_src = "";
			$message_src = &read_source($topic, $page, $postnum) if $GLOBAL_OPTIONS{'nosource'} == 0;
			if ($message_src eq "") {
				($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic,$page);
				$topic_number = $topic;
				@msg = split(/\n/, $message); $flag = 0;
				foreach $line (@msg) {
					if ($line =~ m|<!-Post: (\d+)-!>|) {
						$flag = 1 if $1 == $postnum;
					} elsif ($line =~ m|<!-/Post: (\d+)-!>|) {
						$flag = 0;
					} elsif ($flag && $line =~ m|<!-Text-!>(.*)<!-/Text-!>|) {
						$formatted = $1;
						$message_src = &inverse_webtags($formatted);
					}
				}
			} else {
				$message_src = &unescape($message_src);
			}
			print "Message source length ", length($message_src), "\n" if $DEBUGGING;
			if ($message_src eq "") {
				$reply_message .= "* Failed: Original message did not exist (perhaps it was moved or archived)!\n";
				$reply_message .= "* Command FAILED!\n";
				print "Command failed!\n" if $DEBUGGING;
				next;
			}
			$new_src = join("", $message_src, $message_in) if $INT{$match} == 8;
			$new_src = join("", $message_in, $message_src) if $INT{$match} == 7;
			($lint, $message) = &webtags($new_src, 0, 1);
			if ($lint eq "!Error") {
				$reply_message .= "* Failed: Formatting Error\n";
				$reply_message .= "\n";
				$reply_message .= $message;
				$reply_message .= "\n";
				$reply_message .= "* Command FAILED!\n";
				print "Command failed! Formatting error\n" if $DEBUGGING;
				next;
			}
			if ($GLOBAL_OPTIONS{'profanity'}) {
				($message) = &remove_swearing($message, 0, 1) if ($GLOBAL_OPTIONS{'profanity_detected'} == 1 || $GLOBAL_OPTIONS{'profanity_detected'} eq "");
			}
			&save_edited_message($topic, $page, $postnum, &escape($new_src), $message, $FORM{"username"});
			$reply_message .= "* Message successfully edited.\n";
			$reply_message .= "* Characters: " . length($message_src) . " -> " . length($new_src) . "\n";
			$reply_message .= "* " . length($message_in) . " characters added to ";
			$reply_message .= "TOP of message\n" if $INT{$match} == 7;
			$reply_message .= "BOTTOM of message\n" if $INT{$match} == 8;
			$reply_message .= "* Command successful!\n";
			print "Successful edit!" if $DEBUGGING;
		}
		if ($INT{$match} == 9) {
			$reply_message .= "* Command: Do not send e-mail notification\n";
			$x = 0;
			if ($GLOBAL_OPTIONS{'moderator_turn_off_email'} == 1) {
				$x = 1;
				$reply_message .= "* Moderator recognized: Command accepted\n";
			} elsif ($FORM{'username'} eq $superuser) {
				$x = 1;
				$reply_message .= "* Superuser recognized: Command accepted\n";
			} else {
				$x = 0;
				$reply_message .= "* Failed: This command is not available to you\n";
			}
			$SKIP_NOTIFY_FLAG = $x;
		}
	}
	$reply_message .= "* Concluded.\n";
	return 0 if $donothing;
	&send_email_message($EMAIL_MOD, "Command: Received", $reply_message) if $x_send_flag == 1;
}

#---SEPARATOR---#
#REQ:regenerate_board
#REQ:backup_creator
#REQ:pro_branch
#REQ:show_gauge

sub offline_view {
	my ($topics, $filename) = @_;
	$filename .= "-offline" if $filename !~ m|\-offline$|;
	if ($filename =~ m|[^\w\s\.\-]| || $filename =~ m|\.\.|) {
		&error_message("Invalid File Name", "The export file name, $filename, is not valid, as it contains illegal characters");
	}
	if (!-w "$admin_dir/backups/$filename" && -e "$admin_dir/backups/$filename" || !-w "$admin_dir/backups") {
		&error_message("Invalid File Name", "Program cannot write to the requested file ($admin_dir/backups/$filename).");
	}
	if ($FORM{'first'} != 0) {
		unlink("$admin_dir/backups/$filename.exp");
	}
	if ($FORM{'running'} < 2) {
		&regenerate_board("Exporting Message Base", "$cgiurl1?action=offline_view&running=1&topics=$FORM{'topics'}&username=$superuser&filename=$filename", "$cgiurl1?action=offline_view&running=2&topics=$topics&username=$superuser&filename=$filename", $topics, 16, $filename, "", "", "Exporting");
	} elsif ($FORM{'running'} == 2) {
		undef @filelist;
		opendir(DIR, "$html_dir/icons");
		while ($dir = readdir(DIR)) {
			push (@filelist, "$html_dir/icons/$dir") if $dir !~ m|^\.+$|;
		}
		closedir(DIR);
		opendir(DIR, "$html_dir/clipart");
		while ($dir = readdir(DIR)) {
			push (@filelist, "$html_dir/clipart/$dir") if $dir !~ m|^\.+$|;
		}
		closedir(DIR);
		open (NEWFILE, ">>$admin_dir/backups/$filename.exp");
		binmode(NEWFILE);
		foreach $file (@filelist) {
			$hdir = quotemeta($html_dir);
			if ($file =~ m|^$hdir/(.*)|) {
				$fn_new = $1;
				($filetype, $content, $mode, $uid, $gid, $mtime) = &tar_format_file($file);
				print NEWFILE &format_tar_entry($fn_new, $filetype, $content, $mode, $uid, $gid, $mtime);
			}
		}
		close (NEWFILE);
		&pro_branch(16, "", "", $filename, "", "", "", 1);
		&seturl("$cgiurl1?action=offline_view&running=3&topics=$FORM{'topics'}&username=$superuser&filename=$filename");
	} elsif ($FORM{'running'} == 3) {
		$firsttopic = 0;
		open (TOP, "$message_dir/$board_topics_file"); @top = <TOP>; close (TOP);
		@top = grep(/^<!-Top:/, @top);
		$flag = 0; $mytopic = 0; @tpc = split(/,/, $FORM{'topics'}); $ctr = 0;
		foreach $line (@top) {
			$line =~ m|<!-Top: (\d+)-!>|;
			$o = $1;
			next if !grep(/^$o$/, @tpc);
			$ctr += 1;
			$firsttopic = $o if $firsttopic == 0;
			if ($flag == 1) {
				$mytopic = $o; $flag = 0; $mx_ctr = $ctr;
			}
			$flag = 1 if $1 == $FORM{'crtopic'};
		}
		$mytopic = $firsttopic if $FORM{'crtopic'} eq "";
		&seturl("$cgiurlm?cmd=importexport&username=$superuser") if $mytopic == 0;
		$dir = "$message_dir/$mytopic" if -e "$message_dir/$mytopic";
		$dir = "$secdir/$mytopic" if -e "$secdir/$mytopic";
		open (NEWFILE, ">>$admin_dir/backups/$filename.exp");
		binmode(NEWFILE);
		opendir(DIR, "$dir");
		while ($file = readdir(DIR)) {
			next if $file =~ m|^(\d+)\.$ext$|;
			next if $file =~ m|^\.+$|;
			next if $file =~ m|^(\d+)\.$ext\.BAD$|;
			next if $file =~ m|^(\d+)\.$ext\.NEW$|;
			$fn_new = "messages/$mytopic/$file";
			($filetype, $content, $mode, $uid, $gid, $mtime) = &tar_format_file("$dir/$file");
			print NEWFILE &format_tar_entry($fn_new, $filetype, $content, $mode, $uid, $gid, $mtime);
		}
		closedir(DIR);
		close(NEWFILE);
		&show_gauge("Exporting Message Base", "Transferring attachments", $mx_ctr/$ctr, "$cgiurl1?action=offline_view&running=3&topics=$FORM{'topics'}&username=$superuser&filename=$filename&crtopic=$mytopic");
	}
	&seturl("$cgiurlm?cmd=importexport&username=$superuser") if $mytopic == 0;

}

# END - FILE IS CORRECTLY UPLOADED #
