Okay, after putting all the bits and pieces togather, I 
ended up this piece of code.

Its working fine with me and I have no problems in it.




#############################################################

#!/usr/bin/perl

use strict;
use warnings;

use CGI::Carp 'fatalsToBrowser';

use CGI qw(:standard);

my $q = new CGI;

my $ipban = $q->param('ipban') || '0.0.0.0';

my $action = $q->param('action') || 'unban';


if ($action eq "ban") {
&ban_ip;
}
elsif ($action eq "unban") {
&unban_ip;
}
else {
&make_form;
}


sub ban_ip {
open (IP, ">>ipban.txt");
print IP "$ipban\n";
close (IP);
print header,                   
         start_html('Success IP banned'),
         h1('The IP has banned Successfully'),        
            ($ipban),
            end_html;   
&make_form;               
}

sub unban_ip {
my $infile;
my $outfile;
my $line;
$infile="ipban.txt";
$outfile="temp.txt";
open (IN, "$infile");
open (OUT, ">$outfile");
my @temp;
@temp=<IN>;
foreach $line (@temp) {
if($line !~ m/$ipban/i)
{
print OUT $line;
}
}
close (IN);
close (OUT);

unlink ($infile);

rename ($outfile, $infile);

print header,                    
         start_html('Unban Success'), 
         h1('The IP was Unbanned successfully'),             
             $ipban,
         end_html;
&make_form;              

}

sub make_form {
my @form;
open (FORM, 'ipban.txt');
@form = <FORM>;
chomp @form;
close (FORM);
print start_form (-method => 'Post', 
               -Action => 'ipban.pl'),

scrolling_list( -name           => "ipban",
                        -values         => [EMAIL PROTECTED],
                        -size           => '10',
                        -multiple       => 'false',
                        -style      => 'Color: blue; font-
Size: 12pt;' ),

submit (-name => 'Unban Selected IP'),
                        
                        
end_form,;
                          
}





#########################################################


Okay this script is actually made for the IP banning to prevent the 
posting of messages on forum. 

Every forum message will be containing a link at the bottom, which 
will lead to an .htaccess protected directory and moderators will 
give the passwords to ban the users, if they find the post 
inappropriate.

http://yourserver.com/cgi-bin/ipban.pl?action=ban&ban=23.123.123.43

this will invoke the above script and IP would be banned.

Then when user come back to re-post again. The forum script contains 
a subroutine to check the ipban.txt, if the IP is banned, then the 
user will get a message that there IP is banned and they cannot post 
further.

########### Forum IP checking Subroutine ############
open(IP, '/home/ipban.txt');
foreach $line (<IP>) {
      chomp($line);
    next unless $line eq $current_ip;
      &error(bad_ip);
    last;
      }
#####################################################

OKAY NOW WHATS THE CATCH AFTER TALKING SO MUCH.

I want to add the subject line ($subject) of the message posted with 
IP address separated by delimiter (|), okay again not a big deal.

The above 'ipban' subroutine should be changed to:
############################################

sub ban_ip {
open (IP, ">>ipban.txt");
print IP "$ipban | $subject\n";
close (IP);
print header,                   
         start_html('Success IP banned'),
         h1('The IP has banned Successfully'),        
            ($ipban),
            end_html;   
&make_form;               
}

############################################

The above 'unban_ip' subroutine will work fine whatever we add in the 
subject line. Because it has to match the IP and delete the line, 
nothing to do with the additional subject line addition. So it should 
remain unchanged??????

#############UNCHANGED#######################
sub unban_ip {
my $infile;
my $outfile;
my $line;
$infile="ipban.txt";
$outfile="temp.txt";
open (IN, "$infile");
open (OUT, ">$outfile");
my @temp;
@temp=<IN>;
foreach $line (@temp) {
if($line !~ m/$ipban/i)
{
print OUT $line;
}
}
close (IN);
close (OUT);

unlink ($infile);

rename ($outfile, $infile);

print header,                    
         start_html('Unban Success'), 
         h1('The IP was Unbanned successfully'),             
             $ipban,
         end_html;
&make_form;              

}
##################################################


OKAY HERE COMES MY TWO QUESTIONS;

FIRST:

I modified the forum (IP check subroutine to this one after adding 
the subject line and delimiter to ipban.txt), but it's not working?

################ modified Forum IP check subroutine ######

open IP, '/home/ipban.txt';

         foreach $line (<IP>) {
      chomp($line);
    ($ipban,$subject) = split(/\|/,$line);
    next unless $ipban eq $current_ip;
      &error(bad_ip);
    last;
      }
############################################################

SECOND:
I have just started using CGI.pm, its probably my second or third 
script using CGI.pm and I have no idea how to split the IPban.txt 
and put the variables IPs and Subject line like this:

<select name="ipban" size="10">
<option value="$ipban">$ipban | $subject</option>

Currently I am getting the output like this:
<option value="$ipban">$ipban</option>

see the make_form; subroutine above.

Thanks for any input, for integrating the subject line in already 
existing script for banning IPs of BBS.


Thanks,

Sara.









Reply via email to