Code

Updated table summary
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiHook.inc
index c5e8c47dc0fcf1dd6b8c0c16c5fa1c7e60c8f731..e71fc1b855f504c9007428c6ab98c1a7d3a75b1e 100644 (file)
@@ -43,42 +43,26 @@ class faiHook 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);
+      $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
+      foreach($res as $obj){
 
-      $data = array();
-      while($object = $ldap->fetch()){
-        $data[] = $object;
-      }
-      foreach($data as $object){
+        /* Skip not relevant objects */
+        if(!preg_match("/".preg_quote($this->dn, '/')."$/i",$obj['dn'])) continue;
 
-       /* 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 */
         $objects = array();
         $objects['status']      = "FreshLoaded";
-        $objects['dn']          = $object['dn'];
+        $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();
   }
 
@@ -116,9 +100,9 @@ class faiHook extends plugin
 
   function acl_base_for_current_object($dn)
   {
-    if($dn == "new"){
+    if($dn == "new" || $dn == ""){
       if($this->dn == "new"){
-        $dn = session::get('CurrentMainBase');
+        $dn= $this->parent->parent->acl_base;
       }else{
         $dn = $this->dn;
       }
@@ -170,10 +154,11 @@ class faiHook extends plugin
         }
 
         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
+        $this->dialog->FAIstate = $this->FAIstate;
         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
         $this->dialog->set_acl_category("fai");
 
-        session::set('objectinfo',$obj['dn']);
+        set_object_info($obj['dn']);
         $this->dialog->parent = &$this;
         $this->is_dialog=true;
       }
@@ -199,23 +184,26 @@ class faiHook extends plugin
 
     /* File download requested */
     if(isset($_GET['getFAIHook'])){
-      if(isset($this->SubObjects[$_GET['getFAIHook']])){
-        $obj = $this->SubObjects[$_GET['getFAIHook']];
+      if(isset($this->SubObjects[base64_decode($_GET['getFAIHook'])])){
+        $obj = $this->SubObjects[base64_decode($_GET['getFAIHook'])];
         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
-        send_binary_content($obj['FAIscript'],$obj['cn'].".FAIhook");
+        send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIhook");
       }
     }
 
     /* Edit entries via GET */
     if(isset($_GET['act']) && isset($_GET['id'])){
-      if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
-        $obj = $this->SubObjects[$_GET['id']];
-          if($obj['status'] == "FreshLoaded"){
+      if($_GET['act'] == "edit" && isset($this->SubObjects[base64_decode($_GET['id'])])){
+        $obj = $this->SubObjects[base64_decode($_GET['id'])];
+        if($obj['status'] == "FreshLoaded"){
           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
         }
         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
+        $this->dialog->FAIstate = $this->FAIstate;
         $this->dialog->acl = $this->acl;
-        session::set('objectinfo',$obj['dn']);
+        $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
+        $this->dialog->set_acl_category("fai");
+        set_object_info($obj['dn']);
         $this->dialog->parent = &$this;
         $this->is_dialog=true;
       }
@@ -229,6 +217,7 @@ class faiHook extends plugin
       $c_dn = $this->acl_base_for_current_object($this->dn);
       $this->dialog= new $this->subClassName($this->config,"new");
       $this->dialog->set_acl_base($c_dn);
+      $this->dialog->FAIstate = $this->FAIstate;
       $this->dialog->set_acl_category("fai");
       $this->dialog->parent = &$this;
       $this->is_dialog=true;
@@ -319,12 +308,12 @@ class faiHook extends plugin
       $act = "";
 
       /* Check if this object is freezed, in this case hide the delete icon */
-      if($this->FAIstate == "freeze"){
-        $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
+      if(preg_match("/^freeze/", $this->FAIstate)){
+        $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
       }else{
-        $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
+        $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
         if(preg_match("/d/",$acl)){
-          $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("Delete")."' alt='"._("Delete")."'>";
+          $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s'  title='"._("Delete")."' alt='"._("Delete")."'>";
         }
       }
 
@@ -333,7 +322,7 @@ class faiHook extends plugin
       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
         $down = "";
       }else{
-        $down = "<a href='?plug=".$_GET['plug']."&getFAIHook=".$key."'>
+        $down = "<a href='?plug=".$_GET['plug']."&getFAIHook=".base64_encode($key)."'>
           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
           </a>";
       }
@@ -342,7 +331,7 @@ class faiHook extends plugin
       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn")  ;
       if(preg_match("/r/",$s_acl)){
 
-        $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".$key."'>".$name['name']."</a>";
+        $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".base64_encode($key)."'>".$name['name']."</a>";
         $divlist->AddEntry(array( array("string"=>$edit_link),
               array("string"=>$down , "attach" => "style='width:20px;'"),
               array("string"=>str_replace("%s",base64_encode($key),$act),
@@ -369,7 +358,7 @@ class faiHook extends plugin
     $tmp = $this->plInfo();
       
     $c_dn = $this->acl_base_for_current_object($this->dn);
-    $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && $this->FAIstate!="freeze");
+    $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && !preg_match('/^freeze/', $this->FAIstate));
     foreach($tmp['plProvidedAcls'] as $name => $translation){
       $smarty->assign($name."ACL",$this->getacl($name));
     }
@@ -384,19 +373,19 @@ class faiHook extends plugin
     $a_return=array();
     foreach($this->SubObjects as $obj){
       if($obj['status'] != "delete"){
+
+        $cn   = stripslashes($obj['cn']);
+        $desc = "";
+
+        if((isset($obj['description']))&&(!empty($obj['description']))){
+          $desc = " [".stripslashes($obj['description'])."]"; 
+        }
+
         if($use_dns){
-          if((isset($obj['description']))&&(!empty($obj['description']))){
-            $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
-          }else{
-            $a_return[$obj['cn']]['name']= $obj['cn'];
-          }
+          $a_return[$obj['cn']]['name']= $cn.$desc;
           $a_return[$obj['cn']]['dn']= $obj['dn'];
         }else{
-          if((isset($obj['description']))&&(!empty($obj['description']))){
-            $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
-          }else{
-            $a_return[$obj['cn']]= $obj['cn'];
-          }
+          $a_return[$obj['cn']] =  $cn.$desc;
         }
       }
     }
@@ -408,27 +397,20 @@ class faiHook extends plugin
    */
   function remove_from_parent()
   {
-    $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);
-
-    new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
+    if($this->acl_is_removeable()){
+      $ldap = $this->config->get_ldap_link();
+      $ldap->cd ($this->dn);
+      $release = $this->parent->parent->fai_release;
+      $use_dn = preg_replace("/".preg_quote(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'];
+      foreach($this->SubObjects as $name => $obj){
+        $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
+        FAI::prepare_to_save_FAI_object($use_dn,array(),true);
       }
-      FAI::prepare_to_save_FAI_object($use_dn,array(),true);
+      $this->handle_post_events("remove");
     }
-    $this->handle_post_events("remove");    
   }
 
 
@@ -436,13 +418,8 @@ class faiHook extends plugin
    */
   function save_object()
   {
-    if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
+    if((isset($_POST['FAIhook_posted'])) && !preg_match("/^freeze/", $this->FAIstate)){
       plugin::save_object();
-      foreach($this->attributes as $attrs){
-        if(isset($_POST[$attrs])){
-          $this->$attrs = $_POST[$attrs];
-        }
-      }
     }
   }
 
@@ -453,6 +430,17 @@ class faiHook 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('faiHookRDN').get_ou('faiBaseRDN').$release;
+      $res = faiManagement::check_class_name("FAIhook",$this->cn,$new_dn);
+      if(isset($res[$this->cn])){
+        $message[] = msgPool::duplicated(_("Name"));
+      }
+    }
+
     return ($message);
   }
 
@@ -465,9 +453,6 @@ class faiHook 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);
@@ -486,18 +471,7 @@ class faiHook extends plugin
 
     /* We do not need to save untouched objects */
     foreach($this->SubObjects as $name => $obj){
-      if($obj['status'] == "FreshLoaded"){
-        unset($this->SubObjects[$name]);
-      }
-    }
-
-    foreach($this->SubObjects as $name => $obj){
-      if($obj['status'] == "delete"){
-        $Objects[$name] = $obj; 
-      }
-    }
-    foreach($this->SubObjects as $name => $obj){
-      if($obj['status'] != "delete"){
+      if($obj['status'] != "FreshLoaded"){
         $Objects[$name] = $obj; 
       }
     }
@@ -511,7 +485,8 @@ class faiHook extends plugin
       $tmp = array();
       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
       foreach($attributes as $attrs){
-        if(empty($obj[$attrs])){
+        if(!isset($obj[$attrs])) continue; 
+        if($obj[$attrs] == ""){
           $obj[$attrs] = array();
         }
         if(!is_array($obj[$attrs])){
@@ -555,29 +530,17 @@ class faiHook 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()){
+    $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
+    foreach($res as $obj){
 
-      /* Skip objects, that are tagged as removed */
-      if(isset($object['FAIstate'][0])){
-        if(preg_match("/removed$/",$object['FAIstate'][0])){
-          continue;
-        }
-      }
+      /* Skip not relevant objects */
+      if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
 
-      /* Set status for save management */
       $objects = array();
       $objects['status']      = "edited";
-      $objects['dn']          = $object['dn'];
+      $objects['dn']          = $obj['dn'];
       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
-
       $this->SubObjects[$objects['cn']] = $objects;
     }
   }
@@ -599,6 +562,34 @@ class faiHook 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');
+    }
+  }
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: