source: trunk/dnsbl/dnsbl.cgi@ 19

Last change on this file since 19 was 19, checked in by Kris Deugau, 14 years ago

/trunk/dnsbl

Update dnsbl.cgi and browse.cgi to display all IPs with count properly,

use HTML::Template for structure instead of hand-spewing HTML

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 6.5 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5no warnings qw(uninitialized);
6use CGI::Carp qw (fatalsToBrowser);
7use CGI::Simple;
8use HTML::Template;
9use DNSBL;
10
11# Set up the CGI object...
12my $q = new CGI::Simple;
13# ... and get query-string params as well as POST params if necessary
14$q->parse_query_string;
15
16my %webvar;
17# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
18foreach ($q->param()) {
19 $webvar{$_} = $q->param($_);
20}
21
22my $dnsbl = new DNSBL;
23
24my $dbh = $dnsbl->connect;
25
26print "Content-type: text/html\n\n";
27
28my $page;
29my $templatedir = "templates";
30
31# decide which page to spit out...
32if (!$webvar{page}) {
33 $page = HTML::Template->new(filename => "$templatedir/index.tmpl");
34} else {
35 $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
36}
37
38if ($webvar{page} eq 'report') {
39 $page->param(ip => $webvar{ip});
40 my $count = $dnsbl->ipexists($webvar{ip});
41 $page->param(nreports => $count) if $count;
42 $page->param(browsebits => browse($dbh,$webvar{ip}));
43 for (my $i=0; $i<3; $i++) {
44 my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
45 if ($block) {
46 $page->param("block$i" => $block);
47 $page->param("org$i" => $org);
48 }
49 }
50} elsif ($webvar{page} eq 'dbreport') {
51 my $err = '';
52 my $count = $dnsbl->report($webvar{ip});
53 my $org0id = $dnsbl->orgexists($webvar{org0});
54 if (!$org0id) {
55 $org0id = $dnsbl->addorg($webvar{org0});
56 $page->param(org0 => $webvar{org0});
57 }
58 if (!$dnsbl->blockexists($webvar{block0})) {
59 my $ret = $dnsbl->addblock($webvar{block0}, $org0id, 0);
60 $err .= "error adding $webvar{block0}: $ret<br>\n" if $ret;
61 $page->param(block0 => $webvar{block0});
62 }
63# yes, this is grotty. PTHBTT!
64 if ($webvar{block1}) {
65 my $org1id = $dnsbl->orgexists($webvar{org1});
66 if (!$org1id) {
67 $org1id = $dnsbl->addorg($webvar{org1});
68 $page->param(org1 => $webvar{org1});
69 }
70 if (!$dnsbl->blockexists($webvar{block1})) {
71 my $ret = $dnsbl->addblock($webvar{block1}, $org1id, 1);
72 $err .= "error adding $webvar{block1}: $ret<br>\n" if $ret;
73 $page->param(block1 => $webvar{block1});
74 }
75 if ($webvar{block2}) {
76 my $org2id = $dnsbl->orgexists($webvar{org2});
77 if (!$org2id) {
78 $org2id = $dnsbl->addorg($webvar{org2});
79 $page->param(org2 => $webvar{org2});
80 }
81 if (!$dnsbl->blockexists($webvar{block2})) {
82 my $ret = $dnsbl->addblock($webvar{block2}, $org2id, 2);
83 $err .= "error adding $webvar{block2}: $ret<br>\n" if $ret;
84 $page->param(block2 => $webvar{block2});
85 }
86 }
87 }
88
89 $page->param(ip => $webvar{ip});
90 $page->param(err => $err);
91}
92print $page->output;
93
94exit 0;
95
96
97
98## extra subs. should probably put this in a module somehow to share with browse.cgi
99
100sub browse {
101 my $dbh = shift;
102 my $ip = shift;
103
104 my $basesql = "SELECT b.block,o.orgname,b.listme,o.listme,b.comments,o.comments ".
105 "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ".
106 "WHERE b.block ";
107
108 my $sth0 = $dbh->prepare($basesql." >> ? AND b.level=0 ORDER BY block");
109 my $sth1 = $dbh->prepare($basesql." <<= ? AND b.level=1 ORDER BY block");
110 my $sth2 = $dbh->prepare($basesql." <<= ? AND b.level=2 ORDER BY block");
111 my $sthiplist = $dbh->prepare("select * from iplist where ip <<= ? order by ip");
112
113 my %ipseen;
114 my $out = '';
115
116 my $tmpl0 = new HTML::Template(filename => 'templates/browse-block.tmpl');
117
118 $sth0->execute($ip);
119 while (my ($block0,$org0,$listmeb0,$listmeo0,$bcomments0,$ocomments0) = $sth0->fetchrow_array) {
120 $tmpl0->param(lvlclass => 'lvl0'.($dnsbl->autolist_block($block0) ? ' auto0' : ''));
121 $tmpl0->param(netclass => ($listmeb0 ? 'b0list' : ''));
122 $tmpl0->param(net => $block0);
123 $tmpl0->param(orgclass => ($listmeo0 ? 'b0org' : ''));
124 $tmpl0->param(org => $org0);
125 $tmpl0->param(bcomment => $bcomments0) if $bcomments0;
126 $tmpl0->param(ocomment => $ocomments0) if $ocomments0;
127 $sth1->execute($block0);
128 my $lvl1out = '';
129 if ($sth1->rows > 0) {
130 while (my ($block1,$org1,$listmeb1,$listmeo1,$bcomments1,$ocomments1) = $sth1->fetchrow_array) {
131 my $tmpl1 = new HTML::Template(filename => 'templates/browse-block.tmpl');
132 $tmpl1->param(lvlclass => 'lvl1'.($dnsbl->autolist_block($block1) ? ' auto1' : ''));
133 $tmpl1->param(netclass => ($listmeb1 ? 'b1list' : ''));
134 $tmpl1->param(net => $block1);
135 $tmpl1->param(orgclass => ($listmeo1 ? 'b1org' : ''));
136 $tmpl1->param(org => $org1);
137 $tmpl1->param(bcomment => $bcomments1) if $bcomments1;
138 $tmpl1->param(ocomment => $ocomments1) if $ocomments1;
139 $tmpl1->param(indent => ' ');
140 my $lvl2out = '';
141 $sth2->execute($block1);
142 if ($sth2->rows > 0) {
143 while (my ($block2,$org2,$listmeb2,$listmeo2,$bcomments2,$ocomments2) = $sth2->fetchrow_array) {
144 my $tmpl2 = new HTML::Template(filename => 'templates/browse-block.tmpl');
145 $tmpl2->param(lvlclass => 'lvl2'.($dnsbl->autolist_block($block2) ? ' auto2' : ''));
146 $tmpl2->param(netclass => ($listmeb2 ? 'b2list' : ''));
147 $tmpl2->param(net => $block2);
148 $tmpl2->param(orgclass => ($listmeo2 ? 'b2org' : ''));
149 $tmpl2->param(org => $org2);
150 $tmpl2->param(bcomment => $bcomments2) if $bcomments2;
151 $tmpl2->param(ocomment => $ocomments2) if $ocomments2;
152 $tmpl2->param(indent => ' ');
153 $sthiplist->execute($block2);
154 my @iprows;
155 while (my @data4 = $sthiplist->fetchrow_array) {
156 my %iprow;
157 $iprow{ip} = $data4[0];
158 $iprow{ipcount} = $data4[1];
159 $iprow{indent} = ' ';
160# ip | count | s4list | added
161 push @iprows, \%iprow;
162 $ipseen{$data4[0]} = 1;
163 }
164 $tmpl2->param(iplist => \@iprows);
165 $lvl2out .= $tmpl2->output;
166 }
167 }
168
169 $sthiplist->execute($block1);
170 my @iprows;
171 while (my @data4 = $sthiplist->fetchrow_array) {
172 next if $ipseen{$data4[0]};
173 my %iprow;
174 $iprow{ip} = $data4[0];
175 $iprow{ipcount} = $data4[1];
176 $iprow{indent} = ' ';
177# ip | count | s4list | added
178 push @iprows, \%iprow;
179 $ipseen{$data4[0]} = 1;
180 }
181 $tmpl1->param(iplist => \@iprows);
182 $tmpl1->param(subs => $lvl2out);
183 $lvl1out .= $tmpl1->output;
184
185 }
186 } # sth1->rows
187 $sthiplist->execute($block0);
188 my @iprows;
189 while (my @data4 = $sthiplist->fetchrow_array) {
190 next if $ipseen{$data4[0]};
191 my %iprow;
192 $iprow{ip} = $data4[0];
193 $iprow{ipcount} = $data4[1];
194 $iprow{indent} = '';
195# ip | count | s4list | added
196 push @iprows, \%iprow;
197 $ipseen{$data4[0]} = 1;
198 }
199 $tmpl0->param(iplist => \@iprows);
200 $tmpl0->param(subs => $lvl1out);
201 }
202
203 return $tmpl0->output;
204} # end browse()
Note: See TracBrowser for help on using the repository browser.