# FILE: src-board-subs-11
# Board regeneration and indexing functions
#-------------------------------------------------------------------------------
# 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---#

sub show_gauge {
	my ($operation1, $operation2, $percent_completed, $refresh_url) = @_;
	my ($width, $pct);
	&header;
	print "<HTML><HEAD><TITLE>Progress: $operation</TITLE>\n";
	print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=$refresh_url\">\n" if !$GLOBAL_OPTIONS{'fast_index'};
	print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=$refresh_url\">\n" if $GLOBAL_OPTIONS{'fast_index'};
	print "</HEAD>\n";
	print "<BODY BGCOLOR=#ffffff TEXT=#000000>$fs\n";
	print "<FONT SIZE=3><B><CENTER>$operation1</CENTER></B></FONT><HR>\n";
	print "<P><CENTER><TABLE WIDTH=400 BORDER=2 BORDERCOLOR=#000000>\n";
	print "<TR><TD>$fs<B>Operation:</B> $operation2</FONT></TD></TR>\n";
	print "<TR><TD WIDTH=400 BGCOLOR=#ccccff>\n";
	$percent_completed = 0 if $percent_completed < 0;
	$percent_completed = 1 if $percent_completed > 1;
	$width = int(390*$percent_completed);
	$width = 1 if $width == 0;
	print "<IMG SRC=\"$html_url/icons/dotblue.gif\" HEIGHT=30 WIDTH=$width><BR>\n" if !$GLOBAL_OPTIONS{'fast_index'};
	print "<CENTER>$fs<B>";
	$pct = int(100*$percent_completed);
	print "$pct %</B></FONT></CENTER></TD>\n";
	print "</TR></TABLE>\n";
	print "<P><CENTER><FONT SIZE=3 COLOR=#ff0000>The screen may appear to flash during this procedure.<BR><U>This is normal.</U> DO NOT close your browser</FONT></CENTER></BODY></HTML>\n";
	exit(0);
}

#---SEPARATOR---#
#REQ:show_gauge
#REQ:page_fixer
#REQ:duplicate_clean
#REQ:get_number
#REQ:incremental

