/**
* split_iprange($iprange1, $iprange2)  
* Entnimmt erste IP Range aus der zweiten IP Range und gibt bei Erfolg Array verbleibender 
* IP Ranges zurueck.
*
* @param string iprange1 erste IP Range
* @param string iprange2 zweite IP Range
*
* @return array bei Erfolg bzw. boolean 0 bei Misserfolg
*
* @author Tarik Gasmi
*/
function split_iprange($iprange1, $iprange2)
{
    $iprange3;
    $iprange4;
    $ipranges = array();
    if (check_ip_in_iprange($iprange1, $iprange2) == 1) {
        $ipr1exploded = explode('_', $iprange1);
        $ipr2exploded = explode('_', $iprange2);
        $ipr1s = ip2long($ipr1exploded[0]);
        $ipr1e = ip2long($ipr1exploded[1]);
        $ipr2s = ip2long($ipr2exploded[0]);
        $ipr2e = ip2long($ipr2exploded[1]);
        $ipr3s = $ipr2s;
        $ipr3e = $ipr1s - 1;
        $ipr4s = $ipr1e + 1;
        $ipr4e = $ipr2e;
        if ($ipr3s <= $ipr3e) {
            $iprange3 = long2ip($ipr3s) . "_" . long2ip($ipr3e);
            $ipranges[] = $iprange3;
        }
        if ($ipr4s <= $ipr4e) {
            $iprange4 = long2ip($ipr4s) . "_" . long2ip($ipr4e);
            $ipranges[] = $iprange4;
        }
        #echo "MATCH!<br>";
        return $ipranges;
    } else {
        #echo "IPRange1 not in IPRange2: ";
        return 0;
    }
}
function check_if_max_networks()
{
    global $ds, $suffix, $auDN, $ldapError;
    $networks = 0;
    $mipb_array = get_maxipblocks_au($auDN);
    if ($mipb_array[0] != "") {
        foreach ($mipb_array as $mipb) {
            $exp = explode('_', $mipb);
            $fs = explode('.', $exp[0]);
            $fe = explode('.', $exp[1]);
            if ($fs[3] == 0) {
                return 1;
                break;
            } else {
                $fs[2] = $fs[2] + 1;
                $fs[3] = 0;
                while ($fs[2] <= $fe[2]) {
                    $iprange = implode('_', array(implode('.', $fs), implode('.', $fs)));
                    if (check_ip_in_iprange($iprange, $mipb)) {
                        return 1;
                        break 2;
                    }
                    if ($fs[2] == 255) {
                        $fs[1] = $fs[1] + 1;
                        $fs[2] = 0;
                    } else {
                        $fs[2] = $fs[2] + 1;
                    }
                }
            }
        }
    }
    return $networks;
}
function check_for_existing_mipbs($mipb)
{
    global $ds, $suffix, $auDN, $ldapError;
    #$mipb_match = 0;
    $existing_mipbs = get_maxipblocks_au_childs($auDN);
    foreach (array_keys($existing_mipbs) as $key) {
        if (count($existing_mipbs[$key]) > 1) {
            foreach ($existing_mipbs[$key] as $test) {
                if (check_ip_in_iprange($mipb, $test)) {
                    #echo $test." existiert bereits in AU $key<br><br>";
                    #$mipb_match = 1;
                    return $key;
                }
            }
        } elseif (count($existing_mipbs[$key]) == 1) {
            if (check_ip_in_iprange($mipb, $existing_mipbs[$key])) {
                #echo $existing_mipbs[$key]." existiert bereits in AU $key<br><br>";
                #$mipb_match = 1;
                return $key;
            }
        }
    }
    return 0;
}