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