$forums[$x]^;}
$forumList = qq^$forumList ^;
$formNameField = qq^ ^;
$forumPasswordField = qq^ ^;
$formLogonButton = qq^ ^;
if ($authAllowRegister eq "on") {$formRegisterButton = qq^ ^;}
if ($authAllowSearch eq "on") {$forumSearchButton = qq^^;}
}
else {
$messageUrl = &ReadMessageFields;
for ($x = 1; $x < @forums; $x++) {$forumList .= qq^$forums[$x] \n ^;}
}
$printHiddenFields = &HiddenFields;
require "$board/forums.html";
}
############################################################
#
# subroutine: ReadMessage
# Usage:
# &ReadMessage($message);
#
# Parameters:
# $message = message filename to read
#
# Output:
# Takes a message and outputs it as HTML
# Dependant on the read.html file
############################################################
sub ReadMessage {
local($message) = @_;
local($posterName, $posterEmail,$postDateTime, $postSubject, $postReply) = &GetMessageHeader("$forumDir/$message");
$postDateTime = &formatDateTime($postDateTime);
open (MESSAGEFILE, "$forumDir/$message") || &CgiDie("Could not open message file\n");
for (1 .. 5) { ;}
chop($postMessage = );
close (MESSAGEFILE);
if ($noHtml eq "on") {
$postMessage =~ s/<([^>]+)>/\<$1>/ig;
$postSubject =~ s/<([^>]+)>/\<$1>/ig;
}
if ($noObscenity eq "on") {
foreach (@obscenity) {
$postMessage =~ s/$_/\#\@\&\$\^/ig;
$postSubject =~ s/$_/\#\@\&\$\^/ig;
}
}
$postMessage =~ s/\^p\^:/:/g;
$postMessage =~ s/\^p\^/
/g;
$postMessage =~ s/\^br\^/ /g;
$messageUrl = &ReadMessageFields;
$forumUrl = "$mainScript?forum=$forum&$messageUrl";
$formPostButton = qq^ ^;
$formReplyButton = qq^ ^;
$formSearchButton = qq^ ^;
$formEditButton = ($group eq "admin") ? qq^ ^ : "";
if ($digest eq "on") {
$digestHtml = "";
if ($postSubject !~ /Moderator!/) {
open (DIGEST, "$board/digest.html") || &CgiDie("Could not open digest template\n");
while () {$digestHtml .= $_;}
close (DIGEST);
$digestHtml =~ s/\$posterName/$posterName/g;
$digestHtml =~ s/\$posterEmail/$posterEmail/g;
$digestHtml =~ s/\$postDateTime/$postDateTime/g;
$digestHtml =~ s/\$postSubject/$postSubject/g;
$digestHtml =~ s/\$postMessage/$postMessage/g;
}
($digestHtml)
}
else {
$postReplies = "";
if ($threadedReplies ne "yes") {
opendir(FORUMDIR, "$forumDir") || &CgiDie("Could not open $forumDir directory\n");
$thread = $message;
$messageNumber = substr($message,0,6);
$threadNumber = $messageNumber;
@files = sort(grep(/.......$messageNumber\.msg$/, readdir(FORUMDIR)));
closedir(FORUMDIR);
foreach (@files) {
($replyName, $replyEmail, $replyDateTime, $replySubject, $reply) = &GetMessageHeader("$forumDir/$_");
$replyDateTime = &formatDateTime($replyDateTime);
if ($replySubject =~ /Moderator!/) {}
else {$postReplies .= qq^$readListItem$replySubject \n$readReplyDividerposted $replyDateTime \n^;}
}
}
else {
opendir(FORUMDIR, "$forumDir") || &CgiDie("Could not open forum directory $forumDir\n");
@files = sort(grep(/.*msg$/,readdir(FORUMDIR)));
closedir(FORUMDIR);
@threadFileList = ("$message");
@threadMessages = @threadFileList;
&makeThread(@threadMessages);
@files = @threadFileList;
@sthreads = ();
while (@files > 0) {push(@sthreads,&MakeThreadList(*files));}
if ($rangeby ne "") {
if ($rangefrom eq "") {$rangefrom = 1;}
if ($rangefrom < 1) {$rangefrom = 1;}
if ($rangeby > @files) {$rangefrom = @files;}
@files = @files[($rangefrom - 1), ($rangeby -1)];
}
@threads = calcChildThreads(@sthreads);
shift(@threads);
$ulCount = 0;
$prevLevel = -1;
foreach $x (@threads) {
($level, $messagefile, $threadDate, $children) = split(/\|/,$x);
if ($level > $prevLevel && $level > $displayThreadDepth) {$level = $prevLevel;}
if ($level > $prevLevel) {$ulCount++;}
elsif ($level < $prevLevel) {
for (1 .. ($prevLevel - $level)) {$ulCount--;}
}
if ($postReplies ne "") {$postReplies .= "\n " . " " x ($forumThreadIndent * ($level - 2));}
else {$postReplies .= " " x ($forumThreadIndent * ($level - 2));}
($replyName, $replyEmail, $replyDateTime, $replySubject, $reply) = &GetMessageHeader("$forumDir/$messagefile");
$replyDateTime = &formatDateTime($replyDateTime);
$messageUrl = &ReadMessageFields;
if ($readReplySep eq "") {$children = "";}
if ($readThread ne "on") {
if ($replySubject =~ /Moderator!/) {
if ($group eq "admin") {$postReplies .= qq^$readListItem$replySubject \n$readReplySep$children \n$readReplyDivider$replyDateTime ^;}
}
else {$postReplies .= qq^$readListItem$replySubject \n$readReplySep$children \n$readReplyDivider$replyDateTime ^;}
}
else {
$postReplies .= qq^$readListItem$replySubject \n$readReplySep$children \n$readReplyDivider$replyDateTime ^;
}
$prevLevel = $level;
}
$postReplies .= "\n";
}
$parentNumber = substr($message,7,6);
if ($parentNumber ne "000000") {
opendir(FORUMDIR, "$forumDir") || &CgiDie("Could not open $forumDir directory\n");
($parentMessage) = sort(grep(/$parentNumber.......\.msg$/, readdir(FORUMDIR)));
closedir(FORUMDIR);
($parentName, $parentEmail, $parentDateTime, $parentSubject, $parent) = &GetMessageHeader("$forumDir/$parentMessage");
$parentDateTime = &formatDateTime($parentDateTime);
$parentMessage = qq^$parentSubject \n$readReplyDivider$parentDateTime ^;
}
$postThreadDigest = qq^$$postThreadDigestText ^;
$postAttachHtml = "";
$attachFile = substr($message,0,13);
if (-e "$forumDir/$attachFile.atc") {
$attachTempHtml = "";
open(ATTACHTEMP, "$board/attach.html") || &CgiDie("Could Not Open $board/attach.html\n");
while () {$attachTempHtml .= $_;}
close (ATTACHTEMP);
open(ATTACHFILE, "$forumDir/$attachFile.atc") || &CgiDie("Could Not Open $forumDir/$attachFile\n");
while () {
chop($attachInfo = $_);
($postAttachmentFilename, $postAttachDesc, $postAttachWidth, $postAttachHeight) = split(/\|/, $attachInfo);
$postAttachmentFilename = "$forumDir/$postAttachmentFilename";
$postAttachHtml .= $attachTempHtml;
$postAttachHtml =~ s/\$postAttachmentFilename/$postAttachmentFilename/g;
$postAttachHtml =~ s/\$postAttachDesc/$postAttachDesc/g;
$postAttachHtml =~ s/\$postAttachWidth/$postAttachWidth/g;
$postAttachHtml =~ s/\$postAttachHeight/$postAttachHeight/g;
}
close (ATTACHFILE);
}
if ($forumUniqueHTML ne "") {
$uniqueHtml = "";
open (UNIQUE, "$forumUniqueHTML") || &CgiDie("Could not open forum's unique html\n");
while () {$uniqueHtml .= $_;}
close (UNIQUE);
}
if (($posterName eq $username) && ($posterEmail eq $email) && ($posterEmail ne "")) {$formEditButton = qq^ ^;}
if ($forumInRead eq "on") {
$op = "read";
$messageHtml = &PrintForumPage();
}
if ($SelectBoxForum eq "yes") {
$messageOptions = "";
$thread = "";
opendir(FORUMDIR, "$forumDir") || &CgiDie("Couldn't open $forumDir");
@fmessages = sort(grep(/.*msg$/,readdir(FORUMDIR)));
closedir(FORUMDIR);
if ($digestDecending eq "on") {@fmessages = reverse(@fmessages);}
&expireMessages($forumDir, *fmessages);
if ($displayOnlyNewMessages eq "on" && $lastRead eq "" && $useLastRead eq "on") {$lastRead = &GetUserLastRead($forumDir, $username, $session, $highNumber);}
&extractFileList(*fmessages, $lastRead, $thread, $beginDate, $endDate, $youngestAge, $oldestAge, $keywords, $exactMatch, $forumDir);
foreach $messagefile (@fmessages) {
($sposterName, $sposterEmail, $spostDate, $spostSubject, $spostReply) = &GetMessageHeader("$forumDir/$messagefile");
if ($selectBoxUseDate eq "yes") {$sD = &formatDateTime($spostDate);}
if ($selectBoxUseSubject eq "yes") {$sS = substr($spostSubject,0,23);}
$sText = "$sD $sS";
$messageOptions .= qq^$sText \n^;
}
$sSubject = substr($postSubject,0,23);
}
$printHiddenFields = &HiddenFields;
$printHiddenFields .= qq^ \n^;
$printHiddenFields .= qq^ \n^;
require "$board/read.html";
}
}
############################################################
#
# subroutine: PrintForumPage
# Usage:
# &PrintForumPage;
#
# Parameters:
# None, but CGI Form Variables below affect
# the list of information that comes up
# beginDate = Date To Start Reading Messages From
# endDate = Date To Last Reading
# keywords = Keywords To Search On
# exactMatch= Keyword Search ExactMatch
# youngestAge= Start Reading Messages From
# This Days old
# oldestAge =Finish Reading Messages From
# This Days Old
#
# Output:
# Prints the message list in a forum based on
# last read for the user, date ranges, and keyword
# search if the program is configured for that.
# dependent on forum.html
############################################################
sub PrintForumPage {
local($x);
opendir(FORUMDIR, "$forumDir") || &CgiDie("Could not open forum directory $forumDir\n");
@files = sort(grep(/.*msg$/,readdir(FORUMDIR)));
closedir(FORUMDIR);
$highNumber = substr($files[@files - 1],0,6);
$lowNumber = substr($files[0],0,6);
&expireMessages($forumDir, *files);
if ($displayOnlyNewMessages eq "on" && $lastRead eq "" && $useLastRead eq "on") {$lastRead = &GetUserLastRead($forumDir, $username, $session, $highNumber);}
&extractFileList(*files, $lastRead, $thread, $beginDate, $endDate, $youngestAge, $oldestAge, $keywords, $exactMatch, $forumDir);
$messageHtml = "";
if ($digest eq "on") {
($posterName, $posterEmail, $postDate, $postSubject, $postReply) = &GetMessageHeader("$forumDir/$files[0]");
$forumSubject = "Re: $postSubject" if !($postSubject =~ /^Re:/i);
if ($digestDecending eq "on") {@files = reverse(@files);}
foreach (@files) {$messageHtml .= &ReadMessage($_);}
}
else {
@sthreads = ();
while (@files > 0) {push(@sthreads,&MakeThreadList(*files));}
if ($rangeby ne "") {
if ($rangefrom eq "") {$rangefrom = 1;}
if ($rangefrom < 1) {$rangefrom = 1;}
$rangeto = $rangefrom + $rangeby - 1;
if ($rangeto > @sthreads) {$rangeto = @sthreads;}
$threadCount = @sthreads;
@tmpthreads = ();
$tmpelem = 0;
for ($i = $rangefrom - 1; $i < $rangeto; $i++) {
$tmpthreads[$tmpelem] = $sthreads[$i];
$tmpelem++;
}
@sthreads = @tmpthreads;
}
@threads = calcChildThreads(@sthreads);
$ulCount = 0;
$prevLevel = -1;
foreach $x (@threads) {
($level, $messagefile, $threadDate, $children) = split(/\|/,$x);
$threadDate = &formatDateTime($threadDate);
if ($level > $clipThreadDepth) {next;}
if ($level > $prevLevel && $level > $displayThreadDepth) {$level = $prevLevel;}
if ($level > $prevLevel) {$ulCount++;}
elsif ($level < $prevLevel) {
for (1 .. ($prevLevel - $level)) {$ulCount--;}
}
if ($messageHtml ne "") {$messageHtml .= "\n$forumThreadNewLine" . " " x ($forumThreadIndent * ($level - 1));}
local ($posterName, $posterEmail, $postDate, $postSubject, $postReply) = &GetMessageHeader("$forumDir/$messagefile");
$messageUrl = &ReadMessageFields;
$thisThreadDate = "";
$firstThreadLead = "";
if ($level == 1) {
if ($messageHtml ne "" || $threadDateFirst eq "no") {$firstThreadLead = qq^$firstThreadLeadText^;}
$thisThreadDate = qq^$thisThreadDateText $threadDate$firstLineTeminator^;
}
if ($readThread ne "on") {
if ($postSubject =~ /Moderator!/) {
if ($group eq "admin") {$thisThreadSubject = qq^$listItem $postSubject ^;}
}
else {$thisThreadSubject = qq^$forumListItem $postSubject ^;}
}
else {
$thisThreadSubject = qq^$listItem $postSubject ^;
}
$thisChildren = qq^$forumThreadChildrenSep$children^;
$messageHtml .= ($threadDateFirst eq "yes") ? "$firstThreadLead$thisThreadDate$thisThreadSubject$thisChildren" : "$thisThreadSubject$thisChildren$firstThreadLead$thisThreadDate";
$prevLevel = $level;
}
$messageHtml .= "\n";
}
if ($op eq "read") {
return ($messageHtml);
}
if ($forumUniqueHTML ne "") {
require "$forumUniqueHTML";
$uniqueHtml = &getUniqueHtml;
}
$formBackToTop = ($thread eq "") ? "" : qq^ $formBacktoTopText ^;
$formPostButton = qq^ ^;
$formSearchButton = qq^ ^;
if ($rangeto < $threadCount) {
$nextRangefrom = $rangeto + 1;
$formNextRange = qq^$forumNextText ^;
}
if ($rangefrom > 1) {
$previousRangefrom = $rangefrom - $rangeby - 1;
$formPreviousRange = qq^$forumPreviousText ^;
}
$formDateTime = &getDateTime;
$printHiddenFields = &HiddenFields;
$printHiddenFields .= qq^ \n^;
$printHiddenFields .= qq^ \n^;
require "$board/forum.html";
}
############################################################
#
# subroutine: PrintPostOrReplyPage
# Usage:
# &PrintPostOrReplyPage;
#
# Parameters:
# None, but CGI Form Variables below affect
# the form that comes up if the action that
# is being taken is a reply to a message instead
# of a fresh post.
# replyToMessage = Message # We are replying to
# emailReply = email to notify that a reply has
# occured
# postSubject = Subject of message we are
# replying to.
#
# Output:
# HTML Output for the Create Post (or Reply) Page
# dependant on post.html
############################################################
sub PrintPostOrReplyPage {
local($reply, $previousMessage);
local($replyToMessage, $emailReply);
local($title, $header);
local($emailTag, $replyToEmail);
$previousMessage = "";
$replyToMessage = "";
$emailReply = "";
$header = "$postHeader";
$messageUrl = &ReadMessageFields;
$forumUrl = "$mainScript?forum=$forum&$messageUrl";
if ($replyOp eq "on") {
$replyToMessage = $in{"replyToMessage"};
$emailReply = $in{"emailReply"};
$thread = $replyToMessage;
($messageNumber, $threadNumber) = split(/-/, $replyToMessage);
$threadNumber =~ s/(.*)\.msg/\1/;
$header = "$replyHeader";
$postMessage = "";
open (REPLYFILE, "$forumDir/$replyToMessage") || &CgiDie("Could not open reply message");
chop($postName = );
;
chop($postDate = );
$postDate = &formatDateTime($postDate);
;
chop($reply = );
if ($reply =~ /\@/) {($emailTag,$replyToEmail) = split(/:/,$reply);}
chop($postMessage = );
$postMessage =~ s/\^p\^/\n\n: /g;
$postMessage =~ s/\^br\^/\n: /g;
$postMessage = ": $postName" . " on $postDate wrote:" . $postMessage;
close (REPLYFILE);
$postSubject = $in{"postSubject"};
$postSubject = "Re: $postSubject" if !($postSubject =~ /^Re:/i);
}
$formDateTime = &getDateTime;
# $formDateTime = &formatDateTime($formDateTime);
$formNameField = qq^ ^;
$formEmailField = qq^ ^;
if ($forceName eq "on" && $name ne "") {
$formNameField = qq^ ^;
$formNameField .= "$name";
}
if ($forceEmail eq "on" && $email ne "") {
$formEmailField = qq^ ^;
$formEmailField .= "$email";
}
if ($replyToEmail ne "") {$replyToEmailField = qq^ ^;}
$formWantEmail = "";
if ($allowReplyEmail eq "on") {$formWantEmail = qq^\n $formWantEmailText^;}
$formAttachment = "";
for ($l = 1; $l <= $attachNumber; $l++) {
$formAttachment .= qq^$formAttachmentText \n^;
$formAttachment .= qq^$formAttachmentDescText ^;
}
$formCreateButton = qq^ ^;
$printHiddenFields = &HiddenFields;
$printHiddenFields .= qq^ \n^;
$printHiddenFields .= qq^ \n^;
$printHiddenFields .= qq^ \n^;
require "$board/post.html";
}
############################################################
#
# subroutine: printEditPage
# Usage:
# &PrintEditPage;
#
# Parameters:
# None.
#
# Output:
# HTML Output for the Edit Page
# dependent on edit.html
############################################################
sub PrintEditPage {
$replyToMessage = $in{"replyToMessage"};
$messageName = $replyToMessage;
$messageUrl = &ReadMessageFields;
$forumUrl = "$mainScript?forum=$forum&$messageUrl";
open (REPLYFILE, "$forumDir/$messageName") || &CgiDie("Could not open message");
chop($formName = );
chop($formEmail = );
chop($formDate = );
chop($formSubject = );
chop($reply = );
chop($formMessage = );
close (REPLYFILE);
if ($group ne "admin") {
if (($formName ne $username) && ($formEmail ne $email)) {
&PrintForumPage;
exit;
}
}
$formNameField = qq^ ^;
$formEmailField = qq^ ^;
$formDateField = qq^ ^;
$formSubjectField = qq^ ^;
$replyValue = ($reply =~ /\@/) ? "on" : "off";
$formWantEmailField = qq^ Receive replies via e-mail?^;
$formMessage =~ s/^\^p\^(.*)/\1/g;
$formMessage =~ s/\^p\^/\n\n/g;
$formMessage =~ s/\^br\^/\n/g;
$formMessageField = qq^^;
$formCreateButton = qq^ ^;
$formDeleteButton = qq^ ^;
$printHiddenFields = &HiddenFields;
$printHiddenFields .= qq^ \n^;
require "$board/edit.html";
}
############################################################
#
# subroutine: CreatePosting
# Usage:
# &CreatePosting;
#
# Parameters:
# None, but CGI Form Variables below affect
# how the message is posted.
# formName = firstname of poster
# formEmail = email of poster
# formSubject = subject of the post
# formMessage = body of the post
# replyToMessage = message we are replying to
# replyToEmail = email address of user we are
# replying to
# postWantEmail = "on" if we want email replies
# postAttachment = file upload attachment
#
# Output:
# Posts the message to a file and then prints the
# list of forum messages.
#
############################################################
sub CreatePosting {
require "spammers.ini";
$formName = $in{"formName"};
$formEmail = $in{"formEmail"};
$formSubject = $in{"formSubject"};
$createMsgError = "";
if ($requireSubject eq "on" && $formSubject eq "") {$createMsgError .= "\n You did not enter a subject.";}
if ($requireName eq "on" && $formName eq "") {$createMsgError .= "\n You did not enter your name.";}
if ($requireEmail eq "on" && $formEmail eq "") {$createMsgError .= "\n You did not enter an e-mail address.";}
$formName =~ s/\r|\n//g;
$formEmail =~ s/\r|\n//g;
foreach (@spammers) {
$fEmail = lc($formEmail);
if ($_ eq $fEmail) {
&PrintForumPage;
exit;
}
}
$formSubject =~ s/\r|\n//g;
if (($forumModerate eq "yes") && ($group ne "admin")) {$formSubject = "Moderator! $formSubject"}
$formMessage = "^p^" . $in{"formMessage"};
$formMessage =~ s/\r\n/\n/g;
$formMessage =~ s/\r/\n/g;
$formMessage =~ s/\n\n/^p^/g;
$formMessage =~ s//^p^/gi;
$formMessage =~ s/\n/^br^/g;
$formMessage =~ s/ /^br^/gi;
$replyToMessage = $in{"replyToMessage"};
$replyToMessage = ($replyToMessage < 1) ? "000000" : substr($replyToMessage,0,6);
$replyToEmail = $in{"replyToEmail"};
$formDateTime = $in{"formDateTime"};
opendir(FORUMDIR, "$forumDir") || &CgiDie("Couldn't open $forumDir");
@files = sort(grep(/.*msg$/,readdir(FORUMDIR)));
closedir(FORUMDIR);
$messageName = $in{"messageName"};
if ($messageName eq "") {
for ($x = @files; $x > 0; $x--) {
if ((-M "$forumDir/$files[$x - 1]") > 1) {
&RemoveElement(*files,$x-1);
next;
}
open (REPLYFILE, "$forumDir/$files[$x - 1]") || &CgiDie("Could not open $files[$x - 1]");
chop($msgName = );
;
chop($msgDateTime = );
close (REPLYFILE);
if (($formName eq $msgName) && ($formDateTime eq $msgDateTime)) {
$createMsgError .= " This message already exits.";
last;
}
}
}
$formReply = "";
$postWantEmail = $in{"postWantEmail"};
if ($postWantEmail eq "on" || $forceReplyEmail eq "on") {$formReply = "email:$formEmail";}
if ($createMsgError ne "") {$createMsgError = qq^Error posting to the $forumName \n$createMsgError^;}
else {
$wholeMsg = "";
$wholeMsg .= "$formName\n";
$wholeMsg .= "$formEmail\n";
$wholeMsg .= "$formDateTime\n";
$wholeMsg .= "$formSubject\n";
$wholeMsg .= "$formReply\n";
$wholeMsg .= "$formMessage\n";
if ($messageName eq "") {
opendir(FORUMDIR, "$forumDir") || &CgiDie("Couldn't open $forumDir");
@files = sort(grep(/.*msg$/,readdir(FORUMDIR)));
closedir(FORUMDIR);
$highNumber = substr($files[@files - 1],0,6);
@files = ();
$highNumber++;
$highNumber = sprintf("%6d",$highNumber);
$highNumber =~ tr/ /0/;
$highNumber = "000001" if ($highNumber eq "000000");
$messageName = "$highNumber-$replyToMessage" . ".msg";
}
open(WRITEMSG, ">$forumDir/$messageName") || &CgiDie("Could't open $messageName.msg for writing");
print WRITEMSG $wholeMsg;
close (WRITEMSG);
$postAttachmentData = "";
for ($l = 1; $l <= $attachNumber; $l++) {
$postAttachment = $in{"postAttachment" . $l};
$postAttachmentFilename = $incfn{"postAttachment" . $l};
$postAttachmentFilename =~ s/%([A-Fa-f0-9-_]{2})/pack("c",hex($1))/ge;
$postAttachmentFilename =~ s#^.*[/\\:](.*$)#\1#gi;
$postAttachmentDesc = $in{"postAttachmentDesc" . $l};
if ($postAttachmentFilename ne "") {
open (ATTACH, "$postAttachment");
binmode(ATTACH);
$attfile = "";
while () {$attfile .= $_;}
close (ATTACH);
open (ATTACH, ">$forumDir/$highNumber-$replyToMessage-$postAttachmentFilename");
binmode(ATTACH);
print ATTACH $attfile;
close (ATTACH);
unlink("$postAttachment");
# rename("$postAttachment", "$forumDir/$highNumber-$replyToMessage-$postAttachmentFilename") || &CgiDie("Could not rename $postAttachment $forumDir/$highNumber-$replyToMessage-$postAttachmentFilename\n");
if ($attachImage eq "yes") {($iWidth, $iHeight) = &getImageSize("$forumDir/$highNumber-$replyToMessage-$postAttachmentFilename");}
$postAttachmentData .= "$highNumber-$replyToMessage-$postAttachmentFilename|$postAttachmentDesc|$iWidth|$iHeight\n";
}
else {unlink("$postAttachment");}
}
if ($postAttachmentData ne "") {
open(WRITEATTACH, ">$forumDir/$highNumber-$replyToMessage.atc") || &CgiDie("Could Not Write Attachment Data File\n"); print WRITEATTACH $postAttachmentData;
close(WRITEATTACH);
}
$replyToEmail = $in{"replyToEmail"};
if ($replyToEmail ne "" && $sendReplyEmail eq "on") {
require "mail-lib.pl";
$replySubject = "Reply to your $forumName message.";
&sendMail($fromEmail, $replyToEmail, $replySubject, "The message:\n\n" . $formMessage);
}
}
if (($forumModerate eq "yes") && ($group ne "admin")) {&printConfirmationPage;}
else {&PrintForumPage;}
}
###########################################################
#
# Subroutine: printConfirmationPage
# Usage:
# &printConfirmationPage;
#
# Parameters:
#
# Output:
# Prints the confirmation after a moderated message is
# posted
#
###########################################################
sub printConfirmationPage {
$messageUrl = &ReadMessageFields;
$forumUrl = "$mainScript?forum=$forum&$messageUrl";
require "$board/confirm.html";
}
###########################################################
#
# Subroutine: DeleteMessage and DeleteMessageThread
# Usage:
# &DeleteMessage;
#
# Parameters:
# $message. The message to be deleted
#
# Output:
# Delete a message and its child threads then prints
# the forum page.
#
###########################################################
sub DeleteMessage {
local($message) = @_;
@threadFileList = ("$message");
@messages = ("$message");
&makeThread(@messages);
foreach $deleteFile (@threadFileList) {unlink("$forumDir/$deleteFile");}
&PrintForumPage;
}
sub makeThread {
local(@messages) = @_;
local($messageNumber, $threadFiles);
foreach $thisMessage (@messages) {
$messageNumber = substr($thisMessage,0,6);
opendir(FORUMDIR, "$forumDir") || &CgiDie("Could not open $forumDir directory\n");
@threadFiles = sort(grep(/.......$messageNumber\.msg$/, readdir(FORUMDIR)));
closedir(FORUMDIR);
if (@threadFiles > 0) {
@threadFileList = (@threadFileList, @threadFiles);
&makeThread(@threadFiles)
}
}
}
############################################################
#
# subroutine: PrintSearchPage
# Usage:
# &PrintSearchPage;
#
# Parameters:
# None.
#
# Output:
# HTML Output for the search page
# dependent on search.html
############################################################
sub PrintSearchPage {
for ($x = 1; $x < @forums; $x++) {$forumList .= qq^$forums[$x]^;}
$forumListField = qq^$forumList ^;
$threadField = qq^ ^;
$beginDateField = qq^ ^;
$endDateField = qq^ ^;
$youngestAgeField = qq^ ^;
$oldestAgeField = qq^ ^;
$checked = ($exactMatch eq "on") ? "checked" : "";
$exactMatchField = qq^ ^;
$keywordsField = qq^ ^;
$checked = ($useLastRead eq "on") ? "checked" : "";
$useLastReadField = qq^ ^;
$lastReadField = qq^ ^;
$checked = ($digest eq "on") ? "checked" : "";
$digestField = qq^ ^;
$formSearchButton = qq^ ^;
$messageUrl = &ReadMessageFields;
$forumUrl = "$mainScript?forum=$forum&$messageUrl";
$printHiddenFields = "";
$printHiddenFields .= qq^ \n^;
$printHiddenFields .= qq^ \n^;
require "$board/search.html";
}
############################################################
#
# subroutine: extractFileList
# Usage:
# &extractFileList(*files, $lastRead, $thread, $beginDate,
# $endDate, $youngestAge, $oldestAge,
# $keywords, $exactMatch, $forumDir);
#
# Parameters:
# The non-filename related parameters are criteria
# used to prune the file list down so that not all
# the messages show up in the forum message list.
#
# *files = reference to a list of message filenames
# in the forum for pruning.
# $lastRead = last read message number for the user
# so that only new messages are read
# $thread = the thread number of the message
# $youngestAge= Start Reading Messages From
# This Days old
# $oldestAge =Finish Reading Messages From
# This Days Old
# $beginDate = Date To Start Reading Messages From
# $endDate = Date To Last Reading
# $keywords = Keywords To Search On
# $exactMatch= Keyword Search ExactMatch
#
# Output:
# Prunes a list of messages that do not satisfy the
# criteria being passed to the routine (Such as a
# date range) from the *files reference to an array
# of message filenames.
#
############################################################
sub extractFileList {
local(*files, $lastRead, $thread, $beginDate, $endDate, $youngestAge, $oldestAge, $keywords, $exactMatch, $forumDir) = @_;
local($x, $filename);
local($month, $day, $year, $compDate);
local($fileDate);
@keywordList = split(/\s+/,$keywords);
if ($thread ne "") {
@threadFileList = ("$thread");
@threadMessages = @threadFileList;
&makeThread(@threadMessages);
}
for ($x = @files; $x > 0; $x--) {
if ($lastRead > 0 && substr($files[$x-1],0,6) <= $lastRead && $displayOnlyNewMessages eq "on") {
&RemoveElement(*files,$x-1);
next;
}
if ($thread ne "") {
$found = "no";
foreach $threadMessage (@threadFileList) {
if ($files[$x - 1] eq $threadMessage) {
$found = "yes";
last;
}
}
if ($found eq "no") {
&RemoveElement(*files,$x-1);
next;
}
}
$filename = "$forumDir/$files[$x - 1]";
if (($youngestAge ne "") && ((-M $filename) < $youngestAge)) {
&RemoveElement(*files,$x-1);
next;
}
if (($oldestAge ne "") && ((-M $filename) > $oldestAge)) {
&RemoveElement(*files, $x-1);
next;
}
if ($beginDate ne "" || $endDate ne "") {
($day, $month, $year) = split(/\./, $beginDate);
if (length($month) < 2) {$month = "0" . $month;}
if (length($day) < 2) {$day = "0" . $day;}
if ($year > 50 && $year < 1900) {$year += 1900;}
if ($year < 1900) {$year += 2000;}
$compbeginDate = $year . $month . $day;
($day, $month, $year) = split(/\./, $endDate);
if (length($month) < 2) {$month = "0" . $month;}
if (length($day) < 2) {$day = "0" . $day;}
if ($year > 50 && $year < 1900) {$year += 1900;}
if ($year < 1900) {$year += 2000;}
$compendDate = $year . $month . $day;
$fileDate = (stat($filename))[9];
($day, $month, $year) = (localtime($fileDate))[3,4,5];
$month++;
if (length($month) < 2) {$month = "0" . $month;}
if (length($day) < 2) {$day = "0" . $day;}
if ($year > 50 && $year < 1900) {$year += 1900;}
if ($year < 1900) {$year += 2000;}
$fileDate = $year . $month . $day;
if (($beginDate ne "" && $fileDate < $compbeginDate) || ($endDate ne "" && $fileDate > $compendDate)) {
&RemoveElement(*files, $x-1);
next;
}
}
if ($keywords ne "") {
@notFoundWords = @keywordList;
open(SEARCHFILE, $filename);
while() {
$line = $_;
&FindKeywords($exactMatch, $line, *notFoundWords);
}
close (SEARCHFILE);
if (@notFoundWords > 0) {
&RemoveElement(*files, $x - 1);
next;
}
}
}
$thread = "";
}
############################################################
#
# subroutine: FindKeywords
# Usage:
# &FindKeywords($exactMatch, $line,
# *notFoundWords);
#
# Parameters:
# $exactMatch = 'on' if keyword search is exact match
# $line = line to search
# *notFoundWords = array of words we have not
# found yet.
#
# Output:
# *notFoundWords array gets elements deleted as the
# keywords get found in the $line.
#
############################################################
sub FindKeywords {
local($exactMatch, $line, *notFoundWords) = @_;
local($x, $matchWord);
if ($exactMatch eq "on") {
for ($x = @notFoundWords; $x > 0; $x--) {
$matchWord = $notFoundWords[$x - 1];
if ($line =~ /\b$matchWord\b/i) {splice(@notFoundWords,$x - 1, 1);}
}
}
else {
for ($x = @notFoundWords; $x > 0; $x--) {
$matchWord = $notFoundWords[$x - 1];
if ($line =~ /$matchWord/i) {splice(@notFoundWords,$x - 1, 1);}
}
}
}
############################################################
#
# subroutine: GetSessionInfo
# Usage:
# ($session, $username, $group, @extraFields,
# = &GetSessionInfo($session, "script name",
# *in);
#
# Parameters:
# $session = session id. Null if it is not defined yet
# $mainScript = the script you are calling
# &GetSessionInfo From
# *in = A reference to the form data that was read
# in with &ReadParse.
#
# Output:
# $session = session id
# $username = user name
# $group = group information
# @extraFields = an array of more fields usually
# consisting of the following:
# $name = name
# $email = email address
############################################################
sub GetSessionInfo {
local($session, $mainScript, *in) = @_;
local($sessionFile, @fields);
local(@fields);
if ($session eq "") {
require "auth-extra-lib.pl";
@fields = &VerifyUser($mainScript, *in);
}
else {
$sessionFile = "$session.dat";
open(SESSIONFILE, "$authSessionDir/$sessionFile") || (require "./error.html" && exit);
while () {
chop;
@fields = split(/\|/);
}
close (SESSIONFILE);
unshift(@fields, $session);
}
@fields;
}
############################################################
#
# subroutine: GetUserLastRead
# Usage:
# &GetUserLastRead($forumDir, $username, $highNumber);
#
# Parameters:
# $forumDir = directory path for the forum
# $username = username dirived from authentications
# $session = session id
# $highNumber = the highest message number
#
# Output:
# Returns $lastRead = last read message number.
# $lastRead is written over with the highest message
# number IF the session id is different from the session
# from the lastRead
#
############################################################
sub GetUserLastRead {
local($forumDir, $username, $session, $highNumber) = @_;
local($lastRead, $oldSession);
if ($lastRead eq "") {$lastRead = 0;}
unless (-e "$forumDir/$username.dat") {$lastRead = 0;}
else {
open (USERFILE, "$forumDir/$username.dat") || &CgiDie("Error opening userfile $username\n");
$lastRead = ;
$oldSession = ;
chop ($lastRead);
chop($oldSession);
close (USERFILE);
}
if ($session ne $oldSession) {
open (USERFILE, ">$forumDir/$username.dat") || &CgiDie("Error opening userfile $username\n");
print USERFILE "$highNumber\n";
print USERFILE "$session\n";
close (USERFILE);
}
$lastRead;
}
############################################################
#
# subroutine: GetDateAndTime
# Usage:
# &GetDateAndTime(;
#
# Parameters:
# None.
#
# Output:
# Returns a string of the current date and time.
#
############################################################
sub getDateTime {
local ($sec, $min, $hour, $mday, $mon);
local($year, $wday, $yday, $isdst);
local ($ampm, $currentdatetime, $dateTime);
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
# 01/22/00 MRM: Commented out line below is a Y2K bug. Correct code is below.
# Database is somewhat corrupt. Code inserted at line 1348 fixes that problem.
# Future data s/b okay.
# bug edited out, kurt w. 03.04.2000
# perl's localtime(time)[5] will return
# the number of years elapsed since
# 1900, thus the correct year can be determined
# by adding 1900 to the result.
$year = ($year + 1900);
$mon++;
if (length($min) == 1) {$min = "0" . $min;}
$dateTime = "$year:$mon:$mday:$hour:$min:$sec";
($dateTime);
}
############################################################
#
# subroutine: formatDateTime
# Usage:
# &formatDateTime(;
#
# Parameters:
# None.
#
# Output:
# Returns a string of the current date and time.
#
############################################################
sub formatDateTime {
local ($dateTime) = @_;
local ($date, $time);
local ($year, $mon, $mday, $hour, $min, $sec);
local (@shortMonth) = ("Bad month", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
local (@longMonth) = ("Bad month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
($year, $mon, $mday, $hour, $min, $sec) = split(/:/, $dateTime);
# 01/22/00 MRM: fix Y2K bug.
if ($year < 1900) { $year += 1900; }
# Date
if ($dateFormat > 10) {
if ($mday > 10) {$mday = "0" . $mday;}
}
if ($dateFormat == 1) {$date = "$longMonth[$mon] $mday, $year";}
elsif ($dateFormat == 2) {$date = "$mon/$mday/" . substr($year,2,3);}
elsif ($dateFormat == 3) {$date = "$mon-$mday-" . substr($year,2,3);}
elsif ($dateFormat == 4) {$date = "$mon.$mday." . substr($year,2,3);}
elsif ($dateFormat == 5) {$date = "$mon.$mday.$year";}
elsif ($dateFormat == 6) {$date = "$mday/$mon/" . substr($year,2,3);}
elsif ($dateFormat == 7) {$date = "$mday-$mon-" . substr($year,2,3);}
elsif ($dateFormat == 8) {$date = "$mday.$mon." . substr($year,2,3);}
elsif ($dateFormat == 9) {$date = "$mday.$mon.$year";}
elsif ($dateFormat == 10) {$date = "$mday/$shortMonth[$mon]/$year";}
elsif ($dateFormat == 11) {$date = "$mon/$mday/$year";}
elsif ($dateFormat == 12) {$date = "$mon-$mday-$year";}
elsif ($dateFormat == 13) {$date = "$mon.$mday.$year";}
elsif ($dateFormat == 14) {$date = "$mday/$mon/$year";}
elsif ($dateFormat == 15) {$date = "$mday-$mon-$year";}
elsif ($dateFormat == 16) {$date = "$mday.$mon.$year";}
else {$date = "";}
# Time
$ampm = "";
if ($timeFormat < 2) {
$ampm = "AM";
$ampm = "PM" if ($hour > 11);
if ($hour > 12) {$hour - 12}
else {$hour =~ s/0//g;}
}
if ($timeFormat == 1) {$time = "$hour:$min $ampm";}
elsif ($timeFormat == 2) {$time = "$hour:$min";}
elsif ($timeFormat == 2) {$time = "$hour:$min:$sec";}
else {$time = "";}
# Concat date and time
if ($date ne "" && $time ne "") {$dt = "$date $time";}
else {$dt = "$date$time";}
($dt);
}
############################################################
#
# subroutine: GetMessageHeader
# Usage:
# &GetMessageHeader($filename);
#
# Parameters:
# $filename = message filename to read header from
#
# Output:
# Returns an array of the items in the message header.
#
# $posterName = name
# $posterEmail = email address of poster
# $postDate = date/time of the posting
# $postSubject = subject of the post
# $postReply = post reply e-mail address
#
############################################################
sub GetMessageHeader {
local($filename) = @_;
local($posterName, $posterEmail, $postDate, $postSubject, $postReply);
open (MESSAGEFILE, "$filename") || &CgiDie("Could not open $filename header\n");
chop($posterName = );
chop($posterEmail = );
chop($postDate = );
chop($postSubject = );
chop($postReply = );
close(MESSAGEFILE);
($posterName, $posterEmail, $postDate, $postSubject, $postReply);
}
############################################################
#
# subroutine: MakeThreadList
# Usage:
# &MakeThreadList(*fileList);
#
# Parameters:
# *fileList = array of message file names to make
# a threaded, hierarchical message
# listing out of.
#
# Output:
# @threads = an array containing the threaded,
# hierarchical message structure.
#
############################################################
sub MakeThreadList {
local(*fileList) = @_;
local(@threads,$seqPtr);
local($sequence,$previous);
local($posterName, $posterEmail, $postDate, $postSubject, $postReply);
$seqPtr = @fileList - 1;
if ($seqPtr > -1) {($posterName, $posterEmail, $postDate, $postSubject, $postReply) = &GetMessageHeader("$forumDir/@fileList[$seqPtr]")};
while(1) {
@fileList[$seqPtr] .= "|$postDate";
$sequence = @fileList[$seqPtr];
$previous = substr($sequence,7,6);
$previousPointer = &GetPointer(*fileList, $previous);
if (($previous eq "000000") || ($previousPointer == -1)) {last;}
$seqPtr = $previousPointer;
}
# $sequencepoint is now the top of the thread for the highest sequence #
@seqStack = ($seqPtr);
$curStackSize = 1;
push(@threads, "$curStackSize|$sequence");
while(@fileList > 0) {
$nextSeq = substr($sequence,0,6);
$nextPtr = &GetNextThread(*fileList, $nextSeq, $seqPtr);
if ($nextPtr > -1) {
$curStackSize++;
push(@seqStack, $nextPtr);
$sequence = $fileList[$nextPtr];
$seqPtr = $nextPtr;
push(@threads, "$curStackSize|$sequence");
}
else {
@fileList = &RemoveElement(*fileList, $seqPtr);
$curStackSize--;
pop(@seqStack);
if (@seqStack > 0) {
$seqPtr = $seqStack[@seqStack - 1];
$sequence = $fileList[$seqPtr];
}
else {last;}
}
}
@threads;
}
sub calcChildThreads {
local(@threads) = @_;
local($workingThread, $level, $messagefile, $threadDate, $children, $nextThread);
for($workingThread = 0; $workingThread < @threads; $workingThread++) {
$children = 0;
($level, $messagefile, $threadDate) = split(/\|/,$threads[$workingThread]);
for($nextThread = $workingThread + 1; substr($threads[$nextThread], 0, 1) > $level; $nextThread++) {
$children++;
}
$threads[$workingThread] = "$level|$messagefile|$threadDate|$children";
}
@threads;
}
############################################################
#
# subroutine: GetPointer
# Usage:
# &GetPointer(*fileList, $seq);
#
# Parameters:
# *fileList = list of files
# $seq = sequence number
#
# Output:
# Returns a numerical pointer into the array of
# files where the sequence number appears as the
# message number. Remember, messages appear as
# [MESSAGE NUMBER]-[REPLY TO NUMBER].MSG format.
# where the message number and reply to number
# are a fixed 6 digits.
#
############################################################
sub GetPointer {
local(*fileList, $seq) = @_;
local($pointer,$x);
$pointer = -1;
for ($x = 0;$x < @fileList; $x++) {
if (substr($fileList[$x],0,6) eq $seq) {
$pointer = $x;
last;
}
}
$pointer;
}
############################################################
#
# subroutine: GetNextThread
# Usage:
# &GetNextThread(*fileList, $seq, $start);
#
# Parameters:
# *fileList = list of message filenames
# $seq = sequence/message # to search for
# $start = pointer into array to start searching from
#
# Output:
# Returns the pointer into the array of message
# filenames where the next reply to the message # is.
#
############################################################
sub GetNextThread {
local(*fileList, $seq, $start) = @_;
local($pointer, $x);
$pointer = -1;
for ($x = $start; $x < @fileList; $x++) {
if (substr($fileList[$x],7,6) eq $seq) {
$pointer = $x;
last;
}
}
$pointer;
}
############################################################
#
# subroutine: RemoveElement
# Usage:
# &RemoveElement;
#
# Parameters:
# *fileList = array of message numbers
# $number = pointer into the array of the
# element to remove
#
# Output:
# *fileList without the $number element.
#
############################################################
sub RemoveElement {
local(*fileList, $number) = @_;
if ($number > @fileList) {die "Number was higher than " . "number of elements in file list";}
splice(@fileList,$number,1);
@fileList;
}
############################################################
#
# subroutine: GetForumInfo
# Usage:
# &GetForumInfo($forum);
#
# Parameters:
# $forum = abbreviated forum identifier
#
# Output:
# Returns an array of the forum name and forum
# directory.
#
############################################################
sub GetForumInfo {
local($forum) = @_;
local($forumDir, $forumName, $forumUniqueHTML, $forumModerate);
if (exists($forums{$forum})) {
($forumDir, $forumName, $forumUniqueHTML, $forumModerate) = split(/\|\|/,$forums{$forum});
if ($forumUniqueHTML eq "no") {$forumUniqueHTML = "";}
}
else {
$forumName="";
$forumDir = "";
if ($forum eq "") {$forum = "Forum not entered";}
$error = "Forum '$forum' not found ";
$printHiddenFields = &HiddenFields;
&PrintBoardsPage;
exit;
}
($forumName, $forumDir, $forumUniqueHTML, $forumModerate);
}
############################################################
#
# subroutine: expireMessages
# Usage:
# &expireMessages($forumDir, *files);
#
# Parameters:
# $forumDir = directory of forum
# *files = filename list in forum
#
# Output:
# Unlinks (deletes) messages and attachments in the
# forum directory based on age or sequence number
# of the post.
#
############################################################
sub expireMessages {
local($forumDir, *files) = @_;
local($x);
local($prunefile, $attachfile, $attachfile2);
for ($x = @files; $x >= 1; $x--) {
$prunefile = "$forumDir/$files[$x - 1]";
$attachfile = "$forumDir/" . substr($files[$x - 1],0,14) . "attach";
$attachfile2 = "$attachDir/" . "$forum-" . substr($files[$x - 1],0,14) . "bin";
if ((-M "$prunefile" > $pruneHowManyDays) && ($pruneHowManyDays > 0)) {
unlink("$prunefile");
unlink($attachfile);
unlink($attachfile2);
&RemoveElement(*files, $x - 1);
next;
}
if (($x <= (@files - $pruneHowManySequences)) && ($pruneHowManySequences != 0)) {
unlink("$prunefile");
unlink($attachfile);
unlink($attachfile2);
&RemoveElement(*files, $x - 1);
next;
}
}
}
############################################################
#
# subroutine: HiddenFields
# Usage:
# &HiddenFields;
#
# Parameters:
# None.
#
# Output:
# Returns a buffer containing the HTML code for
# hidden fields that should be passed from screen to
# screen in the BBS Forum.
#
############################################################
sub HiddenFields {
local ($buf);
local ($h);
$h = qq^ \n^;
if ($iniFile ne "") {$buf .= qq^$h="ini" value="$iniFile">\n^;}
$buf .= qq^$h="session" value="$session">\n^;
if ($beginDate ne "") {$buf .= qq^$h="begindate" value="$beginDate">\n^;}
if ($endDate ne "") {$buf .= qq^$h="enddate" value="$endDate">\n^;}
if ($youngestAge ne "") {$buf .= qq^$h="youngestage" value="$youngestAge">\n^;}
if ($oldestAge ne "") {$buf .= qq^$h="oldestage" value="$oldestAge">\n^;}
if ($keywords ne "") {$buf .= qq^$h="keywords" value="$keywords">\n^;}
if ($exactMatch ne "") {$buf .= qq^$h="exactmatch" value="$exactMatch">\n^;}
if ($useLastRead eq "on") {$buf .= qq^$h="uselastread" value="$useLastRead">\n^;}
if ($lastRead ne "") {$buf .= qq^$h="lastread" value="$lastRead">\n^;}
if ($rangefrom ne "") {$buf .= qq^$h="rangefrom" value="$rangefrom">\n^;}
if ($rangeby ne "") {$buf .= qq^$h="rangeby" value="$rangeby">\n^;}
if ($digest eq "on") {$buf .= qq^$h="digest" value="$digest">\n^;}
$buf;
}
############################################################
#
# subroutine: ReadMessageFields
# Usage:
# &ReadMessageFields;
#
# Parameters:
# None.
#
# Output:
# Returns a buffer containing the URL code for
# fields that should be passed from screen to screen
# in the BBS program
#
############################################################
sub ReadMessageFields {
local ($buf);
local ($h);
$buf = qq^session=$session&^;
if ($iniFile ne "") {$buf .= qq^ini=$iniFile&^;}
if ($beginDate ne "") {$buf .= qq^begindate=$beginDate&^;}
if ($endDate ne "") {$buf .= qq^enddate=$endDate&^;}
if ($youngestAge ne "") {$buf .= qq^youngestage=$youngestAge&^;}
if ($oldestAge ne "") {$buf .= qq^oldestage=$oldestAge&^;}
if ($keywords ne "") {$buf .= qq^keywords=$keywords&^;}
if ($exactMatch ne "") {$buf .= qq^exactmatch=$exactMatch&^;}
if ($useLastRead eq "on") {$buf .= qq^uselastread=$useLastRead&^;}
if ($lastRead ne "") {$buf .= qq^lastread=$lastRead&^;}
if ($rangefrom ne "") {$buf .= qq^rangefrom=$rangefrom&^;}
if ($rangeby ne "") {$buf .= qq^rangeby=$rangeby&^;}
if ($digest eq "on") {$buf .= qq^digest=$digest&^;}
$buf =~ s/ /%20/;
$buf =~ s/\//%2F/;
chop($buf);
$buf;
}
###########################################################################
# get the size of the image
###########################################################################
sub getImageSize {
local ($file) = @_;
$UseNewGifSize = 0;
if (-e $file && open(STRM, "<$file")) {
binmode(STRM);
if ($file =~ /\.jpg$/i) {($x,$y) = &jpegsize(\*STRM);}
elsif ($file =~ /\.gif$/i) {($x,$y) = &gifsize(\*STRM);}
elsif ($file =~ /\.xbm$/i) {($x,$y) = &xbmsize(\*STRM);}
elsif ($file =~ /\.png$/i) {($x,$y) = &pngsize(\*STRM);}
else {$x = 10; $y = 10;}
close(STRM);
}
return($x,$y);
}
###########################################################################
# Subroutine gets the size of the specified GIF
###########################################################################
sub gifsize {
my($GIF) = @_;
if ($UseNewGifsize) {return &NEWgifsize($GIF);}
else {return &OLDgifsize($GIF);}
}
sub OLDgifsize {
my($GIF) = @_;
my($type,$a,$b,$c,$d,$s) = (0,0,0,0,0,0);
if (read($GIF, $type, 6) && $type =~ /GIF8[7,9]a/ && read($GIF, $s, 4) == 4 ) {
($a,$b,$c,$d) = unpack("C"x4,$s);
return ($b<<8|$a,$d<<8|$c);
}
return (0,0);
}
# part of NEWgifsize
sub gif_blockskip {
my ($GIF, $skip, $type) = @_;
my ($s) = 0;
my ($dummy) = "";
read ($GIF, $dummy, $skip); # Skip header (if any)
while (1) {
if (eof ($GIF)) {
warn "Invalid/Corrupted GIF (at EOF in GIF $type)\n";
return "";
}
read($GIF, $s, 1); # Block size
last if ord($s) == 0; # Block terminator
read ($GIF, $dummy, ord($s)); # Skip data
}
}
# this code by "Daniel V. Klein"
sub NEWgifsize {
my($GIF) = @_;
my($cmapsize, $a, $b, $c, $d, $e) = 0;
my($type,$s) = (0,0);
my($x,$y) = (0,0);
my($dummy) = "";
return($x,$y) if(!defined $GIF);
read($GIF, $type, 6);
if($type !~ /GIF8[7,9]a/ || read($GIF, $s, 7) != 7 ){
warn "Invalid/Corrupted GIF (bad header)\n";
return($x,$y);
}
($e) = unpack("x4 C",$s);
if ($e & 0x80) {
$cmapsize = 3 * 2**(($e & 0x07) + 1);
if (!read($GIF, $dummy, $cmapsize)) {
warn "Invalid/Corrupted GIF (global color map too small?)\n";
return($x,$y);
}
}
FINDIMAGE:
while (1) {
if (eof ($GIF)) {
warn "Invalid/Corrupted GIF (at EOF w/o Image Descriptors)\n";
return($x,$y);
}
read($GIF, $s, 1);
($e) = unpack("C", $s);
if ($e == 0x2c) {
if (read($GIF, $s, 8) != 8) {
warn "Invalid/Corrupted GIF (missing image header?)\n";
return($x,$y);
}
($a,$b,$c,$d)=unpack("x4 C4",$s);
$x=$b<<8|$a;
$y=$d<<8|$c;
return($x,$y);
}
if ($type eq "GIF89a") {
if ($e == 0x21) {
read($GIF, $s, 1);
($e) = unpack("C", $s);
if ($e == 0xF9) {
read($GIF, $dummy, 6);
next FINDIMAGE;
}
elsif ($e == 0xFE) {
&gif_blockskip ($GIF, 0, "Comment");
next FINDIMAGE;
}
elsif ($e == 0x01) {
&gif_blockskip ($GIF, 12, "text data");
next FINDIMAGE;
}
elsif ($e == 0xFF) {
&gif_blockskip ($GIF, 11, "application data");
next FINDIMAGE;
}
else {
printf STDERR "Invalid/Corrupted GIF (Unknown extension %#x)\n", $e;
return($x,$y);
}
}
else {
printf STDERR "Invalid/Corrupted GIF (Unknown code %#x)\n", $e;
return($x,$y);
}
}
else {
warn "Invalid/Corrupted GIF (missing GIF87a Image Descriptor)\n";
return($x,$y);
}
}
}
sub xbmsize {
my($XBM) = @_;
my($input)="";
if (defined($XBM)) {
$input .= <$XBM>;
$input .= <$XBM>;
$input .= <$XBM>;
$_ = $input;
if (/.define\s+\S+\s+(\d+)\s*\n.define\s+\S+\s+(\d+)\s*\n/i) {return ($1,$2);}
}
return (0,0);
}
# pngsize : gets the width & height (in pixels) of a png file
sub pngsize {
my($PNG) = @_;
my($head) = "";
my($a, $b, $c, $d, $e, $f, $g, $h)=0;
if(read( $PNG, $head, 8 ) == 8 && $head eq "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a" && read($PNG, $head, 4) == 4 && read($PNG, $head, 4) == 4 && $head eq "IHDR" && read($PNG, $head, 8) == 8) {
($a,$b,$c,$d,$e,$f,$g,$h) = unpack("C"x8,$head);
return ($a<<24|$b<<16|$c<<8|$d, $e<<24|$f<<16|$g<<8|$h);
}
return (0,0);
}
# jpegsize : gets the width and height (in pixels) of a jpeg file
# Andrew Tong, werdna@ugcs.caltech.edu February 14, 1995
# modified slightly by alex@ed.ac.uk
sub jpegsize {
my($JPEG) = @_;
my($done)=0;
my($c1,$c2,$ch,$s,$length, $dummy)=(0,0,0,0,0,0);
my($a,$b,$c,$d);
if(read($JPEG, $c1, 1) && read($JPEG, $c2, 1) && ord($c1) == 0xFF && ord($c2) == 0xD8) {
while (ord($ch) != 0xDA && !$done) {
while (ord($ch) != 0xFF) {return(0,0) unless read($JPEG, $ch, 1);}
while (ord($ch) == 0xFF) {return(0,0) unless read($JPEG, $ch, 1); }
if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) {
return(0,0) unless read ($JPEG, $dummy, 3);
return(0,0) unless read($JPEG, $s, 4);
($a,$b,$c,$d)=unpack("C"x4,$s);
return ($c<<8|$d, $a<<8|$b );
}
else {
return(0,0) unless read ($JPEG, $s, 2);
($c1, $c2) = unpack("C"x2,$s);
$length = $c1<<8|$c2;
last if (!defined($length) || $length < 2);
read($JPEG, $dummy, $length-2);
}
}
}
return (0,0);
}