Code

Replaced in_array calls with in_array_strict
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiPartitionTableEntry.inc
index a49dad40e076d6efe9783d5d3ad716953d313943..797afc1d0759beae8b017c9dc13c68870558763b 100644 (file)
@@ -14,63 +14,33 @@ class faiPartitionTableEntry extends plugin
   var $UsedAttrs            = array();
   var $is_edit              = false;
   var $old_cn               = "";
-  var $status               = "new";
-  var $deletePartitions     = array();
+  var $parent               = null;
 
-  var $FAIstate             = "";
-
-  function faiPartitionTableEntry (&$config, $dn= NULL,$usedDiskNames=array(),$disk=false)
+  function faiPartitionTableEntry (&$config, $dn= NULL,&$parent,$disk=false)
   {
     plugin::plugin ($config, $dn);
     $this->UsedAttrs  = array("cn","description","FAIpartitionType","FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
                          "FAImountOptions","FAIfsOptions","FAIpartitionFlags");
 
-    /* Default status is new */
-    $this->status = "new";    
+    $this->parent = $parent;
+
     /* We want to edit an entry */
     if($disk){
 
-      /* Set disk status */      
-      $this->status = $disk['status'];
-
       /* Walk through partitions */
       foreach($disk['partitions'] as $name => $values){
 
-        /* If a partition is already marked as delete, attach it to deletePartitions only. */
-        if($values['status'] == "delete"){
-          unset($disk['partitions'][$name]);
-          $this->deletePartitions[]=$values;
-        }else{
+        $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
 
-          /* Set status, to know which partition must be deleted from ldap
-             new    : Neu partition entry // save
-             edited : Update partition entry in ldap
-             deleted: Remove partition from ldap
-           */
-    
-          /* If status is not new, set to edit mode. 
-           * New means that this partition currently wasn't saved to ldap.
-           */
-          if($disk['partitions'][$name]['status']!="new"){
-            $disk['partitions'][$name]['status']="edited";
-          }
-     
-          $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
-          /* Assign empty attributes, if attribute is missing */
-          foreach($this->UsedAttrs as $attr){
-            if(!isset($values[$attr])){
-              $disk['partitions'][$name][$attr]="";  
-            }
+        /* Assign empty attributes, if attribute is missing */
+        foreach($this->UsedAttrs as $attr){
+          if(!isset($values[$attr])){
+            $disk['partitions'][$name][$attr]="";  
           }
+        }
 
-          if (preg_match('/^_/', $disk['partitions'][$name]['FAIfsType'])){
-            $disk['partitions'][$name]['FAIfsType']= preg_replace('/^_/', '', $disk['partitions'][$name]['FAIfsType']);
-            $disk['partitions'][$name]['FAIencrypted']= true;
-          } else {
-            $disk['partitions'][$name]['FAIencrypted']= false;
-          }
+        if (preg_match('/^_/', $disk['partitions'][$name]['FAIfsType'])){
+          $disk['partitions'][$name]['FAIfsType']= preg_replace('/^_/', '', $disk['partitions'][$name]['FAIfsType']);
         }
       }
 
@@ -99,7 +69,7 @@ class faiPartitionTableEntry extends plugin
      * And Checkbox selection
      */
     foreach($this->attributes as $attrs){
-      $smarty->assign($attrs,$this->$attrs);
+      $smarty->assign($attrs,set_post($this->$attrs));
       if($this->$attrs){
         $smarty->assign($attrs."CHK"," ");
       }else{
@@ -112,8 +82,8 @@ class faiPartitionTableEntry extends plugin
      * The number specifies the index we want to delete
      */
     foreach($_POST as $name => $value){
-      if((preg_match("/Delete_.*/",$name)) && $this->acl_is_removeable()){
-        $tmp = split("_",$name);
+      if((preg_match("/Delete_.*/",$name)) && $this->acl_is_removeable() && !preg_match("/freeze/i",$this->FAIstate)){
+        $tmp = explode("_",$name);
         $s_action = "remove";
         $s_entry  = $tmp[1]; 
       }
@@ -123,14 +93,8 @@ class faiPartitionTableEntry extends plugin
      * We must sort the index again, else we possibly got problems 
      * with partitions order.
      */
-    if($s_action == "remove" && $this->acl_is_removeable()){
-      if($this->partitions[$s_entry]['status'] == "edited"){
-        $this->deletePartitions[$s_entry]= $this->partitions[$s_entry];
-        $this->deletePartitions[$s_entry]['FAIpartitionNr']=$s_entry;
-        unset($this->partitions[$s_entry]);
-      }else{
-        unset($this->partitions[$s_entry]);
-      }
+    if($s_action == "remove" && $this->acl_is_removeable() && !preg_match("/freeze/i",$this->FAIstate)){
+      unset($this->partitions[$s_entry]);
       $tmp= array();
       foreach($this->partitions as $part){
         $tmp[count($tmp)+1]=$part;
@@ -141,13 +105,11 @@ class faiPartitionTableEntry extends plugin
     /* To add a partitions we only append an empty 
      *  array to the already defined partitions.
      */
-    if(isset($_POST['AddPartition']) && $this->acl_is_createable()){
+    if(isset($_POST['AddPartition']) && $this->acl_is_createable() && !preg_match("/freeze/i",$this->FAIstate)){
       foreach($this->UsedAttrs as $attr){
         $tmp[$attr]                = "";     
       }
       $tmp["old_cn"]               = "";     
-      $tmp["FAIencrypted"] = FALSE;     
-      $tmp['status']="new";
       $this->partitions[count($this->partitions)+1]=$tmp;
     }
 
@@ -155,23 +117,18 @@ class faiPartitionTableEntry extends plugin
      */
     $smarty->assign("setup", $this->generateParts());
     foreach($this->attributes as $attr){
-      $smarty->assign($attr,$this->$attr);
+      $smarty->assign($attr,set_post($this->$attr));
     }
 
     $tmp = $this->plInfo();
     $sacl = "";
     foreach($tmp['plProvidedAcls'] as $name => $translated){
-
-      $acl = $this->getacl($name);
-      $sacl .= $acl;
-      if($this->FAIstate == "freezed"){
-        $acl = preg_replace("/w/","",$acl);
-      }
+      $acl = $this->getacl($name, preg_match("/freeze/i",$this->FAIstate));
       $smarty->assign($name."ACL",$acl);
     }
-    $smarty->assign("allowSave",$sacl);
    
     $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
+    $smarty->assign("freeze",preg_match("/freeze/i",$this->FAIstate));
  
     /* Fetch template and show the result
      */
@@ -190,13 +147,12 @@ class faiPartitionTableEntry extends plugin
     if (count($this->partitions)){
       $str .= "<tr>";
       $str .= "<td><b>"._("Type")."</b></td>";
-      $str .= "<td><b>"._("FS type")."</b></td>";
+      $str .= "<td><b>"._("Filesystem type")."</b></td>";
       $str .= "<td><b>"._("Mount point")."</b></td>";
       $str .= "<td><b>"._("Size in MB")."</b></td>";
       $str .= "<td><b>"._("Mount options")."</b></td>";
-      $str .= "<td><b>"._("FS option")."</b></td>";
+      $str .= "<td><b>"._("Filesystem option")."</b></td>";
       $str .= "<td><b>"._("Preserve")."</b></td>";
-      $str .= "<td><b>"._("Encrypted")."</b></td>";
       $str .= "<td>&nbsp;</td>";
       $str .= "</tr>";
     }
@@ -207,112 +163,99 @@ class faiPartitionTableEntry extends plugin
      * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
      */
     foreach($this->partitions as $key => $part){
-      
+
       $dis = "";
       if($part['FAIpartitionFlags'] == "preserve"){
         $dis = " disabled ";
       }
 
       $disableALL = "";
-      if($this->FAIstate =="freeze"){
+      if (preg_match("/freeze/", $this->FAIstate)){
         $disableALL = " disabled ";
       }
 
-      if($part['status']!="delete"){
-
-        /* Generate Partition select box  
-         */
-        $PartitionType = "";
-        if($this->acl_is_writeable("FAIpartitionType")){
-          $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
-          foreach($PartitionTypes as $type => $PTname){
-            if($part['FAIpartitionType'] == $type){
-              $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
-            }else{
-              $PartitionType .="<option value='".$type."'>".$PTname."</option>";
-            }
-          }        
-          $PartitionType.="</select>";   
-        }elseif($this->acl_is_readable("FAIpartitionType")){
-          $PartitionType = $part['FAIpartitionType'];
-        }
+      /* Generate Partition select box  
+       */
+      $PartitionType = "";
+      if($this->acl_is_writeable("FAIpartitionType")){
+        $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
+        foreach($PartitionTypes as $type => $PTname){
+          if($part['FAIpartitionType'] == $type){
+            $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
+          }else{
+            $PartitionType .="<option value='".$type."'>".$PTname."</option>";
+          }
+        }        
+        $PartitionType.="</select>";   
+      }elseif($this->acl_is_readable("FAIpartitionType")){
+        $PartitionType = $part['FAIpartitionType'];
+      }
 
 
-        /* Generate fsType select box  
-         */
-        $FAIfsType= "";
-        if($this->acl_is_writeable("FAIfsType")){
-          $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
-          foreach($FAIfsTypes as $type){
-            if($part['FAIfsType'] == $type){
-              $FAIfsType  .="<option value='".$type."' selected >".$type."</option>";
-            }else{
-              $FAIfsType .="<option value='".$type."'>".$type."</option>";
-            }
-          }        
-          $FAIfsType.="</select>";   
-        }elseif($this->acl_is_readable("FAIfsType")){
-          $FAIfsType = $part['FAIfsType'];
-        }
+      /* Generate fsType select box  
+       */
+      $FAIfsType= "";
+      if($this->acl_is_writeable("FAIfsType")){
+        $FAIfsType= "<select size='1' name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
+        foreach($FAIfsTypes as $type){
+          if($part['FAIfsType'] == $type){
+            $FAIfsType  .="<option value='".$type."' selected >".$type."</option>";
+          }else{
+            $FAIfsType .="<option value='".$type."'>".$type."</option>";
+          }
+        }        
+        $FAIfsType.="</select>";   
+      }elseif($this->acl_is_readable("FAIfsType")){
+        $FAIfsType = $part['FAIfsType'];
+      }
 
-        foreach(array("FAImountPoint","FAIpartitionSize","FAImountOptions","FAIfsOptions") as $attr){
-          $$attr  = "";
-          if($this->acl_is_writeable($attr)){
-            $$attr = "<input name='".$attr."_".$key."'  ".$disableALL."  ".$dis."
-                        value='".$part[$attr]."'    id='".$attr."_".$key."'>";
+      foreach(array("FAImountPoint","FAIpartitionSize","FAImountOptions","FAIfsOptions") as $attr){
+        $$attr  = "";
+        if($this->acl_is_writeable($attr)){
+          $$attr = "<input type='text' name='".$attr."_".$key."'  ".$disableALL."  ".$dis."
+            value='".$part[$attr]."'    id='".$attr."_".$key."'>";
 
-          }elseif($this->acl_is_readable($attr)){
-            $$attr = $part[$attr];
-          }
+        }elseif($this->acl_is_readable($attr)){
+          $$attr = $part[$attr];
         }
+      }
 
 
-        $str .= "\n<tr>";
-        $str .= "\n<td>".$PartitionType."</td>";
-        $str .= "\n<td>".$FAIfsType."</td>";
-        $str .= "\n<td>".$FAImountPoint."</td>";
-        $str .= "\n<td>".$FAIpartitionSize."</td>";
-        $str .= "\n<td>".$FAImountOptions."</td>";
-        $str .= "\n<td>".$FAIfsOptions."</td>";
+      $str .= "\n<tr>";
+      $str .= "\n<td>".$PartitionType."</td>";
+      $str .= "\n<td>".$FAIfsType."</td>";
+      $str .= "\n<td>".$FAImountPoint."</td>";
+      $str .= "\n<td>".$FAIpartitionSize."</td>";
+      $str .= "\n<td>".$FAImountOptions."</td>";
+      $str .= "\n<td>".$FAIfsOptions."</td>";
 
-        $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
-                                 "changeState('FAIpartitionSize_".$key."') ; ".
-                                 "changeState('FAImountOptions_".$key."') ; ".
-                                 "changeState('FAIfsType_".$key."') ; ".
-                                 "changeState('FAIfsOptions_".$key."') ; \"";
-  
-        if($this->acl_is_writeable("FAIpartitionFlags")){
-          if($part['FAIpartitionFlags']!=false){
-            $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
-          }else{
-            $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
-          }
-          if($part['FAIencrypted']!=false){
-            $str .= "\n<td><input type='checkbox' name='FAIencrypted_".$key."' value='encrypted' checked></td>";
-          }else{
-            $str .= "\n<td><input type='checkbox' name='FAIencrypted_".$key."' value='encrypted'></td>";
-          }
+      $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
+        "changeState('FAIpartitionSize_".$key."') ; ".
+        "changeState('FAImountOptions_".$key."') ; ".
+        "changeState('FAIfsType_".$key."') ; ".
+        "changeState('FAIfsOptions_".$key."') ; \"";
+
+      if($this->acl_is_writeable("FAIpartitionFlags")){
+        if($part['FAIpartitionFlags']!=false){
+          $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
         }else{
-          if($part['FAIpartitionFlags']!=false){
-            $str .= "<td>"._("False")."</td>";
-          }else{
-            $str .= "<td>"._("True")."</td>";
-          }
-          if($part['FAIencrypted']!=false){
-            $str .= "<td>"._("False")."</td>";
-          }else{
-            $str .= "<td>"._("True")."</td>";
-          }
+          $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
         }
-        if($this->acl_is_removeable()){
-          $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";    
+      }else{
+        if($part['FAIpartitionFlags']!=false){
+          $str .= "<td>"._("False")."</td>";
         }else{
-          $str .= "\n<td><input type='submit' name='restricted' disabled value='"._("Remove")."'></td>";    
+          $str .= "<td>"._("True")."</td>";
         }
-        $str .= "\n</tr>";    
       }
+      if($this->acl_is_removeable()){
+        $str .= "\n<td><button type='submit' {$disableALL} name='Delete_".$key."'>"._("Remove")."</button></td>";    
+      }else{
+        $str .= "\n<td><button type='button' disabled name='restricted'>"._("Remove")."</button></td>";    
+      }
+      $str .= "\n</tr>";    
     }
-    
+
     if(!empty($str)){
       $str = "<table summary='' style='width:100%'>".$str."</table>";
     }
@@ -327,28 +270,19 @@ class faiPartitionTableEntry extends plugin
 
     /* Attach partitions */
     foreach($this->partitions as $key=>$val) {
-      if (isset($this->partitions[$key]['FAIencrypted']) && $this->partitions[$key]['FAIencrypted']){
-        unset ($this->partitions[$key]['FAIencrypted']);
-        $this->partitions[$key]['FAIfsType']= "_".$this->partitions[$key]['FAIfsType'];
-      }
       $this->partitions[$key]['FAIpartitionNr']=$key;
-    }
-
-    /* Attach deleted */
-    foreach($this->deletePartitions as $key=>$val) {
-      $this->partitions[$key."-delete"]=$val;
-      $this->partitions[$key."-delete"]['status']="delete";
+      $this->partitions[$key]['cn']=$key;
     }
 
     $tmp['description'] = $this->DISKdescription;
     $tmp['partitions']  = $this->partitions;
-    $tmp['status']      = $this->status;
 
     /* If hdd name has changed, tell partitionTable to rename it */
     if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
       $tmp['rename']['from']  = $this->old_cn;
       $tmp['rename']['to']    = $this->DISKcn;
     }
+    $tmp['FAIdiskType'] = "old";
     return($tmp);
   }
 
@@ -356,31 +290,23 @@ class faiPartitionTableEntry extends plugin
   /* Save data to object */
   function save_object()
   {
-    if((isset($_POST['TableEntryFrameSubmitted'])) && ($this->FAIstate != "freeze") ){
+    if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
       plugin::save_object();
 
       foreach($this->partitions as $key => $part){
         foreach($this->UsedAttrs as $attrs){
-
+          if(in_array_strict($attrs,array('cn','description'))) continue;
+          
           if($this->acl_is_writeable($attrs)){
 
             if(isset($_POST[$attrs."_".$key])){
-              $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
+              $this->partitions[$key][$attrs] = get_post($attrs."_".$key);
             }else{
               $this->partitions[$key][$attrs] = false;
             }
           }
         }
 
-        if($this->acl_is_writeable("FAIencrypted")){
-
-          if(isset($_POST["FAIencrypted_".$key])){
-            $this->partitions[$key]["FAIencrypted"] = $_POST["FAIencrypted_".$key];
-          }else{
-            $this->partitions[$key]["FAIencrypted"] = false;
-          }
-        }
-
         /* Adapt ext3 to have -j option */
         if ($this->partitions[$key]["FAIfsType"] == "ext3") {
           $this->partitions[$key]["FAIfsOptions"]= preg_replace('/\s*-j\s*/', '', $this->partitions[$key]["FAIfsOptions"]);
@@ -399,8 +325,16 @@ class faiPartitionTableEntry extends plugin
     $message= plugin::check();
   
     /* Check for an empty disk name */
-    if(empty($this->DISKcn)){
-      $message[] = _("Please specify a valid disc name.");
+    $d = trim($this->DISKcn);
+    if($d == "" ){
+      $message[] = msgPool::required(_("Name"));
+    }
+    if(preg_match("/[^a-z0-9_\-]/i",$d)){
+      $message[] = msgPool::invalid(_("Name"),$d,"/[a-z0-9_\-]/i");
+    }
+
+    if($this->old_cn != $this->DISKcn && in_array_strict($this->DISKcn,$this->parent->getUsedDiskNames())){
+      $message[] = msgPool::duplicated(_("Name"));
     }
        
     /* check every partition.
@@ -422,45 +356,46 @@ class faiPartitionTableEntry extends plugin
         continue;
       }
  
-      if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
+      if((in_array_strict($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
         $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
       }
 
       if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
         if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
-          $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key));
+          $message[]= msgPool::invalid(sprintf(_("partition %s mount point"),$key));
         }
       }
       if($part['FAIfsType'] == "swap"){
-        if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
+        if(in_array_strict($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
           $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
         }
       }
       if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
-        $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
+        $message[]=_("Please use 'swap' as mount point, if 'swap' is used as file system type.");
       }
 
-      $tmp = split("-",$part['FAIpartitionSize']);
+      $tmp = explode("-",$part['FAIpartitionSize']);
+
       switch (count($tmp)){
         case 0:
-                $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
+                $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
                 break;
         case 1:
-                if (!tests::is_id(is_id($tmp[0])) &&(!empty($tmp[1]))){
-                  $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
+                if (!tests::is_id($tmp[0]) || empty($tmp[0])){
+                  $message[]=  msgPool::invalid(sprintf(_("partition %s size"),$key));
                 }
                 break;
                 
         case 2:
-                if((!tests::is_id($tmp[0]))&&(!tests::is_id($tmp[1])) &&(!empty($tmp[1]))){
-                  $message[]=sprintf(_("Please enter a valid range for partition %s."),($key)); 
-                }elseif($tmp[0]>=$tmp[1]){
-                  $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
+                if( !tests::is_id($tmp[0]) && !tests::is_id($tmp[1]) && !empty($tmp[1]) ){
+                  $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
+                }elseif(!empty($tmp[1]) && $tmp[0]>=$tmp[1]){
+                  $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
                 }
                 break;
 
         default:
-                $message[]=sprintf(_("Please enter a range for partition size for partition %s."),($key));
+                $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
       }
 
       foreach($this->UsedAttrs as $attrs){
@@ -504,7 +439,6 @@ class faiPartitionTableEntry extends plugin
             "FAIpartitionSize"  => _("Partition size"),
             "FAImountOptions"   => _("Mount options"),
             "FAIfsOptions"      => _("File system options"),
-            "FAIencrypted"      => _("File system encryption"),
             "FAIpartitionFlags" => _("Partition flags"))
           ));
   }