source: trunk/dnsbl/browse.cgi@ 40

Last change on this file since 40 was 40, checked in by Kris Deugau, 12 years ago

/trunk/dnsbl

Minor cleanups to prepare for semirelease
GPL-tag executables and Perl module from Makefile MANIFEST

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 6.1 KB
Line 
1#!/usr/bin/perl
2# quickndirty browse-the-damned-by-web
3##
4# $Id: browse.cgi 40 2012-03-04 20:02:13Z kdeugau $
5# Copyright 2009-2011 Kris Deugau <kdeugau@deepnet.cx>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19##
20
21use strict;
22use warnings;
23use DBI;
24use CGI::Carp qw(fatalsToBrowser);
25use HTML::Template;
26
27use DNSBL;
28
29my $dnsbl = new DNSBL;
30
31# default DB info - all other settings should be loaded from the DB.
32my $dbhost = "localhost";
33my $dbname = "dnsbl";
34my $dbuser = "dnsbl";
35my $dbpass = "spambgone";
36
37# Load a config ref containing DB host, name, user, and pass info based on
38# from the server name + full script web path. This allows us to host
39# multiple instances without having to duplicate the code.
40# This file is a Perl fragment to be processed inline.
41my $cfgname = $ENV{SERVER_NAME}.$ENV{SCRIPT_NAME};
42$cfgname =~ s|[./-]|_|g;
43$cfgname =~ s|_browse_cgi||;
44if (-e "/etc/dnsbl/$cfgname.conf") {
45 my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
46 ($cfg) = ($cfg =~ /^(.+)$/s); # avoid warnings, failures, and general nastiness with taint mode
47 eval $cfg;
48}
49
50my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
51
52print "Content-Type: text/html\n\n";
53
54my $templatedir = $ENV{SCRIPT_FILENAME};
55$templatedir =~ s/browse\.cgi//;
56$templatedir .= "templates";
57$ENV{HTML_TEMPLATE_ROOT} = $templatedir;
58
59my %config;
60my $sth = $dbh->prepare("SELECT key,value FROM misc");
61$sth->execute;
62while (my ($key,$value) = $sth->fetchrow_array) {
63 $config{$key} = $value;
64}
65
66my $template = HTML::Template->new(filename => "browse.tmpl");
67
68$template->param(pgtitle => $config{pgtitle}) if defined($config{pgtitle});
69$template->param(pgcomment => $config{pgcomment}) if defined($config{pgcomment});
70
71my $basesql = "SELECT b.block,o.orgname,b.listme,o.listme,b.comments,o.comments ".
72 "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ".
73 "WHERE b.parent = ";
74my $sth0 = $dbh->prepare($basesql."'0/0' AND b.level=0 ORDER BY block");
75#my $sth0 = $dbh->prepare($basesql."'64/8' AND b.level=0 ORDER BY block");
76my $sth1 = $dbh->prepare($basesql."? AND b.level=1 ORDER BY block");
77my $sth2 = $dbh->prepare($basesql."? AND b.level=2 ORDER BY block");
78my $sthiplist = $dbh->prepare("select * from iplist where parent = ? order by ip");
79
80my %ipseen;
81
82my $out = '';
83
84$sth0->execute;
85while (my ($block0,$org0,$listmeb0,$listmeo0,$bcomments0,$ocomments0) = $sth0->fetchrow_array) {
86 my $tmpl0 = new HTML::Template(filename => "browse-block.tmpl");
87 $tmpl0->param(lvlclass => 'lvl0'.($dnsbl->autolist_block($block0) ? ' auto0' : ''));
88 $tmpl0->param(netclass => ($listmeb0 ? 'b0list' : ''));
89 $tmpl0->param(net => $block0);
90 $tmpl0->param(orgclass => ($listmeo0 ? 'b0org' : ''));
91 $tmpl0->param(org => $org0);
92 $tmpl0->param(bcomment => $bcomments0) if $bcomments0;
93 $tmpl0->param(ocomment => $ocomments0) if $ocomments0;
94 $sth1->execute($block0);
95 my $lvl1out = '';
96 if ($sth1->rows > 0) {
97 while (my ($block1,$org1,$listmeb1,$listmeo1,$bcomments1,$ocomments1) = $sth1->fetchrow_array) {
98 my $tmpl1 = new HTML::Template(filename => "browse-block.tmpl");
99 $tmpl1->param(lvlclass => 'lvl1'.($dnsbl->autolist_block($block1) ? ' auto1' : ''));
100 $tmpl1->param(netclass => ($listmeb1 ? 'b1list' : ''));
101 $tmpl1->param(net => $block1);
102 $tmpl1->param(orgclass => ($listmeo1 ? 'b1org' : ''));
103 $tmpl1->param(org => $org1);
104 $tmpl1->param(bcomment => $bcomments1) if $bcomments1;
105 $tmpl1->param(ocomment => $ocomments1) if $ocomments1;
106 $tmpl1->param(indent => ' ');
107 my $lvl2out = '';
108 $sth2->execute($block1);
109 if ($sth2->rows > 0) {
110 while (my ($block2,$org2,$listmeb2,$listmeo2,$bcomments2,$ocomments2) = $sth2->fetchrow_array) {
111 my $tmpl2 = new HTML::Template(filename => "browse-block.tmpl");
112 $tmpl2->param(lvlclass => 'lvl2'.($dnsbl->autolist_block($block2) ? ' auto2' : ''));
113 $tmpl2->param(netclass => ($listmeb2 ? 'b2list' : ''));
114 $tmpl2->param(net => $block2);
115 $tmpl2->param(orgclass => ($listmeo2 ? 'b2org' : ''));
116 $tmpl2->param(org => $org2);
117 $tmpl2->param(bcomment => $bcomments2) if $bcomments2;
118 $tmpl2->param(ocomment => $ocomments2) if $ocomments2;
119 $tmpl2->param(indent => ' ');
120 $sthiplist->execute($block2);
121 my @iprows;
122 while (my @data4 = $sthiplist->fetchrow_array) {
123 my %iprow;
124 $iprow{ip} = $data4[0];
125 $iprow{ipcount} = $data4[1];
126 $iprow{indent} = ' ';
127# ip | count | s4list | added
128 push @iprows, \%iprow;
129 $ipseen{$data4[0]} = 1;
130 }
131 $tmpl2->param(iplist => \@iprows);
132 $lvl2out .= $tmpl2->output;
133 }
134 }
135
136 $sthiplist->execute($block1);
137 my @iprows;
138 while (my @data4 = $sthiplist->fetchrow_array) {
139 next if $ipseen{$data4[0]};
140 my %iprow;
141 $iprow{ip} = $data4[0];
142 $iprow{ipcount} = $data4[1];
143 $iprow{indent} = ' ';
144# ip | count | s4list | added
145 push @iprows, \%iprow;
146 $ipseen{$data4[0]} = 1;
147 }
148 $tmpl1->param(iplist => \@iprows);
149 $tmpl1->param(subs => $lvl2out);
150 $lvl1out .= $tmpl1->output;
151 }
152 } # sth1->rows
153 $sthiplist->execute($block0);
154 my @iprows;
155 while (my @data4 = $sthiplist->fetchrow_array) {
156 next if $ipseen{$data4[0]};
157 my %iprow;
158 $iprow{ip} = $data4[0];
159 $iprow{ipcount} = $data4[1];
160 $iprow{indent} = '';
161# ip | count | s4list | added
162 push @iprows, \%iprow;
163 $ipseen{$data4[0]} = 1;
164 }
165 $tmpl0->param(iplist => \@iprows);
166 $tmpl0->param(subs => $lvl1out);
167 $out .= $tmpl0->output;
168}
169
170# probably more efficient ways to do this somewhere...
171$template->param(enchilada => $out);
172print $template->output;
Note: See TracBrowser for help on using the repository browser.