Code

We can now save volume groups.
[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("cn","description","FAIpartitionType",
16       "FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
17       "FAIfsTuneOptions", "FAIfsCreateOptions", "FAImountOptions",
18       "FAIfsOptions","FAIpartitionFlags","FAIdiskOption");
20   var $is_edit              = false;
21   var $old_cn               = "";
22   var $status               = "new";
23   var $deletePartitions     = array();
24   var $fstabkey             = "device";
25   var $disklabel            = "msdos";
26   var $FAIstate             = "";
27   var $FAIdiskType          = "disk";
29   function faiDiskEntry (&$config, $dn= NULL,$parent,$disk,$type)
30   {
31     plugin::plugin ($config, $dn);
33     // Set default attributes 
34     $this->parent = $parent;
35     $this->FAIdiskType = $type;
36     $this->status = "new";    
38     // If disk is not empty, then we are going to edit 
39     //  an existing disk, load disk info now. 
40     if($disk){
42       // Get disk status
43       $this->status = $disk['status'];
45       // Load default attributes 
46       $this->DISKcn          = $disk['cn'];
47       $this->DISKdescription = $disk['description'];
48       $this->is_edit         = true;
49       $this->old_cn          = $disk['cn'];
51       // Load partition info 
52       foreach($disk['partitions'] as $name => $values){
54         // Do not load removed partitions 
55         if($values['status'] == "delete"){
56           unset($disk['partitions'][$name]);
57           $this->deletePartitions[]=$values;
58         }else{
60           /* If status is not new, set to edit mode. 
61            * New means that this partition currently wasn't saved to ldap.
62            */
63           if($disk['partitions'][$name]['status']!="new"){
64             $disk['partitions'][$name]['status']="edited";
65           }
66     
67           // Load partition attributes  
68           $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
69           $disk['partitions'][$name]['FAIdiskType']= $this->FAIdiskType;
70           foreach($this->UsedAttrs as $attr){
71             if(!isset($values[$attr])){
72               $disk['partitions'][$name][$attr]="";  
73             }
74           }
75           if (preg_match('/^_/', $disk['partitions'][$name]['FAIfsType'])){
76             $disk['partitions'][$name]['FAIfsType']= 
77               preg_replace('/^_/', '', $disk['partitions'][$name]['FAIfsType']);
78           }
79         }
80       }
82       $this->partitions      = $disk['partitions'];
84       /* Load FAIdiskOptions.
85        * Some options are disk related and some are used for partitions.
86        *  - fstabkey    -> disk
87        *  - disklabel   -> disk
88        *  - bootable    -> partition
89        *  - preserve    -> partition
90        *  - resize      -> partition
91        */
92       if (isset($disk['FAIdiskOption'])){
93         foreach($disk['FAIdiskOption'] as $option) {
95           // Get fstab key  
96           if (preg_match("/fstabkey:(device|label|uuid)/", $option)){
97             $this->fstabkey= preg_replace("/^.*fstabkey:(device|label|uuid).*$/", "$1", $option);
98             continue;
99           }
101           // Get disk label
102           if (preg_match("/disklabel:(msdos|gpt)/", $option)){
103             $this->disklabel= preg_replace("/^.*disklabel:(msdos|gpt).*$/", "$1", $option);
104             continue;
105           }
107           // Load bootable flag for partitions 
108           if (preg_match("/^bootable:/", $option)){
109             $bootable = split(",", trim(preg_replace("/^bootable:/","",$option),","));
110             foreach($bootable as $bootflag){
111               if(isset($this->partitions[$bootflag])){
112                 $this->partitions[$bootflag]['bootable'] = TRUE;  
113               }
114             }
115             continue;
116           }
118           // Load resize flag for partitions 
119           if (preg_match("/^resize:/", $option)){
120             $resize = split(",", trim(preg_replace("/^resize:/","",$option),","));
121             foreach($resize as $id){
122               if(isset($this->partitions[$bootflag])){
123                 $this->partitions[$id]['resize'] = TRUE;  
124               }
125             }
126             continue;
127           }
129           // Load preserve_always flag for partitions 
130           if (preg_match("/^preserve_always:/", $option)){
131             $preserve = split(",", trim(preg_replace("/^preserve_always:/","",$option),","));
132             foreach($preserve as $presflag){
133               if(isset($this->partitions[$bootflag])){
134                 $this->partitions[$presflag]['preserve'] = TRUE;  
135                 $this->partitions[$presflag]['preserveType'] = 'always';  
136               }
137             }
138             continue;
139           }
141           // Load preserve_reinstall flag for partitions 
142           if (preg_match("/^preserve_reinstall:/", $option)){
143             $preserve = split(",", trim(preg_replace("/^preserve_reinstall:/","",$option),","));
144             foreach($preserve as $presflag){
145               if(isset($this->partitions[$bootflag])){
146                 $this->partitions[$presflag]['preserve'] = TRUE;  
147                 $this->partitions[$presflag]['preserveType'] = 'reinstall'; 
148               } 
149             }
150             continue;
151           }
152         }
153       } else {
154         $this->fstabkey= "device";
155       }
156     }
157   }
160   function execute()
161   {
162     /* Call parent execute */
163     plugin::execute();
165     // Fill templating stuff
166     $smarty     = get_smarty();
167     $s_action   = "";
168     $s_entry    = "";
169     $display    = "";
171     /* Check all Posts if there is something usefull for us,
172      * For example : Delete is posted as Delete_1 
173      * The number specifies the index we want to delete
174      */
175     foreach($_POST as $name => $value){
176       if((preg_match("/Delete_.*/",$name)) && 
177           $this->acl_is_removeable() && 
178           !preg_match("/freeze/i",$this->FAIstate)){
179         $tmp = split("_",$name);
180         $s_action = "remove";
181         $s_entry  = $tmp[1]; 
182       }
183     }
185     /* To remove a partition we unset the index posted.
186      * We must sort the index again, else we possibly get problems 
187      * with the partition order.
188      */
189     if($s_action == "remove" && $this->acl_is_removeable() && 
190         !preg_match("/freeze/i",$this->FAIstate)){
191       if($this->partitions[$s_entry]['status'] == "edited"){
192         $this->deletePartitions[$s_entry]= $this->partitions[$s_entry];
193         $this->deletePartitions[$s_entry]['FAIpartitionNr']=$s_entry;
194         unset($this->partitions[$s_entry]);
195       }else{
196         unset($this->partitions[$s_entry]);
197       }
198       $tmp= array();
199       foreach($this->partitions as $part){
200         $tmp[count($tmp)+1]=$part;
201       }
202       $this->partitions = $tmp;
203     }
206     /* Edit existing partitions here.
207      */    
208     foreach($_POST as $name => $value){
209       if(preg_match("/^EditPartition_/",$name)){
210         $id = preg_replace("/^EditPartition_/","",$name);
211         $id = preg_replace("/_.*$/","",$id);
212         if(isset($this->partitions[$id])){
213           $this->dialog = new faiPartition($this->config,$this->partitions[$id], $this,$this->FAIdiskType);
214           break;
215         }
216       } 
217     }
219     /* Create a nwe partition for this disk.
220      */
221     if(isset($_POST['AddPartition']) && !preg_match("/freeze/i",$this->FAIstate)){
222       $this->dialog = new faiPartition($this->config, array(), $this,$this->FAIdiskType);
223     }
225     /* Handle partition dialogs.
226      */
227     if($this->dialog instanceOf plugin && isset($_POST['PartitionCancel'])){
228       $this->dialog = null;
229     }
230     if($this->dialog instanceOf plugin && isset($_POST['PartitionSave'])){
231       $this->dialog->save_object();
232       $attrs = $this->dialog->save();
233       if(isset($attrs['FAIpartitionNr']) && $attrs['FAIpartitionNr'] != ""){
234         $this->partitions[$attrs['FAIpartitionNr']] = $attrs;
235       }else{
236         $attrs['FAIpartitionNr'] = count($this->partitions) + 1;
237         $attrs['cn'] = &$attrs['FAIpartitionNr'];
238         $this->partitions[$attrs['FAIpartitionNr']] = $attrs;
239       }
240       $this->dialog = null;
241     }
242     if($this->dialog instanceOf plugin){
243       $this->dialog->save_object();
244       return($this->dialog->execute());
245     }
248     // Assign checkbox related values.
249     foreach($this->attributes as $attrs){
250       $smarty->assign($attrs,$this->$attrs);
251       if($this->$attrs){
252         $smarty->assign($attrs."CHK"," ");
253       }else{
254         $smarty->assign($attrs."CHK"," disabled ");
255       }
256     }
258     // Assign disk info to smarty.
259     $smarty->assign("setup", $this->generateParts());
260     $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
261     $smarty->assign("freeze",preg_match("/freeze/i",$this->FAIstate));
262     $smarty->assign("fstabkeys", array("device" => _("Device"), "label" => _("Label"), "uuid" => _("UUID")));
263     $smarty->assign("disklabels", array("msdos" => "MSDOS", "gpt" => "GPT"));
264     $smarty->assign("fstabkey", $this->fstabkey);
265     $smarty->assign("disklabel", $this->disklabel);
266     foreach($this->attributes as $attr){
267       $smarty->assign($attr,$this->$attr);
268     }
270     // Assign partitions
271     $tmp = $this->plInfo();
272     $sacl = "";
273     foreach($tmp['plProvidedAcls'] as $name => $translated){
274       $acl = $this->getacl($name, preg_match("/freeze/i",$this->FAIstate));
275       $smarty->assign($name."ACL",$acl);
276     }
277    
278     $display.= $smarty->fetch(get_template_path('faiDiskEntry.tpl', TRUE));
279     return($display);
280   }
282   function generateParts()
283   {
285     $divlist = new divSelectBox("DiskEntries"); 
286     foreach($this->partitions as $key => $part){
287       $number =array(
288           "string" => $part['FAIpartitionNr'],
289           "attach" => "style='width:20px;'");
290       $size   =array(
291           "string" => $part['FAIpartitionSize'],
292           "attach" => "style='width:100px;'");
293       $fstype =array(
294           "string" => $part['FAIfsType'],
295           "attach" => "style='width:60px;'");
296       $type   =array(
297           "string" => $part['FAIpartitionType'],
298           "attach" => "style='width:80px;'");
299       $opt    =array(
300           "string" => $part['FAImountOptions'],
301           "attach" => "style='width:80px;'");
302       $fsopt  =array(
303           "string" => $part['FAIfsOptions'],
304           "attach" => "style='width:80px;'");
305       $flags  =array(
306           "string" => $part['FAIpartitionFlags'],
307           "attach" => "style='width:80px;'");
308       $mntp   =array("string" => $part['FAImountPoint']);
310    
311       $action =array(
312           "string" => "<input type='image' src='images/lists/edit.png' name='EditPartition_".$key."'>",
313           "attach" => "style='width:40px;'");
315  
316       $fields = array($number,$type,$mntp,$size,$fstype, $opt,$fsopt,$flags,$action);
317       $divlist->AddEntry($fields);
318     }
319     return($divlist->DrawList());    
320   }
322   function save()
323   {
324     $tmp = array();
325     $tmp['cn']          = $this->DISKcn;
327     /* Attach partitions */
328     foreach($this->partitions as $key=>$val) {
329       $this->partitions[$key]['FAIpartitionNr']=$key;
330     }
332     /* Attach deleted */
333     foreach($this->deletePartitions as $key=>$val) {
334       $this->partitions[$key."-delete"]=$val;
335       $this->partitions[$key."-delete"]['status']="delete";
336     }
338     $tmp['description'] = $this->DISKdescription;
339     $tmp['partitions']  = $this->partitions;
340     $tmp['status']      = $this->status;
341     $tmp['FAIdiskType'] = $this->FAIdiskType;
343     /* Assemble flags */
344     $tmp['FAIdiskOption'] = array("fstabkey:".$this->fstabkey, "disklabel:".$this->disklabel);
346     /* If hdd name has changed, tell partitionTable to rename it */
347     if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
348       $tmp['rename']['from']  = $this->old_cn;
349       $tmp['rename']['to']    = $this->DISKcn;
350     }
352     // Build up disk options 
353     $bootable = "";
354     $resize = "";
355     $preserve_always = "";
356     $preserve_reinstall = "";
358     foreach($tmp['partitions'] as $id => $part){
359       if(isset($part['bootable']) && $part['bootable']){
360         $bootable .= $id.",";
361       }
362       if(isset($part['resize']) && $part['resize']){
363         $resize .= $id.",";
364       }
365       if(isset($part['preserve']) && $part['preserve']){
366         if($part['preserveType'] == "always"){
367           $preserve_always .= $id.",";
368         }else{
369           $preserve_reinstall .= $id.",";
370         }
371       }
372       $tmp['partitions'][$id]['status'] = $part['status'];
374       // Unset non valid attributes 
375       foreach(array("bootable","encrypted","preserve","preserveType","resize","FAIdiskType") as $attr){
376         if(isset($tmp['partitions'][$id][$attr])){
377           unset($tmp['partitions'][$id][$attr]);
378         }
379       }
380     }    
382     if(!empty($bootable)){
383       $tmp['FAIdiskOption'][] = "bootable:".trim($bootable,",");
384     }
385     if(!empty($resize)){
386       $tmp['FAIdiskOption'][] = "resize:".trim($resize,",");
387     }
388     if(!empty($preserve_always)){
389       $tmp['FAIdiskOption'][] = "preserve_always:".trim($preserve_always,",");
390     }
391     if(!empty($preserve_reinstall)){
392       $tmp['FAIdiskOption'][] = "preserve_reinstall:".trim($preserve_reinstall,",");
393     }
395     $tmp['status'] = $this->status;
396     return($tmp);
397   }
400   /* Save data to object */
401   function save_object()
402   {
403     if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
404       plugin::save_object();
406       // Save posted disk label and fstab key
407       if (isset($_POST['disklabel']) && preg_match("/^(msdos|gpt)$/", $_POST['disklabel'])){
408         $this->disklabel= $_POST['disklabel'];
409       }
410       if (isset($_POST['fstabkey']) && preg_match("/^(device|label|uuid)$/", $_POST['fstabkey'])){
411         $this->fstabkey= $_POST['fstabkey'];
412       }
413     }
414   }
417   /* Check supplied data */
418   function check()
419   {
420     /* Call common method to give check the hook */
421     $message= plugin::check();
422   
423     /* Check for an empty disk name */
424     $d = trim($this->DISKcn);
425     if($d == "" ){
426       $message[] = msgPool::required(_("Name"));
427     }
428     if(preg_match("/[^a-z0-9_\-]/i",$d)){
429       $message[] = msgPool::invalid(_("Name"),$d,"/[a-z0-9_\-]/i");
430     }
431        
432     /* check every partition.
433      * if there is an invalid value defined, append an errorstr to message
434      */
436     /* Array that contain every partitionname mountpoint etc already assigned */
437     $alreadyUsed    = array();
438     foreach($this->UsedAttrs as $attrs){
439       $alreadyUsed[$attrs] = array();
440     }      
442 //   foreach($this->partitions as $key => $part){
443 // 
444 //     /* Skip all checks, if preserve is set */ 
445 //     if($part['FAIpartitionFlags'] == "preserve"){
446 //       $this->partitions[$key]['FAIfsType']        = "preserve";
447 //       $this->partitions[$key]['FAIpartitionSize'] = "preserve";
448 //       continue;
449 //     }
450 //
451 //     if($part['FAImountPoint'] != "-" && (in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
452 //       $message[]=sprintf(_("Please enter a unique mount point for partition %s"),($key));
453 //     }
454 //
455 //     if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
456 //       if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
457 //         $message[]= msgPool::invalid(sprintf(_("partition %s mount point"),$key));
458 //       }
459 //     }
460 //     if($part['FAIfsType'] == "swap"){
461 //       if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
462 //         $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
463 //       }
464 //     }
465 //     if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
466 //       $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
467 //     }
468 //
469 //     $tmp = split("-",$part['FAIpartitionSize']);
470 //     switch (count($tmp)){
471 //       case 0:
472 //               $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
473 //               break;
474 //       case 1:
475 //               if (!tests::is_id($tmp[0]) &&(!empty($tmp[1]))){
476 //                 $message[]=  msgPool::invalid(sprintf(_("partition %s size"),$key));
477 //               }
478 //               break;
479 //               
480 //       case 2:
481 //               if( !tests::is_id($tmp[0]) && !tests::is_id($tmp[1]) && !empty($tmp[1]) ){
482 //                 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
483 //               }elseif(!empty($tmp[1]) && $tmp[0]>=$tmp[1]){
484 //                 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
485 //               }
486 //               break;
487 //
488 //       default:
489 //               $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
490 //     }
491 //
492 //     foreach($this->UsedAttrs as $attrs){
493 //       if(isset($part[$attrs])){
494 //         $alreadyUsed[$attrs][$key] = $part[$attrs];
495 //       }
496 //     }      
497 //   }
499     $cnt = 0;
500     foreach($this->partitions as $key => $part){
501       if($part['FAIpartitionType'] == "primary"){
502         $cnt ++ ; 
503       }
504     }
505     if($cnt > 3){
506       $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
507     }
509     return ($message);
510   }
512   
514   /* Return plugin informations for acl handling */
515   static function plInfo()
516   {
517     return (array(
518           "plShortName" => _("Partition table entry"),
519           "plDescription" => _("FAI partition table entry"),
520           "plSelfModify"  => FALSE,
521           "plDepends"     => array(),
522           "plPriority"    => 27,
523           "plSection"     => array("administration"),
524           "plCategory"    => array("fai"),
525           "plProvidedAcls" => array(
526             "DISKcn"           => _("Name"),
527             "DISKdescription"  => _("Description"),
528             "DISKFAIdiskOption"  => _("Disk options"),
529             "FAIpartitionType"  => _("Partition type"),
530             "FAIpartitionNr"    => _("Partition no."),
531             "FAIfsType"         => _("File system type"),
532             "FAImountPoint"     => _("Mount point"),
533             "FAIpartitionSize"  => _("Partition size"),
534             "FAImountOptions"   => _("Mount options"),
535             "FAIfsOptions"      => _("File system options"),
536             "FAIpartitionFlags" => _("Partition flags"))
537           ));
538   }
540  
543 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
544 ?>