WHOIS
[ class tree: WHOIS ] [ index: WHOIS ] [ all elements ]

Source for file WHOIS.php

Documentation is available at WHOIS.php

  1. <?php
  2. /**
  3.  * Project: oops\WHOIS :: WHOIS php gateway
  4.  * File:    WHOIS.php<br>
  5.  * Dependency:
  6.  *   - {@link http://pear.oops.org/docs/li_myException.html oops/myException}
  7.  *
  8.  * oops\WHOIS class는 php로 작성된 WHOIS gateway이며, IP 주소 lookup을 지원하며
  9.  * 자동으로 recursion을 하여 최종 결과물을 출력한다.
  10.  *
  11.  * @category  Networking
  12.  * @package   WHOIS
  13.  * @author    JoungKyun.Kim <http://oops.org>
  14.  * @copyright (c) 2018, OOPS.org
  15.  * @license   BSD License
  16.  * @link      https://github.com/OOPS-ORG-PHP/WHOIS
  17.  * @since     File available since release 1.0.0
  18.  * @example   WHOIS/tests/test.php WHOIS pear package 예제 코드
  19.  * @filesource
  20.  */
  21.  
  22. namespace oops;
  23.  
  24. /**
  25.  * import myException class
  26.  */
  27. require_once 'myException.php';
  28.  
  29. /**
  30.  * oops\WHOIS pear pcakge의 main class
  31.  *
  32.  * oops\WHOIS class는 php로 작성된 WHOIS gateway이며, IP 주소 lookup을 지원하며
  33.  * 자동으로 recursion을 하여 최종 결과물을 출력한다.
  34.  *
  35.  * @package   WHOIS
  36.  * @author    JoungKyun.Kim <http://oops.org>
  37.  * @copyright (c) 2018, OOPS.org
  38.  * @license   BSD License
  39.  * @link      https://github.com/OOPS-ORG-PHP/WHOIS
  40.  * @since     File available since release 1.0.0
  41.  * @example   WHOIS/tests/test.php WHOIS pear 예제 코드
  42.  */
  43. Class WHOIS {
  44.     // {{{ properities
  45.     /**#@+
  46.      * @access private
  47.      */
  48.     /**
  49.      * infinite recursion 방지
  50.      * @var string 
  51.      */
  52.     private $pnext '';
  53.     /**
  54.      * Next whois server
  55.      * @var string 
  56.      */
  57.     private $next '';
  58.     /**
  59.      * Allow ip search flags
  60.      * @var array 
  61.      */
  62.     private $allows array ('kr''jp');
  63.  
  64.     private $debug false;
  65.     /**#@-*/
  66.     /**
  67.      * recursion 기능 동작 flag
  68.      * @access public
  69.      * @var boolean 
  70.      */
  71.     public $recurse = false;
  72.     // }}}
  73.  
  74.     // {{{ +-- public lookup ($domain, $server = '', $timeout = 5)
  75.     /**
  76.      * 도메인 또는 IP 주소를 lookup 한다.
  77.      *
  78.      * 두번째 파라미터 Whois server를 지정하면, 자동 recursion 기능이 off가
  79.      * 된다. 만약 whois server를 지정하더라도 recursion이 동작하기를
  80.      * 바란다면 WHOIS::$recurse 의 값을 true로 설정하고 WOHIS::lookup을
  81.      * 호출하면 된다.
  82.      *
  83.      * @access public
  84.      * @return stdClass 최종 요청한 whois server와 결과를 stdClass로 반환
  85.      *          - server 최종 요청을 처리한 whois server
  86.      *          - desc   lookup 내용
  87.      * @param  string $domain   검색할 도메인
  88.      * @param  string $server   (optional) WHOIS server
  89.      * @param  string $timeout  (optional) connection timeout [default 5sec]
  90.      */
  91.     function lookup ($domain$server ''$timeout 5{
  92.         $this->pnext $this->next '';
  93.  
  94.         if strlen (trim ($domain)) ) {
  95.             return (object) array (
  96.                 'server' => '',
  97.                 'desc'   => 'No Domains'
  98.             );
  99.         }
  100.         $domain trim ($domain);
  101.  
  102.         if $server {
  103.             $server $this->detect ($domain);
  104.             $this->recurse = true;
  105.         }
  106.  
  107.         $buf $this->query ($server$domain$timeout);
  108.  
  109.         return (object) array (
  110.             'server' => $server,
  111.             'desc'   => trim ($buf)
  112.         );
  113.     }
  114.     // }}}
  115.  
  116.     // {{{ +-- private query ($server, $domain, $timeout = 5)
  117.     private function query (&$server$domain$timeout 5{
  118.         if preg_match ('/:[0-9]+/'$server) )
  119.             $server .= ':43';
  120.  
  121.         $sock stream_socket_client (
  122.             'tcp://' $server$errno,
  123.             $errstr$timeoutSTREAM_CLIENT_CONNECT
  124.         );
  125.  
  126.         if is_resource ($sock) )
  127.             $this->setError (sprintf ('%s (%s)'$errstr$errno));
  128.  
  129.         $query $this->querySet ($domain$server);
  130.  
  131.         fwrite ($sock$query"\r\n");
  132.  
  133.         while ( ($buf fgets ($sock1024)) ) {
  134.             $recv .= $buf;
  135.             $this->nextServer ($buf);
  136.             if $this->debug {
  137.                 fprintf (STDERR"%-5s : %s\n"'SRC'trim ($buf));
  138.                 fprintf (STDERR"%-5s : %s\n"'PNEXT'$this->pnext);
  139.                 fprintf (STDERR"%-5s : %s\n"'NEXT'$this->next);
  140.             }
  141.         }
  142.  
  143.         fclose ($sock);
  144.  
  145.         if ip2long ($domain&& strlen ($this->next== {
  146.             if array_search (strtolower ($this->next)$this->allows=== false )
  147.                 $this->next '';
  148.         }
  149.  
  150.         if $this->recurse && $this->next && $this->pnext != $this->next {
  151.             if preg_match ('/\./'$this->next) )
  152.                 $this->next sprintf ('%s.whois-servers.net'$this->next);
  153.  
  154.             $this->pnext $this->next;
  155.  
  156.             $nserver $this->next;
  157.             $this->next '';
  158.             $nval $this->query ($nserver$domain$timeout);
  159.             if $nval && preg_match ('/no match|out of this regist/i'$nval) ) {
  160.                 $recv $nval;
  161.                 $server $nserver;
  162.             }
  163.         }
  164.  
  165.         return $recv;
  166.     }
  167.     // }}}
  168.  
  169.     // {{{ +-- public (string) detect (&$v)
  170.     /**
  171.      * 주어진 도메인의 whois server를 결정
  172.      *
  173.      * @access public
  174.      * @return string 
  175.      * @param string   도메인 이름
  176.      */
  177.     public function detect (&$v{
  178.         $v strtolower ($v);
  179.  
  180.         if ip2long ($v) )
  181.             return 'whois.arin.net';
  182.  
  183.         if preg_match ('/\.([a-zA-z]{2,})$/'$v$m) )
  184.             return 'whois.crsnic.net';
  185.         else
  186.             $ext $m[1];
  187.  
  188.         if strlen ($ext== {
  189.             if preg_match ('/\.co\.nl$/'$v) )
  190.                 return 'whois.co.nl';
  191.             else if preg_match ('/\.(ac|gov)\.uk$/'$v) )
  192.                 return 'whois.ja.net';
  193.             else if preg_match ('/\.(cd|dz|so)$/'$v) )
  194.                 return sprintf ('whois.nic.%s'$ext);
  195.             else {
  196.                 switch ($ext{
  197.                     case 'bj' :
  198.                         return 'whois.register.bg';
  199.                     case 'bz' :
  200.                         return 'whois.belizenic.bz';
  201.                     case 'ng' :
  202.                         return 'whois.nic.net.ng';
  203.                     case 'su' :
  204.                         return 'whois.tcinet.ru';
  205.                     case 'tc' :
  206.                         return 'whois.adamsnames.tc';
  207.                     default :
  208.                         return sprintf ('%s.whois-servers.net'$ext);
  209.                 }
  210.             }
  211.         }
  212.  
  213.         switch ($ext{
  214.             case 'asia' :
  215.                 return 'whois.nic.asia';
  216.             case 'aero' :
  217.                 return 'whois.aero';
  218.             case 'arpa' :
  219.                 return 'whois.iana.org';
  220.             case 'biz' :
  221.                 return 'whois.nic.biz';
  222.             case 'cat' :
  223.                 return 'whois.cat';
  224.             case 'coop' :
  225.                 return 'whois.nic.coop';
  226.             case 'gov' :
  227.                 return 'whois.nic.gov';
  228.             case 'info' :
  229.                 return 'whois.afilias.info';
  230.             case 'int' :
  231.                 return 'whois.iana.org';
  232.             case 'jobs' :
  233.                 return 'jobswhois.verisign-grs.com';
  234.             case 'mil' :
  235.                 return 'whois.nic.mil';
  236.             case 'mobi' :
  237.                 return 'whois.dotmobiregistry.net';
  238.             case 'museum' :
  239.                 return 'whois.museum';
  240.             case 'name' :
  241.                 return 'whois.nic.name';
  242.             case 'org' :
  243.                 return 'whois.pir.org';
  244.             case 'pro' :
  245.                 return 'whois.nic.pro';
  246.             case 'tel' :
  247.                 return 'whois.nic.tel';
  248.             case 'tarvel' :
  249.                 return 'whois.nic.travel';
  250.             case 'xxx' :
  251.                 return 'whois.nic.xxx';
  252.             default:
  253.                 if preg_match ('/((br|cn|eu|gb|hu|no|qc|sa|se|uk|us|uy|za)\.com|(gb|se|uk)\.net)$/'$v) )
  254.                     return 'whois.centralnic.com';
  255.                 else
  256.                     return 'whois.crsnic.net';
  257.         }
  258.  
  259.         return 'whois.crsnic.net';
  260.     }
  261.     // }}}
  262.  
  263.     // {{{ +-- private nextServer (&$v, &$next)
  264.     private function nextServer (&$v{
  265.         $v strtolower ($v);
  266.         if preg_match ('/(referralserver|whois server|country|country code):/'$v) )
  267.             return false;
  268.  
  269.         $v preg_replace ('/^[^:]+:/'''$v);
  270.         $v trim (preg_replace ('!(http|whois)://!'''$v));
  271.  
  272.         if $this->next {
  273.             $this->next $v;
  274.             return true;
  275.         }
  276.  
  277.         if $this->next == $v )
  278.             return false;
  279.  
  280.         if preg_match ('/\./'$this->next) )
  281.             return false;
  282.         else
  283.             $this->next $v;
  284.  
  285.         return true;
  286.     }
  287.     // }}}
  288.  
  289.     // {{{ +-- private querySet (&$v, $server)
  290.     private function querySet (&$v$server{
  291.         $server preg_replace ('/:[0-9]+$/'''$server);
  292.  
  293.         if ip2long ($v&& $server == 'whois.arin.net' )
  294.             return 'n ' $v;
  295.  
  296.         if preg_match ('/\.jp/'$v) )
  297.             return sprintf ("%s/e\r\n"$v);
  298.  
  299.         return sprintf ("%s%s\r\n"($server == 'whois.crsnic.net''=' ''$v);
  300.     }
  301.     // }}}
  302.  
  303.     // {{{ +-- private setError (&$msg, $level = E_USER_ERROR)
  304.     private function setError (&$msg$level E_USER_ERROR{
  305.         throw new \myException ($msg$level);
  306.     }
  307.     // }}}
  308. }
  309.  
  310. /*
  311.  * Local variables:
  312.  * tab-width: 4
  313.  * c-basic-offset: 4
  314.  * End:
  315.  * vim600: noet sw=4 ts=4 fdm=marker
  316.  * vim<600: noet sw=4 ts=4
  317.  */
  318. ?>

Documentation generated on Tue, 14 May 2019 02:00:50 +0900 by phpDocumentor 1.4.4