# FILE: src-board-subs-19
# Disk quota manager 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 quota_admin {
	&header;
	undef @hugefiles;
	print "<HTML><HEAD><TITLE>Disk Quota Problem Administrator</TITLE></HEAD>\n";
	print "<BODY BGCOLOR=#ffffff TEXT=#000000>$fs<CENTER><FONT SIZE=3><B>Disk Quota Problem Administrator</B></FONT></CENTER>\n";
	print "<HR>\n";
	print "<H4>Detected Disk Quota Events</H4>\n";
	if ($DO_NOT_WRITE_FILES_FLAG) {
		print "Because you are seeing this message, at the date and time indicated below, a disk quota\n";
		print "problem was detected on your discussion board.  The board was automatically disabled at this point.\n";
		print "If normal use of the board had been permitted to continue, serious corruption of the Discus board could\n";
		print "have taken place.<P>\n";
		if (open (ADMIN, "$admin_dir/backups/QUOTA.txt")) {
			@admin = <ADMIN>;
			close (ADMIN);
			$date = $admin[0]; chomp $date;
			print "<B>Error Date:</B> ", &get_date_time("long", $date), "<BR>\n";
			print "<B>Error Occurred:</B> ", &unescape($admin[1]), "<BR>\n";
		} else {
			print "<B>No quota events detected!</B><P>\n";
		}
	} else {
		print "No disk quota events have been detected to this point.  We encourage you to use the test utility below to\n";
		print "check how much space remains within your disk quota to prevent any problems from happening before they can ever\n";
		print "occur.<P>\n";
	}
	print "<HR>\n";
	print "<H4>Actions and Diagnostics</H4>\n";
	print "<TABLE BGCOLOR=#ffffcc BORDER=1><TR><TD>$fs\n";
	if ($DO_NOT_WRITE_FILES_FLAG) {
		print "<FORM ACTION=$cgiurl METHOD=POST>\n";
		print "Your board is disabled because of a quota event.  To re-enable your board (after confirming that\n";
		print "you have enough space left on your disk), click the button below.  <FONT COLOR=#ff0000><B>It is\n";
		print "important that you understand the problem you encountered and that you resolve it for the future\n";
		print "before you re-enable your board!</B></FONT><P>\n";
		print "<INPUT TYPE=HIDDEN NAME=username VALUE=$superuser>\n";
		print "<INPUT TYPE=HIDDEN NAME=action VALUE=quota_check_off>\n";
		print "<INPUT TYPE=SUBMIT VALUE=\"Enable Board\">\n";
		print "</FORM><P>\n";
	}
	print "<FORM ACTION=$cgiurl METHOD=POST>\n";
	print "This option allows you to test your free disk space.  This is done by having Discus attempt to create\n";
	print "a file of the size that you specify below.  Note that if you do this, it might automatically notify your\n";
	print "web host that you are running out of space even though you are well below your quota.  This should not\n";
	print "cause any damage on your site, but should be used at your own risk.<P>\n";
	print "<TABLE><TR><TD>$fs", "File size:</FONT> <SELECT NAME=size>\n";
	print "<OPTION VALUE=1 SELECTED>1 MB\n";
	print "<OPTION VALUE=5>5 MB\n";
	print "<OPTION VALUE=10>10 MB\n";
	print "<OPTION VALUE=15>15 MB\n";
	print "<OPTION VALUE=20>20 MB\n";
	print "<OPTION VALUE=25>25 MB\n";
	print "</SELECT><INPUT TYPE=HIDDEN NAME=username VALUE=$superuser>\n";
	print "<INPUT TYPE=HIDDEN NAME=action VALUE=quota_check_test>\n";
	print "<INPUT TYPE=SUBMIT VALUE=\"Test Free Space\">\n";
	print "</TD></TR></TABLE></FORM><P>\n";
	print "Discus has automatically performed a test to make sure that it can remove files in your 'locks' and your\n";
	print "'backups' directories.";
	print "<UL>\n";
	print "<LI>'locks' directory: ";
	$fntest = join("", time, ".TMP");
	if (open(FILETEMP, ">$admin_dir/locks/$fntest")) {
		close (FILETEMP);
		if (unlink("$admin_dir/locks/$fntest")) {
			print "<FONT COLOR=#00aa00><B>PASSED!</B></FONT>\n";
		} else {
			print "<FONT COLOR=#ff0000><B>FAILED!</B></FONT> Removal failed [$!]\n";
		}
	} else {
		print "<FONT COLOR=#ff0000><B>FAILED!</B></FONT> Creation failed [$!]\n";
	}
	print "<LI>'backups' directory: ";
	$fntest = join("", time, ".TMP");
	if (open(FILETEMP, ">$admin_dir/backups/$fntest")) {
		close (FILETEMP);
		if (unlink("$admin_dir/backups/$fntest")) {
			print "<FONT COLOR=#00aa00><B>PASSED!</B></FONT>\n";
		} else {
			print "<FONT COLOR=#ff0000><B>FAILED!</B></FONT> Removal failed [$!]\n";
		}
	} else {
		print "<FONT COLOR=#ff0000><B>FAILED!</B></FONT> Creation failed [$!]\n";
	}
	print "</UL>\n";
	print "</TD></TR></TABLE>\n";
	print "<P><HR><P><H4>Scan for Zero-Length, Large, and Unnecessary Files</H4>\n";
	print "<TABLE BGCOLOR=#ffffcc BORDER=1><TR><TD>$fs\n";
	print "<B>Zero-Length File Scan</B><P>\n";
	$count = 0; $ccount = 0;
	opendir(DIR, "$admin_dir");
	while ($dir = readdir(DIR)) {
		$count += 1;
		if ($dir =~ m|\.txt$|) {
			next if $dir eq "log.txt";
			$s = -s "$admin_dir/$dir";
			if ($s == 0) {
				print "<LI>$admin_dir/$dir\n"; $ccount += 1;
			}
		}
		if (-s "$admin_dir/$dir" > 1000000) {
			$s = -s "$admin_dir/$dir";
			push (@hugefiles, "$admin_dir/$dir [$s]");
		}
	}
	closedir(DIR);
	if (-s "$message_dir/$board_topics_file" == 0) {
		print "<LI>$message_dir/$board_topics_file\n";
		$ccount += 1;
	}
	$count += 1;
	open(TOPICS, "$message_dir/$board_topics_file");
	@topics = <TOPICS>;
	close (TOPICS);
	foreach $line (@topics) {
		if ($line =~ m|<!-Top: (\d+)-!>|) {
			if (-e "$message_dir/$1") {
				$dir = "$message_dir/$1";
			} elsif (-e "$secdir/$1") {
				$dir = "$secdir/$1";
			} else {
				$dir = "";
			}
			if ($dir ne "") {
				opendir(DIR, "$dir");
				while ($file = readdir(DIR)) {
					if ($file =~ m|\.$ext$|) {
						next if $file eq "index.html";
						next if $file eq "index.htm";
						next if $file eq "default.htm";
						$s = -s "$dir/$file";
						$count += 1;
						if ($s == 0) {
							print "<LI>$dir/$file"; $ccount += 1;
						}
					}
					if (-s "$dir/$file" > 1000000) {
						$s = -s "$dir/$file";
						push (@hugefiles, "$dir/$file [$s]");
					}
				}
				closedir(DIR);
			}
		}
	}
	print "<P>$count file(s) checked. $ccount file(s) may be corrupted.<P>\n";
	if (scalar(@hugefiles)) {
		print "<B>Large File Scan</B><P>\n";
		print "The following is a list of files greater than 1 MB in size in use by your Discus board.<P>\n";
		print "<LI>", join("\n<LI>", @hugefiles), "\n";
		print "<P>\n<HR>\n";
	}
	@searchfiles = ("addmessage-private.txt", "addmessage-public.txt", "admin.log", "frontpage_conf.txt", "locks.txt", "newpage.txt", "send_mail.conf");
	undef @found;
	foreach $file (@searchfiles) {
		push (@found, $file) if -e "$admin_dir/$file";
	}
	if (scalar(@found)) {
		print "<B>Unnecessary File Scan</B><P>\n";
		print "The following files are left over from earlier versions of Discus and are no longer used\n";
		print "in this version.  These files can be safely removed from your Discus administration directory.<P>\n";
		print "<UL>\n";
		foreach $file (@found) {
			print "<LI>$file (";
			$s = -s "$admin_dir/$file";
			print int($s/1000);
			print " kB)\n";
		}
		print "</UL>\n";
	}
	print "</TD></TR></TABLE>\n";
	print "</BODY></HTML>\n";
	exit(0);
}

