10 defined(
'JPATH_PLATFORM') or die;
31 public $auth_method = null;
43 public $base_dn = null;
49 public $users_dn = null;
55 public $search_string = null;
61 public $use_ldapV3 = null;
67 public $no_referrals = null;
73 public $negotiate_tls = null;
79 public $username = null;
86 public $password = null;
92 private $_resource = null;
108 public function __construct($configObj = null)
110 if (is_object($configObj))
112 $vars = get_class_vars(get_class($this));
114 foreach (array_keys($vars) as $var)
116 if (substr($var, 0, 1) !=
'_')
118 $param = $configObj->get($var);
122 $this->$var = $param;
136 public function connect()
138 if ($this->host ==
'')
143 $this->_resource = @ ldap_connect($this->host, $this->port);
145 if ($this->_resource)
147 if ($this->use_ldapV3)
149 if (!@ldap_set_option($this->_resource, LDAP_OPT_PROTOCOL_VERSION, 3))
155 if (!@ldap_set_option($this->_resource, LDAP_OPT_REFERRALS, (
int) $this->no_referrals))
160 if ($this->negotiate_tls)
162 if (!@ldap_start_tls($this->_resource))
183 public function close()
185 @ ldap_close($this->_resource);
198 public function setDN($username, $nosub = 0)
200 if ($this->users_dn ==
'' || $nosub)
202 $this->_dn = $username;
204 elseif (strlen($username))
206 $this->_dn = str_replace(
'[username]', $username, $this->users_dn);
221 public function getDN()
233 public function anonymous_bind()
235 $bindResult = @ldap_bind($this->_resource);
251 public function bind($username = null, $password = null, $nosub = 0)
253 if (is_null($username))
255 $username = $this->username;
258 if (is_null($password))
260 $password = $this->password;
263 $this->setDN($username, $nosub);
264 $bindResult = @ldap_bind($this->_resource, $this->getDN(), $password);
278 public function simple_search($search)
280 $results = explode(
';', $search);
282 foreach ($results as $key => $result)
284 $results[$key] =
'(' . $result .
')';
287 return $this->search($results);
301 public function search(array $filters, $dnoverride = null, array $attributes = array())
311 $dn = $this->base_dn;
314 $resource = $this->_resource;
316 foreach ($filters as $search_filter)
318 $search_result = @ldap_search($resource, $dn, $search_filter, $attributes);
320 if ($search_result && ($count = @ldap_count_entries($resource, $search_result)) > 0)
322 for ($i = 0; $i < $count; $i++)
324 $result[$i] = array();
328 $firstentry = @ldap_first_entry($resource, $search_result);
332 $firstentry = @ldap_next_entry($resource, $firstentry);
336 $result_array = @ldap_get_attributes($resource, $firstentry);
339 foreach ($result_array as $ki => $ai)
343 $subcount = $ai[
'count'];
344 $result[$i][$ki] = array();
346 for ($k = 0; $k < $subcount; $k++)
348 $result[$i][$ki][$k] = $ai[$k];
353 $result[$i][
'dn'] = @ldap_get_dn($resource, $firstentry);
371 public function replace($dn, $attribute)
373 return @ldap_mod_replace($this->_resource, $dn, $attribute);
386 public function modify($dn, $attribute)
388 return @ldap_modify($this->_resource, $dn, $attribute);
401 public function remove($dn, $attribute)
403 $resource = $this->_resource;
405 return @ldap_mod_del($resource, $dn, $attribute);
419 public function compare($dn, $attribute, $value)
421 return @ldap_compare($this->_resource, $dn, $attribute, $value);
433 public function read($dn)
435 $base = substr($dn, strpos($dn,
',') + 1);
436 $cn = substr($dn, 0, strpos($dn,
','));
437 $result = @ldap_read($this->_resource, $base, $cn);
441 return @ldap_get_entries($this->_resource, $result);
458 public function delete($dn)
460 return @ldap_delete($this->_resource, $dn);
473 public function create($dn, array $entries)
475 return @ldap_add($this->_resource, $dn, $entries);
489 public function add($dn, array $entry)
491 return @ldap_mod_add($this->_resource, $dn, $entry);
506 public function rename($dn, $newdn, $newparent, $deleteolddn)
508 return @ldap_rename($this->_resource, $dn, $newdn, $newparent, $deleteolddn);
518 public function getErrorMsg()
520 return @ldap_error($this->_resource);
532 public static function ipToNetAddress($ip)
534 $parts = explode(
'.', $ip);
537 foreach ($parts as $int)
541 if (strlen($tmp) != 2)
546 $address .=
'\\' . $tmp;
576 public static function LDAPNetAddr($networkaddress)
579 $addrtype = (int) substr($networkaddress, 0, 1);
582 $networkaddress = substr($networkaddress, 2);
584 if (($addrtype == 8) || ($addrtype = 9))
587 $networkaddress = substr($networkaddress, (strlen($networkaddress) - 4));
606 $len = strlen($networkaddress);
610 for ($i = 0; $i < $len; $i++)
612 $byte = substr($networkaddress, $i, 1);
615 if (($addrtype == 1) || ($addrtype == 8) || ($addrtype = 9))
621 if (($addrtype == 1) || ($addrtype == 8) || ($addrtype = 9))
624 $addr = substr($addr, 0, strlen($addr) - 1);
629 $addr .=
JText::_(
'JLIB_CLIENT_ERROR_LDAP_ADDRESS_NOT_AVAILABLE');
631 return array(
'protocol' => $addrtypes[$addrtype],
'address' => $addr);
644 public static function generatePassword($password, $type =
'md5')
646 switch (strtolower($type))
649 $userpassword =
'{SHA}' . base64_encode(pack(
'H*', sha1($password)));
653 $userpassword =
'{MD5}' . base64_encode(pack(
'H*', md5($password)));
657 return $userpassword;