Code

Apply patch for #3716
[gosa.git] / trunk / gosa-plugins / fai / admin / fai / class_faiPartitionTable.inc
1 <?php
3 class faiPartitionTable extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = TRUE;
7   var $attributes       = array("cn","description");
8   var $objectclasses    = array("top","FAIclass","FAIpartitionTable");
10   var $subAttributes      = array("cn","description");
11   var $subPartAttributes  = array("cn","FAIpartitionNr","FAIpartitionSize","FAImountPoint","FAIfsType","FAIpartitionType","FAImountOptions","FAIfsOptions","FAIpartitionFlags","description");
13   var $sub64coded = array();
14   var $subBinary = array();
16   /* Specific attributes */
17   var $cn               = "";       // The class name for this object
18   var $description      = "";       // The description for this set of partitions
19   var $disks            = array();  // All defined Disks 
20   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
22   var $FAIstate         = "";
23   var $ui;
25   var $view_logged      = FALSE;
27   function faiPartitionTable (&$config, $dn= NULL)
28   {
29     /* Load Attributes */
30     plugin::plugin ($config, $dn);
32     /* If "dn==new" we try to create a new entry
33      * Else we must read all objects from ldap which belong to this entry.
34      */
35     $this->ui = get_userinfo();
36     if($dn != "new"){
37       $this->dn =$dn;
39       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
40        */
41       $res = FAI::get_leaf_objects($this->dn, $this->cn, "FAIpartitionDisk", get_ou('faiPartitionRDN'));
42       foreach($res as $obj){
43         $objects = array();
44         $objects['description']  = "";
45         $objects['status']      = "edited";
46         $objects['dn']          = $obj['dn'];
47         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
48         $this->disks[$objects['cn']] = $objects;
49         $this->disks[$objects['cn']]['partitions'] = array();
50       }
52       /* read all partitions for each disk 
53        */
54       foreach($this->disks as $name => $disk){
56         $res = FAI::get_leaf_objects($disk['dn'], $name, "FAIpartitionEntry", "cn=".$this->cn.",".get_ou('faiPartitionRDN'));
57         foreach($res as $obj){
58           $objects = array();
59           $objects['status']      = "edited";
60           $objects['dn']          = $obj['dn'];
61           $objects                = $this->get_object_attributes($objects,$this->subPartAttributes);
62           unset($objects['dn']);;
63           $this->disks[$name]['partitions'][$objects['FAIpartitionNr']] = $objects;
64         }
65       }
66     }
68     $this->is_new = FALSE;
69     if($this->dn == "new"){
70       $this->is_new =TRUE;
71     }
72     ksort($this->disks);
73   }
76   function acl_base_for_current_object($dn)
77   {
78     if($dn == "new" || $dn == ""){
79       if($this->dn == "new"){
80         $dn= $this->parent->parent->acl_base;
81       }else{
82         $dn = $this->dn;
83       }
84     }
85     return($dn);
86   }
89   function execute()
90   {
91     /* Call parent execute */
92     plugin::execute();
94     if($this->is_account && !$this->view_logged){
95       $this->view_logged = TRUE;
96       new log("view","fai/".get_class($this),$this->dn);
97     }
99     /* Fill templating stuff */
100     $smarty= get_smarty();
101     $display= "";
102  
103     /* Add Disk to this Partitionset
104      * This code adds a new HDD to the disks 
105      * A new Dialog will be opened 
106      */
107     if(isset($_POST['AddDisk']) && !preg_match("/freeze/i",$this->FAIstate)){
108       $usedDiskNames =array();
109       foreach($this->disks as $key=>$disk){
110         $usedDiskNames[]= $key;
111       }
112       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames); 
113       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
114       $this->dialog->set_acl_category("fai");
115       $this->dialog->FAIstate = $this->FAIstate;
116       $this->is_dialog = true;
117     }
119     /* Edit disk.
120      * Open dialog which allows us to edit the selected entry 
121      */    
123     if($this->dn != "new"){
124       set_object_info($this->dn);
125     }
127     if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
128       $usedDiskNames =array();
129       $Udisk = $_POST['disks'][0];
130       if(isset($this->disks[$Udisk])){
132         foreach($this->disks  as $key=>$disk){
133           if($key != $Udisk){
134             $usedDiskNames[]= $key;
135           }
136         }
138         /* Set object info string, which will be displayed in plugin info line */ 
139         if(isset($this->disks[$Udisk]['dn'])){
140           set_object_info($this->disks[$Udisk]['dn']);
141           $dn = $this->disks[$Udisk]['dn'];
142         }else{
143           set_object_info("");
144           $dn = "new";
145         }
147         $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$Udisk]); 
148         $this->dialog->set_acl_base($this->acl_base_for_current_object($dn));
149         $this->dialog->set_acl_category("fai");
150         $this->dialog->FAIstate = $this->FAIstate;
152         $this->is_dialog = true;
153       }
154     }
156     /* Edit aborted, close dialog, without saving anything
157      */
158     if(isset($_POST['CancelDisk'])){
159       unset($this->dialog);
160       $this->dialog = FALSE;
161       $this->is_dialog=false;
162     }
164     /* Dialog saved
165      * Save given data from Dialog, if no error is occurred
166      */
167     if(isset($_POST['SaveDisk'])){
169       if (!preg_match("/freeze/i", $this->FAIstate)){
170         $this->dialog->save_object();
171         if(count($this->dialog->check())){
172           foreach($this->dialog->check() as $msg){
173             msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
174           }
175         }else{
176           $disk = $this->dialog->save();
177           if(isset($disk['rename'])){
178             if($this->disks[$disk['rename']['from']]['status']=="edited"){
179               $this->disks[$disk['rename']['from']]['status']="delete";
180             }else{
181               unset($this->disks[$disk['rename']['from']]);
182             }
184             foreach($disk['partitions'] as $key => $val){
185               if($disk['partitions'][$key]['status']!="delete"){
186                 $disk['partitions'][$key]['status']= "new";
187               }
188             }
190             $disk['status']="new";
191             $disk['cn']= $disk['rename']['to'];
192           }
194           $this->disks[$disk['cn']]=$disk; 
195           unset($this->dialog);
196           $this->dialog = FALSE;
197           $this->is_dialog=false;
198           ksort($this->disks);
199         }
200       }else{
201         $this->dialog = FALSE;
202         $this->is_dialog=false;
203       }
204     }
206     /* Delete selected disk drive from list
207      * Assign delete status for all its partitions      
208      */
209     if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
210       if (!preg_match("/freeze/i", $this->FAIstate)){
211         foreach($_POST['disks'] as $disk) {
213           if(isset($this->disks[$disk])){
214             if($this->disks[$disk]['status']=="edited"){
215               $this->disks[$disk."-delete"]=$this->disks[$disk];
216               unset($this->disks[$disk]);
217               $disk = $disk."-delete";        
218               $this->disks[$disk]['status']="delete";
219               foreach($this->disks[$disk]['partitions'] as $name => $value ){
220                 if($value['status']=="edited"){
221                   $this->disks[$disk]['partitions'][$name]['status']="delete"; 
222                 }else{
223                   unset($this->disks[$disk]['partitions'][$name]);
224                 }
225               }
226             }else{
227               unset($this->disks[$disk]);
228             }
229           }
230         }
231       }
232     }
234     /* Display dialog if one is defined
235      */
236     if(is_object($this->dialog)){
237       $this->dialog->save_object();
238       return($this->dialog->execute());
239     }
241     /* Assign all attributes to smarty engine
242      */
243     foreach($this->attributes as $attrs){
244       $smarty->assign($attrs,$this->$attrs);
245       if($this->$attrs){
246         $smarty->assign($attrs."CHK"," ");
247       }else{
248         $smarty->assign($attrs."CHK"," disabled ");
249       }
250     }
251    
252     $dn = $this->acl_base_for_current_object($this->dn);
253     $smarty->assign("sub_object_is_addable",
254         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
255         !preg_match("/freeze/",$this->FAIstate));
256     $smarty->assign("sub_object_is_removeable",
257         preg_match("/d/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
258         !preg_match("/freeze/",$this->FAIstate));
260     $tmp = $this->plInfo();
261     foreach($tmp['plProvidedAcls'] as $name => $translated){
262       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/i",$this->FAIstate)));
263     }
264     $disks = $this->getDisks();
265     $smarty->assign("disks"   ,$disks);
266     $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
267     return($display);
268   }
270   function getDisks(){
271     /* Return all available disks for this partition table
272      * Return in listBox friendly array
273      */
274     $a_return = array();
275     foreach($this->disks as $key => $disk){
277       $dn = "new";
278       if(isset($obj['dn'])){
279         $dn = $obj['dn'];
280       }
281       $dn = $this->acl_base_for_current_object($dn);
282       $acl = $this->ui->get_permissions($dn,"fai/faiPartitionTableEntry");
283       if(preg_match("/(r|w)/",$acl)) {
285         if($disk['status'] != "delete"){
286           $cnt=0;
287           foreach($disk['partitions'] as $val){
288             if($val['status']!="delete"){
289               $cnt ++;
290             }
291           }
292           if(!empty($disk['description'])){
293             if($cnt == 1){
294               $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
295             }else{
296               $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
297             }
298           }else{
299             if($cnt == 1){
300               $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
301             }else{
302               $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
303             }
304           }
305         }
306       }
307     }
308     return($a_return);
309   }
312   /* Delete me, and all my subtrees
313    */
314   function remove_from_parent()
315   {
316     $ldap = $this->config->get_ldap_link();
317     $ldap->cd ($this->dn);
319     $release = $this->parent->parent->fai_release;
320     $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
322     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
323     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);   
324     foreach($this->disks as $disk){
325       $disk_dn = "cn=".$disk['cn'].",".$this->dn;
326       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i",$release, $disk_dn);
327       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
328       foreach($disk['partitions'] as $key => $partition){    
329         $partition_dn= "FAIpartitionNr=".$partition['FAIpartitionNr'].",".$disk_dn;      
330         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $partition_dn);
331         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
332       }
333     }
334   }
337   /* Save data to object 
338    */
339   function save_object()
340   {
341     if (preg_match("/freeze/", $this->FAIstate)) return;
342     plugin::save_object();
343     foreach($this->attributes as $attrs){
344       if(isset($_POST[$attrs])){
345         $this->$attrs = $_POST[$attrs];
346       }
347     }
348   }
351   /* Check supplied data */
352   function check()
353   {
354     /* Call common method to give check the hook */
355     $message= plugin::check();
357     /* Ensure that we do not overwrite an allready existing entry 
358      */
359     if($this->is_new){
360       $release = $this->parent->parent->fai_release;
361       $new_dn= 'cn='.$this->cn.",".get_ou('faiPartitionRDN').get_ou('faiBaseRDN').$release;
362       $res = faiManagement::check_class_name("FAIpartitionTable",$this->cn,$new_dn);
363       if(isset($res[$this->cn])){
364         $message[] = msgPool::duplicated(_("Name"));
365       }
366     }
367     return ($message);
368   }
371   /* Save to LDAP */
372   function save()
373   {
375     plugin::save();
376     /* Save current settings.
377      * 1 : We must save the partition table, with its description and cn 
378      * 2 : Append Disk with cn and  description.
379      * 3 : Save partitions for each disk
380      */  
382     $ldap = $this->config->get_ldap_link();
384     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
386     if($this->initially_was_account){
387       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
388     }else{
389       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
390     }
391  
392     /* Sort entries, because we must delete entries with status="delete" first */
393     $order = array();
394     foreach($this->disks as $key => $disk){
395       if($disk['status'] == "delete"){
396         $order[$key] = $disk;
397       }
398     }
399     foreach($this->disks as $key => $disk){
400       if($disk['status'] != "delete"){
401         $order[$key] = $disk;
402       }
403     }
405     /* Append all disks to ldap */
406     foreach($order as $cn=>$disk){
407       $disk_dn                    = "cn=".$disk['cn'].",".$this->dn;
408       $disk_attrs['cn']           =  $disk['cn'];
409       $disk_attrs['description']  =  $disk['description']; 
411       if(empty($disk_attrs['description']) && $disk['status'] == "edited"){
412         $disk_attrs['description'] = array();
413       }
415       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
417       if($disk['status']=="new"){
418         $ldap->cat($disk_dn,array("objectClass"));
419         if($ldap->count()){
420           $disk['status']="edited";
421         }
422       }
424       /* Tag object */
425       $ui= get_userinfo();
426       $this->tag_attrs($disk_attrs, $disk_dn, $ui->gosaUnitTag);
428       if($disk['status'] == "delete"){
429         FAI::prepare_to_save_FAI_object($disk_dn,array(),true);
430         $this->handle_post_events("remove");
431       }elseif($disk['status'] == "edited"){
432         FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
433         $this->handle_post_events("modify");
434       }elseif($disk['status']=="new"){
435         FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
436         $this->handle_post_events("add");
437       }
439       if($disk['status']!="delete")
440       /* Add all partitions */
441       foreach($disk['partitions'] as $key => $partition){
442         $partition_attrs = array();
444         foreach($partition as $key => $value){
445           if(!empty($value)){
446             $partition_attrs[$key]=$value;        
447           }else{
448             unset($partition_attrs[$key]);        
449           }
450         }
452         $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;      
453         $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
454         $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
455         
456         unset($partition_attrs['status']);
457         unset($partition_attrs['old_cn']);
459         if($partition['status']=="new"){
460           $ldap->cat($partition_dn,array("objectClass"));
461           if($ldap->count()){
462             $partition['status']="edited";
463           }
464         }
466         if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
467           $partition_attrs['FAImountPoint']="swap";
468         }
470         /* Tag object */
471        $ui= get_userinfo();
472         $this->tag_attrs($partition_attrs, $partition_dn, $ui->gosaUnitTag);
474         if($partition['status'] == "delete"){
475           FAI::prepare_to_save_FAI_object($partition_dn,array(),true);
476           $this->handle_post_events("remove");
477         }elseif($partition['status'] == "edited"){
478           FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
479           $this->handle_post_events("modify");
480         }elseif($partition['status']=="new"){
481           FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
482           $this->handle_post_events("add");
483         }
484       }
485     }
486     $this->handle_post_events("add");
487   }
490   function PrepareForCopyPaste($source)
491   {
492     plugin::PrepareForCopyPaste($source);
494     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
495      */
496     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))");
497     foreach($res as $obj){
499       /* Skip not relevant objects */
500       if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
502       $objects = array();
503       $objects['description']  = "";
504       $objects['status']      = "edited";
505       $objects['dn']          = $obj['dn'];
506       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
507       $this->disks[$objects['cn']] = $objects;
508       $this->disks[$objects['cn']]['partitions'] = array();
509     }
511     /* read all partitions for each disk
512      */
513     foreach($this->disks as $name => $disk){
514       $res = FAI::get_all_objects_for_given_base($disk['dn'],"(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))");
515       foreach($res as $obj){
517         /* Skip not relevant objects */
518         if(!preg_match("/".preg_quote($disk['dn'], '/')."$/i",$obj['dn'])) continue;
520         $objects = array();
521         $objects['status']      = "edited";
522         $objects['dn']          = $obj['dn'];
523         $objects                = $this->get_object_attributes($objects,$this->subPartAttributes);
524         unset($objects['dn']);;
525         $this->disks[$name]['partitions'][$objects['FAIpartitionNr']] = $objects;
526       }
527     }
528     ksort($this->disks);
529   }
532   /* Return plugin informations for acl handling */ 
533   static function plInfo()
534   {
535     return (array( 
536           "plShortName" => _("Partition table"),
537           "plDescription" => _("FAI partition table"),
538           "plSelfModify"  => FALSE,
539           "plDepends"     => array(),
540           "plPriority"    => 26,
541           "plSection"     => array("administration"),
542           "plCategory"    => array("fai"),
543           "plProvidedAcls" => array(
544             "cn"                => _("Name")."&nbsp;("._("Read only").")",
545             "description"       => _("Description"))
546           ));
547   }
550   /*! \brief  Used for copy & paste.
551     Returns a HTML input mask, which allows to change the cn of this entry.
552     @param  Array   Array containing current status && a HTML template.
553    */
554   function getCopyDialog()
555   {
556     $vars = array("cn");
557     $smarty = get_smarty();
558     $smarty->assign("cn", htmlentities($this->cn));
559     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
560     $ret = array();
561     $ret['string'] = $str;
562     $ret['status'] = "";
563     return($ret);
564   }
567   /*! \brief  Used for copy & paste.
568     Some entries must be renamed to avaoid duplicate entries.
569    */
570   function saveCopyDialog()
571   {
572     if(isset($_POST['cn'])){
573       $this->cn = get_post('cn');
574     }
575   }
577   /* Reload some attributes */
578   function get_object_attributes($object,$attributes)
579   {
580     $ldap = $this->config->get_ldap_link();
581     $ldap->cd($this->config->current['BASE']);
582     $ldap->cat($object['dn'],$attributes);
583     $tmp  = $ldap->fetch();
585     foreach($attributes as $attrs){
586       if(isset($tmp[$attrs][0])){
587         $var = $tmp[$attrs][0];
589         /* Check if we must decode some attributes */
590         if(in_array_ics($attrs,$this->sub64coded)){
591           $var = base64_decode($var);
592         }
594         /*  check if this is a binary entry */
595         if(in_array_ics($attrs,$this->subBinary)){
596           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
597         }
599         /* Fix slashes */
600         $var = addslashes($var);
601         $object[$attrs] = $var;
602       }
603     }
604     return($object);
605   }
609 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
610 ?>