#---SEPARATOR---#
#REQ:send_email_message

sub mail_administrator_quota {
	return 0 if $GLOBAL_OPTIONS{'capable'} == 0;
	my ($contexter, $backupfile) = @_;
	open (PASSWD, "$admin_dir/passwd.txt");
	@passwd = <PASSWD>;
	close (PASSWD);
	($admin) = grep(/^$superuser:/, @passwd);
	($user, $pass, $email) = split(/:/, $admin);
	undef $email1;
	if ($email =~ m|^([\w\+\-\.]+)\@([\w\+\-\.]+)$|) {
		$email1 = $email;
	}
	$email2 = "";
	if ($GLOBAL_OPTIONS{'admin_contact_email'} ne "") {
		$email2 = $GLOBAL_OPTIONS{'admin_contact_email'};
	} else {
		if ($contact =~ m|([\w\+\-\.]+)\@([\w\+\-\.]+)|) {
			$email2 = "$1\@$2";
		}
	}
	@email = ($email1, $email2); @email = grep(/\S/, @email);
	return 0 if scalar(@email) == 0;
	$addr = join(",", @email);
	$message = "The discussion forum which you administer at this URL:\n";
	$message .= "   $html_url\n";
	$message .= "has been automatically disabled due to ";
	if ($contexter == 2) {
		$message .= "a permissions problem\n";
		$message .= "encountered when attempting to remove files.\n";
	} else {
		$message .= "a detected disk quota\nproblem.\n";
	}
	$message .= "\nPlease go to the administration utility for this\n";
	$message .= "discussion board:\n";
	$message .= "   $html_url/admin.html\n";
	$message .= "and log in as \"$superuser\".  You will be given\n";
	$message .= "directions on how to handle this problem at that time.\n\n";
	$message .= "This is URGENT as your visitors will not be able to post to\n";
	$message .= "your discussion board until this matter is resolved.\n\n";
	$message .= "[This message was automatically generated by your discussion\n";
	$message .= "board.  Please do not reply to this message.]\n\n";
	&send_email_message($addr, "URGENT: Discussion forum automatically disabled", $message);
}

