Changeset 593 for branches/stable/cgi-bin/search.cgi
- Timestamp:
- 05/14/13 18:10:22 (12 years ago)
- Location:
- branches/stable
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable
- Property svn:mergeinfo changed
/branches/htmlform (added) merged: 446-451,456-461,463-484,487-505,511-514,516 /trunk merged: 517
- Property svn:mergeinfo changed
-
branches/stable/cgi-bin/search.cgi
r592 r593 14 14 use warnings; 15 15 use CGI::Carp qw(fatalsToBrowser); 16 use CGI::Simple; 17 use HTML::Template; 16 18 use DBI; 17 use CommonWeb qw(:ALL);18 19 use POSIX qw(ceil); 19 20 use NetAddr::IP; … … 38 39 } 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 40 61 # Why not a global DB handle? (And a global statement handle, as well...) 41 62 # Use the connectDB function, otherwise we end up confusing ourselves … … 44 65 my $errstr; 45 66 ($ip_dbh,$errstr) = connectDB_My; 46 if (!$ip_dbh) { 47 printAndExit("Failed to connect to database: $errstr\n"); 67 if ($ip_dbh) { 68 checkDBSanity($ip_dbh); 69 initIPDBGlobals($ip_dbh); 48 70 } 49 checkDBSanity($ip_dbh); 50 initIPDBGlobals($ip_dbh); 51 52 # Global variables 53 my $RESULTS_PER_PAGE = 25; 54 my %webvar = parse_post(); 55 cleanInput(\%webvar); 56 57 if (defined($webvar{rpp})) { 58 ($RESULTS_PER_PAGE) = ($webvar{rpp} =~ /(\d+)/); 59 } 60 71 72 # Set up some globals 73 $ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates"; 74 75 my $page; 61 76 if (!defined($webvar{stype})) { 62 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"); 63 81 } 64 82 65 # Headerize! Make sure we replace the $$EXTRA0$$ bit as needed. 66 printHeader('', $IPDB::webpath, ($IPDBacl{$authuser} =~ /a/ ? 67 '<td align=right><a href="'.$IPDB::webpath.'/cgi-bin/main.cgi?action=assign">Add new assignment</a></td>' : '' 68 )); 69 70 if ($webvar{stype} eq 'q') { 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') { 71 93 # Quick search. 72 94 … … 111 133 $sqlconcat = "UNION"; 112 134 } 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"; 135 # sum-buddy tryn'a game the system. Match "all" 136 $sqlconcat = "INTERSECT"; 116 137 } 117 138 … … 190 211 "text(cidr) like '$webvar{cidr}%')"; 191 212 } else { 192 # This shouldn't happen, but if it does, whoever gets it deserves what they get...193 printAndExit("Invalid netblock query.");213 # do nothing. 214 ##fixme we'll ignore this to clear out the references to legacy code. 194 215 } # done with CIDR query options. 195 216 … … 201 222 202 223 if ($count == 0) { 203 printError"No matches found. Try eliminating one of the criteria,".204 " or making one or more criteria more general." ;224 $page->param(errmsg => "No matches found. Try eliminating one of the criteria,". 225 " or making one or more criteria more general."); 205 226 } else { 206 227 # Add the limit/offset clauses … … 225 246 226 247 if ($count == 0) { 227 printError "No customers currently listed as connected through this node."; 248 $page->param(errmsg => "No customers currently listed as connected through this node."); 249 ##fixme: still get the results table header 228 250 } else { 229 251 # Add the limit/offset clauses … … 237 259 } else { # how script was called. General case is to show the search criteria page. 238 260 239 # Display search page. We have to do this here, because otherwise240 # we can't retrieve data from the database for the types and cities. >:(241 my $html;242 open HTML,"<../compsearch.html";243 $html = join('',<HTML>);244 close HTML;245 $html =~ s/\$\$WEBPATH\$\$/$IPDB::webpath/g;246 247 261 # Generate table of types 248 my $typetable = "<table class=regular cellspacing=0>\n<tr>";249 262 $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ". 250 263 "order by listorder"); 251 264 $sth->execute; 252 265 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"; 266 my @typelist; 267 while (my ($type,$dispname) = $sth->fetchrow_array) { 268 my %row = ( 269 newrow => ($i % 4 == 0), 270 type => $type, 271 dispname => $dispname, 272 endrow => ($i++ % 4 == 3) 273 ); 274 push @typelist, \%row; 275 } 276 $page->param(typelist => \@typelist); 265 277 266 278 # Generate table of cities 267 my $citytable = "<table class=regular cellspacing=0>\n<tr>";268 279 $sth = $ip_dbh->prepare("select id,city from cities order by city"); 269 280 $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; 281 $i=0; 282 my @citylist; 283 while (my ($id, $city) = $sth->fetchrow_array) { 284 my %row = ( 285 newrow => ($i % 4 == 0), 286 id => $id, 287 city => $city, 288 endrow => ($i++ % 4 == 3) 289 ); 290 push @citylist, \%row; 291 } 292 $page->param(citylist => \@citylist); 293 288 294 } 295 296 print $page->output; 289 297 290 298 # Shut down and clean up. 291 299 finish($ip_dbh); 292 printFooter; 300 301 # We print the footer here, so we don't have to do it elsewhere. 302 my $footer = HTML::Template->new(filename => "footer.tmpl"); 303 # include the admin tools link in the output? 304 $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/)); 305 306 print $footer->output; 307 293 308 # We shouldn't need to directly execute any code below here; it's all subroutines. 294 309 exit 0; … … 323 338 if ($category eq 'all') { 324 339 325 print qq(<div class="heading">Showing all netblock and static-IP allocations</div><br>\n);326 340 $sql = "select $cols from searchme"; 327 341 my $count = countRows($sql); … … 331 345 } elsif ($category eq 'cust') { 332 346 347 ##fixme: this and other quick-search areas; fix up page heading title similar to first grouping above 333 348 print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n); 334 349 … … 394 409 } else { 395 410 # This shouldn't happen, but if it does, whoever gets it deserves what they get... 396 printError("Invalid query.");411 $page->param(errmsg => "Invalid query."); 397 412 } 398 413 } else { 399 414 # This shouldn't happen, but if it does, whoever gets it deserves what they get... 400 printError("Invalid searchfor.");415 $page->param(errmsg => "Invalid searchfor."); 401 416 } 402 417 } # viewBy … … 438 453 $sth->execute(); 439 454 440 startTable('Allocation','CustID','Type','City','Description/Name'); 455 $page->param(searchtitle => "Showing all netblock and static-IP allocations"); 456 441 457 my $count = 0; 442 443 while (my @data = $sth->fetchrow_array) { 444 445 # cidr,custid,type,city,description,notes 446 # Another bit of HairyPerl(TM) to prefix subblocks with "Sub" 447 my @row = (($data[2] =~ /^.r$/ ? 'Sub ' : ''). 448 qq(<a href="$IPDB::webpath/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>), 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="'.$IPDB::webpath.'/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 } 458 my @sresults; 459 while (my ($block, $custid, $type, $city, $desc) = $sth->fetchrow_array) { 460 my %row = ( 461 rowclass => $count++ % 2, 462 issub => ($type =~ /^.r$/ ? 1 : 0), 463 block => $block, 464 ispool => ($type =~ /^.[pd]$/ ? 1 : 0), 465 custid => $custid, 466 disptype => $disp_alloctypes{$type}, 467 city => $city, 468 desc => $desc 469 ); 470 push @sresults, \%row; 471 } 472 $page->param(sresults => \@sresults); 459 473 460 474 # Have to think on this call, it's primarily to clean up unfetched rows from a select. … … 463 477 464 478 my $upper = $offset+$count; 465 print "<tr><td colspan=10 bgcolor=white class=regular>Records found: $rowCount<br><i>Displaying: ".($offset+1)." - $upper</i></td></tr>\n"; 466 print "</table></center>\n"; 479 480 $page->param(resfound => $rowCount); 481 $page->param(resstart => $offset+1); 482 $page->param(resstop => $upper); 467 483 468 484 # print the page thing.. 469 485 if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) { 486 $page->param(multipage => 1); 470 487 my $pages = ceil($rowCount/$RESULTS_PER_PAGE); 471 print qq(<div class="center"> Page: );488 my @pagelist; 472 489 for (my $i = 1; $i <= $pages; $i++) { 490 my %row; 491 $row{pgnum} = $i; 473 492 if ($i == $pageNo) { 474 print "<b>$i </b>\n";493 $row{thispage} = 1; 475 494 } else { 476 print qq(<a href="$IPDB::webpath/cgi-bin/search.cgi?page=$i&stype=$webvar{stype}&);495 $row{stype} = $webvar{stype}; 477 496 if ($webvar{stype} eq 'c') { 478 print"cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".497 $row{extraopts} = "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&". 479 498 "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&". 480 499 "allcities=$webvar{allcities}&"; 481 500 foreach my $key (keys %webvar) { 482 501 if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) { 483 print"$key=$webvar{$key}&";502 $row{extraopts} .= "$key=$webvar{$key}&"; 484 503 } 485 504 } 486 505 } else { 487 print"input=$webvar{input}&";506 $row{extraopts} = "input=$webvar{input}&"; 488 507 } 489 print qq(">$i</a> \n);490 508 } 491 } 492 print "</div>"; 493 } 509 push @pagelist, \%row; 510 } 511 $page->param(pgnums => \@pagelist); 512 } 513 494 514 } # queryResults 495 515
Note:
See TracChangeset
for help on using the changeset viewer.