Code

Updated lvm handling - Fai partitions.
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiDiskEntry.inc
1 <?php
3 class faiDiskEntry extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account= TRUE;
7   var $attributes   = array("DISKcn","DISKdescription");
8   var $objectclasses= array();
10   var $DISKcn              = "";
11   var $DISKdescription     = "";
12   var $DISKFAIdiskOptions     = "";
13   
14   var $partitions           = array();
15   var $UsedAttrs            = array();
16   var $is_edit              = false;
17   var $old_cn               = "";
18   var $status               = "new";
19   var $deletePartitions     = array();
20   var $fstabkey             = "device";
21   var $disklabel            = "msdos";
23   var $FAIstate             = "";
24   var $FAIdiskType          = "disk";
26   function faiDiskEntry (&$config, $dn= NULL,$usedDiskNames=array(),$disk=false)
27   {
28     plugin::plugin ($config, $dn);
29     $this->UsedAttrs  = array("cn","description","FAIpartitionType","FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
30                          "FAImountOptions","FAIfsOptions","FAIpartitionFlags");
32     /* Default status is new */
33     $this->status = "new";    
34  
35     /* We want to edit an entry */
36     if($disk){
38       /* Set disk status */      
39       $this->status = $disk['status'];
41       /* Walk through partitions */
42       foreach($disk['partitions'] as $name => $values){
44         /* If a partition is already marked as delete, attach it to deletePartitions only. */
45         if($values['status'] == "delete"){
46           unset($disk['partitions'][$name]);
47           $this->deletePartitions[]=$values;
48         }else{
50           /* Set status, to know which partition must be deleted from ldap
51              new    : Neu partition entry // save
52              edited : Update partition entry in ldap
53              deleted: Remove partition from ldap
54            */
55     
56           /* If status is not new, set to edit mode. 
57            * New means that this partition currently wasn't saved to ldap.
58            */
59           if($disk['partitions'][$name]['status']!="new"){
60             $disk['partitions'][$name]['status']="edited";
61           }
62      
63           $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
64  
65           /* Assign empty attributes, if attribute is missing */
66           foreach($this->UsedAttrs as $attr){
67             if(!isset($values[$attr])){
68               $disk['partitions'][$name][$attr]="";  
69             }
70           }
72           if (preg_match('/^_/', $disk['partitions'][$name]['FAIfsType'])){
73             $disk['partitions'][$name]['FAIfsType']= preg_replace('/^_/', '', $disk['partitions'][$name]['FAIfsType']);
74           }
75         }
76       }
78       /* Set default attributes */
79       $this->DISKcn          = $disk['cn'];
80       $this->DISKdescription = $disk['description'];
81       $this->partitions      = $disk['partitions'];
82       $this->is_edit         = true;
83       $this->old_cn          = $disk['cn'];
85       /* Move faiDiskOption */
86       if (isset($disk['FAIdiskOption'])){
87         foreach($disk['FAIdiskOption'] as $option) {
88           if (preg_match("/fstabkey:(device|label|uuid)/", $option)){
89             $this->fstabkey= preg_replace("/^.*fstabkey:(device|label|uuid).*$/", "$1", $option);
90             break;
91           }
92           if (preg_match("/disklabel:(msdos|gpt)/", $option)){
93             $this->disklabel= preg_replace("/^.*disklabel:(msdos|gpt).*$/", "$1", $option);
94             break;
95           }
96         }
97       } else {
98         $this->fstabkey= "device";
99       }
101     }
102   }
105   function execute()
106   {
107     /* Call parent execute */
108     plugin::execute();
110     /* Fill templating stuff */
111     $smarty     = get_smarty();
112     $s_action   = "";
113     $s_entry    = "";
114     $display    = "";
116     /* Load parameters */
117     if (isset($_POST['disklabel']) && preg_match("/^(msdos|gpt)$/", $_POST['disklabel'])){
118       $this->disklabel= $_POST['disklabel'];
119     }
120     if (isset($_POST['fstabkey']) && preg_match("/^(device|label|uuid)$/", $_POST['fstabkey'])){
121       $this->fstabkey= $_POST['fstabkey'];
122     }
123     
124     /* Assign values 
125      * And Checkbox selection
126      */
127     foreach($this->attributes as $attrs){
128       $smarty->assign($attrs,$this->$attrs);
129       if($this->$attrs){
130         $smarty->assign($attrs."CHK"," ");
131       }else{
132         $smarty->assign($attrs."CHK"," disabled ");
133       }
134     }
136     /* Check all Posts if there is something usefull for us,
137      * For example : Delete is posted as Delete_1 
138      * The number specifies the index we want to delete
139      */
140     foreach($_POST as $name => $value){
141       if((preg_match("/Delete_.*/",$name)) && $this->acl_is_removeable() && !preg_match("/freeze/i",$this->FAIstate)){
142         $tmp = split("_",$name);
143         $s_action = "remove";
144         $s_entry  = $tmp[1]; 
145       }
146     }
148     /* To remove a partition we unset the index posted.
149      * We must sort the index again, else we possibly got problems 
150      * with partitions order.
151      */
152     if($s_action == "remove" && $this->acl_is_removeable() && !preg_match("/freeze/i",$this->FAIstate)){
153       if($this->partitions[$s_entry]['status'] == "edited"){
154         $this->deletePartitions[$s_entry]= $this->partitions[$s_entry];
155         $this->deletePartitions[$s_entry]['FAIpartitionNr']=$s_entry;
156         unset($this->partitions[$s_entry]);
157       }else{
158         unset($this->partitions[$s_entry]);
159       }
160       $tmp= array();
161       foreach($this->partitions as $part){
162         $tmp[count($tmp)+1]=$part;
163       }
164       $this->partitions = $tmp;
165     }
167     /* To add a partitions we only append an empty 
168      *  array to the already defined partitions.
169      */
170     if(isset($_POST['AddPartition']) && $this->acl_is_createable() && !preg_match("/freeze/i",$this->FAIstate)){
171       foreach($this->UsedAttrs as $attr){
172         $tmp[$attr]                = "";     
173       }
174       $tmp["old_cn"]               = "";     
175       $tmp['status']="new";
176       $this->partitions[count($this->partitions)+1]=$tmp;
177     }
179     /* $setup contains a table with the partitions. 
180      */
181     $smarty->assign("setup", $this->generateParts());
182     foreach($this->attributes as $attr){
183       $smarty->assign($attr,$this->$attr);
184     }
186     $tmp = $this->plInfo();
187     $sacl = "";
188     foreach($tmp['plProvidedAcls'] as $name => $translated){
189       $acl = $this->getacl($name, preg_match("/freeze/i",$this->FAIstate));
190       $smarty->assign($name."ACL",$acl);
191     }
192    
193     $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
194     $smarty->assign("freeze",preg_match("/freeze/i",$this->FAIstate));
196     // Fill boxes
197     $smarty->assign("fstabkeys", array("device" => _("Device"), "label" => _("Label"), "uuid" => _("UUID")));
198     $smarty->assign("disklabels", array("msdos" => "MSDOS", "gpt" => "GPT"));
199     $smarty->assign("fstabkey", $this->fstabkey);
200     $smarty->assign("disklabel", $this->disklabel);
201  
202     /* Fetch template and show the result
203      */
204     $display.= $smarty->fetch(get_template_path('faiDiskEntry.tpl', TRUE));
205     return($display);
206   }
208   function generateParts()
209   {
210     /* Define Arrays with allowed syntax */
211     $PartitionTypes   = array("primary"=>_("primary"),"logical"=>_("logical"));
212     $FAIfsTypes       = array("ext2","ext3","xfs","swap","reiser","dosfat16","winfat32");
214     /* Display Header */
215     $str = "";
216     if (count($this->partitions)){
217       $str .= "<tr>";
218       $str .= "<td><b>"._("Type")."</b></td>";
219       $str .= "<td><b>"._("FS type")."</b></td>";
220       $str .= "<td><b>"._("Mount point")."</b></td>";
221       $str .= "<td><b>"._("Size in MB")."</b></td>";
222       $str .= "<td><b>"._("Mount options")."</b></td>";
223       $str .= "<td><b>"._("FS option")."</b></td>";
224       $str .= "<td><b>"._("Preserve")."</b></td>";
225       $str .= "<td>&nbsp;</td>";
226       $str .= "</tr>";
227     }
228     
229     /* Walk through all defined partitions.
230      * Create a new row for each partition and append it to 
231      *  the header defined above.
232      * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
233      */
234     foreach($this->partitions as $key => $part){
235       
236       $dis = "";
237       if($part['FAIpartitionFlags'] == "preserve"){
238         $dis = " disabled ";
239       }
241       $disableALL = "";
242       if (preg_match("/freeze/", $this->FAIstate)){
243         $disableALL = " disabled ";
244       }
246       if($part['status']!="delete"){
248         /* Generate Partition select box  
249          */
250         $PartitionType = "";
251         if($this->acl_is_writeable("FAIpartitionType")){
252           $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
253           foreach($PartitionTypes as $type => $PTname){
254             if($part['FAIpartitionType'] == $type){
255               $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
256             }else{
257               $PartitionType .="<option value='".$type."'>".$PTname."</option>";
258             }
259           }        
260           $PartitionType.="</select>";   
261         }elseif($this->acl_is_readable("FAIpartitionType")){
262           $PartitionType = $part['FAIpartitionType'];
263         }
266         /* Generate fsType select box  
267          */
268         $FAIfsType= "";
269         if($this->acl_is_writeable("FAIfsType")){
270           $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
271           foreach($FAIfsTypes as $type){
272             if($part['FAIfsType'] == $type){
273               $FAIfsType  .="<option value='".$type."' selected >".$type."</option>";
274             }else{
275               $FAIfsType .="<option value='".$type."'>".$type."</option>";
276             }
277           }        
278           $FAIfsType.="</select>";   
279         }elseif($this->acl_is_readable("FAIfsType")){
280           $FAIfsType = $part['FAIfsType'];
281         }
283         foreach(array("FAImountPoint","FAIpartitionSize","FAImountOptions","FAIfsOptions") as $attr){
284           $$attr  = "";
285           if($this->acl_is_writeable($attr)){
286             $$attr = "<input name='".$attr."_".$key."'  ".$disableALL."  ".$dis."
287                         value='".$part[$attr]."'    id='".$attr."_".$key."'>";
289           }elseif($this->acl_is_readable($attr)){
290             $$attr = $part[$attr];
291           }
292         }
295         $str .= "\n<tr>";
296         $str .= "\n<td>".$PartitionType."</td>";
297         $str .= "\n<td>".$FAIfsType."</td>";
298         $str .= "\n<td>".$FAImountPoint."</td>";
299         $str .= "\n<td>".$FAIpartitionSize."</td>";
300         $str .= "\n<td>".$FAImountOptions."</td>";
301         $str .= "\n<td>".$FAIfsOptions."</td>";
303         $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
304                                  "changeState('FAIpartitionSize_".$key."') ; ".
305                                  "changeState('FAImountOptions_".$key."') ; ".
306                                  "changeState('FAIfsType_".$key."') ; ".
307                                  "changeState('FAIfsOptions_".$key."') ; \"";
308   
309         if($this->acl_is_writeable("FAIpartitionFlags")){
310           if($part['FAIpartitionFlags']!=false){
311             $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
312           }else{
313             $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
314           }
315         }else{
316           if($part['FAIpartitionFlags']!=false){
317             $str .= "<td>"._("False")."</td>";
318           }else{
319             $str .= "<td>"._("True")."</td>";
320           }
321         }
322         if($this->acl_is_removeable()){
323           $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";    
324         }else{
325           $str .= "\n<td><input type='submit' name='restricted' disabled value='"._("Remove")."'></td>";    
326         }
327         $str .= "\n</tr>";    
328       }
329     }
330     
331     if(!empty($str)){
332       $str = "<table summary='' style='width:100%'>".$str."</table>";
333     }
334     return($str);
336   }
338   function save()
339   {
340     $tmp = array();
341     $tmp['cn']          = $this->DISKcn;
343     /* Attach partitions */
344     foreach($this->partitions as $key=>$val) {
345       $this->partitions[$key]['FAIpartitionNr']=$key;
346     }
348     /* Attach deleted */
349     foreach($this->deletePartitions as $key=>$val) {
350       $this->partitions[$key."-delete"]=$val;
351       $this->partitions[$key."-delete"]['status']="delete";
352     }
354     $tmp['description'] = $this->DISKdescription;
355     $tmp['partitions']  = $this->partitions;
356     $tmp['status']      = $this->status;
357     $tmp['FAIdiskType'] = $this->FAIdiskType;
359     /* Assemble flags */
360     $tmp['FAIdiskOption'] = array("fstabkey:".$this->fstabkey, "disklabel:".$this->disklabel);
362     /* If hdd name has changed, tell partitionTable to rename it */
363     if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
364       $tmp['rename']['from']  = $this->old_cn;
365       $tmp['rename']['to']    = $this->DISKcn;
366     }
367     return($tmp);
368   }
371   /* Save data to object */
372   function save_object()
373   {
374     if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
375       plugin::save_object();
377       foreach($this->partitions as $key => $part){
378         foreach($this->UsedAttrs as $attrs){
380           if($this->acl_is_writeable($attrs)){
382             if(isset($_POST[$attrs."_".$key])){
383               $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
384             }else{
385               $this->partitions[$key][$attrs] = false;
386             }
387           }
388         }
390         /* Adapt ext3 to have -j option */
391         if ($this->partitions[$key]["FAIfsType"] == "ext3") {
392           $this->partitions[$key]["FAIfsOptions"]= preg_replace('/\s*-j\s*/', '', $this->partitions[$key]["FAIfsOptions"]);
393           $this->partitions[$key]["FAIfsOptions"]= "-j ".$this->partitions[$key]["FAIfsOptions"];
394         }
395       }
397     }
398   }
401   /* Check supplied data */
402   function check()
403   {
404     /* Call common method to give check the hook */
405     $message= plugin::check();
406   
407     /* Check for an empty disk name */
408     $d = trim($this->DISKcn);
409     if($d == "" ){
410       $message[] = msgPool::required(_("Name"));
411     }
412     if(preg_match("/[^a-z0-9_\-]/i",$d)){
413       $message[] = msgPool::invalid(_("Name"),$d,"/[a-z0-9_\-]/i");
414     }
415        
416     /* check every partition.
417      * if there is an invalid value defined, append an errorstr to message
418      */
420     /* Array that contain every partitionname mountpoint etc already assigned */
421     $alreadyUsed    = array();
422     foreach($this->UsedAttrs as $attrs){
423       $alreadyUsed[$attrs] = array();
424     }      
426     foreach($this->partitions as $key => $part){
427   
428       /* Skip all checks, if preserve is set */ 
429       if($part['FAIpartitionFlags'] == "preserve"){
430         $this->partitions[$key]['FAIfsType']        = "preserve";
431         $this->partitions[$key]['FAIpartitionSize'] = "preserve";
432         continue;
433       }
434  
435       if($part['FAImountPoint'] != "-" && (in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
436         $message[]=sprintf(_("Please enter a unique mount point for partition %s"),($key));
437       }
439       if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
440         if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
441           $message[]= msgPool::invalid(sprintf(_("partition %s mount point"),$key));
442         }
443       }
444       if($part['FAIfsType'] == "swap"){
445         if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
446           $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
447         }
448       }
449       if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
450         $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
451       }
453       $tmp = split("-",$part['FAIpartitionSize']);
454       switch (count($tmp)){
455         case 0:
456                 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
457                 break;
458         case 1:
459                 if (!tests::is_id($tmp[0]) &&(!empty($tmp[1]))){
460                   $message[]=  msgPool::invalid(sprintf(_("partition %s size"),$key));
461                 }
462                 break;
463                 
464         case 2:
465                 if( !tests::is_id($tmp[0]) && !tests::is_id($tmp[1]) && !empty($tmp[1]) ){
466                   $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
467                 }elseif(!empty($tmp[1]) && $tmp[0]>=$tmp[1]){
468                   $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
469                 }
470                 break;
472         default:
473                 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
474       }
476       foreach($this->UsedAttrs as $attrs){
477         $alreadyUsed[$attrs][$key] = $part[$attrs];
478       }      
479     }
481     $cnt = 0;
482     foreach($this->partitions as $key => $part){
483       if($part['FAIpartitionType'] == "primary"){
484         $cnt ++ ; 
485       }
486     }
487     if($cnt > 3){
488       $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
489     }
491     return ($message);
492   }
494   
496   /* Return plugin informations for acl handling */
497   static function plInfo()
498   {
499     return (array(
500           "plShortName" => _("Partition table entry"),
501           "plDescription" => _("FAI partition table entry"),
502           "plSelfModify"  => FALSE,
503           "plDepends"     => array(),
504           "plPriority"    => 27,
505           "plSection"     => array("administration"),
506           "plCategory"    => array("fai"),
507           "plProvidedAcls" => array(
508             "DISKcn"           => _("Name"),
509             "DISKdescription"  => _("Description"),
510             "DISKFAIdiskOption"  => _("Disk options"),
511             "FAIpartitionType"  => _("Partition type"),
512             "FAIpartitionNr"    => _("Partition no."),
513             "FAIfsType"         => _("File system type"),
514             "FAImountPoint"     => _("Mount point"),
515             "FAIpartitionSize"  => _("Partition size"),
516             "FAImountOptions"   => _("Mount options"),
517             "FAIfsOptions"      => _("File system options"),
518             "FAIpartitionFlags" => _("Partition flags"))
519           ));
520   }
522  
525 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
526 ?>