source: trunk/dns.cgi@ 12

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

/trunk

Moved "Listing n of m <blahs> in <foo>" to its own template, and
shuffled code to match
Misc changes, checkpoint

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 14.7 KB
RevLine 
[2]1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-09-10 21:41:30 +0000 (Thu, 10 Sep 2009) $
6# SVN revision $Rev: 12 $
7# Last update by $Author: kdeugau $
8###
[3]9# Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx>
[2]10
11use strict;
12use warnings;
13
14use CGI::Carp qw (fatalsToBrowser);
15use CGI::Simple;
16use HTML::Template;
17use CGI::Session;
18use DBI;
19
20use lib '.';
21# custom modules
22use DNSDB qw(:ALL);
23
24# Let's do these templates right...
25my $templatedir = "templates";
26my $sessiondir = "session";
27
28# Set up the CGI object...
29my $q = new CGI::Simple;
30# ... and get query-string params as well as POST params if necessary
31$q->parse_query_string;
32
33# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
[7]34my %webvar = $q->Vars;
[2]35
36my $sid = ($webvar{sid} ? $webvar{sid} : undef);
37my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir});
38#$sid = $session->id() if !$sid;
39if (!$sid) {
40 # init stuff. can probably axe this down to just above if'n'when user manipulation happens
41 $sid = $session->id();
42# need to know the "upper" group the user can deal with; may as well
43# stick this in the session rather than calling out to the DB every time.
44 $session->param('logingroupid',1);
45 $session->param('workinggroupid',1); # yes, we *do* need to track this too. er, probably.
46}
47
48# handle login redirect
49if ($webvar{action} && $webvar{action} eq 'login') {
[11]50 ##fixme: need to actually do a user/pass check
51 changepage(page => "domlist");
[2]52}
53
54my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
55my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
56
57# default
[3]58#my $perpage = 15;
[7]59my $perpage = 4;
[2]60my $offset = ($webvar{offset} ? $webvar{offset} : 0);
61
62# NB: these must match the field name and SQL ascend/descend syntax respectively
63my $sortfield = "domains";
64my $sortorder = "asc";
65
66my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret");
67#my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
68# { AutoCommit => 0 }) or die $DBI::errstr;
69
70##fixme. PLEASE! <G>
71print $msg if !$dbh;
72
73# fiddle hardcoded "defaults" as per system/user (?) prefs
74initGlobals($dbh);
75
76# Default page is a login page
77my $page; # to be initialized as an HTML::Template entity sooner or later
78
[3]79
80
[2]81# decide which page to spit out...
82if (!$webvar{page}) {
83 $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
84} else {
85 $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
86}
87
88$page->param(sid => $sid);
89
90if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
[3]91
92# hmm. seeing problems in some possibly-not-so-corner cases.
[10]93# this currently only handles "domain on", "domain off"
[3]94 if (defined($webvar{action})) {
95 domStatus($dbh,$webvar{id},$webvar{action});
96 }
97
[11]98 listdomains();
[2]99
100} elsif ($webvar{page} eq 'reclist') {
101
[4]102 # Handle record list for both default records (per-group) and live domain records
[2]103
104 $page->param(defrec => $webvar{defrec});
[4]105 $page->param(id => $webvar{id});
106 $page->param(curpage => 'reclist');
[2]107
[4]108# select count(*) from (default_)?records where (group|domain)_id=?
109 my $sth = $dbh->prepare("SELECT count(*) FROM ".
110 ($webvar{defrec} eq 'y' ? 'default_' : '')."records ".
111 "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ".
112 "AND NOT type=$reverse_typemap{SOA}");
113 $sth->execute($webvar{id});
114 my ($count) = ($sth->fetchrow_array);
115
[12]116# fill the page-count and first-previous-next-last-all details
117 fill_pgcount($count,"records",domainName($dbh,$webvar{id}));
[7]118 fill_fpnla($count); # should put some params on this sub...
[4]119
120 $page->param(defrec => $webvar{defrec});
[2]121 if ($webvar{defrec} eq 'y') {
[12]122##fixme: hardcoded group
[2]123 showdomain('y',1);
124 } else {
125 showdomain('n',$webvar{id});
126 }
127
[4]128} elsif ($webvar{page} eq 'newdomain') {
[2]129
130 # weesa gonna discard parent_group_id for now
131 my $sth = $dbh->prepare("select group_id,parent_group_id,name from groups order by group_id");
132 $sth->execute;
133 my @grplist;
134 while (my ($grpid,$pargrp,$grpname) = $sth->fetchrow_array()) {
135 my %row;
136 $row{grpname} = $grpname;
137 $row{grpval} = $grpid;
138##fixme: need magic
139# $row{defgrp} = '';
140 push @grplist, \%row;
141 }
142
143 $page->param(grplist => \@grplist);
144
[11]145} elsif ($webvar{page} eq 'deldom') {
146
147 $page->param(id => $webvar{id});
148 # first pass = confirm y/n (sorta)
149 if (!defined($webvar{del})) {
150 $page->param(del_getconf => 1);
151 $page->param(domain => domainName($dbh,$webvar{id}));
152# print some neato things?
153
154# } else {
155# #whether actually deleting or cancelling we redirect to the domain list, default format
156
157 } elsif ($webvar{del} eq 'ok') {
158 my ($code,$msg) = delDomain($dbh, $webvar{id});
159 if ($code ne 'OK') {
160# need to find failure mode
161 $page->param(del_failed => 1);
162 $page->param(errmsg => $msg);
163 } else {
164 # success. go back to the domain list, do not pass "GO"
165 changepage(page => "domlist");
166 }
167 } else {
168 # cancelled. whee!
169 changepage(page => "domlist");
170 }
171
[2]172} elsif ($webvar{page} eq 'newrec') {
173 print "whee!\n";
174
[3]175 # populate most fields as needed. (eg, type list.)
176 newrec();
[2]177
178} elsif ($webvar{page} eq 'addrec') {
179
180 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
181 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
182 push @recargs, $webvar{distance};
183 if ($webvar{type} == $reverse_typemap{SRV}) {
184 push @recargs, $webvar{weight};
185 push @recargs, $webvar{port};
186 }
187 }
188 push @recargs,
189 my ($code,$msg) = addRec(@recargs);
190
191 if ($code eq 'OK') {
192 showdomain($webvar{defrec},$webvar{parentid});
193# NB: should **really** redirect here, in case of reload. >_< eyowch.
194 } else {
195 $page->param(add_failed => 1);
196 $page->param(errmsg => $msg);
197 newrec(); # populate the form... er, mostly.
198 $page->param(name => $webvar{name});
199 $page->param(address => $webvar{address});
200 $page->param(distance => $webvar{distance})
201 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
202 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
203 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
204 }
205
[3]206 $page->param(defrec => $webvar{defrec});
207
[2]208} elsif ($webvar{page} eq 'conf_del') {
209
210 $page->param(id => $webvar{id});
211 $page->param(defrec => $webvar{defrec});
212
213 my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
214
215} elsif ($webvar{page} eq 'delrec') {
216
217 $page->param(id => $webvar{id});
218 $page->param(defrec => $webvar{defrec});
219 # first pass = confirm y/n (sorta)
220 if (!defined($webvar{del})) {
221 $page->param(del_getconf => 1);
[3]222 my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
223 $page->param(host => $rec{host});
224 $page->param(ftype => $typemap{$rec{type}});
225 $page->param(recval => $rec{val});
[2]226 } else {
[3]227 my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
228 if ($code ne 'OK') {
229## need to find failure mode
230 $page->param(del_failed => 1);
231 $page->param(errmsg => $msg);
232 }
233##fixme: group/parent instead of hardcoded 1
234 showdomain('y',1);
[2]235 }
236
237} elsif ($webvar{page} eq 'editsoa') {
238
239 fillsoa($webvar{defrec},$webvar{recid});
240
241} elsif ($webvar{page} eq 'updatesoa') {
242 print "ooooo!\n";
243
244 my $sth;
245 my $sql = '';
246 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
247 # plus a bit of magic to update the appropriate table
248 $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
249 " set host='$webvar{prins}:$webvar{contact}',".
250 " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
251 " ttl=$webvar{ttl} where record_id=$webvar{recid}";
252 $sth = $dbh->prepare($sql);
253 $sth->execute;
254
255 if ($sth->err) {
256 $page->param(update_failed => 1);
257 $page->param(msg => $DBI::errstr);
258 fillsoa($webvar{defrec},1);
259 } else {
260 $page->param(update_failed => 0);
261##fixme! need to set group ID properly here
262 showdomain('y',1);
263 }
264
265} elsif ($webvar{page} eq 'adddomain') {
266 # Need some magic here.
267
268##fixme: Group should be variable
269 my ($code,$msg) = addDomain($dbh,$webvar{domain},1,($webvar{makeactive} eq 'on' ? 1 : 0));
270
271# hokay, a bit of magic to decide which page we hit.
272 if ($code eq 'OK') {
[11]273# redirect to dns.cgi?etc&page=reclist
[12]274 changepage(page => "reclist", id => $msg);
[3]275 $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
276 showdomain(0,$msg);
277##work
[2]278 } else {
279# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
280 $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
281 $page->param(add_failed => 1);
282 $page->param(domain => $webvar{domain});
283 $page->param(errmsg => $msg);
284 }
285
286
287}
288
289
[7]290## hmm. may want to move this so we can redirect after adding/deleting/etc
291print "Content-type: text/html\n\n", $header->output;
292
[2]293# spit it out
294print $page->output;
295
296print "<div id=debug>webvar keys: <pre>\n";
297foreach my $key (keys %webvar) {
298 print "key: $key\tval: $webvar{$key}\n";
299}
300print "</pre>\nENV:\n<pre>\n";
301foreach my $key (keys %ENV) {
302 print "key: $key\tval: $ENV{$key}\n";
303}
304print "</pre></div>\n";
305
306print $footer->output;
307
308
309exit 0;
310
311
[11]312sub changepage {
313 my %params = @_; # think this works the way I want...
314
315 # handle user check
316 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
317 foreach (keys %params) {
318 $newurl .= "&$_=$params{$_}";
319 }
320
321 print "Status: 302\nLocation: $newurl\n\n";
322 exit;
323} # end changepage
324
325
[2]326sub fillsoa {
327 my $def = shift;
328 my $id = shift;
329 my $domname;
330
331 if ($webvar{domain} == 0) {
332 $domname = "DOMAIN";
333 } else {
334 my $sth = $dbh->prepare("select domain from domains where domain_id=$webvar{domain}");
335 $sth->execute();
336 ($domname) = $sth->fetchrow_array();
337 }
338
339 $page->param(domain => $domname);
340 $page->param(defrec => !$webvar{domain});
341 $page->param(group => $DNSDB::group);
342
343# defaults
344 $page->param(defcontact => $DNSDB::def{contact});
345 $page->param(defns => $DNSDB::def{prins});
346 $page->param(defsoattl => $DNSDB::def{soattl});
347 $page->param(defrefresh => $DNSDB::def{refresh});
348 $page->param(defretry => $DNSDB::def{retry});
349 $page->param(defexpire => $DNSDB::def{expire});
350 $page->param(defminttl => $DNSDB::def{minttl});
351
352 # there are probably better ways to do this. TMTOWTDI.
353 my %soa = getSOA($dbh,$def,$id);
354
355 $page->param(domainid => $webvar{domain});
356 $page->param(recid => $soa{recid});
357 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
358 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
359 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
360 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
361 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
362 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
363 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
364}
365
366sub showdomain {
367 my $def = shift;
368 my $id = shift;
369
370 # get the SOA first
371 my %soa = getSOA($dbh,$def,$id);
372
373 $page->param(recid => $soa{recid});
374 $page->param(contact => $soa{contact});
375 $page->param(prins => $soa{prins});
376 $page->param(refresh => $soa{refresh});
377 $page->param(retry => $soa{retry});
378 $page->param(expire => $soa{expire});
379 $page->param(minttl => $soa{minttl});
380 $page->param(ttl => $soa{ttl});
381
382# my @foo2 = getDomRecs($dbh,'def',1);
[4]383 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
[2]384
385 my $row = 0;
386 foreach my $rec (@$foo2) {
387 $rec->{type} = $typemap{$rec->{type}};
388 $rec->{row} = $row % 2;
389 # Feh.
390 $rec->{defrec} = $webvar{defrec};
391 # And **FEH!!**
392 $rec->{sid} = $webvar{sid};
393 $row++;
394 }
395 $page->param(reclist => $foo2);
396}
397
398sub newrec {
399 my $sth = $dbh->prepare("select val,name from rectypes where stdflag=1 order by listorder");
400 $sth->execute;
401
402 my @typelist;
403 while (my ($rval,$rname) = $sth->fetchrow_array()) {
404 my %row = ( recval => $rval, recname => $rname );
405 $row{tselect} = 1 if $rval == $webvar{type};
406 push @typelist, \%row;
407 }
408 $page->param(typelist => \@typelist);
409 $page->param(domain => domainName($dbh,$webvar{domainid}));
410 $page->param(defrec => $webvar{defrec});
411 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
[3]412 if ($webvar{defrec} eq 'y') {
413 ##fixme - should be groupid
414 $page->param(parentid => 1);
415 } else {
[4]416print "DEBUG: foobar: parentid $webvar{parentid}<br>\n";
[3]417 $page->param(parentid => $webvar{parentid});
418 }
[2]419}
[7]420
421sub fill_fpnla {
422 my $count = shift;
423##fixme
424 if ($offset eq 'all') {
425 #print "foo! wanna see'em all\n";
426 } else {
427 # all these bits only have sensible behaviour if offset is numeric. err, probably.
428 if ($count > $perpage) {
429 # if there are more results than the default, always show the "all" link
430 $page->param(navall => 1);
431
432 if ($offset > 0) {
433 $page->param(navfirst => 1);
434 $page->param(navprev => 1);
435 $page->param(prevoffs => $offset-1);
436 }
437
438 # show "next" and "last" links if we're not on the last page of results
439 if ( (($offset+1) * $perpage - $count) < 0 ) {
440 $page->param(navnext => 1);
441 $page->param(nextoffs => $offset+1);
442 $page->param(navlast => 1);
[8]443 $page->param(lastoffs => int (($count-1)/$perpage));
[7]444 }
445 }
446 }
[10]447} # end fill_fpnla()
448
449
[12]450sub fill_pgcount {
451 my $pgcount = shift;
452 my $pgtype = shift;
453 my $parent = shift;
454
455 $page->param(ntot => $pgcount);
456 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
457 $page->param(npglast => ($offset eq 'all' ? $pgcount :
458 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
459 ));
460 $page->param(pgtype => $pgtype);
461 $page->param(parent => $parent);
462} # end fill_pgcount()
463
464
[11]465sub listdomains {
[10]466 my $sth = $dbh->prepare("select count(*) from domains");
467 $sth->execute;
468 my ($count) = ($sth->fetchrow_array);
469
[12]470# fill page count and first-previous-next-last-all bits
471##fixme - hardcoded group bit
472 fill_pgcount($count,"domains","default group");
[10]473 fill_fpnla($count);
474
475##fixme - group
476 $page->param(id => 1);
477 my @domlist;
478 my $sth = $dbh->prepare("select domain_id,domain,status,groups.name from domains".
479 " inner join groups on domains.group_id=groups.group_id".
480 " order by domain".($offset eq 'all' ? '' : " limit $perpage offset ".$offset*$perpage));
481 $sth->execute;
482 my $rownum = 0;
483 while (my @data = $sth->fetchrow_array) {
484 my %row;
485 $row{domainid} = $data[0];
486 $row{domain} = $data[1];
487 $row{status} = ($data[2] ? 'Active' : 'Inactive');
488 $row{group} = $data[3];
489 $row{bg} = ($rownum++)%2;
490# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
491 $row{mkactive} = !$data[2];
492 $row{sid} = $sid;
493 $row{offset} = $offset;
494##fixme: need to clean up status indicator/usage/inversion
495 push @domlist, \%row;
496 }
497 $page->param(domtable => \@domlist);
[11]498} # end listdomains()
Note: See TracBrowser for help on using the repository browser.