source: branches/stable/cgi-bin/search.cgi@ 307

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

/branches/stable

Bugfix for slightly broken "bare /nn" search

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 15.7 KB
Line 
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: 2006-02-14 17:44:32 +0000 (Tue, 14 Feb 2006) $
8# SVN revision $Rev: 307 $
9# Last update by $Author: kdeugau $
10###
11# Copyright 2005 Kris Deugau <kdeugau@deepnet.cx>
12
13use strict;
14use warnings;
15use CGI::Carp qw(fatalsToBrowser);
16use DBI;
17use CommonWeb qw(:ALL);
18use MyIPDB;
19use POSIX qw(ceil);
20use NetAddr::IP;
21
22# Don't need a username or syslog here. syslog left active for debugging.
23use Sys::Syslog;
24openlog "IPDBsearch","pid","local2";
25
26# Why not a global DB handle? (And a global statement handle, as well...)
27# Use the connectDB function, otherwise we end up confusing ourselves
28my $ip_dbh;
29my $sth;
30my $errstr;
31($ip_dbh,$errstr) = connectDB_My;
32if (!$ip_dbh) {
33 printAndExit("Failed to connect to database: $errstr\n");
34}
35checkDBSanity($ip_dbh);
36initIPDBGlobals($ip_dbh);
37
38# Global variables
39my $RESULTS_PER_PAGE = 25;
40my %webvar = parse_post();
41cleanInput(\%webvar);
42
43if (!defined($webvar{stype})) {
44 $webvar{stype} = "<NULL>"; #shuts up the warnings.
45}
46
47printHeader('Searching...');
48
49if ($webvar{stype} eq 'q') {
50 # Quick search.
51
52 if (!$webvar{input}) {
53 # No search term. Display everything.
54 viewBy('all', '');
55 } else {
56 # Search term entered. Display matches.
57 # We should really sanitize $webvar{input}, no?
58 my $searchfor;
59 # Chew up leading and trailing whitespace
60 $webvar{input} =~ s/^\s+//;
61 $webvar{input} =~ s/\s+$//;
62 if ($webvar{input} =~ /^\d+$/) {
63 # All-digits, new custID
64 $searchfor = "cust";
65 } elsif ($webvar{input} =~ /^[\d\.]+(\/\d{1,3})?$/) {
66 # IP addresses should only have numbers, digits, and maybe a slash+netmask
67 $searchfor = "ipblock";
68 } else {
69 # Anything else.
70 $searchfor = "desc";
71 }
72 viewBy($searchfor, $webvar{input});
73 }
74
75} elsif ($webvar{stype} eq 'c') {
76 # Complex search.
77
78 # Several major cases, and a whole raft of individual cases.
79 # -> Show all types means we do not need to limit records retrieved by type
80 # -> Show all cities means we do not need to limit records retrieved by city
81 # Individual cases are for the CIDR/IP, CustID, Description, Notes, and individual type
82 # requests.
83
84 my $sqlconcat;
85 if ($webvar{which} eq 'all') {
86 # Must match *all* specified criteria. ## use INTERSECT or EXCEPT
87 $sqlconcat = "INTERSECT";
88 } elsif ($webvar{which} eq 'any') {
89 # Match on any specified criteria ## use UNION
90 $sqlconcat = "UNION";
91 } else {
92 # We can't get here. PTHBTT!
93 printAndExit "PTHBTT!! Your search has been rejected due to Microsoft excuse #4432: ".
94 "Not enough mana";
95 }
96
97# We actually construct a monster SQL statement for all criteria.
98# Iff something has been entered, it will be used as a filter.
99# Iff something has NOT been entered, we still include it but in
100# such a way that it does not actually filter anything out.
101
102 # Columns actually returned. Slightly better than hardcoding it
103 # in each (sub)select
104 my $cols = "cidr,custid,type,city,description";
105
106 # First chunk of SQL. Filter on custid, description, and notes as necessary.
107 my $sql = "(select $cols from searchme where $webvar{custexclude} custid ilike '%$webvar{custid}%')".
108 " $sqlconcat (select $cols from searchme where $webvar{descexclude} description ilike '%$webvar{desc}%')".
109 " $sqlconcat (select $cols from searchme where $webvar{notesexclude} notes ilike '%$webvar{notes}%')";
110
111 # If we're not supposed to search for all types, search for the selected types.
112 if ($webvar{alltypes} ne 'on') {
113 $sql .= " $sqlconcat (select $cols from searchme where $webvar{typeexclude} type in (";
114 foreach my $key (keys %webvar) {
115 $sql .= "'$1'," if $key =~ /type\[(..)\]/;
116 }
117 chop $sql;
118 $sql .= "))";
119 }
120
121 # If we're not supposed to search for all cities, search for the selected cities.
122 # This could be vastly improved with proper foreign keys in the database.
123 if ($webvar{allcities} ne 'on') {
124 $sql .= " $sqlconcat (select $cols from searchme where $webvar{cityexclude} city in (";
125 $sth = $ip_dbh->prepare("select city from cities where id=?");
126 foreach my $key (keys %webvar) {
127 if ($key =~ /city\[(\d+)\]/) {
128 $sth->execute($1);
129 my $city;
130 $sth->bind_columns(\$city);
131 $sth->fetch;
132 $city =~ s/'/''/;
133 $sql .= "'$city',";
134 }
135 }
136 chop $sql;
137 $sql .= "))";
138 }
139
140 ## CIDR query options.
141 $webvar{cidr} =~ s/\s+//; # Hates the nasty spaceseseses we does.
142 if ($webvar{cidr} eq '') { # We has a blank CIDR. Ignore it.
143 } elsif ($webvar{cidr} =~ /\//) {
144 # 209.91.179/26 should show all /26 subnets in 209.91.179
145 my ($net,$maskbits) = split /\//, $webvar{cidr};
146 if ($webvar{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
147 # /0->/9 are silly to worry about right now. I don't think
148 # we'll be getting a class A anytime soon. <g>
149 $sql .= " $sqlconcat (select $cols from searchme where ".
150 "$webvar{cidrexclude} cidr='$webvar{cidr}')";
151 } else {
152 # Partial match; beginning of subnet and maskbits are provided
153 # Show any blocks with the leading octet(s) and that masklength
154 # Need some more magic for bare /nn searches:
155 my $condition = ($net eq '' ?
156 "masklen(cidr)=$maskbits" : "text(cidr) like '$net%' and masklen(cidr)=$maskbits");
157 $sql .= " $sqlconcat (select $cols from searchme where $webvar{cidrexclude} ".
158 "($condition))";
159 }
160 } elsif ($webvar{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
161 # Specific IP address match. Will show either a single netblock,
162 # or a static pool plus an IP.
163 $sql .= " $sqlconcat (select $cols from searchme where $webvar{cidrexclude} ".
164 "cidr >>= '$webvar{cidr}')";
165 } elsif ($webvar{cidr} =~ /^\d{1,3}(\.(\d{1,3}(\.(\d{1,3}\.?)?)?)?)?$/) {
166 # Leading octets in CIDR
167 $sql .= " $sqlconcat (select $cols from searchme where $webvar{cidrexclude} ".
168 "text(cidr) like '$webvar{cidr}%')";
169 } else {
170 # This shouldn't happen, but if it does, whoever gets it deserves what they get...
171 printAndExit("Invalid netblock query.");
172 } # done with CIDR query options.
173
174 # Find the offset for multipage results
175 my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
176
177 # Find out how many rows the "core" query will return.
178 my $count = countRows($sql);
179
180 if ($count == 0) {
181 printError "No matches found. Try eliminating one of the criteria,".
182 " or making one or more criteria more general.";
183 } else {
184 # Add the limit/offset clauses
185 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
186 # And tell the user.
187 print "<div class=heading>Searching...............</div>\n";
188 queryResults($sql, $webvar{page}, $count);
189 }
190
191} else { # how script was called. General case is to show the search criteria page.
192
193 # Display search page. We have to do this here, because otherwise
194 # we can't retrieve data from the database for the types and cities. >:(
195 my $html;
196 open HTML,"<../compsearch.html";
197 $html = join('',<HTML>);
198 close HTML;
199
200# Generate table of types
201 my $typetable = "<table class=regular cellspacing=0>\n<tr>";
202 $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ".
203 "order by listorder");
204 $sth->execute;
205 my $i=0;
206 while (my @data = $sth->fetchrow_array) {
207 $typetable .= "<td><input type=checkbox name=type[$data[0]]>$data[1]</td>";
208 $i++;
209 $typetable .= "</tr>\n<tr>"
210 if ($i % 4 == 0);
211 }
212 if ($i %4 == 0) {
213 $typetable =~ s/<tr>$//;
214 } else {
215 $typetable .= "</tr>\n";
216 }
217 $typetable .= "</table>\n";
218
219# Generate table of cities
220 my $citytable = "<table class=regular cellspacing=0>\n<tr>";
221 $sth = $ip_dbh->prepare("select id,city from cities order by city");
222 $sth->execute;
223 my $i=0;
224 while (my @data = $sth->fetchrow_array) {
225 $citytable .= "<td><input type=checkbox name=city[$data[0]]>$data[1]</td>";
226 $i++;
227 $citytable .= "</tr>\n<tr>"
228 if ($i % 5 == 0);
229 }
230 if ($i %5 == 0) {
231 $citytable =~ s/<tr>$//;
232 } else {
233 $citytable .= "</tr>\n";
234 }
235 $citytable .= "</table>\n";
236
237 $html =~ s/\$\$TYPELIST\$\$/$typetable/;
238 $html =~ s/\$\$CITYLIST\$\$/$citytable/;
239
240 print $html;
241}
242
243# Shut down and clean up.
244finish($ip_dbh);
245printFooter;
246# We shouldn't need to directly execute any code below here; it's all subroutines.
247exit 0;
248
249
250# viewBy()
251# The quick search
252# Takes a category descriptor and a query string
253# Creates appropriate SQL to run the search and display the results
254# with queryResults()
255sub viewBy($$) {
256 my ($category,$query) = @_;
257
258 # Local variables
259 my $sql;
260
261 # Calculate start point for LIMIT clause
262 my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
263
264# Possible cases:
265# 1) Partial IP/subnet. Treated as "octet-prefix".
266# 2a) CIDR subnet. Exact match.
267# 2b) CIDR netmask. YMMV but it should be octet-prefix-with-netmask
268# (ie, all matches with the octet prefix *AND* that netmask)
269# 3) Customer ID. "Match-any-segment"
270# 4) Description. "Match-any-segment"
271# 5) Invalid data which might be interpretable as an IP or something, but
272# which probably shouldn't be for reasons of sanity.
273
274 if ($category eq 'all') {
275
276 print qq(<div class="heading">Showing all netblock and static-IP allocations</div><br>\n);
277 $sql = "select * from searchme";
278 my $count = countRows($sql);
279 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
280 queryResults($sql, $webvar{page}, $count);
281
282 } elsif ($category eq 'cust') {
283
284 print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);
285
286 # Query for a customer ID. Note that we can't restrict to "numeric-only"
287 # as we have non-numeric custIDs in the legacy data. :/
288 $sql = "select * from searchme where custid ilike '%$query%'";
289 my $count = countRows($sql);
290 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
291 queryResults($sql, $webvar{page}, $count);
292
293 } elsif ($category eq 'desc') {
294
295 print qq(<div class="heading">Searching for descriptions containing '$query'</div><br>\n);
296 # Query based on description (includes "name" from old DB).
297 $sql = "select * from searchme where description ilike '%$query%'".
298 " or custid ilike '%$query%'";
299 my $count = countRows($sql);
300 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
301 queryResults($sql, $webvar{page}, $count);
302
303 } elsif ($category =~ /ipblock/) {
304
305 # Query is for a partial IP, a CIDR block in some form, or a flat IP.
306 print qq(<div class="heading">Searching for IP-based matches on '$query'</div><br>\n);
307
308 $query =~ s/\s+//g;
309 if ($query =~ /\//) {
310 # 209.91.179/26 should show all /26 subnets in 209.91.179
311 my ($net,$maskbits) = split /\//, $query;
312 if ($query =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
313 # /0->/9 are silly to worry about right now. I don't think
314 # we'll be getting a class A anytime soon. <g>
315 $sql = "select * from searchme where cidr='$query'";
316 queryResults($sql, $webvar{page}, 1);
317 } else {
318 #print "Finding all blocks with netmask /$maskbits, leading octet(s) $net<br>\n";
319 # Partial match; beginning of subnet and maskbits are provided
320 $sql = "select * from searchme where text(cidr) like '$net%' and ".
321 "text(cidr) like '%$maskbits'";
322 my $count = countRows($sql);
323 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
324 queryResults($sql, $webvar{page}, $count);
325 }
326 } elsif ($query =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
327 # Specific IP address match
328 #print "4-octet pattern found; finding netblock containing IP $query<br>\n";
329 my ($net,$ip) = ($query =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/);
330 my $sfor = new NetAddr::IP $query;
331 $sth = $ip_dbh->prepare("select * from searchme where text(cidr) like '$net%'");
332 $sth->execute;
333 while (my @data = $sth->fetchrow_array()) {
334 my $cidr = new NetAddr::IP $data[0];
335 if ($cidr->contains($sfor)) {
336 queryResults("select * from searchme where cidr='$cidr'", $webvar{page}, 1);
337 }
338 }
339 } elsif ($query =~ /^(\d{1,3}\.){1,3}\d{1,3}\.?$/) {
340 #print "Finding matches with leading octet(s) $query<br>\n";
341 $sql = "select * from searchme where text(cidr) like '$query%'";
342 my $count = countRows($sql);
343 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
344 queryResults($sql, $webvar{page}, $count);
345 } else {
346 # This shouldn't happen, but if it does, whoever gets it deserves what they get...
347 printError("Invalid query.");
348 }
349 } else {
350 # This shouldn't happen, but if it does, whoever gets it deserves what they get...
351 printError("Invalid searchfor.");
352 }
353} # viewBy
354
355
356# args are: a reference to an array with the row to be printed and the
357# class(stylesheet) to use for formatting.
358# if ommitting the class - call the sub as &printRow(\@array)
359sub printRow {
360 my ($rowRef,$class) = @_;
361
362 if (!$class) {
363 print "<tr>\n";
364 } else {
365 print "<tr class=\"$class\">\n";
366 }
367
368ELEMENT: foreach my $element (@$rowRef) {
369 if (!defined($element)) {
370 print "<td></td>\n";
371 next ELEMENT;
372 }
373 $element =~ s|\n|</br>|g;
374 print "<td>$element</td>\n";
375 }
376 print "</tr>";
377} # printRow
378
379
380# queryResults()
381# Display search queries based on the passed SQL.
382# Takes SQL, page number (for multipage search results), and a total count.
383sub queryResults($$$) {
384 my ($sql, $pageNo, $rowCount) = @_;
385 my $offset = 0;
386 $offset = $1 if($sql =~ m/.*limit\s+(.*),.*/);
387
388 my $sth = $ip_dbh->prepare($sql);
389 $sth->execute();
390
391 startTable('Allocation','CustID','Type','City','Description/Name');
392 my $count = 0;
393
394 while (my @data = $sth->fetchrow_array) {
395
396 # cidr,custid,type,city,description,notes
397 # Another bit of HairyPerl(TM) to prefix subblocks with "Sub"
398 my @row = (($data[2] =~ /^.r$/ ? 'Sub ' : '').
399 qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),
400 $data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]);
401 # Allow listing of pool if desired/required.
402 if ($data[2] =~ /^.[pd]$/) {
403 $row[0] .= ' &nbsp; <a href="/ip/cgi-bin/main.cgi?action=listpool'.
404 "&pool=$data[0]\">List IPs</a>";
405 }
406 printRow(\@row, 'color1', 1) if ($count%2==0);
407 printRow(\@row, 'color2', 1) if ($count%2!=0);
408 $count++;
409 }
410
411 # Have to think on this call, it's primarily to clean up unfetched rows from a select.
412 # In this context it's probably a good idea.
413 $sth->finish();
414
415 my $upper = $offset+$count;
416 print "<tr><td colspan=10 bgcolor=white class=regular>Records found: $rowCount<br><i>Displaying: ".($offset+1)." - $upper</i></td></tr>\n";
417 print "</table></center>\n";
418
419 # print the page thing..
420 if ($rowCount > $RESULTS_PER_PAGE) {
421 my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
422 print qq(<div class="center"> Page: );
423 for (my $i = 1; $i <= $pages; $i++) {
424 if ($i == $pageNo) {
425 print "<b>$i&nbsp;</b>\n";
426 } else {
427 print qq(<a href="/ip/cgi-bin/search.cgi?page=$i&stype=$webvar{stype}&);
428 if ($webvar{stype} eq 'c') {
429 print "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
430 "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&".
431 "allcities=$webvar{allcities}&";
432 foreach my $key (keys %webvar) {
433 if ($key =~ /^(?:type|city)\[/) {
434 print "$key=$webvar{$key}&";
435 }
436 }
437 } else {
438 print "input=$webvar{input}&";
439 }
440 print qq(">$i</a>&nbsp;\n);
441 }
442 }
443 print "</div>";
444 }
445} # queryResults
446
447
448# Prints table headings. Accepts any number of arguments;
449# each argument is a table heading.
450sub startTable {
451 print qq(<center><table width="98%" cellspacing="0" class="center"><tr>);
452
453 foreach(@_) {
454 print qq(<td class="heading">$_</td>);
455 }
456 print "</tr>\n";
457} # startTable
458
459
460# Return count of rows to be returned in a "real" query
461# with the passed SQL statement
462sub countRows($) {
463 # Note that the "as foo" is required
464 my $sth = $ip_dbh->prepare("select count(*) from ($_[0]) as foo");
465 $sth->execute();
466 my @a = $sth->fetchrow_array();
467 $sth->finish();
468 return $a[0];
469}
Note: See TracBrowser for help on using the repository browser.