# FILE: src-board-subs-4
# Graphical trees & Main administrative tools
#-------------------------------------------------------------------------------
# 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:printuntil
#REQ:read_profile
#REQ:get_preferences
#REQ:validate_auths

sub build_graphical_tree {
	my ($username, $topics, $present, $iconlink, $textlink, $stopflag, $stop_present, $othertopicstuff, $nsbug, $click, $page_expand, $target_link) = @_;
	undef @aarr; undef @barr; undef @carr; undef @darr;
	if ($topics eq "**") {
		undef @fav;
		open (TOP_FILE, "$message_dir/$board_topics_file");
		@top = <TOP_FILE>;
		close (TOP_FILE);
		undef @topics;
		if ($pro) {
			&get_preferences;
			if ($PREF{'last_favs'}) {
				@fav = split(/,/, $PREF{'favorites'});
			} else {
				$PREF{'favorites'} = "";
			}
		} else {
			undef %PREF;
		}
		foreach $line (@top) {
			if ($line =~ /<!-Top: (\d+)-!>/) {
				$o = $1;
				next if ($PREF{'favorites'} ne "" && !grep(/^$o$/, @fav));
				if (-e "$message_dir/$o") {
					push (@topics, $o);
				} else {
					@auth = &validate_auths($o);
					push (@topics, $o) if grep(/^$o$/, @auth);
				}
			}
		}
		$topics = join(",", @topics);
		&header;
		&printuntil (1, 1, 0, "$L{TVTITLE}");
		print "<CENTER><FONT SIZE=+1><B>$L{TVTITLE}</B></FONT></CENTER><HR>";
		$BOARD_VIEWTREE=1;
	}
	@topics = split(/,/, $topics) if $topics ne "*";
	my (@tree);
	open (TREE, "$admin_dir/tree.txt");
	@TREE_STRUCTURE_FILE = <TREE>;
	close (TREE);
	if ($page_expand ne "") {
		($topic_expand, $page_expand) = ($page_expand =~ m|(\d+)/(\d+)|); $topic_number = $topic_expand;
		if ($topic_expand != 0) {
			($head) = &get_page($topic_expand, $page_expand);
			@head = split(/\n/, $head);
			undef %parents;
			foreach $line (@head) {
				if ($line =~ m|<!--Level (\d+): (\d+)/|) {
					$parents{$2} = 1;
				}
			}
			$parents{$topic_expand} = 1;
			$page_expand_flag = 1;
		}
	}
	if ($present eq "*") {
		$topic_number = $topics[0]; $me_number = $topics[0]; $expl = $topic_number;
		$present = "$topic_number/$me_number";
		$topic_number = 0;
	} else {
		$present =~ m|(\d+)/(\d+)|;
		$topic_number = $1; $me_number = $2;
		$expl = $topic_number;
	}
	$click = $FORM{'click'};
	if ($click =~ m|(\d+)/(\d+)|) {
		$expl = $1; $topic_number = $1;
	} else {
		$expl = $click if $click;
	}
	if ($topic_number > 0) {
		open (TREE, "$admin_dir/msg_index/$topic_number-tree.txt");
		@tree = <TREE>;
		close (TREE);
		$ctr = 0;
		foreach $line (@TREE_STRUCTURE_FILE) {
			@c = split(/\t/, $line);
			if ($c[1] == $topic_number) {
				splice(@TREE_STRUCTURE_FILE, $ctr, 1, @tree);
				last;
			}
			$ctr += 1;
		}
	} else {
		$tmp = $/; undef $/; undef %TT;
		foreach $topic_number (@topics) {
			open (TREE, "$admin_dir/msg_index/$topic_number-tree.txt");
			$TT{$topic_number} = <TREE>;
			close (TREE);
		}
		$/ = $tmp;
		foreach $line (@TREE_STRUCTURE_FILE) {
			@c = split(/\t/, $line);
			$line = $TT{$c[1]} if $TT{$c[1]};
		}
		$tree = join("", @TREE_STRUCTURE_FILE);
		@TREE_STRUCTURE_FILE = split(/\n/, $tree);
		$topic_number = $topics[0];
	}

	foreach $line (@TREE_STRUCTURE_FILE) {
		if ($BOARD_VIEWTREE) {
			@c = split(/\t/, $line);
			if ($GLOBAL_OPTIONS{'skip_archives_tree'} && $c[5] =~ m|Archive|) {
				$line = ""; next;
			}
		}
		if ($line =~ /^0\t(\d+)\t/) {
			$o = $1;
			$lastnum = $o if (grep(/^$o$/, @topics) || $topics eq "*");
			push (@topics, $o) if $topics eq "*";
		}
	}
	$LAST_TOPIC = $lastnum;
	print "<IMG SRC=\"$html_url/icons/tree_h.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>$title";
	print " -- <B>$ph{main}</B>" if $ph{main};
	print "<BR>\n";
	print "<!-- Note:  the icon section is generated by JavaScript so it might not show up in the source. -->\n";
	my ($ie3);
	$ie3 = 0;
	$ie3 = 1  if $ENV{'HTTP_USER_AGENT'} =~ m|MSIE 3\.|;
	$nsbug = 1 if $ie3;
	if (!$nsbug) {
		print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
		print "<!--\n";
		print "function crArray(leng) {\n";
		print "  this.size = leng;\n";
		print "  for(var i = 1; i <= leng; i++) {\n";
		print "    this[i] = 0;\n";
		print "  }\n";
		print "}\n";
	} else {
		print "<!-- You have an old browser; generating this code manually -->\n";
	}
	undef %secured;
	foreach $num (@topics) {
		$secured{$num} = 0;
		if (!-e "$message_dir/$num/$num.$ext" && $textlink eq "<A HREF=\"$message_url/") {
			$set_tls = 1;
			$secured{$num} = 1;
		}
		&extract("//$num/$num.$ext");
		$t = $topic_name;
		$nm = &char_convert($t, 1, 1, "nostrip");
		$nsn = "$num/$num";
		if (scalar(keys(%ph))) {
			$ph{$nsn} += 0;
			$nm .= " -- <B>$ph{$nsn}</B>";
		}
		push (@aarr, "");
		push (@barr, $num);
		push (@carr, "$num/$num.$ext");
		push (@darr, $nm);
		push (@earr, $secured{$num});
		&descend_tree($num, $num, 0, "", $stopflag, $iconlink, $textlink_temp, $stop_present, $page_expand_flag) if (($othertopicstuff ne "" && $expl == $num) || $othertopicstuff eq "");
	}
	if (!$nsbug) {
		$l = scalar(@aarr);
		print "a = new crArray($l);\n";
		print "b = new crArray($l);\n";
		print "c = new crArray($l);\n";
		print "d = new crArray($l);\n";
		print "e = new crArray($l);\n";
		for ($i = 0; $i < $l; $i++) {
			print "a[", $i, "] = '", $aarr[$i], "';\n";
			print "b[", $i, "] = '", $barr[$i], "';\n";
			print "c[", $i, "] = '", $carr[$i], "';\n";
			print "d[", $i, "] = '", $darr[$i], "';\n";
			print "e[", $i, "] = '", $earr[$i], "';\n";
		}
		print "var i0 = '<IMG SRC=\"';\n";
		print "var i1 = '$html_url/icons/tree_';\n";
		print "var i2 = '.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP BORDER=0>';\n";
		print "var il = '$iconlink';\n";
		print "var tl = '$textlink';\n";
		if ($othertopicstuff ne "") {
			print "var ol = '$othertopicstuff';\n";
			print "var ol2 = '>';\n";
			print "var pt = '$expl';\n";
		} else {
			print "var ol = '';\n";
			print "var ol2 = '';\n";
			print "var pt = '';\n";
		}
		print "var tls = '<A HREF=\"$script_url/board-auth.$cgi_extension?file=/';\n" if $set_tls;
		print "var tls = '';\n" if !$set_tls;
		print "var sf = 0;\n" if !$ie3;
		print "var sf = 1;\n" if $ie3;
		print "var k = '';\n";
		print "var lt = '$LAST_TOPIC';\n";
		print "var m = '';\n";
		print "for (var i=0; i < $l; i = i + 1) {\n";
		print "  var tx = ''; var txf = '';\n";
		print "  var p = '';\n";
		print "  var cla = '';\n";
		print "  if (a[i].length == 0) {\n";
		if ($othertopicstuff ne "") {
			print "    if (b[i] != pt) {\n";
			print "      if (b[i] == lt) {\n";
			print "        k = 'c';\n";
			print "      } else {\n";
			print "        k = 'a';\n";
			print "      }\n";
			print "      tx = ol + b[i] + '/' + b[i] + ol2; txf = '</A>';\n";
			print "    } else {\n";
		}
		print "      if (b[i] == lt) {\n";
		print "        k = 'd';\n";
		print "      } else {\n";
		print "        k = 'b';\n";
		print "      }\n";
		print "      tx = ''; txf = '';\n";
		print "    }\n" if $othertopicstuff ne "";
		print "    sf = 1;\n";
		print "    p = '';\n";
		print "    p = p + tx + i0 + i1 + k + i2 + txf;\n";
		print "  } else {\n";
		print "    for (var j = 0; j < a[i].length; j = j + 1) {\n";
		print "      m = a[i].toString();\n";
		print "      k = m.substring(j, 1+j);\n";
		if ($page_expand_flag) {
		print "      if (k == 'a' || k == 'c') {\n";
		print "        tx = ol + c[i] + ol2; txf = '</A>';\n";
		print "        p = p + tx + i0 + i1 + k + i2 + txf;\n";
		print "      } else {\n";
		}
		print "        p = p + i0 + i1 + k + i2 + cla;\n";
		print "      }\n" if $page_expand_flag;
		print "    }\n";
		print "  }\n";
		print "  var linkflag = 0;\n";
		print "  if (c[i].indexOf('\\t') != -1) {\n";
		print "    linkflag = 1;\n";
		print "    p = p + i0 + i1 + 'm' + i2;\n";
		print "    var lastindex = c[i].lastIndexOf('\\t');\n";
		print "    var linkurl = c[i].substring(5, lastindex);\n";
		print "    var target = c[i].substring(lastindex+1, c[i].length);\n";
		print "  } else {\n";
		print "    if (c[i] != '') {\n" if $iconlink ne "";
		print "      p = p + il + c[i] + '\">';\n" if $iconlink ne "";
		print "      p = p + i0 + i1 + 'g' + i2;\n";
		print "      p = p + '</A>';\n" if $iconlink ne "";
		print "    } else {\n" if $iconlink ne "";
		print "      p = p + i0 + i1 + 'k' + i2;\n" if $iconlink ne "";
		print "    }\n" if $iconlink ne "";
		print "  }\n";
		if ($textlink ne "") {
			print "  if (linkflag == 1) {\n";
			print "    p = p + '<A HREF=\"' + linkurl + '\" TARGET=\"' + target + '\">';\n";
			print "  } else {\n";
			print "    if (e[i] == 0) {\n";
			print "      p = p + tl + c[i] + '\">';\n" if $target_link eq "";
			print "      p = p + tl + c[i] + '\" TARGET=$target_link>';\n" if $target_link ne "";
			print "    } else {\n";
			print "      p = p + tls + c[i] + '\">';\n" if $target_link eq "";
			print "      p = p + tls + c[i] + '\" TARGET=$target_link>';\n" if $target_link ne "";
			print "    }\n";
			print "  }\n";
		}
		print "  p = p + d[i];\n";
		print "  p = p + '</A>';\n" if $textlink ne "";
		print "  if (a[i].length == 0 || sf == 1) {\n";
		print "    document.write(p);\n";
		print "    document.write('<BR>\\n');\n";
		print "  }\n";
		print "}\n";
		print "//-->\n";
		print "</SCRIPT>\n";
	} else {
		for ($i = 0; $i < scalar(@aarr); $i++) {
			if (length($aarr[$i]) == 0) {
				if ($barr[$i] == $LAST_TOPIC) {
					$imgname = "c" if $barr[$i] != $expl;
					$imgname = "d" if $barr[$i] == $expl;
				} else {
					$imgname = "a" if $barr[$i] != $expl;
					$imgname = "b" if $barr[$i] == $expl;
				}
				if ($othertopicstuff && $barr[$i] != $expl) {
					print "$othertopicstuff", "$barr[$i]/$barr[i]>";
					print "<IMG SRC=$html_url/icons/tree_$imgname.gif HEIGHT=16 WIDTH=20 ALIGN=TOP BORDER=0>";
					print "</A>";
				} else {
					print "<IMG SRC=$html_url/icons/tree_$imgname.gif HEIGHT=16 WIDTH=20 ALIGN=TOP BORDER=0>";
				}
			} else {
				$x = $aarr[$i]; $x =~ m|(.*)(\w)$|; $bf = $1; $sv = $2;
				while ($bf =~ m|(\w)|g) {
					print "<IMG SRC=$html_url/icons/tree_$1.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>";
				}
				if ($page_expand_flag && ($sv eq "a" || $sv eq "c")) {
					print "$othertopicstuff$carr[$i]>";
					print "<IMG SRC=$html_url/icons/tree_$sv.gif HEIGHT=16 WIDTH=20 ALIGN=TOP BORDER=0>";
					print "</A>";
				} else {
					print "<IMG SRC=$html_url/icons/tree_$sv.gif HEIGHT=16 WIDTH=20 ALIGN=TOP BORDER=0>";
				}
			}
			if ($carr[$i] =~ m|^LINK\t(.*)\t(.*)|) {
				$url = $1; $target = $2;
				print "<IMG SRC=$html_url/icons/tree_m.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>";
				if ($textlink ne "") {
					print "<A HREF=\"$url\" TARGET=\"$target\">$darr[$i]</A><BR>\n";
				} else {
					print "$darr[$i]<BR>\n";
				}
			} else {
				if ($iconlink ne "") {
					if ($carr[$i] ne "") {
						print "$iconlink$carr[$i]\">";
						print "<IMG SRC=$html_url/icons/tree_g.gif HEIGHT=16 WIDTH=20 ALIGN=TOP BORDER=0>";
						print "</A>";
					} else {
						print "<IMG SRC=$html_url/icons/tree_k.gif HEIGHT=16 WIDTH=20 ALIGN=TOP BORDER=0>";
					}
				} else {
					print "<IMG SRC=$html_url/icons/tree_g.gif HEIGHT=16 WIDTH=20 ALIGN=TOP BORDER=0>";
				}
				($topic) = split(/\//, $carr[$i]);
				if ($textlink ne "") {
					print "$textlink$carr[$i]\">$darr[$i]</A><BR>\n" if $secured{$topic} == 0;
					print "<A HREF=\"$script_url/board-auth.$cgi_extension?file=/$carr[$i]\">$darr[$i]</A><BR>\n" if $secured{$topic} == 1;
				} else {
					print "$darr[$i]<BR>\n";
				}
			}
		}
	}
}

sub descend_tree {
	my ($topic_number,$page_number,$level,$show,$stopflag,$iconlink,$textlink,$present,$page_expand_flag) = @_;
	my (@file,$line,$number,$filename,$lastnum,$page,$name,$stopflag1,$iconlink1,$cutnow,@child,@c,$l);
	my (@present) = split(/,/, $present);
	@child = grep(/^(\d+)\t$topic_number\t(\d+)\t$page_number\t/, @TREE_STRUCTURE_FILE);
	if (scalar(@child) > 0) {
		$c = scalar(@child) - 1;
		$l = $child[$c]; $l =~ m|^(\d+)\t(\d+)\t(\d+)\t|;
		$lastnum = $3;
		foreach $line (@child) {
			$line =~ s/\s+$//; @c = split(/\t/, $line);
			$page = $c[2]; $name = &unescape($c[4]); $params=$c[5];
			substr($show, $level, 1) = "1" if $page != $lastnum;
			substr($show, $level, 1) = "0" if $page == $lastnum;
			if ($c[5] =~ m|LINK:(.*)|) {
				$target = $1; $link = &unescape($c[6]);
				&drawicon($level, $show, "*LINK*", $page, $stopflag, $topic_number, $name, $textlink1, $link, $target);
			} else {
				if (grep(/^$page$/, @present) && $stopflag) {
					$global_cutoff = 1;
					$cutnow = 1;
				}
				$iconlink1 = $iconlink;
				$iconlink1 = "" if $global_cutoff != 0;
				$textlink1 = $textlink;
				$textlink1 =~ s/<CLICK>/$topic_number\/$page/g;
				$pef = $page_expand_flag;
				$pef = 0 if $params !~ m|Sublist|;
				&drawicon($level, $show, $iconlink1, $page, $stopflag, $topic_number, $name, $textlink1, "", "", $pef);
				&descend_tree($topic_number, $page, $level+1, $show, $stopflag, $iconlink, $textlink, $present, $pef) if ($parents{$page} == 1 || $page_expand_flag == 0 || $nsbug);
				$global_cutoff = 0 if $cutnow == 1;
				$cutnow = 0 if $cutnow == 1;
				$iconlink = $iconlink1;
			}
		}
	}
}

sub drawicon {
	my ($level, $show, $iconlink, $page, $stopflag, $topic_number, $name, $textlink, $link, $target, $page_expand_flag) = @_;
	my ($astr, $bstr, $name_returned);
	$name_returned = &char_convert($name, 1, 1, "nostrip");
	$astr = "i" if $topic_number != $LAST_TOPIC;
	$astr = "j" if $topic_number == $LAST_TOPIC;
	for ($i = 0; $i < $level; $i++) {
		$img = "j" if substr($show, $i, 1) == 0;
		$img = "i" if substr($show, $i, 1) == 1;
		$astr .= $img;
	}
	if ($page_expand_flag == 0) {
		$astr .= "e" if substr($show, $level, 1) == 1;
		$astr .= "f" if substr($show, $level, 1) == 0;
	} else {
		$astr .= "a" if ($parents{$page} == 0 && substr($show, $level, 1) == 1);
		$astr .= "c" if ($parents{$page} == 0 && substr($show, $level, 1) == 0);
		$astr .= "b" if ($parents{$page} == 1 && substr($show, $level, 1) == 1);
		$astr .= "d" if ($parents{$page} == 1 && substr($show, $level, 1) == 0);
	}
	push (@aarr, $astr);
	push (@barr, $page);
	push (@carr, "$topic_number/$page.$ext") if ($global_cutoff == 0 && $iconlink ne "*LINK*");
	push (@carr, "") if $global_cutoff == 1;
	push (@carr, "LINK\t$link\t$target") if ($iconlink eq "*LINK*" && $global_cutoff == 0);
	$nsn = "$topic_number/$page";
	if (scalar(keys(%ph))) {
		$ph{$nsn} += 0;
		$name_returned .= " -- <B>$ph{$nsn}</B>" if $iconlink ne "*LINK*";
	}
	$name_returned =~ s/\\//g;
	push (@darr, $name_returned);
	push (@earr, $secured{$topic_number});
}

#---SEPARATOR---#
#REQ:userapp_1
#REQ:check_queue
#REQ:cleaning_jobs
#REQ:verify_user_password

sub admin_main_menu {
	my ($user, $crypted_pass_1, $flag, $nocookieflag) = @_;
	&cleaning_jobs;
	$ts = time;
	$USER_mod = $user;
	$host = $ENV{'REMOTE_HOST'};
	$addr = $ENV{'REMOTE_ADDR'};
	$host = "$host ($addr)" if $host ne "";
	$host = "unknown host name ($addr)" if $host eq "";
	$crypted_pass_2 = crypt($crypted_pass_1, "cookie");
	&verify_user_password($user, $FORM{'password'}, "", "", 0, 1, 1);
	$COOKIE_STRING = "";
	$COOKIE_STRING .= "Set-Cookie: admuser$COOKIE_ID=$user; path=/\n" if !$nocookieflag;
	$COOKIE_STRING .= "Set-Cookie: pass$COOKIE_ID=$crypted_pass_2; path=/\n" if !$nocookieflag;
	&header;
	print "<html><head><title>$titlej Administration</title>\n";
	print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
	print "<!--\n";
	print "function setStatus(msg) {\n";
	print "  window.status=msg\n";
	print "  return true\n";
	print "}\n";
	$append = "&username=$user";
	$html_url =~ m|http://([^/]+)|i; $html_host = $1;
	$script_url =~ m|http://([^/]+)|i; $script_host = $1;
	print "function menuload() {\n";
	print "  window.defaultStatus = 'Welcome to $titlej administration'\n";
	if ($] < 5) {
		$perlwarn = 1;
	} else {
		$perlwarn = 0;
	}
	open (FILE, "$admin_dir/upgrade.txt");
	@file = <FILE>;
	close (FILE);
	if (grep(/^Complete: version 3\.10/, @file)) {
		$version_upgrade = 1;
	} else {
		if (open(TOPICS, "$message_dir/$board_topics_file")) {
			@topics = <TOPICS>;
			close (TOPICS);
			if (!grep(/^<!-Top: (\d+)/, @topics)) {
				$version_upgrade = 1;
				open (FILE, ">>$admin_dir/upgrade.txt");
				print FILE "#First-time installation or zero-topic event\n";
				print FILE "Complete: version 3.10\n";
				close (FILE);
			}
		} else {
			&error_message("HTML DIRECTORY ERROR", "Your topics file could not be opened.  Your html_dir in discus.conf is wrong, or you don't have a topics page.");
		}
	}
	if ($html_host eq $script_host) {
		print "  var loctest = '$cgiurlm';\n";
		print "  var loctest2 = 'http://' + parent.frames[1].location.host + parent.frames[1].location.pathname\n";
		print "  var loctest3 = '$cgiurl';\n";
		if ($version_upgrade == 1) {
			print "  var loc = '$cgiurlm?action=mgr_1$append'\n" if ($FORM{'startfrom'} !~ m|^(\d+)/(\d+)$| && $perlwarn == 0);
			print "  var loc = '$cgiurlm?action=perl$append'\n" if ($FORM{'startfrom'} !~ m|^(\d+)/(\d+)$| && $perlwarn == 1);
			print "  var loc = '$cgiurlm?action=page_editor&HTTP_REFERER=//$FORM{'startfrom'}.$ext$append'\n" if ($FORM{'startfrom'} =~ m|^(\d+)/(\d+)$| && $perlwarn == 0);
		} else {
			print "  var loc = '$cgiurlm?action=version$append'\n" if ($version_upgrade == 0 && $FORM{'username'} eq $superuser && $perlwarn == 0);
			print "  var loc = '$html_url/board-admin-instr-01.html'\n" if ($FORM{'username'} ne $superuser && $version_upgrade == 0);
		}
		print "  if (loctest != loctest2 && loctest2 != loctest3) {\n";
		print "    parent.frames[1].location.href = loc\n";
		print "  }\n";
	}
	print "}\n";
	print "//-->\n";
	print "</SCRIPT>\n";
	print "</head>\n";
	print "<body bgcolor=\"ffffff\" text=\"000000\" link=\"0000FF\" vlink=\"0000FF\" ";
	if ($crypted_pass_1 eq "" || $flag == 1) {
		print "onLoad=\"window.defaultStatus = 'You must specify a password';";
		print "top.frames[1].location = '$cgiurlm?action=first_pass$append'\">\n";
		print "<FONT FACE=\"Verdana,Arial\" SIZE=2><B>You must specify a password!</B><P>\n";
		print "You must set your password before continuing.<P>\n";
		print "<LI><A HREF=\"$cgiurlm?action=first_pass$append\" onMouseOver=\"return setStatus('";
		print "Set your password')\" TARGET=\"Admin_Main\">Set your password</A>\n";
		print "<LI><A HREF=\"$html_url/board.html\" TARGET=_parent>Cancel</A>\n";
		print "</UL></BODY></HTML>\n";
		exit(0);
	}
	if ($DO_NOT_WRITE_FILES_FLAG) {
		print "onload=\"window.defaultStatus = 'You must take care of the disk quota problem';";
		print "top.frames[1].location = '$cgiurlm?action=quota_helper$append'\">\n";
		print "<FONT FACE=\"Verdana,Arial\" SIZE=2><B>You must take care of the disk quota problem!</B><P>\n";
		print "A disk quota problem has been detected.  It must be resolved before you continue using this board.<P>\n";
		print "<LI><A HREF=\"$cgiurlm?action=quota_helper$append\" onMouseOver=\"return setStatus('";
		print "Diagnose disk quota problem')\" TARGET=\"Admin_Main\">Diagnose disk quota problem</A>\n";
		print "<LI><A HREF=\"$html_url/board.html\" TARGET=_parent>Cancel</A>\n";
		print "</UL></BODY></HTML>\n";
		exit(0);

	}
	print "onLoad=\"menuload()\">\n";
	print "<FORM NAME=hdata><INPUT TYPE=HIDDEN NAME=FLD1 VALUE=\"\"></FORM>\n";
	print "$fs\n";
	&lock("admin_main_menu", "$admin_dir/adminlog.txt");
	open (ADMINLOG, "$admin_dir/adminlog.txt"); @a = <ADMINLOG>; close (ADMINLOG);
	$flc = 0; undef @flc; $ll = "";
	foreach $line (@a) {
		if ($line =~ m|^$USER_mod;([^;]*);([^;]*);(\d+);(.*)|) {
			($rh, $ra, $dt, $ok) = ($1, $2, $3, $4);
			if ($ok !~ m|ok|) {
				$flc += 1;
				push (@flc, join("", "Failed Login on <B>", (&get_date_time('long', $dt))[0], "</B> from <B>$rh [$ra]</B>")) if $rh ne "";
				push (@flc, join("", "Failed Login on <B>", (&get_date_time('long', $dt))[0], "</B> from <B>$ra</B>")) if $rh eq "";
			} else {
				$ll = "Last Login on <B>" . &get_date_time('short', $dt) . "</B> from <B>$rh</B>";
			}
			$line = "";
		} elsif ($line =~ m|^;|) {
			$line = "";
		}
	}
	@a = grep(/\S/, @a);
	push (@a, "$USER_mod;$host;$addr;$ts;ok\n");
	open (ADMINLOG, ">$admin_dir/adminlog.txt"); print ADMINLOG @a; close (ADMINLOG);
	&unlock("admin_main_menu", "$admin_dir/adminlog.txt");
	if ($flc) {
		print "<FONT COLOR=#ff0000 SIZE=1><B>$flc</B> failed login";
		print "s" if $flc > 1;
		print " since last access (see <A HREF=#failed>bottom</A> for details)</FONT><P>\n";
	}
	print "<IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>\n";
	if (-e "$html_dir/index.html") {
		print "<A HREF=\"$html_url/index.html\" Target=\"_parent\" onMouseOver=\"return setStatus('";
	} else {
		print "<A HREF=\"$html_url/board.html\" Target=\"_parent\" onMouseOver=\"return setStatus('";
	}
	print "Go to the $titlej board main page')\">Go to Board</A><P>\n";
	if ($version_upgrade == 0) {
		if ($FORM{'username'} ne $superuser) {
			print "<B><FONT COLOR=#ff0000>Upgrade Required</FONT></B><P>\n";
			print "The superuser must log in and perform the upgrade procedure\n";
			print "before administration can be accessed.<P>\n";
			print "</BODY></HTML>\n";
			exit(0);
		} else {
			print "<IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP> <B><A HREF=$cgiurlm?username=$superuser&action=version TARGET=Admin_Main>Upgrade</A></B>\n";
			print "<BR><FONT SIZE=1>It is strongly recommended that you run the upgrade procedure\n";
			print "before continuing with administration.</FONT><P>\n";
		}
	}
	print "<IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP> Moderator Functions<BR>\n";
	print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP> ";
	print "<a href=\"$cgiurlm\?action=mgr_1$append\" Target=\"Admin_Main\" onMouseOver=";
	print "\"return setStatus('Edit,  add,  or remove topic pages and messages')\"";
	print ">Page Manager</a><BR>\n";
	print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP> ";
	print "<a href=\"$cgiurlm\?action=access_mgr_1$append\" Target=\"Admin_Main\" onMouseOver=";
	print "\"return setStatus('Configure access/posting parameters of topics')\"";
	print ">Access Manager</a><BR>\n";
	print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP> ";
	print "<a href=\"$cgiurlm\?cmd=user_mgr$append\" Target=\"Admin_Main\" onMouseOver=";
	print "\"return setStatus('Add and remove users to/from your groups')\"";
	print ">User Manager</a><BR>\n";
	if ($pro) {
		&userapp_1($username, "", 2);
	}
	print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP> ";
	print "<a href=\"$script_url/board-profile.$cgi_extension?action=profile_editor_screen&password=adminlogin$append\" Target=\"Admin_Main\" onMouseOver=";
	print "\"return setStatus('Edit your profile or change your password')\"";
	print ">Profile Editor</a><BR>\n";
	if ($pro) {
		@qm = &check_queue($user);
		$q = scalar(@qm);
		$qm = "<B>$q Queued Message" if $q > 0;
		$qm .= "s" if $q > 1;
		$qm .= "</B>" if $q > 0;
		$qm = "Queue Manager" if $q == 0;
		print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP> ";
		print "<a href=\"$cgiurlm\?action=queue_frames$append\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('Approve or refuse queued messages')\"";
		print ">$qm</a><BR>\n";
		print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
		print "<a href=\"$cgiurlm\?cmd=log_analy$append\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('Analyze access logs and posting logs')\"";
		print ">Log Analysis</a><BR>\n";
	}
	print "<IMG SRC=\"$html_url/icons/tree_f.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
	print "<a href=\"$html_url/admin.html\" Target=\"_parent\" onMouseOver=";
	print "\"return setStatus('Return to initial login screen')\"";
	print ">Log In Again</a><P>\n";
	print "<IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP> Instructions<BR>\n";
	print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
	print "<a href=\"$html_url/board-admin-instr-01.html\"  Target=\"Admin_Main\" ";
	print "onMouseOver=\"return setStatus('View the $titlej administration program instructions page')\"";
	print ">Administration</A><BR>\n";
	print "<IMG SRC=\"$html_url/icons/tree_f.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
	print "<a href=\"$html_url/board-formatting.html\"  Target=\"Admin_Main\" ";
	print "onMouseOver=\"return setStatus('View the $titlej formatting instructions page')\"";
	print ">Formatting</A><BR>\n";
	if ($FORM{'username'} eq $superuser) {
		print "<BR>\n";
		print "<IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP> Superuser Functions<BR>\n";
		print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
		print "<a href=\"$cgiurlm\?cmd=board_mgr$append\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('Add/remove/edit topics, change board message and colors')\"";
		print ">Board Manager</a><BR>\n";
		print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
		print "<a href=\"$cgiurlm\?cmd=moderator_mgr$append\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('Add/remove moderator accounts')\"";
		print ">Moderator Manager</a><BR>\n";
		print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
		print "<a href=\"$cgiurlm\?cmd=group_mgr$append\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('Add/remove groups;  assign moderators to groups')\"";
		print ">Group Manager</a><BR>\n";
		print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
		print "<a href=\"$cgiurlm\?cmd=version_mgr$append\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('View Discus version, upgrade, get Discus program information')\"";
		print ">Version Manager</a><BR>\n";
		if ($pro) {
			print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
			print "<a href=\"$cgiurlm\?cmd=archive_mgr$append\" Target=\"Admin_Main\" onMouseOver=";
			print "\"return setStatus('Set board archiving and pruning options')\"";
			print ">Archive Manager</a><BR>\n";
			print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
			print "<a href=\"$cgiurlm\?cmd=backup_mgr$append\" Target=\"Admin_Main\" onMouseOver=";
			print "\"return setStatus('Back up or restore the board')\"";
			print ">Backup Manager</a><BR>\n";
		}
		print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
		print "<a href=\"$cgiurlm\?cmd=data_recovery$append\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('Fix pages that are corrupted or missing')\"";
		print ">Data Recovery</a><BR>\n";
		print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
		print "<a href=\"$cgiurlm\?cmd=options_mgr$append\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('Set miscellaneous board-wide options')\"";
		print ">Options Manager</a><BR>\n";
		print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
		print "<a href=\"$cgiurlm\?cmd=template_mgr$append\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('Edit templates that control the look of your board')\"";
		print ">Template Manager</a><BR>\n";
		print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
		print "<a href=\"$cgiurlm\?cmd=importexport$append\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('Import/Export Discus data')\"";
		print ">Import/Export Utility</a><BR>\n";
		print "<IMG SRC=\"$html_url/icons/tree_e.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
		print "<a href=\"$cgiurlm?action=quota_helper&username=$superuser\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('Get information on your disk quota and test your free space')\"";
		print ">Disk Quota Info.</a><BR>\n";
		print "<IMG SRC=\"$html_url/icons/tree_f.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP><IMG SRC=\"$html_url/icons/tree_g.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>  ";
		print "<a href=\"$script_url/diagnose.$cgi_extension\" Target=\"Admin_Main\" onMouseOver=";
		print "\"return setStatus('Program Self-Diagnostics')\"";
		print ">Program Diagnostics</a><BR>\n";
	}
	if ($GLOBAL_OPTIONS{'admin_login_history'} eq "" || $GLOBAL_OPTIONS{'admin_login_history'} == 1) {
		print "<P><A NAME=failed><P><FONT SIZE=1>$ll<P>\n";
		foreach $fl (@flc) {
			print "<LI>$fl\n";
		}
		print "</FONT>";
	}
	if ($GLOBAL_OPTIONS{'version_notice_no'} eq "" || $GLOBAL_OPTIONS{'version_notice_no'} == 1) {
		print "<P><TABLE BGCOLOR=#00aa22 BORDER=1><TR><TD>$fs<FONT COLOR=#ffffff><P><B>Discus $release_version.";
		print "$free_revision FREEWARE" if !$pro;
		print "$pro_revision PRO" if $pro;
		print "</B><BR>&copy; 2000, <A HREF=http://www.discusware.com TARGET=_blank>DiscusWare, LLC</A>\n";
		print "</FONT></FONT></TD></TR></TABLE>\n";
	} else {
		print "<P><FONT SIZE=1>&copy; 2000, <A HREF=http://www.discusware.com TARGET=_blank>DiscusWare, LLC</A></FONT>\n";
	}
	print "</FONT><P>\n";
	print "</BODY></HTML>\n";
	exit(0);
}

