#native_company# #native_desc#
#native_cta#

LDAP get new uid (login name)

By kervin
on February 17, 2001

Version: 0.1

Type: Function

Category: Other

License: GNU General Public License

Description: LDAP function to generate a login name. Can be used for non-LDAP user databases
If you replace UM_uid_exist() with one that queries your particular name service.

Takes a user’s fullname (usually gecos information) and returns a login name
This function tries four times to generate a login name, it returns empty string if it couldn’t.
See also UM_uid_exist();

<?
        //LDAP function to generate a login name. Can be used for non-LDAP user databases
        //If you replace UM_uid_exist() with one that queries your particular name service.

        //Takes a user's fullname (usually gecos information) and returns a login name
        //This function tries four times to generate a login name, it returns empty
        //string if it couldn't.

        //This function relies on UM_uid_exist().  A function which checks an LDAP
        //server to see if a login is already in use.

        function UM_get_new_uid($fullname)
        {
                //first check that the name is valid
                //Only Letters, Numbers, '.', and '-' are allowed
                if(ereg("[^a-zA-Z0-9 .-]+", $fullname))
                {
                        echo $fullname." - false";
                        return "";
                }

                //Remove numbers, '.' and '-' from the fullname
                $fullname = eregi_replace("[.0-9-]", "", $fullname);

                //Remove redundant whitespace ( confuses strtok() )
                $fullname = eregi_replace("[[:space:]]+", " ", $fullname);

                //Make the fullname lowercase
                $fullname = strtolower($fullname);

                //Tokenize the string. Grab the first token and the last token
                $token = strtok($fullname, " ");
                $first_token = $token;
                while($token)
                {
                        $last_token = $token;
                        $token = strtok(" ");
                }

                //Try first initial lastname
                $new_login = $first_token[0].substr($last_token, 0,7);
                if(!UM_uid_exist($new_login))
                        return $new_login;

                //Now try lastname first initial
 $new_login = substr($last_token, 0,7).$first_token[0];
                if(!UM_uid_exist($new_login))
                        return $new_login;

                //Now try lastname
                $new_login = substr($last_token, 0,8);
                if(!UM_uid_exist($new_login))
                        return $new_login;

                //Now try firstname
                $new_login = substr($first_token, 0,8);
                if(!UM_uid_exist($new_login))
                        return $new_login;

                return "";
        }
?>