4 +-----------------------------------------------------------------------+
5 | program/include/rcube_ldap.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2006-2007, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Interface to an LDAP address directory |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: rcube_ldap.inc 563 2007-05-17 15:58:51Z thomasb $
26 var $fieldmap = array();
30 var $ldap_result = null;
33 /** public properties */
34 var $primary_key = 'ID';
44 * @param array LDAP connection properties
45 * @param integer User-ID
47 function __construct($p)
51 foreach ($p as $prop => $value)
52 if (preg_match('/^(.+)_field$/', $prop, $matches))
53 $this->fieldmap[$matches[1]] = $value;
55 // $this->filter = "(dn=*)";
60 * PHP 4 object constructor
62 * @see rcube_ldap::__construct
64 function rcube_ldap($p)
66 $this->__construct($p);
71 * Establish a connection to the LDAP server
75 if (!function_exists('ldap_connect'))
76 raise_error(array('type' => 'ldap', 'message' => "No ldap support in this installation of PHP"), true);
78 if (is_resource($this->conn))
81 if (!is_array($this->prop['hosts']))
82 $this->prop['hosts'] = array($this->prop['hosts']);
84 foreach ($this->prop['hosts'] as $host)
86 if ($lc = @ldap_connect($host, $this->prop['port']))
88 ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $this->prop['port']);
89 $this->prop['host'] = $host;
95 if (is_resource($this->conn))
98 if (!empty($this->prop['bind_dn']) && !empty($this->prop['bind_pass']))
99 $this->ready = $this->bind($this->prop['bind_dn'], $this->prop['bind_pass']);
102 raise_error(array('type' => 'ldap', 'message' => "Could not connect to any LDAP server, tried $host:{$this->prop[port]} last"), true);
107 * Bind connection with DN and password
109 function bind($dn, $pass)
114 if (@ldap_bind($this->conn, $dn, $pass))
119 'code' => ldap_errno($this->conn),
121 'message' => "Bind failed for dn=$dn: ".ldap_error($this->conn)),
130 * Close connection to LDAP server
135 @ldap_unbind($this->conn);
140 * Set internal list page
142 * @param number Page number to list
145 function set_page($page)
147 $this->list_page = (int)$page;
152 * Set internal page size
154 * @param number Number of messages to display on one page
157 function set_pagesize($size)
159 $this->page_size = (int)$size;
164 * Save a search string for future listings
168 function set_search_set($filter)
170 $this->filter = $filter;
175 * Getter for saved search properties
177 * @return mixed Search properties used by this class
179 function get_search_set()
181 return $this->filter;
186 * Reset all saved results and search parameters
190 $this->result = null;
191 $this->ldap_result = null;
197 * List the current set of contact records
199 * @param array List of cols to show
200 * @return array Indexed list of contact records, each a hash array
202 function list_records($cols=null, $subset=0)
204 // exec LDAP search if no result resource is stored
205 if ($this->conn && !$this->ldap_result)
206 $this->_exec_search();
208 // count contacts for this user
209 $this->result = $this->count();
211 // we have a search result resource
212 if ($this->ldap_result && $this->result->count > 0)
214 if ($this->sort_col && $this->prop['scope'] !== "base")
215 @ldap_sort($this->conn, $this->ldap_result, $this->sort_col);
217 $entries = ldap_get_entries($this->conn, $this->ldap_result);
218 for ($i = $this->result->first; $i < min($entries['count'], $this->result->first + $this->page_size); $i++)
219 $this->result->add($this->_ldap2result($entries[$i]));
222 return $this->result;
229 * @param array List of fields to search in
230 * @param string Search value
231 * @param boolean True if results are requested, False if count only
232 * @return Indexed list of contact records and 'count' value
234 function search($fields, $value, $select=true)
236 // special treatment for ID-based search
237 if ($fields == 'ID' || $fields == $this->primary_key)
239 $ids = explode(',', $value);
240 $result = new rcube_result_set();
241 foreach ($ids as $id)
242 if ($rec = $this->get_record($id, true))
252 $wc = $this->prop['fuzzy_search'] ? '*' : '';
253 if (is_array($this->prop['search_fields']))
255 foreach ($this->prop['search_fields'] as $k => $field)
256 $filter .= "($field=$wc" . rcube_ldap::quote_string($value) . "$wc)";
260 foreach ((array)$fields as $field)
261 if ($f = $this->_map_field($field))
262 $filter .= "($f=$wc" . rcube_ldap::quote_string($value) . "$wc)";
266 // add general filter to query
267 if (!empty($this->prop['filter']))
268 $filter = '(&'.$this->prop['filter'] . $filter . ')';
270 // set filter string and execute search
271 $this->set_search_set($filter);
272 $this->_exec_search();
275 $this->list_records();
277 $this->result = $this->count();
279 return $this->result;
284 * Count number of available contacts in database
286 * @return Result array with values for 'count' and 'first'
291 if ($this->conn && $this->ldap_result)
292 $count = ldap_count_entries($this->conn, $this->ldap_result);
294 return new rcube_result_set($count, ($this->list_page-1) * $this->page_size);
299 * Return the last result set
301 * @return Result array or NULL if nothing selected yet
303 function get_result()
305 return $this->result;
310 * Get a specific contact record
312 * @param mixed record identifier
313 * @return Hash array with all record fields or False if not found
315 function get_record($dn, $assoc=false)
318 if ($this->conn && $dn)
320 $this->ldap_result = @ldap_read($this->conn, base64_decode($dn), "(objectclass=*)", array_values($this->fieldmap));
321 $entry = @ldap_first_entry($this->conn, $this->ldap_result);
323 if ($entry && ($rec = ldap_get_attributes($this->conn, $entry)))
325 $res = $this->_ldap2result($rec);
326 $this->result = new rcube_result_set(1);
327 $this->result->add($res);
331 return $assoc ? $res : $this->result;
336 * Create a new contact record
338 * @param array Assoziative array with save data
339 * @return The create record ID on success, False on error
341 function insert($save_cols)
349 * Update a specific contact record
351 * @param mixed Record identifier
352 * @param array Assoziative array with save data
353 * @return True on success, False on error
355 function update($id, $save_cols)
363 * Mark one or more contact records as deleted
365 * @param array Record identifiers
367 function delete($ids)
375 * Execute the LDAP search based on the stored credentials
379 function _exec_search()
381 if ($this->conn && $this->filter)
383 $function = $this->prop['scope'] == 'sub' ? 'ldap_search' : ($this->prop['scope'] == 'base' ? 'ldap_read' : 'ldap_list');
384 $this->ldap_result = $function($this->conn, $this->prop['base_dn'], $this->filter, array_values($this->fieldmap), 0, 0);
395 function _ldap2result($rec)
400 $out[$this->primary_key] = base64_encode($rec['dn']);
402 foreach ($this->fieldmap as $rf => $lf)
404 if ($rec[$lf]['count'])
405 $out[$rf] = $rec[$lf][0];
415 function _map_field($field)
417 return $this->fieldmap[$field];
424 function quote_string($str)
426 return strtr($str, array('*'=>'\2a', '('=>'\28', ')'=>'\29', '\\'=>'\5c'));