#!C:/Perl/bin/perl.exe
$discus_conf = 'd:/inetpub/wwwroot/wielens/discus_admin/discus.conf';
$pro_fileid = '3348162969543224';
#Discus board new messages script
#-------------------------------------------------------------------------------
# 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.
#-------------------------------------------------------------------------------
# To enable multiple selection of topics, you can enable one of
# the following two options.  However, this makes the interface
# look not-so-good.
# $multiple =  "MULTIPLE SIZE=1";
# $multiple =  "MULTIPLE";
# If you want the "do not use 7 day limit" enabled, uncomment this:
# $nolimit = 1;
# If you want more characters in the username, change this value:
$max_name_chars = 20;
#------------------------------------------------------------------------------
if (open (FILE, "$discus_conf")) {
	@file = <FILE>;
	close (FILE);
	foreach $line (@file) {
		if ($line =~ /^(\w+)=(.*)/) {
			$varname = $1;
			$value = $2;
			$value =~ s/\r//g;
			${$varname} = $value;
		}
	}
	require "$admin_dir/source/src-board-subs-common";
} else {
	print "Content-type: text/html\n\n";
	print "<HTML><HEAD><TITLE>Script Execution Error</TITLE></HEAD>\n";
	print "<BODY BGCOLOR=#ffffff TEXT=#000000>\n";
	print "<H1>Script Execution Error</H1>\n";
	print "Discus scripts could not execute because the discus.conf file\n";
	print "could not be opened.";
	print "<P>Reason: <FONT COLOR=#ff0000><B>$!</B></FONT>" if $!;
	print "<P>This generally indicates a setup error of some kind.\n";
	print "Consult the <A HREF=\"http://www.discusware.com/support\">Discus ";
	print "Resource Center</A> for troubleshooting information.</BODY></HTML>\n";
	exit(0);
}
$version_number = join(".", $release_version, $free_revision) if !$pro;
$version_number = join(".", $release_version, $pro_revision) if $pro;
$version_number .= "(W95)" if ($platform eq "win95" || $win95);
$version_number .= "(NT)" if ($platform eq "NT" || $NT);
$version_number .= "*PRO*" if $pro;
# This tries to keep long lines out of no frames interface
$maxchar = 40;
$maxchar = 25 if $interface eq "noframes";
&parse_form;
&read_cookie;
($time) = &ex('get_date_time', "long");
&error_message($L{FILELOCKERROR}, $L{NOTICE_MAINTENANCEMODE}, 0, 1, 1) if $GLOBAL_OPTIONS{'maintenance'};
#########################################################
# These are now set through the Options Manager
if ($GLOBAL_OPTIONS{'nmbold'} == 1 || $GLOBAL_OPTIONS{'nmbold'} eq "") {
	$bopen = "<B>"; $bclose = "</B>";
} else {
	$bopen = ""; $bclose = "";
}
$CHECKED{'last'} = "CHECKED" if $GLOBAL_OPTIONS{'nm_default'} == 0;
$CHECKED{'user'} = "CHECKED" if $GLOBAL_OPTIONS{'nm_default'} == 1;
$CHECKED{'since'} = "CHECKED" if $GLOBAL_OPTIONS{'nm_default'} == 2;
$CHECKED{'treeview'} = "CHECKED" if ($GLOBAL_OPTIONS{'nmtreev'} == 1 || $GLOBAL_OPTIONS{'nmtreev'} eq "");
#########################################################
# This is the static page that gets spit out
if ($FORM{'method'} eq "" || ($FORM{'method'} eq "mynew" && $COOKIE{"user$COOKIE_ID"} eq "")) {
	$count = 0;
	if (open (TOP, "$message_dir/$board_topics_file")) {
		@top = <TOP>;
		close (TOP);
		@top = grep(/<!-Top: (\d+)-!>/, @top);
	} else {
		&header;
		print "$fs<CENTER><B>Invalid HTML Directory Specification</B></CENTER><P>\n";
		print "The HTML directory (or $board_topics_file file) could not be found.\n";
		print "See your <A HREF=$script_url/diagnose.$cgi_extension>diagnostic";
		print " script</A> for more information.<P>\n";
		print "Discus $version_number\n";
		exit(0);
	}	
	$count = 0; $last = 0; $lastline = "";
	if ($GLOBAL_OPTIONS{'nmshowdate'} == 1 || $GLOBAL_OPTIONS{'nmshowdate'} eq "") {
		foreach $topic (@top) {
			$topic =~ m|<!-Top: (\d+)-!>|;
			open (LOG, "$admin_dir/msg_index/$1-log.txt");
			while (<LOG>) {
				next if !/\S/;
				$count += 1;
				@c = split(/;/, $_);
				if ($c[2] > $last) {
					$last = $c[2];
					$lastline = $_;
				}
			}
			close (LOG);
		}
	}
	$option_string = "";
	$when = (split(/;/, $lastline))[2];
	if ($when ne "") {
		($lasttime) = &ex('get_date_time', "long", $when);
	}
	$lasttime = "N/A" if $when eq "";
	$counter = -1;
	$timer = time + ($GLOBAL_OPTIONS{'timezone'}*3600);
	if ($GLOBAL_OPTIONS{'usegmtime'} == 1) {
		$todays_day = (gmtime($timer))[6];
	} else {
		$todays_day = (localtime($timer))[6];
	}
	for($i = ($todays_day + 5); $i >= ($todays_day); $i -= 1) {
		$counter += 1;
		$option_string .= "<OPTION VALUE=\"$counter\">";
		if ($i <= 6) {
			$ll = $L{NMLASTDAY};
			$wday = $DAY[$i % 7];
			$ll =~ s/\%weekday/$wday/g;
			$option_string .= $ll;
		} else {
			$option_string .= $DAY[$i % 7];
		}
	}
	$option_topic = "";
	if ($pro) {
		&ex('get_preferences', 1);
		if ($PREF{'favorites'} ne "") {
			$option_topic .= "<OPTION VALUE=\"$PREF{'favorites'}\" SELECTED>$L{MY_FAVORITES}\n";
		}
		$CHECKED{'treeview'} = "" if $PREF{'nm_view'};
		if ($COOKIE{'user' . $COOKIE_ID} ne "") {
			$CHECKED{'last'} = "";
			$CHECKED{'user'} = "CHECKED";
		}
	}	
	open (FILE, "$message_dir/$board_topics_file");
	@file = <FILE>;
	close (FILE);
	foreach $line (@file) {
		if ($line =~ /<!-Top: (\d+)-!>/) {
			$o = $1;
			&extract("//$1/$1.$ext");
			if (-e "$message_dir/$o") {
				$option_topic .= "<OPTION VALUE=\"$me_number\">$me_name\n";
			} else {
				@auth = &ex('validate_auths', $o);
				$option_topic .= "<OPTION VALUE=\"$me_number\">$me_name\n" if grep(/^$o$/, @auth);
			}				
		}
	}
	&header;
	$str = "$L{NMTITLE}";
	&ex('printuntil',1, 1, 0, "$L{NMTITLE}");
	print "$place\n";
	$ts = $L{NMALLOF};
	$ts =~ s/\%title/$titlej/g;
	$ampmstr = "<SELECT NAME=\"ampm\" onChange=\"document.forms[0].method[2].checked = true\"><OPTION VALUE=\"am\" SELECTED>$L{AM}<OPTION VALUE=\"pm\">$L{PM}</SELECT>" if $L{HRCLOCK} == 12;
	$lps = $L{NMLASTPOST};
	$lps =~ s/\%date/$lasttime/g;
	if (!($GLOBAL_OPTIONS{'nmshowdate'} == 1 || $GLOBAL_OPTIONS{'nmshowdate'} eq "")) {
		$lps = "";
	}
	if ($nolimit == 1) {
		$nolimit = "<INPUT TYPE=CHECKBOX NAME=nolimit VALUE=1";
		$nolimit .= " CHECKED" if $PREF{'show_all_nm'} == 1;
		$nolimit .= ">$L{NM_SHOW_ALL_NM}";
	} else {
		$nolimit = "";
	}
	$ucid = "user$COOKIE_ID";
	$pcid = "rpwd$COOKIE_ID";
	if ($COOKIE{$ucid}) {
		$CHECKED{'last'} = "";
		$CHECKED{'user'} = "CHECKED";
	}
	print <<EOF;
<CENTER><FONT SIZE=+1><B>$L{NMTITLE}</B></FONT></CENTER>
<HR>
<FORM ACTION="$script_url/board-newmessages.$cgi_extension" METHOD=POST>
<INPUT TYPE=HIDDEN NAME=charset_disp VALUE="$GLOBAL_OPTIONS{'charset_disp'}">
$L{NMSEARCHIN}&nbsp;<SELECT NAME="where" $multiple>
<OPTION VALUE="all">$ts
$option_topic
</SELECT> &nbsp;<INPUT TYPE=SUBMIT VALUE="$L{NMSEARCHBUTTON}">
<HR>
<P>
EOF
	if ($GLOBAL_OPTIONS{'nm_show_since'} == 1 || $GLOBAL_OPTIONS{'nm_show_since'} eq "") {
		print <<EOF;
<INPUT NAME="method" TYPE=radio VALUE="last" $CHECKED{'last'}>
$L{NMWITHINLAST}&nbsp;
<INPUT NAME="number" VALUE="1" SIZE=3 onChange="document.forms[0].method[0].checked = true">
<SELECT NAME="units" onChange="document.forms[0].method[0].checked = true">
<OPTION VALUE="1">$L{NMMINUTES}
<OPTION VALUE="60">$L{NMHOURS}
<OPTION VALUE="1440" SELECTED>$L{NMDAYS}
</SELECT>
<P>
EOF
	}
	if ($GLOBAL_OPTIONS{'nm_pw'} == 1 || $GLOBAL_OPTIONS{'nm_pw'} eq "") {
		print <<EOF;
<INPUT NAME="method" TYPE=radio VALUE="check" onClick=\"document.forms[0].username.focus()\" $CHECKED{'user'}>
$L{NMLASTCHECK}<BR>
<UL><TABLE><TR><TD><FONT FACE="$face" SIZE="$size">$L{PROFILE_USERNAME}</FONT></TD><TD><INPUT TYPE=TEXT NAME=username  VALUE="$COOKIE{$ucid}" SIZE=9 onChange=\"document.forms[0].method[1].checked = true\"></TD><TD><FONT FACE="$face" SIZE="$size">&nbsp;&nbsp;$nolimit</FONT></TD></TR>
<TR><TD><FONT FACE="$face" SIZE="$size">$L{PROFILE_PASSWORD}</FONT></TD><TD><INPUT TYPE=PASSWORD NAME=password SIZE=9 VALUE="$COOKIE{$pcid}"></TD><TD><FONT FACE="$face" SIZE="$size">&nbsp;&nbsp;<INPUT TYPE=CHECKBOX NAME=silent VALUE=on onClick="document.forms[0].method[1].checked = true">$L{NMDONTREMEMBER}</FONT></TD></TR></TABLE>
</UL>
<P>
EOF
	}
	if ($GLOBAL_OPTIONS{'nm_after'} == 1 || $GLOBAL_OPTIONS{'nm_after'} eq "") {
		print <<EOF;
<INPUT NAME="method" TYPE=radio VALUE="since" $CHECKED{'since'}>
$L{NMAFTER}&nbsp;
<INPUT NAME="time" TYPE=text VALUE="$L{NMDEFAULTTIME}" SIZE=5 onChange="document.forms[0].method[2].checked = true">
$ampmstr
<SELECT NAME="day" onChange="document.forms[0].method[2].checked = true">
<OPTION VALUE="-2">$L{NMTODAY}
<OPTION VALUE="-1">$L{NMYESTERDAY}
$option_string
</SELECT>
<P>
EOF
	}
print <<EOF;
<INPUT TYPE=CHECKBOX NAME=tree VALUE=1 $CHECKED{'treeview'}>
$L{NMTREEVIEW}
<BR>
<HR>
$lps
<!-- Discus $version_number %$count% !$when!-->
</FORM>
EOF
	&ex('printuntil', 3, 17, 0, "", 0, 1);
	exit(0);
}
$cutoff = 0;
$timestore = time;
if ($FORM{'method'} eq 'redo') {
	$cutoff = $FORM{'cutoff'};
} elsif ($FORM{'method'} eq 'last') {
	$number = $FORM{'number'};
	$factor = $FORM{'units'};
	&error_message("Invalid Number", "The number you specified is invalid!") if $number <= 0;
	&error_message("Invalid Units", "What are you trying to do?") if $factor <= 0;
	$cutoff = $timestore;
	$cutoff -= $number * $factor * 60;
} elsif ($FORM{'method'} eq 'since') {
	$timestore += (3600*$GLOBAL_OPTIONS{'timezone'});
	if ($GLOBAL_OPTIONS{'usegmtime'} == 1) {
		($sec, $min, $hour, $day, $nmonth, $year, $wday, $yday, $isdst) = gmtime($timestore);
	} else {
		($sec, $min, $hour, $day, $nmonth, $year, $wday, $yday, $isdst) = localtime($timestore);
	}
	$midnighttoday = $timestore - $sec - (60 * $min) - (3600 * $hour);
	$midnightsys = $midnighttoday - (3600*$GLOBAL_OPTIONS{'timezone'});
	$day = $FORM{'day'} + 2;
	$cutoff = $midnightsys - ($day * 86400);
	$cutoff += 43200 if $FORM{'ampm'} eq "pm";
	$timearg = $FORM{'time'};
	$timesep = $L{TIMESEPARATOR};
	$timearg =~ s/[^\d$timesep]//g;
	($hour, $min) = split(/$timesep/, $timearg);
	$hour = 0 if $hour == 12;
	$cutoff += ($min * 60) + ($hour * 3600);
} elsif ($FORM{'method'} eq 'check' || $FORM{'method'} eq "mynew") {
	if ($FORM{'method'} eq "mynew") {
		$FORM{'username'} = $COOKIE{"user$COOKIE_ID"};
		$my_new_feature = 1;
		$FORM{'where'} = "all" if $FORM{'where'} eq "";
	}
	($result) = &ex('verify_user_password', $FORM{'username'}, $FORM{'password'}, "", "", 0, $FORM{'silent'});
	@result = split(/\n/, $result);
	@result_save = @result;
	&error_message($L{BPAUTHERROR}, $L{PROFILE_AUTHERROR_DESCR},0,1) if scalar(@result) == 0;
	if ($pro) {
		&ex('get_preferences', $FORM{'username'}, $FORM{'password'});
		$FORM{'tree'} = "" if $PREF{'nm_view'} == 1;
	}
	$lastcheck = 0;
	foreach $result (@result_save) {
		chomp $result;
		($file, $user, $pass, $foo, $foo, $foo, $foo, $lc, $therest) = split(/:/, $result);
		if ($lc =~ m|/|) {
			$lc = $`; $la = $';
		}
		$lastcheck = $lc if $lc > $lastcheck;
		$password_save = $pass;
	}
	if ($lastcheck == 0) {
		$docut = "$L{NMNOCHECK}\n";
		$cutoff = $timestore;
		$cutoff -= 1440 * 60 * 7;
	} else {
		$cutoff = $lastcheck;
		if ($timestore > ($cutoff + 604800) && $PREF{'show_all_nm'} == 0 && !$FORM{'nolimit'}) {
			$docut .= "$L{NMCHECKSEVENDAYS}<P>\n";
			$cutoff = $timestore - 604800;
		} else {
			$docut .= "$L{NMYOULASTCHECKED} ";
			($dt) = &ex('get_date_time', 'long', $cutoff);
			$docut .= $dt;
			$docut .= ".<BR>\n";
		}
	}
	if ($FORM{'silent'}) {
		$docut .= "$L{NMNOTRECORDED}\n";
	}
	($result) = &ex('verify_user_password', $FORM{'username'}, $FORM{'password'}, "", "", 1, $FORM{'silent'});
}
if ($pro) {
	&ex('get_preferences', 1);
	if ($FORM{'tree'} eq "ON" || $my_new_feature) {
		$FORM{'tree'} = "ON" if $FORM{'tree'} ne "no";
		$FORM{'tree'} = "" if $PREF{'nm_view'} == 1;
		$FORM{'where'} = $PREF{'favorites'} if ($PREF{'last_favs'} == 1 && $PREF{'favorites'} =~ /\S/); 
	}
}
$| = 1;
undef @returned_list;
undef %file;
undef %msgindex;
$counter = 0;
undef %seen;
&header;
@topics = split(/,/, $FORM{'where'});
if ($FORM{'where'} eq "all") {
	undef @topics;
	open (HTML, "$message_dir/$board_topics_file");
	@html = <HTML>;
	close (HTML);
	@x = grep(/^<!-Top: (\d+)-!>/, @html);
	foreach $line (@x) {
		$line =~ m|<!-Top: (\d+)-!>|;
		push (@topics, $1);
	}
}
@topics = &ex('log_authorization', @topics) if $pro;
foreach $topic (@topics) {
	$topic =~ s/\D//g;
	if (open(LOG, "$admin_dir/msg_index/$topic-log.txt")) {
		while (<LOG>) {
			next if !/^(\d+);([^;]*);/;
			chomp;
			($what,$who,$when,$where,$how) = split(/;/, $_);
			if ($when != 0 && $when >= $cutoff) {
				$flag = 0;
				($thistopic, $num) = split(/\//, $where);
				push(@returned_list, $_) if $flag == 0;
				$file{$where} .= "$_," if $flag == 0;
				$flag = 1 if $seen{$where} == 1;
				$seen{$where} = 1;
				$counter += 1 if $flag == 0;
			}
		}
		close (LOG);
	}
}
$tmp = $/;
undef $/;
foreach $topic (@topics) {
	open (TREE, "$admin_dir/msg_index/$topic-tree.txt");
	$TT{$topic} = <TREE>;
	close (TREE);
}
$/ = $tmp;
open (TREE, "$admin_dir/tree.txt");
@tree = <TREE>;
close (TREE);
foreach $line (@tree) {
	@c = split(/\t/, $line);
	if ($TT{$c[1]} ne "") {
		$line = $TT{$c[1]}; delete $TT{$c[1]};
	}
}
@TREE_STRUCTURE_FILE = split(/\n/, join("", @tree));
undef @result;
foreach $line (@returned_list) {
	chomp $line;
	($postindex, $who, $time, $where, $addr, $host, $message2, $who2) = split(/;/, $line);
	($topic, $page) = split(/\//, $where);
	if (($ll) = grep(/^(\d+)\t$topic\t$page\t/, @TREE_STRUCTURE_FILE)) {
		$ll =~ s/\s+$//; @c = split(/\t/, $ll);
		if ($c[5] !~ m|Messages|) {
			$line = ""; next;
		}
		if ($c[5] =~ m|Archive| && $GLOBAL_OPTIONS{"skip_archives_nm"}) {
			$line = ""; next;
		}
		@x = split(/,/, $c[11]);
		if (!grep(/^$postindex$/, @x)) {
			$line = ""; next;
		}
		if ($subname{$where} eq "") {
			push (@result, $where);
		}
		$subname{$where} = &unescape($c[4]);
		$lm{$where} = $c[8];
		$msgplace{$postindex} = $where;
		$msgtext{$postindex} = &unescape($message2);
		$msgindex{$where} = $c[11];
		$postedby{$postindex} = &unescape($who2);
		$postedby{$postindex} =~ s/\s+$//;
		$postedwhen{$postindex} = $time;
		push (@msgid, $postindex);
	} else {
		$line = ""; next;
	}
}
undef %WHERE_ORDER; $c = 0;
foreach $line (@TREE_STRUCTURE_FILE) {
	if ($line =~ m|^(\d+)\t(\d+)\t(\d+)\t|) {
		$c += 1;
		$WHERE_ORDER{"$2/$3"} = $c;
	}
}
@results = sort findorder (@result);
sub findorder {
	($first, $second) = ($a, $b);
	return 0 if $first eq $second;
	return 1 if $WHERE_ORDER{$first} > $WHERE_ORDER{$second};
	return -1 if $WHERE_ORDER{$first} < $WHERE_ORDER{$second};
	return 0;
}
if ($FORM{'redisplay'} ne "") {
	$FORM{'tree'} = "" if $FORM{'redisplay'} !~ /Tree/;
	$FORM{'tree'} = "ON" if $FORM{'redisplay'} =~ /Tree/;
}
&ex('printuntil', 1, 1, 0, "$L{NMRESULTSOFSEARCH}");
print "<FONT SIZE=+1><CENTER><B>$L{NMRESULTSOFSEARCH}</B></CENTER></FONT>\n<HR>\n";
$numnew = scalar(@results);
print "$docut\n";
if ($numnew == 0) {
	print "$L{NMZERONEW}<HR>\n";
	&ex('printuntil', 3, 17, 0, "", 0, 1);
	exit(0);
} elsif ($numnew == 1) {
	print "$L{NMONENEW}<HR>\n";
} else {
	$l = $L{NMPLURALNEW};
	$l =~ s/\%d/$numnew/g;
	print "$l<HR>\n";
}
&ex('printuntil', 3, 5, 0, "", 0, 1);
if ($FORM{'tree'} ne "") {
	$browser = $ENV{'HTTP_USER_AGENT'};
	if ($browser !~ /MSIE/) {
		if ($browser =~ m|Mozilla/(\d+)|) {
			$nsbug = 1 if ($1 < 4 && $interface eq "noframes");
		}
	}
	$ie3 = 0;
	$ie3 = 1  if $ENV{'HTTP_USER_AGENT'} =~ m|MSIE 3\.|;
	$nsbug = 1 if $ie3;
	undef %seen;
	if (!$nsbug) {
		print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
		print "<!-- Hide from older browsers\n";
		print "var i0 = '<IMG SRC=\"';\n";
		print "var i1 = '$html_url/icons/tree_';\n";
		print "var i2 = '.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>';\n";
		print "function crArray(leng) {\n";
		print "  this.size = leng;\n";
		print "  for(var i = 1; i <= leng; i++) {\n";
		print "    this[i] = 0;\n";
		print "  }\n";
		print "}\n";
		print "function p(ic, tl, ln, d) {\n";
		print "    var pr = '';\n";
		print "    for (var j = 0; j < ic.length; j++) {\n";
		print "      m = ic.toString();\n";
		print "      k = m.substring(j, 1+j);\n";
		print "      pr = pr + i0 + i1 + k + i2;\n";
		print "    }\n";
		print "    if (ln != '') {\n";
		print "      pr = pr + '<A ";
		print "TARGET=_blank " if $PREF{'nm_newwindow'};		
		print "HREF=\"' + ln + '\">';\n";
		print "    }\n";
		print "    pr = pr + tl;\n";
		print "    if (ln != '') {\n";
		print "      pr = pr + '</A>';\n";
		print "    }\n";
		print "    pr = pr + d;\n";
		print "    pr = pr + '<BR>';\n";
		print "    document.write(pr);\n";
		print "}\n";
	}
	undef %seen; undef %seen2; undef %seen3; undef %pr; undef %substructure;
	foreach $result (@results) {
		$seen{$result} = 1;
	}
	$last = $results[scalar(@results)-1];
	$last =~ m|^(\d+)/|; $LAST_TOPIC = $1;
	foreach $res (@results) {
		$res =~ m|^(\d+)/(\d+)|; $topic = $1; $page = $2;
		if ($IS_SECURE{$topic} == 0) {
			$IS_SECURE{$topic} = 1 if !-e "$message_dir/$topic";
			$IS_SECURE{$topic} = 2 if -e "$message_dir/$topic";
		}		
	}
	foreach $line (@TREE_STRUCTURE_FILE) {
		$line =~ s/\s+$//;
		@c = split(/\t/, $line);
		$page = $c[2]; $topic = $c[1]; $res = "$topic/$page";
		$parent{$res} = $c[3];
		$lm{$res} = $c[8];
		$subname{$res} = &unescape($c[4]);
		$pres = "$topic/" . $c[3];
		$substructure{$pres} .= ",$topic/$page";
	}
	foreach $res (@results) {
		$res =~ m|^(\d+)/(\d+)|; $topic = $1; $page = $2;
		$parent = $parent{$res};
		$has_subtopics{"$topic/$parent"} = 1;
		$seen3{$res} = 1; undef @x; push (@x, "$topic/$page");
		while ($parent != 0) {
			$page2 = $parent; $parent = $parent{"$topic/$page2"}; $seen3{"$topic/$page2"} = 1;
			push (@x, "$topic/$page2");
		}
		$hd{$res} = join(",", reverse(@x));
	}
	undef %ln;
	foreach $key (keys(%substructure)) {
		$v = $substructure{$key}; @v = split(/,/, $v); @v = grep(/\S/, @v);
		$substructure{$key} = join(",", @v);
		foreach $k (@v) {
			if ($seen3{$k}) {
				$ln{$key} = $k;
			}
		}
	}
	foreach $res (@results) {
		$res =~ m|^(\d+)/|; $topic = $1;
		@hs = split(/,/, $hd{$res}); $LEVEL = 0;
		foreach $res2 (@hs) {
			$LEVEL += 1; $res2 =~ m|^(\d+)/(\d+)|; $topic = $1; $page = $2;
			if (!$seen2{$res2}) {
				print "p('" if !$nsbug;
				$thisstr = "";
				if ($LEVEL == 1) {
					if (!$nsbug) {
						print "g','$bopen$subname{$res2}$bclose',";
						if ($IS_SECURE{$topic} != 1) {
							print "'$message_url/$res2.$ext";
							print "?$lm{$res2}" if !$noqm;
						} else {
							print "'$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lm{$res2}";
						}
						print "','');\n";
					} else {
						$lmstr = "";
						$lmstr = "?$lm{$res2}" if !$noqm;
						print "<IMG SRC=$html_url/icons/tree_g.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>";
						print "<A HREF=$message_url/$res2.$ext$lmstr" if $IS_SECURE{$topic} != 1;
						print "<A HREF=$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lmstr" if $IS_SECURE{$topic} == 1;
						print " TARGET=_blank" if $PREF{'nm_newwindow'};
						print ">$bopen$subname{$res2}$bclose</A><BR>";
					}
					$seen2{$res2} = 1;
				} else {
					for ($i = 1; $i < ($LEVEL-1); $i++) {
						$thisstr .= "i" if $on{$i};
						$thisstr .= "j" if !$on{$i};
					}
					if ($res2 ne $ln{$parent}) {
						$thisstr .= "e";
						$on{$i} = 1;
					} else {
						$thisstr .= "f";
						$on{$i} = 0;
					}
					if (!$nsbug) {
						print "$thisstr", "g','$bopen$subname{$res2}$bclose',";
						if ($IS_SECURE{$topic} != 1) {
							print "'$message_url/$res2.$ext";
							print "?$lm{$res2}" if !$noqm;
						} else {
							print "'$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lm{$res2}";
						}
						print "', '');\n";
					} else {
						while ($thisstr =~ m|(\w)|g) {
							print "<IMG SRC=$html_url/icons/tree_$1.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>";
						}		
						print "<IMG SRC=$html_url/icons/tree_g.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>";
						$lmstr = "?$lm{$res2}" if !$noqm;
						print "<A HREF=$message_url/$res2.$ext$lmstr" if $IS_SECURE{$topic} != 1;
						print "<A HREF=$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lmstr" if $IS_SECURE{$topic} == 1;
						print " TARGET=_blank" if $PREF{'nm_newwindow'};
						print ">$bopen$subname{$res2}$bclose</A><BR>";
					}
					$seen2{$res2} = 1;
				}
			}
			$thisstr =~ s/e$/i/; $thisstr =~ s/f$/j/; $parent = $res2;
			if ($pr{$res2} == 0) {
				$pr{$res2} = 1;
				if (grep(/^$res2$/, values(%msgplace))) {
					@msg = split(/,/, $msgindex{$res2});
					undef @li1; undef @li2; undef @li3; undef @li4; undef @li5;
					foreach $pn (@msg) {
						if (grep(/^$pn$/, @msgid)) {
							push (@li1, $thisstr . "el");
							push (@li4, $pn);
							push (@li5, $postedwhen{$pn});
							if ($postedby{$pn} ne "") {
								$postedby{$pn} =~ s/\([^\)]*\)//g;
								$postedby{$pn} =~ s/\s+$//;
								$postedby{$pn} = &remove_html($postedby{$pn}, 1);
								$msgtext{$pn} = &remove_html($msgtext{$pn}, 1);
								$msgtext{$pn} = substr($msgtext{$pn}, 0, $maxchar);
								$msgtext{$pn} = &char_convert($msgtext{$pn}, 1, 1);
								$msgtext{$pn} =~ s/</&lt;/g;
								$msgtext{$pn} =~ s/>/&gt;/g;								
								push (@li2, &char_convert($postedby{$pn}, 1, 1));
								push (@li3, $msgtext{$pn});
							}
						}
					}
					$p = scalar(@li1);
					if ($p > 0) {
						$li1[$p-1] =~ s/el$/fl/ if !$has_subtopics{$res2};
					}
					for ($i = 0; $i < $p; $i++) {
						if (length($li2[$i]) > $max_name_chars) {
							$l = $li2[$i];
							while ($l =~ m|&#(\d+);|) {
								$l = join("", $`, pack("c", $1), $');
							}
							$l = substr($l, 0, $max_name_chars);
							$l = &char_convert($l, 1);
							$li2[$i] = $l;
						}
						if (!$nsbug) {
							print "p('", $li1[$i], "','<FONT SIZE=-1>", $li2[$i], "</FONT>',";
							if (-e "$message_dir/$res2.$ext") {
								print "'$message_url/$res2.$ext";
								print "?$lm{$res2}" if !$noqm;
							} else {
								print "'$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lm{$res2}";
							}
							print "#POST", $li4[$i], "','<FONT SIZE=-2>-";
							$pt = $li5[$i];
							($dt) = &ex('get_date_time', 'realshort', $pt);
							print "$dt-";
							print $li3[$i], "</FONT>');\n";
						} else {
							while ($li1[$i] =~ m|(\w)|g) {
								print "<IMG SRC=$html_url/icons/tree_$1.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>";
							}
							$lmstr = "";
							$lmstr = "?$lm{$res2}" if !$noqm;
							print "<A HREF=$message_url/$res2.$ext$lmstr#POST$li4[$i] " if $IS_SECURE{$topic} != 1;
							print "<A HREF=$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lmstr#POST$li4[$i]" if $IS_SECURE{$topic} == 1;
							print " TARGET=_blank" if $PREF{'nm_newwindow'};
							print "><FONT SIZE=-1>$li2[$i]</FONT></A><FONT SIZE=-2>-";
							$pt = $li5[$i]; ($dt) = &ex('get_date_time', 'realshort', $pt);
							print "$dt-", "$li3[$i]</FONT><BR>\n";
						}
					}
				}
			}
		}
	}
	if (!$nsbug) {
		print "// -->\n";
		print "</SCRIPT>\n";
	}
} else {
	foreach $where (@results) {
		($topic, $page) = split(/\//, $where);
		($navbar) = &ex('get_navbar', $topic, $page, "nmnav", 0);
		$navbar =~ s/:\s+$//;
		print "<A HREF=\"$message_url/$where.$ext" if -e "$message_dir/$topic";
		print "<A HREF=\"$script_url/board-auth.$cgi_extension?file=/$where.$ext&lm=$lm{$where}" if !-e "$message_dir/$topic";
		print "?$lm{$where}" if (!$noqm && 	-e "$message_dir/$where.$ext");
		print "\"";
		print " TARGET=_blank" if $PREF{'nm_newwindow'};
		print ">$bopen";
		print $navbar;
		print "$bclose</A>\n";
		@message = split(/\n/, $msgdata{$where});
		print "<BLOCKQUOTE>\n";
		@msgind = split(/,/, $msgindex{$where});
		foreach $post (@msgind) {
			$pn = $post;
			($dateprint) = &ex('get_date_time', 'realshort', $postedwhen{$pn});
			if ($postedby{$pn} ne "") {
				$postedby{$pn} =~ s/\([^\)]*\)//g;
				$postedby{$pn} =~ s/\s+$//;
				$by = &char_convert($postedby{$pn}, 1);
				$text = &char_convert($msgtext{$pn}, 1);
				$text =~ s/</&lt;/g;
				$text =~ s/>/&gt;/g;								
				$l = $by;
				while ($l =~ m|&#(\d+);|) {
					$l = join("", $`, pack("c", $1), $');
				}
				$l = substr($l, 0, $max_name_chars);
				$l = &char_convert($l, 1);
				$by = $l;
				print "<FONT SIZE=-1>";
				print "<A HREF=\"$message_url/$where.$ext" if -e "$message_dir/$where.$ext";
				print "<A HREF=\"$script_url/board-auth.$cgi_extension?file=/$where.$ext&lm=$lm{$where}" if !-e "$message_dir/$where.$ext";
				print "?$lm{$where}" if (!$noqm && 	-e "$message_dir/$where.$ext");
				print "#POST$pn\"";
				print " TARGET=_blank" if $PREF{'nm_newwindow'};
				print ">$by</A>: $dateprint: $text</FONT><BR>\n";
				$flag = 0;
			}
		}
		print "</BLOCKQUOTE><P>\n";
	}
}
print "<HR>\n";
print "<FORM ACTION=\"$script_url/board-newmessages.$cgi_extension\" METHOD=POST>\n";
print "<INPUT TYPE=HIDDEN NAME=charset_disp VALUE=\"$GLOBAL_OPTIONS{'charset_disp'}\">\n";
print "<INPUT TYPE=HIDDEN NAME=cutoff VALUE=\"$cutoff\">\n";
print "<INPUT TYPE=HIDDEN NAME=method VALUE=\"redo\">\n";
print "<INPUT TYPE=HIDDEN NAME=where VALUE=\"$FORM{'where'}\">\n";
print "<INPUT TYPE=HIDDEN NAME=tree VALUE=1>\n" if $FORM{'tree'} eq "";
print "<INPUT TYPE=SUBMIT VALUE=\"$L{NMOLDVIEWBUTTON}\">" if $FORM{'tree'} ne "";
print "<INPUT TYPE=SUBMIT VALUE=\"$L{NMTREEVIEWBUTTON}\">" if $FORM{'tree'} eq "";
print "</FORM>\n";
&ex('printuntil', 7, 17, 0, "", 0, 1);
exit(0);
# END - FILE IS CORRECTLY UPLOADED #
