<?php
namespace Overflowsith\Firewall;

use Config;
use Str;

class Firewall
{

    public static function isAllowed($ip)
    {
        switch(Config::get('firewall::config.mode')) {
            case 'disabled':
                return true;
                break;
            case 'enforcing':
                return self::isInWhiteList($ip);
                break;
            case 'permissive':
                return !(self::isInBlackList($ip));
                break;
            default:
                return false;
        }
    }

    public static function isInWhiteList($ip)
    {
        return self::check(Config::get('firewall::config.whitelist', []), $ip);
    }

    public static function isInBlackList($ip)
    {
        return self::check(Config::get('firewall::config.blacklist', []), $ip);
    }

    private static function check($source, $ip)
    {
        foreach($source as $pattern) {
            if (Str::is($pattern, $ip)) {
                return true;
            }
        }
        return false;
    }
}

<?php
namespace Overflowsith\Firewall;

use Config;
use Str;

class Firewall
{

    public static function isAllowed($ip)
    {
        switch(Config::get('firewall::config.mode')) {
            case 'disabled':
                return true;
                break;
            case 'enforcing':
                return self::isInWhiteList($ip) && !(self::isInBlackList($ip));
                break;
            case 'permissive':
                return !(self::isInBlackList($ip));
                break;
            default:
                return false;
        }
    }

    public static function isInWhiteList($ip)
    {
        return self::check(Config::get('firewall::config.whitelist', []), $ip);
    }

    public static function isInBlackList($ip)
    {
        return self::check(Config::get('firewall::config.blacklist', []), $ip);
    }

    private static function check($source, $ip)
    {
        foreach($source as $pattern) {
            if (Str::is($pattern, $ip)) {
                return true;
            }
        }
        return false;
    }
}

Check if ip is in whitelist and not in blacklist
Check if ip is in whitelist and not in blacklist

This can be useful when we define a subnet as whitelist but we want
to exclude a particular ip, for example:

whitelist 192.168.0.*
blacklist 192.168.0.50
