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: 2022-10-21 16:48:30 +0000 (Fri, 21 Oct 2022) $
|
---|
8 | # SVN revision $Rev: 931 $
|
---|
9 | # Last update by $Author: kdeugau $
|
---|
10 | ###
|
---|
11 | # Copyright 2005-2010,2012,2015-2017,2022 - Kris Deugau <kdeugau@deepnet.cx>
|
---|
12 |
|
---|
13 | use strict;
|
---|
14 | use warnings;
|
---|
15 | use CGI::Carp qw(fatalsToBrowser);
|
---|
16 | use CGI::Simple;
|
---|
17 | use HTML::Template;
|
---|
18 | use DBI;
|
---|
19 | use POSIX qw(ceil);
|
---|
20 | use NetAddr::IP;
|
---|
21 |
|
---|
22 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
23 | ##uselib##
|
---|
24 |
|
---|
25 | # push "the directory the script is in" into @INC
|
---|
26 | use FindBin;
|
---|
27 | use lib "$FindBin::RealBin/";
|
---|
28 |
|
---|
29 | use MyIPDB;
|
---|
30 |
|
---|
31 | # Don't formally need a username or syslog here. syslog left active for debugging.
|
---|
32 | use Sys::Syslog;
|
---|
33 | openlog "IPDBsearch","pid","$IPDB::syslog_facility";
|
---|
34 |
|
---|
35 | # ... but we do *use* the username on ACLs now.
|
---|
36 | # Collect the username from HTTP auth. If undefined, we're in
|
---|
37 | # a test environment, or called without a username.
|
---|
38 | my $authuser;
|
---|
39 | if (!defined($ENV{'REMOTE_USER'})) {
|
---|
40 | $authuser = '__temptest';
|
---|
41 | } else {
|
---|
42 | $authuser = $ENV{'REMOTE_USER'};
|
---|
43 | }
|
---|
44 |
|
---|
45 | # Global variables
|
---|
46 | my $RESULTS_PER_PAGE = 25;
|
---|
47 |
|
---|
48 | # anyone got a better name? :P
|
---|
49 | my $thingroot = $ENV{SCRIPT_FILENAME};
|
---|
50 | $thingroot =~ s|cgi-bin/search.cgi||;
|
---|
51 |
|
---|
52 | # Set up the CGI object...
|
---|
53 | my $q = new CGI::Simple;
|
---|
54 | # ... and get query-string params as well as POST params if necessary
|
---|
55 | $q->parse_query_string;
|
---|
56 |
|
---|
57 | # Convenience; saves changing all references to %webvar
|
---|
58 | ##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection)
|
---|
59 | my %webvar = $q->Vars;
|
---|
60 |
|
---|
61 | if (defined($webvar{rpp})) {
|
---|
62 | ($RESULTS_PER_PAGE) = ($webvar{rpp} =~ /(\d+)/);
|
---|
63 | }
|
---|
64 |
|
---|
65 | # Why not a global DB handle? (And a global statement handle, as well...)
|
---|
66 | # Use the connectDB function, otherwise we end up confusing ourselves
|
---|
67 | my $ip_dbh;
|
---|
68 | my $sth;
|
---|
69 | my $errstr;
|
---|
70 | ($ip_dbh,$errstr) = connectDB_My;
|
---|
71 | if ($ip_dbh) {
|
---|
72 | checkDBSanity($ip_dbh);
|
---|
73 | initIPDBGlobals($ip_dbh);
|
---|
74 | }
|
---|
75 |
|
---|
76 | # Set up some globals
|
---|
77 | $ENV{HTML_TEMPLATE_ROOT} = $thingroot;
|
---|
78 | my @templatepath = [ "localtemplates", "templates" ];
|
---|
79 |
|
---|
80 | ## FIXME!
|
---|
81 | ## Pretty much everything from here on down is one giant FIXME
|
---|
82 | ## FIXME!
|
---|
83 |
|
---|
84 | my $page;
|
---|
85 | if (!defined($webvar{stype})) {
|
---|
86 | $webvar{stype} = "<NULL>"; #shuts up the warnings.
|
---|
87 | $page = HTML::Template->new(filename => "search/compsearch.tmpl", path => @templatepath);
|
---|
88 | $page->param(webpath => $IPDB::webpath);
|
---|
89 | } else {
|
---|
90 | $page = HTML::Template->new(filename => "search/sresults.tmpl", global_vars => 1, path => @templatepath);
|
---|
91 | $page->param(webpath => $IPDB::webpath);
|
---|
92 | }
|
---|
93 |
|
---|
94 | my $header = HTML::Template->new(filename => "header.tmpl", path => @templatepath);
|
---|
95 | $header->param(version => $IPDB::VERSION);
|
---|
96 | $header->param(addperm => $IPDBacl{$authuser} =~ /a/);
|
---|
97 | $header->param(webpath => $IPDB::webpath);
|
---|
98 | print "Content-type: text/html\n\n", $header->output;
|
---|
99 |
|
---|
100 | # Columns actually returned. Slightly better than hardcoding it
|
---|
101 | # in each (sub)select
|
---|
102 | my $cols = "s.cidr, s.custid, s.type, s.city, s.description, s.id, s.parent_id, s.available, a.vrf";
|
---|
103 | # Common base select. JOIN provides the VRF which may not be noted on individual allocations
|
---|
104 | my $sqlbase = "SELECT $cols FROM searchme s JOIN allocations a ON s.master_id=a.id";
|
---|
105 |
|
---|
106 | # Handle the DB error first
|
---|
107 | if (!$ip_dbh) {
|
---|
108 | $page = HTML::Template->new(filename => "dberr.tmpl", path => @templatepath);
|
---|
109 | $page->param(errmsg => $errstr);
|
---|
110 | } elsif ($webvar{stype} eq 'q') {
|
---|
111 | # Quick search.
|
---|
112 |
|
---|
113 | if (!$webvar{input}) {
|
---|
114 | # No search term. Display everything.
|
---|
115 | viewBy('all', '');
|
---|
116 | } else {
|
---|
117 | # Search term entered. Display matches.
|
---|
118 | # We should really sanitize $webvar{input}, no?
|
---|
119 | my $searchfor;
|
---|
120 | # Chew up leading and trailing whitespace
|
---|
121 | $webvar{input} =~ s/^\s+//;
|
---|
122 | $webvar{input} =~ s/\s+$//;
|
---|
123 | if ($webvar{input} =~ /^\d+$/) {
|
---|
124 | # All-digits, new custID
|
---|
125 | $searchfor = "cust";
|
---|
126 | } elsif ($webvar{input} =~ /^[\d\.]+(\/\d{1,3})?$/) {
|
---|
127 | # IP addresses should only have numbers, digits, and maybe a slash+netmask
|
---|
128 | $searchfor = "ipblock";
|
---|
129 | } elsif ($webvar{input} =~ /(?:^\d{6}\-|[A-Z][A-Z]\d\d\d)/) {
|
---|
130 | # Looks like part of a circuit ID
|
---|
131 | $searchfor = "circuitid";
|
---|
132 | } else {
|
---|
133 | # Anything else.
|
---|
134 | $searchfor = "desc";
|
---|
135 | }
|
---|
136 | viewBy($searchfor, $webvar{input});
|
---|
137 | }
|
---|
138 |
|
---|
139 | } elsif ($webvar{stype} eq 'c') {
|
---|
140 | # Complex search.
|
---|
141 |
|
---|
142 | # Several major cases, and a whole raft of individual cases.
|
---|
143 | # -> Show all types means we do not need to limit records retrieved by type
|
---|
144 | # -> Show all cities means we do not need to limit records retrieved by city
|
---|
145 | # Individual cases are for the CIDR/IP, CustID, Description, Notes, and individual type
|
---|
146 | # requests.
|
---|
147 |
|
---|
148 | my $sqlconcat;
|
---|
149 | if ($webvar{which} eq 'all') {
|
---|
150 | # Must match *all* specified criteria. ## use INTERSECT or EXCEPT
|
---|
151 | $sqlconcat = "INTERSECT";
|
---|
152 | } elsif ($webvar{which} eq 'any') {
|
---|
153 | # Match on any specified criteria ## use UNION
|
---|
154 | $sqlconcat = "UNION";
|
---|
155 | } else {
|
---|
156 | # sum-buddy tryn'a game the system. Match "all"
|
---|
157 | $sqlconcat = "INTERSECT";
|
---|
158 | }
|
---|
159 |
|
---|
160 | # We actually construct a monster SQL statement for all criteria.
|
---|
161 | # Iff something has been entered, it will be used as a filter.
|
---|
162 | # Iff something has NOT been entered, we still include it but in
|
---|
163 | # such a way that it does not actually filter anything out.
|
---|
164 |
|
---|
165 | # hack fix for undefined variables
|
---|
166 | $webvar{custid} = '' if !$webvar{custid};
|
---|
167 | $webvar{desc} = '' if !$webvar{desc};
|
---|
168 | $webvar{notes} = '' if !$webvar{notes};
|
---|
169 | $webvar{custexclude} = '' if !$webvar{custexclude};
|
---|
170 | $webvar{descexclude} = '' if !$webvar{descexclude};
|
---|
171 | $webvar{notesexclude} = '' if !$webvar{notesexclude};
|
---|
172 |
|
---|
173 | # First chunk of SQL. Filter on custid, description, and notes as necessary.
|
---|
174 | # Putting newlines in the SQL so that any SQL logging is somewhat more readable
|
---|
175 | # than a gigantic long line of conditions.
|
---|
176 | my $sql = "$sqlbase\n";
|
---|
177 | my @bindargs;
|
---|
178 | if ($webvar{custid}) {
|
---|
179 | $sql .= " WHERE $webvar{custexclude} (s.custid ~ ?)\n";
|
---|
180 | push @bindargs, $webvar{custid};
|
---|
181 | }
|
---|
182 | if ($webvar{desc}) {
|
---|
183 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{descexclude} s.description ~* ?)\n";
|
---|
184 | push @bindargs, $webvar{desc};
|
---|
185 | }
|
---|
186 | if ($webvar{notes}) {
|
---|
187 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{notesexclude} s.notes ~ ?)";
|
---|
188 | push @bindargs, $webvar{notes};
|
---|
189 | }
|
---|
190 |
|
---|
191 | # If we're not supposed to search for all types, search for the selected types.
|
---|
192 | $webvar{alltypes} = '' if !$webvar{alltypes};
|
---|
193 | $webvar{typeexclude} = '' if !$webvar{typeexclude};
|
---|
194 | if ($webvar{alltypes} ne 'on') {
|
---|
195 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{typeexclude} s.type IN (";
|
---|
196 | foreach my $key (keys %webvar) {
|
---|
197 | $sql .= "'$1'," if $key =~ /type\[(\w\w)\]/;
|
---|
198 | }
|
---|
199 | chop $sql;
|
---|
200 | $sql .= "))";
|
---|
201 | }
|
---|
202 |
|
---|
203 | # If we're not supposed to search for all cities, search for the selected cities.
|
---|
204 | # This could be vastly improved with proper foreign keys in the database.
|
---|
205 | $webvar{allcities} = '' if !$webvar{allcities};
|
---|
206 | $webvar{cityexclude} = '' if !$webvar{cityexclude};
|
---|
207 | if ($webvar{allcities} ne 'on') {
|
---|
208 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cityexclude} s.city IN (";
|
---|
209 | $sth = $ip_dbh->prepare("SELECT city FROM cities WHERE id=?");
|
---|
210 | foreach my $key (keys %webvar) {
|
---|
211 | if ($key =~ /city\[(\d+)\]/) {
|
---|
212 | $sth->execute($1);
|
---|
213 | my $city;
|
---|
214 | $sth->bind_columns(\$city);
|
---|
215 | $sth->fetch;
|
---|
216 | $city =~ s/'/''/;
|
---|
217 | $sql .= "'$city',";
|
---|
218 | }
|
---|
219 | }
|
---|
220 | chop $sql;
|
---|
221 | $sql .= "))";
|
---|
222 | }
|
---|
223 |
|
---|
224 | ## CIDR query options.
|
---|
225 | $webvar{cidr} =~ s/\s+//; # Hates the nasty spaceseseses we does.
|
---|
226 | if ($webvar{cidr} eq '') { # We has a blank CIDR. Ignore it.
|
---|
227 | } elsif ($webvar{cidr} =~ /\//) {
|
---|
228 | # 192.168.179/26 should show all /26 subnets in 192.168.179
|
---|
229 | my ($net,$maskbits) = split /\//, $webvar{cidr};
|
---|
230 | if ($webvar{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
|
---|
231 | # /0->/9 are silly to worry about right now. I don't think
|
---|
232 | # we'll be getting a class A anytime soon. <g>
|
---|
233 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} s.cidr <<= ?)";
|
---|
234 | push @bindargs, $webvar{cidr};
|
---|
235 | } else {
|
---|
236 | # Partial match; beginning of subnet and maskbits are provided
|
---|
237 | # Show any blocks with the leading octet(s) and that masklength
|
---|
238 | # Need some more magic for bare /nn searches:
|
---|
239 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} (masklen(s.cidr)) = ?";
|
---|
240 | push @bindargs, $maskbits;
|
---|
241 | if ($net ne '') {
|
---|
242 | $sql .= " AND text(s.cidr) LIKE ?";
|
---|
243 | push @bindargs, "$net%";
|
---|
244 | }
|
---|
245 | $sql .= ")";
|
---|
246 | }
|
---|
247 | } elsif ($webvar{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
|
---|
248 | # Specific IP address match. Will show either a single netblock,
|
---|
249 | # or a static pool plus an IP.
|
---|
250 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} s.cidr >>= ?)";
|
---|
251 | push @bindargs, $webvar{cidr};
|
---|
252 | } elsif ($webvar{cidr} =~ /^\d{1,3}(\.(\d{1,3}(\.(\d{1,3}\.?)?)?)?)?$/) {
|
---|
253 | # Leading octets in CIDR
|
---|
254 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} text(s.cidr) LIKE ?)";
|
---|
255 | push @bindargs, "$webvar{cidr}%";
|
---|
256 | } else {
|
---|
257 | # do nothing.
|
---|
258 | ##fixme we'll ignore this to clear out the references to legacy code.
|
---|
259 | } # done with CIDR query options.
|
---|
260 |
|
---|
261 | # Find the offset for multipage results
|
---|
262 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
263 |
|
---|
264 | # Find out how many rows the "core" query will return.
|
---|
265 | my $count = countRows($sql, @bindargs);
|
---|
266 |
|
---|
267 | if ($count == 0) {
|
---|
268 | $page->param(errmsg => "No matches found. Try eliminating one of the criteria,".
|
---|
269 | " or making one or more criteria more general.");
|
---|
270 | } else {
|
---|
271 | # Add the limit/offset clauses
|
---|
272 | # note ORDER BY needs to NOT reference the table alias s as in $sqlbase because Reasons
|
---|
273 | $sql .= " ORDER BY cidr";
|
---|
274 | $sql .= " LIMIT $RESULTS_PER_PAGE OFFSET $offset" if $RESULTS_PER_PAGE != 0;
|
---|
275 | # And tell the user.
|
---|
276 | print "<div class=heading>Searching...............</div>\n";
|
---|
277 | queryResults($sql, $webvar{page}, $count, @bindargs);
|
---|
278 | }
|
---|
279 |
|
---|
280 | } elsif ($webvar{stype} eq 'n') {
|
---|
281 | # Node search.
|
---|
282 |
|
---|
283 | my $sql = "$sqlbase JOIN noderef nr ON nr.block=s.cidr WHERE nr.node_id = ?";
|
---|
284 |
|
---|
285 | # Find the offset for multipage results
|
---|
286 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
287 |
|
---|
288 | # Find out how many rows the "core" query will return.
|
---|
289 | my $count = countRows($sql, $webvar{node});
|
---|
290 |
|
---|
291 | my $nodename = getNodeName($ip_dbh, $webvar{node});
|
---|
292 |
|
---|
293 | if ($count == 0) {
|
---|
294 | $page->param(errmsg => "No customers currently listed as connected through $nodename.");
|
---|
295 | ##fixme: still get the results table header
|
---|
296 | } else {
|
---|
297 | # Add the limit/offset clauses
|
---|
298 | $sql .= " ORDER BY cidr";
|
---|
299 | $sql .= " LIMIT $RESULTS_PER_PAGE OFFSET $offset" if $RESULTS_PER_PAGE != 0;
|
---|
300 | # And tell the user.
|
---|
301 | print "<div class=heading>Searching for assignments terminating on $nodename...</div>\n";
|
---|
302 | queryResults($sql, $webvar{page}, $count, $webvar{node});
|
---|
303 | }
|
---|
304 |
|
---|
305 | } else { # how script was called. General case is to show the search criteria page.
|
---|
306 |
|
---|
307 | # Generate table of types
|
---|
308 | $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ".
|
---|
309 | "order by listorder");
|
---|
310 | $sth->execute;
|
---|
311 | my $i=0;
|
---|
312 | my @typelist;
|
---|
313 | while (my ($type,$dispname) = $sth->fetchrow_array) {
|
---|
314 | my %row = (
|
---|
315 | newrow => ($i % 4 == 0),
|
---|
316 | type => $type,
|
---|
317 | dispname => $dispname,
|
---|
318 | endrow => ($i++ % 4 == 3)
|
---|
319 | );
|
---|
320 | push @typelist, \%row;
|
---|
321 | }
|
---|
322 | $page->param(typelist => \@typelist);
|
---|
323 |
|
---|
324 | # Generate table of cities
|
---|
325 | $sth = $ip_dbh->prepare("select id,city from cities order by city");
|
---|
326 | $sth->execute;
|
---|
327 | $i=0;
|
---|
328 | my @citylist;
|
---|
329 | while (my ($id, $city) = $sth->fetchrow_array) {
|
---|
330 | my %row = (
|
---|
331 | newrow => ($i % 4 == 0),
|
---|
332 | id => $id,
|
---|
333 | city => $city,
|
---|
334 | endrow => ($i++ % 4 == 3)
|
---|
335 | );
|
---|
336 | push @citylist, \%row;
|
---|
337 | }
|
---|
338 | $page->param(citylist => \@citylist);
|
---|
339 |
|
---|
340 | }
|
---|
341 |
|
---|
342 | print $page->output;
|
---|
343 |
|
---|
344 | # Shut down and clean up.
|
---|
345 | finish($ip_dbh);
|
---|
346 |
|
---|
347 | # We print the footer here, so we don't have to do it elsewhere.
|
---|
348 | my $footer = HTML::Template->new(filename => "footer.tmpl", path => @templatepath);
|
---|
349 | # include the admin tools link in the output?
|
---|
350 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
|
---|
351 |
|
---|
352 | print $footer->output;
|
---|
353 |
|
---|
354 | # We shouldn't need to directly execute any code below here; it's all subroutines.
|
---|
355 | exit 0;
|
---|
356 |
|
---|
357 |
|
---|
358 | # viewBy()
|
---|
359 | # The quick search
|
---|
360 | # Takes a category descriptor and a query string
|
---|
361 | # Creates appropriate SQL to run the search and display the results
|
---|
362 | # with queryResults()
|
---|
363 | sub viewBy {
|
---|
364 | my ($category,$query) = @_;
|
---|
365 |
|
---|
366 | # Local variables
|
---|
367 | my $sql;
|
---|
368 |
|
---|
369 | # Calculate start point for LIMIT clause
|
---|
370 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
371 | ##fixme: squeeze ORDER BY etc out into somewhere common, or at least an
|
---|
372 | # includeable bit instead of hardcoding in each block
|
---|
373 |
|
---|
374 | if ($category eq 'all') {
|
---|
375 |
|
---|
376 | # Sort of pointless, just horks up everything.
|
---|
377 | $sql = "$sqlbase";
|
---|
378 | my $count = countRows($sql);
|
---|
379 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
380 | queryResults($sql, $webvar{page}, $count);
|
---|
381 |
|
---|
382 | } elsif ($category eq 'cust') {
|
---|
383 |
|
---|
384 | ##fixme: this and other quick-search areas; fix up page heading title similar to first grouping above
|
---|
385 | print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);
|
---|
386 |
|
---|
387 | # Query for a customer ID. Note that we can't restrict to "numeric-only"
|
---|
388 | # as we have non-numeric custIDs in the legacy data. :/
|
---|
389 | $sql = "$sqlbase WHERE s.custid ~* ? OR s.description ~* ?";
|
---|
390 | my $count = countRows($sql, $query, $query);
|
---|
391 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
392 | queryResults($sql, $webvar{page}, $count, $query, $query);
|
---|
393 |
|
---|
394 | } elsif ($category eq 'desc') {
|
---|
395 |
|
---|
396 | print qq(<div class="heading">Searching for description, customer ID, or circuit ID matching '$query'</div><br>\n);
|
---|
397 | # Query based on description (includes "name" from old DB).
|
---|
398 | $sql = "$sqlbase WHERE s.description ~* ? OR s.custid ~* ? OR s.circuitid ~* ?";
|
---|
399 | my $count = countRows($sql, $query, $query, $query);
|
---|
400 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
401 | queryResults($sql, $webvar{page}, $count, $query, $query, $query);
|
---|
402 |
|
---|
403 | } elsif ($category eq 'circuitid') {
|
---|
404 |
|
---|
405 | print qq(<div class="heading">Searching for allocations with circuit ID matching '$query'</div><br>\n);
|
---|
406 | # Pretty similar to description and cust searches above, but focus on circuit ID
|
---|
407 | # JOIN needed for VRF field
|
---|
408 | $sql = "$sqlbase WHERE s.circuitid ~* ? OR s.description ~* ?";
|
---|
409 | my $count = countRows($sql, $query, $query);
|
---|
410 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
411 | queryResults($sql, $webvar{page}, $count, $query, $query);
|
---|
412 |
|
---|
413 | } elsif ($category =~ /ipblock/) {
|
---|
414 |
|
---|
415 | # Query is for a partial IP, a CIDR block in some form, or a flat IP.
|
---|
416 | print qq(<div class="heading">Searching for IP-based matches on '$query'</div><br>\n);
|
---|
417 |
|
---|
418 | $query =~ s/\s+//g;
|
---|
419 | if ($query =~ /\//) {
|
---|
420 | # 192.168.179/26 should show all /26 subnets in 192.168.179
|
---|
421 | my ($net,$maskbits) = split /\//, $query;
|
---|
422 | if ($query =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
|
---|
423 | # /0->/9 are silly to worry about right now. I don't think
|
---|
424 | # we'll be getting a class A anytime soon. <g>
|
---|
425 | $sql = "$sqlbase WHERE s.cidr = ?";
|
---|
426 | queryResults($sql, $webvar{page}, 1, $query);
|
---|
427 | } else {
|
---|
428 | #print "Finding all blocks with netmask /$maskbits, leading octet(s) $net<br>\n";
|
---|
429 | # Partial match; beginning of subnet and maskbits are provided
|
---|
430 | $sql = "$sqlbase WHERE text(s.cidr) LIKE ? AND text(s.cidr) LIKE ?";
|
---|
431 | my $count = countRows($sql, "$net%", "%$maskbits");
|
---|
432 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
433 | queryResults($sql, $webvar{page}, $count, "$net%", "%$maskbits");
|
---|
434 | }
|
---|
435 |
|
---|
436 | } elsif ($query =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
|
---|
437 | # Specific IP address match
|
---|
438 | #print "4-octet pattern found; finding netblock containing IP $query<br>\n";
|
---|
439 | my ($net,$ip) = ($query =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/);
|
---|
440 | my $sfor = new NetAddr::IP $query;
|
---|
441 | $sql = "$sqlbase WHERE s.cidr >>= ? AND s.type <> 'mm'";
|
---|
442 | my $count = countRows($sql, $sfor);
|
---|
443 | $sql .= " ORDER BY masklen(s.cidr) DESC";
|
---|
444 | queryResults($sql, $webvar{page}, $count, $sfor);
|
---|
445 |
|
---|
446 | } elsif ($query =~ /^(\d{1,3}\.){1,3}\d{1,3}\.?$/) {
|
---|
447 | #print "Finding matches with leading octet(s) $query<br>\n";
|
---|
448 | $sql = "$sqlbase WHERE text(s.cidr) LIKE ?";
|
---|
449 | my $count = countRows($sql, "$query%");
|
---|
450 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
451 | queryResults($sql, $webvar{page}, $count, "$query%");
|
---|
452 | } else {
|
---|
453 | # This shouldn't happen, but if it does, whoever gets it deserves what they get...
|
---|
454 | $page->param(errmsg => "Invalid query.");
|
---|
455 | }
|
---|
456 | } else {
|
---|
457 | # This shouldn't happen, but if it does, whoever gets it deserves what they get...
|
---|
458 | $page->param(errmsg => "Invalid searchfor.");
|
---|
459 | }
|
---|
460 | } # viewBy
|
---|
461 |
|
---|
462 |
|
---|
463 |
|
---|
464 | # queryResults()
|
---|
465 | # Display search queries based on the passed SQL.
|
---|
466 | # Takes SQL, page number (for multipage search results), and a total count.
|
---|
467 | sub queryResults {
|
---|
468 | my $sql = shift;
|
---|
469 | my $pageNo = shift;
|
---|
470 | my $rowCount = shift;
|
---|
471 | my @bindargs = @_;
|
---|
472 |
|
---|
473 | my $offset = 0;
|
---|
474 | $offset = $1 if($sql =~ m/.*LIMIT\s+(.*),.*/);
|
---|
475 |
|
---|
476 | my $sth = $ip_dbh->prepare($sql);
|
---|
477 | $sth->execute(@bindargs);
|
---|
478 |
|
---|
479 | $page->param(searchtitle => "Showing all netblock and static-IP allocations");
|
---|
480 |
|
---|
481 | my $count = 0;
|
---|
482 | my @sresults;
|
---|
483 | while (my ($block, $custid, $type, $city, $desc, $id, $parent, $avail, $vrf) = $sth->fetchrow_array) {
|
---|
484 | my %row = (
|
---|
485 | rowclass => $count++ % 2,
|
---|
486 | vrf => $vrf,
|
---|
487 | issub => ($type =~ /^.r$/ ? 1 : 0),
|
---|
488 | ispool => ($type =~ /^.[pd]$/ ? 1 : 0),
|
---|
489 | basetype => ($type =~ /^.i/ ? 'i' : 'b'),
|
---|
490 | freeip => ($avail eq 'y'),
|
---|
491 | parent => $parent,
|
---|
492 | block => $block,
|
---|
493 | custid => $custid,
|
---|
494 | disptype => $disp_alloctypes{$type},
|
---|
495 | city => $city,
|
---|
496 | desc => $desc,
|
---|
497 | id => $id,
|
---|
498 | );
|
---|
499 | push @sresults, \%row;
|
---|
500 | }
|
---|
501 | $page->param(sresults => \@sresults);
|
---|
502 |
|
---|
503 | # Have to think on this call, it's primarily to clean up unfetched rows from a select.
|
---|
504 | # In this context it's probably a good idea.
|
---|
505 | $sth->finish();
|
---|
506 |
|
---|
507 | my $upper = $offset+$count;
|
---|
508 |
|
---|
509 | $page->param(resfound => $rowCount);
|
---|
510 | $page->param(resstart => $offset+1);
|
---|
511 | $page->param(resstop => $upper);
|
---|
512 |
|
---|
513 | # print the page thing..
|
---|
514 | if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) {
|
---|
515 | $page->param(multipage => 1);
|
---|
516 | my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
|
---|
517 | my @pagelist;
|
---|
518 | for (my $i = 1; $i <= $pages; $i++) {
|
---|
519 | my %row;
|
---|
520 | $row{pgnum} = $i;
|
---|
521 | if ($i == $pageNo) {
|
---|
522 | $row{thispage} = 1;
|
---|
523 | } else {
|
---|
524 | $row{stype} = $webvar{stype};
|
---|
525 | if ($webvar{stype} eq 'c') {
|
---|
526 | $row{extraopts} = "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
|
---|
527 | "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&".
|
---|
528 | "allcities=$webvar{allcities}&";
|
---|
529 | foreach my $key (keys %webvar) {
|
---|
530 | if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) {
|
---|
531 | $row{extraopts} .= "$key=$webvar{$key}&";
|
---|
532 | }
|
---|
533 | }
|
---|
534 | } else {
|
---|
535 | $row{extraopts} = "input=$webvar{input}&";
|
---|
536 | }
|
---|
537 | }
|
---|
538 | push @pagelist, \%row;
|
---|
539 | }
|
---|
540 | $page->param(pgnums => \@pagelist);
|
---|
541 | }
|
---|
542 |
|
---|
543 | } # queryResults
|
---|
544 |
|
---|
545 |
|
---|
546 |
|
---|
547 | # Return count of rows to be returned in a "real" query
|
---|
548 | # with the passed SQL statement
|
---|
549 | sub countRows {
|
---|
550 | my $sql = shift;
|
---|
551 |
|
---|
552 | # Note that the "as foo" is required
|
---|
553 | my @a = $ip_dbh->selectrow_array("SELECT count(*) FROM ($sql) AS foo", undef, @_);
|
---|
554 | return $a[0];
|
---|
555 | }
|
---|