add ldap class
[Snippets.git] / LDAP.class.php
1 <?php
2 /**
3 * @author Jan Altensen (Stricted)
4 * @copyright 2013-2014 Jan Altensen (Stricted)
5 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
6 */
7
8 class LDAP {
9 /**
10 * LDAP resource id
11 */
12 protected $ldap = Null;
13
14 /**
15 * LDAP DN
16 */
17 protected $dn = '';
18
19 /**
20 * Constructs a new instance of LDAP class.
21 */
22 public function __construct () {
23 if(!extension_loaded("ldap")) {
24 throw new Exception("Can not find LDAP extension.");
25 }
26
27 }
28
29 /**
30 * connect to a ldap server
31 *
32 * @param string $server
33 * @param integer $port
34 * @param string $dn
35 * @return bool true/false
36 */
37 public function connect ($server, $port, $dn) {
38 $this->ldap = ldap_connect($server, $port);
39 $this->dn = $dn;
40 if($this->ldap) {
41 ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
42 ldap_set_option($this->ldap, LDAP_OPT_REFERRALS, 0);
43 return true;
44 } else {
45 throw new Exception("Cant connect to ldap server.");
46 }
47 return false;
48 }
49
50 /**
51 * returns ldap user array
52 *
53 * @param string $user
54 * @param string $password
55 * @return array
56 */
57 public function bind ($user, $password) {
58 return ldap_bind($this->ldap, $user.",".$this->dn, $password);
59 }
60
61 /**
62 * search user on ldap server
63 *
64 * @param string $search
65 * @return resource
66 */
67 public function search ($search) {
68 return ldap_search($this->ldap, $this->dn, $search);
69 }
70
71 /**
72 * get entries from search resource
73 *
74 * @param resource $resource
75 * @return array
76 */
77 public function get_entries ($resource) {
78 return ldap_get_entries($this->ldap, $resource);
79 }
80
81 /**
82 * close ldap connection
83 */
84 public function close () {
85 ldap_close($this->ldap);
86 }
87 }
88 ?>