* @package Mammut\LDAP */ class LDAP extends \Mammut\StrictObject { private $server = NULL; private $port = NULL; private $secure = false; private $con = NULL; private $bind = NULL; private $basedn = ''; public function __construct($server = NULL, $port = 389, $secure = false) { if(!extension_loaded('ldap')) throw new \Mammut\Exception\ExtensionException('ldap'); if(!empty($server)) $this->init($server, $port, $secure); } public function init($server, $port = 389, $secure = false) { $this->server = $server; $this->port = $port; $this->secure = $secure; } /** * Connects to the LDAP server * * @param string $usr * username * @param string $pw * password */ public function connect($usr = NULL, $pw = NULL) { if(!is_null($this->con)) throw new \BadMethodCallException('already connected'); $this->con = ldap_connect($this->server, $this->port); if($usr == NULL) { $this->bind = @ldap_bind($this->con); if(!$this->bind) throw new LDAPException(@ldap_error($this->con), @ldap_errno($this->con)); } elseif($pw == NULL) { $this->bind = @ldap_bind($this->con, $usr); if(!$this->bind) throw new AuthenticationException(@ldap_error($this->con), @ldap_errno($this->con)); } else { $this->bind = @ldap_bind($this->con, $usr, $pw); if(!$this->bind) throw new AuthenticationException(@ldap_error($this->con), @ldap_errno($this->con)); } } public function disconnect() { if(!is_null($this->con)) return ldap_unbind($this->con); } public function setOption($option, $value) { ldap_set_option($this->con, $option, $value); } public function getOption($option) { return ldap_get_option($this->con); } public function setBaseDN($dn) { $this->basedn = $dn; } public function getBaseDN() { return $this->basedn; } /** * * @param string $baseDN * @param string $filter * @param string $fields * @return LDAPResult */ public function search($baseDN, $filter, $fields = NULL) { if(is_null($fields)) $sr = ldap_search($this->con, $baseDN, $filter); else $sr = ldap_search($this->con, $baseDN, $filter, $fields); if (is_bool($sr)) return $sr; return new LDAPResult($this->con, $sr); } }