source: trunk/dns.cgi@ 53

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

/trunk

checkpoint
Starts with, search subgroups, and filter all maintain state properly

across requests on a single page; requests back and forth between domain,
user, and group listings sometimes reset as desired but sometimes maintain
state - this leads to confusing results sometimes.

Starts with, search subgroups, and filter all correctly influence both the

"n - m of o" and data lines in domain, user, and group listings

Tweaks to templates to correctly pass "empty" states for starts with, search

subgroups, and filter

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