Code

Added some checks
[gosa.git] / include / class_ldap.inc
index a87a2e75f7dda75c0e365c32198011e5f26abe1b..96ff38e203b08e796d2a663a796ef53dd8e9d190 100644 (file)
@@ -153,7 +153,7 @@ class LDAP{
     if ($basedn=="")
       $basedn = $this->basedn;
     else
-      $basedn = $this-convert($this->basedn);
+      $basedn = $this->convert($this->basedn);
     return(ereg_replace("[^,]*[,]*[ ]*(.*)", "\\1", $basedn));
   }
 
@@ -194,13 +194,13 @@ class LDAP{
     }
   }
 
-  function cat($dn)
+  function cat($dn,$attrs= array("*"))
   {
     if($this->hascon){
       if ($this->reconnect) $this->connect();
       $this->clearResult();
       $filter = "(objectclass=*)";
-      $this->sr = @ldap_read($this->cid, $this->fix($dn), $filter);
+      $this->sr = @ldap_read($this->cid, $this->fix($dn), $filter,$attrs);
       $this->error = @ldap_error($this->cid);
       $this->resetResult();
       $this->hasres=true;
@@ -396,27 +396,59 @@ class LDAP{
   }
 
   /* Copy given attributes and sub-dns with attributes to destination dn 
-      
   */
-  function copy_FAI_resource_recursive($sourcedn,$destinationdn,$type="branch",$is_first = false)
+  function copy_FAI_resource_recursive($sourcedn,$destinationdn,$type="branch",$is_first = true,$depth=0)
   {
     error_reporting(E_ALL);
+    
+    if($is_first){
+      echo "<h2>".sprintf(_("Creating copy of %s"),"<i>".$sourcedn."</i>")."</h2>";
+    }else{
+      if(preg_match("/^ou=/",$sourcedn)){
+        echo "<h3>"._("Processing")." <i>$destinationdn</i></h3>";
+      }else{
+        $tmp = split(",",$sourcedn);
+        
+        echo "&nbsp;<b>"._("Object").":</b> ";
+
+        $deststr = $destinationdn;
+        if(strlen($deststr) > 96){
+          $deststr = substr($deststr,0,96)."...";
+        }
+
+        echo $deststr."<br>";
+      }
+    }
+
+    flush();
+    
     if($this->hascon){
       if ($this->reconnect) $this->connect();
 
       /* Save base dn */
       $basedn= $this->basedn;
       $delarray= array();
-      
+     
       /* Check if destination entry already exists */
-      if($this->count($this->fetch($this->cat($destinationdn)))){
+      $this->cat($destinationdn);
+
+      if($this->count()){
         return;
       }else{
+        
+        $this->clearResult();
 
-        /* Get source entrie */
+        /* Get source entry */
         $this->cd($basedn);
-        $attr = $this->fetch($this->cat($sourcedn));
+        $this->cat($sourcedn);
+        $attr = $this->fetch();
 
+        /* Error while fetching object / attribute abort*/
+        if((!$attr) || (count($attr)) ==0) {
+          echo _("Error while fetching source dn - aborted!");
+          return;
+        }
+  
         /* check if this is a department */
         if(in_array("organizationalUnit",$attr['objectClass'])){
           $attr['dn'] = $this->convert($destinationdn);
@@ -426,23 +458,19 @@ class LDAP{
 
           /* If is first entry, append FAIbranch to department entry */
           if($is_first){
-            $attr= $this->fetch($this->cat($destinationdn));
+            $this->cat($destinationdn);
+            $attr= $this->fetch();
 
             /* Filter unneeded informations */
             foreach($attr as $key => $value){
               if(is_numeric($key)) unset($attr[$key]);
-              if(isset($attr[$key]['count'])){
-                if(($attr[$key]['count']==1)&&($key!="objectClass")){
-                  $attr[$key] = $attr[$key][0];
-                }
-              }
-              
               if(isset($attr[$key]['count'])){
                 if(is_array($attr[$key])){
                   unset($attr[$key]['count']);
                 }
               }
             }
+            
             unset($attr['count']);
             unset($attr['dn']);
 
@@ -452,16 +480,19 @@ class LDAP{
             /* Add this entry */
             $this->modify($attr);
           }
-          
         }else{
-        /* If this is no department */
+
+          /* If this is no department */
           foreach($attr as $key => $value){
-            if(is_numeric($key)) unset($attr[$key]);
-            if(isset($attr[$key]['count'])){
-              if(($attr[$key]['count']==1)&&($key!="objectClass")){
-                $attr[$key] = $attr[$key][0];
+            if(in_array($key ,array("FAItemplateFile","FAIscript", "gotoLogonScript", "gosaApplicationIcon"))){
+              $sr= ldap_read($this->cid, $this->fix($sourcedn), "$key=*", array($key));
+              $ei= ldap_first_entry($this->cid, $sr);
+              if ($tmp= @ldap_get_values_len($this->cid, $ei,$key)){
+                $attr[$key] = $tmp;
               }
             }
+
+            if(is_numeric($key)) unset($attr[$key]);
             if(isset($attr[$key]['count'])){
               if(is_array($attr[$key])){
                 unset($attr[$key]['count']);
@@ -470,18 +501,21 @@ class LDAP{
           }
           unset($attr['count']);
           unset($attr['dn']);
-          if($type=="branch"){
-            $attr['FAIstate'] ="branch";
-          }elseif($type=="freeze"){
-            $attr['FAIstate'] ="freeze";
-          }else{
-            print_red(_("Unknown FAIstate %s"),$type);
+
+          if(!in_array("gosaApplication" , $attr['objectClass'])){
+            if($type=="branch"){
+              $attr['FAIstate'] ="branch";
+            }elseif($type=="freeze"){
+              $attr['FAIstate'] ="freeze";
+            }else{
+              print_red(_("Unknown FAIstate %s"),$type);
+            }
           }
+
           /* Add entry */
           $this->cd($destinationdn);
-          $a = $this->fetch($this->cat($destinationdn));
+          $this->cat($destinationdn);
+          $a = $this->fetch();
           if(!count($a)){
             $this->add($attr);
           }
@@ -506,16 +540,21 @@ class LDAP{
       asort ($delarray);
       reset ($delarray);
 
+       $depth ++;
       foreach($delarray as $dn => $bla){
         if($dn != $destinationdn){
           $this->cd($basedn);
           $item = $this->fetch($this->cat($dn));
           if(!in_array("FAIbranch",$item['objectClass'])){
-            $this->copy_FAI_resource_recursive($dn,str_replace($sourcedn,$destinationdn,$dn),$type);
+            $this->copy_FAI_resource_recursive($dn,str_replace($sourcedn,$destinationdn,$dn),$type,false,$depth);
           } 
         }
       }
     }
+    if($is_first){
+      echo "<p class='seperator'>&nbsp;</p>";
+    }
+
   }
 
   function modify($attrs)