# Dateiname und evtl. Pfad des Templates für die Webseite
$webseite = "dhcpservice.dwt";
include 'dhcp_header.inc.php';
$mnr = 0;
$sbmnr = -1;
###################################################################################
$mnr = $_GET['mnr'];
# Menuleisten erstellen
createMainMenu($rollen, $mainnr);
createDhcpMenu($rollen, $mnr, $auDN, $sbmnr);
###################################################################################
$template->assign(array("DHCPDN" => "", "CN" => "", "PRIMARY" => "", "SECONDARY" => "", "DESCRIPTION" => "", "STATEMENTS" => "", "FAILOVERPEER" => "", "ALLOW" => "", "DENY" => "", "IGNORE" => "", "DDNSUPDATE" => "", "DEFAULTLEASE" => "", "MAXLEASE" => "", "USEHOSTDCL" => "", "DOMAINNAME" => "", "DOMAINNAMESERVERS" => "", "MAXMESSIZE" => "", "NTPSERVERS" => "", "OPTGENERIC" => "", "OPTDEF" => "", "OPTDEFINITION" => "", "DHCPOFFERNOWDN" => "", "DHCPOFFERNOW" => "", "SUBNET" => "keine Subnetze zugewiesen", "NETMASK" => "", "SUBNETAU" => ""));
# LEASE Times Select
$leasetimes = array(array("600", "10 min"), array("900", "15 min"), array("1800", "30 min"), array("3600", "1 h"), array("7200", "2 h"), array("18000", "5 h"), array("36000", "10 h"), array("86400", "1 Tag"), array("172800", "2 Tage"), array("345600", "4 Tage"));
# DHCP Service Daten
$dhcpsv_array = get_dhcpservices($auDN, array("dn", "cn"));
$dhcpserviceDN = $dhcpsv_array[0]['dn'];
$attributes = array("dn", "cn", "dhcpprimarydn", "dhcpsecondarydn", "description", "dhcpofferdn", "dhcpstatements", "dhcpfailoverpeer", "dhcpoptallow", "dhcpoptddns-update-style", "dhcpoptdefault-lease-time", "dhcpoptdeny", "dhcpoptfilename", "dhcpoptignore", "dhcppermittedclients", "dhcpoptmax-lease-time", "dhcpoptnext-server", "optiondefinition", "dhcpoptuse-host-decl-names", "dhcpoptbroadcast-address", "dhcpoptdhcp-max-message-size", "dhcpoptdomain-name", "dhcpoptdomain-name-servers", "dhcpoptgeneric", "dhcpoptntp-servers", "dhcpoptroot-path", "dhcpoptrouters");
$dhcpsv_data = get_node_data($dhcpserviceDN, $attributes);
#print_r($dhcpsv_data);
# DHCP Service Anbieten
# momentanes Offer
# todo: falls dhcpofferDN leer dann standardwert AU teilbaum
$offerexp = ldap_explode_dn($dhcpsv_data['dhcpofferdn'], 1);
$dhcpoffernow = $offerexp[0];
# alternative Offers
$expdn = ldap_explode_dn($auDN, 0);
# Mit Merkmalen
$expdn = array_slice($expdn, 1);
$expou = ldap_explode_dn($auDN, 1);
# nur Werte
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" => ""));
    }
}