commit | author | age
|
d1d2c4
|
1 |
<?php |
S |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| program/include/rcube_ldap.inc | |
|
6 |
| | |
|
7 |
| This file is part of the RoundCube Webmail client | |
|
8 |
| Copyright (C) 2005, RoundCube Dev. - Switzerland | |
|
9 |
| Licensed under the GNU GPL | |
|
10 |
| | |
|
11 |
| PURPOSE: | |
|
12 |
| Manage an LDAP connection | |
|
13 |
| | |
|
14 |
+-----------------------------------------------------------------------+ |
|
15 |
| Author: Jeremy Jongsma <jeremy@jongsma.org> | |
|
16 |
+-----------------------------------------------------------------------+ |
|
17 |
|
|
18 |
$Id$ |
|
19 |
|
|
20 |
*/ |
|
21 |
|
|
22 |
require_once("bugs.inc"); |
|
23 |
|
|
24 |
class rcube_ldap |
|
25 |
{ |
|
26 |
var $conn; |
|
27 |
var $host; |
|
28 |
var $port; |
|
29 |
var $protocol; |
|
30 |
var $base_dn; |
|
31 |
var $bind_dn; |
|
32 |
var $bind_pass; |
|
33 |
|
|
34 |
// PHP 5 constructor |
|
35 |
function __construct() |
|
36 |
{ |
|
37 |
} |
|
38 |
|
|
39 |
// PHP 4 constructor |
|
40 |
function rcube_ldap() |
|
41 |
{ |
|
42 |
$this->__construct(); |
|
43 |
} |
|
44 |
|
|
45 |
function connect($hosts, $port=389, $protocol=3) |
|
46 |
{ |
|
47 |
if (!function_exists('ldap_connect')) |
|
48 |
raise_error(array("type" => "ldap", |
|
49 |
"message" => "No ldap support in this installation of php."), |
|
50 |
TRUE); |
|
51 |
|
|
52 |
if (is_resource($this->conn)) |
|
53 |
return TRUE; |
|
54 |
|
|
55 |
if (!is_array($hosts)) |
|
56 |
$hosts = array($hosts); |
|
57 |
|
|
58 |
foreach ($hosts as $host) |
|
59 |
{ |
|
60 |
if ($lc = @ldap_connect($host, $port)) |
|
61 |
{ |
|
62 |
@ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $protocol); |
|
63 |
$this->host = $host; |
|
64 |
$this->port = $port; |
|
65 |
$this->protocol = $protocol; |
|
66 |
$this->conn = $lc; |
|
67 |
return TRUE; |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
if (!is_resource($this->conn)) |
|
72 |
raise_error(array("type" => "ldap", |
|
73 |
"message" => "Could not connect to any LDAP server, tried $host:$port last"), |
|
74 |
TRUE); |
|
75 |
} |
|
76 |
|
|
77 |
function close() |
|
78 |
{ |
|
79 |
if ($this->conn) |
|
80 |
{ |
|
81 |
if (@ldap_unbind($this->conn)) |
|
82 |
return TRUE; |
|
83 |
else |
|
84 |
raise_error(array("code" => ldap_errno($this->conn), |
|
85 |
"type" => "ldap", |
|
86 |
"message" => "Could not close connection to LDAP server: ".ldap_error($this->conn)), |
|
87 |
TRUE); |
|
88 |
} |
|
89 |
return FALSE; |
|
90 |
} |
|
91 |
|
|
92 |
// Merge with connect()? |
|
93 |
function bind($dn=null, $pass=null) |
|
94 |
{ |
|
95 |
if ($this->conn) |
|
96 |
{ |
|
97 |
if ($dn) |
|
98 |
if (@ldap_bind($this->conn, $dn, $pass)) |
|
99 |
return TRUE; |
|
100 |
else |
|
101 |
raise_error(array("code" => ldap_errno($this->conn), |
|
102 |
"type" => "ldap", |
|
103 |
"message" => "Bind failed for dn=$dn: ".ldap_error($this->conn)), |
|
104 |
TRUE); |
|
105 |
else |
|
106 |
if (@ldap_bind($this->conn)) |
|
107 |
return TRUE; |
|
108 |
else |
|
109 |
raise_error(array("code" => ldap_errno($this->conn), |
|
110 |
"type" => "ldap", |
|
111 |
"message" => "Anonymous bind failed: ".ldap_error($this->conn)), |
|
112 |
TRUE); |
|
113 |
} |
|
114 |
else |
|
115 |
raise_error(array("type" => "ldap", |
|
116 |
"message" => "Attempted bind on nonexistent connection"), TRUE); |
|
117 |
return FALSE; |
|
118 |
} |
|
119 |
|
|
120 |
function count($base, $filter=null, $attributes=null, $scope="sub") |
|
121 |
{ |
|
122 |
if ($this->conn) |
|
123 |
{ |
|
124 |
if ($scope === 'sub') |
|
125 |
$sr = @ldap_search($this->conn, $base, $filter, $attributes, 0, $limit); |
|
126 |
else if ($scope === 'one') |
|
127 |
$sr = @ldap_list($this->conn, $base, $filter, $attributes, 0, $limit); |
|
128 |
else if ($scope === 'base') |
|
129 |
$sr = @ldap_read($this->conn, $base, $filter, $attributes, 0, $limit); |
|
130 |
if ($sr) |
|
131 |
return @ldap_count_entries($this->conn, $sr); |
|
132 |
} |
|
133 |
else |
|
134 |
raise_error(array("type" => "ldap", |
|
135 |
"message" => "Attempted count search on nonexistent connection"), TRUE); |
|
136 |
return FALSE; |
|
137 |
} |
|
138 |
|
|
139 |
function search($base, $filter=null, $attributes=null, $scope='sub', $sort=null, $limit=0) |
|
140 |
{ |
|
141 |
if ($this->conn) |
|
142 |
{ |
|
143 |
if ($scope === 'sub') |
|
144 |
$sr = @ldap_search($this->conn, $base, $filter, $attributes, 0, $limit); |
|
145 |
else if ($scope === 'one') |
|
146 |
$sr = @ldap_list($this->conn, $base, $filter, $attributes, 0, $limit); |
|
147 |
else if ($scope === 'base') |
|
148 |
$sr = @ldap_read($this->conn, $base, $filter, $attributes, 0, $limit); |
|
149 |
if ($sr) |
|
150 |
{ |
|
151 |
if ($sort && $scope !== "base") |
|
152 |
{ |
|
153 |
if (is_array($sort)) |
|
154 |
{ |
|
155 |
// Start from the end so first sort field has highest priority |
|
156 |
$sortfields = array_reverse($sort); |
|
157 |
foreach ($sortfields as $sortfield) |
|
158 |
@ldap_sort($this->conn, $sr, $sortfield); |
|
159 |
} |
|
160 |
else |
|
161 |
@ldap_sort($this->conn, $sr, $sort); |
|
162 |
} |
|
163 |
return @ldap_get_entries($this->conn, $sr); |
|
164 |
} |
|
165 |
} |
|
166 |
else |
|
167 |
raise_error(array("type" => "ldap", |
|
168 |
"message" => "Attempted search on nonexistent connection"), TRUE); |
|
169 |
return FALSE; |
|
170 |
} |
|
171 |
|
|
172 |
function add($dn, $object) |
|
173 |
{ |
|
174 |
if ($this->conn) |
|
175 |
{ |
|
176 |
if (@ldap_add($this->conn, $dn, $object)) |
|
177 |
return TRUE; |
|
178 |
else |
|
179 |
raise_error(array("code" => ldap_errno($this->conn), |
|
180 |
"type" => "ldap", |
|
181 |
"message" => "Add object failed: ".ldap_error($this->conn)), |
|
182 |
TRUE); |
|
183 |
} |
|
184 |
else |
|
185 |
raise_error(array("type" => "ldap", |
|
186 |
"message" => "Add object faile: no connection"), |
|
187 |
TRUE); |
|
188 |
return FALSE; |
|
189 |
} |
|
190 |
|
|
191 |
function modify($dn, $object) |
|
192 |
{ |
|
193 |
if ($this->conn) |
|
194 |
{ |
|
195 |
if (@ldap_modify($this->conn, $dn, $object)) |
|
196 |
return TRUE; |
|
197 |
else |
|
198 |
raise_error(array("code" => ldap_errno($this->conn), |
|
199 |
"type" => "ldap", |
|
200 |
"message" => "Modify object failed: ".ldap_error($this->conn)), |
|
201 |
TRUE); |
|
202 |
} |
|
203 |
else |
|
204 |
raise_error(array("type" => "ldap", |
|
205 |
"message" => "Modify object failed: no connection"), |
|
206 |
TRUE); |
|
207 |
return FALSE; |
|
208 |
} |
|
209 |
|
|
210 |
function rename($dn, $newrdn, $parentdn) |
|
211 |
{ |
|
212 |
if ($this->protocol < 3) |
|
213 |
{ |
|
214 |
raise_error(array("type" => "ldap", |
|
215 |
"message" => "rename() support requires LDAPv3 or above "), |
|
216 |
TRUE); |
|
217 |
return FALSE; |
|
218 |
} |
|
219 |
|
|
220 |
if ($this->conn) |
|
221 |
{ |
|
222 |
if (@ldap_rename($this->conn, $dn, $newrdn, $parentdn, TRUE)) |
|
223 |
return TRUE; |
|
224 |
else |
|
225 |
raise_error(array("code" => ldap_errno($this->conn), |
|
226 |
"type" => "ldap", |
|
227 |
"message" => "Rename object failed: ".ldap_error($this->conn)), |
|
228 |
TRUE); |
|
229 |
} |
|
230 |
else |
|
231 |
raise_error(array("type" => "ldap", |
|
232 |
"message" => "Rename object failed: no connection"), |
|
233 |
TRUE); |
|
234 |
return FALSE; |
|
235 |
} |
|
236 |
|
|
237 |
function delete($dn) |
|
238 |
{ |
|
239 |
if ($this->conn) |
|
240 |
{ |
|
241 |
if (@ldap_delete($this->conn, $dn)) |
|
242 |
return TRUE; |
|
243 |
else |
|
244 |
raise_error(array("code" => ldap_errno($this->conn), |
|
245 |
"type" => "ldap", |
|
246 |
"message" => "Delete object failed: ".ldap_error($this->conn)), |
|
247 |
TRUE); |
|
248 |
} |
|
249 |
else |
|
250 |
raise_error(array("type" => "ldap", |
|
251 |
"message" => "Delete object failed: no connection"), |
|
252 |
TRUE); |
|
253 |
return FALSE; |
|
254 |
} |
|
255 |
|
|
256 |
} |
|
257 |
|
|
258 |
// vi: et ts=2 sw=2 |
|
259 |
?> |