source: trunk/dns.cgi@ 51

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

/dev

checkpoint
Moved sort order/sort-by tracking on domain, user, and group lists into session
Fixed user activation/deactivation and display
Copy-pasted bits for letter-search and filtering from domains to user and group lists

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 39.1 KB
RevLine 
[2]1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-12-17 20:47:50 +0000 (Thu, 17 Dec 2009) $
6# SVN revision $Rev: 51 $
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;
[41]728# and any bits from the "starts with" letter block - (ab)using this widget
[42]729# to fill the bulk of the URI so various templates don't grow to insanity
[41]730 $tmp_ruri =~ s/\&amp;startwith=[a-z09-]+//g;
[38]731# $page->param(whereami => $ENV{REQUEST_URI});
732 $page->param(whereami => $tmp_ruri);
[17]733}
[13]734
[24]735foreach (@debugbits) { print; }
736
[2]737# spit it out
738print $page->output;
739
[38]740if ($debugenv) {
741 print "<div id=\"debug\">webvar keys: <pre>\n";
742 foreach my $key (keys %webvar) {
743 print "key: $key\tval: $webvar{$key}\n";
744 }
745 print "</pre>\nsession:\n<pre>\n";
746 my $sesdata = $session->dataref();
747 foreach my $key (keys %$sesdata) {
748 print "key: $key\tval: ".$sesdata->{$key}."\n";
749 }
750 print "</pre>\nENV:\n<pre>\n";
751 foreach my $key (keys %ENV) {
752 print "key: $key\tval: $ENV{$key}\n";
753 }
754 print "</pre></div>\n";
[2]755}
756
757print $footer->output;
758
[18]759# as per the docs, Just In Case
760$session->flush();
[2]761
762exit 0;
763
764
[24]765sub fill_grptree {
766 my $root = shift;
767 my $cur = shift;
768
769 my @childlist;
770
771 my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
772 getChildren($dbh,$root,\@childlist,'immediate');
773 return if $#childlist == -1;
774 my @grouplist;
775 foreach (@childlist) {
776 my %row;
777 $row{grpname} = groupName($dbh,$_);
778 $row{grpname} = "<b>$row{grpname}</b>" if $_ == $cur;
779 $row{subs} = fill_grptree($_,$cur);
780 push @grouplist, \%row;
781 }
782 $grptree->param(treelvl => \@grouplist);
783 return $grptree->output;
784}
785
786
[11]787sub changepage {
788 my %params = @_; # think this works the way I want...
789
790 # handle user check
791 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
792 foreach (keys %params) {
793 $newurl .= "&$_=$params{$_}";
794 }
795
[30]796 # Just In Case
797 $session->flush();
798
[11]799 print "Status: 302\nLocation: $newurl\n\n";
800 exit;
801} # end changepage
802
803
[2]804sub fillsoa {
805 my $def = shift;
806 my $id = shift;
[39]807 my $domname = ($def eq 'y' ? '' : "DOMAIN");
[2]808
[39]809 $page->param(defrec => $def);
[2]810
[39]811# i had a good reason to do this when I wrote it...
812# $page->param(domain => $domname);
813# $page->param(group => $DNSDB::group);
814 $page->param(isgrp => 1) if $def eq 'y';
815 $page->param(parent => ($def eq 'y' ? groupName($dbh, $DNSDB::group) : domainName($dbh, $id)) );
[2]816
817# defaults
[17]818 $page->param(defcontact => $DNSDB::def{contact});
819 $page->param(defns => $DNSDB::def{prins});
820 $page->param(defsoattl => $DNSDB::def{soattl});
821 $page->param(defrefresh => $DNSDB::def{refresh});
822 $page->param(defretry => $DNSDB::def{retry});
823 $page->param(defexpire => $DNSDB::def{expire});
824 $page->param(defminttl => $DNSDB::def{minttl});
[2]825
826 # there are probably better ways to do this. TMTOWTDI.
827 my %soa = getSOA($dbh,$def,$id);
828
[39]829 $page->param(id => $id);
[2]830 $page->param(recid => $soa{recid});
831 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
832 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
833 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
834 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
835 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
836 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
837 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
838}
839
840sub showdomain {
841 my $def = shift;
842 my $id = shift;
843
844 # get the SOA first
845 my %soa = getSOA($dbh,$def,$id);
846
847 $page->param(recid => $soa{recid});
848 $page->param(contact => $soa{contact});
849 $page->param(prins => $soa{prins});
850 $page->param(refresh => $soa{refresh});
851 $page->param(retry => $soa{retry});
852 $page->param(expire => $soa{expire});
853 $page->param(minttl => $soa{minttl});
854 $page->param(ttl => $soa{ttl});
855
856# my @foo2 = getDomRecs($dbh,'def',1);
[4]857 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
[2]858
859 my $row = 0;
860 foreach my $rec (@$foo2) {
861 $rec->{type} = $typemap{$rec->{type}};
862 $rec->{row} = $row % 2;
863 $rec->{defrec} = $webvar{defrec};
864 $rec->{sid} = $webvar{sid};
[13]865 $rec->{id} = $id;
[23]866 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
867 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
868 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
[2]869 $row++;
870 }
871 $page->param(reclist => $foo2);
872}
873
[23]874
[16]875# fill in record type list on add/update/edit record template
876sub fill_rectypes {
[13]877 my $type = shift || $reverse_typemap{A};
[31]878 my $soaflag = shift || 0;
[13]879
[17]880 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
[2]881 $sth->execute;
882 my @typelist;
883 while (my ($rval,$rname) = $sth->fetchrow_array()) {
884 my %row = ( recval => $rval, recname => $rname );
[13]885 $row{tselect} = 1 if $rval == $type;
[2]886 push @typelist, \%row;
887 }
[31]888 if ($soaflag) {
889 my %row = ( recval => $reverse_typemap{SOA}, recname => 'SOA' );
890 $row{tselect} = 1 if $reverse_typemap{SOA} == $type;
891 push @typelist, \%row;
892 }
[2]893 $page->param(typelist => \@typelist);
[31]894} # fill_rectypes
[16]895
896sub fill_recdata {
897 fill_rectypes($webvar{type});
898
899 $page->param(name => $webvar{name});
900 $page->param(address => $webvar{address});
901 $page->param(distance => $webvar{distance})
902 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
903 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
904 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
[2]905 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
906}
[7]907
[24]908
909sub fill_actypelist {
910 my @actypes;
911
912 my %row1 = (actypeval => 'u', actypename => 'user');
913 $row1{typesel} = 1 if $webvar{accttype} eq 'u';
914 push @actypes, \%row1;
915
916 my %row2 = (actypeval => 'S', actypename => 'superuser');
917 $row2{typesel} = 1 if $webvar{accttype} eq 'S';
918 push @actypes, \%row2;
919
920 $page->param(actypelist => \@actypes);
921}
922
923
[7]924sub fill_fpnla {
925 my $count = shift;
926##fixme
927 if ($offset eq 'all') {
[41]928# uhm....
[7]929 } else {
930 # all these bits only have sensible behaviour if offset is numeric. err, probably.
931 if ($count > $perpage) {
932 # if there are more results than the default, always show the "all" link
933 $page->param(navall => 1);
934
935 if ($offset > 0) {
936 $page->param(navfirst => 1);
937 $page->param(navprev => 1);
938 $page->param(prevoffs => $offset-1);
939 }
940
941 # show "next" and "last" links if we're not on the last page of results
942 if ( (($offset+1) * $perpage - $count) < 0 ) {
943 $page->param(navnext => 1);
944 $page->param(nextoffs => $offset+1);
945 $page->param(navlast => 1);
[8]946 $page->param(lastoffs => int (($count-1)/$perpage));
[7]947 }
948 }
949 }
[10]950} # end fill_fpnla()
951
952
[12]953sub fill_pgcount {
954 my $pgcount = shift;
955 my $pgtype = shift;
956 my $parent = shift;
957
958 $page->param(ntot => $pgcount);
959 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
960 $page->param(npglast => ($offset eq 'all' ? $pgcount :
961 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
962 ));
963 $page->param(pgtype => $pgtype);
964 $page->param(parent => $parent);
965} # end fill_pgcount()
966
967
[11]968sub listdomains {
[41]969
970##fixme: account for searches and starts-with filters
[17]971 my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
[20]972 $sth->execute($curgroup);
[17]973 my ($count) = $sth->fetchrow_array;
974
[12]975# fill page count and first-previous-next-last-all bits
976##fixme - hardcoded group bit
[20]977 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
[10]978 fill_fpnla($count);
979
[41]980# sort/order
[51]981 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
982 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[41]983
[51]984 $sortby = $session->param($webvar{page}.'sortby');
985 $sortorder = $session->param($webvar{page}.'order');
986
[44]987# set up the headers
988 my @cols = ('domain', 'status', 'group');
989 my %colheads = (domain => 'Domain', status => 'Status', group => 'Group');
990 fill_colheads(\@cols, \%colheads);
[41]991
[47]992# $page->param(sortorder => $sortorder);
[41]993 # hack! hack! pthbttt. have to rethink the status column storage,
994 # or inactive comes "before" active. *sigh*
995 $sortorder = ($sortorder eq 'ASC' ? 'DESC' : 'ASC') if $sortby eq 'status';
996
[51]997# waffle, waffle - keep state on these as well as sortby, sortorder?
[41]998 $page->param("start$webvar{startwith}" => 1) if $webvar{startwith} && $webvar{startwith} =~ /^[a-z]$/;
999
1000 $page->param(filter => $webvar{filter}) if $webvar{filter};
1001
1002##fixme
1003##fixme push the SQL and direct database fiddling off into a sub in DNSDB.pm
1004##fixme
1005
[20]1006 $page->param(group => $curgroup);
[10]1007 my @domlist;
[41]1008 my $sql = "SELECT domain_id,domain,status,groups.group_name AS group FROM domains".
[20]1009 " INNER JOIN groups ON domains.group_id=groups.group_id".
1010 " WHERE domains.group_id=?".
[41]1011##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
1012 (defined($webvar{startwith}) ? " AND domain ~* '^[$webvar{startwith}]'" : '').
1013 (defined($webvar{filter}) ? " AND domain ~* '$webvar{filter}'" : '').
1014 " ORDER BY ".($sortby eq 'group' ? 'groups.group_name' : $sortby).
1015 " $sortorder ".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
1016 $sth = $dbh->prepare($sql);
[20]1017 $sth->execute($curgroup);
[10]1018 my $rownum = 0;
1019 while (my @data = $sth->fetchrow_array) {
1020 my %row;
1021 $row{domainid} = $data[0];
1022 $row{domain} = $data[1];
1023 $row{status} = ($data[2] ? 'Active' : 'Inactive');
1024 $row{group} = $data[3];
1025 $row{bg} = ($rownum++)%2;
1026# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
1027 $row{mkactive} = !$data[2];
1028 $row{sid} = $sid;
1029 $row{offset} = $offset;
1030##fixme: need to clean up status indicator/usage/inversion
1031 push @domlist, \%row;
1032 }
1033 $page->param(domtable => \@domlist);
[11]1034} # end listdomains()
[18]1035
1036
[22]1037sub listgroups {
[26]1038 my @childgroups;
1039 getChildren($dbh, $logingroup, \@childgroups, 'all');
1040 my $childlist = join(',',@childgroups);
1041
1042 my $sql = "SELECT count(*) FROM groups WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")";
1043 my $sth = $dbh->prepare($sql);
1044
[22]1045 $sth->execute;
1046 my ($count) = ($sth->fetchrow_array);
1047# fill page count and first-previous-next-last-all bits
1048##fixme - hardcoded group bit
1049 fill_pgcount($count,"groups",'');
1050 fill_fpnla($count);
1051
[51]1052# $sortby = 'group';
[42]1053# sort/order
[51]1054 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1055 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[42]1056
[51]1057 $sortby = $session->param($webvar{page}.'sortby');
1058 $sortorder = $session->param($webvar{page}.'order');
1059
[44]1060# set up the headers
1061 my @cols = ('group','parent','nusers','ndomains');
1062 my %colnames = (group => 'Group', parent => 'Parent Group', nusers => 'Users', ndomains => 'Domains');
1063 fill_colheads(\@cols, \%colnames);
[42]1064
[51]1065# waffle, waffle - keep state on these as well as sortby, sortorder?
1066 $page->param("start$webvar{startwith}" => 1) if $webvar{startwith} && $webvar{startwith} =~ /^[a-z]$/;
1067
1068 $page->param(filter => $webvar{filter}) if $webvar{filter};
1069
1070# munge sortby for columns in database
1071 $sortby = 'g.group_name' if $sortby eq 'group';
1072 $sortby = 'g2.group_name' if $sortby eq 'parent';
1073
[22]1074 my @grouplist;
1075 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
[51]1076 "count(distinct(u.username)) AS nusers, count(distinct(d.domain)) AS ndomains ".
[22]1077 "FROM groups g ".
1078 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
1079 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
1080 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
[26]1081 "WHERE g.group_id IN ($logingroup".($childlist ? ",$childlist" : '').") ".
[51]1082##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
1083 (defined($webvar{startwith}) ? " AND g.group_name ~* '^[$webvar{startwith}]'" : '').
1084 (defined($webvar{filter}) ? " AND g.group_name ~* '$webvar{filter}'" : '').
1085 " GROUP BY g.group_id, g.group_name, g2.group_name ".
1086 " ORDER BY $sortby $sortorder ".
1087 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
[22]1088 $sth->execute;
1089
[51]1090push @debugbits, $sth->errstr;
[22]1091 my $rownum = 0;
1092 while (my @data = $sth->fetchrow_array) {
1093 my %row;
1094 $row{groupid} = $data[0];
1095 $row{groupname} = $data[1];
1096 $row{pgroup} = $data[2];
1097 $row{nusers} = $data[3];
1098 $row{ndomains} = $data[4];
1099 $row{bg} = ($rownum++)%2;
1100 $row{sid} = $sid;
1101 push @grouplist, \%row;
1102 }
1103 $page->param(grouptable => \@grouplist);
1104} # end listgroups()
1105
1106
[20]1107sub fill_grouplist {
[19]1108 my $template_var = shift;
1109 my $cur = shift || $curgroup;
[26]1110
1111 my @childgroups;
1112 getChildren($dbh, $logingroup, \@childgroups, 'all');
1113 my $childlist = join(',',@childgroups);
1114
[18]1115 # weesa gonna discard parent_group_id for now
[26]1116 my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ".
1117 "WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
1118 "ORDER BY group_id");
[18]1119 $sth->execute;
[20]1120 my @grouplist;
1121 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
[18]1122 my %row;
[20]1123 $row{groupname} = $groupname;
1124 $row{groupval} = $groupid;
[18]1125##fixme: need magic
[20]1126# $row{defgroup} = '';
1127 $row{groupactive} = 1 if $groupid == $cur;
1128 push @grouplist, \%row;
[18]1129 }
1130
[20]1131 $page->param("$template_var" => \@grouplist);
[18]1132
[24]1133} # end fill_grouplist()
1134
[26]1135
[24]1136sub list_users {
1137 my $sth = $dbh->prepare("select count(*) from users where group_id=?");
1138 $sth->execute($curgroup);
1139 my ($count) = ($sth->fetchrow_array);
1140
1141# fill page count and first-previous-next-last-all bits
1142##fixme - hardcoded group bit
1143 fill_pgcount($count,"users",'');
1144 fill_fpnla($count);
1145
[51]1146# $sortby = 'user';
[44]1147# sort/order
[51]1148 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1149 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[44]1150
[51]1151 $sortby = $session->param($webvar{page}.'sortby');
1152 $sortorder = $session->param($webvar{page}.'order');
1153
[44]1154# set up the headers
1155 my @cols = ('user','fname','type','group','status');
1156 my %colnames = (user => 'Username', fname => 'Full Name', type => 'Type', group => 'Group', status => 'Status');
1157 fill_colheads(\@cols, \%colnames);
1158
[51]1159# waffle, waffle - keep state on these as well as sortby, sortorder?
1160 $page->param("start$webvar{startwith}" => 1) if $webvar{startwith} && $webvar{startwith} =~ /^[a-z]$/;
1161
1162 $page->param(filter => $webvar{filter}) if $webvar{filter};
1163
1164# munge sortby for columns in database
1165 $sortby = 'u.username' if $sortby eq 'user';
1166 $sortby = 'u.type' if $sortby eq 'type';
1167 $sortby = 'g.group_name' if $sortby eq 'group';
1168 $sortby = 'u.status' if $sortby eq 'status';
1169
[24]1170 my @userlist;
[51]1171 $sth = $dbh->prepare("SELECT u.user_id, u.username, u.firstname || ' ' || u.lastname AS fname, u.type, g.group_name, u.status ".
[24]1172 "FROM users u ".
1173 "INNER JOIN groups g ON u.group_id=g.group_id ".
1174 "WHERE u.group_id=?".
[51]1175##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
1176 (defined($webvar{startwith}) ? " AND u.username ~* '^[$webvar{startwith}]'" : '').
1177 (defined($webvar{filter}) ? " AND u.username ~* '$webvar{filter}'" : '').
1178 " ORDER BY $sortby $sortorder ".
[24]1179 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
[51]1180
1181# " GROUP BY g.group_id, g.group_name, g2.group_name ".
1182# ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
1183
[24]1184 $sth->execute($curgroup);
1185
1186 my $rownum = 0;
1187 while (my @data = $sth->fetchrow_array) {
[41]1188 no warnings "uninitialized"; # Just In Case something stupid happens and a user gets no first or last name
[24]1189 my %row;
1190 $row{userid} = $data[0];
1191 $row{username} = $data[1];
[51]1192 $row{userfull} = $data[2];
1193 $row{usertype} = ($data[3] eq 'S' ? 'superuser' : "user");
1194 $row{usergroup} = $data[4];
1195 $row{active} = $data[5];
[24]1196 $row{bg} = ($rownum++)%2;
1197 $row{sid} = $sid;
1198 push @userlist, \%row;
1199 }
1200 $page->param(usertable => \@userlist);
[18]1201}
[43]1202
1203# Generate all of the glop necessary to add or not the appropriate marker/flag for
1204# the sort order and column in domain, user, group, and record lists
1205# Takes an array ref and hash ref
1206sub fill_colheads {
1207 my $cols = shift;
1208 my $colnames = shift;
1209
1210 my @headings;
1211
1212 foreach my $col (@$cols) {
1213 my %coldata;
1214 $coldata{firstcol} = 1 if $col eq $cols->[0];
1215 $coldata{sid} = $sid;
1216 $coldata{page} = $webvar{page};
1217 $coldata{offset} = $webvar{offset} if $webvar{offset};
1218 $coldata{sortby} = $col;
1219 $coldata{colname} = $colnames->{$col};
1220 if ($col eq $sortby) {
1221 $coldata{order} = ($sortorder eq 'ASC' ? 'DESC' : 'ASC');
1222 $coldata{sortorder} = $sortorder;
1223 } else {
1224 $coldata{order} = 'ASC';
1225 }
1226 push @headings, \%coldata;
1227 }
1228
1229 $page->param(colheads => \@headings);
1230
1231} # end gentableheadings
Note: See TracBrowser for help on using the repository browser.