Code

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