source: trunk/dns.cgi@ 22

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

/trunk

checkpoint - basic group management should be complete

  • fiddled HTML so that group-delete and domain-delete errors don't sit on top of the menu column
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 21.5 KB
Line 
1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-10-15 21:50:22 +0000 (Thu, 15 Oct 2009) $
6# SVN revision $Rev: 22 $
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 DBI;
19
20use lib '.';
21# custom modules
22use DNSDB qw(:ALL);
23
24my @debugbits; # temp, to be spit out near the end of processing
25
26# Let's do these templates right...
27my $templatedir = "templates";
28my $sessiondir = "session";
29
30# Set up the CGI object...
31my $q = new CGI::Simple;
32# ... and get query-string params as well as POST params if necessary
33$q->parse_query_string;
34
35# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
36my %webvar = $q->Vars;
37
38# persistent stuff needed on most/all pages
39my $sid = ($webvar{sid} ? $webvar{sid} : undef);
40my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir});
41#$sid = $session->id() if !$sid;
42if (!$sid) {
43 # init stuff. can probably axe this down to just above if'n'when user manipulation happens
44 $sid = $session->id();
45# need to know the "upper" group the user can deal with; may as well
46# stick this in the session rather than calling out to the DB every time.
47 $session->param('logingroup',1);
48 $session->param('curgroup',1); # yes, we *do* need to track this too. er, probably.
49}
50
51my $logingroup = ($session->param('logingroup') ? $session->param('logingroup') : 1);
52my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $logingroup);
53my $group = ($webvar{group} ? $webvar{group} : 1);
54
55# handle login redirect
56if ($webvar{action} && $webvar{action} eq 'login') {
57 ##fixme: need to actually do a user/pass check
58 changepage(page => "domlist");
59}
60
61if ($webvar{action} && $webvar{action} eq 'chgroup') {
62 # fiddle session-stored group data
63 # magic incantation to... uhhh...
64 $session->param('curgroup', $webvar{group});
65 $curgroup = ($webvar{group} ? $webvar{group} : $session->param('curgroup'));
66}
67
68my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
69my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
70
71# default
72#my $perpage = 15;
73my $perpage = 3;
74my $offset = ($webvar{offset} ? $webvar{offset} : 0);
75
76# NB: these must match the field name and SQL ascend/descend syntax respectively
77my $sortfield = "domains";
78my $sortorder = "asc";
79
80my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret");
81#my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
82# { AutoCommit => 0 }) or die $DBI::errstr;
83
84##fixme. PLEASE! <G>
85print $msg if !$dbh;
86
87# fiddle hardcoded "defaults" as per system/user (?) prefs
88initGlobals($dbh);
89
90## Default page is a login page
91#my $page; # to be initialized as an HTML::Template entity sooner or later
92
93
94
95# decide which page to spit out...
96$webvar{page} = 'login' if !$webvar{page};
97#if (!$webvar{page}) {
98# $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
99#} else {
100#}
101
102my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
103
104$page->param(sid => $sid);
105
106if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
107
108# hmm. seeing problems in some possibly-not-so-corner cases.
109# this currently only handles "domain on", "domain off"
110 if (defined($webvar{action})) {
111 domStatus($dbh,$webvar{id},$webvar{action});
112 }
113
114 $page->param(curpage => $webvar{page});
115
116 listdomains();
117
118} elsif ($webvar{page} eq 'reclist') {
119
120 # Handle record list for both default records (per-group) and live domain records
121
122 $page->param(defrec => $webvar{defrec});
123 $page->param(id => $webvar{id});
124 $page->param(curpage => $webvar{page});
125
126# select count(*) from (default_)?records where (group|domain)_id=?
127 my $sth = $dbh->prepare("SELECT count(*) FROM ".
128 ($webvar{defrec} eq 'y' ? 'default_' : '')."records ".
129 "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ".
130 "AND NOT type=$reverse_typemap{SOA}");
131 $sth->execute($webvar{id});
132 my ($count) = ($sth->fetchrow_array);
133
134# fill the page-count and first-previous-next-last-all details
135 fill_pgcount($count,"records",domainName($dbh,$webvar{id}));
136 fill_fpnla($count); # should put some params on this sub...
137
138 $page->param(defrec => $webvar{defrec});
139 if ($webvar{defrec} eq 'y') {
140##fixme: hardcoded group
141 showdomain('y',$curgroup);
142 } else {
143 showdomain('n',$webvar{id});
144 }
145
146} elsif ($webvar{page} eq 'newdomain') {
147
148
149} elsif ($webvar{page} eq 'deldom') {
150
151 $page->param(id => $webvar{id});
152 # first pass = confirm y/n (sorta)
153 if (!defined($webvar{del})) {
154 $page->param(del_getconf => 1);
155 $page->param(domain => domainName($dbh,$webvar{id}));
156# print some neato things?
157
158# } else {
159# #whether actually deleting or cancelling we redirect to the domain list, default format
160
161 } elsif ($webvar{del} eq 'ok') {
162 my ($code,$msg) = delDomain($dbh, $webvar{id});
163 if ($code ne 'OK') {
164# need to find failure mode
165 $page->param(del_failed => 1);
166 $page->param(errmsg => $msg);
167 listdomains($curgroup);
168 } else {
169 # success. go back to the domain list, do not pass "GO"
170 changepage(page => "domlist");
171 }
172 } else {
173 # cancelled. whee!
174 changepage(page => "domlist");
175 }
176
177} elsif ($webvar{page} eq 'record') {
178
179 if ($webvar{recact} eq 'new') {
180
181 $page->param(todo => "Add record to");
182 $page->param(recact => "add");
183 fill_rectypes();
184
185 } elsif ($webvar{recact} eq 'add') {
186
187 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
188 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
189 push @recargs, $webvar{distance};
190 if ($webvar{type} == $reverse_typemap{SRV}) {
191 push @recargs, $webvar{weight};
192 push @recargs, $webvar{port};
193 }
194 }
195 my ($code,$msg) = addRec(@recargs);
196
197 if ($code eq 'OK') {
198 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
199 } else {
200 $page->param(failed => 1);
201 $page->param(errmsg => $msg);
202 fill_recdata(); # populate the form... er, mostly.
203 }
204
205 } elsif ($webvar{recact} eq 'edit') {
206
207 $page->param(todo => "Update record");
208 $page->param(recact => "update");
209 $page->param(parentid => $webvar{parentid});
210 $page->param(id => $webvar{id});
211 $page->param(defrec => $webvar{defrec});
212 my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ".
213 ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?");
214 $sth->execute($webvar{id});
215 my ($host,$type,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array;
216 $page->param(name => $host);
217 $page->param(address => $val);
218 $page->param(distance => $distance);
219 $page->param(weight => $weight);
220 $page->param(port => $port);
221 $page->param(ttl => $ttl);
222 fill_rectypes($type);
223
224 } elsif ($webvar{recact} eq 'update') {
225
226 my ($code,$msg) = updateRec($dbh,$webvar{defrec},$webvar{id},
227 $webvar{name},$webvar{type},$webvar{address},$webvar{ttl},
228 $webvar{distance},$webvar{weight},$webvar{port});
229
230 if ($code eq 'OK') {
231 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
232 } else {
233 $page->param(failed => 1);
234 $page->param(errmsg => $msg);
235 $page->param(wastrying => "updating");
236 $page->param(todo => "Update record");
237 $page->param(recact => "update");
238 $page->param(parentid => $webvar{parentid});
239 $page->param(defrec => $webvar{defrec});
240 $page->param(id => $webvar{id});
241 fill_recdata();
242 }
243 }
244
245 if ($webvar{defrec} eq 'y') {
246 $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
247 } else {
248 $page->param(dohere => domainName($dbh,$webvar{parentid}));
249 }
250
251} elsif ($webvar{page} eq 'newrec') {
252 push @debugbits, "whee!\n";
253
254 # populate most fields as needed. (eg, type list.)
255 stdrecs();
256
257} elsif ($webvar{page} eq 'addrec') {
258
259 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
260 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
261 push @recargs, $webvar{distance};
262 if ($webvar{type} == $reverse_typemap{SRV}) {
263 push @recargs, $webvar{weight};
264 push @recargs, $webvar{port};
265 }
266 }
267# wtf?
268# push @recargs,
269 my ($code,$msg) = addRec(@recargs);
270
271 if ($code eq 'OK') {
272 showdomain($webvar{defrec},$webvar{parentid});
273# NB: should **really** redirect here, in case of reload. >_< eyowch.
274 } else {
275 $page->param(add_failed => 1);
276 $page->param(errmsg => $msg);
277 stdrecs($webvar{type}); # populate the form... er, mostly.
278 $page->param(name => $webvar{name});
279 $page->param(address => $webvar{address});
280 $page->param(distance => $webvar{distance})
281 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
282 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
283 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
284 }
285
286 $page->param(defrec => $webvar{defrec});
287
288} elsif ($webvar{page} eq 'conf_del') {
289
290 $page->param(id => $webvar{id});
291 $page->param(defrec => $webvar{defrec});
292
293 my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
294
295} elsif ($webvar{page} eq 'delrec') {
296
297 $page->param(id => $webvar{id});
298 $page->param(defrec => $webvar{defrec});
299 # first pass = confirm y/n (sorta)
300 if (!defined($webvar{del})) {
301 $page->param(del_getconf => 1);
302 my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
303 $page->param(host => $rec{host});
304 $page->param(ftype => $typemap{$rec{type}});
305 $page->param(recval => $rec{val});
306 } else {
307 my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
308 if ($code ne 'OK') {
309## need to find failure mode
310 $page->param(del_failed => 1);
311 $page->param(errmsg => $msg);
312 }
313##fixme: group/parent instead of hardcoded 1
314 showdomain('y',1);
315 }
316
317} elsif ($webvar{page} eq 'editsoa') {
318
319 fillsoa($webvar{defrec},$webvar{recid});
320
321} elsif ($webvar{page} eq 'updatesoa') {
322 print "ooooo!\n";
323
324 my $sth;
325 my $sql = '';
326 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
327 # plus a bit of magic to update the appropriate table
328 $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
329 " set host='$webvar{prins}:$webvar{contact}',".
330 " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
331 " ttl=$webvar{ttl} where record_id=$webvar{recid}";
332 $sth = $dbh->prepare($sql);
333 $sth->execute;
334
335 if ($sth->err) {
336 $page->param(update_failed => 1);
337 $page->param(msg => $DBI::errstr);
338 fillsoa($webvar{defrec},1);
339 } else {
340 $page->param(update_failed => 0);
341##fixme! need to set group ID properly here
342 showdomain('y',1);
343 }
344
345} elsif ($webvar{page} eq 'adddomain') {
346 # Need some magic here.
347
348##fixme: Group should be variable
349 my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
350
351# hokay, a bit of magic to decide which page we hit.
352 if ($code eq 'OK') {
353# redirect to dns.cgi?etc&page=reclist
354 changepage(page => "reclist", id => $msg);
355 $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
356 showdomain(0,$msg);
357##work
358 } else {
359# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
360 $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
361 $page->param(add_failed => 1);
362 $page->param(domain => $webvar{domain});
363 $page->param(errmsg => $msg);
364 }
365
366} elsif ($webvar{page} eq 'grpman') {
367
368 listgroups();
369 $page->param(curpage => $webvar{page});
370
371} elsif ($webvar{page} eq 'newgrp') {
372
373 # do.. uhh.. stuff.. if we have no webvar{action}
374 if ($webvar{action} && $webvar{action} eq 'add') {
375 # not gonna provide the 4th param: template-or-clone flag, just yet
376 my ($code,$msg) = addGroup($dbh, $webvar{newgroup}, $webvar{pargroup});
377 changepage(page => "grpman") if $code eq 'OK';
378 $page->param(add_failed => 1);
379 $page->param(errmsg => $msg);
380 $page->param(newgroup => $webvar{newgroup});
381 fill_grouplist('pargroup',$webvar{pargroup});
382 } else {
383# $page->param
384 fill_grouplist('pargroup',$curgroup);
385
386 }
387
388} elsif ($webvar{page} eq 'delgrp') {
389
390 $page->param(id => $webvar{id});
391 # first pass = confirm y/n (sorta)
392 if (!defined($webvar{del})) {
393 $page->param(del_getconf => 1);
394 $page->param(groupname => groupName($dbh,$webvar{id}));
395# print some neato things?
396
397# } else {
398# #whether actually deleting or cancelling we redirect to the group list, default format
399
400 } elsif ($webvar{del} eq 'ok') {
401 my ($code,$msg) = delGroup($dbh, $webvar{id});
402 if ($code ne 'OK') {
403# need to find failure mode
404 $page->param(del_failed => 1);
405 $page->param(errmsg => $msg);
406 $page->param(curpage => $webvar{page});
407 listgroups();
408 } else {
409 # success. go back to the domain list, do not pass "GO"
410 changepage(page => "grpman");
411 }
412 } else {
413 # cancelled. whee!
414 changepage(page => "grpman");
415 }
416
417}
418
419
420# start output here so we can redirect pages.
421print "Content-type: text/html\n\n", $header->output;
422
423foreach (@debugbits) { print; }
424
425##common bits
426if ($webvar{page} ne 'login') {
427 $page->param(group => $curgroup);
428 $page->param(groupname => groupName($dbh,$curgroup));
429
430 # stuff for menu group change. nb: this is icky.
431 fill_grouplist("grouplist");
432 $page->param(whereami => $ENV{REQUEST_URI});
433}
434
435# spit it out
436print $page->output;
437
438print "<div id=debug>webvar keys: <pre>\n";
439foreach my $key (keys %webvar) {
440 print "key: $key\tval: $webvar{$key}\n";
441}
442print "</pre>\nsession:\n<pre>\n";
443my $sesdata = $session->dataref();
444foreach my $key (keys %$sesdata) {
445 print "key: $key\tval: ".$sesdata->{$key}."\n";
446}
447print "</pre>\nENV:\n<pre>\n";
448foreach my $key (keys %ENV) {
449 print "key: $key\tval: $ENV{$key}\n";
450}
451print "</pre></div>\n";
452
453print $footer->output;
454
455# as per the docs, Just In Case
456$session->flush();
457
458exit 0;
459
460
461sub changepage {
462 my %params = @_; # think this works the way I want...
463
464 # handle user check
465 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
466 foreach (keys %params) {
467 $newurl .= "&$_=$params{$_}";
468 }
469
470 print "Status: 302\nLocation: $newurl\n\n";
471 exit;
472} # end changepage
473
474
475sub fillsoa {
476 my $def = shift;
477 my $id = shift;
478 my $domname;
479
480 if ($webvar{domain} == 0) {
481 $domname = "DOMAIN";
482 } else {
483 my $sth = $dbh->prepare("SELECT domain FROM domains WHERE domain_id=?");
484 $sth->execute($webvar{domain});
485 ($domname) = $sth->fetchrow_array();
486 }
487
488 $page->param(domain => $domname);
489 $page->param(defrec => !$webvar{domain});
490 $page->param(group => $DNSDB::group);
491
492# defaults
493 $page->param(defcontact => $DNSDB::def{contact});
494 $page->param(defns => $DNSDB::def{prins});
495 $page->param(defsoattl => $DNSDB::def{soattl});
496 $page->param(defrefresh => $DNSDB::def{refresh});
497 $page->param(defretry => $DNSDB::def{retry});
498 $page->param(defexpire => $DNSDB::def{expire});
499 $page->param(defminttl => $DNSDB::def{minttl});
500
501 # there are probably better ways to do this. TMTOWTDI.
502 my %soa = getSOA($dbh,$def,$id);
503
504 $page->param(domainid => $webvar{domain});
505 $page->param(recid => $soa{recid});
506 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
507 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
508 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
509 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
510 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
511 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
512 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
513}
514
515sub showdomain {
516 my $def = shift;
517 my $id = shift;
518
519 # get the SOA first
520 my %soa = getSOA($dbh,$def,$id);
521
522 $page->param(recid => $soa{recid});
523 $page->param(contact => $soa{contact});
524 $page->param(prins => $soa{prins});
525 $page->param(refresh => $soa{refresh});
526 $page->param(retry => $soa{retry});
527 $page->param(expire => $soa{expire});
528 $page->param(minttl => $soa{minttl});
529 $page->param(ttl => $soa{ttl});
530
531# my @foo2 = getDomRecs($dbh,'def',1);
532 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
533
534 my $row = 0;
535 foreach my $rec (@$foo2) {
536 $rec->{type} = $typemap{$rec->{type}};
537 $rec->{row} = $row % 2;
538 $rec->{defrec} = $webvar{defrec};
539 $rec->{sid} = $webvar{sid};
540 $rec->{id} = $id;
541 $row++;
542 }
543 $page->param(reclist => $foo2);
544}
545
546# fill in record type list on add/update/edit record template
547sub fill_rectypes {
548 my $type = shift || $reverse_typemap{A};
549
550 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
551 $sth->execute;
552 my @typelist;
553 while (my ($rval,$rname) = $sth->fetchrow_array()) {
554 my %row = ( recval => $rval, recname => $rname );
555 $row{tselect} = 1 if $rval == $type;
556 push @typelist, \%row;
557 }
558 $page->param(typelist => \@typelist);
559}
560
561sub fill_recdata {
562 fill_rectypes($webvar{type});
563
564 $page->param(name => $webvar{name});
565 $page->param(address => $webvar{address});
566 $page->param(distance => $webvar{distance})
567 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
568 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
569 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
570 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
571}
572
573sub fill_fpnla {
574 my $count = shift;
575##fixme
576 if ($offset eq 'all') {
577 push @debugbits, "foo! wanna see'em all\n";
578 } else {
579 # all these bits only have sensible behaviour if offset is numeric. err, probably.
580 if ($count > $perpage) {
581 # if there are more results than the default, always show the "all" link
582 $page->param(navall => 1);
583
584 if ($offset > 0) {
585 $page->param(navfirst => 1);
586 $page->param(navprev => 1);
587 $page->param(prevoffs => $offset-1);
588 }
589
590 # show "next" and "last" links if we're not on the last page of results
591 if ( (($offset+1) * $perpage - $count) < 0 ) {
592 $page->param(navnext => 1);
593 $page->param(nextoffs => $offset+1);
594 $page->param(navlast => 1);
595 $page->param(lastoffs => int (($count-1)/$perpage));
596 }
597 }
598 }
599} # end fill_fpnla()
600
601
602sub fill_pgcount {
603 my $pgcount = shift;
604 my $pgtype = shift;
605 my $parent = shift;
606
607 $page->param(ntot => $pgcount);
608 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
609 $page->param(npglast => ($offset eq 'all' ? $pgcount :
610 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
611 ));
612 $page->param(pgtype => $pgtype);
613 $page->param(parent => $parent);
614} # end fill_pgcount()
615
616
617sub listdomains {
618 my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
619 $sth->execute($curgroup);
620 my ($count) = $sth->fetchrow_array;
621
622# fill page count and first-previous-next-last-all bits
623##fixme - hardcoded group bit
624 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
625 fill_fpnla($count);
626
627##fixme - group
628 $page->param(group => $curgroup);
629 my @domlist;
630 $sth = $dbh->prepare("SELECT domain_id,domain,status,groups.group_name FROM domains".
631 " INNER JOIN groups ON domains.group_id=groups.group_id".
632 " WHERE domains.group_id=?".
633 " ORDER BY domain".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
634 $sth->execute($curgroup);
635 my $rownum = 0;
636 while (my @data = $sth->fetchrow_array) {
637 my %row;
638 $row{domainid} = $data[0];
639 $row{domain} = $data[1];
640 $row{status} = ($data[2] ? 'Active' : 'Inactive');
641 $row{group} = $data[3];
642 $row{bg} = ($rownum++)%2;
643# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
644 $row{mkactive} = !$data[2];
645 $row{sid} = $sid;
646 $row{offset} = $offset;
647##fixme: need to clean up status indicator/usage/inversion
648 push @domlist, \%row;
649 }
650 $page->param(domtable => \@domlist);
651} # end listdomains()
652
653
654sub listgroups {
655 my $sth = $dbh->prepare("select count(*) from groups");
656 $sth->execute;
657 my ($count) = ($sth->fetchrow_array);
658
659# fill page count and first-previous-next-last-all bits
660##fixme - hardcoded group bit
661 fill_pgcount($count,"groups",'');
662 fill_fpnla($count);
663
664 my @grouplist;
665 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
666 "count(distinct(u.email)), count(distinct(d.domain)) ".
667 "FROM groups g ".
668 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
669 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
670 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
671 "GROUP BY g.group_id, g.group_name, g2.group_name ".
672 "ORDER BY g.group_id".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
673 $sth->execute;
674
675 my $rownum = 0;
676 while (my @data = $sth->fetchrow_array) {
677 my %row;
678 $row{groupid} = $data[0];
679 $row{groupname} = $data[1];
680 $row{pgroup} = $data[2];
681 $row{nusers} = $data[3];
682 $row{ndomains} = $data[4];
683 $row{bg} = ($rownum++)%2;
684 $row{sid} = $sid;
685 push @grouplist, \%row;
686 }
687 $page->param(grouptable => \@grouplist);
688} # end listgroups()
689
690
691sub fill_grouplist {
692 my $template_var = shift;
693 my $cur = shift || $curgroup;
694 # weesa gonna discard parent_group_id for now
695 my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ORDER BY group_id");
696 $sth->execute;
697 my @grouplist;
698 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
699 my %row;
700 $row{groupname} = $groupname;
701 $row{groupval} = $groupid;
702##fixme: need magic
703# $row{defgroup} = '';
704 $row{groupactive} = 1 if $groupid == $cur;
705 push @grouplist, \%row;
706 }
707
708 $page->param("$template_var" => \@grouplist);
709
710}
Note: See TracBrowser for help on using the repository browser.