Source for file WebAPI_Filter.php
Documentation is available at WebAPI_Filter.php
* Project: WebAPI_Filter :: Filtered context or IP
* File: WebAPI/WebAPI_Filter.php
* WebAPI_Filter class는 단어 및 IP 필터링을 제공한다.
* @subpackage WebAPI_Filter
* @author JoungKyun.Kim <http://oops.org>
* @copyright (c) 2018, OOPS.org
* @link http://pear.oops.org/package/WebAPI
* import oops/IPCALC pear package
require_once 'ipcalc.php';
* WebAPI_Filter class는 단어 및 IP 필터링을 제공한다.
static private $ip = null;
// {{{ +-- private (boolean) context_normalize (&$v)
private function context_normalize (&$v) {
// 첫라인이 # 으로 시작하거나 공백라인일 경우 SKIP
$src = array ('/!/', '/\x5c#/', '/#.*/', '\x5c__sharp__');
$des = array ('\!', '\__sharp__', '', '\#');
// {{{ +-- static public (boolean) context (&$text, &$pattern)
* @param string 필터링 패턴. 정규식은 PCRE를 사용한다.
static public function context (&$text, &$pattern) {
foreach ( $lines as $line ) {
if ( self::context_nomalize ($line) === false )
if ( preg_match ('/' . $line . '/ui', $text, $matches) )
// {{{ +-- static public (boolean) context_file (&$text, &$patfile)
* @param string 필터링 DB 파일
FILE_USE_INCLUDE_PATH| FILE_IGNORE_NEW_LINES| FILE_SKIP_EMPTY_LINES
return self::context ($text, $pattern);
// {{{ +-- private (string) ip_nomalize (&$v)
private function ip_nomalize (&$v) {
// Devided Network And Subnet
list ($network, $subnet) = preg_split ('!/!', $v);
if ( self::$ip === null )
if ( is_numeric ($subnet) )
$subnet = $ip->prefix2mask ($subnet);
if ( ! $ip->valid_ipv4_addr ($subnet) )
$subnet = '255.255.255.255';
$subnet = '255.255.255.255';
if ( is_numeric ($network) && $network > 16777215 ) {
} else if ( ! $ip->valid_ipv4_addr ($network) ) {
if ( preg_match ('/^([0-9]{1,3}\.){2}[0-9]{1,3}\.$/', $network) ) {
$subnet = '255.255.255.0';
} else if ( preg_match ('/^[0-9]{1,3}\.[0-9]{1,3}\.$/', $network) ) {
} else if ( preg_match ('/^[0-9]{1,3}\.$/', $network) ) {
$v = sprintf ('%s/%s', $network, $subnet);
// {{{ +-- static public (boolean) ip (&$v)
* 접속 IP가 주어진 CIDR/MASK에 포함이 되는지 확인
* @param string 매칭시킬 IP 또는 IP 블럭
* - 1.1.1.1/255.255.255.0
* - 1.1.1.1 - 2.2.2.2 (range)
static public function ip (&$v) {
if ( self::$ip === null )
if ( preg_match ('/-/', $v) ) {
if ( ! $ip->valid_ipv4_addr ($start) )
if ( ! $ip->valid_ipv4_addr ($end) )
if ( $myip >= $start && $myip <= $end )
list ($network, $subnet) = preg_split ('!/!', $v);
$net = $ip->network ($network, $subnet);
$netdiff = $ip->network ($myip, $subnet);
* vim: set filetype=php noet sw=4 ts=4 fdm=marker:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
|