<?php
header
("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0"false);
header("Pragma: no-cache");                          // HTTP/1.0
?>
<form name=form1>
<table><tr><td  align='right'>
SOA Record<input type="radio" name="viewChoice" value="SOA" onClick="window.location='dnsApp.php?viewChoice=View&type=SOA';">
</td><td align='right'>
MX Record<input type="radio" name="viewChoice" value="MX" onClick="window.location='dnsApp.php?viewChoice=View&type=MX';">
</td>
<td align='right'>
CNAME Record<input type="radio" name="viewChoice" value="CNAME" onClick="window.location='dnsApp.php?viewChoice=View&type=CNAME';">
</td></tr>
<tr><td align='right'>
A Record<input type="radio" name="viewChoice" value="A" onClick="window.location='dnsApp.php?viewChoice=View&type=A';">
</td><td align='right'>
NS Record<input type="radio" name="viewChoice" value="NS" onClick="window.location='dnsApp.php?viewChoice=View&type=NS';">
</td></tr>  </table>
</form>

<?php
#  Mark Lin      Nov. 27, 2001       Advertex Communications.  Inc.
#  This php is meant to be used for manage DNS file thru the web.  It uses dns.inc from
#  www.ypass.net.  dns.inc is pretty self-documented, so you should have no problem understanding it.
#  So, no more mangling with flat looking DNS.  All the available IP space would now be put into
#  DNS entry including those not being used and those in DHCP scope.  This way, we can manage our
#  IP space more efficiently.  Those who want to use it on their own environment, simply change the
#  zoneName to theirs, and of course some variable modification to dns.inc.

#  Note to myself: 1. when refering to object in print statement, do NOT use double quote to surround them.
#                     Instead, use string concatenation.
#
#  Feature in Future:   1. To be able to sort record.  That involves sorting 'rr' records of 'zone' object.
#                       2. Allow user to input zoneName. (this should be easy to do, but I just dont want to
#                          put it here because we only use one zone, and dont want the hassle of inputting
#                          zone name every time.  If you are ISP and want your customer to change their own
#                          DNS records, this should be implemented.)
#                       3. Better authentication.  Right now, I only use cheesy .htaccess for authentication.
#                          One might wants to authenticate user against system's passwd, or NIS, or LDAP.
include("dns.inc");

$row1_color "cccc33";
$row2_color "66cccc";
$seenSOA $seenA $seenMX $seenNS $seenPTR $seenNS0#Use to display record header properly
$zoneName "macysnyc.com";


$zone = new ZONE("$zoneName"); #use here to mange only one domain.  Hopefully in the
                                  #near future, we can let user either select it from
                                  #named.conf or input a new zone.

$zone->resetRR();  #set the RR counter to zero

# Set a default view
if($viewChoice =="") {
    
$viewChoice "View";
    
$type="SOA";
    }

/*********************************************/
/*******  Main Functions begin below  ********/
/*********************************************/

/******* ADD FUNCION *******/
if ($viewChoice == "Add")
{
    switch (
$type)
    {
        case(
"NS"):
        print 
"<form name=myForm><table>";
        print 
"<tr><td bgcolor=#$row1_color>Domain Name</td>";
        print 
"<td bgcolor=#$row2_color><input name=Domain></td></tr><br>\n";
        print 
"<tr><td bgcolor=#$row1_color>Preference</td>";
        print 
"<td bgcolor=#$row2_color><input name=Pref></td></tr><br>\n";
        print 
"<tr><td bgcolor=#$row1_color>Hostname</td>";
        print 
"<td bgcolor=#$row2_color><input name=Hostname></td></tr><br></table>\n";
        print 
"<input value=Add type=button onClick=\"";
        print 
"window.location='dnsApp.php?viewChoice=Change&type=AddNS&domain=' + this.form.Domain.value +
              '&pref=' + this.form.Pref.value + '&host=' + this.form.Hostname.value \">"
;
        print 
"</form>";
        break;
        
        case(
"A"):
        print 
"<form name=myForm><table>";
        print 
"<tr><td bgcolor=#$row1_color>Hostname</td>";
        print 
"<td bgcolor=#$row2_color><input name=Hostname></td></tr><br>\n";
        print 
"<tr><td bgcolor=#$row1_color>IP</td>";
        print 
"<td bgcolor=#$row2_color><input name=Ip></td></tr><br></table>\n";

        print 
"<input value=Add type=button onClick=\"";
        print 
"window.location='dnsApp.php?viewChoice=Change&type=AddA&ip=' + this.form.Ip.value +
              '&host=' + this.form.Hostname.value \">"
;

        print 
"</form>";

        break;

    }

}