#---SEPARATOR---#

sub quota_check_test {
	$filename = "$admin_dir/backups/quotatest-file";
	if (-e "$filename") {
		unlink($filename) || &error_message("Quota test error", "Test file <B>$filename</B> exists!  Please remove this file before proceeding.");
	}
	$quota_exists = 0;
	if (!-e "$admin_dir/backups/QUOTA.txt") {
		open (SAFEGUARD, ">$admin_dir/backups/QUOTA.txt");
		print SAFEGUARD "time\nShut+down+for+quota+testing\n";
		close (SAFEGUARD);
	} else {
		$quota_exists = 1;
	}
	$bytepattern = "x" x 10000;
	$sized = $FORM{'size'}; $size =~ s/\D//g;
	&error_message("Invalid size") if ($sized < 1 || $sized > 25);
	$sizeflag = 0; undef $!;
	for ($size = 1; $size <= 1000000*$sized; $size += 10000) {
		open (FILE, ">>$filename") || last;
		print FILE $bytepattern;
		close (FILE);
		if (-s $filename < $size) {
			$sizeflag = 1;
			last;
		}
	}
	&header;
	print "<HTML><HEAD><TITLE>Disk Quota Tester</TITLE></HEAD>\n";
	print "<BODY BGCOLOR=#ffffff TEXT=#000000>$fs<CENTER><FONT SIZE=3><B>Disk Quota Tester</B></FONT></CENTER>\n";
	print "<HR>\n";
	print "Discus just tested your disk quota by attempting to create a file with a size of up to $sized.0 MB.\n";
	print "<P>\n";
	if ($sizeflag == 0 && $! eq "") {
		print "Your system <B>passed with flying colors</B>.  You have at least $sized.0 MB of space remaining on your disk quota.<P>\n";
	} elsif ($! ne "") {
		print "I got cut off writing to the file for this reason: <B>$!</B>.  I suspect that you have less than ", ($size/1000), " kB remaining on your disk quota, although I can't be sure.<P>\n";
	} elsif ($sizeflag) {
		print "The file was truncated, presumably due to a disk quota.  I am almost certain that you have less than ", ($size/1000), " kB remaining on your disk quota.  I'm quite sure of this, and this may be a cause for concern.<P>\n";
	}	
	if (!$quota_exists) {
		unlink("$admin_dir/backups/QUOTA.txt") || print "<FONT SIZE=5>WARNING: I couldn't remove $admin_dir/backups/QUOTA.txt.  Do so by FTP or telnet to enable your board!</FONT><P>\n";
	}
	if (-e $filename) {
		unlink($filename) || print "<FONT SIZE=5>WARNING: I couldn't remove $filename.  Do so by FTP or telnet to save disk space!</FONT><P>\n";
	}
	print "<HR><P><CENTER><A HREF=$cgiurlm?action=quota_helper&username=$superuser>Click here to continue</A></CENTER><P>\n";
	print "</BODY></HTML>\n";
	exit(0);
}

# END - FILE IS CORRECTLY UPLOADED #
