# FILE: src-board-subs-16
# Page generation
#-------------------------------------------------------------------------------
# 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.
#-------------------------------------------------------------------------------

#---SEPARATOR---#
#REQ:topic_format

sub change_board_colors {
	local ($bgcolor,$text,$link,$vlink,$alink,$face,$size,$background,$corrupt,$regen_anyway,$remove_spaces) = @_;
	local ($mainmessage_src, $meta_description, $meta_keywords, $meta_robots);
	local ($str, $timestr);
	my (@TREE_STRUCTURE_FILE, $ttlcount, $ttl_count_disp);
	my (@file, @mainfile, $flag2, $xctr, $ERR);
	my (%ts, @topfile, @topics, @mainmessage, @tmsg, $first_ts, $second_ts, %description_seen, %pr, $seenend);
	my ($line, $flag, $d2);
	$ttlcount = 0; $ttl_count_disp = 0;
	if ($bgcolor eq "*" || $bgcolor eq "") {
		return 0 if ($GLOBAL_OPTIONS{'top_page_noregen'} == 1 && $regen_anyway == 0);
		($bgcolor,$text,$link,$vlink,$alink,$face,$size,$background) = &extract_colorsonly;
		$FONT_STRING = "<FONT FACE=\"$face\" SIZE=\"$size\" COLOR=\"$text\">";
	} else {
		&lock("change_board_colors", "$admin_dir/options.txt");
		open (OPTIONS, "$admin_dir/options.txt");
		@options = <OPTIONS>;
		close (OPTIONS);
		@options = grep(!/^COLOR_/, @options);
		open (OPTIONS, ">$admin_dir/options.txt");
		print OPTIONS @options;
		print OPTIONS "COLOR_bgcolor=$bgcolor\n";
		print OPTIONS "COLOR_text=$text\n";
		print OPTIONS "COLOR_link=$link\n";
		print OPTIONS "COLOR_vlink=$vlink\n";
		print OPTIONS "COLOR_alink=$alink\n";
		print OPTIONS "COLOR_image=$background\n";
		print OPTIONS "COLOR_face=$face\n";
		print OPTIONS "COLOR_size=$size\n";
		close (OPTIONS);
		&unlock("change_board_colors", "$admin_dir/options.txt");
		$regen_anyway = 1; $bc_change = 1;
	}
	$image = $background;
	$colorstr = join("\t", $bgcolor, $text, $link, $vlink, $alink, $background, $size, $face);
	if ($regen_anyway) {
		$status = &lock("FORCE:change_board_colors", "$admin_dir/tree.txt");
	} else {
		$status = &lock("change_board_colors", "$admin_dir/tree.txt");
		if ($status == 0) {
			&log_error("src-board-subs-16", "change_board_colors", "Could not gain lock on tree.txt file; aborting rewrite");
			return 0;
		}
	}
	open (TREE, "$admin_dir/tree.txt");
	@TREE_STRUCTURE_FILE = <TREE>;
	close (TREE);
	&unlock("change_board_colors", "$admin_dir/tree.txt");
	foreach $line (@TREE_STRUCTURE_FILE) {
		@c = split(/\t/, $line);
		if ($c[8] ne "" && $c[8] !~ m|=|) {
			&fix_main_tree($c[1], $c[8]);
			open (TREE, "$admin_dir/tree.txt");
			@TREE_STRUCTURE_FILE = <TREE>;
			close (TREE);
			last;
		}
	}
	undef %TRL;
	foreach $line (@TREE_STRUCTURE_FILE) {
		@c = split(/\t/, $line);
		$TRL{$c[1]} = $line;
	}
	undef %ts;
	if ($regen_anyway) {
		$status = &lock("FORCE:change_board_colors", "$message_dir/$board_topics_file");
	} else {
		$status = &lock("change_board_colors", "$message_dir/$board_topics_file");
		if ($status == 0) {
			&log_error("src-board-subs-16", "change_board_colors", "Could not gain lock on board topics file; aborting rewrite");
			return 0;
		}
	}
	open (TOPFILE, "$message_dir/$board_topics_file");
	@topfile = <TOPFILE>;
	close (TOPFILE);
	$flag = 0; $seenend = 0;
	undef @topics; undef @mainmessage; undef @tmsg;
	undef @description; undef %tdseen;
	undef $first_ts; undef $second_ts; undef %description_seen;
	undef %pr;
	foreach $line (@topfile) {
		if ($line =~ m|<!-Top: (\d+)-!>|) {
			$topicxnumber = $1;
			push (@topics, $line) if $pr{$1} == 0;
			$pr{$1} = 1;
			if ($line =~ m|<!-Descr-!>(.*)<!-/Descr-!>|) {
				$description_seen{$topicxnumber} = $1;
			}
		} elsif ($line =~ m|^<!-Description: (\d+)-!>|) {
			$description_seen{$1} = $line;
		} elsif ($line =~ m|^<!--Topics-->|) {
			$first_ts = 1;
		} elsif ($line =~ m|^<!--/Topics-->|) {
			$second_ts = 1;
		} elsif ($line =~ m|^<!--Message-->|) {
			$flag = 1;
			push (@mainmessage, $line);
		} elsif ($line =~ m|^<!--/Message-->|) {
			$flag = 0;
			$mainmessage_src =~ s/\s*$//;
			if ($mainmessage_src =~ /[^\w\%\+]/) {
				$mainmessage_src = &escape($mainmessage_src);
			}
			push (@mainmessage, "<!--Source: $mainmessage_src -->\n");
			push (@mainmessage, $line);
		} elsif ($flag == 1) {
			if ($line =~ m|^<!--Source: (.*) -->| ) {
				$mainmessage_src = $1;
			} elsif ($line =~ m|^<!--Source:\s*$|) {
				$flag = 11;
			} else {
				push (@mainmessage, $line);
			}
		} elsif ($flag == 11) {
			if ($line =~ m|^-->\s*$|) {
				$flag = 1;
			} else {
				$mainmessage_src .= $line;
			}
		} elsif ($line =~ m|^<!--Description Source:|) {
			$flag = 2;
		} elsif ($line =~ m|^<!-?End Description Source-!>-->|i) {
			$flag = 0;
		} elsif ($flag == 2) {
			$line =~ m|<!-Source: (\d+)-!>|;
			$n = $1;
			next if $tdseen{$n};
			$tdseen{$n} = 1;
			push (@description, $line);
		} elsif ($line =~ m|^<!--Title Message-->|) {
			$flag = 3;
			push (@tmsg, $line);
		} elsif ($line =~ m|^<!--/Title Message-->|) {
			$flag = 0;
			push (@tmsg, $line);
		} elsif ($flag == 3) {
			$line =~ s|<IMG SRC="([^"]+)/clipart/([^"]+)">|<IMG SRC="$html_url/clipart/$2">|g if $move_board;
			push (@tmsg, $line);
		} elsif ($line =~ m|<META NAME="DESCRIPTION" CONTENT="([^"]*)">|i) {
			$meta_description = $1;
		} elsif ($line =~ m|<META NAME="KEYWORDS" CONTENT="([^"]*)">|i) {
			$meta_keywords = $1;
		} elsif ($line =~ m|<META NAME="ROBOTS" CONTENT="([^"]*)">|i) {
			$meta_robots = $1;
		} elsif ($line =~ m|</HTML>|i) {
			$seenend = 1;
		}
	}
	if ($seenend == 0) {
		&log_error("src-board-subs-16", "change_board_colors", "Topics page not read in completely; lacking last line");
		if (scalar(@mainmessage) == 0 && !$corrupt) {
			&log_error("src-board-subs-16", "change_board_colors", "Aborting file rewrite for $board_topics_file");
			return 0;
		}
	}
	if (scalar(@tmsg) <= 1) {
		&log_error("src-board-subs-16", "change_board_colors", "Topics file lacking title message section; added");
		$tm = "<!--Title Message-->\n<CENTER><IMG SRC=\"$html_url/clipart/board_logo.gif\"></CENTER>\n<!--Source: %5cc%7b%5cclipart%7bboard_logo%7d%7d -->\n<!--/Title Message-->";
		@tmsg = split(/\n/, $tm);
		foreach $line (@tmsg) {
			$line .= "\n";
		}
	}
	if (scalar(@mainmessage) == 0) {
		&log_error("src-board-subs-16", "change_board_colors", "Topics page missing main message section; added");
		$tm = "<!--Message-->\n<CENTER><A HREF=\"http://www.discusware.com/discus\" TARGET=_top>Powered by Discus</A></CENTER>\n<!--Source: %5ccenter%7b%5ctopurl%7bhttp%3a%2f%2fwww%2ediscusware%2ecom%2fdiscus%2cPowered+by+Discus%7d%7d -->\n<!--/Message-->";
		@mainmessage = split(/\n/, $tm);
		foreach $line (@mainmessage) {
			$line .= "\n";
		}
	}
	my ($templ) = &determine_templates("");
	@mainfile = split(/\n/, $templ);
	foreach $line (@mainfile) {
		$line .= "\n";
	}
	if (!(grep(/<!--Start-->/, @mainfile))) {
		&error_message("Page Regeneration Error", "Template file (newpage.conf) has been corrupted; regeneration stopped for safety reasons.");
	}
	$str = "$titlej"; $timestr = "";
	$TOPICS = ""; $ERR = 0;
	if (scalar(@topics) == 0 && !$bc_change) {
		&log_error("src-board-subs-16", "change_board_colors", "Attempt to regenerate empty topics file canceled.");
		&unlock("change_board_colors", "$message_dir/$board_topics_file");
		return 0;
	}
	foreach $tl (@topics) {
		$tl =~ m|<!-Top: (\d+)-!>|; $tn = $1; undef %DPr;
		@c = split(/\t/, $TRL{$tn}); $x = $TRL{$tn};
		if ($c[8] ne "") {
			@x = split(/;/, $c[8]);
			foreach $x (@x) {
				($par, $val) = split(/=/, $x);
				$DPr{$par} = &unescape($val);
			}
		}
		$xctr += 1 if $DPr{'hidden'} != 1;
		if ($x eq "") {
			&extract("$message_dir/$tn/$tn.$ext") if -e "$message_dir/$tn";
			&extract("$secdir/$tn/$tn.$ext") if -e "$secdir/$tn";
			$count = 0;
			open (LOG, "$admin_dir/msg_index/$tn-log.txt");
			while (<LOG>) {
				$count += 1;
				$ll = $_;
			}
			close (LOG);
			$lmd = (split(/;/, $ll))[2];
			$group = "\u$owner";
		} else {
			@c = split(/\t/, $x);
			$topic_name = &unescape($c[4]);
			$count = $c[7];
			$group = "\u$c[6]";
			$lmd = $c[9];
		}
		if ($description_seen{$tn} ne "") {
			if ($description_seen{$tn} =~ m|<P class="desc">(.*)</P>|) {
				$descr = $1;
			} else {
				$descr = $description_seen{$tn};
			}
		} elsif ($tl =~ m|<!-Descr-!>(.*)<!-/Descr-!>|) {
			$descr = $1;
		} else {
			$descr = "";
		}
		if (-e "$message_dir/$tn") {
			$url = "$message_url/$tn/$tn.$ext";
			$url .= "?$c[9]" if !$noqm;
		} else {
			$url = "$script_url/board-auth.$cgi_extension?file=/$tn/$tn.$ext&lm=$c[9]";
		}
		$ttlcount += $count; $ttl_count_disp += $count if !$DPr{'hidden'};
		if ($remove_spaces) {
			$d2 =~ s/<BR>/\n/g;
			$d2 =~ s/\s//g;
			if ($d2 eq "") {
				$descr = "";
			}
		}
		$tl = &topic_format($tn, $topic_name, $url, $lmd, $group, $descr, $count, $xctr, $DPr{'icons'});
		if ($DPr{'hidden'}) {
			$tl =~ m|<!-Top: $tn-!>|; $af = $'; chomp $af;
			$tl = join("", "<!-Top: $tn-!>", "<!-- ", $af, "-->", "\n");
		}
		if ($tl !~ m|<!-Top: $tn-!>|) {
			&log_error("src-board-subs-16", "change_board_colors", "Topic $tn ($topic_name) was not properly regenerated.  Regenerated line: ***** $tl *****.  Topic page regeneration halted.") if $GLOBAL_OPTIONS{'beta_no_regeneration_record'} == 0;
			$ERR = 1;
		}
		$TOPICS .= $tl;
	}
	if ($ERR == 1 || ($GLOBAL_OPTIONS{'beta_no_regeneration'} == 1 && $regen_anyway == 0)) {
		&unlock("change_board_colors", "$message_dir/$board_topics_file");
		return 0;
	}
	$head = "";
	$announcement_variable = "";
	$announcement_source_variable = "";
	$subtopic_variable = "";
	$about_variable = "";
	$about_source_variable = "";
	$message_variable = "";
	$message_source_variable = "";
	$description_source_variable = "";
	open (TOPFILE, ">$message_dir/$board_topics_file");
	$flag = 0; $flag2 = 0;
	$xctr = 0;
	foreach $line (@mainfile) {
		next if $line !~ m|\S|;
		if ($line =~ /<!--Start-->/) {
			$flag = 1;
		} elsif ($flag == 1) {
			$line = &eval_subst($line);
			if ($line =~ /<!--Bigtitle-->/) {
				$flag2 = 1;
			} elsif ($flag2 == 1 && $line =~ m|<!--/Bigtitle-->|) {
				$flag2 = 0;
			} elsif ($line =~ m|</HEAD>|) {
				print TOPFILE "<STYLE>\n<!--\nP.desc {margin-left: 40; margin-right: 20; margin-top: 0}\n-->\n</STYLE>\n" if $GLOBAL_OPTIONS{'no_style_on_topics'} == 0;
				print TOPFILE $line;
			} elsif ($line =~ m|<!--Navbar-->|) {
				$flag2 = 1;
			} elsif ($line =~ m|<!--/Navbar-->|) {
				$flag2 = 0;
			} elsif ($line =~ m|<!--Sublist-->|) {
				$flag2 = 1;
				print TOPFILE "<!--Topics-->\n";
				@tf = split(/\n/, $TEMPL_FOUND{"topics"});
				$flag3 = 0;
				$TS = "";
				foreach $linex (@tf) {
					$linex .= "\n";
					if ($linex =~ m|^\[General\]|) {
						$flag3 = 1;
					} elsif ($linex =~ m|^\[Topic\]|) {
						$flag3 = 0;
					} elsif ($flag3 == 1) {
						$TS .= $linex;
					}
				}
				$TS =~ s%\$FONT_STRING%<FONT FACE="$face" SIZE="$size">%g;
				$TS =~ s%\$TOTAL_DISPLAYED%$ttl_count_disp%g;
				$TS =~ s%\$TOTAL_MESSAGES%$ttlcount%g;
				if ($TS =~ m|\$TOTAL_USERS|) {
					if ($USER_COUNTER ne "") {
						$TS =~ s%\$TOTAL_USERS%$USER_COUNTER%g;
					} else {
						undef %uc; $count = 0;
						open (USERS, "$admin_dir/users.txt");
						while (<USERS>) {
							next if (!m|\S| || m|^#|);
							($uname, $rest) = split(/:/, $_, 2);
							$count += 1 if $uc{$uname} != 1;
							$uc{$uname} = 1;
						}
						close (USERS);
						$TS =~ s%\$TOTAL_USERS%$count%g;
					}
				}
				if ($TS =~ m|\$DISCUS_VERSION|) {
					$version_string = "Discus $release_version.";
					$version_string .= $free_revision if !$pro;
					$version_string .= $pro_revision if $pro;
					$version_string .= " Pro" if $pro;
					$TS =~ s%\$DISCUS_VERSION%$version_string%g;
				}
				$TS =~ s/\n+/\n/g;
				$TS =~ s%\$TOPICS%$TOPICS%g;
				print TOPFILE $TS;
				print TOPFILE "<!--/Topics-->\n";
			} elsif ($line =~ m|<!--/Sublist-->|) {
				$flag2 = 0;
			} elsif ($line =~ m|<!--Create-->|) {
				$flag2 = 1;
			} elsif ($line =~ m|<!--/Create-->|) {
				$flag2 = 0;
			} elsif ($line =~ m|<!--About-->|) {
				$flag2 = 1;
				$tstr2 = join("", @mainmessage);
				print TOPFILE $tstr2;
			} elsif ($line =~ m|<!--/About-->|) {
				$flag2 = 0;
			} elsif ($line =~ m|<!--Messages-->|) {
				$flag2 = 1;
			} elsif ($line =~ m|<!--/Messages-->|) {
				$flag2 = 0;
			} elsif ($line =~ m|<!--Announcement-->|) {
				$flag2 = 1;
				print TOPFILE @tmsg;
			} elsif ($line =~ m|<!--/Announcement-->|) {
				$flag2 = 0;
			} elsif ($line =~ m|<!--Add-->|) {
				$flag2 = 1;
			} elsif ($line =~ m|<!--/Add-->|) {
				$flag2 = 0;
			} elsif ($line =~ m|<!--Message Source|) {
				$flag2 = 3;
				print TOPFILE "<!--Description Source:\n";
				if (scalar(@description)) {
					print TOPFILE @description;
				}
				print TOPFILE "<!-End Description Source-!>-->\n";
			} elsif ($line =~ m|-->| && $flag2 == 3) {
				$flag2 = 0;
			} elsif ($line =~ m|<!-AdminFromHere-!>|) {
				# Skip
			} elsif ($flag2 == 0) {
				print TOPFILE $line;
			}
		}
	}
	close (TOPFILE);
	&unlock("change_board_colors", "$message_dir/$board_topics_file");
}

