Changeset 517 for trunk/cgi-bin/search.cgi
- Timestamp:
- 10/18/12 16:53:10 (12 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to
/branches/htmlform merged eligible
-
Property svn:mergeinfo
set to
-
trunk/cgi-bin/search.cgi
r455 r517 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('', ($IPDBacl{$authuser} =~ /a/ ? 67 '<td align=right><a href="/ip/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 246 261 # Generate table of types 247 my $typetable = "<table class=regular cellspacing=0>\n<tr>";248 262 $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ". 249 263 "order by listorder"); 250 264 $sth->execute; 251 265 my $i=0; 252 while (my @data = $sth->fetchrow_array) { 253 $typetable .= "<td><input type=checkbox name=type[$data[0]]>$data[1]</td>"; 254 $i++; 255 $typetable .= "</tr>\n<tr>" 256 if ($i % 4 == 0); 257 } 258 if ($i %4 == 0) { 259 $typetable =~ s/<tr>$//; 260 } else { 261 $typetable .= "</tr>\n"; 262 } 263 $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); 264 277 265 278 # Generate table of cities 266 my $citytable = "<table class=regular cellspacing=0>\n<tr>";267 279 $sth = $ip_dbh->prepare("select id,city from cities order by city"); 268 280 $sth->execute; 269 my $i=0; 270 while (my @data = $sth->fetchrow_array) { 271 $citytable .= "<td><input type=checkbox name=city[$data[0]]>$data[1]</td>"; 272 $i++; 273 $citytable .= "</tr>\n<tr>" 274 if ($i % 5 == 0); 275 } 276 if ($i %5 == 0) { 277 $citytable =~ s/<tr>$//; 278 } else { 279 $citytable .= "</tr>\n"; 280 } 281 $citytable .= "</table>\n"; 282 283 $html =~ s/\$\$TYPELIST\$\$/$typetable/; 284 $html =~ s/\$\$CITYLIST\$\$/$citytable/; 285 286 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 287 294 } 295 296 print $page->output; 288 297 289 298 # Shut down and clean up. 290 299 finish($ip_dbh); 291 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 292 308 # We shouldn't need to directly execute any code below here; it's all subroutines. 293 309 exit 0; … … 322 338 if ($category eq 'all') { 323 339 324 print qq(<div class="heading">Showing all netblock and static-IP allocations</div><br>\n);325 340 $sql = "select $cols from searchme"; 326 341 my $count = countRows($sql); … … 330 345 } elsif ($category eq 'cust') { 331 346 347 ##fixme: this and other quick-search areas; fix up page heading title similar to first grouping above 332 348 print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n); 333 349 … … 393 409 } else { 394 410 # This shouldn't happen, but if it does, whoever gets it deserves what they get... 395 printError("Invalid query.");411 $page->param(errmsg => "Invalid query."); 396 412 } 397 413 } else { 398 414 # This shouldn't happen, but if it does, whoever gets it deserves what they get... 399 printError("Invalid searchfor.");415 $page->param(errmsg => "Invalid searchfor."); 400 416 } 401 417 } # viewBy … … 437 453 $sth->execute(); 438 454 439 startTable('Allocation','CustID','Type','City','Description/Name'); 455 $page->param(searchtitle => "Showing all netblock and static-IP allocations"); 456 440 457 my $count = 0; 441 442 while (my @data = $sth->fetchrow_array) { 443 444 # cidr,custid,type,city,description,notes 445 # Another bit of HairyPerl(TM) to prefix subblocks with "Sub" 446 my @row = (($data[2] =~ /^.r$/ ? 'Sub ' : ''). 447 qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>), 448 $data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]); 449 # Allow listing of pool if desired/required. 450 if ($data[2] =~ /^.[pd]$/) { 451 $row[0] .= ' <a href="/ip/cgi-bin/main.cgi?action=listpool'. 452 "&pool=$data[0]\">List IPs</a>"; 453 } 454 printRow(\@row, 'color1', 1) if ($count%2==0); 455 printRow(\@row, 'color2', 1) if ($count%2!=0); 456 $count++; 457 } 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); 458 473 459 474 # Have to think on this call, it's primarily to clean up unfetched rows from a select. … … 462 477 463 478 my $upper = $offset+$count; 464 print "<tr><td colspan=10 bgcolor=white class=regular>Records found: $rowCount<br><i>Displaying: ".($offset+1)." - $upper</i></td></tr>\n"; 465 print "</table></center>\n"; 479 480 $page->param(resfound => $rowCount); 481 $page->param(resstart => $offset+1); 482 $page->param(resstop => $upper); 466 483 467 484 # print the page thing.. 468 485 if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) { 486 $page->param(multipage => 1); 469 487 my $pages = ceil($rowCount/$RESULTS_PER_PAGE); 470 print qq(<div class="center"> Page: );488 my @pagelist; 471 489 for (my $i = 1; $i <= $pages; $i++) { 490 my %row; 491 $row{pgnum} = $i; 472 492 if ($i == $pageNo) { 473 print "<b>$i </b>\n";493 $row{thispage} = 1; 474 494 } else { 475 print qq(<a href="/ip/cgi-bin/search.cgi?page=$i&stype=$webvar{stype}&);495 $row{stype} = $webvar{stype}; 476 496 if ($webvar{stype} eq 'c') { 477 print"cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".497 $row{extraopts} = "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&". 478 498 "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&". 479 499 "allcities=$webvar{allcities}&"; 480 500 foreach my $key (keys %webvar) { 481 501 if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) { 482 print"$key=$webvar{$key}&";502 $row{extraopts} .= "$key=$webvar{$key}&"; 483 503 } 484 504 } 485 505 } else { 486 print"input=$webvar{input}&";506 $row{extraopts} = "input=$webvar{input}&"; 487 507 } 488 print qq(">$i</a> \n);489 508 } 490 } 491 print "</div>"; 492 } 509 push @pagelist, \%row; 510 } 511 $page->param(pgnums => \@pagelist); 512 } 513 493 514 } # queryResults 494 515
Note:
See TracChangeset
for help on using the changeset viewer.