1 | #!/usr/bin/perl
|
---|
2 | # ipdb/cgi-bin/search.cgi
|
---|
3 | # Started splitting search functions (quick and otherwise) from
|
---|
4 | # main IPDB interface 03/11/2005
|
---|
5 | ###
|
---|
6 | # SVN revision info
|
---|
7 | # $Date: 2013-05-15 20:17:00 +0000 (Wed, 15 May 2013) $
|
---|
8 | # SVN revision $Rev: 594 $
|
---|
9 | # Last update by $Author: kdeugau $
|
---|
10 | ###
|
---|
11 | # Copyright 2005-2011 - Kris Deugau
|
---|
12 |
|
---|
13 | use strict;
|
---|
14 | use warnings;
|
---|
15 | use CGI::Carp qw(fatalsToBrowser);
|
---|
16 | use CGI::Simple;
|
---|
17 | use HTML::Template;
|
---|
18 | use DBI;
|
---|
19 | use POSIX qw(ceil);
|
---|
20 | use NetAddr::IP;
|
---|
21 |
|
---|
22 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
23 | ##uselib##
|
---|
24 |
|
---|
25 | use MyIPDB;
|
---|
26 |
|
---|
27 | # Don't formally need a username or syslog here. syslog left active for debugging.
|
---|
28 | use Sys::Syslog;
|
---|
29 | openlog "IPDBsearch","pid","$IPDB::syslog_facility";
|
---|
30 |
|
---|
31 | # ... but we do *use* the username on ACLs now.
|
---|
32 | # Collect the username from HTTP auth. If undefined, we're in
|
---|
33 | # a test environment, or called without a username.
|
---|
34 | my $authuser;
|
---|
35 | if (!defined($ENV{'REMOTE_USER'})) {
|
---|
36 | $authuser = '__temptest';
|
---|
37 | } else {
|
---|
38 | $authuser = $ENV{'REMOTE_USER'};
|
---|
39 | }
|
---|
40 |
|
---|
41 | # Global variables
|
---|
42 | my $RESULTS_PER_PAGE = 25;
|
---|
43 |
|
---|
44 | # anyone got a better name? :P
|
---|
45 | my $thingroot = $ENV{SCRIPT_FILENAME};
|
---|
46 | $thingroot =~ s|cgi-bin/search.cgi||;
|
---|
47 |
|
---|
48 | # Set up the CGI object...
|
---|
49 | my $q = new CGI::Simple;
|
---|
50 | # ... and get query-string params as well as POST params if necessary
|
---|
51 | $q->parse_query_string;
|
---|
52 |
|
---|
53 | # Convenience; saves changing all references to %webvar
|
---|
54 | ##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection)
|
---|
55 | my %webvar = $q->Vars;
|
---|
56 |
|
---|
57 | if (defined($webvar{rpp})) {
|
---|
58 | ($RESULTS_PER_PAGE) = ($webvar{rpp} =~ /(\d+)/);
|
---|
59 | }
|
---|
60 |
|
---|
61 | # Why not a global DB handle? (And a global statement handle, as well...)
|
---|
62 | # Use the connectDB function, otherwise we end up confusing ourselves
|
---|
63 | my $ip_dbh;
|
---|
64 | my $sth;
|
---|
65 | my $errstr;
|
---|
66 | ($ip_dbh,$errstr) = connectDB_My;
|
---|
67 | if ($ip_dbh) {
|
---|
68 | checkDBSanity($ip_dbh);
|
---|
69 | initIPDBGlobals($ip_dbh);
|
---|
70 | }
|
---|
71 |
|
---|
72 | # Set up some globals
|
---|
73 | $ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
|
---|
74 |
|
---|
75 | my $page;
|
---|
76 | if (!defined($webvar{stype})) {
|
---|
77 | $webvar{stype} = "<NULL>"; #shuts up the warnings.
|
---|
78 | $page = HTML::Template->new(filename => "search/compsearch.tmpl");
|
---|
79 | } else {
|
---|
80 | $page = HTML::Template->new(filename => "search/sresults.tmpl");
|
---|
81 | }
|
---|
82 |
|
---|
83 | my $header = HTML::Template->new(filename => "header.tmpl");
|
---|
84 | $header->param(version => $IPDB::VERSION);
|
---|
85 | $header->param(addperm => $IPDBacl{$authuser} =~ /a/);
|
---|
86 | print "Content-type: text/html\n\n", $header->output;
|
---|
87 |
|
---|
88 | # Handle the DB error first
|
---|
89 | if (!$ip_dbh) {
|
---|
90 | $page = HTML::Template->new(filename => "dberr.tmpl");
|
---|
91 | $page->param(errmsg => $errstr);
|
---|
92 | } elsif ($webvar{stype} eq 'q') {
|
---|
93 | # Quick search.
|
---|
94 |
|
---|
95 | if (!$webvar{input}) {
|
---|
96 | # No search term. Display everything.
|
---|
97 | viewBy('all', '');
|
---|
98 | } else {
|
---|
99 | # Search term entered. Display matches.
|
---|
100 | # We should really sanitize $webvar{input}, no?
|
---|
101 | my $searchfor;
|
---|
102 | # Chew up leading and trailing whitespace
|
---|
103 | $webvar{input} =~ s/^\s+//;
|
---|
104 | $webvar{input} =~ s/\s+$//;
|
---|
105 | if ($webvar{input} =~ /^\d+$/) {
|
---|
106 | # All-digits, new custID
|
---|
107 | $searchfor = "cust";
|
---|
108 | } elsif ($webvar{input} =~ /^[\d\.]+(\/\d{1,3})?$/) {
|
---|
109 | # IP addresses should only have numbers, digits, and maybe a slash+netmask
|
---|
110 | $searchfor = "ipblock";
|
---|
111 | } else {
|
---|
112 | # Anything else.
|
---|
113 | $searchfor = "desc";
|
---|
114 | }
|
---|
115 | viewBy($searchfor, $webvar{input});
|
---|
116 | }
|
---|
117 |
|
---|
118 | } elsif ($webvar{stype} eq 'c') {
|
---|
119 | # Complex search.
|
---|
120 |
|
---|
121 | # Several major cases, and a whole raft of individual cases.
|
---|
122 | # -> Show all types means we do not need to limit records retrieved by type
|
---|
123 | # -> Show all cities means we do not need to limit records retrieved by city
|
---|
124 | # Individual cases are for the CIDR/IP, CustID, Description, Notes, and individual type
|
---|
125 | # requests.
|
---|
126 |
|
---|
127 | my $sqlconcat;
|
---|
128 | if ($webvar{which} eq 'all') {
|
---|
129 | # Must match *all* specified criteria. ## use INTERSECT or EXCEPT
|
---|
130 | $sqlconcat = "INTERSECT";
|
---|
131 | } elsif ($webvar{which} eq 'any') {
|
---|
132 | # Match on any specified criteria ## use UNION
|
---|
133 | $sqlconcat = "UNION";
|
---|
134 | } else {
|
---|
135 | # sum-buddy tryn'a game the system. Match "all"
|
---|
136 | $sqlconcat = "INTERSECT";
|
---|
137 | }
|
---|
138 |
|
---|
139 | # We actually construct a monster SQL statement for all criteria.
|
---|
140 | # Iff something has been entered, it will be used as a filter.
|
---|
141 | # Iff something has NOT been entered, we still include it but in
|
---|
142 | # such a way that it does not actually filter anything out.
|
---|
143 |
|
---|
144 | # Columns actually returned. Slightly better than hardcoding it
|
---|
145 | # in each (sub)select
|
---|
146 | my $cols = "cidr,custid,type,city,description";
|
---|
147 |
|
---|
148 | # hack fix for undefined variables
|
---|
149 | $webvar{custid} = '' if !$webvar{custid};
|
---|
150 | $webvar{desc} = '' if !$webvar{desc};
|
---|
151 | $webvar{notes} = '' if !$webvar{notes};
|
---|
152 | $webvar{custexclude} = '' if !$webvar{custexclude};
|
---|
153 | $webvar{descexclude} = '' if !$webvar{descexclude};
|
---|
154 | $webvar{notesexclude} = '' if !$webvar{notesexclude};
|
---|
155 |
|
---|
156 | # First chunk of SQL. Filter on custid, description, and notes as necessary.
|
---|
157 | my $sql = qq(SELECT $cols FROM searchme\n);
|
---|
158 | $sql .= " WHERE $webvar{custexclude} (custid ~ '$webvar{custid}')\n";
|
---|
159 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{descexclude} description ~ '$webvar{desc}')\n";
|
---|
160 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{notesexclude} notes ~ '$webvar{notes}')";
|
---|
161 |
|
---|
162 | # If we're not supposed to search for all types, search for the selected types.
|
---|
163 | $webvar{alltypes} = '' if !$webvar{alltypes};
|
---|
164 | $webvar{typeexclude} = '' if !$webvar{typeexclude};
|
---|
165 | if ($webvar{alltypes} ne 'on') {
|
---|
166 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{typeexclude} type in (";
|
---|
167 | foreach my $key (keys %webvar) {
|
---|
168 | $sql .= "'$1'," if $key =~ /type\[(..)\]/;
|
---|
169 | }
|
---|
170 | chop $sql;
|
---|
171 | $sql .= "))";
|
---|
172 | }
|
---|
173 |
|
---|
174 | # If we're not supposed to search for all cities, search for the selected cities.
|
---|
175 | # This could be vastly improved with proper foreign keys in the database.
|
---|
176 | $webvar{allcities} = '' if !$webvar{allcities};
|
---|
177 | $webvar{cityexclude} = '' if !$webvar{cityexclude};
|
---|
178 | if ($webvar{allcities} ne 'on') {
|
---|
179 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{cityexclude} city in (";
|
---|
180 | $sth = $ip_dbh->prepare("select city from cities where id=?");
|
---|
181 | foreach my $key (keys %webvar) {
|
---|
182 | if ($key =~ /city\[(\d+)\]/) {
|
---|
183 | $sth->execute($1);
|
---|
184 | my $city;
|
---|
185 | $sth->bind_columns(\$city);
|
---|
186 | $sth->fetch;
|
---|
187 | $city =~ s/'/''/;
|
---|
188 | $sql .= "'$city',";
|
---|
189 | }
|
---|
190 | }
|
---|
191 | chop $sql;
|
---|
192 | $sql .= "))";
|
---|
193 | }
|
---|
194 |
|
---|
195 | ## CIDR query options.
|
---|
196 | $webvar{cidr} =~ s/\s+//; # Hates the nasty spaceseseses we does.
|
---|
197 | if ($webvar{cidr} eq '') { # We has a blank CIDR. Ignore it.
|
---|
198 | } elsif ($webvar{cidr} =~ /\//) {
|
---|
199 | # 192.168.179/26 should show all /26 subnets in 192.168.179
|
---|
200 | my ($net,$maskbits) = split /\//, $webvar{cidr};
|
---|
201 | if ($webvar{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
|
---|
202 | # /0->/9 are silly to worry about right now. I don't think
|
---|
203 | # we'll be getting a class A anytime soon. <g>
|
---|
204 | $sql .= " $sqlconcat (select $cols from searchme where ".
|
---|
205 | "$webvar{cidrexclude} cidr<<='$webvar{cidr}')";
|
---|
206 | } else {
|
---|
207 | # Partial match; beginning of subnet and maskbits are provided
|
---|
208 | # Show any blocks with the leading octet(s) and that masklength
|
---|
209 | # Need some more magic for bare /nn searches:
|
---|
210 | my $condition = ($net eq '' ?
|
---|
211 | "masklen(cidr)=$maskbits" : "text(cidr) like '$net%' and masklen(cidr)=$maskbits");
|
---|
212 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{cidrexclude} ".
|
---|
213 | "($condition))";
|
---|
214 | }
|
---|
215 | } elsif ($webvar{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
|
---|
216 | # Specific IP address match. Will show either a single netblock,
|
---|
217 | # or a static pool plus an IP.
|
---|
218 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{cidrexclude} ".
|
---|
219 | "cidr >>= '$webvar{cidr}')";
|
---|
220 | } elsif ($webvar{cidr} =~ /^\d{1,3}(\.(\d{1,3}(\.(\d{1,3}\.?)?)?)?)?$/) {
|
---|
221 | # Leading octets in CIDR
|
---|
222 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{cidrexclude} ".
|
---|
223 | "text(cidr) like '$webvar{cidr}%')";
|
---|
224 | } else {
|
---|
225 | # do nothing.
|
---|
226 | ##fixme we'll ignore this to clear out the references to legacy code.
|
---|
227 | } # done with CIDR query options.
|
---|
228 |
|
---|
229 | # Find the offset for multipage results
|
---|
230 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
231 |
|
---|
232 | # Find out how many rows the "core" query will return.
|
---|
233 | my $count = countRows($sql);
|
---|
234 |
|
---|
235 | if ($count == 0) {
|
---|
236 | $page->param(errmsg => "No matches found. Try eliminating one of the criteria,".
|
---|
237 | " or making one or more criteria more general.");
|
---|
238 | } else {
|
---|
239 | # Add the limit/offset clauses
|
---|
240 | $sql .= " order by cidr";
|
---|
241 | $sql .= " limit $RESULTS_PER_PAGE offset $offset" if $RESULTS_PER_PAGE != 0;
|
---|
242 | # And tell the user.
|
---|
243 | print "<div class=heading>Searching...............</div>\n";
|
---|
244 | queryResults($sql, $webvar{page}, $count);
|
---|
245 | }
|
---|
246 |
|
---|
247 | } elsif ($webvar{stype} eq 'n') {
|
---|
248 | # Node search.
|
---|
249 |
|
---|
250 | my $sql = "SELECT cidr,custid,type,city,description FROM searchme".
|
---|
251 | " WHERE cidr IN (SELECT block FROM noderef WHERE node_id=$webvar{node})";
|
---|
252 |
|
---|
253 | # Find the offset for multipage results
|
---|
254 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
255 |
|
---|
256 | # Find out how many rows the "core" query will return.
|
---|
257 | my $count = countRows($sql);
|
---|
258 |
|
---|
259 | if ($count == 0) {
|
---|
260 | $page->param(errmsg => "No customers currently listed as connected through this node.");
|
---|
261 | ##fixme: still get the results table header
|
---|
262 | } else {
|
---|
263 | # Add the limit/offset clauses
|
---|
264 | $sql .= " order by cidr";
|
---|
265 | $sql .= " limit $RESULTS_PER_PAGE offset $offset" if $RESULTS_PER_PAGE != 0;
|
---|
266 | # And tell the user.
|
---|
267 | print "<div class=heading>Searching...............</div>\n";
|
---|
268 | queryResults($sql, $webvar{page}, $count);
|
---|
269 | }
|
---|
270 |
|
---|
271 | } else { # how script was called. General case is to show the search criteria page.
|
---|
272 |
|
---|
273 | # Generate table of types
|
---|
274 | $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ".
|
---|
275 | "order by listorder");
|
---|
276 | $sth->execute;
|
---|
277 | my $i=0;
|
---|
278 | my @typelist;
|
---|
279 | while (my ($type,$dispname) = $sth->fetchrow_array) {
|
---|
280 | my %row = (
|
---|
281 | newrow => ($i % 4 == 0),
|
---|
282 | type => $type,
|
---|
283 | dispname => $dispname,
|
---|
284 | endrow => ($i++ % 4 == 3)
|
---|
285 | );
|
---|
286 | push @typelist, \%row;
|
---|
287 | }
|
---|
288 | $page->param(typelist => \@typelist);
|
---|
289 |
|
---|
290 | # Generate table of cities
|
---|
291 | $sth = $ip_dbh->prepare("select id,city from cities order by city");
|
---|
292 | $sth->execute;
|
---|
293 | $i=0;
|
---|
294 | my @citylist;
|
---|
295 | while (my ($id, $city) = $sth->fetchrow_array) {
|
---|
296 | my %row = (
|
---|
297 | newrow => ($i % 4 == 0),
|
---|
298 | id => $id,
|
---|
299 | city => $city,
|
---|
300 | endrow => ($i++ % 4 == 3)
|
---|
301 | );
|
---|
302 | push @citylist, \%row;
|
---|
303 | }
|
---|
304 | $page->param(citylist => \@citylist);
|
---|
305 |
|
---|
306 | }
|
---|
307 |
|
---|
308 | print $page->output;
|
---|
309 |
|
---|
310 | # Shut down and clean up.
|
---|
311 | finish($ip_dbh);
|
---|
312 |
|
---|
313 | # We print the footer here, so we don't have to do it elsewhere.
|
---|
314 | my $footer = HTML::Template->new(filename => "footer.tmpl");
|
---|
315 | # include the admin tools link in the output?
|
---|
316 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
|
---|
317 |
|
---|
318 | print $footer->output;
|
---|
319 |
|
---|
320 | # We shouldn't need to directly execute any code below here; it's all subroutines.
|
---|
321 | exit 0;
|
---|
322 |
|
---|
323 |
|
---|
324 | # viewBy()
|
---|
325 | # The quick search
|
---|
326 | # Takes a category descriptor and a query string
|
---|
327 | # Creates appropriate SQL to run the search and display the results
|
---|
328 | # with queryResults()
|
---|
329 | sub viewBy {
|
---|
330 | my ($category,$query) = @_;
|
---|
331 |
|
---|
332 | # Local variables
|
---|
333 | my $sql;
|
---|
334 |
|
---|
335 | # Calculate start point for LIMIT clause
|
---|
336 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
337 |
|
---|
338 | # Possible cases:
|
---|
339 | # 1) Partial IP/subnet. Treated as "octet-prefix".
|
---|
340 | # 2a) CIDR subnet. Exact match.
|
---|
341 | # 2b) CIDR netmask. YMMV but it should be octet-prefix-with-netmask
|
---|
342 | # (ie, all matches with the octet prefix *AND* that netmask)
|
---|
343 | # 3) Customer ID. "Match-any-segment"
|
---|
344 | # 4) Description. "Match-any-segment"
|
---|
345 | # 5) Invalid data which might be interpretable as an IP or something, but
|
---|
346 | # which probably shouldn't be for reasons of sanity.
|
---|
347 |
|
---|
348 | my $cols = "cidr,custid,type,city,description";
|
---|
349 |
|
---|
350 | if ($category eq 'all') {
|
---|
351 |
|
---|
352 | $sql = "select $cols from searchme";
|
---|
353 | my $count = countRows($sql);
|
---|
354 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
|
---|
355 | queryResults($sql, $webvar{page}, $count);
|
---|
356 |
|
---|
357 | } elsif ($category eq 'cust') {
|
---|
358 |
|
---|
359 | ##fixme: this and other quick-search areas; fix up page heading title similar to first grouping above
|
---|
360 | print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);
|
---|
361 |
|
---|
362 | # Query for a customer ID. Note that we can't restrict to "numeric-only"
|
---|
363 | # as we have non-numeric custIDs in the legacy data. :/
|
---|
364 | $sql = "select $cols from searchme where custid ilike '%$query%' or description like '%$query%'";
|
---|
365 | my $count = countRows($sql);
|
---|
366 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
|
---|
367 | queryResults($sql, $webvar{page}, $count);
|
---|
368 |
|
---|
369 | } elsif ($category eq 'desc') {
|
---|
370 |
|
---|
371 | print qq(<div class="heading">Searching for descriptions containing '$query'</div><br>\n);
|
---|
372 | # Query based on description (includes "name" from old DB).
|
---|
373 | $sql = "select $cols from searchme where description ilike '%$query%'".
|
---|
374 | " or custid ilike '%$query%'";
|
---|
375 | my $count = countRows($sql);
|
---|
376 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
|
---|
377 | queryResults($sql, $webvar{page}, $count);
|
---|
378 |
|
---|
379 | } elsif ($category =~ /ipblock/) {
|
---|
380 |
|
---|
381 | # Query is for a partial IP, a CIDR block in some form, or a flat IP.
|
---|
382 | print qq(<div class="heading">Searching for IP-based matches on '$query'</div><br>\n);
|
---|
383 |
|
---|
384 | $query =~ s/\s+//g;
|
---|
385 | if ($query =~ /\//) {
|
---|
386 | # 192.168.179/26 should show all /26 subnets in 192.168.179
|
---|
387 | my ($net,$maskbits) = split /\//, $query;
|
---|
388 | if ($query =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
|
---|
389 | # /0->/9 are silly to worry about right now. I don't think
|
---|
390 | # we'll be getting a class A anytime soon. <g>
|
---|
391 | $sql = "select $cols from searchme where cidr='$query'";
|
---|
392 | queryResults($sql, $webvar{page}, 1);
|
---|
393 | } else {
|
---|
394 | #print "Finding all blocks with netmask /$maskbits, leading octet(s) $net<br>\n";
|
---|
395 | # Partial match; beginning of subnet and maskbits are provided
|
---|
396 | $sql = "select $cols from searchme where text(cidr) like '$net%' and ".
|
---|
397 | "text(cidr) like '%$maskbits'";
|
---|
398 | my $count = countRows($sql);
|
---|
399 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
|
---|
400 | queryResults($sql, $webvar{page}, $count);
|
---|
401 | }
|
---|
402 | } elsif ($query =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
|
---|
403 | # Specific IP address match
|
---|
404 | #print "4-octet pattern found; finding netblock containing IP $query<br>\n";
|
---|
405 | my ($net,$ip) = ($query =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/);
|
---|
406 | my $sfor = new NetAddr::IP $query;
|
---|
407 | $sth = $ip_dbh->prepare("select $cols from searchme where text(cidr) like '$net%'");
|
---|
408 | $sth->execute;
|
---|
409 | while (my @data = $sth->fetchrow_array()) {
|
---|
410 | my $cidr = new NetAddr::IP $data[0];
|
---|
411 | if ($cidr->contains($sfor)) {
|
---|
412 | queryResults("select $cols from searchme where cidr='$cidr'", $webvar{page}, 1);
|
---|
413 | }
|
---|
414 | }
|
---|
415 | } elsif ($query =~ /^(\d{1,3}\.){1,3}\d{1,3}\.?$/) {
|
---|
416 | #print "Finding matches with leading octet(s) $query<br>\n";
|
---|
417 | $sql = "select $cols from searchme where text(cidr) like '$query%'";
|
---|
418 | my $count = countRows($sql);
|
---|
419 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
|
---|
420 | queryResults($sql, $webvar{page}, $count);
|
---|
421 | } else {
|
---|
422 | # This shouldn't happen, but if it does, whoever gets it deserves what they get...
|
---|
423 | $page->param(errmsg => "Invalid query.");
|
---|
424 | }
|
---|
425 | } else {
|
---|
426 | # This shouldn't happen, but if it does, whoever gets it deserves what they get...
|
---|
427 | $page->param(errmsg => "Invalid searchfor.");
|
---|
428 | }
|
---|
429 | } # viewBy
|
---|
430 |
|
---|
431 |
|
---|
432 | # args are: a reference to an array with the row to be printed and the
|
---|
433 | # class(stylesheet) to use for formatting.
|
---|
434 | # if ommitting the class - call the sub as &printRow(\@array)
|
---|
435 | sub printRow {
|
---|
436 | my ($rowRef,$class) = @_;
|
---|
437 |
|
---|
438 | if (!$class) {
|
---|
439 | print "<tr>\n";
|
---|
440 | } else {
|
---|
441 | print "<tr class=\"$class\">\n";
|
---|
442 | }
|
---|
443 |
|
---|
444 | ELEMENT: foreach my $element (@$rowRef) {
|
---|
445 | if (!defined($element)) {
|
---|
446 | print "<td></td>\n";
|
---|
447 | next ELEMENT;
|
---|
448 | }
|
---|
449 | $element =~ s|\n|</br>|g;
|
---|
450 | print "<td>$element</td>\n";
|
---|
451 | }
|
---|
452 | print "</tr>";
|
---|
453 | } # printRow
|
---|
454 |
|
---|
455 |
|
---|
456 | # queryResults()
|
---|
457 | # Display search queries based on the passed SQL.
|
---|
458 | # Takes SQL, page number (for multipage search results), and a total count.
|
---|
459 | sub queryResults {
|
---|
460 | my ($sql, $pageNo, $rowCount) = @_;
|
---|
461 | my $offset = 0;
|
---|
462 | $offset = $1 if($sql =~ m/.*limit\s+(.*),.*/);
|
---|
463 |
|
---|
464 | my $sth = $ip_dbh->prepare($sql);
|
---|
465 | $sth->execute();
|
---|
466 |
|
---|
467 | $page->param(searchtitle => "Showing all netblock and static-IP allocations");
|
---|
468 |
|
---|
469 | my $count = 0;
|
---|
470 | my @sresults;
|
---|
471 | while (my ($block, $custid, $type, $city, $desc) = $sth->fetchrow_array) {
|
---|
472 | my %row = (
|
---|
473 | rowclass => $count++ % 2,
|
---|
474 | issub => ($type =~ /^.r$/ ? 1 : 0),
|
---|
475 | block => $block,
|
---|
476 | ispool => ($type =~ /^.[pd]$/ ? 1 : 0),
|
---|
477 | custid => $custid,
|
---|
478 | disptype => $disp_alloctypes{$type},
|
---|
479 | city => $city,
|
---|
480 | desc => $desc
|
---|
481 | );
|
---|
482 | push @sresults, \%row;
|
---|
483 | }
|
---|
484 | $page->param(sresults => \@sresults);
|
---|
485 |
|
---|
486 | # Have to think on this call, it's primarily to clean up unfetched rows from a select.
|
---|
487 | # In this context it's probably a good idea.
|
---|
488 | $sth->finish();
|
---|
489 |
|
---|
490 | my $upper = $offset+$count;
|
---|
491 |
|
---|
492 | $page->param(resfound => $rowCount);
|
---|
493 | $page->param(resstart => $offset+1);
|
---|
494 | $page->param(resstop => $upper);
|
---|
495 |
|
---|
496 | # print the page thing..
|
---|
497 | if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) {
|
---|
498 | $page->param(multipage => 1);
|
---|
499 | my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
|
---|
500 | my @pagelist;
|
---|
501 | for (my $i = 1; $i <= $pages; $i++) {
|
---|
502 | my %row;
|
---|
503 | $row{pgnum} = $i;
|
---|
504 | if ($i == $pageNo) {
|
---|
505 | $row{thispage} = 1;
|
---|
506 | } else {
|
---|
507 | $row{stype} = $webvar{stype};
|
---|
508 | if ($webvar{stype} eq 'c') {
|
---|
509 | $row{extraopts} = "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
|
---|
510 | "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&".
|
---|
511 | "allcities=$webvar{allcities}&";
|
---|
512 | foreach my $key (keys %webvar) {
|
---|
513 | if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) {
|
---|
514 | $row{extraopts} .= "$key=$webvar{$key}&";
|
---|
515 | }
|
---|
516 | }
|
---|
517 | } else {
|
---|
518 | $row{extraopts} = "input=$webvar{input}&";
|
---|
519 | }
|
---|
520 | }
|
---|
521 | push @pagelist, \%row;
|
---|
522 | }
|
---|
523 | $page->param(pgnums => \@pagelist);
|
---|
524 | }
|
---|
525 |
|
---|
526 | } # queryResults
|
---|
527 |
|
---|
528 |
|
---|
529 | # Prints table headings. Accepts any number of arguments;
|
---|
530 | # each argument is a table heading.
|
---|
531 | sub startTable {
|
---|
532 | print qq(<center><table width="98%" cellspacing="0" class="center"><tr>);
|
---|
533 |
|
---|
534 | foreach(@_) {
|
---|
535 | print qq(<td class="heading">$_</td>);
|
---|
536 | }
|
---|
537 | print "</tr>\n";
|
---|
538 | } # startTable
|
---|
539 |
|
---|
540 |
|
---|
541 | # Return count of rows to be returned in a "real" query
|
---|
542 | # with the passed SQL statement
|
---|
543 | sub countRows {
|
---|
544 | # Note that the "as foo" is required
|
---|
545 | my $sth = $ip_dbh->prepare("select count(*) from ($_[0]) as foo");
|
---|
546 | $sth->execute();
|
---|
547 | my @a = $sth->fetchrow_array();
|
---|
548 | $sth->finish();
|
---|
549 | return $a[0];
|
---|
550 | }
|
---|