<?php 
/** 
 * Here is the ianacclister class DocBlock 
 * @package  IANATools 
 * @author   Kevin Hagel <[email protected]> 
 * @version  $Revision: 2.0 $ 
 * @since    January 23, 2002 Rewritten:  April 27, 2004 
 * 
 * Goes to the iana country code listing page, reads in all the data and builds 
 * a hash keyed on the two-letter country code. 
 * 
 * This class was originally designed to output an actual html table, but that's not 
 * all that MVC is it?  Now it just loads the information from IANA and builds a 
 * country-code keyed hash.  You decide yourself what to do with it. 
 * 
 * Modification History: 
 *  Apr 30, 2004 - Added 'code' => $code to the hash, some PHP functions don't like 
 *    or aren't as easy to use when keyed by the code, I decided to include it in 
 *    the record for each ccode to allow foreach($has as $cc=>$data).. $code=$data['code'] 
 * 
 * Example Usages: 
 * 
 * include_once ('$wherever/class.ianacclister.php'); 
 * $lister = new ianacclister(); 
 * $hash =& $lister->getHash(); 
 * 
 * $hash =& ianacclister::getHash(); 
 */ 
require_once 'PEAR.php'; 
if (!defined(IANACCLISTER_CONSTANTS)) 
{ 
    define("IANACCLISTER_CONSTANTS",    true); 
    // Location of source 
    define("IANACCLISTER_URL",          "http://www.iana.org/cctld/cctld-whois.htm"); 
    // Used to fix whois links 
    define("IANACCLISTER_IANA_HOME",    "http://www.iana.org/"); 
 
    // Errors 
    define("IANACCLISTER_ERROR",            -1); 
    define("IANACCLISTER_ERROR_URLFAILED",  -10); 
} 
class ianacclister extends PEAR 
{ 
    /** 
     * @access  private 
     * @var containing generated output 
     */ 
    var $_hash = NULL; 
    var $error = NULL; 
 
    /** 
     * Constructor 
     * @param  bool causes me to build the has on construction. 
     *    defaults to false; 
     */ 
    function ianacclister($autoload = FALSE) 
    { 
        $this->PEAR(); 
        if($autoload) $this->_buildHash(); 
    } 
 
    /** 
     * builds the country codes hash, stores it in the class variable. 
     * @access  private 
     */ 
    function _buildHash() 
    { 
 
        $this->_hash = array(); 
        $fix = IANACCLISTER_IANA_HOME."root-whois"; 
        $page = join('',file(IANACCLISTER_URL)); 
        if(!$page) { 
            $this->error = PEAR::raiseError("Unable To Access " . IANACCLISTER_URL); 
            return; 
        } 
 
        $search = array( 
            "'[\n|\r|\n\r|\r\n]'",       // dump all newlines NOW 
            "'.*<BLOCKQUOTE>'i",         // everything before <BLOCKQUOTE> inclusive 
            "'<\/BLOCKQUOTE>.*'i",       // everything after </BLOCKQUOTE> inclusive 
            "'\s+'",                     // mult spaces 
            "'&(nbsp|#160);'i",          // &#nbsp; spaces to one actual space 
            "'\s+&(nbsp|#150);\s+'i",    // code for '-' to actual '-', dump lead/trail space 
            "'<BR>'i",                   // Dump them all 
            "'<A NAME=\".\"><\/A>'i",    // internal links we don't want 
            "'(<FONT FACE=\"Arial\">|<\/FONT>)'i",  // dump the font stuff 
            "'(<P>|</P>)'i",                    // should just be one 
            "'\/root-whois'i",           // fix to full URL 
            "'<A HREF=\"(.*)\">'i",      // extract the actual link 
            "'\.(.{2})-'i",              // extract country code itself 
            "'<\/A>'i",                  // more garbage 
        ); 
 
        $replace= array( 
            "",         // newlines 
            "",         // before <BLOCKQUOTE> 
            "",         // after </BLOCKQUOTE> 
            " ",        // mult spaces to one space 
            " ",        // &#nbsp to space 
            "-",        // " – " to "-" 
            "\n",       // <BR> 
            "",         // name = 
            "",         // font stuff 
            "",         // <p> 
            $fix,       // from /root-whois/etc.htm to full url 
            "\\1\n",    // just keep the link part 
            "\\1\n",    // just keep the country code 
            "%",        // country name followed by extra newline and '%' for splitting 
        ); 
        $page = preg_replace($search,$replace,$page); 
 
        // 
        // Ok, at this point the individual records are all stashed in the string, separated 
        // by a '%' character, which we're going to split on. 
        // 
 
        $prerecords = explode("%",$page); 
        for($i=0;$i<count($prerecords);$i++) { 
            $record = explode("\n",trim($prerecords[$i])); 
            if(empty($record)) 
                continue; 
            $whoisurl = trim($record[0]); 
            $ccode = trim($record[1]); 
            $name  = trim($record[2]); 
            if(!empty($whoisurl)&&!empty($ccode)&&!empty($name)) 
              $this->_hash[$ccode] = array("code" => $ccode, "name"=>$name, "whois"=>$whoisurl); 
        } 
        return $this->_hash; 
    } 
 
 
    /** 
     * Returns the hash, auto-creating it if 
     * not currently set. 
     */ 
    function &getHash() 
    { 
        // are we being used as ianacclister::getHash() ? 
        if (!isset($this) or get_class($this) != 'ianacclister') { 
            $this = & new ianacclister(TRUE); 
            return $this->_hash; 
        } 
        // check to see if hash has been build. 
        if($this->_hash == NULL) 
            $this->_buildHash(); 
        return $this->_hash; 
    } 
 
 
 
 
 
} // class ianacclister 
?> 
 
 |