/******* CHANGE FUNCION *******/
/******* Functions that really commit the change for either 'Edit' or 'Add New Record' *******/
elseif ($viewChoice == "Change")
{
    switch (
$type)
    {
        case(
"A"):
           while (
$rr $zone->getRR())
           {
               
# Remeber not to change the value of $rr but to $zone since
               # that's what really refers to the actual zone file
               
if ( $rr->type=="A" && $rr->rhs->ipAddress == $ip ){
                   
$zone->rr[$zone->curRR-1]->lhs $host;
               }
           }
           
$zone->saveZone();
           print 
"<script> parent.location='dnsApp.php?viewChoice=View&type=A'; </script>";
           break;

        case(
"AddA"):
           while (
$rr $zone->getRR())
           {
               if ( 
$rr->type=="A" && $rr->rhs->ipAddress == $ip ){
                   
$zone->error="Same IP already exist, Please check it";
                   
$quit 1;
                   break;
               }
           }
           if (
$quit) {
               print 
$zone->error;
               return(
0);
           }
           else {
              
$zone->addA($host$ip);
              
$zone->saveZone();
              print 
"<script> parent.location='dnsApp.php?viewChoice=View&type=A'; </script>";
          }


        break;

        case(
"MX"):
           while (
$rr $zone->getRR())
           {
               if ( 
$rr->type == "MX" && $rr->rhs->preference == $pref && $rr->lhs == $domain){
                   
$zone->rr[$zone->curRR-1]->rhs->hostname $host;
               }
           }
           
$zone->saveZone();
           print 
"<script> parent.location='dnsApp.php?viewChoice=View&type=MX'; </script>";
           break;
        
        case(
"SOA"):
           while (
$rr $zone->getRR())
           {
               if ( 
$rr->type == "SOA" ){
                   
$zone->rr[$zone->curRR-1]->rhs->refresh $refresh;
                   
$zone->rr[$zone->curRR-1]->rhs->retry $retry;
                   
$zone->rr[$zone->curRR-1]->rhs->expire $expire;
               }
           }
           
$zone->saveZone();
           print 
"<script> parent.location='dnsApp.php?viewChoice=View&type=SOA'; </script>";

           break;
        
        
# Be careful here, we only have one Name Server.  For those of you who has
        # the luxury of having two, you gotta put more condition here.
        
case("NS"):
           while (
$rr $zone->getRR())
           {
               if ( 
$rr->type == "NS" && $rr->lhs == $domain){
                   
$zone->rr[$zone->curRR-1]->rhs->hostname $host;
               }
           }
           
$zone->saveZone();
           print 
"<script> parent.location='dnsApp.php?viewChoice=View&type=NS'; </script>";
           break;

        case(
"AddNS"):
            print 
"$domain <br>";
            print 
"$host <br>";
            print 
"$pref <br>";
        break;
    }
}

