65 function Ldap($h=null, $p=null, $pass=null, $bdn=
'')
68 if (!is_null($h) && !empty($h)) {
69 $this->
connect($h, $p, $pass, $bdn);
70 $this->
bind($bdn, $pass);
88 if (!extension_loaded(
'ldap')) {
93 if ($h != $this->host || $p != $this->port) {
95 if ($this->ptr) ldap_close($this->ptr);
97 if (!($this->ptr = ldap_connect($h, $p))) {
98 trigger_error(
'Unable to connect to LDAP server: '.$h.
' on port '.$p, E_USER_WARNING);
118 function bind($bdn=
'', $pass=null)
120 if (!$this->ptr)
return false;
123 if (is_null($pass)) {
125 if (!($r = ldap_bind($this->ptr))) {
126 trigger_error(
'Unable to Anonomously bind to LDAP server: '.$this->host, E_USER_WARNING);
131 if (!($r = ldap_bind($this->ptr, $bdn, $pass))) {
132 trigger_error(
'Unable to bind to LDAP server: '.$this->host.
' BDN: '.$bdn.
' Using Password: '.(($pass) ?
'YES' :
'NO'), E_USER_WARNING);
152 return ldap_close($this->ptr);
170 $conn_result = $this->
connect($this->host, $this->port);
171 if (!$conn_result)
return false;
172 return $this->
bind($bdn, $pass);
189 function search($startdn, $filter, $sort_by=null, $multi_level=
true, $multi_result=
true, $attributes=Array())
191 if (!$this->ptr)
return 0;
193 if(!empty($attributes)) {
194 if ($multi_level && $multi_result) $sr = @ldap_search($this->ptr, $startdn, $filter, $attributes);
195 else if ($multi_result) $sr = @ldap_list($this->ptr, $startdn, $filter, $attributes);
196 else $sr = @ldap_read($this->ptr, $startdn, $filter, $attributes);
198 if ($multi_level && $multi_result) $sr = @ldap_search($this->ptr, $startdn, $filter);
199 else if ($multi_result) $sr = @ldap_list($this->ptr, $startdn, $filter);
200 else $sr = @ldap_read($this->ptr, $startdn, $filter);
206 if (!is_null($sort_by)) ldap_sort($this->ptr, $sr, $sort_by);
224 if (!($info = ldap_get_entries($this->ptr, $result))) {
225 trigger_error(
'Unable to get entries for LDAP search result ['.$result.
'] :'.ldap_error($this->ptr), E_USER_WARNING);
230 if (!empty($binary_attributes) && ($info[
'count'] > 0)) {
233 $entry_order = Array();
234 for ($i = 0; $i < $info[
'count']; $i++) {
235 $entry_order[$info[$i][
'dn']] = $i;
238 for ($entry_identifier = ldap_first_entry($this->ptr, $result); $entry_identifier !== FALSE; $entry_identifier = ldap_next_entry($this->ptr, $entry_identifier)) {
239 $dn = ldap_get_dn($this->ptr, $entry_identifier);
240 if (($dn !== FALSE) && isset($entry_order[$dn])) {
241 $entry_index = $entry_order[$dn];
242 foreach ($binary_attributes as $attribute) {
244 $lowercased_attribute = strtolower($attribute);
247 if (isset($info[$entry_index][$lowercased_attribute])) {
249 $binary_value = @ldap_get_values_len($this->ptr, $entry_identifier, $attribute);
250 if ($binary_value !== FALSE) {
251 $info[$entry_index][$lowercased_attribute] = $binary_value;
275 if (!$result)
return 0;
276 return ldap_count_entries($this->ptr, $result);
292 if (!ldap_add($this->ptr, $dn, $details)) {
293 trigger_error(
'Unable to INSERT entry into LDAP at DN: ['.$dn.
'] '.ldap_error($this->ptr), E_USER_WARNING);
312 if (!ldap_modify($this->ptr, $dn, $details)) {
313 trigger_error(
'Unable to MODIFY entry in LDAP at DN: ['.$dn.
'] '.ldap_error($this->ptr), E_USER_WARNING);
330 if (!ldap_delete($this->ptr, $dn)) {
331 trigger_error(
'Unable to DELETE entry from LDAP at DN: ['.$dn.
'] '.ldap_error($this->ptr), E_USER_WARNING);
348 $metaChars = array(
'\\',
'(',
')',
'*');
349 $quotedMetaChars = array();
350 foreach ($metaChars as $key => $value) $quotedMetaChars[$key] =
'\\'.dechex(ord($value));
351 $str=str_replace($metaChars,$quotedMetaChars,$str);