<?php

/*
    Copyright 2009 Michael Sumner
    Email: msumner@dnmedia.com
    Web: http://www.DNMedia.com
    
    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 3 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, see <http://www.gnu.org/licenses/>.
*/

if(!isset($_POST['Submit']))
{
    echo 
'<form method="POST" action="' $_SERVER['PHP_SELF'] . '">'
            
'<textarea name="domains" rows="30" cols="30"></textarea><br />'
            
'<input type="submit" name="Submit" value="Submit">'
            
'</form>';
}
else
{
    
$domains explode("\n"str_replace(array("\r\n""\r"), "\n"$_POST['domains']));
    
    foreach(
$domains as $domain)
    {
        
$whois = new Whois($domain);
        
        echo 
$domain ",";
        
        if(!(
$email $whois->get_email()))
        {
            echo 
"FAILED<br />";
        }
        else
        {
            echo 
$email "<br />";
        }
    }
}

class 
Whois
{
    var 
$domain;
    var 
$ext;
    var 
$whoisServer;
    var 
$whoisSource;
    var 
$email;
    
    function 
Whois($domain)
    {
        
$this->domain $domain;
        
$pieces explode('.'$this->domain);
        
$this->ext $pieces[1];
    }
    
    function 
get_email()
    {
        
$matches = array();
        
$pattern "[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})";
        
        
$this->get_whois();
        
        if(
eregi($pattern$this->whoisSource$matches) != 0)
        {
            
$this->email $matches[0];
            return 
$this->email;
        }
        else
        {
            return 
FALSE;
        }
    }
    
    function 
get_server()
    {
        
$check $this->ext ".whois-servers.net";
        
$result $this->get_response($check);
        
        if(
$this->ext != "org")
        {
            if(
$result strstr($result"Whois Server:"))
            {
                
$pieces explode(' '$result);
                
$this->whoisServer rtrim($pieces[2]);
                return 
TRUE;
            }
            else
            {
                return 
FALSE;
            }
        }else
        {
            
$this->whoisServer NULL;
            
$this->whoisSource $result;
            return 
TRUE;
        }
    }
    
    function 
get_whois()
    {
        
$this->get_server();
        
        if(
$this->whoisServer != NULL)
        {
            
$this->whoisSource $this->get_response($this->whoisServer);
        }
    }
    
    function 
get_response($server)
    {
        
$result "";
        
        
$fp = @fsockopen($server43$errno$errstr10);
        
        if(
$fp === FALSE)
        {
            
$fp = @fsockopen($server43$errno$errstr5);
        }
        
        if(!
$fp)
        {
            return 
FALSE;
        }
        else
        {
            
fputs($fp$this->domain "\r\n");
            
            while(!
feof($fp))
            {
                
$result .= fgets($fp128);
            }
            
            
fclose($fp);
            return 
$result;
        }
    }
}

?>