sub rebuild_tree {
	my ($header, $actionurl, $doneurl, $onetopic) = @_;
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	if ($FORM{'submit'} eq "Cancel") {
		&seturl($doneurl);
		exit(0);
	}
	if ($FORM{'submit'} eq "Fix files and index" || $FORM{'cltemp'}) {
		&duplicate_clean($header, $actionurl);
		exit(0);
	}
	$needs_cleanup = 0;
	if ($FORM{'tempt'} eq "" && $FORM{'reindex'} eq "") {
		undef %pagename;
		open (TOPICS, "$message_dir/$board_topics_file"); @top = <TOPICS>; close (TOPICS);
		@top = grep(/^<!-Top: (\d+)-!>/, @top);
		if (scalar(@top) == 0) {
			open (TREE, ">$admin_dir/tree.txt");
			close (TREE);
			&seturl($doneurl);
		}
		opendir(DIR, "$admin_dir/msg_index");
		while ($dir = readdir(DIR)) {
			unlink("$admin_dir/msg_index/$dir") if $dir =~ m|^(\d+)-tree\.TMP$|;
		}
		closedir(DIR);
		($fileref, $counter, $tempt) = &incremental("TREE", 1, "", $onetopic);
		undef %pgseen; $needs_cleanup = 0;
		@file = @{$fileref};
		foreach $line (@file) {
			if ($line =~ m|(\d+)/(\d+).$ext|) {
				if ($pgseen{"$2"}) {
					$needs_cleanup = 1;
					last;
				}
				$pgseen{"$2"} = 1;
			}
		}
		$pct = 0;
		$tempfile = join("", time, $$); $tempfile =~ s/\D//g;
		if (!$needs_cleanup || $FORM{'clean_declined'}) {
			&show_gauge($header, "Reindexing -- Estimating time to index $counter file(s)", 0, "$actionurl&tempt=$tempt&counter=$counter&done=0&tempfile=$tempfile");
		} else {
			&header;
			print "<HTML><HEAD><TITLE>Board Indexing Error</TITLE></HEAD>\n";
			print "<BODY BGCOLOR=#ffffff TEXT=#000000>$fs<FONT SIZE=+1><CENTER><B>Board Indexing Error</B></CENTER></FONT><HR>\n";
			print "Your board has one or more sets of illegally named files which will prevent your board from\n";
			print "indexing properly.  Discus can automatically repair these files for you and then properly\n";
			print "index your board.  This will change the file names of affected files and may break URLs that\n";
			print "you or others have created to those specific pages.  It is strongly recommended that you\n";
			print "allow Discus to resolve this problem for you.  If you do not, many aspects of your board\n";
			print "(including tree view, administration, navigation bars, and subtopic lists) on affected pages\n";
			print "may function erratically or not at all.<P>\n";
			if ($actionurl =~ m|^(https?://[^\?]+)|i) {
				$actionscript = $1;
			} else {
				$actionscript = $cgiurl;
			}
			print "<FORM ACTION=$actionscript METHOD=POST>\n";
			while ($actionurl =~ m|(\w+)=([^&]+)|g) {
				print "<INPUT TYPE=HIDDEN NAME=$1 VALUE=\"$2\">\n";
			}
			print "<INPUT TYPE=HIDDEN NAME=tempfile VALUE=\"$tempfile\">\n";
			print "<INPUT TYPE=HIDDEN NAME=tempt VALUE=\"$tempt\">\n";
			print "<INPUT TYPE=HIDDEN NAME=counter VALUE=\"$counter\">\n";
			print "<P>\n";
			print "<INPUT TYPE=HIDDEN NAME=doneurl VALUE=\"$doneurl\">\n";
			print "<CENTER><INPUT TYPE=SUBMIT NAME=submit VALUE=\"Fix files and index\">\n";
			print "<INPUT TYPE=SUBMIT NAME=submit VALUE=\"Index anyway\">\n";
			print "<INPUT TYPE=SUBMIT NAME=submit VALUE=\"Cancel\"></CENTER>\n";
			print "</FORM></BODY></HTML>\n";
			exit(0);
		}
	} elsif ($FORM{'reindex'} == 0) {
		($fileref, $foo, $tempt) = &incremental($FORM{'tempt'}, 2, "", "");
		@TEMPT = @{$fileref};
		$tempfile = $FORM{'tempfile'}; $tempfile =~ s/\D//g;
		$starttime = time; $was_indexing_topic = 0;
		$sec_due_to_factor = int($GLOBAL_OPTIONS{'reindex_factor'} / 30);
		open (TEMPFILE, ">>$admin_dir/msg_index/$tempfile.TMP") || &log_error("src-board-subs-11", "rebuild_tree", "Could not open temporary file for writing: $tempfile.TMP [$!]");
		while (time <= ($starttime + 2 + $sec_due_to_factor)) {
			$file = shift(@TEMPT);
			last if $file eq "";
			$file =~ m|(\d+)/(\d+)\.$ext|; $page = $2; $topic = $1; $topic_number = $1;
			if ($was_indexing_topic != 0 && $topic != $was_indexing_topic) {
				unshift(@TEMPT, $file);
				last;
			}
			$was_indexing_topic = $topic;
			($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
			@head = split(/\n/, $head);
			@sublist = split(/\n/, $sublist);
			$flag___ = 0;
			if ($head eq "") {
				$code = &page_fixer($topic, $page);
				if ($code == 1) {
					($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
					@head = split(/\n/, $head);
					@sublist = split(/\n/, $sublist);
				} else {
					&log_error("src-board-subs-11", "rebuild_tree", "Unrecoverable page corruption: $topic/$page");
					$dirs = "";
					$dirs = "$message_dir/$topic" if -e "$message_dir/$topic";
					$dirs = "$secdir/$topic" if -e "$secdir/$topic";
					$flag___ = 1;
					if ($dirs ne "") {
						open (DIR, "$dirs/$page.$ext");
						@dirfile = <DIR>;
						close (DIR);
						open (DIR, ">$dirs/$page.$ext.BAD");
						print DIR @dirfile;
						close (DIR);
						unlink ("$dirs/$page.$ext");
					}
				}
			}
			undef @props;
			foreach $key (keys(%DP)) {
				push (@props, "$key=$DP{$key}");
			}
			$props = join(";", @props);
			$FORM{'done'} += 1 if $flag___ != 1;
			if ($flag___ != 1) {
				$head =~ m|<!--Me: (\d+)/(.*)-->|; $mn = $1; $mname = $2;
				if ($mn != 0) {
					if (($sn) = grep(m|<!--Level (\d+): $mn/|, @head)) {
						$LEVEL_NUM = 0;
						$sn =~ m|<!--Level (\d+): $mn/|; $LEVEL_NUM = $1;
					} else {
						$LEVEL_NUM = 0;
					}
				} else {
					$LEVEL_NUM = 0;
				}
				$head =~ m|<!--Param: (.*)-->|; $param = $1;
				$head =~ m|<!--Owner: (.*)-->|; $owner = $1;
				$head =~ m|<!--Parent: (\d+)-->|; $my_parent = $1;
				@lineage = ("$topic");
				$i = 0;
				while (1 != 0) {
					$i += 1;
					if ($head =~ m|<!--Level $i: (\d+)/|) {
						push (@lineage, $1);
					} else {
						last;
					}
				}
				$lineage = join(",", @lineage);
				@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;
					}
				}
				$tm_lm = $lastflag;
				$x = join(",", @x);
				$orig_post = "";
				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";
				}
				undef @stl;
				foreach $line (@sublist) {
					if ($line =~ m|<!-URL: (\d+)-!>|) {
						$num = $1;
						push (@stl, $num);
						$line =~ m|<A HREF="([^"]+)" TARGET="([^"]*)"([^>]*)>(.*)</A>|;
						$link = $1; $target = $2; $name = $4;
						$returned .= join("\t", 1+$LEVEL_NUM, $topic, $num, $page, &escape($name), "LINK:$target", &escape($link), "-", "-", "-", "-", "-");
						$returned .= "\n";
						print TEMPFILE "$num\t\t$lineage\tURL\n";
					} elsif ($line =~ m|<!-Top: (\d+)-!>|) {
						push (@stl, $1);
					}
				}
				print TEMPFILE "$page\t", join(",", @stl), "\t$lineage\n";
				$my_parent = "0" if $topic == $page;
				$returned .= "$LEVEL_NUM\t$topic\t$page\t$my_parent\t" . &escape($mname) . "\t$param\t$owner\t$posts_under\t$props\t$tm_lm\t$orig_post\t$x\n";
			}
		}
		$size = -s "$admin_dir/msg_index/$was_indexing_topic-tree.TMP";
		$size += length($returned);
		open (TREE, ">>$admin_dir/msg_index/$was_indexing_topic-tree.TMP") || &log_error("src-board-subs-11", "rebuild_tree", "Could not open file $was_indexing_topic-tree.TMP for writing because [$!]");
		print TREE $returned;
		close (TREE);
		close (TEMPFILE);
		$size_x = -s "$admin_dir/msg_index/$was_indexing_topic-tree.TMP";
		if ($size_x < $size) {
			&log_error("src-board-subs-11", "rebuild_tree", "Tree file $was_indexing_topic-tree.TMP has wrong length (expected $size, got $size_x). Disk quota problem?");
		}
		$fileref = \@TEMPT;
		($status, $foo, $tempt) = &incremental($FORM{'tempt'}, 3, $fileref, "");
		if ($status > 0) {
			if ($FORM{'counter'} > 0) {
				$pct = ($FORM{'done'} / $FORM{'counter'});
			} else {
				$pct = 0;
			}
			&show_gauge($header, "Reindexing -- Searching $FORM{'done'} of $FORM{'counter'} file(s)", $pct, "$actionurl&tempt=$tempt&counter=$FORM{'counter'}&tempfile=$tempfile&done=$FORM{'done'}");
		}
		&show_gauge($header, "Reindexing -- Estimating Time for Sorting", 0, "$actionurl&reindex=1&tempfile=$tempfile");
	} elsif ($FORM{'reindex'} == 1) {
		undef %repeat_catch; $ITERATION = 0;
		$tempfile = $FORM{'tempfile'}; $tempfile =~ s/\D//g;
		open (TEMPFILE, "$admin_dir/msg_index/$tempfile.TMP");
		@tempfile = <TEMPFILE>;
		close (TEMPFILE);
		$topic_number = $FORM{'ctopic'};
		if ($topic_number == 0) {
			open (TOP, "$message_dir/$board_topics_file");
			@top = <TOP>;
			close (TOP);
			@top = grep(/<!-Top: (\d+)-!>/, @top);
			$xtop = $top[0];
			$xtop =~ m|<!-Top: (\d+)-!>|; $topic_number = $1;
			if ($xtop eq "") {
				&error_message("Indexing Error", "No topics were detected on your board");
			}
			$xhtml = $xtop;
			$xhtml =~ s/</&lt;/g;
			$xhtml =~ s/>/&gt;/g;
			&error_message("Indexing Error", "Could not determine the first topic number on your board.  The line:<P><FONT FACE='Courier New'>$xhtml</FONT><P>... is not a valid topic identifier!", 0, 1) if $topic_number == 0;
		}
		if ($topic_number !~ /^\d+$/) {
			&log_error("src-board-subs-11", "rebuild_tree", "Invalid topic number ($topic_number) passed to subroutine");
			$FORMSTUFF = "<PRE>";
			foreach $key (sort keys(%FORM)) {
				$FORMSTUFF .= "$key=$FORM{$key}\n";
			}
			$FORMSTUFF .= "</PRE>";
			&error_message("Indexing Error", "Invalid topic number passed ($topic_number) at reindex position 0.  Check your errors.txt file.<TABLE BORDER=1><TR><TD>$FORMSTUFF</TD></TR></TABLE>");
		}
		undef %pagest; undef %lins; undef %seen; $tempfile_rebuild_required = 0;
		foreach $line (@tempfile) {
			chomp ($line);
			($page, $pages, $lineage, $isurl) = split(/\t/, $line);
			@lins = split(/,/, $lineage);
			$topic_redo = $lins[0];
			$page_redo = $lins[scalar(@lins)-1];
			if ($seen{$page} == 1) {
				&log_error("src-board-subs-11","rebuild_tree","Duplicate entry: Page [$page] seen multiple times [$spot{$page}--$lineage]!");
				if ($isurl eq "URL") {
					$numb = &get_number;
					($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_redo, $page_redo);
					@subs = split(/\n/, $sublist); $flg_work = 0;
					foreach $lineX (@subs) {
						if ($lineX =~ m|<!-URL: $page-!>|) {
							$lineX = join("", $`, "<!-URL: $numb-!>", $'); $flg_work=1;
							&log_error("src-board-subs-11","rebuild_tree","Duplicate entry: Page [$page] was a simple URL; fixed on $topic_redo/$page_redo by renumbering to $numb!");
							$spot{$numb} = "$topic_redo/$page_redo";
							$pagest{$numb} = ""; $seen{$numb} = 1; $is_url{$numb} = "URL"; $lins{$numb} = $lineage;
							$tempfile_rebuild_required = 1;
						}
					}
					&log_error("src-board-subs-11","rebuild_tree","Page $topic_redo/$page_redo did not contain this link.  Check earlier in log; problem might have been fixed already.") if $flg_work == 0;
					$sublist = join("\n", @subs);
					&set_page($topic_redo, $page_redo, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
				} else {
					if ($is_url{$page}) {
						$numb = &get_number; ($topic_redo_1, $page_redo_1) = split(/\//, $spot{$page});
						($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_redo_1, $page_redo_1);
						@subs = split(/\n/, $sublist); $flg_work = 0;
						foreach $lineX (@subs) {
							if ($lineX =~ m|<!-URL: $page-!>|) {
								$lineX = join("", $`, "<!-URL: $numb-!>", $'); $flg_work=1;
								&log_error("src-board-subs-11","rebuild_tree","Duplicate entry: Page [$page] was a simple URL; fixed on $topic_redo_1/$page_redo_1 by renumbering to $numb!");
								$spot{$numb} = "$topic_redo/$page_redo";
								$pagest{$numb} = ""; $seen{$numb} = 1; $is_url{$numb} = "URL"; $lins{$numb} = $lineage;
								$tempfile_rebuild_required = 1;
							}
						}
						&log_error("src-board-subs-11","rebuild_tree","Page $topic_redo_1/$page_redo_1 did not contain this link.  Check earlier in log; problem might have been fixed already.") if $flg_work == 0;
						$sublist = join("\n", @subs);
						&set_page($topic_redo_1, $page_redo_1, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
						$pagest{$page} = $pages;
						$lins{$page} = $lineage;
						$seen{$page} = 1;
						$spot{$page} = "$topic_redo/$page_redo";
						$is_url{$page} = "";
					} else {
						&log_error("src-board-subs-11","rebuild_tree","Entry for $page in string [$lineage] cannot be processed.  This page also seen in $spot{$page}!  Board not indexed properly.");
					}
				}
			} else {
				$pagest{$page} = $pages;
				$lins{$page} = $lineage;
				$seen{$page} = 1;
				$spot{$page} = "$topic_redo/$page_redo";
				$is_url{$page} = 1 if $isurl eq "URL";
			}
		}
		if ($tempfile_rebuild_required) {
			open (TEMPFILE, ">$admin_dir/msg_index/$tempfile.TMP");
			foreach $key (keys(%pagest)) {
				print TEMPFILE "$key\t$pagest{$key}\t$lins{$key}\t$is_url{$key}\n";
			}
			close (TEMPFILE);
		}
		$donesomething = 0;
		open (TOPICS, "$message_dir/$board_topics_file");
		@top = <TOPICS>;
		close (TOPICS);
		@top = grep(/<!-Top: (\d+)-!>/, @top);
		until ($donesomething == 1) {
			if (open (TREE, "$admin_dir/msg_index/$topic_number-tree.TMP")) {
				$donesomething = 1;
				undef %ldata;
				while (<TREE>) {
					@c = split(/\t/);
					$ldata{$c[2]} = $_;
				}
				close (TREE);
				($lineQ, $posts, $lastmod) = &childfill($topic_number,"Top");
				$lineQ =~ s/\n+/\n/g;
				open (TREE, ">$admin_dir/msg_index/$topic_number-tree.TMP") || &log_error("src-board-subs-11", "rebuild_tree", "Could not open sorted $topic_number-tree.TMP for writing while reindexing [$!]");;
				print TREE $lineQ;
				close (TREE);
			}
			$hitend = 0; $next_topic = 0;
			foreach $line (@top) {
				if ($line =~ m|<!-Top: (\d+)-!>|) {
					if ($1 == $topic_number) {
						$hitend = 1;
					} elsif ($hitend == 1) {
						$next_topic = $1;
						$hitend = 0;
					}
				}
			}
			$FORM{'done'} += 1;
			if (scalar(@top) != 0) {
				$pct = $FORM{'done'} / scalar(@top);
			} else {
				$pct = 1; last;
			}
			last if $hitend;
			$topic_number = $next_topic if !$donesomething;
		}
		&show_gauge($header, "Reindexing -- Sorting lists ($next_topic)", $pct, "$actionurl&ctopic=$next_topic&ts=$topic_string&tempfile=$tempfile&done=$FORM{'done'}&reindex=1") if $hitend == 0;
		&show_gauge($header, "Reindexing -- Compiling lists", 1, "$actionurl&ctopic=$topic1&ts=$topic_string&tempfile=$tempfile&done=$FORM{'done'}&reindex=2");
	} elsif ($FORM{'reindex'} == 2) {
		&lock("rebuild_tree", "$admin_dir/tree.txt");
		open (TREE, ">$admin_dir/tree.txt");
		$tempfile = $FORM{'tempfile'}; $tempfile =~ s/\D//g;
		open (TOPICS, "$message_dir/$board_topics_file"); @top = <TOPICS>; close (TOPICS);
		@top = grep(/^<!-Top: (\d+)-!>/, @top);
		foreach $line (@top) {
			$line =~ m|<!-Top: (\d+)-!>|; $x = $1;
			if (open (FILE, "$admin_dir/msg_index/$x-tree.TMP")) {
				@file = <FILE>;
				close (FILE);
				print TREE $file[0];
				open (FILE, ">$admin_dir/msg_index/$x-tree.txt") || &log_error("src-board-subs-11", "rebuild_tree", "Could not write $topic_number-tree.txt for writing while writing out results [$!]");;
				print FILE @file;
				close (FILE);
				chmod(oct($perms0666),"$admin_dir/msg_index/$x-tree.txt") if $^O ne "MSWin32";
				unlink ("$admin_dir/msg_index/$x-tree.TMP");
			} elsif (open (FILE, "$admin_dir/msg_index/$x-tree.txt")) {
				@file = <FILE>;
				close (FILE);
				print TREE $file[0];
			}
		}
		close (TREE);
		chmod(oct($perms0666),"$admin_dir/tree.txt") if $^O ne "MSWin32";
		&unlock("rebuild_tree", "$admin_dir/tree.txt");
		unlink("$admin_dir/msg_index/$tempfile.TMP");
		&seturl($doneurl);
	}
	$FORMh = "<PRE><UL>";
	foreach $key (keys(%FORM)) {
		$FORMh .= "<LI><FONT COLOR=#ff0000>$key</FONT> = <B><FONT COLOR=#0000ff>$FORM{$key}</FONT></B>\n";
	}
	$FORMh .= "</UL></PRE>\n";
	&error_message("Indexing Error", "Indexing of board failed -- you should never see this error message.  Position: src-board-subs-11/rebuild_tree.  $FORMh");

}

sub childfill {
	my ($number, $call_child) = @_;
	$ITERATION += 1;
	if ($repeat_catcher{$number} != 0) {
		&log_error("src-board-subs-11", "childfill", "Caught duplicate entry for topic $topic_number; number $number; iteration $ITERATION; child $call_child; also child $repeat_catcher{$number}");
		return "";
	}
	$repeat_catcher{$number} = $call_child;
	$repeat_catcher{$number} = -1 if $call_child == 0;
	my ($line, @file, @c, $pctr, $lm, $resultline, $pctr2, $lm2, $resline, $x_ret);
	if ($pagest{$number} eq "") {
		@c = split(/\t/, $ldata{$number});
		return ($ldata{$number}, $c[7], $c[9]);
	}
	@file = split(/,/, $pagest{$number});
	@c = split(/\t/, $ldata{$number});
	$resultline = ""; $pctr = $c[7]; $lm = $c[9];
	foreach $line (@file) {
		($resline, $pctr2, $lm2) = &childfill($line, $number);
		$resultline .= $resline;
		$pctr += $pctr2;
		$lm = $lm2 if $lm2 > $lm;
	}
	@c = split(/\t/, $ldata{$number});
	$c[7] = $pctr;
	$c[9] = $lm;
	$ldata{$number} = join("\t", @c);
	$x_ret = join("\n", $ldata{$number}, $resultline);
	return ($x_ret, $pctr, $lm);
}

#---SEPARATOR---#
#REQ:show_gauge
#REQ:regenerate_subtopic_list
#REQ:regenerate_message
#REQ:branches
#REQ:prune_messages
#REQ:change_board_colors
#REQ:incremental

sub regenerate_board {
	my ($header, $actionurl, $doneurl, $topiclist, $branches, $argument1, $argument2, $factor, $regen_word) = @_;
	$regen_word = "Regeneration" if $regen_word eq "";
	$factor = $GLOBAL_OPTIONS{'reindex_factor'} if $factor == 0;
	$factor = 15 if $factor == 0;
	if ($topiclist eq "all") {
		$topiclist = "";
	}
	if ($FORM{'tempfiler'} eq "") {
		($fileref, $counter, $tempfiler) = &incremental("RGB", 1, "", $topiclist);
		if ($fileref eq "0") {
			&seturl("$doneurl");
		}
		&show_gauge($header, 'Estimating time', 0, "$actionurl&counter=$counter&tempfiler=$tempfiler&old_html_url=$FORM{'old_html_url'}");
	} else {
		&lock("regenerate_board", "*");
		($fileref) = &incremental($FORM{'tempfiler'}, 2, "", "");
		@TEMPFILER = @{$fileref};
		$starttime = time;
		$returned = "";
		$sec_due_to_factor = int($GLOBAL_OPTIONS{'reindex_factor'} / 30);
		$was_using_topic = 0;
		while (time <= ($starttime + 2 + $sec_due_to_factor)) {
			$file = shift(@TEMPFILER);
			last if $file eq "";
			$FORM{'done'} += 1;
			$file =~ m|(\d+)/(\d+)\.$ext|; $page = $2; $topic = $1; $topic_number = $1;
			if ($topic != $was_using_topic) {
				$was_using_topic = $topic;
				undef @THIS_TOPIC_TREE;
				if ($GLOBAL_OPENED) {
					close (INDEX); $GLOBAL_OPENED = 0;
				}
				($newpage, $addfile) = &determine_templates($topic_number);
			}
			if ($branches > 0) {
				&branches($branches, $topic, $page, $argument1, $argument2, $template, $addfile);
			} else {
				my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
				$head =~ m|<!--Param: (.*)-->|; $param = $1;
				if ($param =~ m|Sublist|) {
					$x = length($sublist);
					$sublist = &regenerate_subtopic_list($topic, $page, $sublist, 1);
					if ($x != 0 && $sublist eq "") {
						&log_error("src-board-subs-11", "regenerate_board", "Subtopic list regeneration failed on page $topic/$page");
					}
				}
				if ($param =~ m|Messages|) {
					$x = length($message);
					$message = &regenerate_message($topic, $page, $message, 1);
					if ($x != 0 && $message eq "") {
						&log_error("src-board-subs-11", "regenerate_board", "Message list regeneration failed on page $topic/$page");
					}
				}
				$color = $COLOR_CHANGER if $COLOR_CHANGER;
				&set_page($topic, $page, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src, $template, $addfile, 1);
			}
		}
		if ($branches == 7) {
			&prune_messages($topic, "*");
		}
		&unlock("regenerate_board", "*");
		if ($GLOBAL_OPENED) {
			close (INDEX); $GLOBAL_OPENED = 0;
		}
		$fileref = \@TEMPFILER;
		($status, $foo, $tempfiler) = &incremental($FORM{'tempfiler'}, 3, $fileref);
		if ($status) {
			if ($FORM{'counter'} > 0) {
				$pct = $FORM{'done'} / $FORM{'counter'};
			} else {
				$pct = 0;
			}
			&show_gauge($header, "$regen_word - $FORM{'done'} of $FORM{'counter'} processed", $pct, "$actionurl&tempfiler=$tempfiler&done=$FORM{'done'}&counter=$FORM{'counter'}&tempfile=$tempfile&old_html_url=$FORM{'old_html_url'}");
		}
		if ($branches == 7) {
			&prune_messages($topic, "**");
			&change_board_colors("*");
		}
		unlink("$admin_dir/msg_index/$tempfile.TMP");
		&seturl($doneurl);
	}
}

#---SEPARATOR---#
#REQ:show_gauge
#REQ:inverse_get_date_time
#REQ:incremental
#REQ:write_source

sub upgrade_filestructure {
	my ($header, $actionurl, $doneurl, $caption) = @_;
	$caption = "Upgrading File Structure" if $caption eq "";
	$amonly = $FORM{'amonly'};
	$clean = $FORM{'cleaner'};
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	if ($FORM{'tempfile2'} eq "") {
		($fileref, $counter, $tempfile2) = &incremental("UFS", 1, "", $topiclist);
		$tempfile = time . $$; $tempfile =~ s/\D//g; $tempfile .= "LOG";
		if (!$amonly && !$clean) {
			open (TEMP, ">$admin_dir/msg_index/$tempfile.TMP");
			opendir(DIR, "$admin_dir/msg_index");
			while ($dir = readdir(DIR)) {
				next if $dir !~ m|^(\d+)-log\.txt|;
				open (LOG, "$admin_dir/msg_index/$dir");
				while ($_ = <LOG>) {
					@c = split(/;/, $_);
					print TEMP "$c[0]\t$c[2]\n";
				}
				close (LOG);
			}
			closedir(DIR);
			open (LOG, "$admin_dir/log.txt");
			while ($_ = <LOG>) {
				@c = split(/;/, $_);
				print TEMP "$c[0]\t$c[2]\n";
			}
			close (LOG);
			close (TEMP);
		}
		&show_gauge($header, "$caption -- Estimating time to process $counter file(s)", 0, "$actionurl&counter=$counter&done=0&tempfile2=$tempfile2&tempfile=$tempfile&amonly=$amonly&cleaner=$clean");
	} else {
		$tempfile = $FORM{'tempfile'};
		$tempfile =~ s/\W//g;
		if (!$amonly && !$clean) {
			open (TEMPFILE, "$admin_dir/msg_index/$tempfile.TMP");
			while (<TEMPFILE>) {
				chomp;
				($number, $time) = split(/\s+/, $_);
				next if $number !~ m|^\d+$|;
				next if $time !~ m|^\d+$|;
				$LOG_TIME{$number} = $time;
			}
			close (TEMPFILE);
		}
		($fileref) = &incremental($FORM{'tempfile2'}, 2, "", "");
		@TEMP2 = @{$fileref};
		$time_starter = time;
		if (!$amonly && !$clean) {
			($newpage, $addfile) = &determine_templates($topic_number);
			if (!-e "$admin_dir/msg_index/$topic_number") {
				mkdir ("$admin_dir/msg_index/$topic_number", oct($perms0777)) || &error_message("Directory Creation Error", "The directory $admin_dir/msg_index/$topic_number could not be created.  Check that the 'msg_index' directory has proper permissions (world writable, 0777 rwxrwxrwx) and try again.");
				chmod (oct($perms0777), "$admin_dir/msg_index/$topic_number");
			}
		}
		$sec_due_to_factor = int($GLOBAL_OPTIONS{'reindex_factor'} / 30);
		undef %fileadd;
		my ($line, @msg);
		open (ATTACH_MGR, ">>$admin_dir/msg_index/attach-mgr.txt") if !$clean;
LP:		while (time <= ($time_starter + 2 + $sec_due_to_factor)) {
			$file = shift(@TEMP2);
			last LP if $file eq "";
			$FORM{'done'} += 1;
			$file =~ m|^(\d+)/(\d+)|;
			$page = $2; $topic = $1; $topic_number = $topic;
			($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
			if (!$clean) {
				$ctrline = 0; $badtime = 0; $badflag = 0; $foundflag = 1;
				@msg = split(/\n/, $message);
				undef $message;
				foreach $line (@msg) {
					if ($line =~ m|<!-Post: (\d+)-!>|) {
						$num = $1; $flag = 1; $foundflag = 0;
						$badtime = 0; $badflag = 0;
						if ($line =~ m|<!-Time: (\d+)-!>|) {
							next if $1 != 0;
							$line = join("", $`, $');
						}
						if ($LOG_TIME{$num} == 0) {
							$line .= "<!-Time: 0-!>"; $badtime = 1; $badflag = $ctrline;
						} else {
							$line .= "<!-Time: $LOG_TIME{$num}-!>";
						}
					} elsif (($line =~ m|^<FONT SIZE="?-1"?>| || $line =~ m|^<!-Icons-!>|) && $flag && !$foundflag) {
						$x = "";
						if ($line =~ m|<B><A HREF=(['"])([^>]+)(['"])>(.*?)</A>|) {
							$x = $'; $foundflag = 1; $f = $4; $t = $2; $b = $`;
							$f =~ s/\s+$//;
							$f =~ s/^\s+//;
							$line = join("", $b, "<B><A HREF=\"$t\"><!-Name-!>$f<!-/Name-!></A>", $x);
						} elsif ($line =~ m|<B><A HREF=([^>]+)>(.*?)</A>|) {
							$x = $'; $foundflag = 1; $b = $`; $o = $1; $t = $2;
							$t =~ s/\s+$//;
							$t =~ s/^\s+//;
							$line = join("", $b, "<B><A HREF=\"$o\"><!-Name-!>$t<!-/Name-!></A>", $x);
						} elsif ($line =~ m|<B>(.*?)</B>|) {
							$x = $'; $foundflag = 1; $o = $1; $b = $`;
							$o =~ s/\s+$//;
							$o =~ s/^\s+//;
							$line = join("", $b, "<B><!-Name-!>$o<!-/Name-!></B>", $x);
						}
						if ($badtime == 1) {
							$badtime = 0;
							if ($x =~ m|<B>(.*?)</B>|) {
								$o = $1;
								$tv = &inverse_get_date_time("long", $o);
								if ($tv > 0) {
									$msg[$badflag] =~ s%<!-Time: 0-!>%<!-Time: $tv-!>%;
									&log_error("src-board-subs-11", "upgrade_filestructure", "$o => $tv, line $badflag, now $msg[$badflag]");
								}
							} else {
								&log_error("src-board-subs-11", "upgrade_filestructure", "Could not determine date for line $badflag ending in $x");
							}
						}
					} elsif ($line =~ m|<!-/Post: (\d+)-!>|) {
						$flag = 0;
					}
					$ctrline += 1;
				}
				$message = join("\n", @msg);
			}
			if ($message_src ne "" && !$amonly && !$clean) {
				if (!-e "$admin_dir/msg_index/$topic_number/$page-src.txt") {
					@msg_src = split(/\n/, $message_src);
					$string = ""; $flag = 0; $bigstring = "";
					foreach $line (@msg_src) {
						if ($line =~ m|^<!-Source: (\d+)-!>|) {
							$flag = $1;
						} elsif ($line =~ m|<!-/Source: (\d+)-!>|) {
							$bigstring .= "$flag ";
							$string =~ s/\s+$//;
							$bigstring .= &escape($string);
							$bigstring .= "\n";
							while ($string =~ m|\\image_alreadyuploaded\{(\d+)|) {
								$string = join("", $`, $');
								print ATTACH_MGR "$flag $1.gif\n";
							}
							while ($string =~ m|\\jpeg_alreadyuploaded\{(\d+)|) {
								$string = join("", $`, $');
								print ATTACH_MGR "$flag $1.jpg\n";
							}
							while ($string =~ m|\\attachment_alreadyuploaded\{([^,]+)|) {
								$string = join("", $`, $');
								print ATTACH_MGR "$flag $1\n";
							}
							$flag = 0;
							$string = "";
						} elsif ($flag != 0) {
							chomp $line;
							$string .= &unescape($line);
							$string .= " ";
						}
					}
					&write_source($topic, $page, 0, $bigstring, 1, 1) if $GLOBAL_OPTIONS{'nosource'} == 0;
					$message_src = "";
				}
			}
			if (!$amonly) {
				$maxlev = 0;
				while ($head =~ m|<!--Level (\d+): (\d+)/|g) {
					$maxlev = $1 if $1 > $maxlev;
				}
				if ($maxlev <= 1) {
					$head =~ m|<!--Topic: (\d+)/|;
					$my_parent = $1;
				} else {
					$maxlev -= 1;
					$head =~ m|<!--Level $maxlev: (\d+)/|;
					$my_parent = $1;
				}
				if ($head !~ m|<!--Parent: $my_parent-->|) {
					&log_error("src-board-subs-11", "upgrade_filestructure", "Clean board: invalid parent number found for page $topic/$page: should be $my_parent");
					if ($head =~ m|<!--Parent: (\d+)-->|) {
						$head = join("", $`, "<!--Parent: $my_parent-->", $');
					} else {
						$head .= "\n" if $head !~ m|\n$|;
						$head .= "<!--Parent: $my_parent-->\n";
					}
				}
				$sublist =~ s|<!-Top: (\d+)-!>|\n<!-Top: $1-!>|g;
				$sublist =~ s|<!-URL: (\d+)-!>|\n<!-URL: $1-!>|g;
				@subs = split(/\n/, $sublist);
				@subs = grep(/\S/, @subs);
				undef %FOUND;
				foreach $subline (@subs) {
					if ($subline =~ m|<!-Top: (\d+)-!>|) {
						$subnum = $1; $is_file = 1;
					} elsif ($subline =~ m|<!-URL: (\d+)-!>|) {
						$subnum = $1; $is_file = 0;
					} else {
						&log_error("src-board-subs-11", "upgrade_filestructure", "Clean board: this subtopic line is invalid: $subline");
						$subline = "";
					}
					if ($FOUND{$subnum} == 1) {
						&log_error("src-board-subs-11", "upgrade_filestructure", "Clean board: this subtopic line is repeated: $subline");
						$subline = "";
					}
					if ($subnum == $page || $subnum == $topic) {
						&log_error("src-board-subs-11", "upgrade_filestructure", "Clean board: this subtopic line is not valid: $subline");
						$subline = "";
					}
					if ($is_file && !-e "$message_dir/$topic/$subnum.$ext" && !-e "$secdir/$topic/$subnum.$ext") {
						&log_error("src-board-subs-11", "upgrade_filestructure", "Clean board: this referenced page does not exist: $subline");
						$subline = "";
					}
					$FOUND{$subnum} = 1;
					$subline .= "\n" if $subline ne "";
				}
				$sublist = join("", @subs);
			} elsif ($amonly) {
				undef @source;
				if ($GLOBAL_OPTIONS{'nosource'} == 0 && -e "$admin_dir/msg_index/$topic_number/$page-src.txt") {
					open (FILESOURCE, "$admin_dir/msg_index/$topic_number/$page-src.txt");
					@source = <FILESOURCE>;
					close (FILESOURCE);
					foreach $line (@source) {
						chomp $line;
						($flag, $big) = split(/\s+/, $line);
						$string = &unescape($big);
						while ($string =~ m|\\image_alreadyuploaded\{(\d+)|) {
							$string = join("", $`, $');
							print ATTACH_MGR "$flag $1.gif\n";
							$atseen{"$1.gif"} = 1;
						}
						while ($string =~ m|\\jpeg_alreadyuploaded\{(\d+)|) {
							$string = join("", $`, $');
							print ATTACH_MGR "$flag $1.jpg\n";
							$atseen{"$1.jpg"} = 1;
						}
						while ($string =~ m|\\attachment_alreadyuploaded\{([^,]+)|) {
							$string = join("", $`, $');
							print ATTACH_MGR "$flag $1\n";
							$atseen{"$1"} = 1;
						}
					}
				} else {
					@msg = split(/\n/, $message);
					$ctflag = 0;
					foreach $line (@msg) {
						if ($line =~ m|<!-Post: (\d+)-!>|) {
							$ctflag = $1;
						} elsif ($line =~ m|<!-/Post:|) {
							$ctflag = 0;
						} elsif ($ctflag > 0) {
							while ($line =~ m|<IMG SRC="$message_url/(\d+)/(\d+)\.gif" |g) {
								print ATTACH_MGR "$ctflag $2.gif\n" if !$atseen{"$2.gif"};
								$atseen{"$2.gif"} = 1;
							}
							while ($line =~ m|<IMG SRC="$message_url/(\d+)/(\d+)\.jpg" |g) {
								print ATTACH_MGR "$ctflag $2.jpg\n" if !$atseen{"$2.jpg"};
								$atseen{"$2.jpg"} = 1;
							}
							while ($line =~ m|<!-Attachment: (.*?)-!>|g) {
								print ATTACH_MGR "$ctflag $1\n" if !$atseen{"$1"};
								$atseen{"$1"} = 1;
							}
						}
					}
				}
				@msg = split(/\n/, $message);
			}
			&set_page($topic, $page, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, "", $description_src, $template, $addfile, 1) if !$amonly;
		}
		close (ATTACH_MGR);
		($status, $foo, $tempfile2) = &incremental($FORM{'tempfile2'}, 3, \@TEMP2, "");
		if ($status == 1) {
			if ($FORM{'counter'} > 0) {
				$pct = ($FORM{'done'} / $FORM{'counter'});
			} else {
				$pct = 0;
			}
			&show_gauge($header, "$caption -- $FORM{'done'} of $FORM{'counter'} finished", $pct, "$actionurl&REPAIR_LOG=$FORM{'REPAIR_LOG'}&tempfile2=$tempfile2&counter=$FORM{'counter'}&done=$FORM{'done'}&tempfile=$tempfile&amonly=$amonly&cleaner=$clean");
		}
		unlink "$admin_dir/msg_index/$tempfile.TMP";
		&seturl($doneurl);
	}
}

#---SEPARATOR---#

sub rebuild_logs {
	my ($header, $actionurl, $doneurl) = @_;
	undef %LOGFLAG;
	&seturl($doneurl) if !-e "$admin_dir/log.txt";
	if (-e "$admin_dir/backups/old-log.txt") {
		$size = -s "$admin_dir/log.txt";
		&seturl($doneurl) if $size == 0;
	}
	open (BULOG, ">$admin_dir/backups/old-log.txt");
	open (NEWLOG, ">$admin_dir/msg_index/new-log.txt") || &error_message("Could not create temporary log file", "The file $admin_dir/msg_index/new-log.txt could not be created.  Check that permissions are 0777 (rwxrwxrwx) on the msg_index subdirectory.");
	close (NEWLOG);
	unlink ("$admin_dir/msg_index/new-log.txt");
	undef %seen;
	open (OLDLOG, "$admin_dir/log.txt");
	while (<OLDLOG>) {
		print BULOG $_;
		if (m|^(\d+);([^;]+);([^;]+);([^;]+)|) {
			$pn = $1; $u = $4; $t = (split(/\//, $u))[0];
			next if $LOGFLAG{"$t-$pn"} == 1;
			$fhname = "NEWFILE$t";
			if ($seen{$t} == 0) {
				$seen{$t} = 1;
				open ($fhname, "$admin_dir/msg_index/$t-log.txt");
				while ($x_in = <$fhname>) {
					($post) = split(/;/, $x_in, 2);
					$LOGFLAG{"$t-$post"} = 1;
				}
				close ($fhname);
				open ($fhname, ">>$admin_dir/msg_index/$t-log.txt");
				print $fhname $_ ;
			} else {
				print $fhname $_;
			}
		}
	}
	close (OLDLOG);
	open (OLDLOG, ">$admin_dir/log.txt");
	close (OLDLOG);
	foreach $key (keys(%seen)) {
		$fhname = "NEWFILE$t";
		close ($fhname);
	}
	&seturl($doneurl);
}

#---SEPARATOR---#
#REQ:search_stop
#REQ:prune_messages
#REQ:pro_branch
#REQ:regenerate_subtopic_list
#REQ:regenerate_message

sub branches {
	my ($code, $topic, $page, $argument1, $argument2, $template, $addfile) = @_;
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	if ($code == 1) {
		&rename_subtopic("Topic", $argument1, $topic, $page, $template, $addfile);
	}
	if ($code == 2) {
		&chgrp_topic($topic, $page, $argument1, $template, $addfile);
	}
	if ($code == 3) {
		@a = split(/\t/, $argument1);
		&chmeta($topic, $page, $a[0], $a[1], $a[2], $template, $addfile);
	}
	if ($code == 4) {
		($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
		$head =~ m|<!--Param: (.*)-->|; $param = $1;
		if ($param =~ m|Sublist|) {
			$x = length($sublist);
			$sublist = &regenerate_subtopic_list($topic, $page, $sublist, 1);
			if ($x != 0 && $sublist eq "") {
				&log_error("src-board-subs-11", "branches", "Subtopic list regeneration failed on page $topic/$page");
			}
		}
		if ($param =~ m|Messages|) {
			$x = length($message);
			$message = &regenerate_message($topic, $page, $message, 1);
			if ($x != 0 && $message eq "") {
				&log_error("src-board-subs-11", "branches", "Message list regeneration failed on page $topic/$page");
			}
		}
		&set_page($topic, $page, $head, $argument1, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src, $template, $addfile, 1);
	}
	if ($code == 5) {
		$old_html_url = quotemeta($FORM{'old_html_url'});
		($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
		if ($argument1 != 0) {
			@head = split(/\n/, $head);
			@sublist = split(/\n/, $sublist);
			if (!grep(/<!--Parent:/, @head)) {
				$head .= "<!--Parent: $my_parent-->\n";
			}
			foreach $line (@sublist) {
				if ($line =~ m|$message_url/(\d+)/(\d+).$ext#(\w+)|) {
					$line = join("", $`, "$message_url/$1/$2.$ext?$3", $') if !$noqm;
					$line = join("", $`, "$message_url/$1/$2.$ext", $') if $noqm;
				}
				if ($line =~ m|$old_html_url/(\d+)/(\d+).$ext#(\w+)|) {
					$line = join("", $`, "$message_url/$1/$2.$ext?$3", $') if !$noqm;
					$line = join("", $`, "$message_url/$1/$2.$ext", $') if $noqm;
				}
				$line =~ s|<A HREF="$old_html_url/messages/(\d+)/(\d+)\.(\w+)|<A HREF="$message_url/$1/$2.$3|g;
			}
			$message =~ s|<IMG SRC="([^"]+)/(\d+)/(\d+)\.(\w+)"([^>]*)>|<IMG SRC="$message_url/$2/$3\.$4"$5>|g;
			$message =~ s|<!-Attachment: ([^!]+)-!><CENTER><TABLE BORDER=1><TR><TD><IMG SRC="([^"]+)/icons/([^"]+)\.gif" ALIGN=LEFT ALT="([^"]+)">([^<]*)<BR><A HREF="([^"]+)/(\d+)/([^"]+)"><B>([^<]+)</B></A> \(([^>]+)\)</TD></TR></TABLE></CENTER><!-/Attachment-!>|<!-Attachment: $1-!><CENTER><TABLE BORDER=1><TR><TD><IMG SRC="$html_url/icons/$3.gif" ALIGN=LEFT ALT="$4">$5<BR><A HREF="$message_url/$7/$8"><B>$9</B></A> \($10\)</TD></TR></TABLE></CENTER><!-/Attachment-!>|g;
			$message =~ s|<IMG SRC="([^"]+)/clipart/([^"]+)">|<IMG SRC="$html_url/clipart/$2">|g;
			$about =~ s|<IMG SRC="([^"]+)/clipart/([^"]+)">|<IMG SRC="$html_url/clipart/$2">|g;
			$message =~ s|<A HREF="([^"]+)/board-profile\.(\w+)|<A HREF="$script_url/board-profile.$cgi_extension|g;
			$message =~ s|<A HREF="$old_html_url/messages/(\d+)/(\d+)\.(\w+)|<A HREF="$message_url/$1/$2.$ext|g;
			$about =~ s|<A HREF="$old_html_url/messages/(\d+)/(\d+)\.(\w+)|<A HREF="$message_url/$1/$2.$3|g;
			$sublist = join("\n", @sublist);
		}
		&set_page($topic, $page, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src, $template, $addfile, 1);
	}
	if ($code == 6) {
		if ($GLOBAL_OPENED == 0) {
			open (INDEX, ">>$admin_dir/msg_index/$topic-search.txt");
			$GLOBAL_OPENED = 1;
		}
		($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
		@msg = split(/\n/, $message); $flag = 0;
		foreach $line (@msg) {
			if ($line =~ m|<!-Post: (\d+)-!>|) {
				$flag = $1;
			} elsif ($line =~ m|<!-Text-!>(.*)<!-/Text-!>|) {
				$text = $1;
				$newtext = &remove_html($text);
				($newtext) = &search_stop($newtext);
				print INDEX "$flag $topic $page $newtext\n";
			}
		}
	}
	if ($code == 7) {
		&prune_messages($topic, $page, $argument1, $argument2);
	}
	if ($code >= 15) {
		&pro_branch($code, $topic, $page, $argument1, $argument2, $template, $addfile);
	}
}

sub rename_subtopic {
	my ($type, $newname, $topic, $page, $template, $addfile) = @_;
	($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
	@hd = split(/\n/, $head);
	foreach $line (@hd) {
		if ($line =~ /<!--$type: (\d+)\//) {
			$me_num = $1;
			$line = "<!--$type: $me_num/$newname-->";
		} elsif ($line =~ /<!--Me: $me_num\//) {
			$line = "<!--Me: $me_num/$newname-->";
		}
	}
	$head = join("\n", @hd);
	&set_page($topic, $page, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src, $template, $addfile);
}

sub chgrp_topic {
	my ($topic, $page, $argument1, $template, $addfile) = @_;
	($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
	$head =~ s/<!--Owner: (\w*)-->/<!--Owner: $argument1-->/g;
	&set_page($topic, $page, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src, $template, $addfile);
}

sub chmeta {
	my ($topic, $page, $descr, $key, $robots, $template, $addfile) = @_;
	($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
	$meta_description = $descr;
	$meta_keywords = $key;
	$meta_robots = $robots;
	&set_page($topic, $page, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src, $template, $addfile);
}

#---SEPARATOR---#

sub incremental {
	my ($tempfilename, $operation, $fileref, $topic) = @_;
	my ($tempfile, @file, $line, @top, @topics, $dir, $counter, $content);
	$tempfilename =~ s/\W//g; undef @topics;
	if ($operation == 1) {
		if ($topic eq "" || $topic !~ m|^[\d\,]+$|) {
			open (TOPICS, "$message_dir/$board_topics_file"); @top = <TOPICS>; close (TOPICS);
			foreach $line (@top) {
				if ($line =~ m|<!-Top: (\d+)-!>|) {
					push (@topics, $1);
				}
			}
			return 0 if scalar(@topics) == 0;
		} else {
			@topics = split(/,/, $topic);
		}
		$tempfilename = join("", $$, time, $tempfilename); $tempfilename =~ s/\W//g;
		$tempfile = "$admin_dir/msg_index/$tempfilename.TMP";
		undef @file;
		open (TEMPFILE, ">$tempfile");
		foreach $topic (@topics) {
			if (-e "$message_dir/$topic") {
				opendir(DIR, "$message_dir/$topic");
				while ($dir = readdir(DIR)) {
					if ($dir =~ m|^(\d+)\.$ext$|) {
						print TEMPFILE "$topic/$dir\n"; $counter += 1;
						push (@file, "$topic/$dir\n");
					}
				}
				closedir(DIR);
			} elsif (-e "$secdir/$topic") {
				opendir(DIR, "$secdir/$topic");
				while ($dir = readdir(DIR)) {
					if ($dir =~ m|^(\d+)\.$ext$|) {
						print TEMPFILE "$topic/$dir\n"; $counter += 1;
						push (@file, "$topic/$dir\n");
					}
				}
				closedir(DIR);
			}
		}
		close (TEMPFILE);
		$fileref = \@file;
		return ($fileref, $counter, $tempfilename);
	} elsif ($operation == 2) {
		$tempfile = "$admin_dir/msg_index/$tempfilename.TMP";
		open (TEMPFILE, $tempfile);
		@file = <TEMPFILE>;
		close (TEMPFILE);
		$fileref = \@file;
		return ($fileref, "", $tempfilename);
	} elsif ($operation == 3) {
		$tempfile = "$admin_dir/msg_index/$tempfilename.TMP";
		$content = join("", @{$fileref}); $content .= "\n" if $content !~ m|\n$|;
		if ($content !~ m|\S|) {
			unlink($tempfile);
			return (0, "", $tempfilename);
		} else {
			open (TEMPFILE, ">$tempfile");
			print TEMPFILE $content;
			close (TEMPFILE);
			return (1, "", $tempfilename);
		}
	}
}


# END - FILE IS CORRECTLY UPLOADED #
