19 declare(strict_types=1);
    89         $this->db = $DIC->database();
    90         $this->
lng = $DIC->language();
    91         $this->ilErr = $DIC[
'ilErr'];
    93         $this->server_id = $a_server_id;
   103         return self::$instances[$a_server_id] ?? (self::$instances[$a_server_id] = 
new ilLDAPServer($a_server_id));
   111         if (!$this->fallback_urls) {
   115         $all_urls = array_merge($this->fallback_urls);
   116         $all_urls[] = $this->
getUrl();
   118         $query = 
'UPDATE ldap_server_settings SET ' .
   119                 'url = ' . $this->db->quote(implode(
',', $all_urls), 
'text') . 
' ' .
   120                 'WHERE server_id = ' . $this->db->quote($this->
getServerId(), 
'integer');
   121         $this->db->manipulate($query);
   131         return function_exists(
'ldap_bind');
   143         $ilDB = $DIC[
'ilDB'];
   145         $query = 
"SELECT server_id FROM ldap_server_settings " .
   146             "WHERE active = 1 AND authentication = 1 " .
   152         while ($row = 
$ilDB->fetchObject(
$res)) {
   153             $server_ids[] = (
int) $row->server_id;
   167         $ilDB = $DIC[
'ilDB'];
   169         $query = 
"SELECT server_id FROM ldap_server_settings " .
   170             "WHERE active = 1 " .
   171             "AND sync_per_cron = 1 " .
   178         while ($row = 
$ilDB->fetchObject(
$res)) {
   179             $server_ids[] = (
int) $row->server_id;
   192         $ilDB = $DIC[
'ilDB'];
   194         $query = 
"SELECT server_id FROM ldap_server_settings " .
   195             "WHERE active = 1 " .
   196             "AND role_sync_active = 1 ";
   202         while ($row = 
$ilDB->fetchObject(
$res)) {
   203             $server_ids[] = (
int) $row->server_id;
   215         $servers = self::_getActiveServerList();
   216         if (count($servers)) {
   231         $ilDB = $DIC[
'ilDB'];
   233         $query = 
"SELECT server_id FROM ldap_server_settings ORDER BY name";
   238         while ($row = 
$ilDB->fetchObject(
$res)) {
   239             $server_ids[] = $row->server_id;
   252         $ilDB = $DIC[
'ilDB'];
   254         $query = 
"SELECT server_id FROM ldap_server_settings ORDER BY name";
   274         $ilDB = $DIC[
'ilDB'];
   276         $query = 
"SELECT * FROM ldap_server_settings ORDER BY name";
   291         $ilDB = $DIC[
'ilDB'];
   293         $query = 
"SELECT server_id FROM ldap_server_settings " .
   294             "WHERE active = " . 
$ilDB->quote(1, 
'integer') . 
" " .
   295             "AND authentication = " . 
$ilDB->quote(0, 
'integer') . 
" " .
   296             "AND ( authentication_type = " . 
$ilDB->quote($a_auth_mode, 
'integer') . 
" " .
   297             "OR authentication_type = " . 
$ilDB->quote(0, 
'integer') . 
")";
   300         $server_ids = array();
   302             $server_ids[] = $row->server_id;
   314         $ilDB = $DIC[
'ilDB'];
   316         $query = 
"SELECT server_id FROM ldap_server_settings " .
   317             "WHERE authentication_type = " . 
$ilDB->quote($a_auth_mode, 
'integer') . 
" " .
   318             "AND authentication = " . 
$ilDB->quote(0, 
'integer');
   330         $ilDB = $DIC[
'ilDB'];
   332         $query = 
"SELECT server_id FROM ldap_server_settings " .
   333             "WHERE authentication_type = " . 
$ilDB->quote($a_auth_mode, 
'integer') . 
" ";
   336             return $row->server_id;
   348         $ilDB = $DIC[
'ilDB'];
   350         $query = 
'UPDATE ldap_server_settings ' .
   351             'SET authentication_type = ' . 
$ilDB->quote(0, 
'integer') . 
' ' .
   352             'WHERE authentication_type = ' . 
$ilDB->quote($a_authmode, 
'integer');
   353         $ilDB->manipulate($query);
   363     public static function toggleDataSource(
int $a_ldap_server_id, 
int $a_auth_mode, 
int $a_status): bool
   367         $ilDB = $DIC[
'ilDB'];
   369         self::disableDataSourceForAuthMode($a_auth_mode);
   372             $query = 
"UPDATE ldap_server_settings " .
   373                 'SET authentication_type = ' . 
$ilDB->quote($a_auth_mode, 
'integer') . 
" " .
   374                 'WHERE server_id = ' . 
$ilDB->quote($a_ldap_server_id, 
'integer');
   375             $ilDB->manipulate($query);
   387         $logger = $DIC->logger()->auth();
   389         if ($a_auth_mode === 
'') {
   390             $logger->error(__METHOD__ . 
': No auth mode given..............');
   394         $auth_arr = explode(
'_', $a_auth_mode);
   404         if (self::isAuthModeLDAP($a_auth_mode)) {
   405             $auth_arr = explode(
'_', $a_auth_mode);
   406             return (
int) $auth_arr[1];
   417         $auth_arr = explode(
'_', $a_auth_key);
   418         if (count($auth_arr) > 1) {
   419             return 'ldap_' . $auth_arr[1];
   430         $auth_arr = explode(
'_', $a_auth_mode);
   431         if (count($auth_arr) > 1) {
   448         $this->enabled_authentication = $a_status;
   464         $this->authentication_mapping = $a_map;
   489         $this->active = $a_status;
   499     public function setUrl(
string $a_url): void
   501         $this->url_string = $a_url;
   504         $urls = explode(
',', $a_url);
   507         foreach ($urls as $url) {
   512                 $this->fallback_urls[] = 
$url;
   530         foreach (array_merge(array(0 => $this->url), $this->fallback_urls) as $url) {
   540                 ilLoggerFactory::getLogger(
'auth')->error(
'Cannot connect to LDAP server: ' . $url . 
' ' . $exc->getCode() . 
' ' . $exc->getMessage());
   554         $this->name = $a_name;
   562         $this->version = $a_version;
   570         $this->base_dn = $a_base_dn;
   578         $this->referrals = $a_status;
   586         $this->tls = $a_status;
   594         if ($a_type === self::LDAP_BIND_USER) {
   595             $this->binding_type = self::LDAP_BIND_USER;
   597             $this->binding_type = self::LDAP_BIND_ANONYMOUS;
   606         $this->bind_user = $a_user;
   614         $this->bind_password = $a_password;
   622         $this->search_base = $a_search_base;
   630         $this->user_attribute = $a_user_attr;
   638         $this->
filter = $a_filter;
   646         $this->group_dn = $a_value;
   654         $this->group_filter = $a_value;
   662         $this->group_member = $a_value;
   670         $this->group_name = $a_value;
   681         if (!is_array($names)) {
   685         return array_filter(
array_map(
'trim', $names));
   695         $this->group_attribute = $a_value;
   699         $this->group_optional = $a_status;
   707         $this->group_user_filter = $a_filter;
   720         $this->memberisdn = $a_value;
   724         $this->group_scope = $a_value;
   732         $this->user_scope = $a_value;
   744         $this->sync_on_login = $a_value;
   752         $this->sync_per_cron = $a_value;
   756         $this->global_role = $a_role;
   764         $this->role_bind_dn = $a_value;
   772         $this->role_bind_pass = $a_value;
   780         $this->role_sync_active = $a_value;
   789         $this->username_filter = $a_value;
   794         $this->escape_dn = $a_value;
   807         $this->account_migration = $a_status;
   824         $this->ilErr->setMessage(
'');
   829             $this->ilErr->setMessage($this->
lng->txt(
'fill_out_all_required_fields'));
   834             $this->ilErr->appendMessage($this->
lng->txt(
'ldap_missing_bind_user'));
   838             $this->ilErr->appendMessage($this->
lng->txt(
'ldap_missing_role_assignment'));
   841             $this->ilErr->appendMessage($this->
lng->txt(
'ldap_tls_conflict'));
   844         return $this->ilErr->getMessage() === 
'';
   849         $next_id = $this->db->nextId(
'ldap_server_settings');
   851         $query = 
'INSERT INTO ldap_server_settings (server_id,active,name,url,version,base_dn,referrals,tls,bind_type,bind_user,bind_pass,' .
   852             'search_base,user_scope,user_attribute,filter,group_dn,group_scope,group_filter,group_member,group_memberisdn,group_name,' .
   853             'group_attribute,group_optional,group_user_filter,sync_on_login,sync_per_cron,role_sync_active,role_bind_dn,role_bind_pass,migration, ' .
   854             'authentication,authentication_type,username_filter, escape_dn) ' .
   855             'VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)';
   859                 'integer',
'integer',
'text',
'text',
'integer',
'text',
'integer',
'integer',
'integer',
'text',
'text',
'text',
'integer',
   860                 'text',
'text',
'text',
'integer',
'text',
'text',
'integer',
'text',
'text',
'integer',
'text',
'integer',
'integer',
'integer',
   861                 'text',
'text', 
'integer',
'integer',
'integer',
"text", 
'integer'),
   900         $this->server_id = $next_id;
   906         $query = 
"UPDATE ldap_server_settings SET " .
   907             "active = " . $this->db->quote($this->
isActive(), 
'integer') . 
", " .
   908             "name = " . $this->db->quote($this->
getName(), 
'text') . 
", " .
   909             "url = " . $this->db->quote($this->
getUrlString(), 
'text') . 
", " .
   910             "version = " . $this->db->quote($this->
getVersion(), 
'integer') . 
", " .
   911             "base_dn = " . $this->db->quote($this->
getBaseDN(), 
'text') . 
", " .
   912             "referrals = " . $this->db->quote($this->
isActiveReferrer(), 
'integer') . 
", " .
   913             "tls = " . $this->db->quote($this->
isActiveTLS(), 
'integer') . 
", " .
   914             "bind_type = " . $this->db->quote($this->
getBindingType(), 
'integer') . 
", " .
   915             "bind_user = " . $this->db->quote($this->
getBindUser(), 
'text') . 
", " .
   916             "bind_pass = " . $this->db->quote($this->
getBindPassword(), 
'text') . 
", " .
   917             "search_base = " . $this->db->quote($this->
getSearchBase(), 
'text') . 
", " .
   918             "user_scope = " . $this->db->quote($this->
getUserScope(), 
'integer') . 
", " .
   919             "user_attribute = " . $this->db->quote($this->
getUserAttribute(), 
'text') . 
", " .
   920             "filter = " . $this->db->quote($this->
getFilter(), 
'text') . 
", " .
   921             "group_dn = " . $this->db->quote($this->
getGroupDN(), 
'text') . 
", " .
   922             "group_scope = " . $this->db->quote($this->
getGroupScope(), 
'integer') . 
", " .
   923             "group_filter = " . $this->db->quote($this->
getGroupFilter(), 
'text') . 
", " .
   924             "group_member = " . $this->db->quote($this->
getGroupMember(), 
'text') . 
", " .
   926             "group_name = " . $this->db->quote($this->
getGroupName(), 
'text') . 
", " .
   927             "group_attribute = " . $this->db->quote($this->
getGroupAttribute(), 
'text') . 
", " .
   929             "group_user_filter = " . $this->db->quote($this->
getGroupUserFilter(), 
'text') . 
", " .
   930             "sync_on_login = " . $this->db->quote(($this->
enabledSyncOnLogin() ? 1 : 0), 
'integer') . 
", " .
   931             "sync_per_cron = " . $this->db->quote(($this->
enabledSyncPerCron() ? 1 : 0), 
'integer') . 
", " .
   933             "role_bind_dn = " . $this->db->quote($this->
getRoleBindDN(), 
'text') . 
", " .
   938             ", username_filter = " . $this->db->quote($this->
getUsernameFilter(), 
"text") . 
" " .
   939             ", escape_dn = " . $this->db->quote($this->
enabledEscapeDN() ? 1 : 0, 
'integer') . 
" " .
   940             "WHERE server_id = " . $this->db->quote($this->
getServerId(), 
'integer');
   942         $this->db->manipulate($query);
   949     public function delete(): 
void   960         foreach ($rules as $ruleAssigment) {
   961             $ruleAssigment->delete();
   966         $query = 
"DELETE FROM ldap_server_settings " .
   967             "WHERE server_id = " . $this->db->quote($this->
getServerId(), 
'integer');
   968         $this->db->manipulate($query);
   992             $options[
'userscope'] = 
'one';
   994             $options[
'userscope'] = 
'sub';
   998         $options[
'userfilter'] = $this->
getFilter();
  1003         $options[
'enableLogging'] = 
true;
  1006             case self::LDAP_SCOPE_BASE:
  1007                 $options[
'groupscope'] = 
'base';
  1009             case self::LDAP_SCOPE_ONE:
  1010                 $options[
'groupscope'] = 
'one';
  1013                 $options[
'groupscope'] = 
'sub';
  1033         $filter = trim($a_filter);
  1035         if ($filter === 
'') {
  1039         if (strpos($filter, 
'(') !== 0) {
  1042         if (substr($filter, -1) !== 
')') {
  1057                 $mapping->getFields(),
  1072         if (!$this->server_id) {
  1075         $query = 
"SELECT * FROM ldap_server_settings WHERE server_id = " . $this->db->quote($this->server_id, 
ilDBConstants::T_INTEGER);
  1077         $res = $this->db->query($query);
  1080             $this->
setName($row->name ?? 
'');
  1081             $this->
setUrl($row->url ?? 
'');
 static checkLDAPLib()
Check if ldap module is installed. 
 
static _getFirstActiveServer()
Get first active server. 
 
static disableDataSourceForAuthMode(int $a_authmode)
Disable data source. 
 
toggleMembershipOptional(bool $a_status)
 
read()
Read server settings. 
 
setBindingType(int $a_type)
 
static getDataSource(int $a_auth_mode)
 
static getAvailableDataSources(int $a_auth_mode)
 
setAuthenticationMapping(int $a_map)
Set mapped authentication mapping. 
 
enabledRoleSynchronization()
 
static getLogger(string $a_component_id)
Get component logger. 
 
static _getServerList()
Get list of all configured servers. 
 
enableAuthentication(bool $a_status)
Enable authentication for this ldap server. 
 
static getInstanceByServerId(int $a_server_id)
Get instance by server id. 
 
enableSyncPerCron(bool $a_value)
 
static _deleteByServerId(int $a_server_id)
 
static _getAllServer()
Get list of all configured servers. 
 
validate()
Validate user input. 
 
setFilter(string $a_filter)
 
static _getActiveServerList()
Get active server list. 
 
getPearAtributeArray()
Get attribute array for pear auth data. 
 
static toggleDataSource(int $a_ldap_server_id, int $a_auth_mode, int $a_status)
Toggle Data Source. 
 
static _getAuthModeName($a_auth_key)
 
static getAttributeNames($a_server_id)
get all possible attribute names 
 
setGroupMember(string $a_value)
 
static _getRules($a_server_id)
Get all rules. 
 
setGroupAttribute(string $a_value)
 
setGroupName(string $a_value)
 
toggleReferrer(bool $a_status)
 
static getServerIdByAuthMode(string $a_auth_mode)
Get auth id by auth mode. 
 
static isDataSourceActive(int $a_auth_mode)
Check if a data source is active for a specific auth mode. 
 
const DEFAULT_NETWORK_TIMEOUT
 
const LDAP_BIND_ANONYMOUS
 
getGroupNames()
Get group names as array. 
 
static getKeyByAuthMode(string $a_auth_mode)
Get auth id by auth mode. 
 
static getServerIds()
Get all server ids. 
 
getAuthenticationMapping()
Get authentication mode that is mapped. 
 
doConnectionCheck()
Check ldap connection and do a fallback to the next server if no connection is possible. 
 
toggleActive(bool $a_status)
 
enableEscapeDN(bool $a_value)
 
static _getCronServerIds()
Get list of acticve servers with option 'SyncCron'. 
 
bool $enabled_authentication
 
enableSyncOnLogin(bool $a_value)
 
setGroupUserFilter(string $a_filter)
 
__construct(int $a_server_id=0)
 
toPearAuthArray()
Creates an array of options compatible to PEAR Auth. 
 
setUserAttribute(string $a_user_attr)
 
isAuthenticationEnabled()
Check if authentication is enabled. 
 
setVersion(int $a_version)
 
int $authentication_mapping
 
enableGroupMemberIsDN(bool $a_value)
 
enableRoleSynchronization(bool $a_value)
 
string $group_user_filter
 
setBindPassword(string $a_password)
 
toggleTLS(bool $a_status)
 
setRoleBindDN(string $a_value)
 
setGroupFilter(string $a_value)
 
setGlobalRole(int $a_role)
 
setBindUser(string $a_user)
 
static isAuthModeLDAP(string $a_auth_mode)
Check if user auth mode is LDAP. 
 
setGroupDN(string $a_value)
 
setGroupScope(int $a_value)
 
rotateFallbacks()
Rotate fallback urls in case of connect timeouts. 
 
setUserScope(int $a_value)
 
setSearchBase(string $a_search_base)
 
getAuthenticationMappingKey()
Get authentication mapping key Default is ldap. 
 
setUsernameFilter(string $a_value)
 
static _delete(int $a_server_id)
 
filter(string $filter_id, $class_path, string $cmd, bool $activated=true, bool $expanded=true)
 
setRoleBindPassword(string $a_value)
 
static _getInstanceByServerId(int $a_server_id)
 
setBaseDN(string $a_base_dn)
 
static _getRoleSyncServerIds()
Check whether there if there is an active server with option role_sync_active. 
 
static getAuthModeByKey(string $a_auth_key)
get auth mode by key 
 
enableAccountMigration(bool $a_status)
Enable account migration. 
 
prepareFilter(string $a_filter)
Create brackets for filters if they do not exist. 
 
isAccountMigrationEnabled()
enabled account migration