#!/usr/local/bin/perl
# visit the working demo at
# http://www.cifnet.com/~lwebzem/msgbrd/test.html
use Time::Local;
use cgilib;
print "Content-type: text/html\n\n";
&cgilib::ReadParse (*data);
use HTML::Template;
# max number of chars when all msgs are displayed
$MAX1 = 100;
$dir = $data{dir};
print_header();
$msg_file=$dir."\/m.txt";
my $action = $data{action};
my $n_msg = $data{n_msg};
$id = $data{id};
$terms_input=$data{search_terms};
$search_mode=$data{search_mode};
$case_s=$data{case_s};
my $te = $action.".tmpl";
if ($action eq "view_prev_msgs" || $action eq "view_all_rep" || $action eq "Search")
{
$te = "main11.tmpl";
}
$te= "templates\/".$te;
use constant TMPL_FILE => $te;
do_action();
my $tmpl= new HTML::Template (filename => $te);
$tmpl->param (msgs => \@msgs );
print $tmpl->output;
sub parse_msgs
{
my $file = shift;
my $msgs_to_show=shift;
my $paragraph;
print "
";
print "Post New Msg";
print "";
open (FH,$file );
my $t=":";
my $count_msg=0;
while ($line =)
{ if ($line ne "**LS**LS**LS**\n")
{ $paragraph .=$line;
next; }
if ($line eq "**LS**LS**LS**\n") {
my ($subject, $msg_body, $date, $author, $id )=split ($t, $paragraph);
$paragraph ="";
if ($msgs_to_show eq "" || (index($msgs_to_show." ", " ".$id." ") >= 0) )
{
$msg_body= substr($msg_body, 0, $MAX1);
$msg_body =~ s/\|/:/g;
$count_msg++;
if ($count_msg % 2)
{
$row_class="class1";
}
else { $row_class="class2"; }
my %msg=(subject => $subject,
msg_body=> $msg_body,
date => $date,
author => $author,
dir => $dir,
id => $id,
cl => $row_class);
push @msgs, \%msg;
} #end if
} #end if
} #end while
close (FH);
return reverse (@msgs);
}
sub get_msg
{
# ACTION view
my $file =shift;
my $m_id = shift;
open (FH, $file);
$t=":";
while ($line =)
{
if ($line ne "**LS**LS**LS**\n")
{ $paragraph .=$line;
next; }
if ($line eq "**LS**LS**LS**\n") {
@prodata=split ($t, $paragraph);
$paragraph ="";
if (($data {'id'} eq $prodata[4]) )
{
$subject=$prodata[0];
$msg_body=$prodata[1];
$date=$prodata[2];
$author=$prodata[3];
$id=$prodata[4];
$msg_body =~ s/\|/:/g;
# first msg id is 1
my $prev= $id-1;
my $next= $id+1;
$insert="";
if ($prev >0)
{
$insert= "Prev";
}
# now need get max N for next
if ($next <= get_msgs_number() )
{
$insert=$insert."\|Next";
}
my %msg=(subject => $subject,
msg_body => $msg_body ,
date => $date,
author => $author,
insert => $insert,
id => $id,
dir => $dir);
push @msgs, \%msg;
}
} # line eq LS
}
close (FH);
return @msgs;
}
sub do_action
{
if ($action eq "main11")
{
@msgs=parse_msgs ($msg_file, "");
}
if ($action eq "view_prev_msgs")
{
@msgs=parse_msgs ($msg_file, view_prev_msgs ($n_msg));
}
if ($action eq "Search")
{
@msgs=search_msgs ($msg_file, $terms_input, $search_mode, $case_s);
}
if ($action eq "view_all_rep")
{
@msgs=parse_msgs ($msg_file, view_all_rep ($n_msg));
}
if ($action eq "view")
{
@msgs=get_msg ($msg_file, $id);
}
if ($action eq "edit_msg")
{
@msgs=get_msg_ed ($msg_file, $id);
}
if ($action eq "submit")
{
@msgs=submit();
}
}
sub get_msg_ed
{
my $file =shift;
my $m_id = shift;
open (FH, $file);
$t=":";
while ($line =)
{
if ($line ne "**LS**LS**LS**\n")
{ $paragraph .=$line;
next; }
if ($line eq "**LS**LS**LS**\n") {
@prodata=split ($t, $paragraph);
$paragraph ="";
if (($data {'id'} eq $prodata[4]) )
{
$subject=$prodata[0];
if (substr($subject, 0, 3) ne "Re-")
{
$subject="Re-".$subject;
}
$msg_body="";
$date=$prodata[2];
$author= "";
$id=$prodata[4];
}
}
} # while
if ( $data{'submit'} eq "New Msg")
{
$id= "";
$subject =" ";
$msg_body="";
}
my %msg=(subject => $subject,
msg_body => $msg_body ,
dir =>$dir,
Rep_to_id =>$id);
push @msgs, \%msg;
close (FH);
return @msgs;
}
sub submit
{
# Save to separate file user_urls
foreach $key (keys %data)
{
$data{$key} =~ s/:/|/g;
}
#get last msg # will be sub
# +1
$data{'id'}= get_msgs_number() +1;
FormToFile ($msg_file, %data);
$subject= $data{'subject'};
$msg_body= $data{'msg_body'};
my %msg=(subject => $subject,
msg_body => $msg_body ,
Rep_to_id =>$id);
push @msgs, \%msg;
$Reply= $data{'Rep_to_id'};
if ( $data{'submit'} eq "New Msg")
{
$Reply= "";
}
update_index( $data{'id'}, $Reply);
print "Thanks for using our service
";
print "Back to all msgs";
return @msgs;
}
sub get_msgs_number
{
$temp_file= $dir."\/data.txt";
open (F , $temp_file);
@lines=;
$lines=@lines;
close (F);
return $lines;
}
sub FormToFile
{
my ($filename , %data) = @_;
# create file, if not yet existing
do {open(FH, ">$filename");
close(FH)}
unless -f $filename;
#open for read/write access
if (!open(FH, "+<$filename")) {
$ERROR= "Cannot open $filename (internal error).";
return 0;
}
# protect agaist parallel access
flock(FH, LOCK_EX);
seek(FH, 0 , 2); #append record to end of file
@k=( "subject" , "msg_body", "date" , "author", "id");
if ($data{'author'} eq "")
{
$data{'author'}="No Author";
}
if ($data{'msg_body'} eq "")
{
$data{'msg_body'} =" No message";
}
if ($data{'subject'} eq "")
{
$data{'subject'}="No subject";
}
$data{'msg_body'} = adjust_text ( $data{'msg_body'}, 20);
$data{'author'} = adjust_text ( $data{'author'}, 10);
$data{'subject'} = adjust_text ( $data{'subject'}, 10);
($sec,$min,$hour,$mday, $mon,$year, $wday, $yday, $isdst)=localtime(time);
$mon=$mon+1;
$year= substr ($year,1 );
my $date_posting= $mon."/".$mday."/".$year;
$data { 'date'} = $date_posting;
foreach $key (@k)
{
print (FH $data{$key});
print (FH ":");
}
print (FH "\n");
print (FH "**LS**LS**LS**");
print (FH "\n");
close (FH);
}
sub update_index
{
my ($id , $rep) = @_;
$index_file=$dir."\/data.txt";
# create file, if not yet existing
do {open(FH, ">$index_file");
close(FH)}
unless -f $index_file;
#open for read/write access
if (!open(FH, "+<$index_file")) {
$ERROR= "Cannot open $index_file (internal error).";
return 0;
}
# protect agaist parallel access
flock(FH, LOCK_EX);
seek(FH, 0 , 2); #append record to end of file
print (FH $id);
print (FH ":");
print (FH $rep);
print (FH "\n");
close (FH);
}
sub print_header
{
$dir= "Msgbrd1";
my $te_h= $dir."\/templates\/header.tmpl";
use constant TMPL_FILE_Header => $te_h;
my $tmpl= new HTML::Template (filename => $te_h);
print $tmpl->output;
}
sub view_all_rep
{
#displays all replies to given msg $rep_m
my $rep_m = shift;
$temp_file= $dir."\/data.txt";
$all_rep=" ";
open (F , $temp_file);
while ($line =)
{
if ($line =~ /(.*):$rep_m$/)
{
$all_rep=$all_rep." ".$1;
}
}
close (F);
if ( $all_rep eq " " )
{
print " No replies are found";
}
return $all_rep;
}
sub view_prev_msgs
{
#displays prev. msgs linked to one msg $msg_top
# 2:1 ex: for msg 4 it will be 4->3->2->1
# 3:2
# 4:3
my $msg_top = shift;
$temp_file= $dir."\/data.txt";
$all_rep=" ".$msg_top;
my $temp=1;
while ($temp==1)
{
open (F , $temp_file);
$temp=0;
while ($line =)
{
if ($line =~ /^$msg_top:(.*)$/)
{
$f_msg=$1;
$all_rep=$all_rep." ".$f_msg;
$temp=1;
$msg_top=$f_msg;
# close(F);
}
}
close (F);
}
return $all_rep;
}
sub search_msgs
{
my $file = shift;
my $search_terms=shift;
my $search_mode1=shift;
my $paragraph;
print "";
print "Post New Msg";
print "";
open (FH,$file );
my $t=":";
my $count_msg=0;
while ($line =)
{ if ($line ne "**LS**LS**LS**\n")
{ $paragraph .=$line;
next; }
if ($line eq "**LS**LS**LS**\n") {
my ($subject, $msg_body, $date, $author, $id )=split ($t, $paragraph);
$paragraph ="";
if ( check_msg ($msg_body, $search_terms, $search_mode1) == 1 )
{
$msg_body= substr($msg_body, 0, $MAX1);
$msg_body =~ s/\|/:/g;
$count_msg++;
if ($count_msg % 2)
{
$row_class="class1";
}
else { $row_class="class2"; }
my %msg=(subject => $subject,
msg_body=> $msg_body,
date => $date,
author => $author,
dir => $dir,
id => $id,
cl => $row_class);
push @msgs, \%msg;
} #end if
} #end if
} #end while
close (FH);
if ( $count_msg ==0 )
{
print "Sorry, no results are found for ".$search_terms."" ;
}
return reverse (@msgs);
}
sub check_msg
{
my $ss=shift;
my $term=shift;
my $s_option=shift;
if ( $s_option eq "Phrase" )
{
if ( found($term, $ss)==1 )
{
return 1;
}
else { return 0;}
}
@terms= split (" ", $term);
my $res1=0;
my $res2=1;
foreach $t_term1 (@terms)
{
$res1=$res1 + found($t_term1, $ss);
$res2=$res2 * found($t_term1, $ss);
}
if ( $s_option eq "All" )
{
if ( $res2==1)
{ return 1;
}
else
{ return 0; }
}
if ( $s_option eq "Any" )
{
if ($res1 > 0 )
{ return 1;
}
else
{ return 0; }
}
}
sub found
{
my ($t_term) = shift;
my ($s_string) = shift;
if ($case_opt == 1)
{ if ($s_string =~ m/$t_term/g)
{ return 1;}
else
{ return 0;}
}
else
{ if ($s_string =~ m/$t_term/ig)
{ return 1;}
else
{ return 0;}
}
}
sub adjust_text
{
my $text_to_adjust=shift;
my $MAX_word_chs= shift;
if ((length($text_to_adjust) > $MAX_word_chs+5 ) && (index($text_to_adjust, " ", 1) < 0))
{
$ll=length($text_to_adjust);
for( $i=1; $i<$ll; $i++)
{
if (($i % $MAX_word_chs) == 0)
{
$text_to_adjust=substr($text_to_adjust, 0, $i)." ".substr($text_to_adjust,$i);
}
}
}
return $text_to_adjust;
}