<?php
#   SGT - The SGT for Internet - http://sgt.eti.br
#   Copyright (c) 2016-2018, Jean Azevedo. All rights reserved.

#   This file is part of SGT.

#   SGT is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   any later version.

#   SGT is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

    include("../seguranca_adm_av.php");
    protegePagina();
    include "../conectar.php";
    include "../log_.php";
    $av = $_SESSION['usuarioAv'];
    if($av == 0){
        echo"<script language='javascript' type='text/javascript'>
            window.location.href='list_ip_unid.php';
        </script>";
    }
    $unidade = $_POST['unidade'];
    $ip_rede = $_POST['ip_rede'];
    $ipi_1 = $_POST['ipi_1'];
    $ipi_2 = $_POST['ipi_2'];
    $ipi_3 = $_POST['ipi_3'];
    $ipi_4 = $_POST['ipi_4'];
    $ipf_1 = $_POST['ipf_1'];
    $ipf_2 = $_POST['ipf_2'];
    $ipf_3 = $_POST['ipf_3'];
    $ipf_4 = $_POST['ipf_4'];
    $cidr = $_POST['cidr'];
    $rede = $_POST['rede'];
    $ip_ini = "$ipi_1.$ipi_2.$ipi_3.$ipi_4";
    $ip_fim = "$ipf_1.$ipf_2.$ipf_3.$ipf_4";
    $ip_1 = $ipi_1;
    $ip_2 = $ipi_2;
    $ip_3 = $ipi_3;
    $ip_11 = $ipf_1;
    $ip_22 = $ipf_2;
    $ip_33 = $ipf_3;
    $ip_44_ = $ipf_4;
    $ip_11 ++;
    $ip_22 ++;
    $ip_33 ++;
    $ip_44_ ++;
    $ipfinal = "$ipf_1.$ipf_2.$ipf_3.$ipf_4";
    while ($ip_33 > $ip_3){
        $ipc = $ipi_4;
        if($ip_3 == $ipf_3){
            if($ipi_3 == $ip_3){
                while ($ipc < $ip_44_){
                    $ip = "$ip_1.$ip_2.$ip_3.$ipc";
                    $atualizar=$pdo_sgt->prepare("DELETE FROM tb_map_ip WHERE ip=:ip AND cidr=:cidr AND unidade=:unidade AND rede=:rede");
                    $atualizar->bindValue(":ip",$ip);
                    $atualizar->bindValue(":cidr",$cidr);
                    $atualizar->bindValue(":unidade",$unidade);
                    $atualizar->bindValue(":rede",$rede);
                    $atualizar->execute();
                    $ipc ++;
                }
                $ip_3 ++;
            } else {
                $ipc = 0;
                while ($ipc < $ip_44_){
                    $ip = "$ip_1.$ip_2.$ip_3.$ipc";
                    $atualizar=$pdo_sgt->prepare("DELETE FROM tb_map_ip WHERE ip=:ip AND cidr=:cidr AND unidade=:unidade AND rede=:rede");
                    $atualizar->bindValue(":ip",$ip);
                    $atualizar->bindValue(":cidr",$cidr);
                    $atualizar->bindValue(":unidade",$unidade);
                    $atualizar->bindValue(":rede",$rede);
                    $atualizar->execute();
                    $ipc ++;
                }
                $ip_3 ++;
            }
        } else {
            if($ipi_3 == $ip_3){
                while ($ipc < 256){
                    $ip = "$ip_1.$ip_2.$ip_3.$ipc";
                    $atualizar=$pdo_sgt->prepare("DELETE FROM tb_map_ip WHERE ip=:ip AND cidr=:cidr AND unidade=:unidade AND rede=:rede");
                    $atualizar->bindValue(":ip",$ip);
                    $atualizar->bindValue(":cidr",$cidr);
                    $atualizar->bindValue(":unidade",$unidade);
                    $atualizar->bindValue(":rede",$rede);
                    $atualizar->execute();
                    $ipc ++;
                }
                $ip_3 ++;
            } else {
                $ipc = 0;
                while ($ipc < 256){
                    $ip = "$ip_1.$ip_2.$ip_3.$ipc";
                    $atualizar=$pdo_sgt->prepare("DELETE FROM tb_map_ip WHERE ip=:ip AND cidr=:cidr AND unidade=:unidade AND rede=:rede");
                    $atualizar->bindValue(":ip",$ip);
                    $atualizar->bindValue(":cidr",$cidr);
                    $atualizar->bindValue(":unidade",$unidade);
                    $atualizar->bindValue(":rede",$rede);
                    $atualizar->execute();
                    $ipc ++;
                }
                $ip_3 ++;
            }
        }
    }
    $mensagem = "Foi removida a rede: $ip_rede, com o nome: $rede, para a unidade: $unidade.";
    salvaLog($mensagem);
    echo"<script language='javascript' type='text/javascript'>
        window.location.href='list_ip_unid.php';
    </script>";
?>