Changeset 208
- Timestamp:
- 03/22/05 15:00:35 (20 years ago)
- Location:
- branches/new-search-20050223/cgi-bin
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/new-search-20050223/cgi-bin/main.cgi
r171 r208 45 45 46 46 #prototypes 47 sub viewBy($$); # feed it the category and query48 sub queryResults($$$); # args is the sql, the page# and the rowCount49 47 # Needs rewrite/rename 50 48 sub countRows($); # returns first element of first row of passed SQL … … 114 112 elsif($webvar{action} eq 'listpool') { 115 113 listPool(); 116 }117 elsif($webvar{action} eq 'search') {118 printHeader('');119 if (!$webvar{input}) {120 # No search term. Display everything.121 viewBy('all', '');122 } else {123 # Search term entered. Display matches.124 # We should really sanitize $webvar{input}, no?125 # need to munge up data for $webvar{searchfor}, rather than breaking things here.126 my $searchfor;127 # Chew up leading and trailing whitespace128 $webvar{input} =~ s/^\s+//;129 $webvar{input} =~ s/\s+$//;130 if ($webvar{input} =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{1,3}$/) {131 # "Perfect" IP subnet match132 $searchfor = "ipblock";133 } elsif ($webvar{input} =~ /^(\d{1,3}\.){3}\d{1,3}$/) {134 # "Perfect" IP address match (shows containing block)135 $searchfor = "ipblock";136 } elsif ($webvar{input} =~ /^(\d{1,3}\.){2}\d{1,3}(\.\d{1,3}?)?/) {137 # Partial IP match138 $searchfor = "ipblock";139 } elsif ($webvar{input} =~ /^\d+$/) {140 # All-digits, new custID141 $searchfor = "cust";142 } else {143 # Anything else.144 $searchfor = "desc";145 }146 viewBy($searchfor, $webvar{input});147 }148 114 } 149 115 … … 198 164 199 165 200 sub viewBy($$) {201 my ($category,$query) = @_;202 203 # Local variables204 my $sql;205 206 #print "<pre>\n";207 208 #print "start querysub: query '$query'\n";209 # this may happen with more than one subcategory. Unlikely, but possible.210 211 # Calculate start point for LIMIT clause212 my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;213 214 # Possible cases:215 # 1) Partial IP/subnet. Treated as "first-three-octets-match" in old IPDB,216 # I should be able to handle it similarly here.217 # 2a) CIDR subnet. Treated more or less as such in old IPDB.218 # 2b) CIDR netmask. Not sure how it's treated.219 # 3) Customer ID. Not handled in old IPDB220 # 4) Description.221 # 5) Invalid data which might be interpretable as an IP or something, but222 # which probably shouldn't be for reasons of sanity.223 224 if ($category eq 'all') {225 226 print qq(<div class="heading">Showing all netblock and static-IP allocations</div><br>\n);227 $sql = "select * from searchme";228 my $count = countRows("select count(*) from ($sql) foo");229 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";230 queryResults($sql, $webvar{page}, $count);231 232 } elsif ($category eq 'cust') {233 234 print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);235 236 # Query for a customer ID. Note that we can't restrict to "numeric-only"237 # as we have non-numeric custIDs in the legacy data. :/238 $sql = "select * from searchme where custid ilike '%$query%'";239 my $count = countRows("select count(*) from ($sql) foo");240 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";241 queryResults($sql, $webvar{page}, $count);242 243 } elsif ($category eq 'desc') {244 245 print qq(<div class="heading">Searching for descriptions containing '$query'</div><br>\n);246 # Query based on description (includes "name" from old DB).247 $sql = "select * from searchme where description ilike '%$query%'";248 my $count = countRows("select count(*) from ($sql) foo");249 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";250 queryResults($sql, $webvar{page}, $count);251 252 } elsif ($category =~ /ipblock/) {253 254 # Query is for a partial IP, a CIDR block in some form, or a flat IP.255 print qq(<div class="heading">Searching for IP-based matches on '$query'</div><br>\n);256 257 $query =~ s/\s+//g;258 if ($query =~ /\//) {259 # 209.91.179/26 should show all /26 subnets in 209.91.179260 my ($net,$maskbits) = split /\//, $query;261 if ($query =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {262 # /0->/9 are silly to worry about right now. I don't think263 # we'll be getting a class A anytime soon. <g>264 $sql = "select * from searchme where cidr='$query'";265 queryResults($sql, $webvar{page}, 1);266 } else {267 print "Finding all blocks with netmask /$maskbits, leading octet(s) $net<br>\n";268 # Partial match; beginning of subnet and maskbits are provided269 $sql = "select * from searchme where text(cidr) like '$net%' and ".270 "text(cidr) like '%$maskbits'";271 my $count = countRows("select count(*) from ($sql) foo");272 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";273 queryResults($sql, $webvar{page}, $count);274 }275 } elsif ($query =~ /^(\d{1,3}\.){3}\d{1,3}$/) {276 # Specific IP address match277 print "4-octet pattern found; finding netblock containing IP $query<br>\n";278 my ($net,$ip) = ($query =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/);279 my $sfor = new NetAddr::IP $query;280 $sth = $ip_dbh->prepare("select * from searchme where text(cidr) like '$net%'");281 $sth->execute;282 while (my @data = $sth->fetchrow_array()) {283 my $cidr = new NetAddr::IP $data[0];284 if ($cidr->contains($sfor)) {285 queryResults("select * from searchme where cidr='$cidr'", $webvar{page}, 1);286 }287 }288 } elsif ($query =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.?$/) {289 print "Finding matches where the first three octets are $query<br>\n";290 $sql = "select * from searchme where text(cidr) like '$query%'";291 my $count = countRows("select count(*) from ($sql) foo");292 $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";293 queryResults($sql, $webvar{page}, $count);294 } else {295 # This shouldn't happen, but if it does, whoever gets it deserves what they get...296 printError("Invalid query.");297 }298 } else {299 # This shouldn't happen, but if it does, whoever gets it deserves what they get...300 printError("Invalid searchfor.");301 }302 } # viewBy303 304 305 166 # args are: a reference to an array with the row to be printed and the 306 167 # class(stylesheet) to use for formatting. … … 325 186 print "</tr>"; 326 187 } # printRow 327 328 329 # Display certain types of search query. Note that this can't be330 # cleanly reused much of anywhere else as the data isn't neatly tabulated.331 # This is tied to the search sub tightly enough I may just gut it and provide332 # more appropriate tables directly as needed.333 sub queryResults($$$) {334 my ($sql, $pageNo, $rowCount) = @_;335 my $offset = 0;336 $offset = $1 if($sql =~ m/.*limit\s+(.*),.*/);337 338 my $sth = $ip_dbh->prepare($sql);339 $sth->execute();340 341 startTable('Allocation','CustID','Type','City','Description/Name');342 my $count = 0;343 344 while (my @data = $sth->fetchrow_array) {345 # cidr,custid,type,city,description,notes346 # Fix up types from pools (which are single-char)347 # Fixing the database would be... painful. :(348 ##fixme LEGACY CODE349 if ($data[2] =~ /^[cdsmw]$/) {350 $data[2] .= 'i';351 }352 my @row = (qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),353 $data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]);354 # Allow listing of pool if desired/required.355 if ($data[2] =~ /^.[pd]$/) {356 $row[0] .= ' <a href="/ip/cgi-bin/main.cgi?action=listpool'.357 "&pool=$data[0]\">List IPs</a>";358 }359 printRow(\@row, 'color1', 1) if ($count%2==0);360 printRow(\@row, 'color2', 1) if ($count%2!=0);361 $count++;362 }363 364 # Have to think on this call, it's primarily to clean up unfetched rows from a select.365 # In this context it's probably a good idea.366 $sth->finish();367 368 my $upper = $offset+$count;369 print "<tr><td colspan=10 bgcolor=white class=regular>Records found: $rowCount<br><i>Displaying: $offset - $upper</i></td></tr>\n";370 print "</table></center>\n";371 372 # print the page thing..373 if ($rowCount > $RESULTS_PER_PAGE) {374 my $pages = ceil($rowCount/$RESULTS_PER_PAGE);375 print qq(<div class="center"> Page: );376 for (my $i = 1; $i <= $pages; $i++) {377 if ($i == $pageNo) {378 print "<b>$i </b>\n";379 } else {380 print qq(<a href="/ip/cgi-bin/main.cgi?page=$i&input=$webvar{input}&action=search&searchfor=$webvar{searchfor}">$i</a> \n);381 }382 }383 print "</div>";384 }385 } # queryResults386 188 387 189 -
branches/new-search-20050223/cgi-bin/search.cgi
r207 r208 49 49 if ($webvar{stype} eq 'q') { 50 50 # Quick search. 51 print "Quick Search <zip>\n";52 51 53 52 if (!$webvar{input}) { … … 98 97 # We actually construct a monster SQL statement for all criteria. 99 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. 100 101 101 102 # Columns actually returned. Slightly better than hardcoding it
Note:
See TracChangeset
for help on using the changeset viewer.