<?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 get_blocks ( $rawdata, $items )
{

$endtag="";

while (list($key,$val)=each($rawdata))
      {
        $val=trim($val);
	if ($val=="") continue;

	$found=false;
	reset($items);

	while (list($field, $match)=each($items)) {

		$pos=strpos($val,$match);

		if ($field!="" && $pos!==false) {
			$last=substr(trim($val),-1,1);

			if ($last==":" || $last=="-" || $last=="]") {
				$found=true;
				$endtag=$last;
				$line=$val;
				break;
			}
			else {
				$var=getvarname(strtok($field,"#"));
				$itm=trim(substr($val,$pos+strlen($match)));
				eval($var."=\$itm;");
			}
		}
	}

	if (!$found) continue;

	$block=array();
	$found=false;
	$spaces=0;

	while (list($key,$val)=each($rawdata))
              { 
		$val=trim($val);
		if ($val=="") { 
                	if ($found && ++$spaces==2) break;	
		     	continue;
                }
                if (!$found) {
			$found=true;
			$block[]=$val;
			continue;
		}
		$last=substr(trim($val),-1,1);
		if ($last==$endtag) {
			prev($rawdata);
			break;
		}
		if ($spaces>0) {
			reset($items);
			$ok=true;
			while (list($field, $match)=each($items)) {
				$pos=strpos($val,$match);
				if ($pos!==false) $ok=false;
			}
			if (!$ok) {
				prev($rawdata);
				break;
			}
		}
		$block[]=$val;
              }

	reset($items);

	while (list($field, $match)=each($items)) {
                $pos=strpos($line,$match);
                if ($pos!==false) {
        		$var=getvarname($field);
        		eval($var."=\$block;");
		}
	}
      }

return $r;
}

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

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

return $var;
}

function get_contact ( $array )
{

$items = array (
		"phone:" => "phone",
		"fax..:" => "fax",
		"fax-" => "fax",
		"fax:"   => "fax",
		"[fax]" => "fax",
		"email:" => "email"
               );

while (list($key,$val)=each($array))
      {
	reset($items);

	while (list($match,$field)=each($items))
	      {
		$pos=strpos(strtolower($val),$match);
		if ($pos===false) continue;
		$itm=trim(substr($val,$pos+strlen($match)));
		$r[$field]=$itm;
		$val=trim(substr($val,0,-strlen($itm)-1-strlen($match)));
		if ($val=="")
                     unset($array[$key]);
	        else $array[$key]=$val;
                break;
              } 	

	if ($val=="") continue;

	if (!preg_match("/[^0-9\(\)\-\.\+ ]/", $val))
           {
	     if (isset($r["phone"]))
	          $r["fax"]=$val;	
	     else $r["phone"]=$val;
	     unset($array[$key]);
	     continue;
	   }
	if (strstr($val,"@"))
           {
	     $parts=explode(" ",$val);
             $top=count($parts)-1;
             $r["email"]=$parts[$top];
             array_pop($parts);
             $val=implode(" ",$parts);
	     if ($val=="") {
		unset($array[$key]);
		continue;
	     }
             $r["name"]=$val;
             unset($array[$key]);
	     if ($key==1)
                {
		  $r["organization"]=$array[0];
		  unset($array[0]);
                }
           }
      }     

if (!isset($r["name"]))
   {
     $r["name"]=$array[0];
     unset($array[0]);
   }

if (!empty($array)) $r["address"]=$array;

return $r;
}
?>
