include '../standard_header.inc.php';
# Dateiname und evtl. Pfad des Templates für die Webseite
$webseite = "dhcppools.dwt";
include 'dhcp_header.inc.php';
$sbmnr = -1;
###################################################################################
$mnr = $_GET['mnr'];
$sbmnr = $_GET['sbmnr'];
# Menuleisten erstellen
createMainMenu($rollen, $mainnr);
createDhcpMenu($rollen, $mnr, $auDN, $sbmnr);
###################################################################################
# DHCP Pools Daten holen
$attributes = array("dn", "cn", "dhcphlpcont", "dhcprange", "description", "dhcpstatements", "dhcpoptallow", "dhcpoptdefault-lease-time", "dhcpoptdeny", "dhcpoptignore", "dhcpoptmax-lease-time", "dhcpoptgeneric", "hlprbservice", "dhcpoptfilename", "dhcpoptnext-server", "dhcppermittedclients");
$pools = get_dhcppools($auDN, $attributes);
$scope_attributes = array("dn", "cn", "dhcphlpcont", "dhcpstatements", "dhcpoptallow", "dhcpoptdeny", "dhcpoptignore", "dhcpoptdefault-lease-time", "dhcpoptmax-lease-time", "dhcpoptgeneric", "hlprbservice", "dhcpoptfilename", "dhcpoptnext-server", "dhcppermittedclients");
$global_data = get_node_data($DHCP_SERVICE, $scope_attributes);
#print_r($pools);
if (count($pools) == 0) {
    redirect(0, "dhcpnopool.php?mnr=" . $mnr, "", $addSessionId = TRUE);
    die;
}
$template->assign(array("POOLDN" => "", "CN" => "", "SUBNETDN" => "", "SUBNET" => "", "IPRANGES" => "", "DESCRIPTION" => "", "STATEMENTS" => "", "ALLOW" => "", "DENY" => "", "IGNORE" => "", "RBS" => "", "RBSRV" => "", "RBSSELECT" => "", "DEFAULTLEASE" => "", "MAXLEASE" => "", "MNR" => $mnr));
$template->define_dynamic("Dhcppools", "Webseite");
$pools = array_natsort($pools, "dhcphlpcont", "dhcphlpcont");
# Für jeden Pool ...
foreach ($pools as $pool) {
    $subnet_data = get_node_data($pool['dhcphlpcont'], $scope_attributes);
    # DHCP Subnet des Pools
    if ($pool['dhcphlpcont']) {
/**
* objects_to_adjust($newmipb,$delip,$childauDN,$cchild_array)  
* Liefert die durch eine Reduzierung einer IP Delegierung betroffenen Host/DHCP-Objekte der Child-AU
* und Child-Child-AUs in einem Array. Dieses enthaelt fuer jedes Objekt dessen Distinguished Name,
* dessen IP Adresse(n)/Range(s) und den Distinguished Name der AU der das Objekt angehoert. Parameter 
* die fuer die Funktionen delete_ip_host(), delete_ip_dhcprange(), modify_ip_dhcprange() benoetigt werden.
*
* @param string newmipb IP Bereich der nach Reduzierung verbleibt
* @param string delip IP Bereich der durch Reduzierung wegfaellt
* @param string childauDN Distinguished Name des untergeordneten (Child) LDAP AU-Objektes
* @param array cchild_array Array von allen Child-Child-AUs (jedes Item enthaelt DN und MaxIPBlock)
*
* @return array objects_to_adjust Array aller betroffenen Objekte (DN, IP, auDN)
*
* @author Tarik Gasmi
*/
function objects_to_adjust($newmipb, $delip, $childauDN, $cchild_array)
{
    global $ds, $suffix, $ldapError;
    # Hosts von child-AU, child-child-AU
    $chosts = get_hosts($childauDN, array("dn", "ipaddress"));
    # print_r($chosts);printf("<br><br>");
    $cchosts = array();
    foreach ($cchild_array as $item) {
        $cchostsitem = get_hosts($item['dn'], array("dn", "ipaddress"));
        foreach ($cchostsitem as $item2) {
            $cchosts[] = $item2;
        }
    }
    $chosts = array_merge($chosts, $cchosts);
    # print_r($chosts);printf("<br><br>");
    # Pools von child-AU, child-child-AU
    $csubnets = get_dhcppools($childauDN, array("dn", "dhcprange"));
    # print_r($csubnets);printf("<br><br>");
    $ccsubnets = array();
    foreach ($cchild_array as $item) {
        $ccsubnetsitem = get_hosts($item['dn'], array("dn", "dhcprange"));
        foreach ($ccsubnetsitem as $item2) {
            $ccsubnets[] = $item2;
        }
    }
    $csubnets = array_merge($csubnets, $ccsubnets);
    # print_r($csubnets);printf("<br><br>");
    # Zu loeschende Hosts bestimmen
    $chosts_todo = array();
    foreach ($delip as $delipitem) {
        if (count($chosts) != 0) {
            foreach ($chosts as $item) {
                if (count($item['ipaddress']) > 1) {
                    foreach ($item['ipaddress'] as $item2) {
                        if (intersect_ipranges($delipitem, $item2) != false) {
                            $chosts_todo[] = array('dn' => $item['dn'], 'ip' => $item['ipaddress'], 'auDN' => $item['auDN']);
                        }
                    }
                } elseif (count($item['ipaddress']) == 1) {
                    if (intersect_ipranges($delipitem, $item['ipaddress']) != false) {
                        $chosts_todo[] = array('dn' => $item['dn'], 'ip' => $item['ipaddress'], 'auDN' => $item['auDN']);
                    }
                }
            }
        }
    }
    # print_r($chosts_todo);printf("<br><br>");
    # Zu loeschende Subnets bestimmen, und wie IP Range anzupassen ist
    $csubnets_todo = array();
    foreach ($delip as $delipitem) {
        if (count($csubnets) != 0) {
            foreach ($csubnets as $item) {
                if (count($item['dhcprange']) > 1) {
                    foreach ($item['dhcprange'] as $item2) {
                        # print_r(intersect_ipranges($delipitem,$item2));
                        if (intersect_ipranges($delipitem, $item2) != false) {
                            $csubnets_todo[] = array('dn' => $item['dn'], 'newrange' => intersect_ipranges($newmipb, $item2), 'auDN' => $item['auDN']);
                        }
                    }
                } elseif (count($item['dhcprange']) == 1) {
                    # print_r(intersect_ipranges($delipitem,$item['dhcprange']));
                    if (intersect_ipranges($delipitem, $item['dhcprange']) != false) {
                        $csubnets_todo[] = array('dn' => $item['dn'], 'newrange' => intersect_ipranges($newmipb, $item['dhcprange']), 'auDN' => $item['auDN']);
                    }
                }
            }
        }
    }
    # print_r($csubnets_todo);printf("<br><br>");
    $objects_to_adjust = array('hostips' => $chosts_todo, 'dhcpranges' => $csubnets_todo);
    return $objects_to_adjust;
}
function createDhcpMenu($rollen, $mnr, $auDN, $sbmnr)
{
    global $template, $START_PATH, $rootAU;
    $i = 0;
    $mipbs = get_maxipblocks_au($auDN);
    #echo "MIPB: "; print_r ($mipbs); echo "<br>";
    if ($mipbs[0] != "") {
        $hauptmenu = array();
    } else {
        $hauptmenu = array(array("link" => "no_dhcp.php?mnr=" . $i, "text" => "&Uuml;bersicht", "zugriff" => "alle"));
        $i++;
    }
    $pools = get_dhcppools($auDN, array("dn"));
    if (count($pools) == 0) {
        $poollink = "dhcpnopool.php";
    } else {
        $poollink = "dhcppools.php";
    }
    if ($auDN == $rootAU) {
        $dhcpservice_array = get_dhcpservices($auDN, array("dn", "cn"));
        if (count($dhcpservice_array) == 0) {
            $dhcpsvlink = "new_dhcpservice.php?mnr=1";
        } else {
            $dhcpsvlink = "dhcpservice.php?mnr=1";
        }
        $hauptmenu[] = array("link" => $dhcpsvlink, "text" => "DHCP Service", "zugriff" => array("MainAdmin", "DhcpAdmin"));
        $i++;
    }
    #if ($mipbs[0] != ""){
    #if ( check_if_max_networks() ){
    $hauptmenu[] = array("link" => "dhcpsubnets.php?mnr=" . $i, "text" => "DHCP Subnets", "zugriff" => array("MainAdmin", "DhcpAdmin"));
    if ($mipbs[0] != "") {
        $subnets = array();
        # falls komplette Netze verfügbar, link zum Neuanlegen
        if (check_if_free_networks()) {
            #$dhcpsubnet_array = get_dhcpsubnets($auDN,array("dn","cn"));
            /*for ($j=0;$j<count($dhcpsubnet_array);$j++){
             		$subnets[] = array("link" => "dhcpsubnet.php?dn=".$dhcpsubnet_array[$j]['dn']."&mnr=".$i."&sbmnr=".$j,
             							"text" => $dhcpsubnet_array[$j]['cn'],
            						"zugriff" => array("MainAdmin","DhcpAdmin"));
             	}*/
            $subnets[] = array("link" => "new_dhcpsubnet.php?mnr=" . $i . "&sbmnr=0", "text" => "Neues DHCP Subnet", "zugriff" => array("MainAdmin", "DhcpAdmin"));
        }
        $i++;
        $hauptmenu[] = array("link" => $poollink . "?mnr=" . $i, "text" => "Dynamische DHCP Pools", "zugriff" => array("MainAdmin", "DhcpAdmin"));
        $submenu = array($subnets, array());
        #}else{
        #   $hauptmenu [] = array("link" => $poollink."?mnr=".$i,
        #                          "text" => "Dynamische DHCP Pools",
        #                          "zugriff" => array("MainAdmin","DhcpAdmin"));
        #}
    }
    # DHCP Classes
    if ($auDN == $rootAU) {
        $hauptmenu[] = array("link" => "dhcp_classes.php?mnr=" . ++$i, "text" => "DHCP Classes", "zugriff" => array("MainAdmin", "DhcpAdmin"));
        $hauptmenu[] = array("link" => "dhcp_condstmts.php?mnr=" . ++$i, "text" => "DHCP Conditional Statements", "zugriff" => array("MainAdmin", "DhcpAdmin"));
    }
    # Zusammenstellen der Menuleiste
    $template->define_dynamic("Hauptmenu", "Menu");
    $template->define_dynamic("Submenu", "Menu");
    $i = 0;
    $maxmenu = count($hauptmenu);
    foreach ($hauptmenu as $item) {
        $template->clear_parse("SUBMENU_LIST");
        if ($item['zugriff'] === "alle" || vergleicheArrays($rollen, $item['zugriff'])) {
            $subempty = 0;
            $j = 0;
            $maxsub = count($submenu[$mnr]);
            if ($maxsub > 0) {
                foreach ($submenu[$mnr] as $item2) {
                    if ($item2['zugriff'] === "alle" || vergleicheArrays($rollen, $item2['zugriff'])) {
                        if ($i != $mnr) {
                            $template->assign(array("SUB" => ""));
                            # 								"LINK_S" => "",
                            #                       "TEXT_S" => ""));
                            $template->parse("SUBMENU_LIST", ".Submenu");
                            $template->clear_dynamic("Submenu");
                        } else {
                            if ($j == 0) {
                                if ($sbmnr == 0) {
                                    if ($maxsub == 1) {
                                        $zwisch = "";
                                    } else {
                                        $zwisch = "";
                                    }
                                    $lastaktive = true;
                                    $farb = "#505050";
                                } else {
                                    if ($maxsub == 1) {
                                        $zwisch = "";
                                    } else {
                                        $zwisch = "";
                                    }
                                    $farb = "#A0A0A0";
                                    $lastaktive = false;
                                }
                            } else {
                                if ($sbmnr == $j) {
                                    if ($maxsub == $j + 1) {
                                        $zwisch = "";
                                    } else {
                                        $zwisch = "";
                                    }
                                    $lastaktive = true;
                                    $farb = "#505050";
                                } else {
                                    $farb = "#A0A0A0";
                                    if ($maxsub == $j + 1) {
                                        $zwisch = "";
                                    } else {
                                        $zwisch = "";
                                    }
                                    # if ($lastaktive) {$zwisch="branch";}
                                    # else {$zwisch="branch";}
                                    $lastaktive = false;
                                }
                            }
                            $htmlcode = "\n      \t\t   \t\t<tr height='2'>\n      \t\t\t\t\t\t<td></td><td></td><td></td><td></td>\n      \t\t   \t\t</tr>\n      \t\t   \t\t<tr>\n      \t\t\t\t\t\t<td width='8%'>&nbsp;</td>\n      \t\t\t\t\t\t<td width='8%' align='right'>" . $zwisch . "</td>\n      \t\t     \t\t\t<td width='74%' align='left' style='border-width:1 1 1 1;border-color:#000000;border-style:solid;padding:2;padding-left:15px;background-color:{FARBE_S}'> \n      \t\t     \t\t\t<a href='" . $item2['link'] . "' style='text-decoration:none'><code class='submenue_schrift'>" . $item2['text'] . "</code></a></td>\n      \t\t\t\t\t\t<td width='10%'>&nbsp;</td> \t\t\t\t\t\t\n      \t\t\t\t\t</tr>\n      \t\t\t\t\t";
                            $template->assign(array("SUB" => $htmlcode));
                            $template->assign(array("FARBE_S" => $farb));
                            $template->parse("SUBMENU_LIST", ".Submenu");
                            $template->clear_dynamic("Submenu");
                        }
                    } else {
                        $subempty++;
                    }
                    $j = $j + 1;
                }
            }
            if ($subempty == count($submenu[$mnr])) {
                $template->assign(array("SUB" => ""));
                #							  "LINK_S" => "",
                #                       "TEXT_S" => ""));
                $template->parse("SUBMENU_LIST", ".Submenu");
                $template->clear_dynamic("Submenu");
            }
            # weiter im Hauptmenü
            if ($i == 0) {
                if ($mnr == 0) {
                    if (count($submenu[$i][0]) != 0) {
                        if ($maxmenu == 1) {
                            $zwisch = "";
                        } else {
                            $zwisch = "";
                        }
                    } else {
                        $zwisch = "";
                    }
                    $lastaktive = true;
                    $farb = "#505050";
                } else {
                    if (count($submenu[$i][0]) != 0) {
                        if ($maxmenu == 1) {
                            $zwisch = "<a href='{LINK_M}' style='border-style=none;text-decoration:none'>\n               \t<img style='border-width:0;border-style=none;' src='../pics/plus3.gif'></a>";
                        } else {
                            $zwisch = "<a href='{LINK_M}' style='border-style=none;text-decoration:none'>\n               \t<img style='border-width:0;border-style=none;' src='../pics/plus3.gif'></a>";
                        }
                    } else {
                        $zwisch = "";
                    }
                    $farb = "#A0A0A0";
                    $lastaktive = false;
                }
            } else {
                if ($mnr == $i) {
                    if (count($submenu[$mnr][0]) != 0) {
                        if ($maxmenu == $i + 1) {
                            $zwisch = "";
                        } else {
                            $zwisch = "";
                        }
                    } else {
                        $zwisch = "";
                    }
                    $lastaktive = true;
                    $farb = "#505050";
                } else {
                    $farb = "#A0A0A0";
                    if (count($submenu[$i][0]) != 0) {
                        if ($maxmenu == $i + 1) {
                            $zwisch = "<a href='{LINK_M}' style='border-style=none;text-decoration:none'>\n               \t<img style='border-width:0;border-style=none;' src='../pics/plus3.gif'></a>";
                        } else {
                            $zwisch = "<a href='{LINK_M}' style='border-style=none;text-decoration:none'>\n               \t<img style='border-width:0;border-style=none;' src='../pics/plus3.gif'></a>";
                        }
                    } else {
                        $zwisch = "";
                    }
                    #if ($lastaktive) {$zwisch="";}
                    #else {$zwisch="";}
                    $lastaktive = false;
                }
            }
            $template->assign(array("ICON" => $zwisch, "FARBE" => $farb, "LINK_M" => $item["link"], "TEXT_M" => $item["text"]));
            $template->parse("HAUPTMENU_LIST", ".Hauptmenu");
            $template->clear_dynamic("Hauptmenu");
        }
        $i = $i + 1;
    }
    if ($lastaktive) {
        $template->assign(array("ENDE" => ""));
    } else {
        $template->assign(array("ENDE" => ""));
    }
}