Code

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