#---SEPARATOR---#
#REQ:cleaning_jobs

sub front_page {
	&header;
	print "<html><head><title>$title Administration</title></head>\n";
	print "<body bgcolor=ffffff text=000000 onLoad=\"document.forms[0].";
	print "username.focus()\">\n";
	print "$fs<FONT SIZE=3><CENTER><B>Administration</B></CENTER></FONT>\n";
	print "<hr>\n";
	print "<FORM Method=POST Action=\"$cgiurlm\">\n";
	print "<TABLE BORDER=1 WIDTH=100% BGCOLOR=#ffffcc><TR><TD>\n";
	print "<TABLE><TR><TD>$fs";
	print "Username:</TD><TD><input type=text name=\"username\" SIZE=10></td></tr>\n";
	print "<tr><td>$fs", "Password:</TD><TD><input type=password ";
	print "name=\"password\" SIZE=10></TD></TR></TABLE>\n";
	print "<P>$fs\n";
	print "<Input Type=hidden name=\"action\" value=\"menu\">\n";
	if ($FORM{'startfrom'} ne "") {
		print "<Input type=hidden name=\"startfrom\" value=\"$FORM{'startfrom'}\">\n";
	}
	print "<Input Type=submit name=submit Value=\"Log In\"></TD></TR></TABLE>\n";
	print "</form>\n";
	print "<hr><br>\n";
	print "<NOSCRIPT>\n<FONT COLOR=#ff0000><H4>Warning!</H4>You do not have JavaScript enabled\n";
	print "on your browser.  The administration program requires JavaScript.";
	print "\n<P>\n</FONT>\n<HR>\n</NOSCRIPT>\n";
	print "<FONT SIZE=1>The administration program will store internal information\n";
	print "in a COOKIE.  For this to work, you must have cookies\n";
	print "enabled on your browser!  Also, be sure to exit the browser\n";
	print "when you are done so that the information in the cookie will\n";
	print "be reset.</FONT>\n";
	print "</body></html>\n";
	&cleaning_jobs;
	exit(0);
}

