package ISPMan::DNSMan::SOArecords;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $Config);

require Exporter;

@ISA = qw(Exporter AutoLoader);
@EXPORT = qw(
editsoarecords
addsoarecord
modifysoarecords
getSOA
);
$VERSION = '0.01';

use ISPMan::Config;
$Config=ISPMan::Config->new();


sub editsoarecords {
   my $self=shift;
   my $r=shift;
   my $dn=$r->param("dn");

   my $records=$self->getSOA($dn);

   print  $r->param('domain'), "::SOA Records<br><hr noshade>";
   print qq|
     <form method=POST>
     <input type=hidden name=dn value="$dn">
     <input type=hidden name=mode value="modifysoarecords">
  |;

  print "<table border=0>\n";
  for (qw( primary  rootmail expire retry refresh minimum)) {
       print qq|
       <tr>
       <td><font face="Helvetica, Arial" color="#666699" size=3><b>$_</b></font></td>
        <td><font face="Helvetica, Arial" color="#666699" size=3><b><input  size=25 type=text name="$_" value='$records->{$_}'></b></font></td>
     </tr>
     |;
  }
  print qq|
  <tr>

     <td colspan=2>
     <a href="javascript:void(document.forms[0].reset())"><img name="resetForm" border=0 alt="Discard Changes" src="/gfx/but/but_discard_chg_0.gif"></a>
     <a href="javascript:void(document.forms[0].submit())"><img name="applyChanges" border=0 alt="Apply Changes" src="/gfx/but/but_apply_chg_0.gif"></a>
     </td>
  <tr>

     <input type=hidden name=serial value='$records->{'serial'}'>
  </form>
  |;

  print "</table>";

}


sub addsoarecord {
   my $self=shift;
   my $r=shift;
   my $dn=$r->param("dn");
   
   my $record=join ',', ($r->param("origion"), $r->param("pref"), $r->param("host"));
   $r->param("record", $record);
   $self->{'ldap'}->addEntry($r);
   $self->editsoarecords($r);
}



sub modifysoarecords {
   my $self=shift;
   my $r=shift;
   my $dn=$r->param("dn");
   my $data;
   for (qw(primary rootmail expire retry refresh minimum serial)) {
      $data->{$_}=$r->param($_);
   }
   
   $self->{'ldap'}->updateEntryWithData($dn, $data);
   $self->editsoarecords($r);
}


sub getSOA {
   my $self=shift;
   my $dn=shift;
   
   $dn=($dn=~/^cn/)?$dn:"cn=soarecords, ou=dnsdata, $dn";
   my $soa=$self->{'ldap'}->getEntryAsHashRef($dn);
   my $num="00";

   if ($soa->{'serial'}) {
      ($num)=$soa->{'serial'} =~/(\d\d$)/;
   }
   $soa->{'serial'}=join "", ($self->{'year'}, $self->{'mon'}, $self->{'mday'}, $num);
   $soa->{'serial'}++;
   return $soa;
}




1;
__END__

