Code

Updated some FAI plugins
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiVariable.inc
index e13a8f38bfcd64c87972840442481ff34f2f9b17..40ece132514c32bb34b116f27ba164e1ca17b31b 100644 (file)
@@ -21,6 +21,7 @@ class faiVariable extends plugin
   /* Attributes to initialise for each subObject */
   var $subAttributes    = array("cn","description","FAIvariableContent"); 
   var $sub64coded       = array();  
+  var $subBinary        = array();
 
   /* Specific attributes */
   var $cn               = "";       // The class name for this object
@@ -40,47 +41,21 @@ class faiVariable extends plugin
     if($dn != "new"){
       $this->dn =$dn;
 
-      /* Get FAIstate
-       */
-      if(isset($this->attrs['FAIstate'][0])){
-        $this->FAIstate = $this->attrs['FAIstate'][0];
-      }
-
       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
        */
-      $ldap     = $this->config->get_ldap_link();
-      $ldap->cd ($this->dn);
-
-      $attrs_to_search = $this->subAttributes;
-      $attrs_to_search[] = "FAIstate";
-      $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
-
-      while($object = $ldap->fetch()){
-      
-        /* Skip objects, that are tagged as removed */
-        if(isset($object['FAIstate'][0])){
-          if(preg_match("/removed$/",$object['FAIstate'][0])){
-            continue;
-          }
-        }
-
-        /* Set status for save management */
-        foreach($this->subAttributes as $attrs){
-          if(!isset($object[$attrs][0])){
-            $this->SubObjects[$object['cn'][0]][$attrs]="";
-          }else{
-            $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
-          }
-        }
-     
-        foreach($this->sub64coded as $codeIt){
-          $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
-        }
-        $this->SubObjects[$object['cn'][0]]['status']      = "edited";
-        $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
+      $res = FAI::get_all_objects_for_given_object($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
+      foreach($res as $obj){
+        $objects = array();
+        $objects['description'] = "";
+        $objects['status']      = "FreshLoaded";
+        $objects['dn']          = $obj['dn'];
+        $objects                = $this->get_object_attributes($objects,$this->subAttributes);
+        $this->SubObjects[$objects['cn']] = $objects;
       }
-
+    }
+    $this->is_new = FALSE;
+    if($this->dn == "new"){
+      $this->is_new =TRUE;
     }
     $this->ui = get_userinfo();
   }
@@ -88,7 +63,7 @@ class faiVariable extends plugin
 
   function acl_base_for_current_object($dn)
   {
-    if($dn == "new"){
+    if($dn == "new" || $dn == ""){
       if($this->dn == "new"){
         $dn = session::get('CurrentMainBase');
       }else{
@@ -114,11 +89,12 @@ class faiVariable extends plugin
     $display= "";
 
     /* Add new sub object */
-    if(isset($_POST['AddSubObject'])){
+    if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
       $this->dialog= new $this->subClassName($this->config,"new");
       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
       $this->dialog->set_acl_category("fai");
       $this->dialog->parent = &$this;
+      $this->dialog->FAIstate = $this->FAIstate;
       $this->is_dialog=true;
     }
 
@@ -135,19 +111,20 @@ class faiVariable extends plugin
       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
       $this->dialog->set_acl_category("fai");
       $this->dialog->set_acl_base($c_dn);
+      $this->dialog->FAIstate = $this->FAIstate;
       $this->dialog->parent = &$this;
       session::set('objectinfo',$this->SubObjects[$var]['dn']);
       $this->is_dialog=true;
     }
     
     /* Remove Sub object */
-    if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
+    if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject'])) && !preg_match("/freeze/i",$this->FAIstate)){
       foreach($_POST['SubObject'] as $var){
 
         $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
         $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
         if(preg_match("/d/",$acl)){
-          if($this->SubObjects[$var]['status'] == "edited"){
+          if($this->SubObjects[$var]['status'] != "new"){
             $this->SubObjects[$var]['status']= "delete";
           }else{
             unset($this->SubObjects[$var]);
@@ -157,7 +134,7 @@ class faiVariable extends plugin
     }
 
     /* Save Dialog */
-    if(isset($_POST['SaveSubObject'])){
+    if(isset($_POST['SaveSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
       $this->dialog->save_object();
       $msgs = $this->dialog->check();
       if(count($msgs)>0){
@@ -167,15 +144,16 @@ class faiVariable extends plugin
       }else{
         $obj = $this->dialog->save();
         if(isset($obj['remove'])){
-          if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
+          if($this->SubObjects[$obj['remove']['from']]['status'] != "new"){
             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
-          }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
+          }else{
             unset($this->SubObjects[$obj['remove']['from']]);
           }
           $obj['status'] = "new";
           $this->SubObjects[$obj['remove']['to']] = $obj;
           unset($this->SubObjects[$obj['remove']['to']]['remove']);
         }else{
+          $obj['status'] = "edited";
           $this->SubObjects[$obj['cn']]=$obj;
         }
         $this->is_dialog=false;
@@ -214,7 +192,7 @@ class faiVariable extends plugin
     $tmp = array();
     foreach($this->SubObjects as $key => $obj){
       $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
-      if(preg_match("/r/",$acl) || $obj['dn'] == "new"){
+      if((preg_match("/r/",$acl) || $obj['dn'] == "new") && isset($ret[$key])){
         $tmp[$key] = $ret[$key];
       } 
     }
@@ -233,12 +211,12 @@ class faiVariable extends plugin
     }
 
     $c_dn = $this->acl_base_for_current_object($this->dn);
-    $smarty->assign("is_createable",     preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
-    $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
+    $smarty->assign("is_createable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
+    $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
 
     $tmp = $this->plInfo();
     foreach($tmp['plProvidedAcls'] as $name => $translation) {
-      $smarty->assign($name."ACL",$this->getacl($name));
+      $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/i",$this->FAIstate)));
     }
     
 
@@ -278,21 +256,13 @@ class faiVariable extends plugin
     if($this->acl_is_removeable()){
       $ldap = $this->config->get_ldap_link();
       $ldap->cd ($this->dn);
-
-      $faifilter = session::get('faifilter');
-      $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
-      if($faifilter['branch'] == "main"){
-        $use_dn = $this->dn;
-      }
-  
-      FAI::prepare_to_save_FAI_object($use_dn,array(),true);
+      $release = $this->parent->parent->fai_release;
+      $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
+      FAI::prepare_to_save_FAI_object($use_dn,array(),true);
 
       foreach($this->SubObjects as $name => $obj){
-        $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
-        if($faifilter['branch'] == "main"){
-          $use_dn = $obj['dn'];
-        }
+        $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
       }
       $this->handle_post_events("remove");
@@ -304,13 +274,8 @@ class faiVariable extends plugin
    */
   function save_object()
   {
-    if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
+    if((isset($_POST['FAIvariable_posted'])) && !preg_match("/freeze/", $this->FAIstate) ){
       plugin::save_object();
-      foreach($this->attributes as $attrs){
-        if(isset($_POST[$attrs])){
-          $this->$attrs = $_POST[$attrs];
-        }
-      }
     }
   }
 
@@ -321,6 +286,17 @@ class faiVariable extends plugin
     /* Call common method to give check the hook */
     $message= plugin::check();
 
+    /* Ensure that we do not overwrite an allready existing entry 
+     */
+    if($this->is_new){
+      $release = $this->parent->parent->fai_release;
+      $new_dn= 'cn='.$this->cn.",".get_ou('faiVariableRDN').get_ou('faiBaseRDN').$release;
+      $res = faiManagement::check_class_name("FAIvariable",$this->cn,$new_dn);
+      if(isset($res[$this->cn])){
+        $message[] = msgPool::duplicated(_("Name"));
+      }
+    }
+
     return ($message);
   }
 
@@ -332,9 +308,6 @@ class faiVariable extends plugin
  
     $ldap = $this->config->get_ldap_link();
     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
-    if (!$ldap->success()){
-      msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
-    }
 
     if($this->initially_was_account){
       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
@@ -347,18 +320,7 @@ class faiVariable extends plugin
      *  Because we must delete old entries first.
      * After deletion, we perform add and modify 
      */
-    $Objects = array();
-    foreach($this->SubObjects as $name => $obj){
-      if($obj['status'] == "delete"){
-        $Objects[$name] = $obj; 
-      }
-    }
-    foreach($this->SubObjects as $name => $obj){
-      if($obj['status'] != "delete"){
-        $Objects[$name] = $obj; 
-      }
-    }
-
+    $Objects = $this->SubObjects;
     foreach($Objects as $name => $obj){
 
       foreach($this->sub64coded as $codeIt){
@@ -367,7 +329,7 @@ class faiVariable extends plugin
 
       $tmp = array();
       foreach($this->subAttributes as $attrs){
-        if(empty($obj[$attrs])){
+        if($obj[$attrs] == ""){
           $obj[$attrs] = array();
         }
         $tmp[$attrs] = $obj[$attrs];
@@ -408,33 +370,14 @@ class faiVariable extends plugin
 
     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
      */
-    $ldap     = $this->config->get_ldap_link();
-    $ldap->cd ($source['dn']);
-    $attrs_to_search = $this->subAttributes;
-    $attrs_to_search[] = "FAIstate";
-    $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
-    while($object = $ldap->fetch()){
-
-      /* Skip objects, that are tagged as removed */
-      if(isset($object['FAIstate'][0])){
-        if(preg_match("/removed$/",$object['FAIstate'][0])){
-          continue;
-        }
-      }
-
-      /* Set status for save management */
-      foreach($this->subAttributes as $attrs){
-        if(!isset($object[$attrs][0])){
-          $this->SubObjects[$object['cn'][0]][$attrs]="";
-        }else{
-          $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
-        }
-      }
-      foreach($this->sub64coded as $codeIt){
-        $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
-      }
-      $this->SubObjects[$object['cn'][0]]['status']      = "edited";
-      $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
+    $res = FAI::get_all_objects_for_given_object($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
+    foreach($res as $obj){
+
+      $objects = array();
+      $objects['status']      = "edited";
+      $objects['dn']          = $obj['dn'];
+      $objects                = $this->get_object_attributes($objects,$this->subAttributes);
+      $this->SubObjects[$objects['cn']] = $objects;
     }
   } 
 
@@ -455,6 +398,65 @@ class faiVariable extends plugin
             "description"       => _("Description"))
           ));
   }
+
+
+  /*! \brief  Used for copy & paste.
+    Returns a HTML input mask, which allows to change the cn of this entry.
+    @param  Array   Array containing current status && a HTML template.
+   */
+  function getCopyDialog()
+  {
+    $vars = array("cn");
+    $smarty = get_smarty();
+    $smarty->assign("cn", htmlentities($this->cn));
+    $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
+    $ret = array();
+    $ret['string'] = $str;
+    $ret['status'] = "";
+    return($ret);
+  }
+
+
+  /*! \brief  Used for copy & paste.
+    Some entries must be renamed to avaoid duplicate entries.
+   */
+  function saveCopyDialog()
+  {
+    if(isset($_POST['cn'])){
+      $this->cn = get_post('cn');
+    }
+  }
+
+  /* Reload some attributes */
+  function get_object_attributes($object,$attributes)
+  {
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->cat($object['dn'],$attributes);
+    $tmp  = $ldap->fetch();
+
+    foreach($attributes as $attrs){
+      if(isset($tmp[$attrs][0])){
+        $var = $tmp[$attrs][0];
+
+        /* Check if we must decode some attributes */
+        if(in_array_ics($attrs,$this->sub64coded)){
+          $var = base64_decode($var);
+        }
+
+        /*  check if this is a binary entry */
+        if(in_array_ics($attrs,$this->subBinary)){
+          $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
+        }
+
+        /* Fix slashes */
+        $var = addslashes($var);
+        $object[$attrs] = $var;
+      }
+    }
+    return($object);
+  }
+
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: