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