Ignore:
Timestamp:
05/14/13 18:10:22 (12 years ago)
Author:
Kris Deugau
Message:

/branches/stable

Merge /trunk r517 (merge /branches/htmlform)
Conflicts all resolved towards /trunk.
Fix a minor syntax error with "while (@data..." -> "while (my @data..."
(may cause merge conflicts later)

Location:
branches/stable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/stable

  • branches/stable/cgi-bin/search.cgi

    r592 r593  
    1414use warnings;   
    1515use CGI::Carp qw(fatalsToBrowser);
     16use CGI::Simple;
     17use HTML::Template;
    1618use DBI;
    17 use CommonWeb qw(:ALL);
    1819use POSIX qw(ceil);
    1920use NetAddr::IP;
     
    3839}
    3940
     41# Global variables
     42my $RESULTS_PER_PAGE = 25;
     43
     44# anyone got a better name?  :P
     45my $thingroot = $ENV{SCRIPT_FILENAME};
     46$thingroot =~ s|cgi-bin/search.cgi||;
     47
     48# Set up the CGI object...
     49my $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)
     55my %webvar = $q->Vars;
     56
     57if (defined($webvar{rpp})) {
     58  ($RESULTS_PER_PAGE) = ($webvar{rpp} =~ /(\d+)/);
     59}
     60
    4061# Why not a global DB handle?  (And a global statement handle, as well...)
    4162# Use the connectDB function, otherwise we end up confusing ourselves
     
    4465my $errstr;
    4566($ip_dbh,$errstr) = connectDB_My;
    46 if (!$ip_dbh) {
    47   printAndExit("Failed to connect to database: $errstr\n");
     67if ($ip_dbh) {
     68  checkDBSanity($ip_dbh);
     69  initIPDBGlobals($ip_dbh);
    4870}
    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
     75my $page;
    6176if (!defined($webvar{stype})) {
    6277  $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");
    6381}
    6482
    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') {
     83my $header = HTML::Template->new(filename => "header.tmpl");
     84$header->param(version => $IPDB::VERSION);
     85$header->param(addperm => $IPDBacl{$authuser} =~ /a/);
     86print "Content-type: text/html\n\n", $header->output;
     87
     88# Handle the DB error first
     89if (!$ip_dbh) {
     90  $page = HTML::Template->new(filename => "dberr.tmpl");
     91  $page->param(errmsg => $errstr);
     92} elsif ($webvar{stype} eq 'q') {
    7193  # Quick search.
    7294
     
    111133    $sqlconcat = "UNION";
    112134  } 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";
    116137  }
    117138
     
    190211        "text(cidr) like '$webvar{cidr}%')";
    191212  } 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.
    194215  } # done with CIDR query options.
    195216
     
    201222
    202223  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.");
    205226  } else {
    206227    # Add the limit/offset clauses
     
    225246
    226247  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
    228250  } else {
    229251    # Add the limit/offset clauses
     
    237259} else { # how script was called.  General case is to show the search criteria page.
    238260
    239   # Display search page.  We have to do this here, because otherwise
    240   # 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 
    247261# Generate table of types
    248   my $typetable = "<table class=regular cellspacing=0>\n<tr>";
    249262  $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ".
    250263        "order by listorder");
    251264  $sth->execute;
    252265  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);
    265277
    266278# Generate table of cities
    267   my $citytable = "<table class=regular cellspacing=0>\n<tr>";
    268279  $sth = $ip_dbh->prepare("select id,city from cities order by city");
    269280  $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
    288294}
     295
     296print $page->output;
    289297
    290298# Shut down and clean up.
    291299finish($ip_dbh);
    292 printFooter;
     300
     301# We print the footer here, so we don't have to do it elsewhere.
     302my $footer = HTML::Template->new(filename => "footer.tmpl");
     303# include the admin tools link in the output?
     304$footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
     305
     306print $footer->output;
     307
    293308# We shouldn't need to directly execute any code below here;  it's all subroutines.
    294309exit 0;
     
    323338  if ($category eq 'all') {
    324339
    325     print qq(<div class="heading">Showing all netblock and static-IP allocations</div><br>\n);
    326340    $sql = "select $cols from searchme";
    327341    my $count = countRows($sql);
     
    331345  } elsif ($category eq 'cust') {
    332346
     347##fixme:  this and other quick-search areas;  fix up page heading title similar to first grouping above
    333348    print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);
    334349
     
    394409    } else {
    395410      # 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.");
    397412    }
    398413  } else {
    399414    # 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.");
    401416  }
    402417} # viewBy
     
    438453  $sth->execute();
    439454
    440   startTable('Allocation','CustID','Type','City','Description/Name');
     455  $page->param(searchtitle => "Showing all netblock and static-IP allocations");
     456
    441457  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] .= ' &nbsp; <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);
    459473
    460474  # Have to think on this call, it's primarily to clean up unfetched rows from a select.
     
    463477
    464478  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);
    467483
    468484  # print the page thing..
    469485  if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) {
     486    $page->param(multipage => 1);
    470487    my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
    471     print qq(<div class="center"> Page: );
     488    my @pagelist;
    472489    for (my $i = 1; $i <= $pages; $i++) {
     490      my %row;
     491      $row{pgnum} = $i;
    473492      if ($i == $pageNo) {
    474         print "<b>$i&nbsp;</b>\n";
     493        $row{thispage} = 1;
    475494      } else {
    476         print qq(<a href="$IPDB::webpath/cgi-bin/search.cgi?page=$i&stype=$webvar{stype}&);
     495        $row{stype} = $webvar{stype};
    477496        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}&".
    479498                "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&".
    480499                "allcities=$webvar{allcities}&";
    481500          foreach my $key (keys %webvar) {
    482501            if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) {
    483               print "$key=$webvar{$key}&";
     502              $row{extraopts} .= "$key=$webvar{$key}&";
    484503            }
    485504          }
    486505        } else {
    487           print "input=$webvar{input}&";
     506          $row{extraopts} = "input=$webvar{input}&";
    488507        }
    489         print qq(">$i</a>&nbsp;\n);
    490508      }
    491     }
    492     print "</div>";
    493   }
     509      push @pagelist, \%row;
     510    }
     511    $page->param(pgnums => \@pagelist);
     512  }
     513
    494514} # queryResults
    495515
Note: See TracChangeset for help on using the changeset viewer.