Code

878ffafe935e28c69c6028ff42b2b60cda0c4844
[gosa.git] / plugins / admin / fai / class_faiPartitionTableEntry.inc
1 <?php
3 class faiPartitionTableEntry extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage server basic objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account= TRUE;
12   var $attributes   = array("DISK_cn","DISK_description");
13   var $objectclasses= array();
15   var $DISK_cn              = "";
16   var $DISK_description     = "";
17   
18   var $partitions           = array();
19   var $UsedAttrs            = array();
20   var $AvailableDiskNames   = array();
21   var $is_edit              = false;
22   var $old_cn               = "";
23   var $status               = "new";
24   var $deletePartitions     = array();
26   function faiPartitionTableEntry ($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     /* Generate available disknames, to avoid duplicates in cn */
33     $this->AvailableDiskNames = array();
34     foreach(array("a","b","c","d","e","f","g","h") as $ts){
35       if(!in_array("hd".$ts,$usedDiskNames)){
36         $this->AvailableDiskNames["hd".$ts]= "hd".$ts;
37       }
38     }
39     
40     $this->AvailableDiskNames['disk1']= "disk1";
41     $this->AvailableDiskNames['disk2']= "disk2";
42     /* Default status is new */
43     $this->status = "new";    
44  
45     /* We want to edit an entry */
46     if($disk){
48       /* Set disk status */      
49       $this->status = $disk['status'];
51       /* Walk through partitions */
52       foreach($disk['partitions'] as $name => $values){
54         /* If a partition is already marked as delete, attach it to deletePartitions only. */
55         if($values['status'] == "delete"){
56           unset($disk['partitions'][$name]);
57           $this->deletePartitions[]=$values;
58         }else{
60           /* Set status, to know which partition must be deleted from ldap
61              new    : Neu partition entry // save
62              edited : Update partition entry in ldap
63              deleted: Remove partition from ldap
64            */
65     
66           /* If status is not new, set to edit mode. 
67            * New means that this partition currently wasn't saved to ldap.
68            */
69           if($disk['partitions'][$name]['status']!="new"){
70             $disk['partitions'][$name]['status']="edited";
71           }
72      
73           $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
74  
75           /* Assign empty attributes, if attribute is missing */
76           foreach($this->UsedAttrs as $attr){
77             if(!isset($values[$attr])){
78               $disk['partitions'][$name][$attr]="";  
79             }
80           }
81         }
82       }
84       /* Set default attributes */
85       $this->DISK_cn          = $disk['cn'];
86       $this->DISK_description = $disk['description'];
87       $this->partitions       = $disk['partitions'];
88       $this->is_edit          = true;
89       $this->old_cn           = $disk['cn'];
90     }
91   }
93   function execute()
94   {
95     /* Fill templating stuff */
96     $smarty     = get_smarty();
97     $s_action   = "";
98     $s_entry    = "";
99     $display    = "";
100     
101     /* Assign values 
102      * And Checkbox selection
103      */
104     foreach($this->attributes as $attrs){
105       $smarty->assign($attrs,$this->$attrs);
106       if($this->$attrs){
107         $smarty->assign($attrs."CHK"," ");
108       }else{
109         $smarty->assign($attrs."CHK"," disabled ");
110       }
111     }
113     /* Check all Posts if there is something usefull for us,
114      * For example : Delete is posted as Delete_1 
115      * The number specifies the index we want to delete
116      */
117     foreach($_POST as $name => $value){
118       if(preg_match("/Delete_.*/",$name)){
119         $tmp = split("_",$name);
120         $s_action = "remove";
121         $s_entry  = $tmp[1]; 
122       }
123     }
125     /* To remove a partition we unset the index posted.
126      * We must sort the index again, else we possibly got problems 
127      * with partitions order.
128      */
129     if($s_action == "remove"){
130       if($this->partitions[$s_entry]['status'] == "edited"){
131         $this->deletePartitions[]= $this->partitions[$s_entry];
132         unset($this->partitions[$s_entry]);
133       }else{
134         unset($this->partitions[$s_entry]);
135       }
136       $tmp= array();
137       foreach($this->partitions as $part){
138         $tmp[count($tmp)+1]=$part;
139       }
140       $this->partitions = $tmp;
141     }
144     /* To add a partitions we only append an empty 
145      *  array to the already defined partitions.
146      */
147     if(isset($_POST['AddPartition'])){
148       foreach($this->UsedAttrs as $attr){
149         $tmp[$attr]                = "";     
150       }
151       $tmp["old_cn"]               = "";     
152       $tmp['status']="new";
153       $this->partitions[count($this->partitions)+1]=$tmp;
154     }
156     /* Tell smarty which Disknames are available.
157      */
158     $smarty->assign("cns", $this->AvailableDiskNames); 
159     
160     /* $setup contains a table with the partitions. 
161      */
162     $smarty->assign("setup", $this->generateParts());
163     foreach($this->attributes as $attr){
164       $smarty->assign($attr,$this->$attr);
165     }
167     /* The user can't change a diskname 
168      *  if we are currently in edit mode for the specified disk 
169      */
170 //    if($this->is_edit){
171 //      $smarty->assign("DISK_cnACL"," disabled ");
172 //    }else{
173       $smarty->assign("DISK_cnACL","");
174 //    }
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","logical");
186     $FAIfsTypes       = array("ext2","ext3","xfs","swap","reiserfs","dos fat 16","win fat 32");
188     /* Display Header */
189     $str = "<table>";
190     $str .= "<tr>";
191     $str .= "<td>"._("Nr")."</td>";
192     $str .= "<td>"._("Name")."</td>";
193     $str .= "<td>"._("Description")."</td>";
194     $str .= "<td>"._("Type")."</td>";
195     $str .= "<td>"._("FS type")."</td>";
196     $str .= "<td>"._("Mount point")."</td>";
197     $str .= "<td>"._("Size in MB")."</td>";
198     $str .= "<td>"._("Mount options")."</td>";
199     $str .= "<td>"._("FS option")."</td>";
200     $str .= "<td>"._("Preserve")."</td>";
201     $str .= "<td></td>";
202     $str .= "</tr>";
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       if($part['status']!="delete"){
211         /* Generate Partition select box  
212          */
213         $PartitionType = "<select name='FAIpartitionType_".$key."'>";
214         foreach($PartitionTypes as $type){
215           if($part['FAIpartitionType'] == $type){
216             $PartitionType .="<option value='".$type."' selected >".$type."</option>";
217           }else{
218             $PartitionType .="<option value='".$type."'>".$type."</option>";
219           }
220         }        
221         $PartitionType.="</select>";   
224         /* Generate fsType select box  
225          */
226         $FAIfsType= "<select name='FAIfsType_".$key."'>";
227         foreach($FAIfsTypes as $type){
228           if($part['FAIfsType'] == $type){
229             $FAIfsType  .="<option value='".$type."' selected >".$type."</option>";
230           }else{
231             $FAIfsType .="<option value='".$type."'>".$type."</option>";
232           }
233         }        
234         $FAIfsType.="</select>";   
236         $str .= "<tr>";
237         $str .= "<td>".($key)."</td>";
238         $str .= "<td><input name='cn_".$key."'                value='".$part['cn']."'               maxlength='8' style='width:80px;'></td>";
239         $str .= "<td><input name='description_".$key."'       value='".$part['description']."'      maxlength='8' style='width:80px;'></td>";
240         $str .= "<td>".$PartitionType."</td>";
241         $str .= "<td>".$FAIfsType."</td>";
242         $str .= "<td><input name='FAImountPoint_".$key."'     value='".$part['FAImountPoint']."'    ></td>";
243         $str .= "<td><input name='FAIpartitionSize_".$key."'  value='".$part['FAIpartitionSize']."' style='width:200px;'></td>";
244         $str .= "<td><input name='FAImountOptions_".$key."'   value='".$part['FAImountOptions']."'  style='width:100px;'></td>";
245         $str .= "<td><input name='FAIfsOptions_".$key."'      value='".$part['FAIfsOptions']."'     style='width:100px;'></td>";
246         if($part['FAIpartitionFlags']!=false){
247           $str .= "<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked></td>";
248         }else{
249           $str .= "<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve'></td>";
250         }
251         $str .= "<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."'></td>";
252         $str .= "</tr>";    
253       }
254     }
255     $str.="</table>";
256     return($str);
258   }
260   /* Save data to object */
261   function save_object()
262   {
263     if(isset($_POST['TableEntryFrameSubmitted'])){
264       plugin::save_object();
266       /* Check base attributes */
267       foreach($this->attributes as $attrs){
268         if(isset($_POST[$attrs])){
269           $this->$attrs = $_POST[$attrs];
270         }
271       }
272      
273       foreach($this->partitions as $key => $part){
274         foreach($this->UsedAttrs as $attrs){
275           if(isset($_POST[$attrs."_".$key])){
276             $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
277           }else{
278             $this->partitions[$key][$attrs] = false;
279           }
280         }
281       }
282     }
283   }
286   /* Check supplied data */
287   function check()
288   {
289     $message= array();
290    
291     /* Check descriptions 
292      */ 
293     if(empty($this->DISK_description)){
294       $message[] = _("Please enter a description for your disk.");
295     }
297     $str = utf8_encode("üöä");
298     if(preg_match("/[^a-z0-9".$str."\.,;:\-_\? ]/i",$this->DISK_description)){
299       $message[]=_("Please enter a valid description.");
300     }
301     
302     /* check every partition.
303      * if there is an invalid value defined, append an errorstr to message
304      */
306     /* Array that contain every partitionname mountpoint etc already assigned */
307     $alreadyUsed    = array();
308     foreach($this->UsedAttrs as $attrs){
309       $alreadyUsed[$attrs] = array();
310     }      
312     foreach($this->partitions as $key => $part){
313     
314       if((empty($part['cn']))||(preg_match("/[^0-9a-z]/",$part['cn']))){
315         $message[]=sprintf(_("Please enter a valid name for partition %s."),($key));
316       }
318       if(in_array($part['cn'],$alreadyUsed['cn'])){
319         $message[]=sprintf(_("please enter a unique name for partition %s"),($key));
320       }
321     
322       if(in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint'])){
323         $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
324       }
326       if(empty($part['description'])){
327         $message[]=sprintf(_("Please enter a valid description for partition %s." ),($key));
328       }
330       if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
331         $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key));
332       }
334       if($part['FAIfsType'] == "swap"){
335         if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
336           $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
337         }
338       }
340       $tmp = split("-",$part['FAIpartitionSize']);
341       if(count($tmp)!=2){
342         $message[]=sprintf(_("Please enter a range for partition size, 'from-to', for partition %s."),($key));
343       }elseif((!is_numeric($tmp[0]))||(!is_numeric($tmp[1]))){
344         $message[]=sprintf(_("Please enter a valid range for partition %s."),($key)); 
345       }elseif($tmp[0]>=$tmp[1]){
346         $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
347       }elseif($this->checkSize($part['FAIpartitionSize'],$alreadyUsed['FAIpartitionSize'],$key)){
348         $message[]=$this->checkSize($part['FAIpartitionSize'],$alreadyUsed['FAIpartitionSize'],$key);
349       }
350       foreach($this->UsedAttrs as $attrs){
351         $alreadyUsed[$attrs][$key] = $part[$attrs];
352       }      
353     }
356     /* Only show 5 errors */
357     $cnt = 0;
358     if(count($message)>5){
359       foreach($message as $msg){
360         if($cnt < 5){
361           $tmp2[]=$msg;
362         }
363         $cnt ++;
364       }
365     $tmp2[] = "...";
366     $message = $tmp2;
367     }
369     return ($message);
370   }
371  
373   /* This function checks the selected partition sizes 
374    * If a size setting overlap with another, it returns an error msg
375    */ 
376   function checkSize($current,$others,$ckey){
377     $tmp = split("-",$current);
378     $from = $tmp[0];
379     $to   = $tmp[1];
380     foreach($others as $key=> $other){
381       $tmp  = split("-",$other);
382       $oth_from = $tmp[0];
383       $oth_to   = $tmp[1];
384       /* surrounds another entry */
385       if( (($from <= $oth_from)&&($to >= $oth_to))||    // inside
386           (($from >= $oth_from)&&($to <= $oth_to))||    // outside
387           (($to >= $oth_from)&&($to <= $oth_to))  ||    // overlap at right
388           (($from >= $oth_from)&&($from <= $oth_to)) ){ // overlap left
389         $message =sprintf( _("Size settings for partition %s, overlap with size settings for partition %s."),$ckey,$key);
390         return($message);
391       }
392     }
393     return(false);
394   }
395   
396   function save()
397   {
398     /* This function returns the new or edited disk with its partitions.
399      * If the disk was edited and not a new one, we reset the name of the disk
400      *  to keep the correct index.
401      */
403     /* Check partition rename
404     */
405     foreach($this->partitions as $key => $part){
406       if($part['cn'] != $part['old_cn']){
407         if($part['status'] == "edited"){
409           $tmp          = $part;
410           $tmp['status']= "delete";
411           $tmp['cn']    = $part['old_cn'];
413           $this->deletePartitions[] = $tmp;
414         }
415         $this->partitions[$key]['old_cn']=$part['cn'];
416         $this->partitions[$key]['status']="new";
417       }
418     }
420     $tmp = array();
421     $tmp['cn']          = $this->DISK_cn;
422  
423     /* Attach partitions */ 
424     foreach($this->partitions as $key=>$val) {
425       $this->partitions[$key]['FAIpartitionNr']=$key;
426     }
428     /* Attach deleted */ 
429     foreach($this->deletePartitions as $key=>$val) {
430       $this->partitions[$key."-delete"]=$val;
431       $this->partitions[$key."-delete"]['status']="delete";
432     } 
433  
434     $tmp['description'] = $this->DISK_description;
435     $tmp['partitions']  = $this->partitions;
436     $tmp['status']      = $this->status;
438     /* If hdd name has changed, tell partitionTable to rename it */
439     if(($this->is_edit)&&($this->old_cn != $this->DISK_cn)){
440       $tmp['rename']['from']  = $this->old_cn;    
441       $tmp['rename']['to']    = $this->DISK_cn;    
442     }
444     return($tmp);
445   }
449 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
450 ?>