From 63d6e6dfc35e6d82c4a64f37c408794c163becd4 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Wed, 28 Sep 2011 15:16:41 -0400
Subject: [PATCH] Bump versions to 0.6 stable
---
program/include/rcube_ldap.php | 205 ++++++++++++++++++++++++++++++--------------------
1 files changed, 122 insertions(+), 83 deletions(-)
diff --git a/program/include/rcube_ldap.php b/program/include/rcube_ldap.php
index c363dc5..69ae8f8 100644
--- a/program/include/rcube_ldap.php
+++ b/program/include/rcube_ldap.php
@@ -5,6 +5,7 @@
| |
| This file is part of the Roundcube Webmail client |
| Copyright (C) 2006-2011, The Roundcube Dev Team |
+ | Copyright (C) 2011, Kolab Systems AG |
| Licensed under the GNU GPL |
| |
| PURPOSE: |
@@ -13,6 +14,7 @@
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
| Andreas Dick <andudi (at) gmx (dot) ch> |
+ | Aleksander Machniak <machniak@kolabsys.com> |
+-----------------------------------------------------------------------+
$Id$
@@ -71,8 +73,14 @@
$this->prop = $p;
// check if groups are configured
- if (is_array($p['groups']) and count($p['groups']))
+ if (is_array($p['groups']) && count($p['groups'])) {
$this->groups = true;
+ // set member field
+ if (!empty($p['groups']['member_attr']))
+ $this->prop['member_attr'] = strtolower($p['groups']['member_attr']);
+ else if (empty($p['member_attr']))
+ $this->prop['member_attr'] = 'member';
+ }
// fieldmap property is given
if (is_array($p['fieldmap'])) {
@@ -113,8 +121,8 @@
foreach ($this->prop['required_fields'] as $key => $val)
$this->prop['required_fields'][$key] = $this->_attr_name(strtolower($val));
- $this->sort_col = is_array($p['sort']) ? $p['sort'][0] : $p['sort'];
- $this->debug = $debug;
+ $this->sort_col = is_array($p['sort']) ? $p['sort'][0] : $p['sort'];
+ $this->debug = $debug;
$this->mail_domain = $mail_domain;
$this->_connect();
@@ -171,7 +179,10 @@
$bind_pass = $this->prop['bind_pass'];
$bind_user = $this->prop['bind_user'];
$bind_dn = $this->prop['bind_dn'];
- $this->base_dn = $this->prop['base_dn'];
+
+ $this->base_dn = $this->prop['base_dn'];
+ $this->groups_base_dn = ($this->prop['groups']['base_dn']) ?
+ $this->prop['groups']['base_dn'] : $this->base_dn;
// User specific access, generate the proper values to use.
if ($this->prop['user_specific']) {
@@ -181,8 +192,11 @@
}
// Get the pieces needed for variable replacement.
- $fu = $RCMAIL->user->get_username();
- list($u, $d) = explode('@', $fu);
+ if ($fu = $RCMAIL->user->get_username())
+ list($u, $d) = explode('@', $fu);
+ else
+ $d = $this->mail_domain;
+
$dc = 'dc='.strtr($d, array('.' => ',dc=')); // hierarchal domain string
$replaces = array('%dc' => $dc, '%d' => $d, '%fu' => $fu, '%u' => $u);
@@ -194,7 +208,7 @@
$this->_debug("S: searching with base {$this->prop['search_base_dn']} for {$this->prop['search_filter']}");
- $res = ldap_search($this->conn, $this->prop['search_base_dn'], $this->prop['search_filter'], array('uid'));
+ $res = @ldap_search($this->conn, $this->prop['search_base_dn'], $this->prop['search_filter'], array('uid'));
if ($res && ($entry = ldap_first_entry($this->conn, $res))) {
$bind_dn = ldap_get_dn($this->conn, $entry);
@@ -207,8 +221,9 @@
}
}
// Replace the bind_dn and base_dn variables.
- $bind_dn = strtr($bind_dn, $replaces);
- $this->base_dn = strtr($this->base_dn, $replaces);
+ $bind_dn = strtr($bind_dn, $replaces);
+ $this->base_dn = strtr($this->base_dn, $replaces);
+ $this->groups_base_dn = strtr($this->groups_base_dn, $replaces);
if (empty($bind_user)) {
$bind_user = $u;
@@ -431,34 +446,41 @@
// 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);
+ // 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;
+ // get all entries from the ldap server
$entries = ldap_get_entries($this->conn, $this->ldap_result);
+
+ // filtering for group members
+ if ($this->groups and $this->group_id)
+ {
+ $count = 0;
+ $members = array();
+ foreach ($entries as $entry)
+ {
+ if ($this->group_members[self::dn_encode($entry['dn'])])
+ {
+ $members[] = $entry;
+ $count++;
+ }
+ }
+ $entries = $members;
+ $entries['count'] = $count;
+ $this->result->count = $count;
+ }
+
+ // filter entries for this page
for ($i = $start_row; $i < min($entries['count'], $last_row); $i++)
$this->result->add($this->_ldap2result($entries[$i]));
}
-
- // temp hack for filtering group members
- if ($this->groups and $this->group_id)
- {
- $result = new rcube_result_set();
- while ($record = $this->result->iterate())
- {
- if ($this->group_members[$record['ID']])
- {
- $result->add($record);
- $result->count++;
- }
- }
- $this->result = $result;
- }
-
return $this->result;
}
@@ -604,7 +626,7 @@
$res = null;
if ($this->conn && $dn)
{
- $dn = base64_decode($dn);
+ $dn = self::dn_decode($dn);
$this->_debug("C: Read [dn: $dn] [(objectclass=*)]");
@@ -675,8 +697,8 @@
} // end foreach
// Verify that the required fields are set.
+ $missing = null;
foreach ($this->prop['required_fields'] as $fld) {
- $missing = null;
if (!isset($newentry[$fld])) {
$missing[] = $fld;
}
@@ -703,11 +725,13 @@
$this->_debug("S: OK");
+ $dn = self::dn_encode($dn);
+
// add new contact to the selected group
if ($this->groups)
- $this->add_to_group($this->group_id, base64_encode($dn));
+ $this->add_to_group($this->group_id, $dn);
- return base64_encode($dn);
+ return $dn;
}
@@ -728,7 +752,7 @@
$newdata = array();
$replacedata = array();
$deletedata = array();
-
+
// flatten composite fields in $record
if (is_array($record['address'])) {
foreach ($record['address'] as $i => $struct) {
@@ -766,7 +790,7 @@
} // end if
} // end foreach
- $dn = base64_decode($id);
+ $dn = self::dn_decode($id);
// Update the entry as required.
if (!empty($deletedata)) {
@@ -823,17 +847,21 @@
}
$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(base64_encode($dn));
+ $group_ids = $this->get_record_groups($dn);
foreach ($group_ids as $group_id)
{
- $this->remove_from_group($group_id, base64_encode($dn));
- $this->add_to_group($group_id, base64_encode($newdn));
+ $this->remove_from_group($group_id, $dn);
+ $this->add_to_group($group_id, $newdn);
}
}
- return base64_encode($newdn);
+
+ return $newdn;
}
return true;
@@ -856,7 +884,7 @@
} // end if
foreach ($ids as $id) {
- $dn = base64_decode($id);
+ $dn = self::dn_decode($id);
$this->_debug("C: Delete [dn: $dn]");
// Delete the record.
$res = ldap_delete($this->conn, $dn);
@@ -868,12 +896,11 @@
$this->_debug("S: OK");
// remove contact from all groups where he was member
- if ($this->groups)
- {
- $group_ids = $this->get_record_groups(base64_encode($dn));
- foreach ($group_ids as $group_id)
- {
- $this->remove_from_group($group_id, base64_encode($dn));
+ 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
@@ -957,7 +984,7 @@
$out = array();
if ($rec['dn'])
- $out[$this->primary_key] = base64_encode($rec['dn']);
+ $out[$this->primary_key] = self::dn_encode($rec['dn']);
foreach ($this->fieldmap as $rf => $lf)
{
@@ -1057,7 +1084,7 @@
for ($i=0; $i<$cache_members["count"]; $i++)
{
if (!empty($cache_members[$i]))
- $members[base64_encode($cache_members[$i])] = 1;
+ $members[self::dn_encode($cache_members[$i])] = 1;
}
$this->group_members = $members;
$this->group_id = $group_id;
@@ -1074,35 +1101,18 @@
*/
function list_groups($search = null)
{
- global $RCMAIL;
-
if (!$this->groups)
return array();
-
- $this->groups_base_dn = ($this->prop['groups']['base_dn']) ?
- $this->prop['groups']['base_dn'] : $this->base_dn;
-
- // replace user specific dn
- if ($this->prop['user_specific'])
- {
- $fu = $RCMAIL->user->get_username();
- list($u, $d) = explode('@', $fu);
- $dc = 'dc='.strtr($d, array('.' => ',dc='));
- $replaces = array('%dc' => $dc, '%d' => $d, '%fu' => $fu, '%u' => $u);
-
- $this->groups_base_dn = strtr($this->groups_base_dn, $replaces);
- }
$base_dn = $this->groups_base_dn;
$filter = $this->prop['groups']['filter'];
$this->_debug("C: Search [$filter][dn: $base_dn]");
- $res = ldap_search($this->conn, $base_dn, $filter, array('cn','member'));
+ $res = @ldap_search($this->conn, $base_dn, $filter, array('cn', $this->prop['member_attr']));
if ($res === false)
{
$this->_debug("S: ".ldap_error($this->conn));
- $this->set_error(self::ERROR_SAVING, 'errorsaving');
return array();
}
@@ -1116,10 +1126,10 @@
$group_name = $ldap_data[$i]['cn'][0];
if (!$search || strstr(strtolower($group_name), strtolower($search)))
{
- $group_id = base64_encode($group_name);
+ $group_id = self::dn_encode($group_name);
$groups[$group_id]['ID'] = $group_id;
$groups[$group_id]['name'] = $group_name;
- $groups[$group_id]['members'] = $ldap_data[$i]['member'];
+ $groups[$group_id]['members'] = $ldap_data[$i][$this->prop['member_attr']];
$group_sortnames[] = strtolower($group_name);
}
}
@@ -1142,12 +1152,12 @@
$base_dn = $this->groups_base_dn;
$new_dn = "cn=$group_name,$base_dn";
- $new_gid = base64_encode($group_name);
+ $new_gid = self::dn_encode($group_name);
$new_entry = array(
'objectClass' => $this->prop['groups']['object_classes'],
'cn' => $group_name,
- 'member' => '',
+ $this->prop['member_attr'] => '',
);
$this->_debug("C: Add [dn: $new_dn]: ".print_r($new_entry, true));
@@ -1212,7 +1222,7 @@
$group_name = $this->group_cache[$group_id]['name'];
$old_dn = "cn=$group_name,$base_dn";
$new_rdn = "cn=$new_name";
- $new_gid = base64_encode($new_name);
+ $new_gid = self::dn_encode($new_name);
$this->_debug("C: Rename [dn: $old_dn] [dn: $new_rdn]");
@@ -1241,13 +1251,14 @@
if (!$this->group_cache)
$this->list_groups();
- $base_dn = $this->groups_base_dn;
- $group_name = $this->group_cache[$group_id]['name'];
- $group_dn = "cn=$group_name,$base_dn";
+ $base_dn = $this->groups_base_dn;
+ $group_name = $this->group_cache[$group_id]['name'];
+ $member_attr = $this->prop['member_attr'];
+ $group_dn = "cn=$group_name,$base_dn";
$new_attrs = array();
foreach (explode(",", $contact_ids) as $id)
- $new_attrs['member'][] = base64_decode($id);
+ $new_attrs[$member_attr][] = self::dn_decode($id);
$this->_debug("C: Add [dn: $group_dn]: ".print_r($new_attrs, true));
@@ -1276,13 +1287,14 @@
if (!$this->group_cache)
$this->list_groups();
- $base_dn = $this->groups_base_dn;
- $group_name = $this->group_cache[$group_id]['name'];
- $group_dn = "cn=$group_name,$base_dn";
+ $base_dn = $this->groups_base_dn;
+ $group_name = $this->group_cache[$group_id]['name'];
+ $member_attr = $this->prop['member_attr'];
+ $group_dn = "cn=$group_name,$base_dn";
$del_attrs = array();
foreach (explode(",", $contact_ids) as $id)
- $del_attrs['member'][] = base64_decode($id);
+ $del_attrs[$member_attr][] = self::dn_decode($id);
$this->_debug("C: Delete [dn: $group_dn]: ".print_r($del_attrs, true));
@@ -1312,17 +1324,17 @@
if (!$this->groups)
return array();
- $base_dn = $this->groups_base_dn;
- $contact_dn = base64_decode($contact_id);
- $filter = strtr("(member=$contact_dn)", array('\\' => '\\\\'));
+ $base_dn = $this->groups_base_dn;
+ $contact_dn = self::dn_decode($contact_id);
+ $member_attr = $this->prop['member_attr'];
+ $filter = strtr("($member_attr=$contact_dn)", array('\\' => '\\\\'));
$this->_debug("C: Search [$filter][dn: $base_dn]");
- $res = ldap_search($this->conn, $base_dn, $filter, array('cn'));
+ $res = @ldap_search($this->conn, $base_dn, $filter, array('cn'));
if ($res === false)
{
$this->_debug("S: ".ldap_error($this->conn));
- $this->set_error(self::ERROR_SAVING, 'errorsaving');
return array();
}
$ldap_data = ldap_get_entries($this->conn, $res);
@@ -1332,7 +1344,7 @@
for ($i=0; $i<$ldap_data["count"]; $i++)
{
$group_name = $ldap_data[$i]['cn'][0];
- $group_id = base64_encode($group_name);
+ $group_id = self::dn_encode($group_name);
$groups[$group_id] = $group_id;
}
return $groups;
@@ -1391,7 +1403,7 @@
/**
* create ber encoding for sort control
*
- * @pararm array List of cols to sort by
+ * @param array List of cols to sort by
* @return string BER encoded option value
*/
private function _sort_ber_encode($sortcols)
@@ -1445,11 +1457,38 @@
/**
* Returns ascii string encoded in hex
*/
- private static function _string2hex($str) {
+ private static function _string2hex($str)
+ {
$hex = '';
for ($i=0; $i < strlen($str); $i++)
$hex .= dechex(ord($str[$i]));
return $hex;
}
+ /**
+ * HTML-safe DN string encoding
+ *
+ * @param string $str DN string
+ *
+ * @return string Encoded HTML identifier string
+ */
+ static function dn_encode($str)
+ {
+ // @TODO: to make output string shorter we could probably
+ // remove dc=* items from it
+ return rtrim(strtr(base64_encode($str), '+/', '-_'), '=');
+ }
+
+ /**
+ * Decodes DN string encoded with _dn_encode()
+ *
+ * @param string $str Encoded HTML identifier string
+ *
+ * @return string DN string
+ */
+ static function dn_decode($str)
+ {
+ $str = str_pad(strtr($str, '-_', '+/'), strlen($str) % 4, '=', STR_PAD_RIGHT);
+ return base64_decode($str);
+ }
}
--
Gitblit v1.9.1