source: trunk/dns.cgi@ 52

Last change on this file since 52 was 52, checked in by Kris Deugau, 15 years ago

/trunk

Make subgroup y/n checkbox, search filter more or less work on user, domain, and group lists

  • still need to figure out how to retain the letter-filter, subgroup, and filter data when they're needed but discard them when they should be discarded
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 40.2 KB
Line 
1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-12-22 21:47:28 +0000 (Tue, 22 Dec 2009) $
6# SVN revision $Rev: 52 $
7# Last update by $Author: kdeugau $
8###
9# Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx>
10
11use strict;
12use warnings;
13
14use CGI::Carp qw (fatalsToBrowser);
15use CGI::Simple;
16use HTML::Template;
17use CGI::Session;
18use Crypt::PasswdMD5;
19use Net::DNS;
20use DBI;
21
22use lib '.';
23# custom modules
24use DNSDB qw(:ALL);
25
26my @debugbits; # temp, to be spit out near the end of processing
27my $debugenv = 0;
28
29# Let's do these templates right...
30my $templatedir = "templates";
31my $sessiondir = "session";
32
33# Set up the CGI object...
34my $q = new CGI::Simple;
35# ... and get query-string params as well as POST params if necessary
36$q->parse_query_string;
37
38# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
39my %webvar = $q->Vars;
40
41# persistent stuff needed on most/all pages
42my $sid = ($webvar{sid} ? $webvar{sid} : undef);
43my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir});
44#$sid = $session->id() if !$sid;
45if (!$sid) {
46 # init stuff. can probably axe this down to just above if'n'when user manipulation happens
47 $sid = $session->id();
48# need to know the "upper" group the user can deal with; may as well
49# stick this in the session rather than calling out to the DB every time.
50 $session->param('logingroup',1);
51 $session->param('curgroup',1); # yes, we *do* need to track this too. er, probably.
52 $session->param('domlistsortby','domain');
53 $session->param('domlistorder','ASC');
54 $session->param('useradminsortby','username');
55 $session->param('useradminorder','ASC');
56 $session->param('grpmansortby','group');
57 $session->param('grpmanorder','ASC');
58 $session->param('reclistsortby','name');
59 $session->param('reclistorder','ASC');
60}
61
62my $logingroup = ($session->param('logingroup') ? $session->param('logingroup') : 1);
63my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $logingroup);
64my $group = ($webvar{group} ? $webvar{group} : 1);
65
66# nrgh, can't handle login here because we don't have a database handle to check the user/pass with yet
67
68if ($webvar{action} && $webvar{action} eq 'chgroup') {
69 # fiddle session-stored group data
70 # magic incantation to... uhhh...
71 $session->param('curgroup', $webvar{group});
72 $curgroup = ($webvar{group} ? $webvar{group} : $session->param('curgroup'));
73}
74
75my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
76my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
77
78# default
79#my $perpage = 15;
80my $perpage = 3;
81my $offset = ($webvar{offset} ? $webvar{offset} : 0);
82
83# NB: these must match the field name and SQL ascend/descend syntax respectively
84my $sortby = "domain";
85my $sortorder = "ASC";
86
87my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret","dbhost");
88#my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
89# { AutoCommit => 0 }) or die $DBI::errstr;
90
91##fixme. PLEASE! <G>
92print $msg if !$dbh;
93
94# fiddle hardcoded "defaults" as per system/user (?) prefs
95initGlobals($dbh);
96
97# handle login redirect
98if ($webvar{action}) {
99 if ($webvar{action} eq 'login') {
100 my $sth = $dbh->prepare("SELECT user_id,group_id,password,firstname,lastname FROM users WHERE username=?");
101 $sth->execute($webvar{username});
102 my ($uid,$gid,$pass,$fname,$lname) = $sth->fetchrow_array;
103 $webvar{loginfailed} = 1 if !defined($uid);
104
105 if ($pass =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
106 $webvar{loginfailed} = 1 if $pass ne unix_md5_crypt($webvar{password},$1);
107 } else {
108 $webvar{loginfailed} = 1 if $pass ne $webvar{password};
109 }
110
111 # set session bits
112 $session->param('logingroup',$gid);
113 $session->param('curgroup',$gid);
114 $session->param('username',$webvar{username});
115
116 changepage(page => "domlist") if !defined($webvar{loginfailed});
117 } elsif ($webvar{action} eq 'logout') {
118 # delete the session
119 $session->delete();
120 $session->flush();
121
122 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}";
123 $newurl =~ s|/[^/]+$|/|;
124 print "Status: 302\nLocation: $newurl\n\n";
125 exit;
126
127 }
128}
129
130## Default page is a login page
131#my $page; # to be initialized as an HTML::Template entity sooner or later
132
133
134
135# decide which page to spit out...
136$webvar{page} = 'login' if !$webvar{page};
137#if (!$webvar{page}) {
138# $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
139#} else {
140#}
141
142my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
143
144$page->param(sid => $sid);
145
146if ($webvar{page} eq 'login') {
147
148 $page->param(loginfailed => 1) if $webvar{loginfailed};
149##fixme: set up session init to actually *check* for session timeout
150 $page->param(timeout => 1) if $webvar{sesstimeout};
151
152} elsif ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
153
154# hmm. seeing problems in some possibly-not-so-corner cases.
155# this currently only handles "domain on", "domain off"
156 if (defined($webvar{action})) {
157 domStatus($dbh,$webvar{id},$webvar{action});
158 }
159
160 $page->param(curpage => $webvar{page});
161
162 listdomains();
163
164} elsif ($webvar{page} eq 'newdomain') {
165
166
167} elsif ($webvar{page} eq 'deldom') {
168
169 $page->param(id => $webvar{id});
170 # first pass = confirm y/n (sorta)
171 if (!defined($webvar{del})) {
172 $page->param(del_getconf => 1);
173 $page->param(domain => domainName($dbh,$webvar{id}));
174# print some neato things?
175
176# } else {
177# #whether actually deleting or cancelling we redirect to the domain list, default format
178
179 } elsif ($webvar{del} eq 'ok') {
180 my ($code,$msg) = delDomain($dbh, $webvar{id});
181 if ($code ne 'OK') {
182# need to find failure mode
183 $page->param(del_failed => 1);
184 $page->param(errmsg => $msg);
185 listdomains($curgroup);
186 } else {
187 # success. go back to the domain list, do not pass "GO"
188 changepage(page => "domlist");
189 }
190 } else {
191 # cancelled. whee!
192 changepage(page => "domlist");
193 }
194
195} elsif ($webvar{page} eq 'reclist') {
196
197 # Handle record list for both default records (per-group) and live domain records
198
199 $page->param(defrec => $webvar{defrec});
200 $page->param(id => $webvar{id});
201 $page->param(curpage => $webvar{page});
202
203# select count(*) from (default_)?records where (group|domain)_id=?
204 my $sth = $dbh->prepare("SELECT count(*) FROM ".
205 ($webvar{defrec} eq 'y' ? 'default_' : '')."records ".
206 "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ".
207 "AND NOT type=$reverse_typemap{SOA}");
208 $sth->execute($webvar{id});
209 my ($count) = ($sth->fetchrow_array);
210
211# fill the page-count and first-previous-next-last-all details
212 fill_pgcount($count,"records",domainName($dbh,$webvar{id}));
213 fill_fpnla($count); # should put some params on this sub...
214
215 $page->param(defrec => $webvar{defrec});
216 if ($webvar{defrec} eq 'y') {
217##fixme: hardcoded group
218 showdomain('y',$curgroup);
219 } else {
220 showdomain('n',$webvar{id});
221 }
222
223} elsif ($webvar{page} eq 'record') {
224
225 if ($webvar{recact} eq 'new') {
226
227 $page->param(todo => "Add record to");
228 $page->param(recact => "add");
229 fill_rectypes();
230
231 } elsif ($webvar{recact} eq 'add') {
232
233 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
234 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
235 push @recargs, $webvar{distance};
236 if ($webvar{type} == $reverse_typemap{SRV}) {
237 push @recargs, $webvar{weight};
238 push @recargs, $webvar{port};
239 }
240 }
241 my ($code,$msg) = addRec(@recargs);
242
243 if ($code eq 'OK') {
244 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
245 } else {
246
247 $page->param(failed => 1);
248 $page->param(errmsg => $msg);
249 $page->param(wastrying => "adding");
250 $page->param(todo => "Add record to");
251 $page->param(recact => "add");
252 $page->param(parentid => $webvar{parentid});
253 $page->param(defrec => $webvar{defrec});
254 $page->param(id => $webvar{id});
255 fill_recdata(); # populate the form... er, mostly.
256 }
257
258 } elsif ($webvar{recact} eq 'edit') {
259
260 $page->param(todo => "Update record");
261 $page->param(recact => "update");
262 $page->param(parentid => $webvar{parentid});
263 $page->param(id => $webvar{id});
264 $page->param(defrec => $webvar{defrec});
265 my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ".
266 ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?");
267 $sth->execute($webvar{id});
268 my ($host,$type,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array;
269 $page->param(name => $host);
270 $page->param(address => $val);
271 $page->param(distance => $distance);
272 $page->param(weight => $weight);
273 $page->param(port => $port);
274 $page->param(ttl => $ttl);
275 fill_rectypes($type);
276
277 } elsif ($webvar{recact} eq 'update') {
278
279 my ($code,$msg) = updateRec($dbh,$webvar{defrec},$webvar{id},
280 $webvar{name},$webvar{type},$webvar{address},$webvar{ttl},
281 $webvar{distance},$webvar{weight},$webvar{port});
282
283 if ($code eq 'OK') {
284 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
285 } else {
286 $page->param(failed => 1);
287 $page->param(errmsg => $msg);
288 $page->param(wastrying => "updating");
289 $page->param(todo => "Update record");
290 $page->param(recact => "update");
291 $page->param(parentid => $webvar{parentid});
292 $page->param(defrec => $webvar{defrec});
293 $page->param(id => $webvar{id});
294 fill_recdata();
295 }
296 }
297
298 if ($webvar{defrec} eq 'y') {
299 $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
300 } else {
301 $page->param(parentid => $webvar{parentid});
302# $page->param(id => $webvar{id});
303 $page->param(dohere => domainName($dbh,$webvar{parentid}));
304 }
305
306} elsif ($webvar{page} eq 'newrec') {
307 push @debugbits, "whee!\n";
308
309 # populate most fields as needed. (eg, type list.)
310 stdrecs();
311
312} elsif ($webvar{page} eq 'addrec') {
313
314 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
315 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
316 push @recargs, $webvar{distance};
317 if ($webvar{type} == $reverse_typemap{SRV}) {
318 push @recargs, $webvar{weight};
319 push @recargs, $webvar{port};
320 }
321 }
322# wtf?
323# push @recargs,
324 my ($code,$msg) = addRec(@recargs);
325
326 if ($code eq 'OK') {
327 showdomain($webvar{defrec},$webvar{parentid});
328# NB: should **really** redirect here, in case of reload. >_< eyowch.
329 } else {
330 $page->param(add_failed => 1);
331 $page->param(errmsg => $msg);
332 stdrecs($webvar{type}); # populate the form... er, mostly.
333 $page->param(name => $webvar{name});
334 $page->param(address => $webvar{address});
335 $page->param(distance => $webvar{distance})
336 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
337 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
338 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
339 }
340
341 $page->param(defrec => $webvar{defrec});
342
343} elsif ($webvar{page} eq 'conf_del') {
344
345 $page->param(id => $webvar{id});
346 $page->param(defrec => $webvar{defrec});
347
348 my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
349
350} elsif ($webvar{page} eq 'delrec') {
351
352 $page->param(id => $webvar{id});
353 $page->param(defrec => $webvar{defrec});
354 $page->param(parentid => $webvar{parentid});
355 # first pass = confirm y/n (sorta)
356 if (!defined($webvar{del})) {
357 $page->param(del_getconf => 1);
358 my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
359 $page->param(host => $rec{host});
360 $page->param(ftype => $typemap{$rec{type}});
361 $page->param(recval => $rec{val});
362 } elsif ($webvar{del} eq 'ok') {
363 my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
364 if ($code ne 'OK') {
365## need to find failure mode
366 $page->param(del_failed => 1);
367 $page->param(errmsg => $msg);
368 showdomain($webvar{defrec}, $webvar{parentid});
369 } else {
370 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
371 }
372 } else {
373 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
374 }
375
376} elsif ($webvar{page} eq 'editsoa') {
377
378 fillsoa($webvar{defrec},$webvar{id});
379
380} elsif ($webvar{page} eq 'updatesoa') {
381
382 my $sth;
383 my $sql = '';
384 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
385 # plus a bit of magic to update the appropriate table
386 $sql = "update ".($webvar{defrec} eq 'y' ? "default_records" : "records").
387 " set host='$webvar{prins}:$webvar{contact}',".
388 " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
389 " ttl=$webvar{ttl} where record_id=$webvar{recid}";
390 $sth = $dbh->prepare($sql);
391 $sth->execute;
392
393 if ($sth->err) {
394 $page->param(update_failed => 1);
395 $page->param(msg => $DBI::errstr);
396 fillsoa($webvar{defrec},$webvar{id});
397 } else {
398 changepage(page => "reclist", id => $webvar{id}, defrec => $webvar{defrec});
399 $page->param(update_failed => 0);
400##fixme! need to set group ID properly here
401# showdomain('y',1);
402 }
403
404} elsif ($webvar{page} eq 'adddomain') {
405 # Need some magic here.
406
407##fixme: Group should be variable
408 my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
409
410# hokay, a bit of magic to decide which page we hit.
411 if ($code eq 'OK') {
412# redirect to dns.cgi?etc&page=reclist
413 changepage(page => "reclist", id => $msg);
414# $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
415# showdomain(0,$msg);
416 } else {
417# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
418##fixme: session ID
419 $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
420 $page->param(add_failed => 1);
421 $page->param(domain => $webvar{domain});
422 $page->param(errmsg => $msg);
423 }
424
425} elsif ($webvar{page} eq 'grpman') {
426
427 listgroups();
428 $page->param(curpage => $webvar{page});
429
430} elsif ($webvar{page} eq 'newgrp') {
431
432 # do.. uhh.. stuff.. if we have no webvar{action}
433 if ($webvar{action} && $webvar{action} eq 'add') {
434 # not gonna provide the 4th param: template-or-clone flag, just yet
435 my ($code,$msg) = addGroup($dbh, $webvar{newgroup}, $webvar{pargroup});
436 changepage(page => "grpman") if $code eq 'OK';
437 $page->param(add_failed => 1);
438 $page->param(errmsg => $msg);
439 $page->param(newgroup => $webvar{newgroup});
440 fill_grouplist('pargroup',$webvar{pargroup});
441 } else {
442# $page->param
443 fill_grouplist('pargroup',$curgroup);
444
445 }
446
447} elsif ($webvar{page} eq 'delgrp') {
448
449 $page->param(id => $webvar{id});
450 # first pass = confirm y/n (sorta)
451 if (!defined($webvar{del})) {
452 $page->param(del_getconf => 1);
453# $page->param(groupname => groupName($dbh,$webvar{id}));
454# print some neato things?
455
456# } else {
457# #whether actually deleting or cancelling we redirect to the group list, default format
458
459 } elsif ($webvar{del} eq 'ok') {
460 my ($code,$msg) = delGroup($dbh, $webvar{id});
461push @debugbits, groupName($dbh, $webvar{id});
462 if ($code ne 'OK') {
463# need to find failure mode
464 $page->param(del_failed => 1);
465 $page->param(errmsg => $msg);
466 $page->param(curpage => $webvar{page});
467 listgroups();
468 } else {
469 # success. go back to the domain list, do not pass "GO"
470 changepage(page => "grpman");
471 }
472 } else {
473 # cancelled. whee!
474 changepage(page => "grpman");
475 }
476 $page->param(delgroupname => groupName($dbh, $webvar{id}));
477
478} elsif ($webvar{page} eq 'useradmin') {
479
480 if (defined($webvar{action})) {
481 userStatus($dbh,$webvar{id},$webvar{action});
482 }
483
484 $page->param(curpage => $webvar{page});
485
486 list_users();
487
488} elsif ($webvar{page} eq 'newuser') {
489
490 # foo?
491 fill_actypelist();
492
493} elsif ($webvar{page} eq 'adduser') {
494
495 my ($code,$msg);
496
497 if ($webvar{pass1} ne $webvar{pass2}) {
498 $code = 'FAIL';
499 $msg = "Passwords don't match";
500 } else {
501 ($code,$msg) = addUser($dbh,$webvar{uname}, $webvar{group}, $webvar{pass1},
502 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype},
503 $webvar{fname}, $webvar{lname}, $webvar{phone});
504 }
505
506# hokay, a bit of magic to decide which page we hit.
507 if ($code eq 'OK') {
508 changepage(page => "useradmin");
509 } else {
510# oddity - apparently, xhtml 1.0 strict swallows username as an HTML::Template var. O_o
511 $page->param(add_failed => 1);
512 $page->param(uname => $webvar{uname});
513 $page->param(fname => $webvar{fname});
514 $page->param(lname => $webvar{lname});
515 $page->param(pass1 => $webvar{pass1});
516 $page->param(pass2 => $webvar{pass2});
517 $page->param(errmsg => $msg);
518 fill_actypelist();
519 }
520
521# $page->param(add_failed => 1);
522
523} elsif ($webvar{page} eq 'deluser') {
524
525 $page->param(id => $webvar{id});
526 # first pass = confirm y/n (sorta)
527 if (!defined($webvar{del})) {
528 $page->param(del_getconf => 1);
529 $page->param(user => userFullName($dbh,$webvar{id}));
530 } elsif ($webvar{del} eq 'ok') {
531 my ($code,$msg) = delUser($dbh, $webvar{id});
532 if ($code ne 'OK') {
533# need to find failure mode
534 $page->param(del_failed => 1);
535 $page->param(errmsg => $msg);
536 list_users($curgroup);
537 } else {
538 # success. go back to the domain list, do not pass "GO"
539 changepage(page => "useradmin");
540 }
541 } else {
542 # cancelled. whee!
543 changepage(page => "useradmin");
544 }
545
546} elsif ($webvar{page} eq 'dnsq') {
547
548 $page->param(qfor => $webvar{qfor}) if $webvar{qfor};
549 fill_rectypes($webvar{type} ? $webvar{type} : '', 1);
550 $page->param(nrecurse => $webvar{nrecurse}) if $webvar{nrecurse};
551 $page->param(resolver => $webvar{resolver}) if $webvar{resolver};
552
553 if ($webvar{qfor}) {
554 my $resolv = Net::DNS::Resolver->new;
555 $resolv->tcp_timeout(5); # make me adjustable!
556 $resolv->udp_timeout(5); # make me adjustable!
557 $resolv->recurse(0) if $webvar{nrecurse};
558 $resolv->nameservers($webvar{resolver}) if $webvar{resolver};
559 my $query = $resolv->query($webvar{qfor}, $typemap{$webvar{type}});
560 if ($query) {
561
562 $page->param(showresults => 1);
563
564 my @answer;
565 foreach my $rr ($query->answer) {
566# next unless $rr->type eq "A" or $rr->type eq 'NS';
567 my %row;
568 my ($host,$ttl,$class,$type,$data) =
569 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/s);
570 $row{host} = $host;
571 $row{ftype} = $type;
572 $row{rdata} = ($type eq 'SOA' ? "<pre>$data</pre>" : $data);
573 push @answer, \%row;
574 }
575 $page->param(answer => \@answer);
576
577 my @additional;
578 foreach my $rr ($query->additional) {
579# next unless $rr->type eq "A" or $rr->type eq 'NS';
580 my %row;
581 my ($host,$ttl,$class,$type,$data) =
582 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
583 $row{host} = $host;
584 $row{ftype} = $type;
585 $row{rdata} = $data;
586 push @additional, \%row;
587 }
588 $page->param(additional => \@additional);
589
590 my @authority;
591 foreach my $rr ($query->authority) {
592# next unless $rr->type eq "A" or $rr->type eq 'NS';
593 my %row;
594 my ($host,$ttl,$class,$type,$data) =
595 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
596 $row{host} = $host;
597 $row{ftype} = $type;
598 $row{rdata} = $data;
599 push @authority, \%row;
600 }
601 $page->param(authority => \@authority);
602
603 $page->param(usedresolver => $resolv->answerfrom);
604 $page->param(frtype => $typemap{$webvar{type}});
605
606 } else {
607 $page->param(errmsg => $resolv->errorstring);
608 }
609 }
610 ## done DNS query
611
612} elsif ($webvar{page} eq 'axfr') {
613
614 # don't need this while we've got the dropdown in the menu. hmm.
615 #fill_grouplist;
616
617 $page->param(ifrom => $webvar{ifrom}) if $webvar{ifrom};
618 $page->param(rwsoa => $webvar{rwsoa}) if $webvar{rwsoa};
619 $page->param(rwns => $webvar{rwns}) if $webvar{rwns};
620 $page->param(dominactive => 1) if (!$webvar{domactive} && $webvar{doit}); # eww.
621 $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
622##work
623
624##fixme: check group too?
625 if ($webvar{doit} eq 'y' && !$webvar{ifrom}) {
626 $page->param(errmsg => "Need to set host to import from");
627 } elsif ($webvar{doit} eq 'y' && !$webvar{importdoms}) {
628 $page->param(errmsg => "Need domains to import");
629 } else {
630 my @domlist = split /\s+/, $webvar{importdoms};
631 my @results;
632my $rnum = 0;
633 foreach my $domain (@domlist) {
634 my %row;
635 my ($code,$msg) = importAXFR($dbh, $webvar{ifrom}, $domain, $webvar{group},
636 $webvar{domstatus}, $webvar{rwsoa}, $webvar{rwns});
637 $row{domok} = $msg if $code eq 'OK';
638 if ($code eq 'WARN') {
639 $msg =~ s|\n|<br />|g;
640 $row{domwarn} = $msg;
641 }
642 if ($code eq 'FAIL') {
643 $msg =~ s|\n|<br />|g;
644 $row{domerr} = $msg;
645 }
646 # do stuff! DNSDB::importAXFR($webvar{ifrom}, $webvar{rwsoa}, $webvar{rwns}, $domain, <flags>)
647 $row{domain} = $domain;
648# $row{row} = $rnum++;
649 push @results, \%row;
650 }
651 $page->param(axfrresults => \@results);
652 }
653
654} elsif ($webvar{page} eq 'whoisq') {
655
656 if ($webvar{qfor}) {
657 use Net::Whois::Raw;
658 use Text::Wrap;
659
660# caching useful?
661#$Net::Whois::Raw::CACHE_DIR = "/var/spool/pwhois/";
662#$Net::Whois::Raw::CACHE_TIME = 60;
663
664 my ($dominfo, $whois_server) = whois($webvar{qfor});
665##fixme: if we're given an IP, try rwhois as well as whois so we get the real final data
666
667 # le sigh. idjits spit out data without linefeeds...
668 $Text::Wrap::columns = 88;
669
670# &%$@%@# high-bit crap. We should probably find a way to properly recode these instead of one-by-one.
671# Mainly an XHTML validation thing.
672 $dominfo =~ s/\xa9/\&copy;/g;
673 $dominfo =~ s/\xae/\&reg;/g;
674
675 $page->param(qfor => $webvar{qfor});
676 $page->param(dominfo => wrap('','',$dominfo));
677 $page->param(whois_server => $whois_server);
678 } else {
679 $page->param(errmsg => "Missing host or domain to query in WHOIS") if $webvar{askaway};
680 }
681
682} elsif ($webvar{page} eq 'log') {
683
684##fixme put in some real log-munching stuff
685##fixme need to add bits to *create* log entries...
686 my $sth = $dbh->prepare("SELECT * FROM log");
687 $sth->execute;
688 my @logbits;
689 while (my @data = $sth->fetchrow_array) {
690 my %row;
691# (1,1,1,'test@test','Test','frobbed the whatsit',now());
692 $row{userfname} = $data[4];
693 $row{userid} = $data[1];
694 $row{useremail} = $data[3];
695 $row{logentry} = $data[5];
696 $row{logtime} = $data[6];
697 push @logbits, \%row;
698 }
699 $page->param(logentries => \@logbits);
700
701}
702
703
704# start output here so we can redirect pages.
705print "Content-type: text/html\n\n", $header->output;
706
707##common bits
708if ($webvar{page} ne 'login') {
709 $page->param(username => $session->param("username"));
710
711 $page->param(group => $curgroup);
712 $page->param(groupname => groupName($dbh,$curgroup));
713 $page->param(logingrp => groupName($dbh,$logingroup));
714
715 # group tree. should go elsewhere, probably
716 my $tmpgrplist = fill_grptree($logingroup,$curgroup);
717 $page->param(grptree => $tmpgrplist);
718
719 $page->param(inlogingrp => $curgroup == $logingroup);
720
721 # stuff for menu group change. nb: this is icky.
722 fill_grouplist("grouplist");
723# @#$%@%@#% XHTML - & in a URL must be escaped. >:(
724 my $tmp_ruri = $ENV{REQUEST_URI};
725 $tmp_ruri =~ s/\&([a-z])/\&amp\;$1/g;
726# le sigh. and we need to strip any previous action
727 $tmp_ruri =~ s/\&amp;action=[^&]+//g;
728
729##fixme: need to sort out how to propagate these without dragging them along too far, and <n>plicating them
730# and any bits from the "starts with" letter block - (ab)using this widget
731# to fill the bulk of the URI so various templates don't grow to insanity
732 $tmp_ruri =~ s/\&amp;startwith=[a-z09-]+//g;
733# search box webvars
734 $tmp_ruri =~ s/\&amp;searchsubs=[a-z09-]+//g;
735 $tmp_ruri =~ s/\&amp;filter=[a-z09-]+//g;
736
737# $page->param(whereami => $ENV{REQUEST_URI});
738 $page->param(whereami => $tmp_ruri);
739}
740
741foreach (@debugbits) { print; }
742
743# spit it out
744print $page->output;
745
746if ($debugenv) {
747 print "<div id=\"debug\">webvar keys: <pre>\n";
748 foreach my $key (keys %webvar) {
749 print "key: $key\tval: $webvar{$key}\n";
750 }
751 print "</pre>\nsession:\n<pre>\n";
752 my $sesdata = $session->dataref();
753 foreach my $key (keys %$sesdata) {
754 print "key: $key\tval: ".$sesdata->{$key}."\n";
755 }
756 print "</pre>\nENV:\n<pre>\n";
757 foreach my $key (keys %ENV) {
758 print "key: $key\tval: $ENV{$key}\n";
759 }
760 print "</pre></div>\n";
761}
762
763print $footer->output;
764
765# as per the docs, Just In Case
766$session->flush();
767
768exit 0;
769
770
771sub fill_grptree {
772 my $root = shift;
773 my $cur = shift;
774
775 my @childlist;
776
777 my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
778 getChildren($dbh,$root,\@childlist,'immediate');
779 return if $#childlist == -1;
780 my @grouplist;
781 foreach (@childlist) {
782 my %row;
783 $row{grpname} = groupName($dbh,$_);
784 $row{grpname} = "<b>$row{grpname}</b>" if $_ == $cur;
785 $row{subs} = fill_grptree($_,$cur);
786 push @grouplist, \%row;
787 }
788 $grptree->param(treelvl => \@grouplist);
789 return $grptree->output;
790}
791
792
793sub changepage {
794 my %params = @_; # think this works the way I want...
795
796 # handle user check
797 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
798 foreach (keys %params) {
799 $newurl .= "&$_=$params{$_}";
800 }
801
802 # Just In Case
803 $session->flush();
804
805 print "Status: 302\nLocation: $newurl\n\n";
806 exit;
807} # end changepage
808
809
810sub fillsoa {
811 my $def = shift;
812 my $id = shift;
813 my $domname = ($def eq 'y' ? '' : "DOMAIN");
814
815 $page->param(defrec => $def);
816
817# i had a good reason to do this when I wrote it...
818# $page->param(domain => $domname);
819# $page->param(group => $DNSDB::group);
820 $page->param(isgrp => 1) if $def eq 'y';
821 $page->param(parent => ($def eq 'y' ? groupName($dbh, $DNSDB::group) : domainName($dbh, $id)) );
822
823# defaults
824 $page->param(defcontact => $DNSDB::def{contact});
825 $page->param(defns => $DNSDB::def{prins});
826 $page->param(defsoattl => $DNSDB::def{soattl});
827 $page->param(defrefresh => $DNSDB::def{refresh});
828 $page->param(defretry => $DNSDB::def{retry});
829 $page->param(defexpire => $DNSDB::def{expire});
830 $page->param(defminttl => $DNSDB::def{minttl});
831
832 # there are probably better ways to do this. TMTOWTDI.
833 my %soa = getSOA($dbh,$def,$id);
834
835 $page->param(id => $id);
836 $page->param(recid => $soa{recid});
837 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
838 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
839 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
840 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
841 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
842 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
843 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
844}
845
846sub showdomain {
847 my $def = shift;
848 my $id = shift;
849
850 # get the SOA first
851 my %soa = getSOA($dbh,$def,$id);
852
853 $page->param(recid => $soa{recid});
854 $page->param(contact => $soa{contact});
855 $page->param(prins => $soa{prins});
856 $page->param(refresh => $soa{refresh});
857 $page->param(retry => $soa{retry});
858 $page->param(expire => $soa{expire});
859 $page->param(minttl => $soa{minttl});
860 $page->param(ttl => $soa{ttl});
861
862# my @foo2 = getDomRecs($dbh,'def',1);
863 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
864
865 my $row = 0;
866 foreach my $rec (@$foo2) {
867 $rec->{type} = $typemap{$rec->{type}};
868 $rec->{row} = $row % 2;
869 $rec->{defrec} = $webvar{defrec};
870 $rec->{sid} = $webvar{sid};
871 $rec->{id} = $id;
872 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
873 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
874 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
875 $row++;
876 }
877 $page->param(reclist => $foo2);
878}
879
880
881# fill in record type list on add/update/edit record template
882sub fill_rectypes {
883 my $type = shift || $reverse_typemap{A};
884 my $soaflag = shift || 0;
885
886 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
887 $sth->execute;
888 my @typelist;
889 while (my ($rval,$rname) = $sth->fetchrow_array()) {
890 my %row = ( recval => $rval, recname => $rname );
891 $row{tselect} = 1 if $rval == $type;
892 push @typelist, \%row;
893 }
894 if ($soaflag) {
895 my %row = ( recval => $reverse_typemap{SOA}, recname => 'SOA' );
896 $row{tselect} = 1 if $reverse_typemap{SOA} == $type;
897 push @typelist, \%row;
898 }
899 $page->param(typelist => \@typelist);
900} # fill_rectypes
901
902sub fill_recdata {
903 fill_rectypes($webvar{type});
904
905 $page->param(name => $webvar{name});
906 $page->param(address => $webvar{address});
907 $page->param(distance => $webvar{distance})
908 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
909 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
910 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
911 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
912}
913
914
915sub fill_actypelist {
916 my @actypes;
917
918 my %row1 = (actypeval => 'u', actypename => 'user');
919 $row1{typesel} = 1 if $webvar{accttype} eq 'u';
920 push @actypes, \%row1;
921
922 my %row2 = (actypeval => 'S', actypename => 'superuser');
923 $row2{typesel} = 1 if $webvar{accttype} eq 'S';
924 push @actypes, \%row2;
925
926 $page->param(actypelist => \@actypes);
927}
928
929
930sub fill_fpnla {
931 my $count = shift;
932##fixme
933 if ($offset eq 'all') {
934# uhm....
935 } else {
936 # all these bits only have sensible behaviour if offset is numeric. err, probably.
937 if ($count > $perpage) {
938 # if there are more results than the default, always show the "all" link
939 $page->param(navall => 1);
940
941 if ($offset > 0) {
942 $page->param(navfirst => 1);
943 $page->param(navprev => 1);
944 $page->param(prevoffs => $offset-1);
945 }
946
947 # show "next" and "last" links if we're not on the last page of results
948 if ( (($offset+1) * $perpage - $count) < 0 ) {
949 $page->param(navnext => 1);
950 $page->param(nextoffs => $offset+1);
951 $page->param(navlast => 1);
952 $page->param(lastoffs => int (($count-1)/$perpage));
953 }
954 }
955 }
956} # end fill_fpnla()
957
958
959sub fill_pgcount {
960 my $pgcount = shift;
961 my $pgtype = shift;
962 my $parent = shift;
963
964 $page->param(ntot => $pgcount);
965 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
966 $page->param(npglast => ($offset eq 'all' ? $pgcount :
967 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
968 ));
969 $page->param(pgtype => $pgtype);
970 $page->param(parent => $parent);
971} # end fill_pgcount()
972
973
974sub listdomains {
975
976##fixme: $logingroup or$curgroup?
977 my @childgroups;
978 getChildren($dbh, $logingroup, \@childgroups, 'all');
979 my $childlist = join(',',@childgroups);
980
981 my $sql = "SELECT count(*) FROM domains WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
982 (defined($webvar{startwith}) ? " AND domain ~* '^[$webvar{startwith}]'" : '').
983 (defined($webvar{filter}) ? " AND domain ~* '$webvar{filter}'" : '');
984 my $sth = $dbh->prepare($sql);
985 $sth->execute;
986 my ($count) = $sth->fetchrow_array;
987
988# fill page count and first-previous-next-last-all bits
989##fixme - hardcoded group bit
990 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
991 fill_fpnla($count);
992
993# sort/order
994 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
995 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
996
997 $sortby = $session->param($webvar{page}.'sortby');
998 $sortorder = $session->param($webvar{page}.'order');
999
1000# set up the headers
1001 my @cols = ('domain', 'status', 'group');
1002 my %colheads = (domain => 'Domain', status => 'Status', group => 'Group');
1003 fill_colheads(\@cols, \%colheads);
1004
1005# $page->param(sortorder => $sortorder);
1006 # hack! hack! pthbttt. have to rethink the status column storage,
1007 # or inactive comes "before" active. *sigh*
1008 $sortorder = ($sortorder eq 'ASC' ? 'DESC' : 'ASC') if $sortby eq 'status';
1009
1010# waffle, waffle - keep state on these as well as sortby, sortorder?
1011 $page->param("start$webvar{startwith}" => 1) if $webvar{startwith} && $webvar{startwith} =~ /^[a-z]$/;
1012
1013 $page->param(filter => $webvar{filter}) if $webvar{filter};
1014
1015##fixme
1016##fixme push the SQL and direct database fiddling off into a sub in DNSDB.pm
1017##fixme
1018
1019 $page->param(group => $curgroup);
1020 my @domlist;
1021 $sql = "SELECT domain_id,domain,status,groups.group_name AS group FROM domains".
1022 " INNER JOIN groups ON domains.group_id=groups.group_id".
1023 " WHERE domains.group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
1024##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
1025 (defined($webvar{startwith}) ? " AND domain ~* '^[$webvar{startwith}]'" : '').
1026 (defined($webvar{filter}) ? " AND domain ~* '$webvar{filter}'" : '').
1027 " ORDER BY ".($sortby eq 'group' ? 'groups.group_name' : $sortby).
1028 " $sortorder ".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
1029 $sth = $dbh->prepare($sql);
1030 $sth->execute;
1031 my $rownum = 0;
1032 while (my @data = $sth->fetchrow_array) {
1033 my %row;
1034 $row{domainid} = $data[0];
1035 $row{domain} = $data[1];
1036 $row{status} = ($data[2] ? 'Active' : 'Inactive');
1037 $row{group} = $data[3];
1038 $row{bg} = ($rownum++)%2;
1039# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
1040 $row{mkactive} = !$data[2];
1041 $row{sid} = $sid;
1042 $row{offset} = $offset;
1043##fixme: need to clean up status indicator/usage/inversion
1044 push @domlist, \%row;
1045 }
1046 $page->param(domtable => \@domlist);
1047} # end listdomains()
1048
1049
1050sub listgroups {
1051 my @childgroups;
1052 getChildren($dbh, $logingroup, \@childgroups, 'all');
1053 my $childlist = join(',',@childgroups);
1054
1055 my $sql = "SELECT count(*) FROM groups WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
1056 (defined($webvar{startwith}) ? " AND group_name ~* '^[$webvar{startwith}]'" : '').
1057 (defined($webvar{filter}) ? " AND group_name ~* '$webvar{filter}'" : '');
1058 my $sth = $dbh->prepare($sql);
1059
1060 $sth->execute;
1061 my ($count) = ($sth->fetchrow_array);
1062# fill page count and first-previous-next-last-all bits
1063##fixme - hardcoded group bit
1064 fill_pgcount($count,"groups",'');
1065 fill_fpnla($count);
1066
1067# $sortby = 'group';
1068# sort/order
1069 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1070 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
1071
1072 $sortby = $session->param($webvar{page}.'sortby');
1073 $sortorder = $session->param($webvar{page}.'order');
1074
1075# set up the headers
1076 my @cols = ('group','parent','nusers','ndomains');
1077 my %colnames = (group => 'Group', parent => 'Parent Group', nusers => 'Users', ndomains => 'Domains');
1078 fill_colheads(\@cols, \%colnames);
1079
1080# waffle, waffle - keep state on these as well as sortby, sortorder?
1081 $page->param("start$webvar{startwith}" => 1) if $webvar{startwith} && $webvar{startwith} =~ /^[a-z]$/;
1082
1083 $page->param(filter => $webvar{filter}) if $webvar{filter};
1084
1085# munge sortby for columns in database
1086 $sortby = 'g.group_name' if $sortby eq 'group';
1087 $sortby = 'g2.group_name' if $sortby eq 'parent';
1088
1089 my @grouplist;
1090 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
1091 "count(distinct(u.username)) AS nusers, count(distinct(d.domain)) AS ndomains ".
1092 "FROM groups g ".
1093 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
1094 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
1095 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
1096 "WHERE g.group_id IN ($logingroup".($childlist ? ",$childlist" : '').") ".
1097##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
1098 (defined($webvar{startwith}) ? " AND g.group_name ~* '^[$webvar{startwith}]'" : '').
1099 (defined($webvar{filter}) ? " AND g.group_name ~* '$webvar{filter}'" : '').
1100 " GROUP BY g.group_id, g.group_name, g2.group_name ".
1101 " ORDER BY $sortby $sortorder ".
1102 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
1103 $sth->execute;
1104
1105 my $rownum = 0;
1106 while (my @data = $sth->fetchrow_array) {
1107 my %row;
1108 $row{groupid} = $data[0];
1109 $row{groupname} = $data[1];
1110 $row{pgroup} = $data[2];
1111 $row{nusers} = $data[3];
1112 $row{ndomains} = $data[4];
1113 $row{bg} = ($rownum++)%2;
1114 $row{sid} = $sid;
1115 push @grouplist, \%row;
1116 }
1117 $page->param(grouptable => \@grouplist);
1118} # end listgroups()
1119
1120
1121sub fill_grouplist {
1122 my $template_var = shift;
1123 my $cur = shift || $curgroup;
1124
1125 my @childgroups;
1126 getChildren($dbh, $logingroup, \@childgroups, 'all');
1127 my $childlist = join(',',@childgroups);
1128
1129 # weesa gonna discard parent_group_id for now
1130 my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ".
1131 "WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
1132 "ORDER BY group_id");
1133 $sth->execute;
1134 my @grouplist;
1135 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
1136 my %row;
1137 $row{groupname} = $groupname;
1138 $row{groupval} = $groupid;
1139##fixme: need magic
1140# $row{defgroup} = '';
1141 $row{groupactive} = 1 if $groupid == $cur;
1142 push @grouplist, \%row;
1143 }
1144
1145 $page->param("$template_var" => \@grouplist);
1146
1147} # end fill_grouplist()
1148
1149
1150sub list_users {
1151
1152 my @childgroups;
1153 getChildren($dbh, $curgroup, \@childgroups, 'all') if $webvar{searchsubs};
1154 my $childlist = join(',',@childgroups);
1155
1156 my $sql = "SELECT count(*) FROM users WHERE group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
1157 (defined($webvar{startwith}) ? " AND username ~* '^[$webvar{startwith}]'" : '').
1158 (defined($webvar{filter}) ? " AND username ~* '$webvar{filter}'" : '');
1159 my $sth = $dbh->prepare($sql);
1160 $sth->execute;
1161 my ($count) = ($sth->fetchrow_array);
1162
1163# fill page count and first-previous-next-last-all bits
1164##fixme - hardcoded group bit
1165 fill_pgcount($count,"users",'');
1166 fill_fpnla($count);
1167
1168# $sortby = 'user';
1169# sort/order
1170 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1171 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
1172
1173 $sortby = $session->param($webvar{page}.'sortby');
1174 $sortorder = $session->param($webvar{page}.'order');
1175
1176# set up the headers
1177 my @cols = ('user','fname','type','group','status');
1178 my %colnames = (user => 'Username', fname => 'Full Name', type => 'Type', group => 'Group', status => 'Status');
1179 fill_colheads(\@cols, \%colnames);
1180
1181# waffle, waffle - keep state on these as well as sortby, sortorder?
1182 $page->param("start$webvar{startwith}" => 1) if $webvar{startwith} && $webvar{startwith} =~ /^[a-z]$/;
1183
1184 $page->param(filter => $webvar{filter}) if $webvar{filter};
1185 $page->param(searchsubs => $webvar{searchsubs}) if $webvar{searchsubs};
1186
1187# munge sortby for columns in database
1188 $sortby = 'u.username' if $sortby eq 'user';
1189 $sortby = 'u.type' if $sortby eq 'type';
1190 $sortby = 'g.group_name' if $sortby eq 'group';
1191 $sortby = 'u.status' if $sortby eq 'status';
1192
1193 my @userlist;
1194 $sql = "SELECT u.user_id, u.username, u.firstname || ' ' || u.lastname AS fname, u.type, g.group_name, u.status ".
1195 "FROM users u ".
1196 "INNER JOIN groups g ON u.group_id=g.group_id ".
1197 "WHERE u.group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
1198##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
1199 (defined($webvar{startwith}) ? " AND u.username ~* '^[$webvar{startwith}]'" : '').
1200 (defined($webvar{filter}) ? " AND u.username ~* '$webvar{filter}'" : '').
1201 " ORDER BY $sortby $sortorder ".
1202 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
1203
1204# " GROUP BY g.group_id, g.group_name, g2.group_name ".
1205# ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
1206
1207 $sth = $dbh->prepare($sql);
1208 $sth->execute;
1209
1210 my $rownum = 0;
1211 while (my @data = $sth->fetchrow_array) {
1212 no warnings "uninitialized"; # Just In Case something stupid happens and a user gets no first or last name
1213 my %row;
1214 $row{userid} = $data[0];
1215 $row{username} = $data[1];
1216 $row{userfull} = $data[2];
1217 $row{usertype} = ($data[3] eq 'S' ? 'superuser' : "user");
1218 $row{usergroup} = $data[4];
1219 $row{active} = $data[5];
1220 $row{bg} = ($rownum++)%2;
1221 $row{sid} = $sid;
1222 push @userlist, \%row;
1223 }
1224 $page->param(usertable => \@userlist);
1225}
1226
1227# Generate all of the glop necessary to add or not the appropriate marker/flag for
1228# the sort order and column in domain, user, group, and record lists
1229# Takes an array ref and hash ref
1230sub fill_colheads {
1231 my $cols = shift;
1232 my $colnames = shift;
1233
1234 my @headings;
1235
1236 foreach my $col (@$cols) {
1237 my %coldata;
1238 $coldata{firstcol} = 1 if $col eq $cols->[0];
1239 $coldata{sid} = $sid;
1240 $coldata{page} = $webvar{page};
1241 $coldata{offset} = $webvar{offset} if $webvar{offset};
1242 $coldata{sortby} = $col;
1243 $coldata{colname} = $colnames->{$col};
1244 if ($col eq $sortby) {
1245 $coldata{order} = ($sortorder eq 'ASC' ? 'DESC' : 'ASC');
1246 $coldata{sortorder} = $sortorder;
1247 } else {
1248 $coldata{order} = 'ASC';
1249 }
1250 push @headings, \%coldata;
1251 }
1252
1253 $page->param(colheads => \@headings);
1254
1255} # end gentableheadings
Note: See TracBrowser for help on using the repository browser.