[2] | 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 | ###
|
---|
[3] | 9 | # Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx>
|
---|
[2] | 10 |
|
---|
| 11 | use strict;
|
---|
| 12 | use warnings;
|
---|
| 13 |
|
---|
| 14 | use CGI::Carp qw (fatalsToBrowser);
|
---|
| 15 | use CGI::Simple;
|
---|
| 16 | use HTML::Template;
|
---|
| 17 | use CGI::Session;
|
---|
[29] | 18 | use Crypt::PasswdMD5;
|
---|
[30] | 19 | use Net::DNS;
|
---|
[2] | 20 | use DBI;
|
---|
| 21 |
|
---|
| 22 | use lib '.';
|
---|
| 23 | # custom modules
|
---|
| 24 | use DNSDB qw(:ALL);
|
---|
| 25 |
|
---|
[13] | 26 | my @debugbits; # temp, to be spit out near the end of processing
|
---|
[38] | 27 | my $debugenv = 0;
|
---|
[13] | 28 |
|
---|
[2] | 29 | # Let's do these templates right...
|
---|
| 30 | my $templatedir = "templates";
|
---|
| 31 | my $sessiondir = "session";
|
---|
| 32 |
|
---|
| 33 | # Set up the CGI object...
|
---|
| 34 | my $q = new CGI::Simple;
|
---|
| 35 | # ... and get query-string params as well as POST params if necessary
|
---|
| 36 | $q->parse_query_string;
|
---|
| 37 |
|
---|
| 38 | # This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
|
---|
[7] | 39 | my %webvar = $q->Vars;
|
---|
[2] | 40 |
|
---|
[13] | 41 | # persistent stuff needed on most/all pages
|
---|
[2] | 42 | my $sid = ($webvar{sid} ? $webvar{sid} : undef);
|
---|
| 43 | my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir});
|
---|
| 44 | #$sid = $session->id() if !$sid;
|
---|
| 45 | if (!$sid) {
|
---|
| 46 | # init stuff. can probably axe this down to just above if'n'when user manipulation happens
|
---|
| 47 | $sid = $session->id();
|
---|
| 48 | # need to know the "upper" group the user can deal with; may as well
|
---|
| 49 | # stick this in the session rather than calling out to the DB every time.
|
---|
[18] | 50 | $session->param('logingroup',1);
|
---|
| 51 | $session->param('curgroup',1); # yes, we *do* need to track this too. er, probably.
|
---|
[51] | 52 | $session->param('domlistsortby','domain');
|
---|
| 53 | $session->param('domlistorder','ASC');
|
---|
| 54 | $session->param('useradminsortby','username');
|
---|
| 55 | $session->param('useradminorder','ASC');
|
---|
| 56 | $session->param('grpmansortby','group');
|
---|
| 57 | $session->param('grpmanorder','ASC');
|
---|
| 58 | $session->param('reclistsortby','name');
|
---|
| 59 | $session->param('reclistorder','ASC');
|
---|
[53] | 60 | # $session->param('filter','login');
|
---|
| 61 | # $session->param('startwith','login');
|
---|
| 62 | # $session->param('searchsubs','login');
|
---|
[2] | 63 | }
|
---|
| 64 |
|
---|
[19] | 65 | my $logingroup = ($session->param('logingroup') ? $session->param('logingroup') : 1);
|
---|
| 66 | my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $logingroup);
|
---|
[17] | 67 | my $group = ($webvar{group} ? $webvar{group} : 1);
|
---|
[18] | 68 |
|
---|
[53] | 69 | # filter, starts with, search subgroups
|
---|
| 70 | $session->param('filter', "$webvar{page}:filter$webvar{filter}") if defined($webvar{filter});
|
---|
| 71 | my $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});
|
---|
| 75 | my $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});
|
---|
| 80 | my $searchsubs = $session->param('searchsubs');
|
---|
| 81 | $searchsubs = ($searchsubs =~ /^$webvar{page}:searchsubs(.+)$/ ? $1 : '');
|
---|
| 82 |
|
---|
[26] | 83 | # nrgh, can't handle login here because we don't have a database handle to check the user/pass with yet
|
---|
[2] | 84 |
|
---|
[20] | 85 | if ($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 |
|
---|
[2] | 92 | my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
|
---|
| 93 | my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
|
---|
| 94 |
|
---|
| 95 | # default
|
---|
[38] | 96 | #my $perpage = 15;
|
---|
| 97 | my $perpage = 3;
|
---|
[2] | 98 | my $offset = ($webvar{offset} ? $webvar{offset} : 0);
|
---|
| 99 |
|
---|
| 100 | # NB: these must match the field name and SQL ascend/descend syntax respectively
|
---|
[41] | 101 | my $sortby = "domain";
|
---|
| 102 | my $sortorder = "ASC";
|
---|
[2] | 103 |
|
---|
[29] | 104 | my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret","dbhost");
|
---|
[2] | 105 | #my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
|
---|
| 106 | # { AutoCommit => 0 }) or die $DBI::errstr;
|
---|
| 107 |
|
---|
| 108 | ##fixme. PLEASE! <G>
|
---|
| 109 | print $msg if !$dbh;
|
---|
| 110 |
|
---|
| 111 | # fiddle hardcoded "defaults" as per system/user (?) prefs
|
---|
| 112 | initGlobals($dbh);
|
---|
| 113 |
|
---|
[26] | 114 | # handle login redirect
|
---|
[30] | 115 | if ($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);
|
---|
[26] | 121 |
|
---|
[30] | 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 | }
|
---|
[29] | 127 |
|
---|
[30] | 128 | # set session bits
|
---|
| 129 | $session->param('logingroup',$gid);
|
---|
| 130 | $session->param('curgroup',$gid);
|
---|
| 131 | $session->param('username',$webvar{username});
|
---|
[26] | 132 |
|
---|
[30] | 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 | }
|
---|
[26] | 145 | }
|
---|
| 146 |
|
---|
[15] | 147 | ## Default page is a login page
|
---|
| 148 | #my $page; # to be initialized as an HTML::Template entity sooner or later
|
---|
[2] | 149 |
|
---|
[3] | 150 |
|
---|
| 151 |
|
---|
[2] | 152 | # decide which page to spit out...
|
---|
[15] | 153 | $webvar{page} = 'login' if !$webvar{page};
|
---|
| 154 | #if (!$webvar{page}) {
|
---|
| 155 | # $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
|
---|
| 156 | #} else {
|
---|
| 157 | #}
|
---|
[2] | 158 |
|
---|
[15] | 159 | my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
|
---|
| 160 |
|
---|
[2] | 161 | $page->param(sid => $sid);
|
---|
| 162 |
|
---|
[26] | 163 | if ($webvar{page} eq 'login') {
|
---|
[3] | 164 |
|
---|
[26] | 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 |
|
---|
[3] | 171 | # hmm. seeing problems in some possibly-not-so-corner cases.
|
---|
[10] | 172 | # this currently only handles "domain on", "domain off"
|
---|
[3] | 173 | if (defined($webvar{action})) {
|
---|
| 174 | domStatus($dbh,$webvar{id},$webvar{action});
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[18] | 177 | $page->param(curpage => $webvar{page});
|
---|
| 178 |
|
---|
[11] | 179 | listdomains();
|
---|
[2] | 180 |
|
---|
[4] | 181 | } elsif ($webvar{page} eq 'newdomain') {
|
---|
[2] | 182 |
|
---|
| 183 |
|
---|
[11] | 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);
|
---|
[22] | 202 | listdomains($curgroup);
|
---|
[11] | 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 |
|
---|
[47] | 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 |
|
---|
[13] | 240 | } elsif ($webvar{page} eq 'record') {
|
---|
[16] | 241 |
|
---|
[13] | 242 | if ($webvar{recact} eq 'new') {
|
---|
[16] | 243 |
|
---|
[15] | 244 | $page->param(todo => "Add record to");
|
---|
| 245 | $page->param(recact => "add");
|
---|
[16] | 246 | fill_rectypes();
|
---|
| 247 |
|
---|
[15] | 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 {
|
---|
[24] | 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});
|
---|
[16] | 272 | fill_recdata(); # populate the form... er, mostly.
|
---|
[15] | 273 | }
|
---|
| 274 |
|
---|
[13] | 275 | } elsif ($webvar{recact} eq 'edit') {
|
---|
[15] | 276 |
|
---|
[16] | 277 | $page->param(todo => "Update record");
|
---|
| 278 | $page->param(recact => "update");
|
---|
| 279 | $page->param(parentid => $webvar{parentid});
|
---|
[17] | 280 | $page->param(id => $webvar{id});
|
---|
[16] | 281 | $page->param(defrec => $webvar{defrec});
|
---|
[13] | 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);
|
---|
[16] | 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') {
|
---|
[17] | 301 | changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
|
---|
[16] | 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});
|
---|
[17] | 310 | $page->param(id => $webvar{id});
|
---|
[16] | 311 | fill_recdata();
|
---|
| 312 | }
|
---|
[13] | 313 | }
|
---|
[16] | 314 |
|
---|
[13] | 315 | if ($webvar{defrec} eq 'y') {
|
---|
[20] | 316 | $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
|
---|
[13] | 317 | } else {
|
---|
[24] | 318 | $page->param(parentid => $webvar{parentid});
|
---|
| 319 | # $page->param(id => $webvar{id});
|
---|
[16] | 320 | $page->param(dohere => domainName($dbh,$webvar{parentid}));
|
---|
[13] | 321 | }
|
---|
| 322 |
|
---|
[2] | 323 | } elsif ($webvar{page} eq 'newrec') {
|
---|
[13] | 324 | push @debugbits, "whee!\n";
|
---|
[2] | 325 |
|
---|
[3] | 326 | # populate most fields as needed. (eg, type list.)
|
---|
[13] | 327 | stdrecs();
|
---|
[2] | 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 | }
|
---|
[13] | 339 | # wtf?
|
---|
| 340 | # push @recargs,
|
---|
[2] | 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);
|
---|
[13] | 349 | stdrecs($webvar{type}); # populate the form... er, mostly.
|
---|
[2] | 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 |
|
---|
[3] | 358 | $page->param(defrec => $webvar{defrec});
|
---|
| 359 |
|
---|
[2] | 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});
|
---|
[39] | 371 | $page->param(parentid => $webvar{parentid});
|
---|
[2] | 372 | # first pass = confirm y/n (sorta)
|
---|
| 373 | if (!defined($webvar{del})) {
|
---|
| 374 | $page->param(del_getconf => 1);
|
---|
[3] | 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});
|
---|
[39] | 379 | } elsif ($webvar{del} eq 'ok') {
|
---|
[3] | 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);
|
---|
[39] | 385 | showdomain($webvar{defrec}, $webvar{parentid});
|
---|
| 386 | } else {
|
---|
| 387 | changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
|
---|
[3] | 388 | }
|
---|
[39] | 389 | } else {
|
---|
| 390 | changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
|
---|
[2] | 391 | }
|
---|
| 392 |
|
---|
| 393 | } elsif ($webvar{page} eq 'editsoa') {
|
---|
| 394 |
|
---|
[39] | 395 | fillsoa($webvar{defrec},$webvar{id});
|
---|
[2] | 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
|
---|
[39] | 403 | $sql = "update ".($webvar{defrec} eq 'y' ? "default_records" : "records").
|
---|
[2] | 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);
|
---|
[39] | 413 | fillsoa($webvar{defrec},$webvar{id});
|
---|
[2] | 414 | } else {
|
---|
[39] | 415 | changepage(page => "reclist", id => $webvar{id}, defrec => $webvar{defrec});
|
---|
[2] | 416 | $page->param(update_failed => 0);
|
---|
| 417 | ##fixme! need to set group ID properly here
|
---|
[39] | 418 | # showdomain('y',1);
|
---|
[2] | 419 | }
|
---|
| 420 |
|
---|
| 421 | } elsif ($webvar{page} eq 'adddomain') {
|
---|
| 422 | # Need some magic here.
|
---|
| 423 |
|
---|
| 424 | ##fixme: Group should be variable
|
---|
[20] | 425 | my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
|
---|
[2] | 426 |
|
---|
| 427 | # hokay, a bit of magic to decide which page we hit.
|
---|
| 428 | if ($code eq 'OK') {
|
---|
[11] | 429 | # redirect to dns.cgi?etc&page=reclist
|
---|
[12] | 430 | changepage(page => "reclist", id => $msg);
|
---|
[24] | 431 | # $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
|
---|
| 432 | # showdomain(0,$msg);
|
---|
[2] | 433 | } else {
|
---|
| 434 | # oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
|
---|
[24] | 435 | ##fixme: session ID
|
---|
[2] | 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 |
|
---|
[17] | 442 | } elsif ($webvar{page} eq 'grpman') {
|
---|
[2] | 443 |
|
---|
[22] | 444 | listgroups();
|
---|
[18] | 445 | $page->param(curpage => $webvar{page});
|
---|
| 446 |
|
---|
[17] | 447 | } elsif ($webvar{page} eq 'newgrp') {
|
---|
[20] | 448 |
|
---|
[18] | 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});
|
---|
[20] | 457 | fill_grouplist('pargroup',$webvar{pargroup});
|
---|
[19] | 458 | } else {
|
---|
| 459 | # $page->param
|
---|
[20] | 460 | fill_grouplist('pargroup',$curgroup);
|
---|
[19] | 461 |
|
---|
[18] | 462 | }
|
---|
[20] | 463 |
|
---|
[22] | 464 | } elsif ($webvar{page} eq 'delgrp') {
|
---|
[20] | 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);
|
---|
[23] | 470 | # $page->param(groupname => groupName($dbh,$webvar{id}));
|
---|
[20] | 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});
|
---|
[23] | 478 | push @debugbits, groupName($dbh, $webvar{id});
|
---|
[20] | 479 | if ($code ne 'OK') {
|
---|
| 480 | # need to find failure mode
|
---|
| 481 | $page->param(del_failed => 1);
|
---|
| 482 | $page->param(errmsg => $msg);
|
---|
[22] | 483 | $page->param(curpage => $webvar{page});
|
---|
| 484 | listgroups();
|
---|
[20] | 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 | }
|
---|
[23] | 493 | $page->param(delgroupname => groupName($dbh, $webvar{id}));
|
---|
[24] | 494 |
|
---|
| 495 | } elsif ($webvar{page} eq 'useradmin') {
|
---|
| 496 |
|
---|
[51] | 497 | if (defined($webvar{action})) {
|
---|
| 498 | userStatus($dbh,$webvar{id},$webvar{action});
|
---|
| 499 | }
|
---|
| 500 |
|
---|
[24] | 501 | $page->param(curpage => $webvar{page});
|
---|
| 502 |
|
---|
[51] | 503 | list_users();
|
---|
| 504 |
|
---|
[24] | 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 {
|
---|
[38] | 518 | ($code,$msg) = addUser($dbh,$webvar{uname}, $webvar{group}, $webvar{pass1},
|
---|
[25] | 519 | ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype},
|
---|
| 520 | $webvar{fname}, $webvar{lname}, $webvar{phone});
|
---|
[24] | 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 {
|
---|
[38] | 527 | # oddity - apparently, xhtml 1.0 strict swallows username as an HTML::Template var. O_o
|
---|
[24] | 528 | $page->param(add_failed => 1);
|
---|
[38] | 529 | $page->param(uname => $webvar{uname});
|
---|
[24] | 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 |
|
---|
[38] | 538 | # $page->param(add_failed => 1);
|
---|
[25] | 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 |
|
---|
[30] | 563 | } elsif ($webvar{page} eq 'dnsq') {
|
---|
| 564 |
|
---|
| 565 | $page->param(qfor => $webvar{qfor}) if $webvar{qfor};
|
---|
[31] | 566 | fill_rectypes($webvar{type} ? $webvar{type} : '', 1);
|
---|
| 567 | $page->param(nrecurse => $webvar{nrecurse}) if $webvar{nrecurse};
|
---|
[30] | 568 | $page->param(resolver => $webvar{resolver}) if $webvar{resolver};
|
---|
| 569 |
|
---|
| 570 | if ($webvar{qfor}) {
|
---|
| 571 | my $resolv = Net::DNS::Resolver->new;
|
---|
[31] | 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};
|
---|
[30] | 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) =
|
---|
[31] | 586 | ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/s);
|
---|
[30] | 587 | $row{host} = $host;
|
---|
| 588 | $row{ftype} = $type;
|
---|
[31] | 589 | $row{rdata} = ($type eq 'SOA' ? "<pre>$data</pre>" : $data);
|
---|
[30] | 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 |
|
---|
[31] | 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};
|
---|
[37] | 637 | $page->param(dominactive => 1) if (!$webvar{domactive} && $webvar{doit}); # eww.
|
---|
[31] | 638 | $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
|
---|
| 639 | ##work
|
---|
[33] | 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;
|
---|
| 649 | my $rnum = 0;
|
---|
| 650 | foreach my $domain (@domlist) {
|
---|
[34] | 651 | my %row;
|
---|
| 652 | my ($code,$msg) = importAXFR($dbh, $webvar{ifrom}, $domain, $webvar{group},
|
---|
| 653 | $webvar{domstatus}, $webvar{rwsoa}, $webvar{rwns});
|
---|
[35] | 654 | $row{domok} = $msg if $code eq 'OK';
|
---|
| 655 | if ($code eq 'WARN') {
|
---|
| 656 | $msg =~ s|\n|<br />|g;
|
---|
| 657 | $row{domwarn} = $msg;
|
---|
| 658 | }
|
---|
[37] | 659 | if ($code eq 'FAIL') {
|
---|
| 660 | $msg =~ s|\n|<br />|g;
|
---|
| 661 | $row{domerr} = $msg;
|
---|
| 662 | }
|
---|
[33] | 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 |
|
---|
[48] | 671 | } elsif ($webvar{page} eq 'whoisq') {
|
---|
[47] | 672 |
|
---|
[48] | 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/\©/g;
|
---|
| 690 | $dominfo =~ s/\xae/\®/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 |
|
---|
[47] | 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 |
|
---|
[2] | 718 | }
|
---|
| 719 |
|
---|
| 720 |
|
---|
[17] | 721 | # start output here so we can redirect pages.
|
---|
[7] | 722 | print "Content-type: text/html\n\n", $header->output;
|
---|
| 723 |
|
---|
[20] | 724 | ##common bits
|
---|
[17] | 725 | if ($webvar{page} ne 'login') {
|
---|
[30] | 726 | $page->param(username => $session->param("username"));
|
---|
| 727 |
|
---|
[20] | 728 | $page->param(group => $curgroup);
|
---|
| 729 | $page->param(groupname => groupName($dbh,$curgroup));
|
---|
[43] | 730 | $page->param(logingrp => groupName($dbh,$logingroup));
|
---|
[20] | 731 |
|
---|
[24] | 732 | # group tree. should go elsewhere, probably
|
---|
| 733 | my $tmpgrplist = fill_grptree($logingroup,$curgroup);
|
---|
| 734 | $page->param(grptree => $tmpgrplist);
|
---|
| 735 |
|
---|
[42] | 736 | $page->param(inlogingrp => $curgroup == $logingroup);
|
---|
| 737 |
|
---|
[20] | 738 | # stuff for menu group change. nb: this is icky.
|
---|
| 739 | fill_grouplist("grouplist");
|
---|
[53] | 740 |
|
---|
| 741 | ## set up "URL to self"
|
---|
[38] | 742 | # @#$%@%@#% XHTML - & in a URL must be escaped. >:(
|
---|
| 743 | my $tmp_ruri = $ENV{REQUEST_URI};
|
---|
| 744 | $tmp_ruri =~ s/\&([a-z])/\&\;$1/g;
|
---|
[53] | 745 |
|
---|
[39] | 746 | # le sigh. and we need to strip any previous action
|
---|
| 747 | $tmp_ruri =~ s/\&action=[^&]+//g;
|
---|
[52] | 748 |
|
---|
[53] | 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/\&startwith=[a-z09-]*(\&)?/$1/g;
|
---|
| 754 | $tmp_ruri =~ s/\&searchsubs=[a-z09-]*(\&)?/$1/g;
|
---|
| 755 | $tmp_ruri =~ s/\&filter=[a-z09-]*(\&)?/$1/g;
|
---|
| 756 | use warnings qw(uninitialized);
|
---|
[52] | 757 |
|
---|
[53] | 758 | # fill in the URL-to-self
|
---|
[38] | 759 | $page->param(whereami => $tmp_ruri);
|
---|
[17] | 760 | }
|
---|
[13] | 761 |
|
---|
[24] | 762 | foreach (@debugbits) { print; }
|
---|
| 763 |
|
---|
[2] | 764 | # spit it out
|
---|
| 765 | print $page->output;
|
---|
| 766 |
|
---|
[38] | 767 | if ($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";
|
---|
[2] | 782 | }
|
---|
| 783 |
|
---|
| 784 | print $footer->output;
|
---|
| 785 |
|
---|
[18] | 786 | # as per the docs, Just In Case
|
---|
| 787 | $session->flush();
|
---|
[2] | 788 |
|
---|
| 789 | exit 0;
|
---|
| 790 |
|
---|
| 791 |
|
---|
[24] | 792 | sub 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 |
|
---|
[11] | 814 | sub 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 |
|
---|
[30] | 823 | # Just In Case
|
---|
| 824 | $session->flush();
|
---|
| 825 |
|
---|
[11] | 826 | print "Status: 302\nLocation: $newurl\n\n";
|
---|
| 827 | exit;
|
---|
| 828 | } # end changepage
|
---|
| 829 |
|
---|
| 830 |
|
---|
[2] | 831 | sub fillsoa {
|
---|
| 832 | my $def = shift;
|
---|
| 833 | my $id = shift;
|
---|
[39] | 834 | my $domname = ($def eq 'y' ? '' : "DOMAIN");
|
---|
[2] | 835 |
|
---|
[39] | 836 | $page->param(defrec => $def);
|
---|
[2] | 837 |
|
---|
[39] | 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)) );
|
---|
[2] | 843 |
|
---|
| 844 | # defaults
|
---|
[17] | 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});
|
---|
[2] | 852 |
|
---|
| 853 | # there are probably better ways to do this. TMTOWTDI.
|
---|
| 854 | my %soa = getSOA($dbh,$def,$id);
|
---|
| 855 |
|
---|
[39] | 856 | $page->param(id => $id);
|
---|
[2] | 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 |
|
---|
| 867 | sub 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);
|
---|
[4] | 884 | my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
|
---|
[2] | 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};
|
---|
[13] | 892 | $rec->{id} = $id;
|
---|
[23] | 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');
|
---|
[2] | 896 | $row++;
|
---|
| 897 | }
|
---|
| 898 | $page->param(reclist => $foo2);
|
---|
| 899 | }
|
---|
| 900 |
|
---|
[23] | 901 |
|
---|
[16] | 902 | # fill in record type list on add/update/edit record template
|
---|
| 903 | sub fill_rectypes {
|
---|
[13] | 904 | my $type = shift || $reverse_typemap{A};
|
---|
[31] | 905 | my $soaflag = shift || 0;
|
---|
[13] | 906 |
|
---|
[17] | 907 | my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
|
---|
[2] | 908 | $sth->execute;
|
---|
| 909 | my @typelist;
|
---|
| 910 | while (my ($rval,$rname) = $sth->fetchrow_array()) {
|
---|
| 911 | my %row = ( recval => $rval, recname => $rname );
|
---|
[13] | 912 | $row{tselect} = 1 if $rval == $type;
|
---|
[2] | 913 | push @typelist, \%row;
|
---|
| 914 | }
|
---|
[31] | 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 | }
|
---|
[2] | 920 | $page->param(typelist => \@typelist);
|
---|
[31] | 921 | } # fill_rectypes
|
---|
[16] | 922 |
|
---|
| 923 | sub 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};
|
---|
[2] | 932 | $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
|
---|
| 933 | }
|
---|
[7] | 934 |
|
---|
[24] | 935 |
|
---|
| 936 | sub 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 |
|
---|
[7] | 951 | sub fill_fpnla {
|
---|
| 952 | my $count = shift;
|
---|
| 953 | ##fixme
|
---|
| 954 | if ($offset eq 'all') {
|
---|
[41] | 955 | # uhm....
|
---|
[7] | 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);
|
---|
[8] | 973 | $page->param(lastoffs => int (($count-1)/$perpage));
|
---|
[7] | 974 | }
|
---|
| 975 | }
|
---|
| 976 | }
|
---|
[10] | 977 | } # end fill_fpnla()
|
---|
| 978 |
|
---|
| 979 |
|
---|
[12] | 980 | sub 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 |
|
---|
[11] | 995 | sub listdomains {
|
---|
[41] | 996 |
|
---|
[53] | 997 | ##fixme: $logingroup or $curgroup?
|
---|
[52] | 998 | my @childgroups;
|
---|
[53] | 999 | getChildren($dbh, $logingroup, \@childgroups, 'all') if $searchsubs;
|
---|
[52] | 1000 | my $childlist = join(',',@childgroups);
|
---|
| 1001 |
|
---|
| 1002 | my $sql = "SELECT count(*) FROM domains WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
|
---|
[53] | 1003 | ($startwith ? " AND domain ~* '^[$startwith]'" : '').
|
---|
| 1004 | ($filter ? " AND domain ~* '$filter'" : '');
|
---|
[52] | 1005 | my $sth = $dbh->prepare($sql);
|
---|
| 1006 | $sth->execute;
|
---|
[17] | 1007 | my ($count) = $sth->fetchrow_array;
|
---|
| 1008 |
|
---|
[12] | 1009 | # fill page count and first-previous-next-last-all bits
|
---|
| 1010 | ##fixme - hardcoded group bit
|
---|
[20] | 1011 | fill_pgcount($count,"domains",groupName($dbh,$curgroup));
|
---|
[10] | 1012 | fill_fpnla($count);
|
---|
| 1013 |
|
---|
[41] | 1014 | # sort/order
|
---|
[51] | 1015 | $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
|
---|
| 1016 | $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
|
---|
[41] | 1017 |
|
---|
[51] | 1018 | $sortby = $session->param($webvar{page}.'sortby');
|
---|
| 1019 | $sortorder = $session->param($webvar{page}.'order');
|
---|
| 1020 |
|
---|
[44] | 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);
|
---|
[41] | 1025 |
|
---|
[47] | 1026 | # $page->param(sortorder => $sortorder);
|
---|
[41] | 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 |
|
---|
[51] | 1031 | # waffle, waffle - keep state on these as well as sortby, sortorder?
|
---|
[53] | 1032 | $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
|
---|
[41] | 1033 |
|
---|
[53] | 1034 | $page->param(filter => $filter) if $filter;
|
---|
| 1035 | $page->param(searchsubs => $searchsubs) if $searchsubs;
|
---|
[41] | 1036 |
|
---|
| 1037 | ##fixme
|
---|
| 1038 | ##fixme push the SQL and direct database fiddling off into a sub in DNSDB.pm
|
---|
| 1039 | ##fixme
|
---|
| 1040 |
|
---|
[20] | 1041 | $page->param(group => $curgroup);
|
---|
[10] | 1042 | my @domlist;
|
---|
[52] | 1043 | $sql = "SELECT domain_id,domain,status,groups.group_name AS group FROM domains".
|
---|
[20] | 1044 | " INNER JOIN groups ON domains.group_id=groups.group_id".
|
---|
[52] | 1045 | " WHERE domains.group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
|
---|
[41] | 1046 | ##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
|
---|
[53] | 1047 | ($startwith ? " AND domain ~* '^[$startwith]'" : '').
|
---|
| 1048 | ($filter ? " AND domain ~* '$filter'" : '').
|
---|
[41] | 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);
|
---|
[52] | 1052 | $sth->execute;
|
---|
[10] | 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);
|
---|
[11] | 1069 | } # end listdomains()
|
---|
[18] | 1070 |
|
---|
| 1071 |
|
---|
[22] | 1072 | sub listgroups {
|
---|
[53] | 1073 |
|
---|
[26] | 1074 | my @childgroups;
|
---|
[53] | 1075 | getChildren($dbh, $logingroup, \@childgroups, 'all') if $searchsubs;
|
---|
[26] | 1076 | my $childlist = join(',',@childgroups);
|
---|
| 1077 |
|
---|
[52] | 1078 | my $sql = "SELECT count(*) FROM groups WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
|
---|
[53] | 1079 | ($startwith ? " AND group_name ~* '^[$startwith]'" : '').
|
---|
| 1080 | ($filter ? " AND group_name ~* '$filter'" : '');
|
---|
[26] | 1081 | my $sth = $dbh->prepare($sql);
|
---|
| 1082 |
|
---|
[22] | 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 |
|
---|
[51] | 1090 | # $sortby = 'group';
|
---|
[42] | 1091 | # sort/order
|
---|
[51] | 1092 | $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
|
---|
| 1093 | $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
|
---|
[42] | 1094 |
|
---|
[51] | 1095 | $sortby = $session->param($webvar{page}.'sortby');
|
---|
| 1096 | $sortorder = $session->param($webvar{page}.'order');
|
---|
| 1097 |
|
---|
[44] | 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);
|
---|
[42] | 1102 |
|
---|
[51] | 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 |
|
---|
[53] | 1106 | $page->param(filter => $filter) if $filter;
|
---|
| 1107 | $page->param(searchsubs => $searchsubs) if $searchsubs;
|
---|
[51] | 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 |
|
---|
[22] | 1113 | my @grouplist;
|
---|
| 1114 | $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
|
---|
[51] | 1115 | "count(distinct(u.username)) AS nusers, count(distinct(d.domain)) AS ndomains ".
|
---|
[22] | 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 ".
|
---|
[26] | 1120 | "WHERE g.group_id IN ($logingroup".($childlist ? ",$childlist" : '').") ".
|
---|
[51] | 1121 | ##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
|
---|
[53] | 1122 | ($startwith ? " AND g.group_name ~* '^[$startwith]'" : '').
|
---|
| 1123 | ($filter ? " AND g.group_name ~* '$filter'" : '').
|
---|
[51] | 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));
|
---|
[22] | 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 |
|
---|
[20] | 1145 | sub fill_grouplist {
|
---|
[19] | 1146 | my $template_var = shift;
|
---|
| 1147 | my $cur = shift || $curgroup;
|
---|
[26] | 1148 |
|
---|
| 1149 | my @childgroups;
|
---|
| 1150 | getChildren($dbh, $logingroup, \@childgroups, 'all');
|
---|
| 1151 | my $childlist = join(',',@childgroups);
|
---|
| 1152 |
|
---|
[18] | 1153 | # weesa gonna discard parent_group_id for now
|
---|
[26] | 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");
|
---|
[18] | 1157 | $sth->execute;
|
---|
[20] | 1158 | my @grouplist;
|
---|
| 1159 | while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
|
---|
[18] | 1160 | my %row;
|
---|
[20] | 1161 | $row{groupname} = $groupname;
|
---|
| 1162 | $row{groupval} = $groupid;
|
---|
[18] | 1163 | ##fixme: need magic
|
---|
[20] | 1164 | # $row{defgroup} = '';
|
---|
| 1165 | $row{groupactive} = 1 if $groupid == $cur;
|
---|
| 1166 | push @grouplist, \%row;
|
---|
[18] | 1167 | }
|
---|
| 1168 |
|
---|
[20] | 1169 | $page->param("$template_var" => \@grouplist);
|
---|
[18] | 1170 |
|
---|
[24] | 1171 | } # end fill_grouplist()
|
---|
| 1172 |
|
---|
[26] | 1173 |
|
---|
[24] | 1174 | sub list_users {
|
---|
[52] | 1175 |
|
---|
| 1176 | my @childgroups;
|
---|
[53] | 1177 | getChildren($dbh, $curgroup, \@childgroups, 'all') if $searchsubs;
|
---|
[52] | 1178 | my $childlist = join(',',@childgroups);
|
---|
| 1179 |
|
---|
| 1180 | my $sql = "SELECT count(*) FROM users WHERE group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
|
---|
[53] | 1181 | ($startwith ? " AND username ~* '^[$startwith]'" : '').
|
---|
| 1182 | ($filter ? " AND username ~* '$filter'" : '');
|
---|
[52] | 1183 | my $sth = $dbh->prepare($sql);
|
---|
| 1184 | $sth->execute;
|
---|
[24] | 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 |
|
---|
[51] | 1192 | # $sortby = 'user';
|
---|
[44] | 1193 | # sort/order
|
---|
[51] | 1194 | $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
|
---|
| 1195 | $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
|
---|
[44] | 1196 |
|
---|
[51] | 1197 | $sortby = $session->param($webvar{page}.'sortby');
|
---|
| 1198 | $sortorder = $session->param($webvar{page}.'order');
|
---|
| 1199 |
|
---|
[44] | 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 |
|
---|
[51] | 1205 | # waffle, waffle - keep state on these as well as sortby, sortorder?
|
---|
[53] | 1206 | $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^[a-z]$/;
|
---|
[51] | 1207 |
|
---|
[53] | 1208 | $page->param(filter => $filter) if $filter;
|
---|
| 1209 | $page->param(searchsubs => $searchsubs) if $searchsubs;
|
---|
[51] | 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 |
|
---|
[24] | 1217 | my @userlist;
|
---|
[52] | 1218 | $sql = "SELECT u.user_id, u.username, u.firstname || ' ' || u.lastname AS fname, u.type, g.group_name, u.status ".
|
---|
[24] | 1219 | "FROM users u ".
|
---|
| 1220 | "INNER JOIN groups g ON u.group_id=g.group_id ".
|
---|
[52] | 1221 | "WHERE u.group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
|
---|
[51] | 1222 | ##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
|
---|
[53] | 1223 | ($startwith ? " AND u.username ~* '^[$startwith]'" : '').
|
---|
| 1224 | ($filter ? " AND u.username ~* '$filter'" : '').
|
---|
[51] | 1225 | " ORDER BY $sortby $sortorder ".
|
---|
[52] | 1226 | ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
|
---|
[51] | 1227 |
|
---|
[52] | 1228 | $sth = $dbh->prepare($sql);
|
---|
| 1229 | $sth->execute;
|
---|
[24] | 1230 |
|
---|
| 1231 | my $rownum = 0;
|
---|
| 1232 | while (my @data = $sth->fetchrow_array) {
|
---|
[41] | 1233 | no warnings "uninitialized"; # Just In Case something stupid happens and a user gets no first or last name
|
---|
[24] | 1234 | my %row;
|
---|
| 1235 | $row{userid} = $data[0];
|
---|
| 1236 | $row{username} = $data[1];
|
---|
[51] | 1237 | $row{userfull} = $data[2];
|
---|
| 1238 | $row{usertype} = ($data[3] eq 'S' ? 'superuser' : "user");
|
---|
| 1239 | $row{usergroup} = $data[4];
|
---|
| 1240 | $row{active} = $data[5];
|
---|
[24] | 1241 | $row{bg} = ($rownum++)%2;
|
---|
| 1242 | $row{sid} = $sid;
|
---|
| 1243 | push @userlist, \%row;
|
---|
| 1244 | }
|
---|
| 1245 | $page->param(usertable => \@userlist);
|
---|
[18] | 1246 | }
|
---|
[43] | 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
|
---|
| 1251 | sub 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
|
---|