From 028734b25cfa2ee30a129710db0440617f9f3095 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Mon, 18 Aug 2014 11:21:03 -0400
Subject: [PATCH] Make LDAP errors available to the application

---
 program/lib/Roundcube/rcube_ldap_generic.php |   26 +++++++++++++++++++-------
 program/lib/Roundcube/rcube_ldap.php         |   20 ++++++++++++++++++--
 2 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/program/lib/Roundcube/rcube_ldap.php b/program/lib/Roundcube/rcube_ldap.php
index 274616a..6805c49 100644
--- a/program/lib/Roundcube/rcube_ldap.php
+++ b/program/lib/Roundcube/rcube_ldap.php
@@ -906,7 +906,6 @@
         return $this->result;
     }
 
-
     /**
      * Get a specific contact record
      *
@@ -946,6 +945,23 @@
         }
 
         return $assoc ? $res : $this->result;
+    }
+
+    /**
+     * Returns the last error occurred (e.g. when updating/inserting failed)
+     *
+     * @return array Hash array with the following fields: type, message
+     */
+    function get_error()
+    {
+        $err = $this->error;
+
+        // check ldap connection for errors
+        if (!$err && $this->ldap->get_error()) {
+            $err = array(self::ERROR_SEARCH, $this->ldap->get_error());
+        }
+
+        return $err;
     }
 
 
@@ -1609,7 +1625,7 @@
 
         if ($search) {
             foreach ($group_cache as $group) {
-                if ($this->compare_search_value('name', $group['name'], $search, $mode)) {
+                if ($this->compare_search_value('name', $group['name'], mb_strtolower($search), $mode)) {
                     $groups[] = $group;
                 }
             }
diff --git a/program/lib/Roundcube/rcube_ldap_generic.php b/program/lib/Roundcube/rcube_ldap_generic.php
index 9823dbb..a76ad6d 100644
--- a/program/lib/Roundcube/rcube_ldap_generic.php
+++ b/program/lib/Roundcube/rcube_ldap_generic.php
@@ -32,6 +32,7 @@
     /** private properties */
     protected $cache = null;
     protected $attributes = array('dn');
+    protected $error;
 
     function __construct($config = null)
     {
@@ -88,9 +89,20 @@
 
         case LOG_ERR:
         case LOG_WARNING:
+            $this->error = $msg;
             rcube::raise_error($msg, true, false);
             break;
         }
+    }
+
+    /**
+     * Returns the last LDAP error occurred
+     *
+     * @return mixed Error message string or null if no error occured
+     */
+    function get_error()
+    {
+        return $this->error;
     }
 
     /**
@@ -151,7 +163,7 @@
         $this->_debug("C: Replace $dn: ".print_r($entry, true));
 
         if (!ldap_mod_replace($this->conn, $dn, $entry)) {
-            $this->_debug("S: ".ldap_error($this->conn));
+            $this->_error("ldap_mod_replace() failed with " . ldap_error($this->conn));
             return false;
         }
 
@@ -169,7 +181,7 @@
         $this->_debug("C: Add $dn: ".print_r($entry, true));
 
         if (!ldap_mod_add($this->conn, $dn, $entry)) {
-            $this->_debug("S: ".ldap_error($this->conn));
+            $this->_error("ldap_mod_add() failed with " . ldap_error($this->conn));
             return false;
         }
 
@@ -187,7 +199,7 @@
         $this->_debug("C: Delete $dn: ".print_r($entry, true));
 
         if (!ldap_mod_del($this->conn, $dn, $entry)) {
-            $this->_debug("S: ".ldap_error($this->conn));
+            $this->_error("ldap_mod_del() failed with " . ldap_error($this->conn));
             return false;
         }
 
@@ -205,7 +217,7 @@
         $this->_debug("C: Rename $dn to $newrdn");
 
         if (!ldap_rename($this->conn, $dn, $newrdn, $newparent, $deleteoldrdn)) {
-            $this->_debug("S: ".ldap_error($this->conn));
+            $this->_error("ldap_rename() failed with " . ldap_error($this->conn));
             return false;
         }
 
@@ -228,7 +240,7 @@
             $list = ldap_get_entries($this->conn, $result);
 
             if ($list === false) {
-                $this->_debug("S: ".ldap_error($this->conn));
+                $this->_error("ldap_get_entries() failed with " . ldap_error($this->conn));
                 return array();
             }
 
@@ -238,7 +250,7 @@
             $this->_debug("S: $count record(s)");
         }
         else {
-            $this->_debug("S: ".ldap_error($this->conn));
+            $this->_error("ldap_list() failed with " . ldap_error($this->conn));
         }
 
         return $list;
@@ -257,7 +269,7 @@
         if ($this->conn && $dn) {
             $result = @ldap_read($this->conn, $dn, $filter, $attributes, 0, (int)$this->config['sizelimit'], (int)$this->config['timelimit']);
             if ($result === false) {
-                $this->_debug("S: ".ldap_error($this->conn));
+                $this->_error("ldap_read() failed with " . ldap_error($this->conn));
                 return false;
             }
 

--
Gitblit v1.9.1