sub fix_main_tree {
	my ($status, $x, @topfile, %x, @c, $line, $key, @props, $props);
	my ($arg1, $arg2) = @_;
	&lock("fix_main_tree", "$admin_dir/tree.txt");
	open (TREE, "$admin_dir/tree.txt");
	@TREE_STRUCTURE_FILE = <TREE>;
	close (TREE);
	&lock("fix_main_tree", "$message_dir/$board_topics_file");
	open (TOPFILE, "$message_dir/$board_topics_file");
	@topfile = <TOPFILE>;
	close (TOPFILE);
	foreach $line (@topfile) {
		if ($line =~ m|^<!-Top: (\d+)-!>|) {
			$x = $1;
			my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($x, $x);
			undef @props;
			foreach $key (keys(%DP)) {
				push (@props, "$key=$DP{$key}");
			}
			$props = join(";", @props);
			$x{$x} = $props;
		}
	}
	foreach $line (@TREE_STRUCTURE_FILE) {
		@c = split(/\t/, $line);
		$c[8] = $x{$c[1]};
		$line = join("\t", @c);
	}
	open (TREE, ">$admin_dir/tree.txt");
	print TREE @TREE_STRUCTURE_FILE;
	close (TREE);
	&unlock("fix_main_tree", "$admin_dir/tree.txt");
	&unlock("fix_main_tree", "$message_dir/$board_topics_file");
	&log_error("src-board-subs-16", "fix_main_tree", "Invoked due to topic [$arg1] with argument [$arg2]");
}

