Code

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