some code styleup
[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 * @var object
12 */
13 protected $ldap = Null;
14
15 /**
16 * LDAP DN
17 * @var string
18 */
19 protected $dn = '';
20
21 /**
22 * Constructs a new instance of LDAP class.
23 */
24 public function __construct () {
25 if (!extension_loaded("ldap")) {
26 throw new Exception("Can not find LDAP extension.");
27 }
28 }
29
30 /**
31 * connect to a ldap server
32 *
33 * @param string $server
34 * @param integer $port
35 * @param string $dn
36 * @return bool true/false
37 */
38 public function connect ($server, $port, $dn) {
39 $this->ldap = ldap_connect($server, $port);
40 $this->dn = $dn;
41
42 if ($this->ldap) {
43 ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
44 ldap_set_option($this->ldap, LDAP_OPT_REFERRALS, 0);
45 return true;
46 }
47 else {
48 throw new Exception("Cant connect to ldap server.");
49 }
50
51 return false;
52 }
53
54 /**
55 * returns ldap user array
56 *
57 * @param string $user
58 * @param string $password
59 * @return array
60 */
61 public function bind ($user, $password) {
62 return ldap_bind($this->ldap, $user.",".$this->dn, $password);
63 }
64
65 /**
66 * search user on ldap server
67 *
68 * @param string $search
69 * @return resource
70 */
71 public function search ($search) {
72 return ldap_search($this->ldap, $this->dn, $search);
73 }
74
75 /**
76 * get entries from search resource
77 *
78 * @param resource $resource
79 * @return array
80 */
81 public function get_entries ($resource) {
82 return ldap_get_entries($this->ldap, $resource);
83 }
84
85 /**
86 * close ldap connection
87 */
88 public function close () {
89 ldap_close($this->ldap);
90 }
91 }
92 ?>