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

Source for file WebAPI_Filter.php

Documentation is available at WebAPI_Filter.php

  1. <?php
  2. /**
  3.  * Project: WebAPI_Filter :: Filtered context or IP
  4.  * File:    WebAPI/WebAPI_Filter.php
  5.  *
  6.  * WebAPI_Filter class는 단어 및 IP 필터링을 제공한다.
  7.  *
  8.  * @category    HTTP
  9.  * @package     WebAPI
  10.  * @subpackage  WebAPI_Filter
  11.  * @author      JoungKyun.Kim <http://oops.org>
  12.  * @copyright   (c) 2018, OOPS.org
  13.  * @license     BSD License
  14.  * @link        http://pear.oops.org/package/WebAPI
  15.  * @filesource
  16.  * @since       0.0.1
  17.  */
  18.  
  19. /**
  20.  * import oops/IPCALC pear package
  21.  */
  22. require_once 'ipcalc.php';
  23.  
  24. /**
  25.  * Filtered context or IP
  26.  *
  27.  * WebAPI_Filter class는 단어 및 IP 필터링을 제공한다.
  28.  *
  29.  * @package     WebAPI
  30.  */
  31. Class WebAPI_Filter {
  32.     // {{{ properties
  33.     /**#@+
  34.      * @access private
  35.      */
  36.     /**
  37.      * IPCALC constructor
  38.      * @var IPCALC 
  39.      */
  40.     static private $ip null;
  41.     /**#@-*/
  42.     // }}}
  43.  
  44.     // {{{ +-- private (boolean) context_normalize (&$v)
  45.     /**
  46.      * 필터링 패턴을 정규화 함
  47.      *
  48.      * @access private
  49.      * @return boolean 
  50.      * @param string 필터링 패턴
  51.      */
  52.     private function context_normalize (&$v{
  53.         // 첫라인이 # 으로 시작하거나 공백라인일 경우 SKIP
  54.         if preg_match ('/^#|^[\s]*$/'$v) ) {
  55.             $v null;
  56.             return false;
  57.         }
  58.  
  59.         $src array ('/!/''/\x5c#/''/#.*/''\x5c__sharp__');
  60.         $des array ('\!''\__sharp__''''\#');
  61.         $v preg_replace ($src$des$v);
  62.  
  63.         return true;
  64.     }
  65.     // }}}
  66.  
  67.     // {{{ +-- static public (boolean) context (&$text, &$pattern)
  68.     /**
  69.      * 정규식을 이용하여 필터링
  70.      *
  71.      * @access public
  72.      * @return boolean 
  73.      * @param string 필터링할 문자열
  74.      * @param string 필터링 패턴. 정규식은 PCRE를 사용한다.
  75.      */
  76.     static public function context (&$text&$pattern{
  77.         $lines preg_split ("/\r?\n/"$pattern);
  78.  
  79.         foreach $lines as $line {
  80.             if self::context_nomalize ($line=== false )
  81.                 continue;
  82.  
  83.             if preg_match ('/' $line '/ui'$text$matches) )
  84.                 return true;
  85.         }
  86.  
  87.         return false;
  88.     }
  89.     // }}}
  90.  
  91.     // {{{ +-- static public (boolean) context_file (&$text, &$patfile)
  92.     /**
  93.      * 필터링 파일을 이용하여 필터링
  94.      *
  95.      * @access public
  96.      * @return boolean 
  97.      * @param string 필터링할 문자열
  98.      * @param string 필터링 DB 파일
  99.      */
  100.     static public function context_file (&$text&$patfile{
  101.         if file_exists ($patfile) )
  102.             return false;
  103.  
  104.         $pattern file (
  105.             $patfile,
  106.             FILE_USE_INCLUDE_PATH|FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES
  107.         );
  108.  
  109.         return self::context ($text$pattern);
  110.     }
  111.     // }}}
  112.  
  113.     // {{{ +-- private (string) ip_nomalize (&$v)
  114.     /**
  115.      * @access public
  116.      * @return string 
  117.      * @param string 매칭할 IP
  118.      */
  119.     private function ip_nomalize (&$v{
  120.         // Devided Network And Subnet
  121.         list ($network$subnetpreg_split ('!/!'$v);
  122.  
  123.         if self::$ip === null )
  124.             self::$ip new IPCALC;
  125.         $ip &self::$ip;
  126.  
  127.         // Nomalizing Subnet
  128.         if $subnet {
  129.             if is_numeric ($subnet) )
  130.                 $subnet $ip->prefix2mask ($subnet);
  131.             else {
  132.                 if $ip->valid_ipv4_addr ($subnet) )
  133.                     $subnet '255.255.255.255';
  134.             }
  135.         else
  136.             $subnet '255.255.255.255';
  137.  
  138.         // Nomalizing Network
  139.         if is_numeric ($network&& $network 16777215 {
  140.             $network long2ip ($network);
  141.         else if $ip->valid_ipv4_addr ($network) ) {
  142.             if preg_match ('/^([0-9]{1,3}\.){2}[0-9]{1,3}\.$/'$network) ) {
  143.                 $network .= '0';
  144.                 $subnet '255.255.255.0';
  145.             else if preg_match ('/^[0-9]{1,3}\.[0-9]{1,3}\.$/'$network) ) {
  146.                 $network .= '0.0';
  147.                 $subnet '255.255.0.0';
  148.             else if preg_match ('/^[0-9]{1,3}\.$/'$network) ) {
  149.                 $network .= '0.0.0';
  150.                 $subnet '255.0.0.0';
  151.             }
  152.         }
  153.  
  154.         $v sprintf ('%s/%s'$network$subnet);
  155.     }
  156.     // }}}
  157.  
  158.     // {{{ +-- static public (boolean) ip (&$v)
  159.     /**
  160.      * 접속 IP가 주어진 CIDR/MASK에 포함이 되는지 확인
  161.      *
  162.      * @access public
  163.      * @return boolean 
  164.      * @param  string  매칭시킬 IP 또는 IP 블럭
  165.      *      - 허가된 표현식
  166.      *        - 1.1.1.1
  167.      *        - 1.1.1.1/24
  168.      *        - 1.1.1.1/255.255.255.0
  169.      *        - 1.1.1. (1.1.1.0/24)
  170.      *        - 1.1. (1.1.0.0/16)
  171.      *        - 1.1.1.1 - 2.2.2.2 (range)
  172.      */
  173.     static public function ip (&$v{
  174.         $v preg_replace ('/[\s]+/'''$v);
  175.         $myip WebAPI::client_ip ();
  176.  
  177.         if self::$ip === null )
  178.             self::$ip new IPCALC;
  179.  
  180.         $ip &self::$ip;
  181.  
  182.         // range
  183.         if preg_match ('/-/'$v) ) {
  184.             $myip ip2long ($myip);
  185.             list ($start$endpreg_split ('/-/'$v);
  186.  
  187.             if $ip->valid_ipv4_addr ($start) )
  188.                 $start 0;
  189.             if $ip->valid_ipv4_addr ($end) )
  190.                 $end 4294967295;
  191.  
  192.             $start ip2long ($start);
  193.             $end   ip2long ($end);
  194.  
  195.             if $myip >= $start && $myip <= $end )
  196.                 return true;
  197.  
  198.             return false;
  199.         }
  200.         self::ip_nomalize ($v);
  201.  
  202.         list ($network$subnetpreg_split ('!/!'$v);
  203.  
  204.         $net $ip->network ($network$subnet);
  205.         $netdiff $ip->network ($myip$subnet);
  206.  
  207.         if $net === $netdiff )
  208.             return true;
  209.  
  210.         return false;
  211.     }
  212.     // }}}
  213. }
  214.  
  215. /*
  216.  * Local variables:
  217.  * tab-width: 4
  218.  * c-basic-offset: 4
  219.  * End:
  220.  * vim: set filetype=php noet sw=4 ts=4 fdm=marker:
  221.  * vim600: noet sw=4 ts=4 fdm=marker
  222.  * vim<600: noet sw=4 ts=4
  223.  */
  224. ?>

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