#---SEPARATOR---#

sub cleaning_jobs {
	my ($ctr);
	$ctr = 0;
	opendir(DIR, "$admin_dir/backups");
	while ($dir = readdir(DIR)) {
		if ($dir =~ m|^(\w+)-(\d+)\.conf$|) {
			($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$admin_dir/backups/$dir");
			if ($mtime < (time - (14*24*60*60))) {
				unlink "$admin_dir/backups/$dir";
			}
		} elsif ($dir =~ m|^(\w+).TMP$|) {
			($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$admin_dir/backups/$dir");
			if ($mtime < (time - (3*60*60))) {
				unlink "$admin_dir/backups/$dir";
			}
		}
	}
	closedir(DIR);
	opendir(DIR, "$admin_dir/msg_index");
	while ($dir = readdir(DIR)) {
		if ($dir =~ m|^(\w+).TMP$|) {
			($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$admin_dir/msg_index/$dir");
			if ($mtime < (time - (3*60*60))) {
				unlink "$admin_dir/msg_index/$dir";
			}
		}
		if ($dir =~ m|^(\d+)-tree\.txt$|) {
			$ctr += 1;
		}
		chmod(oct($perms0666),"$admin_dir/msg_index/$dir") if ($^O ne "MSWin32" && $dir =~ m|\.txt$|);
		next if $dir =~ m|^\.+$|;
		chmod(oct($perms0777),"$admin_dir/msg_index/$dir") if ($^O ne "MSWin32" && -d "$admin_dir/msg_index/$dir");
	}
	closedir(DIR);
	if ($^O ne "MSWin32") {
		opendir(DIR, "$admin_dir");
		while ($dir = readdir(DIR)) {
			chmod(oct($perms0666),"$admin_dir/$dir") if $dir =~ m|\.txt$|;
			next if $dir =~ m|^\.+$|;
			chmod(oct($perms0777),"$admin_dir/$dir") if (-d "$admin_dir/$dir" && $dir ne "source");
		}
		closedir(DIR);
		opendir(DIR, "$message_dir");
		while ($dir = readdir(DIR)) {
			next if $dir =~ m|^\.+$|;
			chmod(oct($perms0777),"$message_dir/$dir") if -d "$message_dir/$dir";
		}
		closedir(DIR);
		opendir(DIR, "$secdir");
		while ($dir = readdir(DIR)) {
			next if $dir =~ m|^\.+$|;
			chmod(oct($perms0777),"$secdir/$dir") if -d "$secdir/$dir";
		}
		closedir(DIR);
		chmod(oct($perms0666),"$message_dir/$board_topics_file")
	}
	if ($ctr == 0) {
		&lock("$admin_dir/tree.txt");
		my (@tree, %opened, %key);
		open (TREE, "$admin_dir/tree.txt");
		@tree = <TREE>;
		close (TREE);
		open (TREE, ">$admin_dir/tree.txt");
		foreach $line (@tree) {
			@c = split(/\t/, $line);
			if ($opened{$c[1]} == 0) {
				$x = "FILE$c[1]"; $opened{$c[1]} = 1;
				open ($x, ">$admin_dir/msg_index/$c[1]-tree.txt");
				print $x $line;
			} else {
				$x = "FILE$c[1]";
				print $x $line;
			}
			print TREE $line if $c[1] == $c[2];
			$line = "" if $c[1] != $c[2];
		}
		foreach $key (keys(%opened)) {
			$x = "FILE$key";
			close ($x);
			chmod(oct($perms0666),"$admin_dir/msg_index/$x-tree.txt") if $^O ne "MSWin32";
		}
		close (TREE);
		chmod(oct($perms0666),"$admin_dir/tree.txt") if $^O ne "MSWin32";
		&unlock("$admin_dir/tree.txt");
	}
}

#---SEPARATOR---#

sub admin_from_here {
	if (open(HTML, "$html_dir/admin.html")) {
		@html = <HTML>;
		close (HTML);
	} else {
		&seturl("$html_url/admin.html");
		exit(0);
	}
	$FORM{'username'} = $COOKIE{"user$COOKIE_ID"};
	$FORM{'username'} = $COOKIE{"admuser$COOKIE_ID"} if $COOKIE{"admuser$COOKIE_ID"};
	$code = &check_passwd(0, 0, 592);
	if ($code == -1) {
		foreach $line (@html) {
			if ($line =~ m|(")([^"]+)(board-admin-menuonly.$cgi_extension)|) {
				$line = join("", $`, $1, $script_url, "/", $3, "?startfrom=$FORM{'startfrom'}", $');
			}
		}
	} else {
		foreach $line (@html) {
			if ($line =~ m|(")([^"]+)(board-admin-menuonly.$cgi_extension)|) {
				$line = join("", $`, $1, $script_url, "/", $3, "?startfrom=$FORM{'startfrom'}&username=$FORM{'username'}&action=menu&autologin=1", $');
			} elsif ($line =~ m|(")([^"]+)warning\.html|) {
				$line = join("", $`, $1, $script_url, "/", "board-admin-menuonly.$cgi_extension?action=page_editor&HTTP_REFERER=$FORM{'startfrom'}.$ext&username=$FORM{'username'}", $');
			}
		}
	}
	&header;
	print @html;
	exit(0);
}

#---SEPARATOR---#

sub get_navbar {
	my ($topic_num, $page_num, $context, $original) = @_;
	my (@c, $result, $hops, $intermed, $ll, $result);
	open (TREE, "$admin_dir/msg_index/$topic_num-tree.txt"); @TREE_STRUCTURE_FILE = <TREE>; close (TREE);
	if (($ll) = grep(/^(\d+)\t$topic_num\t$page_num\t/, @TREE_STRUCTURE_FILE)) {
		@c = split(/\t/, $ll);
		$hops = 0;
		if ($c[3] != 0 && $c[3] != $page_num) {
			$hops += 1;
			if ($hops >= 100) {
				&error_message("Navigation Bar Error", "Maximum hops exceeded.  Reindex your board using the Board Manager.");
			}
			$result = &get_navbar($topic_num, $c[3], $context, 0);
		}
		$t = &unescape($c[4]); $tj = &JavaScript_prepare($t);
		$result .= "<A HREF=\"$cgiurlm?username=$FORM{'username'}&HTTP_REFERER=//$topic_num/$page_num.$ext&action=page_editor\" onMouseOver=\"window.status = 'Return to editing: $tj'; return true\">" if (!$original && $context eq "page");
		if ($context eq "post") {
			$result .= "<A HREF=\"$message_url/$topic_num/$page_num.$ext" if -e "$message_dir/$topic_num";
			$result .= "?$c[8]" if (!$noqm && -e "$message_dir/$topic_num");
			$result .= "<A HREF=\"$script_url/board-auth.$cgi_extension?file=/$topic_num/$page_num.$ext&lm=$c[8]" if !-e "$message_dir/$topic_num";
			$result .= "\" onMouseOver=\"window.status = '$L{NBRETURN} $tj'; return true\">";
		}
		$result .= $t;
		$result .= "</A>: " if (!$original && ($context eq "page" || $context eq "post"));
		$result .= ": " if $context eq "nmnav";
		return $result;
	} else {
		return "<FONT COLOR=#ff0000>* $topic_num/$page_num not in tree file! *</FONT>" if $context ne "page";
        my ($head) = &get_page($topic_num, $page_num);
        my ($number, $name, @array, %level, $str, @head, $topic_name, $line, $key);
        @head = split(/\n/, $head);
        $result = "";
		foreach $line (@head) {
			if ($line =~ m|<!--Level (\d+): (\d+)/(.*)-->|) {
				$level{$1} = "$2:$3";
			} elsif ($line =~ m|<!--Topic: (\d+)/(.*)-->|) {
				@array = ("$1:$2"); $topic_name = $2;
			}
        }
		foreach $key (sort by_number keys(%level)) {
			push (@array, $level{$key});
		}
		if ($topic_num == $page_num) {
			return $topic_name;
		}
		foreach $line (@array) {
			($number,$name) = split(/:/, $line, 2);
			$result .= "<A HREF=\"$cgiurlm?username=$username&action=page_editor";
			$result .=  "&HTTP_REFERER=//$topic_number/$number.$ext\">";
			$result .=  "$name</A>:\n";
		}
		$result .=  "$closing_text\n\n";
		if ($original) {
			$result =~ s/\s+$//;
			@result = split(/\n/, $result);
			if ($result[scalar(@result)-1] =~ m|<A HREF=.*>(.*)</A>|) {
				$result[scalar(@result)-1] = $1;
			}
			$result = join("\n", @result);
		}
		return $result;
	}
}



# END - FILE IS CORRECTLY UPLOADED #
