function ldap_members_set($username)
 {
     // turn off reporting errors in case the password will be incorrect during binding
     $reporting = error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
     global $board_config;
     $this->ldapMembers = array();
     $this->ldapConn = ldap_connect_ex();
     if ($this->ldapConn == false) {
         message_die(GENERAL_ERROR, 'Could not connect to LDAP directory.', '', __LINE__, __FILE__, '');
         return false;
     } else {
         if ($board_config["ldap_proxy_dn"] != "") {
             $bind = ldap_bind($this->ldapConn, $board_config["ldap_proxy_dn"], $board_config["ldap_proxy_dn_pass"]);
         } else {
             $bind = ldap_bind($this->ldapConn);
         }
         if ($bind == false) {
             message_die(GENERAL_ERROR, 'Could not bind to LDAP directory.', '', __LINE__, __FILE__, '');
             return false;
         } else {
             //-------------------------------------
             //   Get Primary Group ID
             //-------------------------------------
             $ldapSearch = ldap_search($this->ldapConn, $board_config["ldap_dn"], $board_config['ldap_uid'] . '=' . $username, array("primarygroupid"));
             //$ldapSearch = ldap_read($this->ldapConn, $ObjectDN, 'objectClass=*',array("primarygroupid"));
             $ldapResults = ldap_get_entries($this->ldapConn, $ldapSearch);
             if ($ldapResults["count"] != 1) {
                 message_die(GENERAL_ERROR, 'Could not find user in LDAP directory.', '', __LINE__, __FILE__, '');
                 return false;
             } else {
                 $this->userDN = $ldapResults[0]["dn"];
                 if ($this->userDN == '') {
                     message_die(GENERAL_ERROR, 'Could not find DN.', '', __LINE__, __FILE__, '');
                 }
                 if (isset($ldapResults[0]['primarygroupid'][0])) {
                     $this->ldapMembers[] = $ldapResults[0]['primarygroupid'][0];
                 }
                 //else - Non AD systems don't have PrimaryGroupID's so no need for an error.
                 //message_die(GENERAL_ERROR, 'Could did find Primary Group ID.', '', __LINE__, __FILE__, '');
                 ldap_free_result($ldapSearch);
                 //-------------------------------------
                 //  Get the other Groups
                 //-------------------------------------
                 $this->ldap_members($this->userDN);
                 return true;
             }
             //-------------------------------------
             //  Clean up
             //-------------------------------------
             ldap_unbind($this->ldapConn);
         }
     }
 }
function add_ldap_user($username)
{
    global $db, $board_config;
    // reading user informations from ldap
    $connection = ldap_connect_ex();
    if ($board_config['ldap_proxy_dn'] != '') {
        $bind = ldap_bind($connection, $board_config['ldap_proxy_dn'], $board_config['ldap_proxy_dn_pass']);
    } else {
        $bind = ldap_bind($connection);
    }
    $query = ldap_search($connection, $board_config['ldap_dn'], $board_config['ldap_uid'] . '=' . $username);
    $query_result = ldap_get_entries($connection, $query);
    $email = $query_result[0][$board_config['ldap_email']][0];
    $web = $query_result[0][$board_config['ldap_web']][0];
    $location = $query_result[0][$board_config['ldap_location']][0];
    $occupation = $query_result[0][$board_config['ldap_occupation']][0];
    $signature = $query_result[0][$board_config['ldap_signature']][0];
    ldap_close($connection);
    // obtaining new user id
    $sql = "SELECT MAX(user_id) AS total\n\t\tFROM " . USERS_TABLE;
    if (!($result = $db->sql_query($sql))) {
        message_die(GENERAL_ERROR, 'Could not obtain next user_id information', '', __LINE__, __FILE__, $sql);
    }
    if (!($row = $db->sql_fetchrow($result))) {
        message_die(GENERAL_ERROR, 'Could not obtain next user_id information', '', __LINE__, __FILE__, $sql);
    }
    $user_id = $row['total'] + 1;
    // creating new user
    $sql = "INSERT INTO " . USERS_TABLE . "( user_id, " . "username, " . "user_regdate, " . "user_password, " . "user_email, " . "user_website, " . "user_occ, " . "user_from, " . "user_sig, " . "user_viewemail, " . "user_attachsig, " . "user_allowsmile, " . "user_allowhtml, " . "user_allowbbcode, " . "user_allow_viewonline, " . "user_notify, " . "user_notify_pm, " . "user_popup_pm, " . "user_timezone, " . "user_dateformat, " . "user_lang, " . "user_style, " . "user_level, " . "user_allow_pm, " . "user_active," . "user_type" . ")" . "VALUES (" . "{$user_id}, " . "'" . str_replace("\\'", "''", $username) . "', " . time() . ", " . "'', " . "'" . str_replace("\\'", "''", $email) . "', " . "'" . str_replace("\\'", "''", $web) . "', " . "'" . str_replace("\\'", "''", $occupation) . "', " . "'" . str_replace("\\'", "''", $location) . "', " . "'" . str_replace("\\'", "''", $signature) . "', " . "1, " . $board_config['allow_sig'] . ", " . $board_config['allow_smilies'] . ", " . $board_config['allow_html'] . ", " . $board_config['allow_bbcode'] . ", " . "1, " . "0, " . "1, " . "1, " . $board_config['board_timezone'] . ", " . "'" . $board_config['default_dateformat'] . "', " . "'" . $board_config['default_lang'] . "', " . $board_config['default_style'] . ", " . "0, " . "1, " . "1, " . User_Type_LDAP . " " . ")";
    if (!($result = $db->sql_query($sql, BEGIN_TRANSACTION))) {
        message_die(GENERAL_ERROR, 'Could not insert data into users table', '', __LINE__, __FILE__, $sql);
    }
    // creating new 'personal user' group
    $sql = "INSERT INTO " . GROUPS_TABLE . " (group_name, group_description, group_single_user, group_moderator)\n\t\tVALUES ('', 'Personal User', 1, 0)";
    if (!($result = $db->sql_query($sql))) {
        message_die(GENERAL_ERROR, 'Could not insert data into groups table', '', __LINE__, __FILE__, $sql);
    }
    $group_id = $db->sql_nextid();
    // assigning new user to the new 'personal user' group
    $sql = "INSERT INTO " . USER_GROUP_TABLE . " (user_id, group_id, user_pending)\n\t\tVALUES ({$user_id}, {$group_id}, 0)";
    if (!($result = $db->sql_query($sql, END_TRANSACTION))) {
        message_die(GENERAL_ERROR, 'Could not insert data into user_group table', '', __LINE__, __FILE__, $sql);
    }
}