#!/usr/bin/env php
<?php
    if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) { 
        include_once('/etc/asterisk/freepbx.conf'); 
        $restrict_mods = array('firewall' => true);
    }
    $fw     = FreePBX::Firewall();
    $FC     = fpbx_which("fail2ban-client");
    $cmd    = "$FC status | grep 'Jail list' | cut -f3";
    $wl     = $fw->getConfig("dynamic_whitelist");
    $wl     = explode("\n",$wl);
    exec($cmd, $out, $ret);
    $jails  = explode(", ",$out[0]);
    if($ret === 0 && is_array($jails)){
        foreach($jails as $jail){
            exec("$FC get $jail ignoreip | grep - | cut -d' ' -f2 | uniq", $out2, $ret2);
            foreach($wl as $ip){
                if(array_search($ip, $out2) === false){
                    exec("$FC set $jail addignoreip $ip");
                }
            }
            unset($out2);
        }
    }
    $fw->setConfig("syncing", "no");
    exit();
?>