/******* EDIT FUNCION *******/
elseif ($viewChoice == "Edit")
   {
   switch (
$type)
      {
           case 
"A":
                print 
"<table>";
                print 
"<tr><td bgcolor=#$row1_color>IP</td><td bgcolor=#$row2_color>$ip</td></tr>\n";
                print 
"<tr><td bgcolor=#$row1_color>Input New Name</td><td bgcolor=#$row2_color>";
                print 
"<FORM name=form1><input name=Change value=$host></td></tr></table><br>\n";
                print 
"<input name=change value=change type=button onClick=\"";
                print 
"window.location='dnsApp.php?viewChoice=Change&type=A&ip=$ip&host=' + this.form.Change.value\">";
                print 
"</form>";
                print 
"<font color=red>*</font>DO NOT forget the trailing dot if you ARE inputting a fully qualify domain name.<br>";
                break;

           case 
"NS":
                print 
"<table>";
                print 
"<tr><td bgcolor=#$row1_color>Input New Name</td><td bgcolor=#$row2_color>";
                print 
"<FORM name=form1><input name=Change value=$host></td></tr></table><br>\n";
                print 
"<input name=change value=change type=button onClick=\"";
                print 
"window.location='dnsApp.php?viewChoice=Change&type=NS&domain=$domain&host=' + this.form.Change.value\">";
                print 
"</form>";
                print 
"<font color=red>*</font>DO NOT forget the trailing dot if you ARE inputting a fully qualify domain name.<br>";
                break;

           case 
"MX":
                print 
"<table>";
                print 
"<tr><td bgcolor=#$row1_color>Domain</td><td bgcolor=#$row2_color>$domain</td></tr>\n";
                print 
"<tr><td bgcolor=#$row1_color>Preference</td><td bgcolor=#$row2_color>$pref</td></tr>\n";
                print 
"<tr><td bgcolor=#$row1_color>Input New Hostname</td><td bgcolor=#$row2_color>";
                print 
"<FORM name=form1><input name=Change value=$host></td></tr></table><br>\n";
                print 
"<input name=change value=change type=button onClick=\"";
                print 
"window.location='dnsApp.php?viewChoice=Change&type=MX&domain=$domain&pref=$pref&host=' + this.form.Change.value\">";
                print 
"</form>";
                print 
"<font color=red>*</font>DO NOT forget the trailing dot if you ARE inputring a fully qualify domain name.<br>";
                break;

           case 
"SOA":
                echo 
"<table>\n";
                while (
$rr $zone->getRR())
                {
                   if (
$seenSOA == 0) {
                      print 
"<tr><td> SOA RECORD </td></tr>";
                      
$seenSOA 1;
                   }
                   if (
$rr->type == "SOA"){
                   print 
"<tr><td bgcolor=#$row1_color> Domain Name </td>
                        <td bgcolor=#
$row2_color>" $zone->domainName " </td></tr>\n";
                   print 
"<tr><td bgcolor=#$row1_color> Primary Name Server </td>
                        <td bgcolor=#
$row2_color>" $rr->rhs->priNS " </td></tr>\n";
                   print 
"<tr><td bgcolor=#$row1_color> Serial Number </td>
                        <td bgcolor=#
$row2_color>" $rr->rhs->serial " </td></tr>\n";

                   print 
"<tr><td bgcolor=#$row1_color> Refresh Timer </td>
                        <td bgcolor=#
$row2_color>";
                   print 
"<form name=form1><input name=Refresh value=" $rr->rhs->refresh "></td></tr>\n";

                   print 
"<tr><td bgcolor=#$row1_color> Retry Timer </td>
                        <td bgcolor=#
$row2_color>";
                   print 
"<input name=Retry value=" $rr->rhs->retry "></td></tr>\n";
                   print 
"<tr><td bgcolor=#$row1_color> Expire Timer </td>
                        <td bgcolor=#
$row2_color>";
                   print 
"<input name=Expire value=" $rr->rhs->expire "></td></tr>\n</table>";
                   print 
"<input name=change value=change type=button onClick=\"";
                   print 
"window.location='dnsApp.php?viewChoice=Change&type=SOA&refresh=' + this.form.Refresh.value + '&retry=' + this.form.Retry.value + '&expire=' + this.form.Expire.value\">";
                   print 
"</form>";

                   }
                }
                break;
       }
   }

