source: trunk/dns.cgi@ 23

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

/trunk

checkpoint

  • basic group management functional
  • add readable failure messages on domain/group delete
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 21.9 KB
Line 
1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-10-20 18:47:36 +0000 (Tue, 20 Oct 2009) $
6# SVN revision $Rev: 23 $
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});
402push @debugbits, groupName($dbh, $webvar{id});
403 if ($code ne 'OK') {
404# need to find failure mode
405 $page->param(del_failed => 1);
406 $page->param(errmsg => $msg);
407 $page->param(curpage => $webvar{page});
408 listgroups();
409 } else {
410 # success. go back to the domain list, do not pass "GO"
411 changepage(page => "grpman");
412 }
413 } else {
414 # cancelled. whee!
415 changepage(page => "grpman");
416 }
417 $page->param(delgroupname => groupName($dbh, $webvar{id}));
418}
419
420
421# start output here so we can redirect pages.
422print "Content-type: text/html\n\n", $header->output;
423
424foreach (@debugbits) { print; }
425
426##common bits
427if ($webvar{page} ne 'login') {
428 $page->param(group => $curgroup);
429 $page->param(groupname => groupName($dbh,$curgroup));
430
431 # stuff for menu group change. nb: this is icky.
432 fill_grouplist("grouplist");
433 $page->param(whereami => $ENV{REQUEST_URI});
434}
435
436# spit it out
437print $page->output;
438
439print "<div id=debug>webvar keys: <pre>\n";
440foreach my $key (keys %webvar) {
441 print "key: $key\tval: $webvar{$key}\n";
442}
443print "</pre>\nsession:\n<pre>\n";
444my $sesdata = $session->dataref();
445foreach my $key (keys %$sesdata) {
446 print "key: $key\tval: ".$sesdata->{$key}."\n";
447}
448print "</pre>\nENV:\n<pre>\n";
449foreach my $key (keys %ENV) {
450 print "key: $key\tval: $ENV{$key}\n";
451}
452print "</pre></div>\n";
453
454print $footer->output;
455
456# as per the docs, Just In Case
457$session->flush();
458
459exit 0;
460
461
462sub changepage {
463 my %params = @_; # think this works the way I want...
464
465 # handle user check
466 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
467 foreach (keys %params) {
468 $newurl .= "&$_=$params{$_}";
469 }
470
471 print "Status: 302\nLocation: $newurl\n\n";
472 exit;
473} # end changepage
474
475
476sub fillsoa {
477 my $def = shift;
478 my $id = shift;
479 my $domname;
480
481 if ($webvar{domain} == 0) {
482 $domname = "DOMAIN";
483 } else {
484 my $sth = $dbh->prepare("SELECT domain FROM domains WHERE domain_id=?");
485 $sth->execute($webvar{domain});
486 ($domname) = $sth->fetchrow_array();
487 }
488
489 $page->param(domain => $domname);
490 $page->param(defrec => !$webvar{domain});
491 $page->param(group => $DNSDB::group);
492
493# defaults
494 $page->param(defcontact => $DNSDB::def{contact});
495 $page->param(defns => $DNSDB::def{prins});
496 $page->param(defsoattl => $DNSDB::def{soattl});
497 $page->param(defrefresh => $DNSDB::def{refresh});
498 $page->param(defretry => $DNSDB::def{retry});
499 $page->param(defexpire => $DNSDB::def{expire});
500 $page->param(defminttl => $DNSDB::def{minttl});
501
502 # there are probably better ways to do this. TMTOWTDI.
503 my %soa = getSOA($dbh,$def,$id);
504
505 $page->param(domainid => $webvar{domain});
506 $page->param(recid => $soa{recid});
507 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
508 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
509 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
510 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
511 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
512 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
513 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
514}
515
516sub showdomain {
517 my $def = shift;
518 my $id = shift;
519
520 # get the SOA first
521 my %soa = getSOA($dbh,$def,$id);
522
523 $page->param(recid => $soa{recid});
524 $page->param(contact => $soa{contact});
525 $page->param(prins => $soa{prins});
526 $page->param(refresh => $soa{refresh});
527 $page->param(retry => $soa{retry});
528 $page->param(expire => $soa{expire});
529 $page->param(minttl => $soa{minttl});
530 $page->param(ttl => $soa{ttl});
531
532# my @foo2 = getDomRecs($dbh,'def',1);
533 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
534
535 my $row = 0;
536 foreach my $rec (@$foo2) {
537 $rec->{type} = $typemap{$rec->{type}};
538 $rec->{row} = $row % 2;
539 $rec->{defrec} = $webvar{defrec};
540 $rec->{sid} = $webvar{sid};
541 $rec->{id} = $id;
542 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
543 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
544 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
545 $row++;
546 }
547 $page->param(reclist => $foo2);
548}
549
550
551# fill in record type list on add/update/edit record template
552sub fill_rectypes {
553 my $type = shift || $reverse_typemap{A};
554
555 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
556 $sth->execute;
557 my @typelist;
558 while (my ($rval,$rname) = $sth->fetchrow_array()) {
559 my %row = ( recval => $rval, recname => $rname );
560 $row{tselect} = 1 if $rval == $type;
561 push @typelist, \%row;
562 }
563 $page->param(typelist => \@typelist);
564}
565
566sub fill_recdata {
567 fill_rectypes($webvar{type});
568
569 $page->param(name => $webvar{name});
570 $page->param(address => $webvar{address});
571 $page->param(distance => $webvar{distance})
572 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
573 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
574 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
575 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
576}
577
578sub fill_fpnla {
579 my $count = shift;
580##fixme
581 if ($offset eq 'all') {
582 push @debugbits, "foo! wanna see'em all\n";
583 } else {
584 # all these bits only have sensible behaviour if offset is numeric. err, probably.
585 if ($count > $perpage) {
586 # if there are more results than the default, always show the "all" link
587 $page->param(navall => 1);
588
589 if ($offset > 0) {
590 $page->param(navfirst => 1);
591 $page->param(navprev => 1);
592 $page->param(prevoffs => $offset-1);
593 }
594
595 # show "next" and "last" links if we're not on the last page of results
596 if ( (($offset+1) * $perpage - $count) < 0 ) {
597 $page->param(navnext => 1);
598 $page->param(nextoffs => $offset+1);
599 $page->param(navlast => 1);
600 $page->param(lastoffs => int (($count-1)/$perpage));
601 }
602 }
603 }
604} # end fill_fpnla()
605
606
607sub fill_pgcount {
608 my $pgcount = shift;
609 my $pgtype = shift;
610 my $parent = shift;
611
612 $page->param(ntot => $pgcount);
613 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
614 $page->param(npglast => ($offset eq 'all' ? $pgcount :
615 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
616 ));
617 $page->param(pgtype => $pgtype);
618 $page->param(parent => $parent);
619} # end fill_pgcount()
620
621
622sub listdomains {
623 my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
624 $sth->execute($curgroup);
625 my ($count) = $sth->fetchrow_array;
626
627# fill page count and first-previous-next-last-all bits
628##fixme - hardcoded group bit
629 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
630 fill_fpnla($count);
631
632##fixme - group
633 $page->param(group => $curgroup);
634 my @domlist;
635 $sth = $dbh->prepare("SELECT domain_id,domain,status,groups.group_name FROM domains".
636 " INNER JOIN groups ON domains.group_id=groups.group_id".
637 " WHERE domains.group_id=?".
638 " ORDER BY domain".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
639 $sth->execute($curgroup);
640 my $rownum = 0;
641 while (my @data = $sth->fetchrow_array) {
642 my %row;
643 $row{domainid} = $data[0];
644 $row{domain} = $data[1];
645 $row{status} = ($data[2] ? 'Active' : 'Inactive');
646 $row{group} = $data[3];
647 $row{bg} = ($rownum++)%2;
648# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
649 $row{mkactive} = !$data[2];
650 $row{sid} = $sid;
651 $row{offset} = $offset;
652##fixme: need to clean up status indicator/usage/inversion
653 push @domlist, \%row;
654 }
655 $page->param(domtable => \@domlist);
656} # end listdomains()
657
658
659sub listgroups {
660 my $sth = $dbh->prepare("select count(*) from groups");
661 $sth->execute;
662 my ($count) = ($sth->fetchrow_array);
663
664# fill page count and first-previous-next-last-all bits
665##fixme - hardcoded group bit
666 fill_pgcount($count,"groups",'');
667 fill_fpnla($count);
668
669 my @grouplist;
670 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
671 "count(distinct(u.email)), count(distinct(d.domain)) ".
672 "FROM groups g ".
673 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
674 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
675 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
676 "GROUP BY g.group_id, g.group_name, g2.group_name ".
677 "ORDER BY g.group_id".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
678 $sth->execute;
679
680 my $rownum = 0;
681 while (my @data = $sth->fetchrow_array) {
682 my %row;
683 $row{groupid} = $data[0];
684 $row{groupname} = $data[1];
685 $row{pgroup} = $data[2];
686 $row{nusers} = $data[3];
687 $row{ndomains} = $data[4];
688 $row{bg} = ($rownum++)%2;
689 $row{sid} = $sid;
690 push @grouplist, \%row;
691 }
692 $page->param(grouptable => \@grouplist);
693} # end listgroups()
694
695
696sub fill_grouplist {
697 my $template_var = shift;
698 my $cur = shift || $curgroup;
699 # weesa gonna discard parent_group_id for now
700 my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ORDER BY group_id");
701 $sth->execute;
702 my @grouplist;
703 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
704 my %row;
705 $row{groupname} = $groupname;
706 $row{groupval} = $groupid;
707##fixme: need magic
708# $row{defgroup} = '';
709 $row{groupactive} = 1 if $groupid == $cur;
710 push @grouplist, \%row;
711 }
712
713 $page->param("$template_var" => \@grouplist);
714
715}
Note: See TracBrowser for help on using the repository browser.