<?php

function print_header() {
   print( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" );
   print( "<html>\n" );
   print( "\t<head>\n" );
   print( "\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n" );
   print( "\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"css/style.css\" title=\"default\">\n" );
   print( "\t\t<title>DNSLIST</title>\n" );
?>
<!-- Expandable/Collapsable cell script grabbed from http://proft.50megs.com/experience.html#top -->
<!-- No point re-inventing the wheel here.... -->
<script type="text/javascript">

/********************************************************************************
* Switch Content script- (C) Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use. Last updated Oct 21st, 2003.
* Visit http://www.dynamicdrive.com/ for full source code
*********************************************************************************/

var enablepersist="on"      //  Enable saving state of content structure using session cookies? (on/off)
var collapseprevious="yes"  //  Collapse previously open content when opening present? (yes/no)

if (document.getElementById) {
   document.write('<style type="text/css">')
   document.write('.switchcontent{display:none;}')
   document.write('</style>')
}

function getElementbyClass(classname) {
   ccollect=new Array()
   var inc=0
   var alltags=document.all? document.all : document.getElementsByTagName("*")
   for (i=0; i<alltags.length; i++) {
      if (alltags[i].className==classname)
         ccollect[inc++]=alltags[i]
   }
}

function contractcontent(omit) {
   var inc=0
   while (ccollect[inc]) {
      if (ccollect[inc].id!=omit)
         ccollect[inc].style.display="none"
      inc++
   }
}

function expandcontent(cid) {
   if (typeof ccollect!="undefined") {
      if (collapseprevious=="yes")
         contractcontent(cid)
      document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"
   }
}

function revivecontent() {
   contractcontent("omitnothing")
   selectedItem=getselectedItem()
   selectedComponents=selectedItem.split("|")
   for (i=0; i<selectedComponents.length-1; i++)
      document.getElementById(selectedComponents[i]).style.display="block"
}

function get_cookie(Name) {
   var search = Name + "="
   var returnvalue = "";
   if (document.cookie.length > 0) {
      offset = document.cookie.indexOf(search)
      if (offset != -1) {
         offset += search.length
         end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
         returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
   return returnvalue;
}

function getselectedItem() {
   if (get_cookie(window.location.pathname) != "") {
      selectedItem=get_cookie(window.location.pathname)
      return selectedItem
   }
   else
      return ""
}

function saveswitchstate() {
   var inc=0, selectedItem=""
   while (ccollect[inc]) {
      if (ccollect[inc].style.display=="block")
         selectedItem+=ccollect[inc].id+"|"
      inc++
   }
   document.cookie=window.location.pathname+"="+selectedItem
}

function do_onload() {
   getElementbyClass("switchcontent")
   if (enablepersist=="on" && typeof ccollect!="undefined")
      revivecontent()
}

if (window.addEventListener)
   window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
   window.attachEvent("onload", do_onload)
else if (document.getElementById)
   window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
   window.onunload=saveswitchstate
</script>

<?php
   print( "\t</head>\n" );
   print( "\t<body>\n" );
?>
<a name="top"></a>
<?php
   print( "\t\t<div class=\"foo\">\n" );
   print( "\t\t\t<center><h1>DNS List</h1></center>\n" );
   print( "\t\t\t<br><br>\n" );
}

function print_title( $title, $colour ) {
   print_nav();
   print( "\t\t\t<center><b style=\"color:$colour;\">$title</b></center>\n" );
}

function print_nav() {
   print( "\t\t\t<!-- START NAV BAR -->\n" );
   print( "\t\t\t\t<center>\n" );
   print( "\t\t\t\t\tZone <b>test.zone</b>: \n" );
   print( "\t\t\t\t\t[ <a href=\"dnslist.php?type=zone&zonefile=kw.zone\">A Record List</a> ] \n" );
   print( "\t\t\t\t\t[ <a href=\"dnslist.php?type=cname&zonefile=kw.zone\">CNAME Record List</a> ] \n" );
   print( "\t\t\t\t</center>\n" );
   print( "\t\t\t\t<br><br>\n" );
   print( "\t\t\t<!-- END NAV BAR -->\n" );
}

function print_footer() {
   print( "\t\t</div>\n" );
   print( "\t</body>\n" );
   print( "</html>\n" );
}

function print_error( $ecode, $estring ) {
   print( "\t\t\t<p>\n" );
   print( "\t\t\t<b>Error details:</b><br>\n" );
   print( "\t\t\tECODE: $etype<br>\n" );
   print( "\t\t\tEDATA: $estring<br>\n" );
   print( "\t\t\t</p>\n" );
}

/* Very IMPORTANT:
 * My zone files do not use in-line (i.e. record-specific) TTL values, therefore
 * each line can have either three or four fields, e.g.
 * hostname   IN   A    ip.address.goes.here   ; maybe a comment
 * hostname        A    ip.address.goes.here   ; maybe a comment
 */
function print_a_list( $zonefile ) {
   $zonefile = "zonefile/$zonefile";
   if ( ! file_exists( "$zonefile" ) ) {
      print( "<br><center>\n" );
      print( "<b><font color=\"red\">ERROR:</font> $zonefile does not exist!</b>\n" );
      print( "</div></body></html>\n" );
      die();
   }
   $fh = fopen( "$zonefile", "r" );
   $count = 0;
   print( "\t\t\t<p>\n" );
   print( "\t\t\t<center>\n" );
   print( "\t\t\t\t<table width=\"650\" cellspacing=\"2\">\n" );
   print( "\t\t\t\t\t<tr bgcolor=\"teal\">\n" );
   print( "\t\t\t\t\t\t<td class=\"tableheader\" width=\"400\">\n" );
   print( "\t\t\t\t\t\t\tHostname\n" );
   print( "\t\t\t\t\t\t</td>\n" );
   print( "\t\t\t\t\t\t<td class=\"tableheader\">\n" );
   print( "\t\t\t\t\t\t\tIP Address\n" );
   print( "\t\t\t\t\t\t</td>\n" );
   print( "\t\t\t\t\t</tr>\n" );
   while ( ! feof( $fh ) ) {
      $line = fgets( $fh, 1024 );
      if ( ereg( "^[a-z0-9A-Z.-]+[      ]+(IN)*[        ]+A[    ]+192\.168\.[0-9]+\.[0-9]+.*$", $line ) ) {
         $count++;
         if ( $count % 2 ) {
            $colour="#CCCCCC";
         } else {
            $colour="#BBBBBB";
         }
         if ( ereg( "^[a-z0-9A-Z.-]+[   ]+IN[   ]+A[    ]+192\.168\.[0-9]+\.[0-9]+.*$", $line ) ) {
            list( $hostname, $in, $rectype, $ip ) = split( "[   ]+", $line );
            $ip=chop($ip);
            $this_href=$hostname;
            print( "\t\t\t\t\t<tr bgcolor=\"$colour\">\n" );
            print( "\t\t\t\t\t\t<td><a onClick=\"expandcontent('$this_href')\" style=\"cursor:hand; cursor:pointer\" class=\"exp\">$hostname</a>\n" );
            print( "\t\t\t\t\t\t<div id=\"$this_href\" class=\"switchcontent\">\n" );
            print( "\t\t\t\t\t\t\t<p>\n" );
            /* Open a second filehandle on the zone file so we can search for CNAME records */
            $cname_count = 0;
            $fh2 = fopen( "$zonefile", "r" );
            print( "\t\t\t\t\t\t\t\t<center><table width=\"350\" cellspacing=\"1\">\n" );
            while ( ! feof( $fh2 ) ) {
               $line2 = fgets( $fh2, 1024 );
               if ( ereg( "^[a-z0-9A-Z.-]+[     ]+(IN)*[        ]+CNAME[        ]+$hostname.*$", $line2 ) ) {
                  $cname_count++;
                  list( $cname, $cin, $crectype, $cpointer ) = split( "[        ]+", $line2 );
                  print( "\t\t\t\t\t\t\t\t\t<tr bgcolor=\"#DDDDDD\">\n" );
                  print( "\t\t\t\t\t\t\t\t\t<td width=\"100\">\n" );
                  print( "\t\t\t\t\t\t\t\t\t\tCNAME\n" );
                  print( "\t\t\t\t\t\t\t\t\t</td>\n" );
                  print( "\t\t\t\t\t\t\t\t\t<td>\n" );
                  print( "\t\t\t\t\t\t\t\t\t\t$cname\n" );
                  print( "\t\t\t\t\t\t\t\t\t</td>\n" );
                  print( "\t\t\t\t\t\t\t\t\t</tr>\n" );
               }
            }
            print( "\t\t\t\t\t\t\t\t</table></center>\n" );
            fclose( $fh2 );
            if ( $cname_count == 0 ) {
               print( "\t\t\t\t\t\t\t\tNo CNAME records for $hostname\n<br>" );
            }
            print( "\t\t\t\t\t\t\t</p>\n" );
            print( "\t\t\t\t\t\t</div>\n" );
            print( "\t\t\t\t\t\t</td>\n" );
            print( "\t\t\t\t\t\t<td>$ip</td>\n" );
            print( "\t\t\t\t\t</tr>\n" );
         } elseif ( ereg( "^[a-z0-9A-Z.-]+[     ]+A[    ]+192\.168\.[0-9]+\.[0-9]+.*$", $line ) ) {
            list( $hostname, $rectype, $ip ) = split( "[        ]+", $line );
            $this_href=$hostname;
            $ip=chop($ip);
            print( "\t\t\t\t\t<tr bgcolor=\"$colour\">\n" );
            print( "\t\t\t\t\t\t<td><a onClick=\"expandcontent('$this_href')\" style=\"cursor:hand; cursor:pointer\" class=\"exp\">$hostname</a>\n" );
            print( "\t\t\t\t\t\t<div id=\"$this_href\" class=\"switchcontent\">\n" );
            print( "\t\t\t\t\t\t\t<p>\n" );
            /* Open a second filehandle on the zone file so we can search for CNAME records */
            $cname_count = 0;
            $fh2 = fopen( "$zonefile", "r" );
            print( "\t\t\t\t\t\t\t\t<center><table width=\"350\" cellspacing=\"1\">\n" );
            while ( ! feof( $fh2 ) ) {
               $line2 = fgets( $fh2, 1024 );
               if ( ereg( "^[a-z0-9A-Z.-]+[     ]+(IN)*[        ]+CNAME[        ]+$hostname.*$", $line2 ) ) {
                  $cname_count++;
                  list( $cname, $crectype, $cpointer ) = split( "[      ]+", $line2 );
                  print( "\t\t\t\t\t\t\t\t\t<tr bgcolor=\"#DDDDDD\">\n" );
                  print( "\t\t\t\t\t\t\t\t\t<td width=\"100\">\n" );
                  print( "\t\t\t\t\t\t\t\t\t\tCNAME\n" );
                  print( "\t\t\t\t\t\t\t\t\t</td>\n" );
                  print( "\t\t\t\t\t\t\t\t\t<td>\n" );
                  print( "\t\t\t\t\t\t\t\t\t\t$cname\n" );
                  print( "\t\t\t\t\t\t\t\t\t</td>\n" );
                  print( "\t\t\t\t\t\t\t\t\t</tr>\n" );
               }
            }
            print( "\t\t\t\t\t\t\t\t</table></center>\n" );
            fclose( $fh2 );
            if ( $cname_count == 0 ) {
               print( "\t\t\t\t\t\t\t\tNo CNAME records for $hostname\n<br>" );
            }
            print( "\t\t\t\t\t\t\t</p>\n" );
            print( "\t\t\t\t\t\t</div>\n" );
            print( "\t\t\t\t\t\t</td>\n" );
            print( "\t\t\t\t\t\t<td>$ip</td>\n" );
            print( "\t\t\t\t\t</tr>\n" );
         } else {
            /* Due to the main ereg, this should never be reached */
            print( "\t\t\t\t\t<tr bgcolor=\"$colour\">\n" );
            print( "\t\t\t\t\t\t<td colspan=\"2\"><b>Unknown A-record format found: $line</td></b>\n" );
            print( "\t\t\t\t\t</tr>\n" );
         }
      }
   }
   fclose( $fh );
   print( "\t\t\t\t</table>\n" );
   print( "\t\t\t</center>\n" );
   print( "\t\t\t</p>\n" );
}

function print_cname_list( $zonefile ) {
   $zonefile = "zonefile/$zonefile";
   if ( ! file_exists( "$zonefile" ) ) {
      print( "<br><center>\n" );
      print( "<b><font color=\"red\">ERROR:</font> $zonefile does not exist!</b>\n" );
      print( "</div></body></html>\n" );
      die();
   }
   $fh = fopen( "$zonefile", "r" );
   print( "\t\t\t<p>\n" );
   print( "\t\t\t<center>\n" );
   print( "\t\t\t\t<table width=\"650\" cellspacing=\"2\">\n" );
   print( "\t\t\t\t\t<tr bgcolor=\"teal\">\n" );
   print( "\t\t\t\t\t\t<td class=\"tableheader\" width=\"400\">\n" );
   print( "\t\t\t\t\t\t\tCNAME\n" );
   print( "\t\t\t\t\t\t</td>\n" );
   print( "\t\t\t\t\t\t<td class=\"tableheader\">\n" );
   print( "\t\t\t\t\t\t\tHostname\n" );
   print( "\t\t\t\t\t\t</td>\n" );
   print( "\t\t\t\t\t</tr>\n" );
   while ( ! feof( $fh ) ) {
      $line = fgets( $fh, 1024 );
      if ( ereg( "^[a-z0-9A-Z.-]+[      ]+(IN)*[        ]+CNAME[        ]+.*$", $line ) ) {
         if ( ereg( "^[a-z0-9A-Z.-]+[   ]+IN[   ]+CNAME[        ]+[a-z0-9A-Z.-]+.*$", $line ) ) {
            list( $cname, $in, $rectype, $hostname ) = split( "[        ]+", $line );
            $cnames[ $cname ] = $hostname;
         } elseif ( ereg( "^[a-z0-9A-Z.-]+[     ]+CNAME[        ]+[a-z0-9A-Z.-]+.*$", $line ) ) {
            list( $cname, $rectype, $hostname ) = split( "[     ]+", $line );
            $cnames[ $cname ] = $hostname;
         } else {
            /* ignore it... */
         }
      }
   }
   fclose( $fh );
   ksort( $cnames );
   $count = 0;
   foreach( $cnames as $key => $value ) {
      $count++;
      if ( $count % 2 ) {
         $colour="#CCCCCC";
      } else {
         $colour="#BBBBBB";
      }
      print( "\t\t\t\t\t<tr bgcolor=\"$colour\">\n" );
      print( "\t\t\t\t\t\t<td>\n" );
      print( "\t\t\t\t\t\t\t$key\n" );
      print( "\t\t\t\t\t\t</td>\n" );
      print( "\t\t\t\t\t\t<td>\n" );
      print( "\t\t\t\t\t\t\t$value\n" );
      print( "\t\t\t\t\t\t</td>\n" );
      print( "\t\t\t\t\t</tr>\n" );
   }
}

/* START CONTENT GENERATION*/
print_header();
$type = $HTTP_GET_VARS[ "type" ];
if ( ! isset( $type ) || $type == "" ) {
   # Printing a list of A records is default behaviour
   print_title( "A Record List for .kw domain", "#000000" );
   $zonefile = "kw.zone";
   print_a_list( $zonefile );
} else {
   switch ( $type ) {
      case "dnslist":   $zonefile( "kw.zone" );
                        print_title( "A Record List for $zonefile", "#000000" );
                        print_a_list( $zonefile );
                        break;
      case "zone":      if ( $HTTP_GET_VARS[ "zonefile" ] == "" ) {
                           $zonefile = "kw.zone";
                        } else {
                           $zonefile = $HTTP_GET_VARS[ "zonefile" ];
                        }
                        print_title( "A Record List for $zonefile", "#000000" );
                        print_a_list( $zonefile );
                        break;
      case "cname":     if ( $HTTP_GET_VARS[ "zonefile" ] == "" ) {
                           $zonefile = "kw.zone";
                        } else {
                           $zonefile = $HTTP_GET_VARS[ "zonefile" ];
                        }
                        print_title( "CNAME Record List for $zonefile", "#000000" );
                        print_cname_list( $zonefile );
                        break;
      default:          print_title( "An Error Has Occurred", "#FF0000" );
                        print_error( "ETYPE", "$type not a valid type" );
   }
}
print_footer();
/* END CONTENT GENERATION*/

?>