pull/6796/head
Aleksander Machniak 5 years ago
parent 24913e51d3
commit 8932abd2e7

@ -1006,8 +1006,8 @@ class rcube_ldap extends rcube_addressbook
/** /**
* Get a specific contact record * Get a specific contact record
* *
* @param mixed Record identifier * @param mixed $dn Record identifier
* @param boolean Return as associative array * @param boolean $assoc Return as associative array
* *
* @return mixed Hash array or rcube_result_set with all record fields * @return mixed Hash array or rcube_result_set with all record fields
*/ */
@ -1065,8 +1065,9 @@ class rcube_ldap extends rcube_addressbook
* Check the given data before saving. * Check the given data before saving.
* If input not valid, the message to display can be fetched using get_error() * If input not valid, the message to display can be fetched using get_error()
* *
* @param array Assoziative array with data to save * @param array &$save_data Associative array with data to save
* @param boolean Try to fix/complete record automatically * @param boolean $autofix Try to fix/complete record automatically
*
* @return boolean True if input is valid, False if not. * @return boolean True if input is valid, False if not.
*/ */
public function validate(&$save_data, $autofix = false) public function validate(&$save_data, $autofix = false)
@ -1083,23 +1084,24 @@ class rcube_ldap extends rcube_addressbook
} }
// Verify that the required fields are set. // Verify that the required fields are set.
$missing = null; $missing = array();
$ldap_data = $this->_map_data($save_data); $ldap_data = $this->_map_data($save_data);
foreach ($this->prop['required_fields'] as $fld) { foreach ($this->prop['required_fields'] as $fld) {
if (!isset($ldap_data[$fld]) || $ldap_data[$fld] === '') { if (!isset($ldap_data[$fld]) || $ldap_data[$fld] === '') {
$missing[$fld] = 1; $missing[$fld] = 1;
} }
} }
if ($missing) { if (!empty($missing)) {
// try to complete record automatically // try to complete record automatically
if ($autofix) { if ($autofix) {
$sn_field = $this->fieldmap['surname']; $sn_field = $this->fieldmap['surname'];
$fn_field = $this->fieldmap['firstname']; $fn_field = $this->fieldmap['firstname'];
$mail_field = $this->fieldmap['email']; $mail_field = $this->fieldmap['email'];
// try to extract surname and firstname from displayname // try to extract surname and firstname from displayname
$name_parts = preg_split('/[\s,.]+/', $save_data['name']); $name_parts = preg_split('/[\s,.]+/', $save_data['name']);
if ($sn_field && $missing[$sn_field]) { if ($sn_field && $missing[$sn_field]) {
$save_data['surname'] = array_pop($name_parts); $save_data['surname'] = array_pop($name_parts);
@ -1135,10 +1137,10 @@ class rcube_ldap extends rcube_addressbook
/** /**
* Create a new contact record * Create a new contact record
* *
* @param array Associative array with save data * @param array $save_cols Associative array with save data
* Keys: Field name with optional section in the form FIELD:SECTION * Keys: Field name with optional section in the form FIELD:SECTION
* Values: Field value. Can be either a string or an array of strings for multiple values * Values: Field value. Can be either a string or an array of strings for multiple values
* @param boolean True to check for duplicates first * @param boolean $check True to check for duplicates first
* *
* @return mixed The created record ID on success, False on error * @return mixed The created record ID on success, False on error
*/ */
@ -1198,8 +1200,9 @@ class rcube_ldap extends rcube_addressbook
$dn = self::dn_encode($dn); $dn = self::dn_encode($dn);
// add new contact to the selected group // add new contact to the selected group
if ($this->group_id) if ($this->group_id) {
$this->add_to_group($this->group_id, $dn); $this->add_to_group($this->group_id, $dn);
}
return $dn; return $dn;
} }
@ -1214,17 +1217,15 @@ class rcube_ldap extends rcube_addressbook
*/ */
function update($id, $save_cols) function update($id, $save_cols)
{ {
$record = $this->get_record($id, true); $record = $this->get_record($id, true);
$newdata = array(); $newdata = array();
$replacedata = array(); $replacedata = array();
$deletedata = array(); $deletedata = array();
$subdata = array(); $subdata = array();
$subdeldata = array(); $subdeldata = array();
$subnewdata = array(); $subnewdata = array();
$ldap_data = $this->_map_data($save_cols);
$ldap_data = $this->_map_data($save_cols); $old_data = $record['_raw_attrib'];
$old_data = $record['_raw_attrib'];
// special handling of photo col // special handling of photo col
if ($photo_fld = $this->fieldmap['photo']) { if ($photo_fld = $this->fieldmap['photo']) {
@ -1286,9 +1287,9 @@ class rcube_ldap extends rcube_addressbook
// The data was modified, save it out. // The data was modified, save it out.
$replacedata[$fld] = $val; $replacedata[$fld] = $val;
} }
} // end if }
} // end if }
} // end foreach }
// console($old_data, $ldap_data, '----', $newdata, $replacedata, $deletedata, '----', $subdata, $subnewdata, $subdeldata); // console($old_data, $ldap_data, '----', $newdata, $replacedata, $deletedata, '----', $subdata, $subnewdata, $subdeldata);
@ -1301,7 +1302,7 @@ class rcube_ldap extends rcube_addressbook
$this->set_error(self::ERROR_SAVING, 'errorsaving'); $this->set_error(self::ERROR_SAVING, 'errorsaving');
return false; return false;
} }
} // end if }
if (!empty($replacedata)) { if (!empty($replacedata)) {
// Handle RDN change // Handle RDN change
@ -1322,7 +1323,7 @@ class rcube_ldap extends rcube_addressbook
return false; return false;
} }
} }
} // end if }
// RDN change, we need to remove all sub-entries // RDN change, we need to remove all sub-entries
if (!empty($newrdn)) { if (!empty($newrdn)) {
@ -1346,7 +1347,7 @@ class rcube_ldap extends rcube_addressbook
$this->set_error(self::ERROR_SAVING, 'errorsaving'); $this->set_error(self::ERROR_SAVING, 'errorsaving');
return false; return false;
} }
} // end if }
// Handle RDN change // Handle RDN change
if (!empty($newrdn)) { if (!empty($newrdn)) {
@ -1398,7 +1399,7 @@ class rcube_ldap extends rcube_addressbook
if (!is_array($ids)) { if (!is_array($ids)) {
// Not an array, break apart the encoded DNs. // Not an array, break apart the encoded DNs.
$ids = explode(',', $ids); $ids = explode(',', $ids);
} // end if }
foreach ($ids as $id) { foreach ($ids as $id) {
$dn = self::dn_decode($id); $dn = self::dn_decode($id);
@ -1423,13 +1424,14 @@ class rcube_ldap extends rcube_addressbook
// remove contact from all groups where he was a member // remove contact from all groups where he was a member
if ($this->groups) { if ($this->groups) {
$dn = self::dn_encode($dn); $dn = self::dn_encode($dn);
$group_ids = $this->get_record_groups($dn); $group_ids = $this->get_record_groups($dn);
foreach (array_keys($group_ids) as $group_id) { foreach (array_keys($group_ids) as $group_id) {
$this->remove_from_group($group_id, $dn); $this->remove_from_group($group_id, $dn);
} }
} }
} // end foreach }
return count($ids); return count($ids);
} }
@ -1478,7 +1480,7 @@ class rcube_ldap extends rcube_addressbook
$attrvals['{'.$k.'}'] = is_array($v) ? $v[0] : $v; $attrvals['{'.$k.'}'] = is_array($v) ? $v[0] : $v;
} }
foreach ((array)$this->prop['autovalues'] as $lf => $templ) { foreach ((array) $this->prop['autovalues'] as $lf => $templ) {
if (empty($attrs[$lf])) { if (empty($attrs[$lf])) {
if (strpos($templ, '(') !== false) { if (strpos($templ, '(') !== false) {
// replace {attr} placeholders with (escaped!) attribute values to be safely eval'd // replace {attr} placeholders with (escaped!) attribute values to be safely eval'd
@ -1514,16 +1516,17 @@ class rcube_ldap extends rcube_addressbook
*/ */
private function _ldap2result($rec) private function _ldap2result($rec)
{ {
$out = array('_type' => 'person'); $out = array('_type' => 'person');
$fieldmap = $this->fieldmap; $fieldmap = $this->fieldmap;
if ($rec['dn']) if ($rec['dn']) {
$out[$this->primary_key] = self::dn_encode($rec['dn']); $out[$this->primary_key] = self::dn_encode($rec['dn']);
}
// determine record type // determine record type
if ($this->is_group_entry($rec)) { if ($this->is_group_entry($rec)) {
$out['_type'] = 'group'; $out['_type'] = 'group';
$out['readonly'] = true; $out['readonly'] = true;
$fieldmap['name'] = $this->group_data['name_attr'] ?: $this->prop['groups']['name_attr']; $fieldmap['name'] = $this->group_data['name_attr'] ?: $this->prop['groups']['name_attr'];
} }
@ -1537,8 +1540,7 @@ class rcube_ldap extends rcube_addressbook
} }
} }
foreach ($fieldmap as $rf => $lf) foreach ($fieldmap as $rf => $lf) {
{
// we might be dealing with normalized and non-normalized data // we might be dealing with normalized and non-normalized data
$entry = $rec[$lf]; $entry = $rec[$lf];
if (!is_array($entry) || !isset($entry['count'])) { if (!is_array($entry) || !isset($entry['count'])) {
@ -1616,7 +1618,7 @@ class rcube_ldap extends rcube_addressbook
foreach ($this->fieldmap as $rf => $fld) { foreach ($this->fieldmap as $rf => $fld) {
$val = $save_cols[$rf]; $val = $save_cols[$rf];
// check for value in base field (eg.g email instead of email:foo) // check for value in base field (eg. email instead of email:foo)
list($col, $subtype) = explode(':', $rf); list($col, $subtype) = explode(':', $rf);
if (!$val && !empty($save_cols[$col])) { if (!$val && !empty($save_cols[$col])) {
$val = $save_cols[$col]; $val = $save_cols[$col];
@ -1626,8 +1628,10 @@ class rcube_ldap extends rcube_addressbook
$val = $this->get_col_values($col, $save_cols, true); $val = $this->get_col_values($col, $save_cols, true);
} }
if (is_array($val)) if (is_array($val)) {
$val = array_filter($val); // remove empty entries $val = array_filter($val); // remove empty entries
}
if ($fld && $val) { if ($fld && $val) {
// The field does exist, add it to the entry. // The field does exist, add it to the entry.
$ldap_data[$fld] = $val; $ldap_data[$fld] = $val;
@ -1910,7 +1914,8 @@ class rcube_ldap extends rcube_addressbook
/** /**
* Get group properties such as name and email address(es) * Get group properties such as name and email address(es)
* *
* @param string Group identifier * @param string $group_id Group identifier
*
* @return array Group properties as hash array * @return array Group properties as hash array
*/ */
function get_group($group_id) function get_group($group_id)
@ -1924,7 +1929,8 @@ class rcube_ldap extends rcube_addressbook
/** /**
* Create a contact group with the given name * Create a contact group with the given name
* *
* @param string The group name * @param string $group_name The group name
*
* @return mixed False on error, array with record props in success * @return mixed False on error, array with record props in success
*/ */
function create_group($group_name) function create_group($group_name)
@ -1954,7 +1960,8 @@ class rcube_ldap extends rcube_addressbook
/** /**
* Delete the given group and all linked group members * Delete the given group and all linked group members
* *
* @param string Group identifier * @param string $group_id Group identifier
*
* @return boolean True on success, false if no data was changed * @return boolean True on success, false if no data was changed
*/ */
function delete_group($group_id) function delete_group($group_id)
@ -1978,9 +1985,10 @@ class rcube_ldap extends rcube_addressbook
/** /**
* Rename a specific contact group * Rename a specific contact group
* *
* @param string Group identifier * @param string $group_id Group identifier
* @param string New name to set for this group * @param string $new_name New name to set for this group
* @param string New group identifier (if changed, otherwise don't set) * @param string &$new_gid New group identifier (if changed, otherwise don't set)
*
* @return boolean New name on success, false if no data was changed * @return boolean New name on success, false if no data was changed
*/ */
function rename_group($group_id, $new_name, &$new_gid) function rename_group($group_id, $new_name, &$new_gid)
@ -2005,8 +2013,8 @@ class rcube_ldap extends rcube_addressbook
/** /**
* Add the given contact records the a certain group * Add the given contact records the a certain group
* *
* @param string Group identifier * @param string $group_id Group identifier
* @param array|string List of contact identifiers to be added * @param array|string $contact_ids List of contact identifiers to be added
* *
* @return int Number of contacts added * @return int Number of contacts added
*/ */
@ -2040,8 +2048,8 @@ class rcube_ldap extends rcube_addressbook
/** /**
* Remove the given contact records from a certain group * Remove the given contact records from a certain group
* *
* @param string Group identifier * @param string $group_id Group identifier
* @param array|string List of contact identifiers to be removed * @param array|string $contact_ids List of contact identifiers to be removed
* *
* @return int Number of deleted group members * @return int Number of deleted group members
*/ */
@ -2075,7 +2083,7 @@ class rcube_ldap extends rcube_addressbook
/** /**
* Get group assignments of a specific contact record * Get group assignments of a specific contact record
* *
* @param mixed Record identifier * @param mixed $contact_id Record identifier
* *
* @return array List of assigned groups as ID=>Name pairs * @return array List of assigned groups as ID=>Name pairs
* @since 0.5-beta * @since 0.5-beta
@ -2092,9 +2100,11 @@ class rcube_ldap extends rcube_addressbook
$member_attr = $this->get_group_member_attr(); $member_attr = $this->get_group_member_attr();
$add_filter = ''; $add_filter = '';
if ($member_attr != 'member' && $member_attr != 'uniqueMember') if ($member_attr != 'member' && $member_attr != 'uniqueMember') {
$add_filter = "($member_attr=$contact_dn)"; $add_filter = "($member_attr=$contact_dn)";
$filter = strtr("(|(member=$contact_dn)(uniqueMember=$contact_dn)$add_filter)", array('\\' => '\\\\')); }
$filter = strtr("(|(member=$contact_dn)(uniqueMember=$contact_dn)$add_filter)", array("\\" => "\\\\"));
$ldap_data = $this->ldap->search($base_dn, $filter, 'sub', array('dn', $name_attr)); $ldap_data = $this->ldap->search($base_dn, $filter, 'sub', array('dn', $name_attr));
if ($ldap_data === false) { if ($ldap_data === false) {
@ -2103,10 +2113,12 @@ class rcube_ldap extends rcube_addressbook
$groups = array(); $groups = array();
foreach ($ldap_data as $entry) { foreach ($ldap_data as $entry) {
if (!$entry['dn']) if (!$entry['dn']) {
$entry['dn'] = $ldap_data->get_dn(); $entry['dn'] = $ldap_data->get_dn();
}
$group_name = $entry[$name_attr][0]; $group_name = $entry[$name_attr][0];
$group_id = self::dn_encode($entry['dn']); $group_id = self::dn_encode($entry['dn']);
$groups[$group_id] = $group_name; $groups[$group_id] = $group_name;
} }

Loading…
Cancel
Save