+ }
+
+
+ /**
+ * Close connection to LDAP server
+ */
+ function close()
+ {
+ if ($this->conn)
+ {
+ $this->_debug("C: Close");
+ ldap_unbind($this->conn);
+ $this->conn = null;
+ }
+ }
+
+
+ /**
+ * Returns address book name
+ *
+ * @return string Address book name
+ */
+ function get_name()
+ {
+ return $this->prop['name'];
+ }
+
+
+ /**
+ * Set internal list page
+ *
+ * @param number $page Page number to list
+ */
+ function set_page($page)
+ {
+ $this->list_page = (int)$page;
+ }
+
+
+ /**
+ * Set internal page size
+ *
+ * @param number $size Number of messages to display on one page
+ */
+ function set_pagesize($size)
+ {
+ $this->page_size = (int)$size;
+ }
+
+
+ /**
+ * Save a search string for future listings
+ *
+ * @param string $filter Filter string
+ */
+ function set_search_set($filter)
+ {
+ $this->filter = $filter;
+ }
+
+
+ /**
+ * Getter for saved search properties
+ *
+ * @return mixed Search properties used by this class
+ */
+ function get_search_set()
+ {
+ return $this->filter;
+ }
+
+
+ /**
+ * Reset all saved results and search parameters
+ */
+ function reset()
+ {
+ $this->result = null;
+ $this->ldap_result = null;
+ $this->filter = '';
+ }
+
+
+ /**
+ * List the current set of contact records
+ *
+ * @param array List of cols to show
+ * @param int Only return this number of records
+ *
+ * @return array Indexed list of contact records, each a hash array
+ */
+ function list_records($cols=null, $subset=0)
+ {
+ if ($this->prop['searchonly'] && empty($this->filter) && !$this->group_id)
+ {
+ $this->result = new rcube_result_set(0);
+ $this->result->searchonly = true;
+ return $this->result;
+ }
+
+ // fetch group members recursively
+ if ($this->group_id && $this->group_data['dn'])
+ {
+ $entries = $this->list_group_members($this->group_data['dn']);
+
+ // make list of entries unique and sort it
+ $seen = array();
+ foreach ($entries as $i => $rec) {
+ if ($seen[$rec['dn']]++)
+ unset($entries[$i]);
+ }
+ usort($entries, array($this, '_entry_sort_cmp'));
+
+ $entries['count'] = count($entries);
+ $this->result = new rcube_result_set($entries['count'], ($this->list_page-1) * $this->page_size);
+ }
+ else
+ {
+ // add general filter to query
+ if (!empty($this->prop['filter']) && empty($this->filter))
+ $this->set_search_set($this->prop['filter']);
+
+ // exec LDAP search if no result resource is stored
+ if ($this->conn && !$this->ldap_result)
+ $this->_exec_search();
+
+ // count contacts for this user
+ $this->result = $this->count();
+
+ // we have a search result resource
+ if ($this->ldap_result && $this->result->count > 0)
+ {
+ // sorting still on the ldap server
+ if ($this->sort_col && $this->prop['scope'] !== 'base' && !$this->vlv_active)
+ ldap_sort($this->conn, $this->ldap_result, $this->sort_col);
+
+ // get all entries from the ldap server
+ $entries = ldap_get_entries($this->conn, $this->ldap_result);
+ }
+
+ } // end else
+
+ // start and end of the page
+ $start_row = $this->vlv_active ? 0 : $this->result->first;
+ $start_row = $subset < 0 ? $start_row + $this->page_size + $subset : $start_row;
+ $last_row = $this->result->first + $this->page_size;
+ $last_row = $subset != 0 ? $start_row + abs($subset) : $last_row;
+
+ // filter entries for this page
+ for ($i = $start_row; $i < min($entries['count'], $last_row); $i++)
+ $this->result->add($this->_ldap2result($entries[$i]));
+
+ return $this->result;
+ }
+
+ /**
+ * Get all members of the given group
+ *
+ * @param string Group DN
+ * @param array Group entries (if called recursively)
+ * @return array Accumulated group members
+ */
+ function list_group_members($dn, $count = false, $entries = null)
+ {
+ $group_members = array();
+
+ // fetch group object
+ if (empty($entries)) {
+ $result = @ldap_read($this->conn, $dn, '(objectClass=*)', array('dn','objectClass','member','uniqueMember','memberURL'));
+ if ($result === false)
+ {
+ $this->_debug("S: ".ldap_error($this->conn));
+ return $group_members;
+ }
+
+ $entries = @ldap_get_entries($this->conn, $result);
+ }
+
+ for ($i=0; $i < $entries["count"]; $i++)
+ {
+ $entry = $entries[$i];
+
+ if (empty($entry['objectclass']))
+ continue;
+
+ foreach ((array)$entry['objectclass'] as $objectclass)
+ {
+ switch (strtolower($objectclass)) {
+ case "groupofnames":
+ case "kolabgroupofnames":
+ $group_members = array_merge($group_members, $this->_list_group_members($dn, $entry, 'member', $count));
+ break;
+ case "groupofuniquenames":
+ case "kolabgroupofuniquenames":
+ $group_members = array_merge($group_members, $this->_list_group_members($dn, $entry, 'uniquemember', $count));
+ break;
+ case "groupofurls":
+ $group_members = array_merge($group_members, $this->_list_group_memberurl($dn, $entry, $count));
+ break;
+ }
+ }
+
+ if ($this->prop['sizelimit'] && count($group_members) > $this->prop['sizelimit'])
+ break;
+ }
+
+ return array_filter($group_members);
+ }
+
+ /**
+ * Fetch members of the given group entry from server
+ *
+ * @param string Group DN
+ * @param array Group entry
+ * @param string Member attribute to use
+ * @return array Accumulated group members
+ */
+ private function _list_group_members($dn, $entry, $attr, $count)
+ {
+ // Use the member attributes to return an array of member ldap objects
+ // NOTE that the member attribute is supposed to contain a DN
+ $group_members = array();
+ if (empty($entry[$attr]))
+ return $group_members;
+
+ // read these attributes for all members
+ $attrib = $count ? array('dn') : array_values($this->fieldmap);
+ $attrib[] = 'objectClass';
+ $attrib[] = 'member';
+ $attrib[] = 'uniqueMember';
+ $attrib[] = 'memberURL';
+
+ for ($i=0; $i < $entry[$attr]['count']; $i++)
+ {
+ $result = @ldap_read($this->conn, $entry[$attr][$i], '(objectclass=*)',
+ $attrib, 0, (int)$this->prop['sizelimit'], (int)$this->prop['timelimit']);
+
+ $members = @ldap_get_entries($this->conn, $result);
+ if ($members == false)
+ {
+ $this->_debug("S: ".ldap_error($this->conn));
+ $members = array();
+ }
+
+ // for nested groups, call recursively
+ $nested_group_members = $this->list_group_members($entry[$attr][$i], $count, $members);
+
+ unset($members['count']);
+ $group_members = array_merge($group_members, array_filter($members), $nested_group_members);
+ }
+
+ return $group_members;
+ }
+
+ /**
+ * List members of group class groupOfUrls
+ *
+ * @param string Group DN
+ * @param array Group entry
+ * @param boolean True if only used for counting
+ * @return array Accumulated group members
+ */
+ private function _list_group_memberurl($dn, $entry, $count)
+ {
+ $group_members = array();
+
+ for ($i=0; $i < $entry['memberurl']['count']; $i++)
+ {
+ // extract components from url
+ if (!preg_match('!ldap:///([^\?]+)\?\?(\w+)\?(.*)$!', $entry['memberurl'][$i], $m))
+ continue;
+
+ // add search filter if any
+ $filter = $this->filter ? '(&(' . $m[3] . ')(' . $this->filter . '))' : $m[3];
+ $func = $m[2] == 'sub' ? 'ldap_search' : ($m[2] == 'base' ? 'ldap_read' : 'ldap_list');
+
+ $attrib = $count ? array('dn') : array_values($this->fieldmap);
+ if ($result = @$func($this->conn, $m[1], $filter,
+ $attrib, 0, (int)$this->prop['sizelimit'], (int)$this->prop['timelimit'])
+ ) {
+ $this->_debug("S: ".ldap_count_entries($this->conn, $result)." record(s) for ".$m[1]);
+ }
+ else {
+ $this->_debug("S: ".ldap_error($this->conn));
+ return $group_members;
+ }
+
+ $entries = @ldap_get_entries($this->conn, $result);
+ for ($j = 0; $j < $entries['count']; $j++)
+ {
+ if ($nested_group_members = $this->list_group_members($entries[$j]['dn'], $count))
+ $group_members = array_merge($group_members, $nested_group_members);
+ else
+ $group_members[] = $entries[$j];
+ }
+ }
+
+ return $group_members;
+ }
+
+ /**
+ * Callback for sorting entries
+ */
+ function _entry_sort_cmp($a, $b)
+ {
+ return strcmp($a[$this->sort_col][0], $b[$this->sort_col][0]);
+ }
+
+
+ /**
+ * Search contacts
+ *
+ * @param mixed $fields The field name of array of field names to search in
+ * @param mixed $value Search value (or array of values when $fields is array)
+ * @param int $mode Matching mode:
+ * 0 - partial (*abc*),
+ * 1 - strict (=),
+ * 2 - prefix (abc*)
+ * @param boolean $select True if results are requested, False if count only
+ * @param boolean $nocount (Not used)
+ * @param array $required List of fields that cannot be empty
+ *
+ * @return array Indexed list of contact records and 'count' value
+ */
+ function search($fields, $value, $mode=0, $select=true, $nocount=false, $required=array())
+ {
+ $mode = intval($mode);
+
+ // special treatment for ID-based search
+ if ($fields == 'ID' || $fields == $this->primary_key)
+ {
+ $ids = !is_array($value) ? explode(',', $value) : $value;
+ $result = new rcube_result_set();
+ foreach ($ids as $id)
+ {
+ if ($rec = $this->get_record($id, true))
+ {
+ $result->add($rec);
+ $result->count++;
+ }
+ }
+ return $result;
+ }
+
+ // use VLV pseudo-search for autocompletion
+ if ($this->prop['vlv_search'] && $this->conn && join(',', (array)$fields) == 'email,name')
+ {
+ // add general filter to query
+ if (!empty($this->prop['filter']) && empty($this->filter))
+ $this->set_search_set($this->prop['filter']);
+
+ // set VLV controls with encoded search string
+ $this->_vlv_set_controls($this->prop, $this->list_page, $this->page_size, $value);
+
+ $function = $this->_scope2func($this->prop['scope']);
+ $this->ldap_result = @$function($this->conn, $this->base_dn, $this->filter ? $this->filter : '(objectclass=*)',
+ array_values($this->fieldmap), 0, $this->page_size, (int)$this->prop['timelimit']);
+
+ $this->result = new rcube_result_set(0);
+
+ if (!$this->ldap_result) {
+ $this->_debug("S: ".ldap_error($this->conn));
+ return $this->result;
+ }
+
+ $this->_debug("S: ".ldap_count_entries($this->conn, $this->ldap_result)." record(s)");
+
+ // get all entries of this page and post-filter those that really match the query
+ $search = mb_strtolower($value);
+ $entries = ldap_get_entries($this->conn, $this->ldap_result);
+
+ for ($i = 0; $i < $entries['count']; $i++) {
+ $rec = $this->_ldap2result($entries[$i]);
+ foreach (array('email', 'name') as $f) {
+ $val = mb_strtolower($rec[$f]);
+ switch ($mode) {
+ case 1:
+ $got = ($val == $search);
+ break;
+ case 2:
+ $got = ($search == substr($val, 0, strlen($search)));
+ break;
+ default:
+ $got = (strpos($val, $search) !== false);
+ break;
+ }
+
+ if ($got) {
+ $this->result->add($rec);
+ $this->result->count++;
+ break;
+ }
+ }
+ }
+
+ return $this->result;
+ }
+
+ // use AND operator for advanced searches
+ $filter = is_array($value) ? '(&' : '(|';
+ // set wildcards
+ $wp = $ws = '';
+ if (!empty($this->prop['fuzzy_search']) && $mode != 1) {
+ $ws = '*';
+ if (!$mode) {
+ $wp = '*';
+ }
+ }
+
+ if ($fields == '*')
+ {
+ // search_fields are required for fulltext search
+ if (empty($this->prop['search_fields']))
+ {
+ $this->set_error(self::ERROR_SEARCH, 'nofulltextsearch');
+ $this->result = new rcube_result_set();
+ return $this->result;
+ }
+ if (is_array($this->prop['search_fields']))
+ {
+ foreach ($this->prop['search_fields'] as $field) {
+ $filter .= "($field=$wp" . $this->_quote_string($value) . "$ws)";
+ }
+ }
+ }
+ else
+ {
+ foreach ((array)$fields as $idx => $field) {
+ $val = is_array($value) ? $value[$idx] : $value;
+ if ($f = $this->_map_field($field)) {
+ $filter .= "($f=$wp" . $this->_quote_string($val) . "$ws)";
+ }
+ }
+ }
+ $filter .= ')';
+
+ // add required (non empty) fields filter
+ $req_filter = '';
+ foreach ((array)$required as $field)
+ if ($f = $this->_map_field($field))
+ $req_filter .= "($f=*)";
+
+ if (!empty($req_filter))
+ $filter = '(&' . $req_filter . $filter . ')';
+
+ // avoid double-wildcard if $value is empty
+ $filter = preg_replace('/\*+/', '*', $filter);
+
+ // add general filter to query
+ if (!empty($this->prop['filter']))
+ $filter = '(&(' . preg_replace('/^\(|\)$/', '', $this->prop['filter']) . ')' . $filter . ')';
+
+ // set filter string and execute search
+ $this->set_search_set($filter);
+ $this->_exec_search();
+
+ if ($select)
+ $this->list_records();
+ else
+ $this->result = $this->count();
+
+ return $this->result;
+ }
+
+
+ /**
+ * Count number of available contacts in database
+ *
+ * @return object rcube_result_set Resultset with values for 'count' and 'first'
+ */
+ function count()
+ {
+ $count = 0;
+ if ($this->conn && $this->ldap_result) {
+ $count = $this->vlv_active ? $this->vlv_count : ldap_count_entries($this->conn, $this->ldap_result);
+ }
+ else if ($this->group_id && $this->group_data['dn']) {
+ $count = count($this->list_group_members($this->group_data['dn'], true));
+ }
+ else if ($this->conn) {
+ // We have a connection but no result set, attempt to get one.
+ if (empty($this->filter)) {
+ // The filter is not set, set it.
+ $this->filter = $this->prop['filter'];
+ }
+ $this->_exec_search(true);
+ if ($this->ldap_result) {
+ $count = ldap_count_entries($this->conn, $this->ldap_result);
+ }
+ }
+
+ return new rcube_result_set($count, ($this->list_page-1) * $this->page_size);
+ }
+
+
+ /**
+ * Return the last result set
+ *
+ * @return object rcube_result_set Current resultset or NULL if nothing selected yet
+ */
+ function get_result()
+ {
+ return $this->result;
+ }
+
+
+ /**
+ * Get a specific contact record
+ *
+ * @param mixed Record identifier
+ * @param boolean Return as associative array
+ *
+ * @return mixed Hash array or rcube_result_set with all record fields
+ */
+ function get_record($dn, $assoc=false)
+ {
+ $res = null;
+ if ($this->conn && $dn)
+ {
+ $dn = self::dn_decode($dn);
+
+ $this->_debug("C: Read [dn: $dn] [(objectclass=*)]");
+
+ if ($this->ldap_result = @ldap_read($this->conn, $dn, '(objectclass=*)', array_values($this->fieldmap)))
+ $entry = ldap_first_entry($this->conn, $this->ldap_result);
+ else
+ $this->_debug("S: ".ldap_error($this->conn));
+
+ if ($entry && ($rec = ldap_get_attributes($this->conn, $entry)))
+ {
+ $this->_debug("S: OK"/* . print_r($rec, true)*/);
+
+ $rec = array_change_key_case($rec, CASE_LOWER);
+
+ // Add in the dn for the entry.
+ $rec['dn'] = $dn;
+ $res = $this->_ldap2result($rec);
+ $this->result = new rcube_result_set(1);
+ $this->result->add($res);
+ }
+ }
+
+ return $assoc ? $res : $this->result;
+ }
+
+
+ /**
+ * Check the given data before saving.
+ * If input not valid, the message to display can be fetched using get_error()
+ *
+ * @param array Assoziative array with data to save
+ * @param boolean Try to fix/complete record automatically
+ * @return boolean True if input is valid, False if not.
+ */
+ public function validate(&$save_data, $autofix = false)
+ {
+ // check for name input
+ if (empty($save_data['name'])) {
+ $this->set_error(self::ERROR_VALIDATE, 'nonamewarning');
+ return false;
+ }
+
+ // Verify that the required fields are set.
+ $missing = null;
+ $ldap_data = $this->_map_data($save_data);
+ foreach ($this->prop['required_fields'] as $fld) {
+ if (!isset($ldap_data[$fld])) {
+ $missing[$fld] = 1;
+ }
+ }
+
+ if ($missing) {
+ // try to complete record automatically
+ if ($autofix) {
+ $reverse_map = array_flip($this->fieldmap);
+ $name_parts = preg_split('/[\s,.]+/', $save_data['name']);
+ if ($missing['sn']) {
+ $sn_field = $reverse_map['sn'];
+ $save_data[$sn_field] = array_pop ($name_parts);
+ }
+ if ($missing[($fn_field = $this->fieldmap['firstname'])]) {
+ $save_data['firstname'] = array_shift($name_parts);
+ }
+
+ return $this->validate($save_data, false);
+ }
+
+ // TODO: generate message saying which fields are missing
+ $this->set_error(self::ERROR_VALIDATE, 'formincomplete');
+ return false;
+ }
+
+ // validate e-mail addresses
+ return parent::validate($save_data, $autofix);
+ }
+
+
+ /**
+ * Create a new contact record
+ *
+ * @param array Hash array with save data
+ *
+ * @return encoded record ID on success, False on error
+ */
+ function insert($save_cols)
+ {
+ // Map out the column names to their LDAP ones to build the new entry.
+ $newentry = $this->_map_data($save_cols);
+ $newentry['objectClass'] = $this->prop['LDAP_Object_Classes'];
+
+ // Verify that the required fields are set.
+ $missing = null;
+ foreach ($this->prop['required_fields'] as $fld) {
+ if (!isset($newentry[$fld])) {
+ $missing[] = $fld;
+ }
+ }
+
+ // abort process if requiered fields are missing
+ // TODO: generate message saying which fields are missing
+ if ($missing) {
+ $this->set_error(self::ERROR_VALIDATE, 'formincomplete');
+ return false;
+ }
+
+ // Build the new entries DN.
+ $dn = $this->prop['LDAP_rdn'].'='.$this->_quote_string($newentry[$this->prop['LDAP_rdn']], true).','.$this->base_dn;
+
+ $this->_debug("C: Add [dn: $dn]: ".print_r($newentry, true));
+
+ $res = ldap_add($this->conn, $dn, $newentry);
+ if ($res === FALSE) {
+ $this->_debug("S: ".ldap_error($this->conn));
+ $this->set_error(self::ERROR_SAVING, 'errorsaving');
+ return false;
+ } // end if
+
+ $this->_debug("S: OK");
+
+ $dn = self::dn_encode($dn);
+
+ // add new contact to the selected group
+ if ($this->group_id)
+ $this->add_to_group($this->group_id, $dn);
+
+ return $dn;
+ }
+
+
+ /**
+ * Update a specific contact record
+ *
+ * @param mixed Record identifier
+ * @param array Hash array with save data
+ *
+ * @return boolean True on success, False on error
+ */
+ function update($id, $save_cols)
+ {
+ $record = $this->get_record($id, true);
+ $result = $this->get_result();
+ $record = $result->first();
+
+ $newdata = array();
+ $replacedata = array();
+ $deletedata = array();
+
+ $ldap_data = $this->_map_data($save_cols);
+ $old_data = $record['_raw_attrib'];
+
+ foreach ($this->fieldmap as $col => $fld) {
+ $val = $ldap_data[$fld];
+ if ($fld) {
+ // remove empty array values
+ if (is_array($val))
+ $val = array_filter($val);
+ // The field does exist compare it to the ldap record.
+ if ($old_data[$fld] != $val) {
+ // Changed, but find out how.
+ if (!isset($old_data[$fld])) {
+ // Field was not set prior, need to add it.
+ $newdata[$fld] = $val;
+ }
+ else if ($val == '') {
+ // Field supplied is empty, verify that it is not required.
+ if (!in_array($fld, $this->prop['required_fields'])) {
+ // It is not, safe to clear.
+ $deletedata[$fld] = $old_data[$fld];
+ }
+ } // end elseif
+ else {
+ // The data was modified, save it out.
+ $replacedata[$fld] = $val;
+ }
+ } // end if
+ } // end if
+ } // end foreach
+
+ $dn = self::dn_decode($id);
+
+ // Update the entry as required.
+ if (!empty($deletedata)) {
+ // Delete the fields.
+ $this->_debug("C: Delete [dn: $dn]: ".print_r($deletedata, true));
+ if (!ldap_mod_del($this->conn, $dn, $deletedata)) {
+ $this->_debug("S: ".ldap_error($this->conn));
+ $this->set_error(self::ERROR_SAVING, 'errorsaving');
+ return false;
+ }
+ $this->_debug("S: OK");
+ } // end if
+
+ if (!empty($replacedata)) {
+ // Handle RDN change
+ if ($replacedata[$this->prop['LDAP_rdn']]) {
+ $newdn = $this->prop['LDAP_rdn'].'='
+ .$this->_quote_string($replacedata[$this->prop['LDAP_rdn']], true)
+ .','.$this->base_dn;
+ if ($dn != $newdn) {
+ $newrdn = $this->prop['LDAP_rdn'].'='
+ .$this->_quote_string($replacedata[$this->prop['LDAP_rdn']], true);
+ unset($replacedata[$this->prop['LDAP_rdn']]);
+ }
+ }
+ // Replace the fields.
+ if (!empty($replacedata)) {
+ $this->_debug("C: Replace [dn: $dn]: ".print_r($replacedata, true));
+ if (!ldap_mod_replace($this->conn, $dn, $replacedata)) {
+ $this->_debug("S: ".ldap_error($this->conn));
+ return false;
+ }
+ $this->_debug("S: OK");
+ } // end if
+ } // end if
+
+ if (!empty($newdata)) {
+ // Add the fields.
+ $this->_debug("C: Add [dn: $dn]: ".print_r($newdata, true));
+ if (!ldap_mod_add($this->conn, $dn, $newdata)) {
+ $this->_debug("S: ".ldap_error($this->conn));
+ $this->set_error(self::ERROR_SAVING, 'errorsaving');
+ return false;
+ }
+ $this->_debug("S: OK");
+ } // end if
+
+ // Handle RDN change
+ if (!empty($newrdn)) {
+ $this->_debug("C: Rename [dn: $dn] [dn: $newrdn]");
+ if (!ldap_rename($this->conn, $dn, $newrdn, NULL, TRUE)) {
+ $this->_debug("S: ".ldap_error($this->conn));
+ return false;
+ }
+ $this->_debug("S: OK");
+
+ $dn = self::dn_encode($dn);
+ $newdn = self::dn_encode($newdn);
+
+ // change the group membership of the contact
+ if ($this->groups)
+ {
+ $group_ids = $this->get_record_groups($dn);
+ foreach ($group_ids as $group_id)
+ {
+ $this->remove_from_group($group_id, $dn);
+ $this->add_to_group($group_id, $newdn);
+ }
+ }
+
+ return $newdn;
+ }
+
+ return true;
+ }
+
+
+ /**
+ * Mark one or more contact records as deleted
+ *
+ * @param array Record identifiers
+ * @param boolean Remove record(s) irreversible (unsupported)
+ *
+ * @return boolean True on success, False on error
+ */
+ function delete($ids, $force=true)
+ {
+ if (!is_array($ids)) {
+ // Not an array, break apart the encoded DNs.
+ $ids = explode(',', $ids);
+ } // end if
+
+ foreach ($ids as $id) {
+ $dn = self::dn_decode($id);
+ $this->_debug("C: Delete [dn: $dn]");
+ // Delete the record.
+ $res = ldap_delete($this->conn, $dn);
+ if ($res === FALSE) {
+ $this->_debug("S: ".ldap_error($this->conn));
+ $this->set_error(self::ERROR_SAVING, 'errorsaving');
+ return false;
+ } // end if
+ $this->_debug("S: OK");
+
+ // remove contact from all groups where he was member
+ if ($this->groups) {
+ $dn = self::dn_encode($dn);
+ $group_ids = $this->get_record_groups($dn);
+ foreach ($group_ids as $group_id) {
+ $this->remove_from_group($group_id, $dn);
+ }
+ }
+ } // end foreach
+
+ return count($ids);
+ }
+
+
+ /**
+ * Execute the LDAP search based on the stored credentials
+ */
+ private function _exec_search($count = false)
+ {
+ if ($this->ready)
+ {
+ $filter = $this->filter ? $this->filter : '(objectclass=*)';
+ $function = $this->_scope2func($this->prop['scope'], $ns_function);
+
+ $this->_debug("C: Search [$filter][dn: $this->base_dn]");
+
+ // when using VLV, we get the total count by...
+ if (!$count && $function != 'ldap_read' && $this->prop['vlv'] && !$this->group_id) {
+ // ...either reading numSubOrdinates attribute
+ if ($this->prop['numsub_filter'] && ($result_count = @$ns_function($this->conn, $this->base_dn, $this->prop['numsub_filter'], array('numSubOrdinates'), 0, 0, 0))) {
+ $counts = ldap_get_entries($this->conn, $result_count);
+ for ($this->vlv_count = $j = 0; $j < $counts['count']; $j++)
+ $this->vlv_count += $counts[$j]['numsubordinates'][0];
+ $this->_debug("D: total numsubordinates = " . $this->vlv_count);
+ }
+ else // ...or by fetching all records dn and count them
+ $this->vlv_count = $this->_exec_search(true);
+
+ $this->vlv_active = $this->_vlv_set_controls($this->prop, $this->list_page, $this->page_size);
+ }
+
+ // only fetch dn for count (should keep the payload low)
+ $attrs = $count ? array('dn') : array_values($this->fieldmap);
+ if ($this->ldap_result = @$function($this->conn, $this->base_dn, $filter,
+ $attrs, 0, (int)$this->prop['sizelimit'], (int)$this->prop['timelimit'])
+ ) {
+ $entries_count = ldap_count_entries($this->conn, $this->ldap_result);
+ $this->_debug("S: $count_entries record(s)");
+
+ return $count ? $count_entries : true;
+ }
+ else {
+ $this->_debug("S: ".ldap_error($this->conn));
+ }
+ }
+