<?php
/*
  Generic3.php    PHP functions for parsing whois output

  Copyright (C)2003

  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.
*/

function generic_whois ($rawdata,$translate,$contacts,$main="domain")
{
$r=array();
$newblock=false;
$hasdata=false;
$block=array();
$gkey="main";
$dend=false;

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

	if ($v=="") continue;

        $hasdata=true;

	if (isset($translate[$k])) 
           {
             $k=$translate[$k];
	     if (strstr($k,"."))
                {
                  eval("\$block".getvarname($k)."=\$v;");
                  continue;
                }
           }
	else $k=strtolower($k);

	if ($k=="handle") $gkey=$v;

	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;

if (isset($disclaimer) && is_array($disclaimer)) 
    $ret["disclaimer"]=$disclaimer;

if (!isset($blocks) || !is_array($blocks["main"]))
   { $ret["registered"]="no";
     return $ret;
   }

$r=$blocks["main"];

$ret["registered"]="yes";

while (list($key,$val)=each($contacts))
       if (isset($r[$key]))
	 {
	   if (is_array($r[$key]))
	        $blk=$r[$key][count($r[$key])-1];
	   else $blk=$r[$key];

	   $ret[$val]=$blocks[$blk];
	   unset($r[$key]); 
         }

$ret[$main]=$r;
return $ret;
}

function getvarname ( $vdef )
{
$parts=explode(".",$vdef);
$var="";

while (list($fn,$mn)=each($parts))
       if ($mn=="")
            $var=$var."[]";
       else $var=$var."[\"".$mn."\"]";

return $var;
}

?>