#---SEPARATOR---#

sub topic_format {
	my ($topic_number, $name, $url, $date, $group, $descr, $count, $col_ctr, $icon_code, $hidden_code) = @_;
	my ($name_j, @topic, $flag, $line, @file);
	$name_j = &JavaScript_prepare($name);
	if ($TOPIC_FORMAT_STRING eq "") {
		&determine_templates;
		if ($TEMPL_FOUND{'topics'} ne "") {
			@topic = split(/\n/, $TEMPL_FOUND{'topics'});
			foreach $line (@topic) {
				$line .= "\n";
			}
			@topic = grep(!/^\s*#/, @topic);
			@topic = grep(/\S/, @topic);
			$flag = 0;
			foreach $line (@topic) {
				if ($line =~ m|^\[Topic\]|) {
					$flag = 1;
				} elsif ($flag == 1) {
					$TOPIC_FORMAT_STRING .= $line;
				}
			}
		} else {
			$TOPIC_FORMAT_STRING = '<LI><B><A HREF="$URL" onMouseOver="return setStatus(\'$TOPIC_NAME_J\')">$TOPIC_NAME</A></B> <FONT SIZE=-1>$LAST_MOD_DATE</FONT><P class="desc">$TOPIC_DESCR</P>';
		}
		$TOPIC_FORMAT_STRING =~ s/\s+/ /g;
	}
	if ($FONT_STRING eq "") {
		my ($bgcolor, $text, $link, $vlink, $alink, $face, $size, $image) = &extract_colorsonly;
		$FONT_STRING = "<FONT SIZE=\"$size\" FACE=\"$face\" COLOR=\"$text\">";
	}
	my ($dts, $sfs, $ic_d, $AFT, $BEF, $ctr, @col, $ttl, $mynum, $my_col);
	$dts = &get_date_time('short', $date); $dts = "&nbsp;" if ($dts eq "" || $date == 0);
	$count = "-" if $count eq "";
	$descr = "&nbsp;" if $descr eq "";
	$sfs = "<!-Top: $topic_number-!>";
	$sfs .= $TOPIC_FORMAT_STRING if $hidden_code != 1;
	$sfs = &common_discus_variables($sfs);
	$sfs =~ s/\$TOPIC_GROUP/$group/g;
	$sfs =~ s/\$LAST_MOD_DATE/$dts/g;
	$sfs =~ s/\$URL/$url/g;
	$sfs =~ s/\$MESSAGE_COUNT/$count/g;
	$sfs =~ s/\$FONT_STRING/$FONT_STRING/g;
	if ($icon_code eq "0") {
		$ic_d = "<IMG SRC=\"$html_url/icons/tree_j.gif\" HEIGHT=16 WIDTH=20>";
	} elsif ($icon_code == 1 || $icon_code eq "") {
		$ic_d = "<IMG SRC=\"$html_url/icons/tree_n.gif\" HEIGHT=16 WIDTH=20>";
	} elsif ($icon_code == 2) {
		$ic_d = "<IMG SRC=\"$html_url/icons/tree_o.gif\" HEIGHT=16 WIDTH=20>";
	} elsif ($icon_code == 3) {
		$ic_d = "<IMG SRC=\"$message_url/$topic_number-icon.gif\">";
	}
	$sfs =~ s/\$FOLDER_ICON/$ic_d/g;
	if ($descr eq "" || $descr eq "&nbsp;") {
		$sfs =~ s/<!-Blank-!>(.*?)<!-\/Blank-!>//g;
	} else {
		$sfs =~ s/<!-Blank-!>//g;
		$sfs =~ s/<!-\/Blank-!>//g;
	}
	$col_ctr -= 1;
	while ($sfs =~ m|\$ALTCOLOR\{([\d,]+)\}|) {
		$AFT = $'; $BEF = $`;
		$ctr = $1; @col = split(/,/, $ctr); $ttl = scalar(@col);
		$mynum = $col[($col_ctr % $ttl)];
		$my_col = $GLOBAL_OPTIONS{"alt_color_$mynum"};
		$sfs = join("", $BEF, $my_col, $AFT);
	}
	$sfs =~ s/\$TOPIC_DESCR/<!-Descr-!>$descr<!-\/Descr-!>/g;
	$sfs =~ s/\$TOPIC_NAME_J/$name_j/g;
	$sfs =~ s/\$TOPIC_NAME/$name/g;
	$sfs .= "\n";
	return $sfs;
}

#---SEPARATOR---#
#REQ:format_subtopics

sub regenerate_subtopic_list {
	my ($topic, $page, $sublist_incoming, $nolockflag, $fakeurl) = @_;
	my ($st, @sl, $num, $nl, @c, $dt, $ut, $xx, $tmp, @THIS_TOPIC_TREE);
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	if ($TPC_TREE{$topic} eq "") {
		$tmp = $/; undef $/;
		open (FILE, "$admin_dir/msg_index/$topic-tree.txt");
		$TPC_TREE{$topic} = <FILE>;
		close (FILE);
		$/ = $tmp;
	}
	@THIS_TOPIC_TREE = split(/\n/, $TPC_TREE{$topic});
	$m = scalar(@THIS_TOPIC_TREE);
	@sl = split(/\n/, $sublist_incoming);
	undef @new_sl; $indexer = 0;
	undef %treel; $flag = 0;
	foreach $line (@THIS_TOPIC_TREE) {
		@c = split(/\t/, $line);
		if ($c[3] == $page) {
			$treel{$c[2]} = $line;
			$flag = $c[0];
		}
	}
ML:	foreach $line (@sl) {
		$num = 0;
		if ($line =~ m|<!-Top: (\d+)-!>|) {
			$num = $1; $nl = "<!-Top: $num-!>"; $link = 0;
		} elsif ($line =~ m|<!-URL: (\d+)-!>|) {
			$num = $1; $nl = "<!-URL: $num-!>"; $link = 1;
		}
		if ($num != 0) {
			$indexer += 1;
			$ll = $treel{$num};
			if ($ll eq "") {
				&log_error("src-board-subs-16", "regenerate_subtopic_list", "Missing entry for $topic/$num in tree file in generating for $page! File length: $m");
				if ($link == 1) {
					$line =~ m|<A HREF="([^"]+)" TARGET="([^"]*)"([^>]*)>(.*)</A>|;
					$linkplace = $1; $target = $2; $name = $4;
					$ll = "?\t$topic\t$num\t$page\t";
					$ll .= &escape($name);
					$ll .= "\tLINK:$target\t";
					$ll.= &escape($linkplace);
					$ll .= "\t-\t-\t-\t-\t-\n";
				} else {
					($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $num);
					if ($head ne "") {
						$head =~ m|<!--Me: $num/(.*)-->|; $me_name = $1;
						$parent = 0;
						$head =~ m|<!--Parent: (\d+)-->|; $parent = $1;
						@tr_rel = grep(/^(\d+)\t(\d+)\t(\d+)\t$num\t/, @THIS_TOPIC_TREE);
						$pc = 0; $lm = 0;
						foreach $x (@tr_rel) {
							@x = split(/\t/, $x);
							$pc += $x[7];
							$lm = $x[9] if $x[9] > $lm;
						}
						@h = split(/\n/, $head); $lc = 0;
						foreach $line (@h) {
							if ($line =~ m|<!--Level (\d+):|) {
								$lc = $1 if $1 > $lc;
							}
						}
						$ll = "$lc\t$topic\t$num\t$parent\t";
						$ll .= &escape($me_name);
						$head =~ m|<!--Param: (.*)-->|; $param = $1;
						$ll .= "\t$param";
						$head =~ m|<!--Owner: (.*)-->|; $owner = $1;
						$ll .= "\t$owner";
						@msg = split(/\n/, $message);
						@ct = grep(/^<!-Post: /, @msg); $posts_under = scalar(@ct);
						undef @x; $firstpost = 0; $firstflag = 0; $lastpost = 0; $lastflag = 0;
						foreach $x (@ct) {
							if ($x =~ m|<!-Post: (\d+)-!>.*<!-Time: (\d+)-!>|) {
								push (@x, $1);
								if ($2 < $firstflag || $firstflag == 0) {
									$firstflag = $2; $firstpost = $1;
								}
								if ($2 > $lastflag || $lastflag == 0) {
									$lastflag = $2; $lastpost = $1;
								}
							} elsif ($x =~ m|<!-Post: (\d+)-!>|) {
								push (@x, $1);
								$firstpost = $1 if $firstpost == 0;
								$lastpost = $1;
							}
						}
						$orig_post = ""; $tm_lm = $lastflag;
						if (scalar(@ct)) {
							$flag = 0;
							foreach $line (@msg) {
								if ($line =~ m|<!-Post: $firstpost-!>|) {
									$flag = 1;
									if ($line =~ m|<!-Name-!>(.*?)<!-/Name-!>|) {
										$orig_post = $1; $orig_post = &remove_html($1);
										if ($orig_post =~ m|(.*) \(.*\)\s*$|) {
											$orig_post = $1; $orig_post =~ s/\([^\(\)]*\)//g;
										}
										last;
									}
								} elsif ($flag == 1) {
									if ($line =~ m|<!-Name-!>(.*?)<!-/Name-!>|) {
										$orig_post = $1; $orig_post = &remove_html($1);
										if ($orig_post =~ m|(.*) \(.*\)\s*$|) {
											$orig_post = $1;
										}
										last;
									}
								} elsif ($flag == 1 && $line =~ m|<!-/Post: $firstpost-!>|) {
									$flag = 0; last;
								}
							}
						} else {
							$tm_lm = "0";
						}
						$pc += $posts_under;
						$ll .= "\t$pc\t$PROPS\t$tm_lm\t$orig_post\t";
						$ll .= join(",", @x); $ll .= "\n";
					} else {
						&log_error("src-board-subs-16", "regenerate_subtopic_list", "Page not found or corrupt: $topic/$num; unlinking...");
						$line = "";
						$indexer -= 1;
						next ML;
					}
				}
			}
			@c = split(/\t/, $ll);
			$dt = $c[9];
			$st = &get_date_time("shorter", $c[9]);
			$st = "" if $c[9] == 0;
			$c[9] = time if $c[9] == 0;
			if ($link == 1) {
				$c[5] =~ m|LINK:(.*)|;
				$ut = $1;
				$ut = "Main" if $ut eq "";
				$url = &unescape($c[6]); $url .= "\" TARGET=\"$ut";
			} else {
				$url = "$message_url/$topic/$num.$ext";
				$url .= "?$dt" if !$noqm;
			}
			$xx = $nl;
			$url = "#" if $fakeurl;
			$xx .= &format_subtopics($url, &unescape($c[4]), $st, $c[7], &unescape($c[10]), $indexer, $topic, $fakeurl);
			push (@new_sl,  $xx);
		}
	}
	$sublist_incoming = join("", @new_sl);
	return $sublist_incoming;
}

#---SEPARATOR---#

sub format_messages {
	my ($poster_name, $post_id, $message_text, $col_ctr, $message_email_override, $datetime, $uid, $where, $P, $Pref, $fakeurl) = @_;
	my ($num, @message, $line, $line2, @c, $topic_number, $me_number, $message, $sfs, @P);
	($topic_number, $me_number) = split(/\//, $where);
	&flush_language($topic_number);
	&determine_templates($topic_number);
	$MESSAGE_FORMAT_STRING = $TEMPL_FOUND{"messages_$topic_number"};
	$LAST_TOPIC_FORMAT_STRING = $topic_number;
	if ($MESSAGE_FORMAT_STRING !~ m|<!-Text-!>| || $MESSAGE_FORMAT_STRING !~ m|<!-/Text-!>|) {
		&error_message("Format Messages Error", "The messages.conf template file is invalid as it does not contain either the &lt;!-Text-!&gt; and/or &lt;!-/Text-!&gt; identifiers.", 0, 1);
	}
	if ($MESSAGE_FORMAT_STRING !~ m|<!-Name-!>| || $MESSAGE_FORMAT_STRING !~ m|<!-/Name-!>|) {
		&error_message("Format Messages Error", "The messages.conf template file is invalid as it does not contain either the &lt;!-Name-!&gt; and/or &lt;!-/Name-!&gt; identifiers.", 0, 1);
	}
	if ($MESSAGE_FORMAT_STRING !~ m|<!-Email-!>| || $MESSAGE_FORMAT_STRING !~ m|<!-/Email-!>|) {
		&error_message("Format Messages Error", "The messages.conf template file is invalid as it does not contain either the &lt;!-Email-!&gt; and/or &lt;!-/Email-!&gt; identifiers.", 0, 1);
	}
	if ($FONT_STRING eq "") {
		my ($bgcolor, $text, $link, $vlink, $alink, $face, $size, $image) = &extract_colorsonly;
		$FONT_STRING = "<FONT SIZE=\"$size\" FACE=\"$face\" COLOR=\"$text\">";
	}
	$sfs = $MESSAGE_FORMAT_STRING;
	$sfs = &common_discus_variables($sfs, $topic_number, $me_number);
	$sfs =~ s/\$FONT_STRING/$FONT_STRING/g;
	$dt = &get_date_time("message", $datetime);
	$dt = "Unrecorded Date" if ($datetime == 0 && $L{'UNRECORDED_DATE'} eq "");
	$dt = $L{'UNRECORDED_DATE'} if ($datetime == 0 && $L{'UNRECORDED_DATE'} ne "");
	$sfs =~ s/\$DATE/$dt/g;
	($userid, $group) = split(/:/, $uid);
	foreach $line (@{$Pref}) {
		$line =~ m|<!-Post: (\d+)-!>|;
		push (@P, $1);
	}
	if ($GLOBAL_OPTIONS{'message_icons'} == 1 && $MESSAGE_FORMAT_STRING =~ m|\$ICON_STRING|) {
		$ctr = $col_ctr;
		$line2 = "<!-Icons-!>";
		$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 < ($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 == ($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";
		$sfs =~ s/\$ICON_STRING/$line/g;
	} else {
		$sfs =~ s/\$ICON_STRING//g;
	}
	if (scalar(@POST_OPTIONS_FILE) == 0) {
		open (FILE, "$admin_dir/postoptions.txt"); @POST_OPTIONS_FILE = <FILE>; close (FILE);
	}
	($line) = grep(/^$topic_number:/, @POST_OPTIONS_FILE); chop $line if $line =~ /\n$/;
	($tn, $anonymous_forbidden, $stamp_forbidden, $email_forbidden) = split(/:/, $line);
	$out_profile_string = "<A HREF=\"javascript:alert('$L{NOT_AVAILABLE}')\" onMouseOver=\"window.status = '$L{NOT_AVAILABLE}'; return true\">";
	if (!$email_forbidden && ($GLOBAL_OPTIONS{'email_on_post'} == 1 || $GLOBAL_OPTIONS{'options_used'} == 0)) {
		if ($userid ne "PUBLIC" && $userid ne "") {
			if ($pro) {
				$file = &getfn("$admin_dir/profiles/$userid-$group.txt");
				if (-e "$file") {
					$out_profile_string = "<A HREF=\"$script_url/board-profile.$cgi_extension?action=display_profile&profile=$userid-$group\">";
				} elsif ($message_email_override ne "") {
					$out_profile_string = "<A HREF=\"mailto:" . $message_email_override . "\">" if $message_email_override !~ m|^https?://|i;
					$out_profile_string = "<A HREF=\"$message_email_override\" TARGET=_blank>" if $message_email_override =~ m|^https?://|i;
				}
			} else {
				if ($message_email_override ne "") {
					$out_profile_string = "<A HREF=\"mailto:" . $message_email_override . "\">" if $message_email_override !~ m|^https?://|i;
					$out_profile_string = "<A HREF=\"$message_email_override\" TARGET=_blank>" if $message_email_override =~ m|^https?://|i;
				}
			}
		} else {
			if ($message_email_override ne "") {
				$out_profile_string = "<A HREF=\"mailto:" . $message_email_override . "\">" if $message_email_override !~ m|^https?://|i;
				$out_profile_string = "<A HREF=\"$message_email_override\" TARGET=_blank>" if $message_email_override =~ m|^https?://|i;
			}
		}
	}
	$out_profile_string = "<A HREF=\"javascript:alert('* $L{BPANONYMOUS} *');\">" if $poster_name eq $L{BPANONYMOUS};
	$poster_name =~ s/\$/&#36;/g;
	$pnesc = &escape($poster_name);
	if ($poster_name =~ m|\((.*)\)|) {
		$pusername = &escape($1);
	}
	$sfs =~ s/\$PROFILE_LINK/$out_profile_string/g if !$fakeurl;
	$sfs =~ s/\$PROFILE_LINK/<A HREF="#">/g if $fakeurl;
	$sfs =~ s%\$EDITOR_LINK%<A HREF="$script_url/board-profile.$cgi_extension?action=editpost&postid=$post_id&page=$where">%g if !$fakeurl;
	$sfs =~ s%\$EDITOR_LINK%<A HREF="#">%g if $fakeurl;
	$sfs =~ s%\$POST_NUMBER%$post_id%g;
	$sfs =~ s/\$POSTER_NAME_ESCAPED/$pnesc/g;
	$sfs =~ s/\$POSTER_USERNAME_ESCAPED/$pusername/g;
	$sfs =~ s/\$POSTER_NAME/$poster_name/g;
	$message_text_nq = $message_text;
	$message_text_nq =~ s/"//g;
	$message_text_nq =~ s/\$/&#36;/g;
	$message_text_nq =~ s/&#34;//g;
	$message_text_nq =~ s/&quot;//g;
	$sfs =~ s%\$MESSAGE_TEXT_NQ%$message_text_nq%g;
	$sfs =~ s%\$MESSAGE_TEXT%$message_text%g;
	while ($sfs =~ m|\$ALTCOLOR\{([\d,]+)\}|) {
		$a = $'; $b = $`;
		$ctr = $1; @col = split(/,/, $ctr); $ttl = scalar(@col);
		$mynum = $col[($col_ctr % $ttl)];
		$my_col = $GLOBAL_OPTIONS{"alt_color_$mynum"};
		$sfs = join("", $b, $my_col, $a);
	}
	$sfs =~ s/\s/ /g; $sfs .= "\n";
	return $sfs;
}

#---SEPARATOR---#
#REQ:format_messages

sub regenerate_message {
	my ($topic, $page, $message, $nolock, $fakeurl) = @_;
	my ($mc, $name, $email, $mc, $postnum, $flag, $timepost, $profile, $line, $text, $a, $pb, $eml, @msg);
	if ($LAST_TOPIC_FORMAT_STRING != $topic || $MESSAGE_FORMAT_STRING eq "") {
		$MESSAGE_FORMAT_STRING = (&determine_templates($topic, "", "", $fakeurl))[3];
		$MESSAGE_FORMAT_STRING =~ s%$script_url/([\w\-]+)\.$cgi_extension%#%g if $fakeurl;
		$LAST_TOPIC_FORMAT_STRING = $topic;
	}
	@msg = split(/\n/, $message);
	$mc = -1; $flag = 0;
	$mt = "";
	@mx = grep(/<!-Post: (\d+)-!>/, @msg);
	$P = scalar(@mx);
	$PRef = \@mx;
	foreach $line (@msg) {
		if ($line =~ m|<!-Post: (\d+)-!>|) {
			$mc += 1; $postnum = $1; $timepost = 0; $authorflag = 0;
			if ($line =~ m|<!-Time: (\d+)-!>|) {
				$timepost = $1;
			}
			$name = ""; $email = ""; $text = ""; $profile = "";
			$line = ""; $flag = 1;
		} elsif ($line =~ m|<!-Text-!>|) {
			$line =~ m|<!-Name-!>(.*)<!-/Name-!>|; $name = $1;
			$line =~ m|<!-Email-!>(.*)<!-/Email-!>|; $email = $1;
			$line =~ m|<!-Text-!>(.*)<!-/Text-!>|; $text = $1;
			if ($email =~ m|<a href=['"]([^'"]+)['"][^>]*>|i) {
				$email = $1;
				if ($email =~ m|^mailto:|) {
					$email = $';
				} elsif ($email =~ m|\&profile=(.*)|) {
					$profile = $1;
				} elsif ($email !~ m|^https?://|i) {
					$email = "";
				}
			} elsif ($email =~ m|<a href=mailto:([^>]+)>|i) {
				$email = $1;
			} else {
				$email = "";
			}
			$line = "";
		} elsif (($line =~ m|^<FONT SIZE="?-1"?>| || $line =~ m|^<!-Icons-!>|) && $flag && !$authorflag) {
			$authorflag = 1;
			if ($line =~ m|<B><A HREF=['"]([^>]+)['"]>(.*?)</A>|) {
				$eml = $1; $name = $2; $name =~ s%<!-/?Name-!>%%g;
				$name =~ s/\s+$//; $name =~ s/^\s+//;
				if ($eml =~ m|^mailto:|) {
					$email = $';
				} else {
					$eml =~ m|\&profile=(.*)|;
					$profile = $1; $email = "";
				}
			} elsif ($line =~ m|<B><A HREF=([^>]+)>(.*?)</A>|) {
				$eml = $1; $name = $2; $name =~ s%<!-/?Name-!>%%g;
				$name =~ s/\s+$//; $name =~ s/^\s+//;
				if ($eml =~ m|^mailto:|) {
					$email = $';
				} else {
					$eml =~ m|\&profile=(.*?)|;
					$profile = $1; $email = "";
				}
			} elsif ($line =~ m|<B>(.*?)</B>|) {
				$email = ""; $name = $1; $profile = "";	$name =~ s%<!-/?Name-!>%%g;
				$name =~ s/\s+$//; $name =~ s/^\s+//;
			} else {
				$email = ""; $name = ""; $profile = "";
			}
			$line = "";
		} elsif ($line =~ m|^<HR>| && $flag) {
			$line = "";
			next;
		} elsif ($line =~ m|<!-/Post: (\d+)-!>|) {
			$line = "<!-Post: $postnum-!>";
			if ($MESSAGE_FORMAT_STRING !~ m|<A NAME=['"]?POST\$POST_NUMBER['"]?>|i) {
				$line .= "<A NAME=\"POST$postnum\"></A>";
			}
			$line .= "<!-Time: ";
			if ($timepost != 0) {
				$line .= $timepost;
			} else {
				$timepost = $TIME_LOG[$postnum];
				$timepost = 0 if $timepost == 0;
				$line .= $timepost;
			}
			$line .= "-!>\n";
			$profile =~ s/-/:/g;
			$line .= &format_messages($name, $postnum, $text, $mc, $email, $timepost, $profile, "$topic/$page", $P, $PRef, $fakeurl);
			$line .= "\n";
			$line .= "<!-/Post: $postnum-!>\n";
			$flag = 0;
		} elsif ($flag) {
			$text .= $line; $line = "";
		} else {
			$line = "";
		}
	}
	@msg = grep(/\S/, @msg);
	$message = join("\n", @msg);
	return $message;
}

#---SEPARATOR---#

sub format_subtopics {
	my ($url, $name, $last_modified, $message_count, $originator, $col_ctr, $topic, $fakeurl) = @_;
	chomp ($url); chomp ($message_count); chomp ($last_modified); chomp ($originator);
	if ($url =~ m|TARGET=\"|) {
		$last_modified = "&nbsp;"; $message_count = "-"; $originator = "&nbsp;";
	}
	$last_modified = "&nbsp;" if $last_modified eq "";
	$message_count = "-" if $message_count eq "";
	$originator = "&nbsp;" if $originator eq "";
	&determine_templates($topic, 0, 0, $fakeurl);
	$SUBTOPIC_FORMAT_STRING = $TEMPL_FOUND{"subtopics_$topic"};
	$LAST_TOPIC_FORMAT_STRING = $topic;
	if ($FONT_STRING eq "") {
		my ($bgcolor, $text, $link, $vlink, $alink, $face, $size, $image) = &extract_colorsonly;
		$FONT_STRING = "<FONT SIZE=\"$size\" FACE=\"$face\" COLOR=\"$text\">";
	}
	$sfs = $SUBTOPIC_FORMAT_STRING;
	$n2 = &JavaScript_prepare($name);
	$sfs = &common_discus_variables($sfs);
	$sfs =~ s/\$URL/$url/g;
	$sfs =~ s/\$LAST_MOD_DATE/$last_modified/g;
	$sfs =~ s/\$MESSAGE_COUNT/$message_count/g;
	$sfs =~ s/\$ORIGINATOR/$originator/g;
	$sfs =~ s/\$FONT_STRING/$FONT_STRING/g;
	$sfs =~ s%\$ICON_STRING%$html_url/icons/tree_p.gif%g if $url !~ m|TARGET=\"|;
	$sfs =~ s%\$ICON_STRING%$html_url/icons/tree_q.gif%g if $url =~ m|TARGET=\"|;
	while ($sfs =~ m|\$ALTCOLOR\{([\d,]+)\}|) {
		$a = $'; $b = $`;
		$ctr = $1; @col = split(/,/, $ctr); $ttl = scalar(@col);
		$col_ctr -= 1; $mynum = $col[($col_ctr % $ttl)];
		$my_col = $GLOBAL_OPTIONS{"alt_color_$mynum"};
		$sfs = join("", $b, $my_col, $a);
	}
	$sfs =~ s/\$SUBTOPIC_NAME_J/$n2/g;
	$sfs =~ s/\$SUBTOPIC_NAME/$name/g;
	$sfs =~ s/\s/ /g;
	$sfs .= "\n";
	return $sfs;
}

# END - FILE IS CORRECTLY UPLOADED #
