# FILE: src-board-subs-2
# Subtopic and page manipulation
#-------------------------------------------------------------------------------
# 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:new_file
#REQ:get_number
#REQ:format_subtopics
#REQ:regenerate_subtopic_list

sub add_page {
	my ($newpage, $type, $url, $target, $tn, $mn, $grp, $datetime_in, $poster_name, $add_post, $archiving, $tree_locked) = @_;
	my ($num, $dt, @sublist, $line, $ts, $owner);
	$owner = $grp if $grp ne "";
	$num = &get_number;
	if ($tn != 0) {
		$topic_number = $tn; $me_number = $mn;
	}
	$url = "$message_url/$topic_number/$num.$ext" if $type ne "LINK";
	$dt = time;
	$newpage = &remove_links($newpage);
	$url .= "?$dt" if ($type ne "LINK" && !$noqm);
	$url_orig = $url; $url .= "\" TARGET=\"$target" if $type eq "LINK";
	$glo = $GLOBAL_OPTIONS{'alphabet_subs'}; @glo = split(/,/, $glo);
	$alph = 0; $alph = 1 if grep(/^$topic_number$/, @glo);
	$glo = $GLOBAL_OPTIONS{'reverse_subs'}; @glo = split(/,/, $glo);
	$rev = 0; $rev = 1 if grep(/^$topic_number$/, @glo);
	&new_file ($topic_number, $num, $me_number, $owner, $newpage, $type) if $type ne "LINK";
	&lock("add_page", "$admin_dir/msg_index/$topic_number-tree.txt");
	open (FILE, "$admin_dir/msg_index/$topic_number-tree.txt");
	@TREE_STRUCTURE_FILE = <FILE>;
	close (FILE);
	$ct = 0; $ct_2 = 0; undef @my_p; $flag = -1; $hold = 0; undef %found;
	foreach $line (@TREE_STRUCTURE_FILE) {
		$ct++;
		@c = split(/\t/, $line);
		if ($c[2] == $me_number) {
			$flag = $c[0];
			$hold = $ct;
			$chold = $line;
		} elsif ($flag != -1) {
			if ($c[0] <= $flag) {
				$flag = -1;
				last;
			} else {
				push(@my_p, $line);
				if ($c[0] == (1+$flag)) {
					$found{$c[2]} = $line;
					$my_pos = $c[2];
				} else {
					$found{$my_pos} .= $line;
				}
			}
		}
	}
	@c = split(/\t/, $chold);
	$poster_name =~ s/\([^\(\)]*\)//g;
	$newline = join("\t", ($c[0]+1), $c[1], $num, $me_number, &escape($newpage), $type, $owner, $add_post, $dt, time, $poster_name, "") if $type ne "LINK";
	$newline = join("\t", ($c[0]+1), $c[1], $num, $me_number, &escape($newpage), "LINK:$target", &escape($url_orig), 0, $dt, time, "", "") if $type eq "LINK";
	$newline .= "\n";
	if (($alph == 0 && $rev == 0) || $archiving) {
		push (@my_p, $newline);
	} elsif ($alph == 0 && $rev == 1) {
		splice(@my_p, 0, 0, $newline);
	} elsif ($alph == 1) {
		$found{"newpost"} = $newline;
		@my_p_new = sort alphabetical_tree values(%found);
		@my_p_new = reverse(@my_p_new) if $rev;
		$x = join("", @my_p_new);
		@my_p = split(/\n/, $x);
		foreach $line (@my_p) {
			$line .= "\n";
		}
	}
	splice(@TREE_STRUCTURE_FILE, $hold, scalar(@my_p)-1, @my_p);
	$pwline = \@TREE_STRUCTURE_FILE;
	&safe_write("$admin_dir/msg_index/$topic_number-tree.txt", $pwline);
	&unlock("add_page", "$admin_dir/msg_index/$topic_number-tree.txt");
	&lock("add_page", "$message_dir/$topic_number/$me_number.$ext");
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number,$me_number);
	$ts = &get_date_time('shorter');
	$ts = $datetime_in if $datetime_in ne "";
	($sl1) = &format_subtopics($url, $newpage, $ts, 0, "", $ct_2);
	if (($rev == 0 && $alph == 0) || $archiving) {
		$sublist .= "<!-Top: $num-!>$sl1\n" if $type ne "LINK";
		$sublist .= "<!-URL: $num-!>$sl1\n" if $type eq "LINK";
	} else {
		$sublist = "<!-Top: $num-!>$sl1\n$sublist" if $type ne "LINK";
		$sublist = "<!-URL: $num-!>$sl1\n$sublist" if $type eq "LINK";
	}
	@sublist = split(/\n/, $sublist);
	@sublist = grep(/\S/, @sublist);
	@sublist = sort alphabetical_sort(@sublist) if $alph;
	@sublist = reverse(@sublist) if ($alph && $rev);
	$sublist = join("\n", @sublist);
	$sublist = &regenerate_subtopic_list($topic_number, $me_number, $sublist);
	&set_page($topic_number, $me_number, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	&unlock("add_page", "$message_dir/$topic_number/$me_number.$ext");
	return $num;
}

sub alphabetical_sort {
	$a =~ m|<A[^>]+>(.*)</A>|i; $subnamea = $1;
	$b =~ m|<A[^>]+>(.*)</A>|i; $subnameb = $1;
	$subnamea =~ tr/A-Z/a-z/;
	$subnameb =~ tr/A-Z/a-z/;
	return 1 if $subnamea gt $subnameb;
	return -1 if $subnamea lt $subnameb;
	return 0;
}

sub alphabetical_tree {
	$a =~ m|^(\d+)\t(\d+)\t(\d+)\t(\d+)\t(\S+)|; $ac = &unescape($5);
	$b =~ m|^(\d+)\t(\d+)\t(\d+)\t(\d+)\t(\S+)|; $bc = &unescape($5);
	$ac =~ tr/A-Z/a-z/;
	$bc =~ tr/A-Z/a-z/;
	return 1 if $ac gt $bc;
	return -1 if $ac lt $bc;
	return 0;
}

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

