update some classes
[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 * connect to a ldap server
30 *
31 * @param string $server
32 * @param integer $port
33 * @param string $dn
34 * @return bool true/false
35 */
36 public function connect ($server, $port, $dn) {
37 $this->ldap = ldap_connect($server, $port);
38 $this->dn = $dn;
39
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 }
45 else {
46 throw new Exception("Cant connect to ldap server.");
47 }
48
49 return false;
50 }
51
52 /**
53 * returns ldap user array
54 *
55 * @param string $user
56 * @param string $password
57 * @return array
58 */
59 public function bind ($user, $password) {
60 return ldap_bind($this->ldap, $user.",".$this->dn, $password);
61 }
62
63 /**
64 * search user on ldap server
65 *
66 * @param string $search
67 * @return resource
68 */
69 public function search ($search) {
70 return ldap_search($this->ldap, $this->dn, $search);
71 }
72
73 /**
74 * get entries from search resource
75 *
76 * @param resource $resource
77 * @return array
78 */
79 public function get_entries ($resource) {
80 return ldap_get_entries($this->ldap, $resource);
81 }
82
83 /**
84 * close ldap connection
85 */
86 public function close () {
87 ldap_close($this->ldap);
88 }
89 }
90 ?>