Esempio n. 1
0
function importuser($suffix, $ou, $OnlyBranch = 0)
{
    $ldap = new ldapAD();
    if ($OnlyBranch == 1) {
        $hash = $ldap->Ldap_list($suffix, "(objectClass=user)", array(), 5000);
    } else {
        $hash = $ldap->Ldap_search($suffix, "(objectClass=user)", array(), 5000);
    }
    echo " {$hash["count"]} users\n";
    $prefix = "INSERT IGNORE INTO activedirectory_users \n(dn,samaccountname,mail,userprincipalname,displayname,ou,telephonenumber,mobile,givenname,title,sn) VALUES";
    $q = new mysql();
    for ($i = 0; $i < $hash["count"]; $i++) {
        $dn = $hash[$i]["dn"];
        $displayname = $hash[$i]["displayname"][0];
        $userprincipalname = $hash[$i]["userprincipalname"][0];
        $samaccountname = $hash[$i]["samaccountname"][0];
        $telephoneNumber = $hash[$i]["telephoneNumber"][0];
        $mobile = $hash[$i]["mobile"][0];
        $mail = $hash[$i]["mail"][0];
        $givenname = $hash[$i]["givenname"][0];
        $title = $hash[$i]["title"][0];
        $sn = $hash[$i]["sn"][0];
        for ($z = 0; $z < $hash[$i]["memberof"]["count"]; $z++) {
            LinkGroups($hash[$i]["memberof"][$z], $dn);
        }
        $dn = addslashes(utf8_encode($dn));
        $displayname = addslashes(utf8_encode($displayname));
        $userprincipalname = addslashes(utf8_encode($userprincipalname));
        $samaccountname = addslashes(utf8_encode($samaccountname));
        $givenname = addslashes(utf8_encode($givenname));
        $title = addslashes(utf8_encode($title));
        $sn = addslashes(utf8_encode($sn));
        $GLOBALS["MEMORY_COUNT_USERS"] = $GLOBALS["MEMORY_COUNT_USERS"] + 1;
        $sql[] = "('{$dn}','{$samaccountname}','{$mail}','{$userprincipalname}','{$displayname}','{$ou}','{$telephoneNumber}','{$mobile}','{$givenname}','{$title}','{$sn}')";
        if (count($sql) > 500) {
            if ($GLOBALS["VERBOSE"]) {
                "echo add 500 users\n";
            }
            $sqlfinal = $prefix . " " . @implode(",", $sql);
            $q->QUERY_SQL($sqlfinal, "artica_backup");
            if (!$q->ok) {
                echo $q->mysql_error . "\n";
                return;
            }
            $sql = array();
        }
    }
    if (count($sql) > 0) {
        if ($GLOBALS["VERBOSE"]) {
            "echo add " . count($sql) . " users\n";
        }
        $sqlfinal = $prefix . " " . @implode(",", $sql);
        $q->QUERY_SQL($sqlfinal, "artica_backup");
    }
}
Esempio n. 2
0
function GetUsersArray()
{
    $unix = new unix();
    $netbin = $unix->LOCATE_NET_BIN_PATH();
    exec("{$netbin} ads search '(objectClass=user)' {$GLOBALS["AUTHCMD"]} 2>&1", $results);
    $array = array();
    while (list($index, $line) = each($results)) {
        if (preg_match("#distinguishedName: (.+)#", $line, $re)) {
            $array[trim($re[1])] = true;
            continue;
        }
    }
    $groups = array();
    while (list($dn, $line) = each($array)) {
        $results = array();
        exec("{$netbin} ads search '(&(objectclass=user)(distinguishedName={$dn}))' {$GLOBALS["AUTHCMD"]} 2>&1", $results);
        $givenname = null;
        $displayname = null;
        $samaccountname = null;
        $userprincipalname = null;
        $telephoneNumber = null;
        $mobile = null;
        $title = null;
        $sn = null;
        $ou = null;
        $mail = null;
        while (list($index, $ligne) = each($results)) {
            if (preg_match("#givenName: (.+)#", $ligne, $re)) {
                $givenname = trim($re[1]);
                continue;
            }
            if (preg_match("#displayName: (.+)#", $ligne, $re)) {
                $displayname = trim($re[1]);
                continue;
            }
            if (preg_match("#sAMAccountName: (.+)#", $ligne, $re)) {
                $samaccountname = trim($re[1]);
                continue;
            }
            if (preg_match("#userPrincipalName: (.+)#", $ligne, $re)) {
                $userprincipalname = trim($re[1]);
                continue;
            }
            if (preg_match("#telephoneNumber: (.+)#", $ligne, $re)) {
                $telephoneNumber = trim($re[1]);
                continue;
            }
            if (preg_match("#mobile: (.+)#", $ligne, $re)) {
                $mobile = trim($re[1]);
                continue;
            }
            if (preg_match("#title: (.+)#", $ligne, $re)) {
                $title = trim($re[1]);
                continue;
            }
            if (preg_match("#sn: (.+)#", $ligne, $re)) {
                $sn = trim($re[1]);
                continue;
            }
            if (preg_match("#mail: (.+)#", $ligne, $re)) {
                $mail = trim($re[1]);
                continue;
            }
            if (preg_match("#memberOf: (.+)#", $ligne, $re)) {
                $groups[$dn][] = trim($re[1]);
            }
        }
        if (strpos($samaccountname, "\$") > 0) {
            continue;
        }
        $givenname = addslashes($givenname);
        $displayname = addslashes($displayname);
        $samaccountname = addslashes($samaccountname);
        $userprincipalname = addslashes($userprincipalname);
        $telephoneNumber = addslashes($telephoneNumber);
        $mobile = addslashes($mobile);
        $title = addslashes($title);
        $sn = addslashes($sn);
        if ($GLOBALS["VERBOSE"]) {
            echo $dn . " `{$samaccountname}`\n";
        }
        $sql[] = "('{$dn}','{$samaccountname}','{$mail}','{$userprincipalname}','{$displayname}','{$ou}','{$telephoneNumber}','{$mobile}','{$givenname}','{$title}','{$sn}')";
    }
    if (count($sql) == 0) {
        return;
    }
    $q = new mysql();
    $q->QUERY_SQL("TRUNCATE TABLE activedirectory_users", "artica_backup");
    $q->QUERY_SQL("TRUNCATE TABLE activedirectory_groups", "artica_backup");
    $prefix = "INSERT IGNORE INTO activedirectory_users (dn,samaccountname,mail,userprincipalname,displayname,ou,telephonenumber,mobile,givenname,title,sn) VALUES";
    $sqlfinal = $prefix . " " . @implode(",", $sql);
    if ($GLOBALS["VERBOSE"]) {
        echo $sqlfinal . "\n";
    }
    $q->QUERY_SQL($sqlfinal, "artica_backup");
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        return;
    }
    $sql = array();
    while (list($userdn, $groupdnArray) = each($groups)) {
        while (list($a, $groupdn) = each($groupdnArray)) {
            if ($GLOBALS["VERBOSE"]) {
                echo "link {$userdn} -> {$groupdn}\n";
            }
            LinkGroups($groupdn, $userdn);
        }
    }
    checksGroups();
}