Code

Backport from trunk
[gosa.git] / 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", "FAIpartitionMethod");
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","FAIfsCreateOptions","FAIfsTuneOptions","FAIfsOptions","FAIpartitionFlags","FAIlvmDevice");
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 $FAIpartitionMethod = "";       // "setup-storage" or not assigned
20   var $disks              = array();  // All defined Disks 
21   var $is_dialog          = false;    // specifies which buttons will be shown to save or abort
23   var $FAIstate           = "";
24   var $ui;
26   var $view_logged      = FALSE;
28   function faiPartitionTable (&$config, $dn= NULL)
29   {
30     /* Load Attributes */
31     plugin::plugin ($config, $dn);
33     /* If "dn==new" we try to create a new entry
34      * Else we must read all objects from ldap which belong to this entry.
35      */
36     $this->ui = get_userinfo();
37     if($dn != "new"){
38       $this->dn =$dn;
40       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
41        */
42       $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))");
43       foreach($res as $obj){
45         /* Skip not relevant objects */
46         if(!preg_match("/".preg_quote($this->dn, '/')."$/i",$obj['dn'])) continue;
47         $objects = array();
48         $objects['description']  = "";
50         // Transform disk type into image later...
51         if (!isset($obj['FAIdiskType'])){
52           $objects['FAIdiskType']        = "old";
53         } else {
54           $objects['FAIdiskType']        = $obj['FAIdiskType'];
55         }
57         // Get disk options, without 'count' index. 
58         $objects['FAIdiskOption'] = array();
59         if (isset($obj['FAIdiskOption'])){
60           for($i=0;$i<$obj['FAIdiskOption']['count'];$i++){ 
61             $objects['FAIdiskOption'][] = $obj['FAIdiskOption'][$i];
62           }
63         }
65         // Transform potential lvm information
66         if (isset($obj['FAIlvmDevice'])){
67           for($i=0;$i<$obj['FAIlvmDevice']['count'];$i++){
68             $name = $obj['FAIlvmDevice'][$i]; 
69             $objects['FAIlvmDevice'][$name] = $name;
70           }
71         }
73         $objects['dn']          = $obj['dn'];
74         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
75         $this->disks[$objects['cn']] = $objects;
76         $this->disks[$objects['cn']]['partitions'] = array();
77       }
79       /* read all partitions for each disk 
80        */
81       foreach($this->disks as $name => $disk){
82         $res = FAI::get_all_objects_for_given_base($disk['dn'],"(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))");
83         foreach($res as $obj){
85           /* Skip not relevant objects */
86           if(!preg_match("/".preg_quote($disk['dn'], '/')."$/i",$obj['dn'])) continue;
88           $objects = array();
89           $objects['dn']          = $obj['dn'];
90           $objects                = $this->get_object_attributes($objects,$this->subPartAttributes);
91           unset($objects['dn']);;
92           $this->disks[$name]['partitions'][$objects['FAIpartitionNr']] = $objects;
93         }
94       }
95     }
97     /* Keep track of disk changes, by comparing the initial and resulting 
98      *  disk setup.
99      */
100     $this->initial_disks = $this->disks;
102     $this->is_new = FALSE;
103     if($this->dn == "new"){
104       $this->is_new =TRUE;
105     }
106     ksort($this->disks);
108     // Prepare lists
109     $this->diskList = new sortableListing();
110     $this->diskList->setDeleteable(true);
111     $this->diskList->setEditable(true);
112     $this->diskList->setWidth("100%");
113     $this->diskList->setHeight("400px");
114     $this->diskList->setColspecs(array('20px','*','*','40px'));
115     $this->diskList->setHeader(array(("?"),_("Name"),_("Description"),"#"));
116     $this->diskList->setDefaultSortColumn(1);
118   }
121   function acl_base_for_current_object($dn)
122   {
123     if($dn == "new" || $dn == ""){
124       if($this->dn == "new"){
125         $dn= $this->parent->parent->acl_base;
126       }else{
127         $dn = $this->dn;
128       }
129     }
130     return($dn);
131   }
134   function execute()
135   {
136     /* Call parent execute */
137     plugin::execute();
139     if($this->is_account && !$this->view_logged){
140       $this->view_logged = TRUE;
141       new log("view","fai/".get_class($this),$this->dn);
142     }
144     /* Fill templating stuff */
145     $smarty= get_smarty();
146     $display= "";
147  
148     /* Add Disk to this Partitionset
149      * This code adds a new HDD to the disks 
150      * A new Dialog will be opened 
151      */
152     if((isset($_POST['AddDisk']) || isset($_POST['AddRaid']) || isset($_POST['AddVolgroup'])) && 
153         !preg_match("/freeze/i",$this->FAIstate)){
154       $usedDiskNames =array();
155       foreach($this->disks as $key=>$disk){
156         $usedDiskNames[]= $key;
157       }
158       if ($this->FAIpartitionMethod == "setup-storage") {
159         if(isset($_POST['AddDisk'])) $type = "disk";
160         if(isset($_POST['AddRaid'])) $type = "raid";
161         if(isset($_POST['AddVolgroup'])) $type = "lvm";
162         $this->dialog = new faiDiskEntry($this->config,$this->dn,$this, array(),$type); 
163       } else {
164         $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$this); 
165       }
167       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
168       $this->dialog->set_acl_category("fai");
169       $this->dialog->FAIstate = $this->FAIstate;
172       $this->is_dialog = true;
173     }
175     /* Edit disk.
176      * Open dialog which allows us to edit the selected entry 
177      */    
179     if($this->dn != "new"){
180       set_object_info($this->dn);
181     }
183     /* Edit entries via GET */
184     $s_action = "";
185     $s_entry = "";
186     if(isset($_GET['act']) && isset($_GET['id'])){
187       if($_GET['act'] == "edit" && isset($this->disks[$_GET['id']])){
188         $s_entry= $_GET['id'];
189         $s_action= "edit";
190       }
191     }
193     $this->diskList->save_object();
194     $action = $this->diskList->getAction();
195     if($action['action'] == 'edit'){
196         $s_entry = $this->diskList->getKey($action['targets'][0]);
197         $s_action ="edit";
198     }
199     if($action['action'] == 'delete'){
200         $s_entry = $this->diskList->getKey($action['targets'][0]);
201         $s_action ="remove";
202     }
204     /* Disk remove was requested. 
205      * Now check if the disk is still in use, in this case 
206      *  display a warning message and abort the removal. 
207      * If the disk is not used anymore, then remove it. 
208      */
209     if($s_action == "remove"){
210       if(isset($this->disks[$s_entry])){
212         /* Create a list of all partitions that are used in 
213          *  lvm or raid compilations. 
214          */
215         $list = array();
216         foreach($this->disks as $dname => $disk){
217           if($disk['FAIdiskType'] != "disk" && $dname != $s_entry){
218             if($disk['FAIdiskType'] == "lvm"){
219               foreach($disk['FAIlvmDevice'] as $partname){
220                 $list[preg_replace("/:.*$/","",$partname)][] = $disk;
221               }
222             }
223             foreach($disk['partitions'] as $partkey => $part){
224               if($disk['FAIdiskType'] == "raid"){
225                 foreach(explode(",",$part['FAIpartitionSize']) as $partname){
226                   $list[preg_replace("/:.*$/","",$partname)][] = $disk;
227                 }
228               }
229             }  
230           }
231         }
233         /* Now that we've a list of all partition references, lets check if
234          *  one of the partitions we are going to remove is still in use.
235          */
236         $used = array();
237         foreach($this->disks[$s_entry]['partitions'] as $part){
238           if(isset($list[$part['cn']])){    
239             foreach($list[$part['cn']] as $disk){
240               $used[$disk['cn']] = $disk['cn'];
241             }
242           }
243         }
245         /* Skip removal while disk is in use. 
246          */
247         if(count($used)){
248           $used = implode(",",$used);
249           msg_dialog::display(_("Error"), 
250               sprintf(_("The disk cannot be deleted while it is used in the '%s' disk definition!"), 
251                 $used), ERROR_DIALOG);
252         }else{
254           /* Everything is ok, we can remove the disk now.
255            */
256           unset($this->disks[$s_entry]);
257         } 
258       } 
259     }
261     
263     if($s_action == "edit"){
265       /* Set object info string, which will be displayed in plugin info line */ 
266       if(isset($this->disks[$s_entry]['dn'])){
267         set_object_info($this->disks[$s_entry]['dn']);
268         $dn = $this->disks[$s_entry]['dn'];
269       }else{
270         set_object_info("");
271         $dn = "new";
272       }
274       $type ="old"; 
275       if(isset($this->disks[$s_entry]['FAIdiskType'])){
276         $type = $this->disks[$s_entry]['FAIdiskType'];
277       }
278       if(in_array_strict($type,array('raid','lvm','disk'))){
279         $this->dialog = new faiDiskEntry(
280             $this->config,$this->dn,$this,$this->disks[$s_entry], 
281             $this->disks[$s_entry]['FAIdiskType']); 
283       }else{
284         $this->dialog = new faiPartitionTableEntry(
285             $this->config,$this->dn,$this,$this->disks[$s_entry]); 
286       }
287       if($this->dialog){
288         $this->dialog->set_acl_base($this->acl_base_for_current_object($dn));
289         $this->dialog->set_acl_category("fai");
290         $this->dialog->FAIstate = $this->FAIstate;
291         $this->is_dialog = true;
292       }
293     }
295     /* Edit aborted, close dialog, without saving anything
296      */
297     if(isset($_POST['CancelDisk'])){
298       unset($this->dialog);
299       $this->dialog = FALSE;
300       $this->is_dialog=false;
301     }
303     /* Dialog saved
304      * Save given data from Dialog, if no error is occurred
305      */
306     if(isset($_POST['SaveDisk'])){
308       if (!preg_match("/freeze/i", $this->FAIstate)){
309         $this->dialog->save_object();
310         if(count($this->dialog->check())){
311           foreach($this->dialog->check() as $msg){
312             msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
313           }
314         }else{
315           $disk = $this->dialog->save();
316           if(isset($disk['rename'])){
317             unset($this->disks[$disk['rename']['from']]);
318             $disk['cn']= $disk['rename']['to'];
319           }
321           $this->disks[$disk['cn']]=$disk; 
322           unset($this->dialog);
323           $this->dialog = FALSE;
324           $this->is_dialog=false;
325           ksort($this->disks);
326         }
327       }else{
328         $this->dialog = FALSE;
329         $this->is_dialog=false;
330       }
331     }
333     /* Display dialog if one is defined
334      */
335     if(is_object($this->dialog)){
336       $this->dialog->save_object();
337       return($this->dialog->execute());
338     }
340     /* Assign all attributes to smarty engine
341      */
342     foreach($this->attributes as $attrs){
343       $smarty->assign($attrs,set_post($this->$attrs));
344       if($this->$attrs){
345         $smarty->assign($attrs."CHK"," ");
346       }else{
347         $smarty->assign($attrs."CHK"," disabled ");
348       }
349     }
350    
351     $dn = $this->acl_base_for_current_object($this->dn);
352     $smarty->assign("sub_object_is_addable",
353         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
354         !preg_match("/freeze/",$this->FAIstate));
355     $smarty->assign("sub_object_is_removeable",
356         preg_match("/d/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
357         !preg_match("/freeze/",$this->FAIstate));
359     $tmp = $this->plInfo();
360     foreach($tmp['plProvidedAcls'] as $name => $translated){
361       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/i",$this->FAIstate)));
362     }
364     /* Assign mode */
365     if ($this->FAIpartitionMethod == ""){
366       $smarty->assign("mode", "");
367       $smarty->assign("storage_mode", "disabled");
368     } else {
369       $smarty->assign("mode", "checked");
370       $smarty->assign("storage_mode", "");
371     }
372     if (!count($this->disks)) {
373       $smarty->assign("lockmode", "");
374     } else {
375       $smarty->assign("lockmode", "disabled");
376     }
377     if (isset($this->disks['raid'])){
378       $smarty->assign("addraid", "disabled");
379     } else {
380       $smarty->assign("addraid", "");
381     }
383     $dn = $this->acl_base_for_current_object($this->dn);
384     $acl = $this->ui->get_permissions($dn,"fai/faiPartitionTableEntry");
385     $this->diskList->setAcl($acl);
386     $data = $lData = array();
387     foreach($this->disks as $key => $disk){
388       $act = "";
390       $dn = "new";
391       if(isset($obj['dn'])){
392         $dn = $obj['dn'];
393       }
394       $dn = $this->acl_base_for_current_object($dn);
395       $acl = $this->ui->get_permissions($dn,"fai/faiPartitionTableEntry");
396       if(preg_match("/(r|w)/",$acl)) {
397         $cnt= count($disk['partitions']);
398         $types= array(
399             "old"  => "plugins/fai/images/fai_partitionTable.png", 
400             "disk" => "plugins/fai/images/fai_partitionTable.png",
401             "raid" => "plugins/fai/images/raid.png", "lvm" => "plugins/ogroups/images/list_ogroup.png");
402         $type = isset($disk['FAIdiskType'])?$types[$disk['FAIdiskType']]:$types['old'];
403         $data[$key] = $key;
404         $lData[$key] = array('data' => array(image($type),$key,$disk['description'],$cnt));
405       }
406     }
407     $this->diskList->setListData($data,$lData);
408     $this->diskList->update();
409     $smarty->assign("Entry_listing",$this->diskList->render());
411     $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
412     return($display);
413   }
416   function getUsedDiskNames()
417   {
418     $ret = array();
419     foreach($this->disks as $disk){
420       $ret[] = $disk['cn'];
421     }
422     return($ret);  
423   }
426   /* Delete me, and all my subtrees
427    */
428   function remove_from_parent()
429   {
430     $ldap = $this->config->get_ldap_link();
431     $ldap->cd ($this->dn);
433     $release = $this->parent->parent->fai_release;
434     $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
436     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
437     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);   
438     foreach($this->initial_disks as $disk){
439       $disk_dn = "cn=".$disk['cn'].",".$this->dn;
440       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i",$release, $disk_dn);
441       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
442       foreach($disk['partitions'] as $key => $partition){    
443         $partition_dn= "FAIpartitionNr=".$partition['FAIpartitionNr'].",".$disk_dn;      
444         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $partition_dn);
445         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
446       }
447     }
448   }
451   /* Save data to object 
452    */
453   function save_object()
454   {
455     if(isset($_POST['FAIpartitionTablePosted'])){
456       if (preg_match("/freeze/", $this->FAIstate)) return;
457       plugin::save_object();
458       if(!count($this->disks)){
459         if(isset($_POST['mode'])){
460           $this->FAIpartitionMethod = "setup-storage";
461         }else{
462           $this->FAIpartitionMethod = "";
463         }
464       }
465     }
466   }
469   /* Check supplied data */
470   function check()
471   {
472     /* Call common method to give check the hook */
473     $message= plugin::check();
475     /* Ensure that we do not overwrite an allready existing entry 
476      */
477     if($this->is_new){
478       $release = $this->parent->parent->fai_release;
479       $new_dn= 'cn='.$this->cn.",".get_ou("faiPartitionTable", "faiPartitionRDN").get_ou("faiManagement", "faiBaseRDN").$release;
480       $res = faiManagement::check_class_name("FAIpartitionTable",$this->cn,$new_dn);
481       if(isset($res[$this->cn])){
482         $message[] = msgPool::duplicated(_("Name"));
483       }
484     }
485     return ($message);
486   }
489   /* Save to LDAP */
490   function save()
491   {
492     plugin::save();
494     /* Save current settings.
495      * 1 : We must save the partition table, with its description and cn 
496      * 2 : Append Disk with cn and  description.
497      * 3 : Save partitions for each disk
498      */  
500     $ldap = $this->config->get_ldap_link();
502     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
504     if($this->initially_was_account){
505       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
506       @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$this->dn , "Saving disk: ");
507     }else{
508       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
509       @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$this->dn , "Adding disk: ");
510     }
511  
512     // Prepare disks to be saved - The 'status' attribute is added here.
513     $this->prepareDiskToBeSave(); 
514  
515     /* Sort entries, because we must delete entries with status="delete" first */
516     $order = array();
517     foreach($this->disks as $key => $disk){
518       if($disk['status'] == "delete"){
519         $order[$key] = $disk;
520       }
521     }
522     foreach($this->disks as $key => $disk){
523       if($disk['status'] != "delete"){
524         $order[$key] = $disk;
525       }
526     }
529     /* Append all disks to ldap */
530     foreach($order as $cn=>$disk){
532       $disk_dn                    = "cn=".$disk['cn'].",".$this->dn;
533       $short_dn                   = "cn=".$disk['cn'].",...";
534       $disk_attrs['cn']           =  $disk['cn'];
535       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
536   
538       if($disk['status']=="new"){
539         $ldap->cat($disk_dn,array("objectClass"));
540         if($ldap->count()){
541           $disk['status']="edited";
542         }
543       }
545       foreach(array("description","FAIdiskType","FAIdiskOption","FAIlvmDevice") as $attr){
546         if($disk['status'] == "new"){
547           if(isset($disk_attrs[$attr])) unset($disk_attrs[$attr]);
548           if(isset($disk[$attr]) && !empty($disk[$attr])){
549             if(is_array($disk[$attr])){
550               $disk_attrs[$attr] = array_values($disk[$attr]);
551             }else{
552               $disk_attrs[$attr] = $disk[$attr];
553             }
554           }
555         }else{
556           if(isset($disk[$attr]) && !empty($disk[$attr])){
557             if(is_array($disk[$attr])){
558               $disk_attrs[$attr] = array_values($disk[$attr]);
559             }else{
560               $disk_attrs[$attr] = $disk[$attr];
561             }
562           }else{
563             $disk_attrs[$attr] = array();
564           }
565         }
566       }
569       /* Tag object */
570       $this->tag_attrs($disk_attrs, $disk_dn, $this->gosaUnitTag);
572       if($disk['status'] == "delete"){
573         @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Removing disk: ");
574         FAI::prepare_to_save_FAI_object($disk_dn,array(),true);
575         $this->handle_post_events("remove");
576         unset($this->disks[$cn]);
577       }elseif($disk['status'] == "edited"){
578         @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Updating disk: ");
579         FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
580         $this->handle_post_events("modify");
581       }elseif($disk['status']=="new"){
582         @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Adding disk: ");
583         FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
584         $this->handle_post_events("add");
585       }
587       if($disk['status']!="delete")
589       /* Add all partitions */
590       foreach($disk['partitions'] as $pkey => $partition){
591         $partition_attrs = array();
593         foreach($partition as $key => $value){
594           if(!empty($value)){
595             $partition_attrs[$key]=$value;        
596           }else{
597             unset($partition_attrs[$key]);        
598           }
599         }
601         $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;     
602         $short_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",...";
603      
604         $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
605         
606         unset($partition_attrs['status']);
607         unset($partition_attrs['old_cn']);
609         if($partition['status']=="new"){
610           $ldap->cat($partition_dn,array("objectClass"));
611           if($ldap->count()){
612             $partition['status']="edited";
613           }
614         }
616         if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
617           $partition_attrs['FAImountPoint']="swap";
618         }
620         /* Tag object */
621         $this->tag_attrs($partition_attrs, $partition_dn, $this->gosaUnitTag);
623         
624         if($partition['status'] == "delete"){
625           @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Removing partition: ");
626           FAI::prepare_to_save_FAI_object($partition_dn,array(),true);
627           $this->handle_post_events("remove");
628           unset($this->disks[$cn]['partitions'][$pkey]);
629         }elseif($partition['status'] == "edited"){
630           @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Updating partition: ");
631           FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
632           $this->handle_post_events("modify");
633         }elseif($partition['status']=="new"){
634           @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Adding partition: ");
635           FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
636           $this->handle_post_events("add");
637         }
638         
639         // We do not need the status flag any longer 
640         if(isset($this->disks[$cn]['partitions'][$pkey]['status'])){
641           unset($this->disks[$cn]['partitions'][$pkey]['status']);
642         }
643       }
644       // We do not need the status flag any longer 
645       if(isset($this->disks[$cn]['status'])){
646         unset($this->disks[$cn]['status']);
647       }
648     }
649     $this->initial_disks = $this->disks;
650     $this->handle_post_events("add");
651   }
654   function prepareDiskToBeSave()
655   {
656     foreach($this->disks as $id => $disk){
657       
658       /* Correct FAIpartitionNr.
659        * If we've only primary partition then set the partition numbers from  
660        *  1 to 4, else set the primary from 1 to 3 and logical >= 5 
661        * 
662        */
663       if(!isset($disk['partitions'])){
664         $disk['partitions'] = array();
665       }
666       $newSetup = array();
668       if($disk['FAIdiskType'] == "disk"){
669         $primary = $logical = array();
670         foreach($disk['partitions'] as $partid => $part){
671           if($part['FAIpartitionType'] == "primary"){
672             $primary[$partid] = $part;
673           }elseif($part['FAIpartitionType'] == "logical"){
674             $logical[$partid] = $part;
675           }else{
676             trigger_error("Fatal: unknown disk type? ".$part['FAIpartitionType']); 
677           }
678         }
679         $cnt = 1;
680         foreach($primary as $part){
681           $part['FAIpartitionNr'] = $cnt;
682           $part['cn'] = $disk['cn'].$cnt;
683           $newSetup[$cnt] = $part;
684           $cnt ++;
685         } 
686         $cnt = 5;
687         foreach($logical as $part){
688           $part['FAIpartitionNr'] = $cnt;
689           $part['cn'] = $disk['cn'].$cnt;
690           $newSetup[$cnt] = $part;
691           $cnt ++;
692         }
693         $this->disks[$disk['cn']]['partitions'] = $newSetup; 
694       }
695     }
697  
698     # FAIpartitionNr have to be used as index for this->disks
699     #  else the next operation will fail. 
701  
702     /* Check if there are disks to be removed, edited or added.
703      * We compare the initial disk setup with the current setup and
704      *  and add a status flag, which will then be used to perform the 
705      *  correct action - add, edited, remove.
706      */
707     foreach($this->disks as $key => $disk){
709       // - A complete NEW disk
710       if(!isset($this->initial_disks[$disk['cn']])){
711         $this->disks[$key]['status'] = "new";
712         foreach($disk['partitions'] as $pkey => $part){
713           $this->disks[$disk['cn']]['partitions'][$pkey]['status'] = "new";
714         }
715       }else{
716     
717         // - Disk was "EDITED" 
718         $this->disks[$key]['status'] = "edited";
719         foreach($disk['partitions'] as $pkey => $part){
721           // - Check whether partition is "NEW" or "EDITED" 
722           if(!isset($this->initial_disks[$key]['partitions'][$pkey])){
723             $this->disks[$key]['partitions'][$pkey]['status'] = "new";
724           }else{
725             $this->disks[$key]['partitions'][$pkey]['status'] = "edited";
726           }
727         }
728       }
729     }
730      
731     /* Check which partitions havbe to be removed. 
732      * (They intially existed, but are now gone.)
733      */ 
734     foreach($this->initial_disks as $ikey => $idisk){
735       
736       // - Complete disk was REMOVED.
737       if(!isset($this->disks[$idisk['cn']])){
738         $this->disks[$idisk['cn']] = $idisk;
739         $this->disks[$idisk['cn']]['status'] = "delete";
740         foreach($idisk['partitions'] as $pkey=>$part){
741           $this->disks[$idisk['cn']]['partitions'][$pkey] = $part;
742           $this->disks[$idisk['cn']]['partitions'][$pkey]["status"] = "delete";
743         }
744       }else{
745         foreach($idisk['partitions'] as $pkey=>$part){
746           if(!isset($this->disks[$idisk['cn']]['partitions'][$pkey])){
747             $this->disks[$idisk['cn']]['partitions'][$pkey] = $part;
748             $this->disks[$idisk['cn']]['partitions'][$pkey]["status"] = "delete";
749           }
750         }
751       }
752     }
753   }
754   
756   function PrepareForCopyPaste($source)
757   {
758     plugin::PrepareForCopyPaste($source);
760     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
761      */
763     /* To be sure to copy disks and partitions correctly, just create a 
764      *  new PartitionTable object and use the 'disks' attribute 
765      *  from this it. This is much easier and less code.
766      */
767     $obj = new faiPartitionTable($this->config, $source['dn']);
768     $this->disks = $obj->disks;
769   }
772   /* Return plugin informations for acl handling */ 
773   static function plInfo()
774   {
775     return (array( 
776           "plShortName" => _("Partition table"),
777           "plDescription" => _("FAI partition table"),
778           "plSelfModify"  => FALSE,
779           "plDepends"     => array(),
780           "plPriority"    => 26,
781           "plSection"     => array("administration"),
782           "plCategory"    => array("fai"),
783           "plProperties" =>
784           array(
785               array(
786                   "name"          => "faiPartitionRDN",
787                   "type"          => "rdn",
788                   "default"       => "ou=disk,",
789                   "description"   => _("RDN for FAI partition storage."),
790                   "check"         => "gosaProperty::isRdn",
791                   "migrate"       => "migrate_faiPartitionRDN",
792                   "group"         => "plugin",
793                   "mandatory"     => TRUE)),
795           "plProvidedAcls" => array(
796             "cn"                => _("Name")."&nbsp;("._("Read only").")",
797             "description"       => _("Description"))
798           ));
799   }
802   /*! \brief  Used for copy & paste.
803     Returns a HTML input mask, which allows to change the cn of this entry.
804     @param  Array   Array containing current status && a HTML template.
805    */
806   function getCopyDialog()
807   {
808     $vars = array("cn");
809     $smarty = get_smarty();
810     $smarty->assign("cn", set_post($this->cn));
811     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
812     $ret = array();
813     $ret['string'] = $str;
814     $ret['status'] = "";
815     return($ret);
816   }
819   /*! \brief  Used for copy & paste.
820     Some entries must be renamed to avaoid duplicate entries.
821    */
822   function saveCopyDialog()
823   {
824     if(isset($_POST['cn'])){
825       $this->cn = get_post('cn');
826     }
827   }
829   /* Reload some attributes */
830   function get_object_attributes($object,$attributes)
831   {
832     $ldap = $this->config->get_ldap_link();
833     $ldap->cd($this->config->current['BASE']);
834     $ldap->cat($object['dn'],$attributes);
835     $tmp  = $ldap->fetch();
837     foreach($attributes as $attrs){
838       if(isset($tmp[$attrs][0])){
839         $var = $tmp[$attrs][0];
841         /* Check if we must decode some attributes */
842         if(in_array_ics($attrs,$this->sub64coded)){
843           $var = base64_decode($var);
844         }
846         /*  check if this is a binary entry */
847         if(in_array_ics($attrs,$this->subBinary)){
848           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
849         }
851         $object[$attrs] = $var;
852       }
853     }
854     return($object);
855   }
859 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
860 ?>