<?php

/*
  Whois2.php        PHP classes to conduct whois queries
  
  Copyright (C)1999,2000 easyDNS Technologies Inc. & Mark Jeftovic
  
  Maintained by Mark Jeftovic <markjr@easydns.com>
  
  For the most recent version of this package:
  
  http://www.easydns.com/~markjr/whois2/
  
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

/* denic.whois        0.3 by David Saez <david@ols.es> */    
/* denic.whois        0.2 by Elmar K. Bins <elmi@4ever.de> */
/* based upon brnic.whois by Marcelo Sanches <msanches@sitebox.com.br> */
/* and        atnic.whois by Martin Pircher <martin@pircher.net> */

/* this version does not yet deliver contact data, but handles only */

if(!defined("__DENIC_HANDLER__")) define("__DENIC_HANDLER__",1);

class denic extends Whois {

	function denic($data) {
		$this->result = $this->parse($data);
	}

	function parse ($data_str) {
		$convert = array(
			"domain" => "name",
			"descr" => "address",
			"city" => "address", 
			"pcode" => "address",
			"country" => "address"
			);

		$sections = array(
			"main" => "domain",
			"admin-c" => "admin",
			"tech-c" => "tech",
			"zone-c" => "zone"
			);

		$r = array();
		$newblock = false;
		$hasdata = false;
		$block = array();
		$gkey = "main";
		$rawdata = $data_str["rawdata"];

		while (list($key,$val) = each($rawdata)) {	
			$val = trim($val);
			if (substr($val,0,1) == '%') {
				$disclaimer[] = trim(substr($val,1));
				continue;
			}
			if ($val == "") {
				$newblock = true;
				continue;
			}
			if ($newblock && $hasdata) {
				$blocks[$gkey] = $block;
				$block = array();
			}
			if ($newblock && substr($val,0,1)=="[") {
				$gkey = str_replace("]["," ",$val);
				$gkey = str_replace("]","",$gkey);
				$gkey = str_replace("[","",$gkey);
				$newblock = false;
				continue; 
			}
			$hasdata = true;
			$newblock = false;
			$k = strtolower(trim(strtok($val,":")));
			$v = trim(substr(strstr($val,":"),1));
			
			if (isset($convert[$k])) {
				$k = $convert[$k];
				if ($k == "") continue; 
			}
                    
			if (isset($block[$k]) && is_array($block[$k]))
				$block[$k][] = $v;
			else if (!isset($block[$k]) || $block[$k]=="")
				$block[$k] = $v;
			else {
				$x = $block[$k];
				unset($block[$k]);
				$block[$k][] = $x;
				$block[$k][] = $v;
			}

		}

		if ($hasdata)
			$blocks[$gkey] = $block;

		$r["regrinfo"]["owner"]["organization"]=$blocks["main"]["address"][0];
		unset($blocks["main"]["address"][0]);
		$r["regrinfo"]["owner"]["address"]=$blocks["main"]["address"];
		unset($blocks["main"]["address"]);

		while (list($gkey,$gval) = each($blocks))
                      { 
                        $parts=explode(" ",$gkey);
                        while (list($pkey,$pval) = each($parts))
                              { 
				if (isset($gval["address"][0]))
				   {
				     $gval["organization"]=$gval["address"][0];
				     unset($gval["address"][0]);
				   }

				$r["regrinfo"][$sections[$pval]]=$gval;	
                              }
                      }

		$r["rawdata"] = $data_str["rawdata"];

		$r["regyinfo"] = array( "whois" => "whois.denic.de",
					"registrar" => "DENIC eG",
					"referrer" => "http://www.denic.de/");

		if (isset($disclaimer)) $r["regrinfo"]["disclaimer"]=$disclaimer;
		return($r);
	}
}
