@@ -27,137 +27,137 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | class LDAPClient { |
| 29 | 29 | |
| 30 | - //host and port |
|
| 31 | - protected $host = ""; |
|
| 32 | - protected $port = 389; |
|
| 33 | - |
|
| 34 | - //flag, if uri is used instead of host |
|
| 35 | - protected $uri_used = false; |
|
| 36 | - protected $uri = ""; |
|
| 30 | + //host and port |
|
| 31 | + protected $host = ""; |
|
| 32 | + protected $port = 389; |
|
| 33 | + |
|
| 34 | + //flag, if uri is used instead of host |
|
| 35 | + protected $uri_used = false; |
|
| 36 | + protected $uri = ""; |
|
| 37 | 37 | |
| 38 | - protected $conn = null; |
|
| 39 | - protected $res = null; |
|
| 38 | + protected $conn = null; |
|
| 39 | + protected $res = null; |
|
| 40 | 40 | |
| 41 | - protected $dn = ""; |
|
| 42 | - |
|
| 43 | - protected $ldap_config = array(); |
|
| 44 | - |
|
| 45 | - //flag, if connection is readonly |
|
| 46 | - protected $readonly = false; |
|
| 47 | - |
|
| 48 | - public function __construct (string $host = "", int $port = 0, bool $ssl = false) { |
|
| 49 | - $ldap_config = array( |
|
| 50 | - 'enabled' => true, |
|
| 51 | - 'ssl' => $ssl |
|
| 52 | - ); |
|
| 53 | - |
|
| 54 | - if (empty($host)) { |
|
| 55 | - //load local config |
|
| 56 | - if (!file_exists(CONFIG_PATH . "ldap.php")) { |
|
| 57 | - throw new IllegalStateException("No ldap configuration file config/ldap.php exists!"); |
|
| 58 | - } |
|
| 41 | + protected $dn = ""; |
|
| 42 | + |
|
| 43 | + protected $ldap_config = array(); |
|
| 44 | + |
|
| 45 | + //flag, if connection is readonly |
|
| 46 | + protected $readonly = false; |
|
| 47 | + |
|
| 48 | + public function __construct (string $host = "", int $port = 0, bool $ssl = false) { |
|
| 49 | + $ldap_config = array( |
|
| 50 | + 'enabled' => true, |
|
| 51 | + 'ssl' => $ssl |
|
| 52 | + ); |
|
| 53 | + |
|
| 54 | + if (empty($host)) { |
|
| 55 | + //load local config |
|
| 56 | + if (!file_exists(CONFIG_PATH . "ldap.php")) { |
|
| 57 | + throw new IllegalStateException("No ldap configuration file config/ldap.php exists!"); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - //override $ldap_config |
|
| 61 | - require(CONFIG_PATH . "ldap.php"); |
|
| 60 | + //override $ldap_config |
|
| 61 | + require(CONFIG_PATH . "ldap.php"); |
|
| 62 | 62 | |
| 63 | - //check, if ldap is enabled |
|
| 64 | - if ($ldap_config['enabled'] == false) { |
|
| 65 | - throw new IllegalStateException("LDAP is disabled. Enable ldap in file config/ldap.php ."); |
|
| 66 | - } |
|
| 63 | + //check, if ldap is enabled |
|
| 64 | + if ($ldap_config['enabled'] == false) { |
|
| 65 | + throw new IllegalStateException("LDAP is disabled. Enable ldap in file config/ldap.php ."); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - $this->host = $ldap_config['host']; |
|
| 69 | - $this->port = intval($ldap_config['port']); |
|
| 68 | + $this->host = $ldap_config['host']; |
|
| 69 | + $this->port = intval($ldap_config['port']); |
|
| 70 | 70 | |
| 71 | - $this->readonly = boolval($ldap_config['readonly']); |
|
| 72 | - } else { |
|
| 73 | - $this->host = $host; |
|
| 74 | - $this->port = $port; |
|
| 75 | - } |
|
| 71 | + $this->readonly = boolval($ldap_config['readonly']); |
|
| 72 | + } else { |
|
| 73 | + $this->host = $host; |
|
| 74 | + $this->port = $port; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - if (isset($this->ldap_config['use_uri']) && $this->ldap_config['use_uri']) { |
|
| 78 | - $this->uri = "ldap://" . $this->host . ":" . $this->port; |
|
| 77 | + if (isset($this->ldap_config['use_uri']) && $this->ldap_config['use_uri']) { |
|
| 78 | + $this->uri = "ldap://" . $this->host . ":" . $this->port; |
|
| 79 | 79 | |
| 80 | - //set flag, that uri is used |
|
| 81 | - $this->uri_used = true; |
|
| 82 | - } |
|
| 80 | + //set flag, that uri is used |
|
| 81 | + $this->uri_used = true; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - //check, if SSL is enabled |
|
| 85 | - if ($ldap_config['ssl'] == true) { |
|
| 86 | - //use OpenLDAP 2.x.x URI instead of host |
|
| 87 | - $this->uri = "ldaps://" . $this->host . ":" . $this->port; |
|
| 84 | + //check, if SSL is enabled |
|
| 85 | + if ($ldap_config['ssl'] == true) { |
|
| 86 | + //use OpenLDAP 2.x.x URI instead of host |
|
| 87 | + $this->uri = "ldaps://" . $this->host . ":" . $this->port; |
|
| 88 | 88 | |
| 89 | - //set flag, that uri is used |
|
| 90 | - $this->uri_used = true; |
|
| 91 | - } |
|
| 89 | + //set flag, that uri is used |
|
| 90 | + $this->uri_used = true; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - //check, if host / uri is valide (this statement doesnt connect to server!) - see also http://php.net/manual/de/function.ldap-connect.php |
|
| 94 | - if ($this->uri_used) { |
|
| 95 | - $this->conn = ldap_connect($this->uri); |
|
| 96 | - } else { |
|
| 97 | - $this->conn = ldap_connect($this->host, $this->port); |
|
| 98 | - } |
|
| 93 | + //check, if host / uri is valide (this statement doesnt connect to server!) - see also http://php.net/manual/de/function.ldap-connect.php |
|
| 94 | + if ($this->uri_used) { |
|
| 95 | + $this->conn = ldap_connect($this->uri); |
|
| 96 | + } else { |
|
| 97 | + $this->conn = ldap_connect($this->host, $this->port); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - if ($this->conn === FALSE) { |
|
| 101 | - $error_str = ($this->uri_used ? "URI: " . $this->uri : " Host: " . $this->host . ", port: " . $this->port); |
|
| 100 | + if ($this->conn === FALSE) { |
|
| 101 | + $error_str = ($this->uri_used ? "URI: " . $this->uri : " Host: " . $this->host . ", port: " . $this->port); |
|
| 102 | 102 | |
| 103 | - throw new IllegalStateException("LDAP connection parameters (host or port) are invalide." . (DEBUG_MODE ? " " . $error_str : "")); |
|
| 104 | - } |
|
| 103 | + throw new IllegalStateException("LDAP connection parameters (host or port) are invalide." . (DEBUG_MODE ? " " . $error_str : "")); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - $this->dn = $ldap_config['dn']; |
|
| 106 | + $this->dn = $ldap_config['dn']; |
|
| 107 | 107 | |
| 108 | - //set ldap params |
|
| 109 | - if (isset($ldap_config['params'])) { |
|
| 110 | - foreach ($ldap_config['params'] as $key=>$value) { |
|
| 111 | - // configure ldap params |
|
| 112 | - ldap_set_option($this->conn,$key, $value); |
|
| 113 | - } |
|
| 114 | - } |
|
| 108 | + //set ldap params |
|
| 109 | + if (isset($ldap_config['params'])) { |
|
| 110 | + foreach ($ldap_config['params'] as $key=>$value) { |
|
| 111 | + // configure ldap params |
|
| 112 | + ldap_set_option($this->conn,$key, $value); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - $this->ldap_config = $ldap_config; |
|
| 117 | - } |
|
| 116 | + $this->ldap_config = $ldap_config; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - public function bind (string $username = null, string $password = null) : bool { |
|
| 120 | - if (is_null($username) && isset($this->ldap_config['user'])) { |
|
| 121 | - $username = $this->ldap_config['user']; |
|
| 122 | - $password = $this->ldap_config['password']; |
|
| 123 | - } |
|
| 119 | + public function bind (string $username = null, string $password = null) : bool { |
|
| 120 | + if (is_null($username) && isset($this->ldap_config['user'])) { |
|
| 121 | + $username = $this->ldap_config['user']; |
|
| 122 | + $password = $this->ldap_config['password']; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - $ldap_usr_dom = ""; |
|
| 126 | - $ldap_usr_prefix = (isset($this->ldap_config['user_prefix']) ? $this->ldap_config['user_prefix'] : ""); |
|
| 125 | + $ldap_usr_dom = ""; |
|
| 126 | + $ldap_usr_prefix = (isset($this->ldap_config['user_prefix']) ? $this->ldap_config['user_prefix'] : ""); |
|
| 127 | 127 | |
| 128 | - if (isset($this->ldap_config['ldap_usr_dom'])) { |
|
| 129 | - $ldap_usr_dom = $this->ldap_config['ldap_usr_dom']; |
|
| 130 | - } |
|
| 128 | + if (isset($this->ldap_config['ldap_usr_dom'])) { |
|
| 129 | + $ldap_usr_dom = $this->ldap_config['ldap_usr_dom']; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - if ($this->conn === FALSE) { |
|
| 133 | - throw new IllegalStateException("ldap connection check failed."); |
|
| 134 | - } |
|
| 132 | + if ($this->conn === FALSE) { |
|
| 133 | + throw new IllegalStateException("ldap connection check failed."); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - //http://www.selfadsi.de/ads-attributes/user-sAMAccountName.htm |
|
| 136 | + //http://www.selfadsi.de/ads-attributes/user-sAMAccountName.htm |
|
| 137 | 137 | |
| 138 | - //connect and bind to ldap server |
|
| 139 | - if (!is_null($username)) { |
|
| 140 | - //with authentification |
|
| 141 | - $this->res = @ldap_bind($this->conn, $ldap_usr_prefix . $username . $ldap_usr_dom, $password); |
|
| 142 | - } else { |
|
| 143 | - //anonymous binding |
|
| 144 | - $this->res = @ldap_bind($this->conn); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return $this->res !== FALSE; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - public function listGroupsOfUser (string $user) : array { |
|
| 151 | - // check presence in groups |
|
| 152 | - //$filter = "(sAMAccountName=" . $user . ")"; |
|
| 153 | - $filter = "(uid=" . $user . ")"; |
|
| 154 | - $attr = array("memberof"); |
|
| 155 | - |
|
| 156 | - //https://samjlevy.com/php-ldap-login/ |
|
| 157 | - |
|
| 158 | - $result = ldap_search($this->conn, $this->dn, $filter, $attr) or exit("Unable to search LDAP server"); |
|
| 138 | + //connect and bind to ldap server |
|
| 139 | + if (!is_null($username)) { |
|
| 140 | + //with authentification |
|
| 141 | + $this->res = @ldap_bind($this->conn, $ldap_usr_prefix . $username . $ldap_usr_dom, $password); |
|
| 142 | + } else { |
|
| 143 | + //anonymous binding |
|
| 144 | + $this->res = @ldap_bind($this->conn); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return $this->res !== FALSE; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + public function listGroupsOfUser (string $user) : array { |
|
| 151 | + // check presence in groups |
|
| 152 | + //$filter = "(sAMAccountName=" . $user . ")"; |
|
| 153 | + $filter = "(uid=" . $user . ")"; |
|
| 154 | + $attr = array("memberof"); |
|
| 155 | + |
|
| 156 | + //https://samjlevy.com/php-ldap-login/ |
|
| 157 | + |
|
| 158 | + $result = ldap_search($this->conn, $this->dn, $filter, $attr) or exit("Unable to search LDAP server"); |
|
| 159 | 159 | |
| 160 | - /* |
|
| 160 | + /* |
|
| 161 | 161 | *return_value["count"] = number of entries in the result |
| 162 | 162 | * return_value[0] : refers to the details of first entry |
| 163 | 163 | * return_value[i]["dn"] = DN of the ith entry in the result |
@@ -167,9 +167,9 @@ discard block |
||
| 167 | 167 | * attribute in ith entry |
| 168 | 168 | * return_value[i]["attribute"][j] = jth value of attribute in ith entry |
| 169 | 169 | */ |
| 170 | - $entries = ldap_get_entries($this->conn, $result); |
|
| 170 | + $entries = ldap_get_entries($this->conn, $result); |
|
| 171 | 171 | |
| 172 | - /*$array = array(); |
|
| 172 | + /*$array = array(); |
|
| 173 | 173 | |
| 174 | 174 | $count = intval($entries['count']); |
| 175 | 175 | |
@@ -183,57 +183,57 @@ discard block |
||
| 183 | 183 | ); |
| 184 | 184 | }*/ |
| 185 | 185 | |
| 186 | - //https://stackoverflow.com/questions/7187994/memberof-vs-groupmembership-in-ldap-liferay |
|
| 186 | + //https://stackoverflow.com/questions/7187994/memberof-vs-groupmembership-in-ldap-liferay |
|
| 187 | 187 | |
| 188 | - $groups = array(); |
|
| 188 | + $groups = array(); |
|
| 189 | 189 | |
| 190 | - //print_r($entries); |
|
| 190 | + //print_r($entries); |
|
| 191 | 191 | |
| 192 | - if (!isset($entries[0]) || !isset($entries[0]['memberof'])) { |
|
| 193 | - //ldap server doesnt contains information about user groups |
|
| 194 | - return $groups; |
|
| 195 | - } |
|
| 192 | + if (!isset($entries[0]) || !isset($entries[0]['memberof'])) { |
|
| 193 | + //ldap server doesnt contains information about user groups |
|
| 194 | + return $groups; |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - foreach($entries[0]['memberof'] as $grps) { |
|
| 198 | - $groups[] = $grps; |
|
| 199 | - } |
|
| 197 | + foreach($entries[0]['memberof'] as $grps) { |
|
| 198 | + $groups[] = $grps; |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | - /* |
|
| 201 | + /* |
|
| 202 | 202 | * isMemberOf: cn=Dynamic Home Directories,ou=groups,dc=example,dc=com |
| 203 | 203 | * isMemberOf: cn=bellevue,ou=groups,dc=example,dc=com |
| 204 | 204 | * isMemberOf: cn=shadow entries,ou=groups,dc=example,dc=com |
| 205 | 205 | * isMemberOf: cn=persons,ou=groups,dc=example,dc=com |
| 206 | 206 | */ |
| 207 | 207 | |
| 208 | - return $groups; |
|
| 209 | - } |
|
| 208 | + return $groups; |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | - public function listAllAttributesOfUser (string $user) : array { |
|
| 212 | - $filter = "(uid=" . $user . ")"; |
|
| 211 | + public function listAllAttributesOfUser (string $user) : array { |
|
| 212 | + $filter = "(uid=" . $user . ")"; |
|
| 213 | 213 | |
| 214 | - $result = ldap_search($this->conn, $this->dn, $filter/*, $attr*/) or exit("Unable to search LDAP server"); |
|
| 214 | + $result = ldap_search($this->conn, $this->dn, $filter/*, $attr*/) or exit("Unable to search LDAP server"); |
|
| 215 | 215 | |
| 216 | - $entries = ldap_get_entries($this->conn, $result); |
|
| 216 | + $entries = ldap_get_entries($this->conn, $result); |
|
| 217 | 217 | |
| 218 | - if (count($entries) == 0) { |
|
| 219 | - //no user found in ldap server |
|
| 218 | + if (count($entries) == 0) { |
|
| 219 | + //no user found in ldap server |
|
| 220 | 220 | |
| 221 | - //throw new IllegalStateException("user (uid=" . $user . ") not found in ldap server."); |
|
| 221 | + //throw new IllegalStateException("user (uid=" . $user . ") not found in ldap server."); |
|
| 222 | 222 | |
| 223 | - return array(); |
|
| 224 | - } |
|
| 223 | + return array(); |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - return $entries[0]; |
|
| 227 | - } |
|
| 226 | + return $entries[0]; |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - public function unbind () { |
|
| 230 | - //disconnect from ldap server |
|
| 231 | - ldap_unbind($this->conn); |
|
| 232 | - } |
|
| 229 | + public function unbind () { |
|
| 230 | + //disconnect from ldap server |
|
| 231 | + ldap_unbind($this->conn); |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - public function getConnection () { |
|
| 235 | - return $this->conn; |
|
| 236 | - } |
|
| 234 | + public function getConnection () { |
|
| 235 | + return $this->conn; |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | 238 | } |
| 239 | 239 | |