From e3caaf59c20fd556364f838ae6082b3e2fba6d56 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Thu, 17 May 2007 11:58:51 -0400
Subject: [PATCH] Use global filters and bind username/ for Ldap searches (#1484159)

---
 CHANGELOG                      |    4 +
 config/main.inc.php.dist       |    3 +
 program/include/rcube_ldap.inc |   54 ++++++++++++--------------
 3 files changed, 31 insertions(+), 30 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index a42c2a6..0275606 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -3,9 +3,11 @@
 
 2007/05/17 (thomasb)
 ----------
-- Updated Catalan, Russian, and Chinese translations
+- Updated Catalan, Russian, Portuguese and Chinese translations
 - Renamed localization folder for Chinese (Big5)
 - Chanegd Slovenian language code from 'si' to 'sl'
+- Added Sinhala (Sri-Lanka) localization
+- Use global filters and bind username/ for Ldap searches (#1484159)
 
 
 2007/05/13 (thomasb)
diff --git a/config/main.inc.php.dist b/config/main.inc.php.dist
index 894dfa4..d11888d 100644
--- a/config/main.inc.php.dist
+++ b/config/main.inc.php.dist
@@ -197,10 +197,13 @@
  *  'hosts'         => array('directory.verisign.com'),
  *  'port'          => 389,
  *  'base_dn'       => '',
+ *  'bind_dn'       => '',
+ *  'bind_pass'     => '',
  *  'search_fields' => array('mail', 'cn'),  // fields to search in
  *  'name_field'    => 'cn',    // this field represents the contact's name
  *  'email_field'   => 'mail',  // this field represents the contact's e-mail
  *  'scope'         => 'sub',   // search mode: sub|base|list
+ *  'filter'        => '',      // will be &'d with search field ex: (status=act)
  *  'fuzzy_search'  => true);   // server allows wildcard search
  */
 
diff --git a/program/include/rcube_ldap.inc b/program/include/rcube_ldap.inc
index 06a99ad..055c346 100644
--- a/program/include/rcube_ldap.inc
+++ b/program/include/rcube_ldap.inc
@@ -93,45 +93,37 @@
     }
     
     if (is_resource($this->conn))
+    {
       $this->ready = true;
+      if (!empty($this->prop['bind_dn']) && !empty($this->prop['bind_pass']))
+        $this->ready = $this->bind($this->prop['bind_dn'], $this->prop['bind_pass']);
+    }
     else
       raise_error(array('type' => 'ldap', 'message' => "Could not connect to any LDAP server, tried $host:{$this->prop[port]} last"), true);
   }
 
 
   /**
-   * Merge with connect()?
+   * Bind connection with DN and password
    */
-  function bind($dn=null, $pass=null)
+  function bind($dn, $pass)
   {
-    if ($this->conn)
-    {
-      if ($dn)
-      {
-        if (@ldap_bind($this->conn, $dn, $pass))
-          return true;
-        else
-          raise_error(array('code' => ldap_errno($this->conn),
-                            'type' => 'ldap',
-                            'message' => "Bind failed for dn=$dn: ".ldap_error($this->conn)),
-                      true);
-      }
-      else
-      {
-        if (@ldap_bind($this->conn))
-          return true;
-        else
-          raise_error(array('code' => ldap_errno($this->conn),
-                            'type' => 'ldap',
-                            'message' => "Anonymous bind failed: ".ldap_error($this->conn)),
-                      true);
-        }
-    }
+    if (!$this->conn)
+      return false;
+    
+    if (@ldap_bind($this->conn, $dn, $pass))
+      return true;
     else
-      raise_error(array('type' => 'ldap', 'message' => "Attempted bind on nonexistent connection"), true);
-      
-    return false;
+    {
+      raise_error(array(
+        'code' => ldap_errno($this->conn),
+        'type' => 'ldap',
+        'message' => "Bind failed for dn=$dn: ".ldap_error($this->conn)),
+      true);
     }
+    
+    return false;
+  }
 
 
   /**
@@ -270,6 +262,10 @@
           $filter .= "($f=$wc" . rcube_ldap::quote_string($value) . "$wc)";
     }
     $filter .= ')';
+    
+    // add general filter to query
+    if (!empty($this->prop['filter']))
+      $filter = '(&'.$this->prop['filter'] . $filter . ')';
 
     // set filter string and execute search
     $this->set_search_set($filter);
@@ -385,7 +381,7 @@
     if ($this->conn && $this->filter)
     {
       $function = $this->prop['scope'] == 'sub' ? 'ldap_search' : ($this->prop['scope'] == 'base' ? 'ldap_read' : 'ldap_list');
-      $this->ldap_result = @$function($this->conn, $this->prop['base_dn'], $this->filter, array_values($this->fieldmap), 0, 0);
+      $this->ldap_result = $function($this->conn, $this->prop['base_dn'], $this->filter, array_values($this->fieldmap), 0, 0);
       return true;
     }
     else

--
Gitblit v1.9.1