Code

Added additional information to the extended error message
[gosa.git] / include / class_ldap.inc
index 677f7f459025e90075dbff282d84a4f2e14e357e..a1f960aa3bc730f67939ee52a170f3530ec83e84 100644 (file)
@@ -30,16 +30,24 @@ class LDAP{
   var $hostname = "";
   var $follow_referral = FALSE;
   var $referrals= array();
-
+  var $max_ldap_query_time = 0;   // 0, empty or negative values will disable this check 
 
   function LDAP($binddn,$bindpw, $hostname, $follow_referral= FALSE, $tls= FALSE)
   {
+    global $config;
     $this->follow_referral= $follow_referral;
     $this->tls=$tls;
     $this->binddn=$this->convert($binddn);
 
     $this->bindpw=$bindpw;
     $this->hostname=$hostname;
+
+    /* Check if MAX_LDAP_QUERY_TIME is defined */ 
+    if(isset($config->data['MAIN']['MAX_LDAP_QUERY_TIME'])){
+      $str = $config->data['MAIN']['MAX_LDAP_QUERY_TIME'];
+      $this->max_ldap_query_time = (float)($str);
+    }
+
     $this->connect();
   }
 
@@ -171,12 +179,23 @@ class LDAP{
   {
     if($this->hascon){
       if ($this->reconnect) $this->connect();
+
+      $start = microtime();
+   
       $this->clearResult();
       $this->sr = @ldap_search($this->cid, $this->fix($this->basedn), $filter, $attrs);
       $this->error = @ldap_error($this->cid);
       $this->resetResult();
       $this->hasres=true;
-  
+   
+      /* Check if query took longer as specified in max_ldap_query_time */
+      if($this->max_ldap_query_time){
+        $diff = get_MicroTimeDiff($start,microtime());
+        if($diff > $this->max_ldap_query_time){
+          print_red(sprintf(_("The LDAP server is slow (%.2fs for the last query). This may be responsible for performance breakdowns."),$diff)) ;
+        }
+      }
+
       return($this->sr);
     }else{
       $this->error = "Could not connect to LDAP server";
@@ -193,10 +212,22 @@ class LDAP{
         $basedn = $this->basedn;
       else
         $basedn= $this->convert($basedn);
+  
+      $start = microtime();
+
       $this->sr = @ldap_list($this->cid, $this->fix($basedn), $filter,$attrs);
       $this->error = @ldap_error($this->cid);
       $this->resetResult();
       $this->hasres=true;
+
+       /* Check if query took longer as specified in max_ldap_query_time */
+      if($this->max_ldap_query_time){
+        $diff = get_MicroTimeDiff($start,microtime());
+        if($diff > $this->max_ldap_query_time){
+          print_red(sprintf(_("The ldapserver is answering very slow (%.2f), this may be responsible for performance breakdowns."),$diff)) ;
+        }
+      }
+
       return($this->sr);
     }else{
       $this->error = "Could not connect to LDAP server";
@@ -236,6 +267,7 @@ class LDAP{
 
   function fetch()
   {
+    $att= array();
     if($this->hascon){
       if($this->hasres){
         if ($this->start == 0)
@@ -607,6 +639,7 @@ class LDAP{
     $l= array_reverse(ldap_explode_dn($real_path,0));
     unset($l['count']);
     $cdn= $this->basedn;
+    $tag= "";
 
     foreach ($l as $part){
       $cdn= "$part,$cdn";
@@ -628,18 +661,35 @@ class LDAP{
       $attrs= $this->fetch();
 
       /* Create missing entry? */
-      if (!count ($attrs)){
+      if (count ($attrs)){
+      
+        /* Catch the tag - if present */
+        if (isset($attrs['gosaUnitTag'][0])){
+          $tag= $attrs['gosaUnitTag'][0];
+        }
+
+      } else {
         $type= preg_replace('/^([^=]+)=.*$/', '\\1', $cdn);
         $param= preg_replace('/^[^=]+=([^,]+),.*$/', '\\1', $cdn);
 
         $na= array();
         switch ($type){
           case 'ou':
-            $na["objectClass"]= "organizationalUnit";
+            if ($tag != ""){
+              $na["objectClass"]= array("organizationalUnit", "gosaAdministrativeUnitTag");
+              $na["gosaUnitTag"]= $tag;
+            } else {
+              $na["objectClass"]= "organizationalUnit";
+            }
             $na["ou"]= $param;
             break;
           case 'dc':
-            $na["objectClass"]= array("dcObject", "top", "locality");
+            if ($tag != ""){
+              $na["objectClass"]= array("dcObject", "top", "locality", "gosaAdministrativeUnitTag");
+              $na["gosaUnitTag"]= $tag;
+            } else {
+              $na["objectClass"]= array("dcObject", "top", "locality");
+            }
             $na["dc"]= $param;
             break;
           default:
@@ -710,7 +760,12 @@ class LDAP{
     if ($this->error == 'Success'){
       return $this->error;
     } else {
-      $error= $this->error." (".$this->get_additional_error().")";
+      $adderror= $this->get_additional_error();
+      if ($adderror != ""){
+        $error= $this->error." (".$this->get_additional_error().", ".sprintf(_("while operating on '%s' using LDAP server '%s'"), $this->base, $this->hostname).")";
+      } else {
+        $error= $this->error." (".sprintf(_("while operating on LDAP server %s"), $this->hostname).")";
+      }
       return $error;
     }
   }