View Single Post
Old 01-14-2008, 01:29 AM   · #1
Mikor
Resident Linux Geek
 
Mikor's Avatar
 
Name: Michael Walker
Location: East Yorkshire, England
Trader Rating: (7)
Join Date: Aug 2005
Posts: 2,413
NP$: 300.25 (Donate)
Mikor is a splendid one to beholdMikor is a splendid one to beholdMikor is a splendid one to beholdMikor is a splendid one to beholdMikor is a splendid one to beholdMikor is a splendid one to behold
A whois class (sort of)

This isn't actually a whois class, it's a class that selects a whois server for a domain. If multiple whois servers are available, it chooses a random one.
The script uses this XML file to get the servers (human-readable version here).
As per their request, by default the class keeps a local cache of the XML file and updates once a day. To enable caching, make the directory the cache will be in writable. To disable caching change
PHP Code:
var $use_cache = true;

To
PHP Code:
var $use_cache = false;


Here is the class
PHP Code:
class whoisserver{

    var
$url = "http://serverlist.domaininformation.de/";
    var
$cache = "whois.xml";
    var
$use_cache = true;

    function
parsexml($file){
        
$parser = xml_parser_create('');
        
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
        
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
        
xml_parse_into_struct($parser, file_get_contents($file), $tags);
        
xml_parser_free($parser);
    
        
$elements = array();
        
$stack = array();
        foreach(
$tags as $tag){
            
$index = count($elements);
            if(
$tag['type'] == "complete" || $tag['type'] == "open"){
                
$elements[$index] = array();
                
$elements[$index]['name'] = $tag['tag'];
                
$elements[$index]['attributes'] = empty($tag['attributes']) ? "" : $tag['attributes'];
                
$elements[$index]['content']    = empty($tag['value']) ? "" : $tag['value'];
            
                if(
$tag['type'] == "open"){    # push
                    
$elements[$index]['children'] = array();
                    
$stack[count($stack)] = &$elements;
                    
$elements = &$elements[$index]['children'];
                }
            }
        
            if(
$tag['type'] == "close"){    # pop
                
$elements = &$stack[count($stack) - 1];
                unset(
$stack[count($stack) - 1]);
            }
        }
        return
$elements[0];
    }

    function
cachecheck(){
        if(!
file_exists($this->cache) || filemtime($this->cache) < time() - 86400 && $this->use_cache == true){
            
$xml = file_get_contents($this->url);
            
            
unlink($this->cache);
            
            
$fp = fopen($this->cache, "w+");
            
fwrite($fp, $xml);
            
fclose($fp);
            
            return
true;
        }else{
            return
false;
        }
    }

    function
getxml(){
        
$file = ($this->use_cache == true) ? $this->cache : $this->url;
        
$xml = $this->parsexml($file);
        
        return
$xml;
    }
    
    function
getservers(){
        
$xml = $this->getxml();
        
$servers = array();

        foreach(
$xml['children'] as $child){
            if(!empty(
$child['children'])){
                foreach(
$child['children'] as $baby){
                    if(
$baby['name'] == 'domain'){
                        if(empty(
$servers["{$baby['attributes']['name']}"])){
                            
$servers["{$baby['attributes']['name']}"] = array("{$child['attributes']['host']}");
                        }else{
                            
$servers["{$baby['attributes']['name']}"][] = "{$child['attributes']['host']}";
                        }
                    }
                }
            }
        }
        
        return
$servers;
    }
    
    function
pickserver($tld){
        
$servers = $this->getservers();
        
        if(!empty(
$servers["{$tld}"])){
            
$server = $servers["{$tld}"];
            if(
count($server) != 1){
                return
$server[array_rand($server)];
            }else{
                return
$server[0];
            }
        }else{
            return
false;
        }
    }
    
    function
go($domain){
        
$this->cachecheck();
        
        
$domain = str_replace('www.', '', $domain);
        
$domain = strrev($domain);
        
$domain = explode('.', $domain);
        
        if(
count($domain) == 3){
            
$tld = strrev($domain[1]) . '.' . strrev($domain[0]);
        }else{
            
$tld = strrev($domain[0]);
        }
        
        
$server = $this->pickserver($tld);
        return
$server;
    }
}


I wrote the whole class except the parsexml() function which I found on php.net. Do what you want with the code.

Here are two examples:
PHP Code:
$ws = new whoisserver;
echo
$ws->go("http://www.yarrt.com")."\n"; // should return "whois.crsnic.net"
echo $ws->go("http://hello.uyp")."\n"; // should return false


Please register or log-in into NamePros to hide ads
__________________
Me | Blog | Last.fm | F@h

archlinux User
Mikor is offline   Reply With Quote
Site Sponsors
Custom Logo Design Custom Logo Design Get Me Visits
Advertise your business at NamePros
All times are GMT -7. The time now is 03:38 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.