1: <?php
2: namespace Mapbender\Component;
3:
4: /**
5: * Copyright (C) 2011 Wheregroup
6: *
7: * This program is free software; you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as published by
9: * the Free Software Foundation; either version 2, or (at your option)
10: * any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: *
21: * v0.2
22: */
23:
24: class Ldap {
25: private $conn;
26: private $error = null;
27: private $protocol_version;
28:
29: public function __construct($server, $port, $protocol_version) {
30: $this->protocol_version = $protocol_version;
31:
32: $this->conn = ldap_connect($server, $port);
33: if($this->conn) {
34: return true;
35: }
36:
37: $this->error = "The LDAP server is not reachable.";
38: return false;
39: }
40:
41: public function bind($username = null, $password = null) {
42: if(!$this->conn) return false;
43:
44: ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, $this->protocol_version);
45:
46: if(ldap_bind($this->conn, $username, $password)) {
47:
48: return true;
49: }
50:
51: $this->error = "Cannot bind to the LDAP server.";
52: return false;
53: }
54:
55: public function search($base_dn, $filter) {
56: if(!$this->conn) return false;
57:
58: return ldap_get_entries(
59: $this->conn,
60: ldap_search($this->conn, $base_dn, $filter)
61: );
62: }
63:
64:
65: public function lastError($return = false) {
66: if($return) {
67: return $this->error;
68: }
69: echo $this->error;
70: }
71:
72: public function hasError() {
73: if(is_null($this->error)) {
74: return false;
75: }
76: return true;
77: }
78: }
79: