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
RevLine 
[2]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###
[3]9# Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx>
[2]10
11use strict;
12use warnings;
13
14use CGI::Carp qw (fatalsToBrowser);
15use CGI::Simple;
16use HTML::Template;
17use CGI::Session;
[29]18use Crypt::PasswdMD5;
[30]19use Net::DNS;
[2]20use DBI;
21
22use lib '.';
23# custom modules
24use DNSDB qw(:ALL);
25
[13]26my @debugbits; # temp, to be spit out near the end of processing
[38]27my $debugenv = 0;
[13]28
[2]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...
[7]39my %webvar = $q->Vars;
[2]40
[13]41# persistent stuff needed on most/all pages
[2]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.
[18]50 $session->param('logingroup',1);
51 $session->param('curgroup',1); # yes, we *do* need to track this too. er, probably.
[51]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');
[2]60}
61
[19]62my $logingroup = ($session->param('logingroup') ? $session->param('logingroup') : 1);
63my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $logingroup);
[17]64my $group = ($webvar{group} ? $webvar{group} : 1);
[18]65
[26]66# nrgh, can't handle login here because we don't have a database handle to check the user/pass with yet
[2]67
[20]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
[2]75my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
76my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
77
78# default
[38]79#my $perpage = 15;
80my $perpage = 3;
[2]81my $offset = ($webvar{offset} ? $webvar{offset} : 0);
82
83# NB: these must match the field name and SQL ascend/descend syntax respectively
[41]84my $sortby = "domain";
85my $sortorder = "ASC";
[2]86
[29]87my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret","dbhost");
[2]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
[26]97# handle login redirect
[30]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);
[26]104
[30]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 }
[29]110
[30]111 # set session bits
112 $session->param('logingroup',$gid);
113 $session->param('curgroup',$gid);
114 $session->param('username',$webvar{username});
[26]115
[30]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 }
[26]128}
129
[15]130## Default page is a login page
131#my $page; # to be initialized as an HTML::Template entity sooner or later
[2]132
[3]133
134
[2]135# decide which page to spit out...
[15]136$webvar{page} = 'login' if !$webvar{page};
137#if (!$webvar{page}) {
138# $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
139#} else {
140#}
[2]141
[15]142my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
143
[2]144$page->param(sid => $sid);
145
[26]146if ($webvar{page} eq 'login') {
[3]147
[26]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
[3]154# hmm. seeing problems in some possibly-not-so-corner cases.
[10]155# this currently only handles "domain on", "domain off"
[3]156 if (defined($webvar{action})) {
157 domStatus($dbh,$webvar{id},$webvar{action});
158 }
159
[18]160 $page->param(curpage => $webvar{page});
161
[11]162 listdomains();
[2]163
[4]164} elsif ($webvar{page} eq 'newdomain') {
[2]165
166
[11]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);
[22]185 listdomains($curgroup);
[11]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
[47]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
[13]223} elsif ($webvar{page} eq 'record') {
[16]224
[13]225 if ($webvar{recact} eq 'new') {
[16]226
[15]227 $page->param(todo => "Add record to");
228 $page->param(recact => "add");
[16]229 fill_rectypes();
230
[15]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 {
[24]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});
[16]255 fill_recdata(); # populate the form... er, mostly.
[15]256 }
257
[13]258 } elsif ($webvar{recact} eq 'edit') {
[15]259
[16]260 $page->param(todo => "Update record");
261 $page->param(recact => "update");
262 $page->param(parentid => $webvar{parentid});
[17]263 $page->param(id => $webvar{id});
[16]264 $page->param(defrec => $webvar{defrec});
[13]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);
[16]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') {
[17]284 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
[16]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});
[17]293 $page->param(id => $webvar{id});
[16]294 fill_recdata();
295 }
[13]296 }
[16]297
[13]298 if ($webvar{defrec} eq 'y') {
[20]299 $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
[13]300 } else {
[24]301 $page->param(parentid => $webvar{parentid});
302# $page->param(id => $webvar{id});
[16]303 $page->param(dohere => domainName($dbh,$webvar{parentid}));
[13]304 }
305
[2]306} elsif ($webvar{page} eq 'newrec') {
[13]307 push @debugbits, "whee!\n";
[2]308
[3]309 # populate most fields as needed. (eg, type list.)
[13]310 stdrecs();
[2]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 }
[13]322# wtf?
323# push @recargs,
[2]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);
[13]332 stdrecs($webvar{type}); # populate the form... er, mostly.
[2]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
[3]341 $page->param(defrec => $webvar{defrec});
342
[2]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});
[39]354 $page->param(parentid => $webvar{parentid});
[2]355 # first pass = confirm y/n (sorta)
356 if (!defined($webvar{del})) {
357 $page->param(del_getconf => 1);
[3]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});
[39]362 } elsif ($webvar{del} eq 'ok') {
[3]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);
[39]368 showdomain($webvar{defrec}, $webvar{parentid});
369 } else {
370 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
[3]371 }
[39]372 } else {
373 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
[2]374 }
375
376} elsif ($webvar{page} eq 'editsoa') {
377
[39]378 fillsoa($webvar{defrec},$webvar{id});
[2]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
[39]386 $sql = "update ".($webvar{defrec} eq 'y' ? "default_records" : "records").
[2]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);
[39]396 fillsoa($webvar{defrec},$webvar{id});
[2]397 } else {
[39]398 changepage(page => "reclist", id => $webvar{id}, defrec => $webvar{defrec});
[2]399 $page->param(update_failed => 0);
400##fixme! need to set group ID properly here
[39]401# showdomain('y',1);
[2]402 }
403
404} elsif ($webvar{page} eq 'adddomain') {
405 # Need some magic here.
406
407##fixme: Group should be variable
[20]408 my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
[2]409
410# hokay, a bit of magic to decide which page we hit.
411 if ($code eq 'OK') {
[11]412# redirect to dns.cgi?etc&page=reclist
[12]413 changepage(page => "reclist", id => $msg);
[24]414# $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
415# showdomain(0,$msg);
[2]416 } else {
417# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
[24]418##fixme: session ID
[2]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
[17]425} elsif ($webvar{page} eq 'grpman') {
[2]426
[22]427 listgroups();
[18]428 $page->param(curpage => $webvar{page});
429
[17]430} elsif ($webvar{page} eq 'newgrp') {
[20]431
[18]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});
[20]440 fill_grouplist('pargroup',$webvar{pargroup});
[19]441 } else {
442# $page->param
[20]443 fill_grouplist('pargroup',$curgroup);
[19]444
[18]445 }
[20]446
[22]447} elsif ($webvar{page} eq 'delgrp') {
[20]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);
[23]453# $page->param(groupname => groupName($dbh,$webvar{id}));
[20]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});
[23]461push @debugbits, groupName($dbh, $webvar{id});
[20]462 if ($code ne 'OK') {
463# need to find failure mode
464 $page->param(del_failed => 1);
465 $page->param(errmsg => $msg);
[22]466 $page->param(curpage => $webvar{page});
467 listgroups();
[20]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 }
[23]476 $page->param(delgroupname => groupName($dbh, $webvar{id}));
[24]477
478} elsif ($webvar{page} eq 'useradmin') {
479
[51]480 if (defined($webvar{action})) {
481 userStatus($dbh,$webvar{id},$webvar{action});
482 }
483
[24]484 $page->param(curpage => $webvar{page});
485
[51]486 list_users();
487
[24]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 {
[38]501 ($code,$msg) = addUser($dbh,$webvar{uname}, $webvar{group}, $webvar{pass1},
[25]502 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype},
503 $webvar{fname}, $webvar{lname}, $webvar{phone});
[24]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 {
[38]510# oddity - apparently, xhtml 1.0 strict swallows username as an HTML::Template var. O_o
[24]511 $page->param(add_failed => 1);
[38]512 $page->param(uname => $webvar{uname});
[24]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
[38]521# $page->param(add_failed => 1);
[25]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
[30]546} elsif ($webvar{page} eq 'dnsq') {
547
548 $page->param(qfor => $webvar{qfor}) if $webvar{qfor};
[31]549 fill_rectypes($webvar{type} ? $webvar{type} : '', 1);
550 $page->param(nrecurse => $webvar{nrecurse}) if $webvar{nrecurse};
[30]551 $page->param(resolver => $webvar{resolver}) if $webvar{resolver};
552
553 if ($webvar{qfor}) {
554 my $resolv = Net::DNS::Resolver->new;
[31]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};
[30]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) =
[31]569 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/s);
[30]570 $row{host} = $host;
571 $row{ftype} = $type;
[31]572 $row{rdata} = ($type eq 'SOA' ? "<pre>$data</pre>" : $data);
[30]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
[31]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};
[37]620 $page->param(dominactive => 1) if (!$webvar{domactive} && $webvar{doit}); # eww.
[31]621 $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
622##work
[33]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) {
[34]634 my %row;
635 my ($code,$msg) = importAXFR($dbh, $webvar{ifrom}, $domain, $webvar{group},
636 $webvar{domstatus}, $webvar{rwsoa}, $webvar{rwns});
[35]637 $row{domok} = $msg if $code eq 'OK';
638 if ($code eq 'WARN') {
639 $msg =~ s|\n|<br />|g;
640 $row{domwarn} = $msg;
641 }
[37]642 if ($code eq 'FAIL') {
643 $msg =~ s|\n|<br />|g;
644 $row{domerr} = $msg;
645 }
[33]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
[48]654} elsif ($webvar{page} eq 'whoisq') {
[47]655
[48]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
[47]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
[2]701}
702
703
[17]704# start output here so we can redirect pages.
[7]705print "Content-type: text/html\n\n", $header->output;
706
[20]707##common bits
[17]708if ($webvar{page} ne 'login') {
[30]709 $page->param(username => $session->param("username"));
710
[20]711 $page->param(group => $curgroup);
712 $page->param(groupname => groupName($dbh,$curgroup));
[43]713 $page->param(logingrp => groupName($dbh,$logingroup));
[20]714
[24]715 # group tree. should go elsewhere, probably
716 my $tmpgrplist = fill_grptree($logingroup,$curgroup);
717 $page->param(grptree => $tmpgrplist);
718
[42]719 $page->param(inlogingrp => $curgroup == $logingroup);
720
[20]721 # stuff for menu group change. nb: this is icky.
722 fill_grouplist("grouplist");
[38]723# @#$%@%@#% XHTML - & in a URL must be escaped. >:(
724 my $tmp_ruri = $ENV{REQUEST_URI};
725 $tmp_ruri =~ s/\&([a-z])/\&amp\;$1/g;
[39]726# le sigh. and we need to strip any previous action
727 $tmp_ruri =~ s/\&amp;action=[^&]+//g;
[52]728
729##fixme: need to sort out how to propagate these without dragging them along too far, and <n>plicating them
[41]730# and any bits from the "starts with" letter block - (ab)using this widget
[42]731# to fill the bulk of the URI so various templates don't grow to insanity
[41]732 $tmp_ruri =~ s/\&amp;startwith=[a-z09-]+//g;
[52]733# search box webvars
734 $tmp_ruri =~ s/\&amp;searchsubs=[a-z09-]+//g;
735 $tmp_ruri =~ s/\&amp;filter=[a-z09-]+//g;
736
[38]737# $page->param(whereami => $ENV{REQUEST_URI});
738 $page->param(whereami => $tmp_ruri);
[17]739}
[13]740
[24]741foreach (@debugbits) { print; }
742
[2]743# spit it out
744print $page->output;
745
[38]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";
[2]761}
762
763print $footer->output;
764
[18]765# as per the docs, Just In Case
766$session->flush();
[2]767
768exit 0;
769
770
[24]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
[11]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
[30]802 # Just In Case
803 $session->flush();
804
[11]805 print "Status: 302\nLocation: $newurl\n\n";
806 exit;
807} # end changepage
808
809
[2]810sub fillsoa {
811 my $def = shift;
812 my $id = shift;
[39]813 my $domname = ($def eq 'y' ? '' : "DOMAIN");
[2]814
[39]815 $page->param(defrec => $def);
[2]816
[39]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)) );
[2]822
823# defaults
[17]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});
[2]831
832 # there are probably better ways to do this. TMTOWTDI.
833 my %soa = getSOA($dbh,$def,$id);
834
[39]835 $page->param(id => $id);
[2]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);
[4]863 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
[2]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};
[13]871 $rec->{id} = $id;
[23]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');
[2]875 $row++;
876 }
877 $page->param(reclist => $foo2);
878}
879
[23]880
[16]881# fill in record type list on add/update/edit record template
882sub fill_rectypes {
[13]883 my $type = shift || $reverse_typemap{A};
[31]884 my $soaflag = shift || 0;
[13]885
[17]886 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
[2]887 $sth->execute;
888 my @typelist;
889 while (my ($rval,$rname) = $sth->fetchrow_array()) {
890 my %row = ( recval => $rval, recname => $rname );
[13]891 $row{tselect} = 1 if $rval == $type;
[2]892 push @typelist, \%row;
893 }
[31]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 }
[2]899 $page->param(typelist => \@typelist);
[31]900} # fill_rectypes
[16]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};
[2]911 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
912}
[7]913
[24]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
[7]930sub fill_fpnla {
931 my $count = shift;
932##fixme
933 if ($offset eq 'all') {
[41]934# uhm....
[7]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);
[8]952 $page->param(lastoffs => int (($count-1)/$perpage));
[7]953 }
954 }
955 }
[10]956} # end fill_fpnla()
957
958
[12]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
[11]974sub listdomains {
[41]975
[52]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;
[17]986 my ($count) = $sth->fetchrow_array;
987
[12]988# fill page count and first-previous-next-last-all bits
989##fixme - hardcoded group bit
[20]990 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
[10]991 fill_fpnla($count);
992
[41]993# sort/order
[51]994 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
995 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[41]996
[51]997 $sortby = $session->param($webvar{page}.'sortby');
998 $sortorder = $session->param($webvar{page}.'order');
999
[44]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);
[41]1004
[47]1005# $page->param(sortorder => $sortorder);
[41]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
[51]1010# waffle, waffle - keep state on these as well as sortby, sortorder?
[41]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
[20]1019 $page->param(group => $curgroup);
[10]1020 my @domlist;
[52]1021 $sql = "SELECT domain_id,domain,status,groups.group_name AS group FROM domains".
[20]1022 " INNER JOIN groups ON domains.group_id=groups.group_id".
[52]1023 " WHERE domains.group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
[41]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);
[52]1030 $sth->execute;
[10]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);
[11]1047} # end listdomains()
[18]1048
1049
[22]1050sub listgroups {
[26]1051 my @childgroups;
1052 getChildren($dbh, $logingroup, \@childgroups, 'all');
1053 my $childlist = join(',',@childgroups);
1054
[52]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}'" : '');
[26]1058 my $sth = $dbh->prepare($sql);
1059
[22]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
[51]1067# $sortby = 'group';
[42]1068# sort/order
[51]1069 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1070 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[42]1071
[51]1072 $sortby = $session->param($webvar{page}.'sortby');
1073 $sortorder = $session->param($webvar{page}.'order');
1074
[44]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);
[42]1079
[51]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
[22]1089 my @grouplist;
1090 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
[51]1091 "count(distinct(u.username)) AS nusers, count(distinct(d.domain)) AS ndomains ".
[22]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 ".
[26]1096 "WHERE g.group_id IN ($logingroup".($childlist ? ",$childlist" : '').") ".
[51]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));
[22]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
[20]1121sub fill_grouplist {
[19]1122 my $template_var = shift;
1123 my $cur = shift || $curgroup;
[26]1124
1125 my @childgroups;
1126 getChildren($dbh, $logingroup, \@childgroups, 'all');
1127 my $childlist = join(',',@childgroups);
1128
[18]1129 # weesa gonna discard parent_group_id for now
[26]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");
[18]1133 $sth->execute;
[20]1134 my @grouplist;
1135 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
[18]1136 my %row;
[20]1137 $row{groupname} = $groupname;
1138 $row{groupval} = $groupid;
[18]1139##fixme: need magic
[20]1140# $row{defgroup} = '';
1141 $row{groupactive} = 1 if $groupid == $cur;
1142 push @grouplist, \%row;
[18]1143 }
1144
[20]1145 $page->param("$template_var" => \@grouplist);
[18]1146
[24]1147} # end fill_grouplist()
1148
[26]1149
[24]1150sub list_users {
[52]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;
[24]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
[51]1168# $sortby = 'user';
[44]1169# sort/order
[51]1170 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1171 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[44]1172
[51]1173 $sortby = $session->param($webvar{page}.'sortby');
1174 $sortorder = $session->param($webvar{page}.'order');
1175
[44]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
[51]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};
[52]1185 $page->param(searchsubs => $webvar{searchsubs}) if $webvar{searchsubs};
[51]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
[24]1193 my @userlist;
[52]1194 $sql = "SELECT u.user_id, u.username, u.firstname || ' ' || u.lastname AS fname, u.type, g.group_name, u.status ".
[24]1195 "FROM users u ".
1196 "INNER JOIN groups g ON u.group_id=g.group_id ".
[52]1197 "WHERE u.group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[51]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 ".
[52]1202 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
[51]1203
1204# " GROUP BY g.group_id, g.group_name, g2.group_name ".
1205# ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
1206
[52]1207 $sth = $dbh->prepare($sql);
1208 $sth->execute;
[24]1209
1210 my $rownum = 0;
1211 while (my @data = $sth->fetchrow_array) {
[41]1212 no warnings "uninitialized"; # Just In Case something stupid happens and a user gets no first or last name
[24]1213 my %row;
1214 $row{userid} = $data[0];
1215 $row{username} = $data[1];
[51]1216 $row{userfull} = $data[2];
1217 $row{usertype} = ($data[3] eq 'S' ? 'superuser' : "user");
1218 $row{usergroup} = $data[4];
1219 $row{active} = $data[5];
[24]1220 $row{bg} = ($rownum++)%2;
1221 $row{sid} = $sid;
1222 push @userlist, \%row;
1223 }
1224 $page->param(usertable => \@userlist);
[18]1225}
[43]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.