4 +-----------------------------------------------------------------------+
5 | program/include/rcube_ldap.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Manage an LDAP connection |
14 +-----------------------------------------------------------------------+
15 | Author: Jeremy Jongsma <jeremy@jongsma.org> |
16 +-----------------------------------------------------------------------+
18 $Id: rcube_ldap.inc 95 2006-01-08 07:15:44Z justinrandell $
22 require_once("bugs.inc");
35 function __construct()
45 function connect($hosts, $port=389, $protocol=3)
47 if (!function_exists('ldap_connect'))
48 raise_error(array("type" => "ldap",
49 "message" => "No ldap support in this installation of php."),
52 if (is_resource($this->conn))
55 if (!is_array($hosts))
56 $hosts = array($hosts);
58 foreach ($hosts as $host)
60 if ($lc = @ldap_connect($host, $port))
62 @ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $protocol);
65 $this->protocol = $protocol;
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"),
81 if (@ldap_unbind($this->conn))
84 raise_error(array("code" => ldap_errno($this->conn),
86 "message" => "Could not close connection to LDAP server: ".ldap_error($this->conn)),
92 // Merge with connect()?
93 function bind($dn=null, $pass=null)
98 if (@ldap_bind($this->conn, $dn, $pass))
101 raise_error(array("code" => ldap_errno($this->conn),
103 "message" => "Bind failed for dn=$dn: ".ldap_error($this->conn)),
106 if (@ldap_bind($this->conn))
109 raise_error(array("code" => ldap_errno($this->conn),
111 "message" => "Anonymous bind failed: ".ldap_error($this->conn)),
115 raise_error(array("type" => "ldap",
116 "message" => "Attempted bind on nonexistent connection"), TRUE);
120 function count($base, $filter=null, $attributes=null, $scope="sub")
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);
131 return @ldap_count_entries($this->conn, $sr);
134 raise_error(array("type" => "ldap",
135 "message" => "Attempted count search on nonexistent connection"), TRUE);
139 function search($base, $filter=null, $attributes=null, $scope='sub', $sort=null, $limit=0)
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);
151 if ($sort && $scope !== "base")
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);
161 @ldap_sort($this->conn, $sr, $sort);
163 return @ldap_get_entries($this->conn, $sr);
167 raise_error(array("type" => "ldap",
168 "message" => "Attempted search on nonexistent connection"), TRUE);
172 function add($dn, $object)
176 if (@ldap_add($this->conn, $dn, $object))
179 raise_error(array("code" => ldap_errno($this->conn),
181 "message" => "Add object failed: ".ldap_error($this->conn)),
185 raise_error(array("type" => "ldap",
186 "message" => "Add object faile: no connection"),
191 function modify($dn, $object)
195 if (@ldap_modify($this->conn, $dn, $object))
198 raise_error(array("code" => ldap_errno($this->conn),
200 "message" => "Modify object failed: ".ldap_error($this->conn)),
204 raise_error(array("type" => "ldap",
205 "message" => "Modify object failed: no connection"),
210 function rename($dn, $newrdn, $parentdn)
212 if ($this->protocol < 3)
214 raise_error(array("type" => "ldap",
215 "message" => "rename() support requires LDAPv3 or above "),
222 if (@ldap_rename($this->conn, $dn, $newrdn, $parentdn, TRUE))
225 raise_error(array("code" => ldap_errno($this->conn),
227 "message" => "Rename object failed: ".ldap_error($this->conn)),
231 raise_error(array("type" => "ldap",
232 "message" => "Rename object failed: no connection"),
241 if (@ldap_delete($this->conn, $dn))
244 raise_error(array("code" => ldap_errno($this->conn),
246 "message" => "Delete object failed: ".ldap_error($this->conn)),
250 raise_error(array("type" => "ldap",
251 "message" => "Delete object failed: no connection"),