/***** View Function *****/
elseif ($viewChoice == "View")
{
    switch(
$type){
        case(
"NS"):
        echo 
"<table>\n";

        while (
$rr $zone->getRR())
            {
              if (
$seenNS == 0) {
                  print 
"<tr><td> NS RECORD </td>";
                  print 
"<td><input name=Add Value='Add One' type=button onClick=\"
                  window.location='dnsApp.php?viewChoice=Add&type=NS'\"> </td></tr>"
;
                  
$seenNS 1;
                  }
              if(
$rr->type == "NS") {
                  print 
"<tr><td bgcolor=#$row1_color>" $rr->lhs "</td>
                     <td bgcolor=#
$row2_color>
                     <a href=dnsApp.php?viewChoice=Edit&type=NS&domain=" 
$rr->lhs "&host=" $rr->rhs->hostname ">" $rr->rhs->hostname " </a></td></tr>\n";
                 }
            }
             print 
"</table>\n";
        break;
        
        case(
"A"):
             echo 
"<table>\n";

             while (
$rr $zone->getRR())
                {
                 if (
$seenA == 0) {
                    print 
"<tr><td> A RECORD </td>";
                    print 
"<td><input name=Add Value='Add New Entry' type=button onClick=\"
                    window.location='dnsApp.php?viewChoice=Add&type=A'\"> </td></tr>"
;

                    print 
"<tr><td bgcolor=#$row1_color> IP </td>
                          <td bgcolor=#
$row2_color> Hostname </td></tr>";

                    
$seenA 1;
                 }
                if(
$rr->type == "A"){
                print 
"<tr><td bgcolor=#$row1_color>"
                      
$rr->rhs->ipAddress "</td><td bgcolor=#$row2_color>
                       <a href=dnsApp.php?viewChoice=Edit&type=A&ip=" 
$rr->rhs->ipAddress .
                       
"&host=" $rr->lhs ">" $rr->lhs "</a></td></tr>\n";
                 }
             }
             print 
"</table>\n";
        break;

        case(
"CNAME"):
             echo 
"<table>\n";


             while (
$rr $zone->getRR())
                {
                 if (
$seenA == 0) {
                    print 
"<tr><td> A RECORD </td></tr>";
                    print 
"<tr><td bgcolor=#$row1_color> Host </td><td bgcolor=#$row2_color>
                          CNAME To</td></tr>"
;
                    
$seenA 1;
                 }
                if(
$rr->type == "CNAME"){
                print 
"<tr><td bgcolor=#$row1_color$rr->lhs </td><td bgcolor=#$row2_color>
                      <a href=dnsApp.php?viewChoice=Edit&type=CNAME&host=" 
$rr->rhs->hostname ">"
                       
$rr->rhs->hostname " </a></td></tr>\n";
                 }
             }
        break;

        case(
"MX"):
           echo 
"<table>\n";
           while (
$rr $zone->getRR())
              {

              if (
$seenMX == 0) {
                  print 
"<tr><td> MX RECORD </td></tr>";
                  print 
"<tr><td bgcolor=#$row1_color> Domain Name </td>
                         <td bgcolor=#
$row1_color> MX Preference </td>
                         <td bgcolor=#
$row2_color> Mail Server Name </td> </tr>";
                  
$seenMX 1;
              }
              if(
$rr->type == "MX") {
              print 
"<tr><td bgcolor=#$row1_color>" $rr->lhs " </td>\n";
              print 
"<td bgcolor=#$row1_color>" $rr->rhs->preference " </td>\n";
              print 
"<td bgcolor=#$row2_color>
                     <a href=dnsApp.php?viewChoice=Edit&type=MX&domain=" 
$rr->lhs "&pref=" $rr->rhs->preference .
                     
"&host=" $rr->rhs->hostname ">" $rr->rhs->hostname " </a></td></tr>\n";
              }
             }
             print 
"</table>\n";
        break;
        
        case(
"SOA"):
           echo 
"<table>\n";
           while (
$rr $zone->getRR())
           {
               if (
$seenSOA == 0) {
                   print 
"<tr><td> SOA RECORD </td></tr>";
                   
$seenSOA 1;
               }
               if (
$rr->type == "SOA"){
               print 
"<tr><td bgcolor=#$row1_color> Domain Name </td>
                     <td bgcolor=#
$row2_color>" $zone->domainName " </td></tr>\n";
               print 
"<tr><td bgcolor=#$row1_color> Primary Name Server </td>
                     <td bgcolor=#
$row2_color>" $rr->rhs->priNS " </td></tr>\n";
               print 
"<tr><td bgcolor=#$row1_color> Serial Number </td>
                     <td bgcolor=#
$row2_color>" $rr->rhs->serial " </td></tr>\n";
               print 
"<tr><td bgcolor=#$row1_color> Refresh Timer </td>
                     <td bgcolor=#
$row2_color>
                     <a href=dnsApp.php?viewChoice=Edit&type=SOA>" 
$rr->rhs->refresh "</a> </td></tr>\n";
               print 
"<tr><td bgcolor=#$row1_color> Retry Timer </td>
                     <td bgcolor=#
$row2_color>
                     <a href=dnsApp.php?viewChoice=Edit&type=SOA>" 
$rr->rhs->retry "</a> </td></tr>\n";
               print 
"<tr><td bgcolor=#$row1_color> Expire Timer </td>
                     <td bgcolor=#
$row2_color>
                     <a href=dnsApp.php?viewChoice=Edit&type=SOA>" 
$rr->rhs->expire "</a> </td></tr>\n";
               }
           }
             print 
"</table>\n";
        break;
    }
}
        
print 
$zone->error;
?>