sub rename_subtopic {
	my ($referer, $num, $newname) = @_;
	my ($file, @lines, $line, @files, @file, %seen, @head, @sublist);
	&extract($referer);
	$file = "$message_dir/$topic_number/$me_number.$ext" if -e "$message_dir/$topic_number/$me_number.$ext";
	$file = "$secdir/$topic_number/$me_number.$ext" if !-e "$message_dir/$topic_number/$me_number.$ext";
	&lock("rename_subtopic", "$admin_dir/msg_index/$topic_number-tree.txt");
	open (FILE, "$admin_dir/msg_index/$topic_number-tree.txt"); @file = <FILE>; close (FILE);
	foreach $line (@file) {
		@c = split(/\t/, $line);
		if ($c[1] == $topic_number && $c[2] == $num) {
			$c[4] = &escape($newname);
			$line = join("\t", @c);
			@chold = @c;
			last;
		}
	}
	open (FILE, ">$admin_dir/msg_index/$topic_number-tree.txt"); print FILE @file; close (FILE);
	&unlock("rename_subtopic", "$admin_dir/msg_index/$topic_number-tree.txt");
	foreach $line (@file) {
		@c = split(/\t/, $line);
		if ($c[3] == $num || $seen{$c[3]} == 1) {
			push (@files, $c[2]);
			$seen{$c[2]} = 1;
		}
	}
	&lock("rename_subtopic", "$message_dir/$topic_number/$me_number.$ext");
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number, $me_number);
	@sublist = split(/\n/, $sublist);
	$ctr = 0;
	foreach $line (@sublist) {
		if ($line =~ m|<!-Top: $num-!>|) {
			$ctr += 1;
			$url = "$message_url/$topic_number/$num.$ext";
			$url .= "?" . $chold[9] if !$noqm;
			$x = &get_date_time('shorter', $chold[9]);
			$x = "-" if $chold[9] == 0;
			$line = &format_subtopics($url, $newname, $x, $chold[7], $chold[10], $ctr-1);
			$line = "<!-Top: $num-!>$line";
		} elsif ($line =~ m|<!-Top: (\d+)-!>|) {
			$ctr += 1;
		} elsif ($line =~ m|<!-URL: (\d+)-!>|) {
			$ctr += 1;
		}
	}
	$sublist = join("\n", @sublist);
	$sublist = &regenerate_subtopic_list($topic_number, $me_number, $sublist);
	&set_page($topic_number, $me_number, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	&unlock("rename_subtopic", "$message_dir/$topic_number/$me_number.$ext");
	&lock("rename_subtopic", "$message_dir/$topic_number/$num.$ext");
	($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number, $num);
	@head = split(/\n/, $head); $LN = 1;
	foreach $line (@head) {
		if ($line =~ m|<!--Level (\d+): $num/(.*)-->|) {
			$LN = $1;
			$line = join("", $`, "<!--Level $LN: $num/$newname-->", $');
		} elsif ($line =~ m|<!--Me: $num/(.*)-->|) {
			$line = join("", $`, "<!--Me: $num/$newname-->", $');
		}
	}
	$head = join("\n", @head);
	&set_page($topic_number, $num, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	&unlock("rename_subtopic", "$message_dir/$topic_number/$num.$ext");
	undef @LM;
	foreach $file (@files) {
		push (@LM, "$message_dir/$topic_number/$file.$ext");
	}
	&lock("rename_subtopic", @LM);
	foreach $file (@files) {
		next if $file == $num;
		my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number, $file);
		@head = split(/\n/, $head); $LN = 1;
		foreach $line (@head) {
			if ($line =~ m|<!--Level $LN: $num/(.*)-->|) {
				$line = join("", $`, "<!--Level $LN: $num/$newname-->", $');
			}
		}
		$head = join("\n", @head);
		&set_page($topic_number, $file, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	}
	&unlock("rename_subtopic", @LM);
}

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

sub rename_link {
	my ($referer, $linknum, $newname, $newurl, $newtarget) = @_;
	my ($file, $line, $topic, $page, $url, $target, $str);
	if ($referer =~ m|/(\d+)/(\d+)\.$ext|) {
		($topic, $page) = ($1, $2);
	} else {
		&error_message("Rename Link Error", "Could not extract requested page.");
	}
	&lock("rename_link", "$admin_dir/msg_index/$topic_number-tree.txt");
	open (FILE, "$admin_dir/msg_index/$topic_number-tree.txt"); @file = <FILE>; close (FILE);
	foreach $line (@file) {
		@c = split(/\t/, $line);
		if ($c[1] == $topic_number && $c[2] == $linknum) {
			$c[4] = &escape($newname);
			$c[5] = "LINK:$newtarget";
			$c[6] = &escape($newurl);
			$line = join("\t", @c);
			last;
		}
	}
	open (FILE, ">$admin_dir/msg_index/$topic_number-tree.txt"); print FILE @file; close (FILE);
	&unlock("rename_link", "$admin_dir/msg_index/$topic_number-tree.txt");
	&lock("rename_link", "$message_dir/$topic/$page.$ext");
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic,$page);
	@sublist = split(/\n/, $sublist); $ctr = 0;
	foreach $line (@sublist) {
		if ($line =~ /<!-URL: $linknum-!>/) {
			$ctr += 1;
			$line = "<!-URL: $linknum-!>" . &format_subtopics("$newurl\" TARGET=\"$newtarget", $newname, "", "", "", $ctr);
		} elsif ($line =~ m|<!-Top: (\d+)-!>|) {
			$ctr += 1;
		} elsif ($line =~ m|<!-URL: (\d+)-!>|) {
			$ctr += 1;
		}
	}
	$sublist = join("\n", @sublist);
	$sublist = &regenerate_subtopic_list($topic, $page, $sublist);
	&set_page($topic, $page, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	&unlock("rename_link", "$message_dir/$topic/$page.$ext");
}

#---SEPARATOR---#
#REQ:regenerate_subtopic_list

sub reorder_subtopics {
	my ($referer, $array) = @_;
	my (%T_Items, %seen, @ascending,$line,@lines,%newarray,$flag,$key,%printed);
	@ascending = split(/:/, $array);
	undef %T_Items; undef %seen; &extract($referer);
	&lock("reorder_subtopics", "$admin_dir/msg_index/$topic_number-tree.txt");
	open (FILE, "$admin_dir/msg_index/$topic_number-tree.txt"); @TREE_STRUCTURE_FILE = <FILE>; close (FILE);
	foreach $line (@TREE_STRUCTURE_FILE) {
		@c = split(/\t/, $line);
		if ($c[3] == $me_number && $c[1] == $topic_number) {
			$seen{$c[3]} = $me_number;
			$T_Items{$c[2]} .= $line;
			$line = "";
		} elsif ($seen{$c[3]} != 0) {
			$seen{$c[2]} = $seen{$c[3]};
			$T_Items{$seen{$c[2]}} .= $line;
			$line = "";
		}
	}
	@TREE_STRUCTURE_FILE = grep(/\S/, @TREE_STRUCTURE_FILE);
	open (FILE, ">$admin_dir/msg_index/$topic_number-tree.txt");
	foreach $line (@TREE_STRUCTURE_FILE) {
		@c = split(/\t/, $line);
		if ($c[2] == $me_number && $c[1] == $topic_number) {
			print FILE $line;
			foreach $key (@ascending) {
				print FILE $T_Items{$key};
			}
		} else {
			print FILE $line;
		}
	}
	close (FILE);
	undef @TREE_STRUCTURE_FILE;
	&unlock("reorder_subtopics", "$admin_dir/msg_index/$topic_number-tree.txt");
	&lock("reorder_subtopics", "$message_dir/$topic_number/$me_number.$ext");
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number,$me_number);
	@sublist = split(/\n/, $sublist);
	undef %printed;
	foreach $line (@sublist) {
		if ($line =~ /<!-Top: (\d+)-!>/ || $line =~ /<!-URL: (\d+)-!>/) {
			$newarray{$1} = $line;
			$printed{$1} = 0;
		}
	}
	undef @sublist;
	foreach $key (@ascending) {
		push (@sublist, $newarray{$key});
		$printed{$key} = 1;
	}
	foreach $key (keys(%printed)) {
		push (@sublist, $newarray{$key}) if $printed{$key} == 0;
	}
	$sublist = join("\n", @sublist);
	$sublist = &regenerate_subtopic_list($topic_number, $me_number, $sublist);
	&set_page($topic_number, $me_number, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	&unlock("reorder_subtopics", "$message_dir/$topic_number/$me_number.$ext");
}

#---SEPARATOR---#

sub change_about_message {
	my ($file, $source, $formatted) = @_;
	my (@file, $messageflag, $line, $source_esc, $topic, $page);
	if ($file =~ m|/(\d+)/(\d+)\.$ext|) {
		($topic, $page) = ($1, $2);
	} else {
		&error_message("Change About Message Error", "Could not open requested file");
	}
	&lock("change_about_message", "$message_dir/$topic/$page.$ext");
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
	&set_page($topic, $page, $head, $color, $lm, $ann, $ann_src, $sublist, $formatted, &escape($source), $message, $message_src, $description_src);
	&unlock("change_about_message", "$message_dir/$topic/$page.$ext");
}

#---SEPARATOR---#
#REQ:change_board_colors
#REQ:regenerate_subtopic_list

sub remove_page {
	my ($topic_number, $parent, $page_number) = @_;
	my ($pn, @pn, @lines, $line, $filename, $filenumber, @ps);
	@pn = split(/,/, $page_number);
	undef @pn if $page_number eq "***";
	foreach $pn (@pn) {
		$pn =~ s/^0//g;
	}

	&lock("remove_page", "$message_dir/$topic_number/$parent.$ext");
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number, $parent);
	@lines = split(/\n/, $sublist);
	foreach $line (@lines) {
		$filenumber = "-NONE-";
		$filenumber = $1 if $line =~ m|<!-Top: (\d+)-!>|;
		$filenumber = $1 if $line =~ m|<!-URL: (\d+)-!>|;
		if ($page_number eq "***" || grep(/^$filenumber$/, @pn)) {
			$line = "";
			push (@ps, $filenumber);
		}
	}
	$sublist = join("\n", grep(/\S/, @lines));
	&set_page($topic_number, $parent, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	&unlock("remove_page", "$message_dir/$topic_number/$parent.$ext");

	&lock("remove_page", "$admin_dir/msg_index/$topic_number-tree.txt");
	undef %seen;
	undef @TREE_STRUCTURE_FILE;
	open (TREE, "$admin_dir/msg_index/$topic_number-tree.txt");
	@TREE_STRUCTURE_FILE = <TREE>;
	close (TREE);
	undef @nukes; undef %msgs;
	undef %updates;
	foreach $line (reverse(@TREE_STRUCTURE_FILE)) {
		@c = split(/\t/, $line);
		if ($c[2] == $parent) {
			$updates{$c[2]} = 1;
			$updates{$c[3]} = 1;
		} elsif ($updates{$c[2]} == 1) {
			$updates{$c[3]} = 1;
		}
	}
	$msg_remove = 0;
	foreach $line (@TREE_STRUCTURE_FILE) {
		@c = split(/\t/, $line); $c2 = $c[2];
		if ($c[1] == $topic_number && grep(/^$c2$/, @ps)) {
			$line = "";
			$seen{$c[2]} = 1;
			push (@nukes, $c[2]);
			$c[11] =~ s/\s//g;
			@x = split(/,/, $c[11]);
			foreach $x (@x) {
				$msgs{$x} = 1;
			}
			$msg_remove += $c[7];
		} elsif ($seen{$c[3]} == 1) {
			$seen{$c[2]} = 1;
			$line = "";
			push (@nukes, $c[2]);
			$c[11] =~ s/\s//g;
			@x = split(/,/, $c[11]);
			foreach $x (@x) {
				$msgs{$x} = 1;
			}
		}
	}
	@TREE_STRUCTURE_FILE = grep(/\S/, @TREE_STRUCTURE_FILE);
	foreach $line (@TREE_STRUCTURE_FILE) {
		@c = split(/\t/, $line);
		if ($updates{$c[2]}) {
			$c[7] -= $msg_remove;
			$line = join("\t", @c);
		}
	}
	$pwline = \@TREE_STRUCTURE_FILE;
	&safe_write("$admin_dir/msg_index/$topic_number-tree.txt", $pwline);
	&unlock("remove_page", "$admin_dir/msg_index/$topic_number-tree.txt");

	&lock("$admin_dir/tree.txt");
	open (TREE, "$admin_dir/tree.txt");
	@TREE_STRUCTURE_FILE = <TREE>;
	close (TREE);
	foreach $line (@TREE_STRUCTURE_FILE) {
		@c = split(/\t/, $line);
		if ($c[1] == $topic_number) {
			$c[7] -= $msg_remove; $line = join("\t", @c); last;
		}
	}
	open (TREE, ">$admin_dir/tree.txt");
	print TREE @TREE_STRUCTURE_FILE;
	close (TREE);
	&unlock("$admin_dir/tree.txt");

	if (-e "$message_dir/$topic_number") {
		$dirname = "$message_dir/$topic_number";
	} else {
		$dirname = "$secdir/$topic_number";
	}
	foreach $file (@nukes) {
		unlink ("$dirname/$file.$ext");
		unlink ("$admin_dir/msg_index/$topic_number/$file-src.txt");
	}
	&lock("remove_page", "$admin_dir/msg_index/attach-mgr.txt");
	undef @nukefile;
	open (FILE, "$admin_dir/msg_index/attach-mgr.txt");
	@file = <FILE>;
	close (FILE);
	foreach $line (@file) {
		($post, $file) = split(/\s+/, $line);
		if ($msgs{$post} == 1) {
			push (@nukefile, "$dirname/$file");
			$line = "";
		}
	}
	open (FILE, ">$admin_dir/msg_index/attach-mgr.txt");
	print FILE @file;
	close (FILE);
	&unlock("remove_page", "$admin_dir/msg_index/attach-mgr.txt");
	$filename = "$admin_dir/msg_index/$topic_number-log.txt";
	$fsize = 0;
	&lock("remove_page", $filename);
	open (LOG, "$filename");
	open (LOG2, ">$filename.NEW");
	while (<LOG>) {
		@c = split(/;/, $_, 2); $c0 = $c[0];
		if ($msgs{$c0} != 1) {
			print LOG2 $_;
			$fsize += length($_);
		}
	}
	close (LOG2); close (LOG);
	&rename_file($filename, $fsize);
	chmod (oct($perms0666), "$filename");
	&unlock("remove_page", $filename);
	$filename = "$admin_dir/msg_index/$topic_number-search.txt";
	&lock("remove_page", $filename);
	$fsize = 0;
	open (LOG, "$filename");
	open (LOG2, ">$filename.NEW");
	while (<LOG>) {
		@c = split(/\s/, $_, 2); $c0 = $c[0];
		if ($msgs{$c0} != 1) {
			print LOG2 $_;
			$fsize += length($_);
		}
	}
	close (LOG2); close (LOG);
	&rename_file($filename, $fsize);
	chmod (oct($perms0666), "$filename");
	&unlock("remove_page", $filename);
	foreach $file (@nukefile) {
		unlink($file);
	}
	foreach $key (keys(%updates)) {
		next if $key == 0;
		&lock("remove_page", "$message_dir/$topic_number/$key.$ext");
		my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number, $key);
		$sublist = &regenerate_subtopic_list($topic_number, $key, $sublist);
		&set_page($topic_number, $key, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
		&unlock("remove_page", "$message_dir/$topic_number/$key.$ext");
	}
	&change_board_colors("*");
	return 1;
}


#---SEPARATOR---#
#REQ:regenerate_subtopic_list
#REQ:change_board_colors
#REQ:show_gauge

sub move_page {
	my ($old_topic, $page_number, $old_parent, $new_topic, $new_parent) = @_;
	return 1 if ($old_topic == $new_topic && $old_parent == $new_parent);
	my ($headstr, $nextkey, $owner, $line, $param, @tomove, $temp, @pn, %pn, %seen, %post);
	my ($t1, $t2, $t3);
	my ($head1, $color1, $lm1, $ann1, $ann_src1, $sublist1, $about1, $about_src1, $message1, $message_src1, $description_src1);
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);

	@pn = split(/,/, $page_number);
	foreach $x (@pn) {
		$pn{$x} = 1;
	}

	# Change the tree file to reflect the move

	&lock("move_page", "$admin_dir/msg_index/$old_topic-tree.txt");
	open (TREE, "$admin_dir/msg_index/$old_topic-tree.txt");
	@tree = <TREE>;
	close (TREE);

	undef @insert; $msgs = 0; undef %updates;
	foreach $line (@tree) {
		@c = split(/\t/, $line);
		if ($pn{$c[2]} == 1) {
			$seen{$c[2]} = 1 + $lc;
			$msgs += $c[7];
			$c[0] = 1 + $lc;
			$line = join("\t", @c);
			push (@insert, $line);
			$line = "";
		} elsif ($seen{$c[3]} >= 1) {
			$seen{$c[2]} = 1 + $seen{$c[3]};
			$c[0] = 1 + $seen{$c[3]};
			$line = join("\t", @c);
			push (@insert, $line);
			$line = "";
		}
	}

	$t = time;
	undef %updates; undef %seenit;
	foreach $line (reverse(@tree)) {
		@c = split(/\t/, $line);
		if ($c[2] == $new_parent) {
			$updates{$c[2]} += 1;
			$seenit{$c[2]} = 1;
			$seenit{$c[3]} = 1;
			if ($c[5] !~ m|Sublist|) {
				$c[5] .= "Sublist";
				$line = join("\t", @c);
			}
		} elsif ($seenit{$c[2]}) {
			$updates{$c[2]} += 1;
			$seenit{$c[3]} = 1;
		}
	}
	undef %seenit;
	foreach $line (reverse(@tree)) {
		@c = split(/\t/, $line);
		if ($c[2] == $old_parent) {
			$updates{$c[2]} -= 1;
			$seenit{$c[2]} = 1;
			$seenit{$c[3]} = 1;
		} elsif ($seenit{$c[2]}) {
			$updates{$c[2]} -= 1;
			$seenit{$c[3]} = 1;
		}
	}
	foreach $line (@tree) {
		@c = split(/\t/, $line);
		if ($updates{$c[2]} == 1) {
			$c[7] += $msgs;
			$line = join("\t", @c);
		} elsif ($updates{$c[2]} == -1) {
			$c[7] -= $msgs;
			$line = join("\t", @c);
		}
		$line .= "\n" if $line !~ m|\n$|;
	}

	if ($old_topic != $new_topic) {
		open (TREE, ">$admin_dir/msg_index/$old_topic-tree.txt");
		print TREE @tree;
		close (TREE);
		&unlock("move_page", "$admin_dir/msg_index/$old_topic-tree.txt");
		&lock("move_page", "$admin_dir/tree.txt");
		open (TREE, "$admin_dir/tree.txt");
		@tree = <TREE>;
		close (TREE);
		foreach $line (@tree) {
			@c = split(/\t/, $line);
			if ($c[1] == $old_topic) {
				$c[7] -= $msgs;
				$line = join("\t", @c);
			} elsif ($c[1] == $new_topic) {
				$c[7] += $msgs;
				$c[5] .= "Sublist" if ($c[5] !~ m|Sublist| && $new_topic == $new_parent);
				$line = join("\t", @c);
			}
		}
		open (TREE, ">$admin_dir/tree.txt");
		print TREE @tree;
		close (TREE);
		&unlock("move_page", "$admin_dir/tree.txt");

		&lock("move_page", "$admin_dir/msg_index/$new_topic-tree.txt");
		open (TREE, "$admin_dir/msg_index/$new_topic-tree.txt");
		@tree = <TREE>;
		close (TREE);

		foreach $line (reverse(@tree)) {
			@c = split(/\t/, $line);
			if ($c[2] == $new_parent) {
				$updates{$c[2]} += 1;
				$seenit{$c[2]} = 1;
				$seenit{$c[3]} = 1;
			} elsif ($seenit{$c[2]}) {
				$updates{$c[2]} += 1;
				$seenit{$c[3]} = 1;
			}
		}
		foreach $line (@tree) {
			@c = split(/\t/, $line);
			if ($updates{$c[2]} == 1) {
				$c[7] += $msgs;
				$line = join("\t", @c);
			}
		}

	}

	@tree = grep(/\S/, @tree); $ctr = 0; $cpflag = 0;
	foreach $line (@tree) {
		@c = split(/\t/, $line);
		if ($c[2] == $new_parent) {
			$c[5] .= "Sublist" if $c[5] !~ m|Sublist|;
			$line = join("\t", @c);
		}
		if ($c[2] == $new_parent || $c[3] == $new_parent) {
			$offset = $ctr; $cpflag = $c[0];
		} elsif ($c[0] > $cpflag && $cpflag > 0) {
			$offset = $ctr;
		} else {
			$cpflag = 0;
		}
		$ctr += 1;
	}
	foreach $line (@insert) {
		@c = split(/\t/, $line);
		$c[1] = $new_topic;
		$c[6] = $newgroup if $c[5] !~ m|^LINK:|;
		if ($pn{$c[2]} == 1) {
			$c[3] = $new_parent;
		}
		$line = join("\t", @c);
		chomp ($c[11]);
		@x = split(/,/, $c[11]);
		foreach $x (@x) {
			$post{$x} = 1;
		}
		$line .= "\n" if $line !~ m|\n$|;
	}
	splice(@tree, $offset+1, 0, @insert);

	open (TREE, ">$admin_dir/msg_index/$new_topic-tree.txt");
	print TREE @tree;
	close (TREE);
	&unlock("move_page", "$admin_dir/msg_index/$new_topic-tree.txt");

	# Remove links from original parent page

	&lock("move_page", "$message_dir/$old_topic/$old_parent.$ext");
	($head1, $color1, $lm1, $ann1, $ann_src1, $sublist1, $about1, $about_src1, $message1, $message_src1, $description_src1) = &get_page($old_topic, $old_parent);
	undef @tomove;
	@temp = split(/\n/, $sublist1);
	foreach $line (@temp) {
		$line =~ m|<!-(\w+): (\d+)-!>|;
		if ($pn{$2} == 1) {
			push (@tomove, $line); $line = "";
		}
	}
	$sublist1 = join("\n", grep(/\S/, @temp));
	$sublist1 = &regenerate_subtopic_list($old_topic, $old_parent, $sublist1);
	&set_page($old_topic, $old_parent, $head1, $color1, $lm1, $ann1, $ann_src1, $sublist1, $about1, $about_src1, $message1, $message_src1, $description_src1);
	&unlock("move_page", "$message_dir/$old_topic/$old_parent.$ext");

	# Put links on new parent page

	&lock("remove_page", "$message_dir/$new_topic/$new_parent.$ext");
	($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($new_topic, $new_parent);
	@head = split(/\n/, $head);
	foreach $line (@head) {
		if ($line =~ m|<!--Param: (\w+)-->|) {
			$p = $1;
			if ($p !~ m|Sublist|) {
				$line = "<!--Param: Sublist$p-->\n";
			}
		}
	}
	$head = join("\n", @head);
	$head =~ m|<!--Owner: (\w+)-->|; $newgroup = $1;
	$lc = 0;
	foreach $line (@head) {
		if ($line =~ m|<!--Level (\d+):|) {
			$lc = $1 if $1 > $lc;
		}
	}
	if ($new_topic != $old_topic) {
		foreach $line (@tomove) {
			if ($line =~ m|<!-Top: (\d+)-!>|) {
				if ($pn{$1} == 1) {
					$line =~ s|/messages/$old_topic/(\d+).$ext|/messages/$new_topic/$1.$ext|g;
				}
			}
		}
	}
	@sublist = split(/\n/, $sublist);
	foreach $x (@tomove) {
		push (@sublist, $x);
	}
	$sublist = join("\n", @sublist);
	$sublist = &regenerate_subtopic_list($new_topic, $new_parent, $sublist);
	&set_page($new_topic, $new_parent, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	&unlock("move_page", "$message_dir/$new_topic/$new_parent.$ext");

	# Update the message counters and last updated times

	$myspot = 0;
	foreach $key (sort {$a <=> $b} keys(%updates)) {
		next if $key == 0;
		$topic_number = $old_topic if $updates{$key} <= 0;
		$topic_number = $new_topic if $updates{$key} == 1;
		next if $key == $old_parent;
		next if $key == $new_parent;
		&lock("move_page", "$message_dir/$topic_number/$key.$ext");
		($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number, $key);
		$sublist = &regenerate_subtopic_list($topic_number, $key, $sublist);
		&set_page($topic_number, $key, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
		&unlock("move_page", "$message_dir/$topic_number/$key.$ext");
	}

	&change_board_colors("*");

	# Change the log/search files to indicate move (if necessary)

	if ($old_topic != $new_topic) {
		undef @new; $bc_old = 0; $bc_new = 0;
		&lock("move_page", "$admin_dir/msg_index/$old_topic-log.txt", "$admin_dir/msg_index/$new_topic-log.txt");
		open (SRC, "$admin_dir/msg_index/$old_topic-log.txt");
		open (NEW, ">$admin_dir/msg_index/$old_topic-log.txt.NEW");
		while (<SRC>) {
			@c = split(/;/);
			$n = $c[0];
			if ($post{$n} == 1) {
				$c[3] =~ m|(\d+)/(\d+)|;
				$c[3] = join("/", $new_topic, $2);
				$_ = join(";", @c);
				push (@new, $_);
			} else {
				print NEW $_;
				$bc_old += length($_);
			}
		}
		close (NEW);
		close (SRC);
		open (SRC, "$admin_dir/msg_index/$new_topic-log.txt");
		open (NEW, ">$admin_dir/msg_index/$new_topic-log.txt.NEW");
		@x = sort {$a <=> $b} @new;
		while (<SRC>) {
			$line = $_;
			@c = split(/;/);
			$u = (split(/;/, $x[0]))[0];
			while (($u < $c[0] || $c[0] == 0) && $u != 0) {
				$m = shift(@x);
				print NEW $m;
				$bc_new += length($m);
				$u = (split(/;/, $x[0]))[0];
			}
			print NEW $line;
			$bc_new += length($line);
		}
		foreach $line (@x) {
			print NEW $line;
			$bc_new += length($line);
		}
		close (NEW);
		close (SRC);
		&rename_file("$admin_dir/msg_index/$new_topic-log.txt", $bc_new);
		&rename_file("$admin_dir/msg_index/$old_topic-log.txt", $bc_old);
		&unlock("move_page", "$admin_dir/msg_index/$old_topic-log.txt", "$admin_dir/msg_index/$new_topic-log.txt");
		undef @new; $bc_old = 0; $bc_new = 0;
		&lock("move_page", "$admin_dir/msg_index/$old_topic-search.txt", "$admin_dir/msg_index/$new_topic-search.txt");
		open (SRC, "$admin_dir/msg_index/$old_topic-search.txt");
		open (NEW, ">$admin_dir/msg_index/$old_topic-search.txt.NEW");
		while (<SRC>) {
			@c = split(/\s/, $_, 4);
			$n = $c[0];
			if ($post{$n} == 1) {
				$c[1] = $new_topic;
				$_ = join(" ", @c);
				push (@new, $_);
			} else {
				print NEW $_;
				$bc_old += length($_);
			}
		}
		close (NEW);
		close (SRC);
		open (SRC, "$admin_dir/msg_index/$new_topic-search.txt");
		open (NEW, ">$admin_dir/msg_index/$new_topic-search.txt.NEW");
		@x = sort {$a <=> $b} @new;
		while (<SRC>) {
			$line = $_;
			@c = split(/\s/, $_, 4);
			$u = (split(/\s/, $x[0]))[0];
			while (($u < $c[0] || $c[0] == 0) && $u != 0) {
				$m = shift(@x);
				print NEW $m;
				$bc_new += length($m);
				$u = (split(/\s/, $x[0]))[0];
			}
			print NEW $line;
			$bc_new += length($line);
		}
		foreach $line (@x) {
			print NEW $line;
			$bc_new += length($line);
		}
		close (NEW);
		close (SRC);
		&rename_file("$admin_dir/msg_index/$new_topic-search.txt", $bc_new);
		&rename_file("$admin_dir/msg_index/$old_topic-search.txt", $bc_old);
		&unlock("move_page", "$admin_dir/msg_index/$old_topic-search.txt", "$admin_dir/msg_index/$new_topic-search.txt");
	}

	# Set up for page regeneration

	&show_gauge('Moving Subtopics/Links', 'Estimating Time', 0, "$cgiurl1?username=$FORM{'username'}&action=move_sub_action&new_parent=$new_parent&new_topic=$new_topic&old_topic=$old_topic&old_parent=$old_parent&pages=$page_number&cpos=1&quick=$FORM{'quick'}");

}

#---SEPARATOR---#
#REQ:regenerate_subtopic_list
#REQ:regenerate_message

sub change_layout {
	my ($topic_number,$page_number, $param) = @_;
	&lock("change_layout", "$message_dir/$topic_number/$page_number.$ext");
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number, $page_number);
	my (@head, $line);
	@head = split(/\n/, $head);
	foreach $line (@head) {
		if ($line =~ m|<!--Param:|) {
			$line = "<!--Param: $param-->";
		}
	}
	$head = join("\n", grep(/\S/, @head));
	$sublist = &regenerate_subtopic_list($topic_number, $page_number, $sublist);
	$message = &regenerate_message($topic_number, $page_number, $message);
	&set_page($topic_number, $page_number, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	&unlock("change_layout", "$message_dir/$topic_number/$page_number.$ext");
	&lock("change_layout", "$admin_dir/msg_index/$topic_number-tree.txt");
	open (FILE, "$admin_dir/msg_index/$topic_number-tree.txt"); @file = <FILE>; close (FILE);
	foreach $line (@file) {
		@c = split(/\t/, $line);
		if ($c[1] == $topic_number && $c[2] == $page_number) {
			$c[5] = $param;
			$line = join("\t", @c);
			last;
		}
	}
	$pwline = \@file;
	&safe_write("$admin_dir/msg_index/$topic_number-tree.txt", $pwline);
	&unlock("change_layout", "$admin_dir/msg_index/$topic_number-tree.txt");
	return 1;
}

#---SEPARATOR---#

sub change_announce_message {
	my ($file, $source, $formatted) = @_;
	my (@file, $messageflag, $line, $source_esc, $topic, $page);
	if ($file =~ m|/(\d+)/(\d+)\.$ext|) {
		($topic, $page) = ($1, $2);
	} else {
		&error_message("Change Announcement Error", "Could not open requested file", 0, 1);
	}
	&lock("change_announce_message", "$message_dir/$topic/$page.$ext");
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
	&set_page($topic, $page, $head, $color, $lm, $formatted, &escape($source), $sublist, $about, $about_src, $message, $message_src, $description_src);
	&unlock("change_announce_message", "$message_dir/$topic/$page.$ext");
}

#---SEPARATOR---#

sub new_file {
	my ($topic_number, $page_number, $parent, $group, $pagetitle, $param, $forcetime, $params, $skip_lock) = @_;
	my ($head, $color, $lm, $sublist, $about, $about_src, $message, $message_src);
	my (@head, $line, $maxkey);
	$param = "Sublist" if $param eq "";
	if ($parent == 0) {
		my ($bg, $tx, $li, $vl, $al, $fa, $si, $im) = &extract_colorsonly;
		$color = join("\t", $bg, $tx, $li, $vl, $al, $im, $si, $fa);
		$head = "<!--Topic: $page_number/$pagetitle-->\n";
		$head .= "<!--Me: $page_number/$pagetitle-->\n";
		$head .= "<!--Owner: $group-->\n";
		$head .= "<!--Properties: $params-->\n";
		undef %DP;
	} else {
		if (-e "$message_dir/$topic_number/$page_number.$ext" || -e "$secdir/$topic_number/$page_number.$ext") {
			&log_error("src-board-subs-2", "new_file", "Warning! New child page ($topic_number/$page_number) already exists.");
			if ($GLOBAL_OPTIONS{'diskquota_disable'} == 1 || $GLOBAL_OPTIONS{'diskquota_disable'} eq "") {
				open (BACKUPS, ">$admin_dir/backups/QUOTA.txt");
				print BACKUPS time, "\n";
				print BACKUPS &escape("Create new destination file $topic_number/$page_number.$ext; file already exists!  The data.txt file is probably corrupt."), "\n";
				close (BACKUPS);
				&ex('mail_administrator_quota', 1);
			}
			&error_message("Create new file error", "Could not create new file, as file already exists in this topic.  The board administrator should check the data.txt file for problems.", 0, 1);
		}
		&lock("new_file", "$message_dir/$topic_number/$parent.$ext") if $skip_lock != 12345;
		($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number, $parent);
		@head = split(/\n/, $head);
		foreach $line (@head) {
			if ($line =~ m|<!--Level (\d+)|) {
				$maxkey = $1 if $maxkey < $1;
			} elsif ($line !~ m|<!--Topic|) {
				$line = "";
			}
		}
		&unlock("new_file", "$message_dir/$topic_number/$parent.$ext") if $skip_lock != 12345;;
		$head = join("\n", grep(/\S/, @head));
		$maxkey += 1;
		$head .= "\n";
		$head .= "<!--Level $maxkey: $page_number/$pagetitle-->\n";
		$head .= "<!--Me: $page_number/$pagetitle-->\n";
		$head .= "<!--Owner: $group-->\n";
		$head .= "<!--Parent: $parent-->\n";
		$head .= "<!--Properties: $params-->\n";
		undef %DP;
	}
	$head .= "<!--Param: $param-->\n";
	my ($timestr) = &get_date_time('long', $forcetime);
	$timestr =~ s/\W//g;
	&set_page($topic_number, $page_number, $head, $color, $timestr, "", "", "", "", "", "", "");
}

#---SEPARATOR---#
#REQ:regenerate_subtopic_list
#REQ:format_subtopics
#REQ:regenerate_message
#REQ:show_gauge
#REQ:page_mgr_2
#REQ:inverse_webtags

sub page_regeneration {
	my ($new_topic, $new_parent, $old_topic, $old_parent, $page_number, $counter) = @_;
	my (@c, @pn, %pn, %seen, @files, @files2, $ctr, $flag, $ctr_begin, $ctr_end);
	my ($line, @common_head, %lf, $lc, @head, $x, $lctr);
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	@pn = split(/,/, $page_number);
	undef %pn; undef %seen;
	foreach $line (@pn) {
		$pn{$line} = 1;
	}
	undef @files; undef @files2;
	$ctr = 0; $flag = 0;
	$ctr_begin = (1+(20*$counter));
	$ctr_end = (20*($counter+1));
	&lock("page_regeneration", "$admin_dir/msg_index/$old_topic-tree.txt");
	open (TREE, "$admin_dir/msg_index/$old_topic-tree.txt");
	@TREE_STRUCTURE_FILE = <TREE>;
	close (TREE);
	&unlock("page_regeneration", "$admin_dir/msg_index/$old_topic-tree.txt");
	foreach $line (@TREE_STRUCTURE_FILE) {
		@c = split(/\t/, $line);
		if ($pn{$c[2]} == 1) {
			push (@files, $line);
			$seen{$c[2]} = 1;
		} elsif ($seen{$c[3]} == 1) {
			push (@files, $line);
			$seen{$c[2]} = 1;
		}
	}
	if ($old_topic != $new_topic) {
		&lock("page_regeneration", "$admin_dir/msg_index/$new_topic-tree.txt");
		open (TREE, "$admin_dir/msg_index/$new_topic-tree.txt");
		@TREE_STRUCTURE_FILE = <TREE>;
		close (TREE);
		&unlock("page_regeneration", "$admin_dir/msg_index/$new_topic-tree.txt");
		foreach $line (@TREE_STRUCTURE_FILE) {
			@c = split(/\t/, $line);
			if ($pn{$c[2]} == 1) {
				push (@files, $line);
				$seen{$c[2]} = 1;
			} elsif ($seen{$c[3]} == 1) {
				push (@files, $line);
				$seen{$c[2]} = 1;
			}
		}
	}
	($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($new_topic, $new_parent);
	$new_parent_head = $head;
	$lc += 1;
	$ctr = 0;
	foreach $file (@files) {
		$ctr += 1;
		if ($ctr >= $ctr_begin && $ctr <= $ctr_end) {
			@c = split(/\t/, $file);
			push (@filelist, "$message_dir/$c[1]/$c[2].$ext");
		}
	}
	if ($new_topic != $old_topic) {
		open (ATTACH, "$admin_dir/msg_index/attach-mgr.txt");
		@attach = <ATTACH>;
		close (ATTACH);
	}
	&lock("page_regeneration", @filelist); $ctr = 0;
	foreach $file (@files) {
		$ctr += 1;
		if ($ctr >= $ctr_begin && $ctr <= $ctr_end) {
			@c = split(/\t/, $file);
			next if $c[5] =~ m|^LINK:|;
			if ($old_topic == $new_topic) {
				($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($c[1], $c[2]);
			} else {
				$fsize = -1;
				$dir = "$message_dir/$new_topic" if -e "$message_dir/$new_topic";
				$dir = "$secdir/$new_topic" if -e "$secdir/$new_topic";
				if (-e "$dir/$c[2].$ext") {
					&log_error("src-board-subs-2", "page_regeneration", "WARNING: In page move, destination file $new_topic/$c[2].$ext already exists!");
I:					for ($i = 1; $i <= 10000; $i++) {
						last I if !-e "$dir/$c[2].$i.$ext";
					}
					&log_error("src-board-subs-2", "page_regeneration", "Original content saved as $c[2].$i.$ext");
					&rename_file("$dir/$c[2].$ext", $fsize, "$dir/$c[2].$i.$ext");
				}
				&rename_file("$admin_dir/msg_index/$old_topic/$c[2]-src.txt", $fsize, "$admin_dir/msg_index/$new_topic/$c[2]-src.txt") if $GLOBAL_OPTIONS{'nosource'} == 0;
				($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($old_topic, $c[2]);
				$sublist = regenerate_subtopic_list($new_topic, $c[2], $sublist, 1, undef);
				chomp ($c[11]);
				@posts = split(/,/, $c[11]);
				undef %px;
				foreach $p (@posts) {
					$px{$p} = 1;
				}
				undef @fm;
				undef %attmoved;
				foreach $line (@attach) {
					($post, $file) = split(/\s+/, $line);
					if ($px{$post}) {
						&copier($old_topic, $new_topic, $file);
					}
				}
				@msg = split(/\n/, $message);
				$postnum = 0;
				foreach $line (@msg) {
					if ($line =~ m|<!-Post: (\d+)-!>|) {
						$postnum = $1;
					}
					if ($line =~ m|<!-Text-!>(.*)<!-/Text-!>|) {
						$text = $1;
						while ($text =~ m|<IMG SRC="$message_url/(\d+)/(\d+)\.gif" |g) {
							$file = "$2.gif";
							next if $attmoved{$file} == 1;
							next if -e "$dir/$file";
							&copier($old_topic, $new_topic, $file, $postnum);
							&attach_error($old_topic, $new_topic, $file);
						}
						while ($text =~ m|<IMG SRC="$message_url/(\d+)/(\d+)\.jpg" |g) {
							$file = "$2.jpg";
							next if $attmoved{$file} == 1;
							next if -e "$dir/$file";
							&copier($old_topic, $new_topic, $file);
							&attach_error($old_topic, $new_topic, $file, $postnum);
						}
						while ($text =~ m|<!-Attachment: (.*?)-!>|g) {
							$file = $1;
							next if $attmoved{$file} == 1;
							next if -e "$dir/$file";
							&copier($old_topic, $new_topic, $file);
							&attach_error($old_topic, $new_topic, $file, $postnum);
						}
					}
				}
				foreach $file (@fm) {
					foreach $line (@msg) {
						$line =~ s%$message_url/(\d+)/$file%$message_url/$new_topic/$file%g;
					}
				}
				$message = &regenerate_message($new_topic, $c[2], join("\n", @msg), 1);
			}
			$head = &re_head($old_topic, $old_parent, $new_topic, $new_parent, $file, $head, $new_parent_head);
			&set_page($c[1], $c[2], $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
			if ($old_topic != $new_topic) {
				unlink ("$message_dir/$old_topic/$c[2].$ext");
				unlink ("$secdir/$old_topic/$c[2].$ext");
			}
		}
	}
	&unlock("page_regeneration", @filelist);
	$counter += 2;
	if (scalar(@files) <= $ctr_end) {
		&page_mgr_2("//$old_topic/$old_parent.$ext",$FORM{"username"}) if $FORM{'quick'} == 0;
		&seturl("$message_url/$old_topic/$old_parent.$ext") if -e "$message_dir/$old_topic";
		&seturl("$script_url/board-auth.$cgi_extension?file=/$old_topic/$old_parent.$ext") if -e "$secdir/$old_topic";
	} else {
		&show_gauge('Moving Subtopics/Links', 'Working', ($ctr_end/scalar(@files)), "$cgiurl1?username=$FORM{'username'}&action=move_sub_action&new_parent=$new_parent&new_topic=$new_topic&old_topic=$old_topic&old_parent=$old_parent&pages=$page_number&cpos=$counter&HTTP_REFERER=$FORM{'HTTP_REFERER'}&quick=$FORM{'quick'}");
	}
}

sub attach_error {
	my ($old_topic, $new_topic, $file, $postnum) = @_;
	&log_error("src-board-subs-2", "page_regeneration(attach_error)", "File [$file] moved from $old_topic to $new_topic");
	open (ATTACH, ">> $admin_dir/msg_index/attach-mgr.txt");
	print ATTACH "$postnum $file\n";
	close (ATTACH);
}

sub re_head {
	my ($old_topic, $old_parent, $new_topic, $new_parent, $file, $head, $new_parent_head) = @_;
	my @myhead = ();
	my @myoldhead = split(/\n/, $head);
	my @newhead = split(/\n/, $new_parent_head);
	undef my $oldparam;
	my $oplevel = 0;
	foreach my $line (@myoldhead) {
		if ($line =~ m|^<!--Parent: (\d+)-->|) {
			$oldparam->{parent} = $1;
		} elsif ($line =~ m|^<!--Level (\d+): (\d+)/(.*)-->|) {
			$oldparam->{level}->{$1}->{number} = $2;
			$oldparam->{level}->{$1}->{name} = $3;
			$oplevel = $1 if $2 == $old_parent;
		} elsif ($line =~ m|^<!--Topic:| || $line =~ m|^<!--Owner:|) {
			##
		} else {
			push (@myhead, $line);
		}
	}
	undef my $newparam;
	my $lmax = 0;
	foreach my $line (@newhead) {
		if ($line =~ m|^<!--Owner:| || $line =~ m|^<!--Topic|) {
			push (@myhead, $line);
		} elsif ($line =~ m|^<!--Level (\d+):|) {
			$lmax = $1 if $1 > $lmax;
			push (@myhead, $line);
		}
	}
	if ($oldparam->{parent} == $old_parent) {
		push (@myhead, "<!--Parent: $new_parent-->");
	} else {
		push (@myhead, "<!--Parent: $oldparam->{parent}-->");
	}
	for (my $i = 1+$oplevel; $i <= scalar(keys(%{ $oldparam->{level} })); $i++) {
		$lmax += 1;
		push (@myhead, "<!--Level $lmax: $oldparam->{level}->{$i}->{number}/$oldparam->{level}->{$i}->{name}-->");
	}
	@myhead = sort @myhead;
	return join("\n", @myhead);
}

sub copier {
	my ($old_topic, $new_topic, $file) = @_;
	my $srcdir = ( -e "$message_dir/$old_topic" ? "$message_dir/$old_topic" : "$secdir/$old_topic" );
	my $destdir = ( -e "$message_dir/$new_topic" ? "$message_dir/$new_topic" : "$secdir/$new_topic" );
	if ($srcdir eq $destdir) {
		&log_error("src-board-subs-2", "copier", "Source directory and destination directory are the same ($srcdir: $file)");
		return 0;
	}
	if (open(SRC, "$srcdir/$file")) {
		binmode(SRC);
		if (open(DEST, ">$destdir/$file")) {
			binmode(DEST);
			while (<SRC>) {
				print DEST;
			}
			close (DEST);
		} else {
			log_error("src-board-subs-2", "copier", "Could not write $new_topic/$file: $!");
		}
		close (SRC);
		unlink("$srcdir/$file");
	} else {
		log_error("src-board-subs-2", "copier", "Could not read $old_topic/$file: $!");
	}
	push (@fm, $file);
	$attmoved{$file} = 1;
}

#---SEPARATOR---#
#REQ:get_navbar

sub reorder_chooser {
	my ($topic_number, $me_number, $username) = @_;
	open (TREE, "$admin_dir/msg_index/$topic_number-tree.txt");
	@TREE_STRUCTURE_FILE = <TREE>;
	close (TREE);
	($myline) = grep(/^(\d+)\t$topic_number\t$me_number\t/, @TREE_STRUCTURE_FILE);
	my ($level, $topic, $page, $parent_number, $name, $param, $owner, $posts) = split(/\t/, $myline);
	$me_name = &unescape($name);
	$str = &JavaScript_prepare($me_name);
	&header;
	print "<HTML><HEAD><TITLE>Reorder Subtopics: $str</TITLE>\n";
	print "</HEAD><BODY BGCOLOR=#ffffff TEXT=#000000 LINK=#0000ff VLINK=#0000ff ALINK=#ff0000>\n";
	print $fs, "<FONT SIZE=3><CENTER><B>Reorder Subtopics</B></CENTER></FONT>\n";
	print "<HR><STRONG>\n";
	print "<A HREF=\"$cgiurlm?username=$username&action=mgr_1";
	print "&HTTP_REFERER=$page_referer\" onMouseOver=\"return setStatus('";
	print "Return to topic selection screen')\">Page Manager</A>:\n";
	($result) = &get_navbar($topic_number, $me_number, "page", 0);
	print $result;
	print " Reorder Subtopics</STRONG><HR>\n";
	print "<FORM ACTION=\"$cgiurl1\" METHOD=POST NAME=sf>\n";
	print "<INPUT TYPE=HIDDEN NAME=action VALUE=\"subtopics\">\n";
	print "<INPUT TYPE=HIDDEN NAME=SELECTION VALUE=\"reorderall\">\n";
	print "<INPUT TYPE=HIDDEN NAME=username VALUE=\"$username\">\n";
	print "<INPUT TYPE=HIDDEN NAME=HTTP_REFERER VALUE=\"//$topic_number/$me_number.$ext\">\n";
	print "<TABLE BGCOLOR=#ffffcc BORDER=1><TR><TD>\n";
	print "$fs", "Select a new order for your subtopics using numbers in the boxes below.  You may\n";
	print "use decimals or negative numbers for your convenience.  You may also automatically alphabetize\n";
	print "your subtopics using the buttons at the bottom of the list.</FONT><P>\n";
	print "<TABLE>\n";
	print "<TR><TD>$fs", "<B>Order</B>&nbsp;&nbsp;&nbsp;</FONT></TD><TD><B>$fs", "Subtopic</FONT></B></TD></TR>\n";
	@selected = grep(/^(\d+)\t$topic_number\t(\d+)\t$me_number\t/, @TREE_STRUCTURE_FILE);
	$counter = 0;
	undef @tn;
	foreach $thing (@selected) {
		$counter += 1;
		($level, $topic, $page, $parent, $name, $xparam, $group) = split(/\t/, $thing);
		push (@tn, &unescape($name));
		print "<TR><TD><INPUT TYPE=TEXT NAME=ORDER$page VALUE=$counter SIZE=4></TD><TD>$fs", &unescape($name), "</TD></TR>\n";
	}
	print "</TABLE>\n";
	print "<P>\n";
	@tn = sort(@tn); undef %ps; $ct = 0;
	foreach $line (@tn) {
		$ct += 1;
		$ps{$line} = $ct;
	}
	foreach $thing (@selected) {
		($level, $topic, $page, $parent, $name, $xparam, $group) = split(/\t/, $thing);
		$name = &unescape($name);
		$jsstr .= "document.sf.ORDER$page.value = (-1 * document.sf.ORDER$page.value); ";
		$jsstr2 .= "document.sf.ORDER$page.value = '$ps{$name}'; ";
		$jsstr3 .= "document.sf.ORDER$page.value = '" . (scalar(keys(%ps))-$ps{$name}+1) . "'; ";
	}
	print "<INPUT TYPE=BUTTON VALUE=\"Reverse Order\" onClick=\"javascript:$jsstr\">\n";
	print "<INPUT TYPE=BUTTON VALUE=\"Sort A-Z\" onClick=\"javascript:$jsstr2\">\n";
	print "<INPUT TYPE=BUTTON VALUE=\"Sort Z-A\" onClick=\"javascript:$jsstr3\">\n";
	print "<INPUT TYPE=RESET VALUE=\"Reset Order\">\n";
	print "<INPUT TYPE=SUBMIT VALUE=\"Save Order\">\n";
	print "</TD></TR></TABLE><P></FORM>\n";
	print "</BODY></HTML>\n";
	exit(0);

}


# END - FILE IS CORRECTLY UPLOADED #
