From: Stricted Date: Sun, 16 Feb 2014 23:56:52 +0000 (+0100) Subject: add ldap class X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=0efe1c122b937c771fa0d861f104e511ae2d3e95;p=Snippets.git add ldap class --- diff --git a/LDAP.class.php b/LDAP.class.php new file mode 100644 index 0000000..fbf4623 --- /dev/null +++ b/LDAP.class.php @@ -0,0 +1,88 @@ + + */ + +class LDAP { + /** + * LDAP resource id + */ + protected $ldap = Null; + + /** + * LDAP DN + */ + protected $dn = ''; + + /** + * Constructs a new instance of LDAP class. + */ + public function __construct () { + if(!extension_loaded("ldap")) { + throw new Exception("Can not find LDAP extension."); + } + + } + + /** + * connect to a ldap server + * + * @param string $server + * @param integer $port + * @param string $dn + * @return bool true/false + */ + public function connect ($server, $port, $dn) { + $this->ldap = ldap_connect($server, $port); + $this->dn = $dn; + if($this->ldap) { + ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3); + ldap_set_option($this->ldap, LDAP_OPT_REFERRALS, 0); + return true; + } else { + throw new Exception("Cant connect to ldap server."); + } + return false; + } + + /** + * returns ldap user array + * + * @param string $user + * @param string $password + * @return array + */ + public function bind ($user, $password) { + return ldap_bind($this->ldap, $user.",".$this->dn, $password); + } + + /** + * search user on ldap server + * + * @param string $search + * @return resource + */ + public function search ($search) { + return ldap_search($this->ldap, $this->dn, $search); + } + + /** + * get entries from search resource + * + * @param resource $resource + * @return array + */ + public function get_entries ($resource) { + return ldap_get_entries($this->ldap, $resource); + } + + /** + * close ldap connection + */ + public function close () { + ldap_close($this->ldap); + } +} +?> \ No newline at end of file