Code

Replaced in_array calls for gosa-plugins
[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);
107   }
110   function acl_base_for_current_object($dn)
111   {
112     if($dn == "new" || $dn == ""){
113       if($this->dn == "new"){
114         $dn= $this->parent->parent->acl_base;
115       }else{
116         $dn = $this->dn;
117       }
118     }
119     return($dn);
120   }
123   function execute()
124   {
125     /* Call parent execute */
126     plugin::execute();
128     if($this->is_account && !$this->view_logged){
129       $this->view_logged = TRUE;
130       new log("view","fai/".get_class($this),$this->dn);
131     }
133     /* Fill templating stuff */
134     $smarty= get_smarty();
135     $display= "";
136  
137     /* Add Disk to this Partitionset
138      * This code adds a new HDD to the disks 
139      * A new Dialog will be opened 
140      */
141     if((isset($_POST['AddDisk']) || isset($_POST['AddRaid']) || isset($_POST['AddVolgroup'])) && 
142         !preg_match("/freeze/i",$this->FAIstate)){
143       $usedDiskNames =array();
144       foreach($this->disks as $key=>$disk){
145         $usedDiskNames[]= $key;
146       }
147       if ($this->FAIpartitionMethod == "setup-storage") {
148         if(isset($_POST['AddDisk'])) $type = "disk";
149         if(isset($_POST['AddRaid'])) $type = "raid";
150         if(isset($_POST['AddVolgroup'])) $type = "lvm";
151         $this->dialog = new faiDiskEntry($this->config,$this->dn,$this, array(),$type); 
152       } else {
153         $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$this); 
154       }
156       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
157       $this->dialog->set_acl_category("fai");
158       $this->dialog->FAIstate = $this->FAIstate;
161       $this->is_dialog = true;
162     }
164     /* Edit disk.
165      * Open dialog which allows us to edit the selected entry 
166      */    
168     if($this->dn != "new"){
169       set_object_info($this->dn);
170     }
172     /* Edit entries via GET */
173     $s_action = "";
174     $s_entry = "";
175     if(isset($_GET['act']) && isset($_GET['id'])){
176       if($_GET['act'] == "edit" && isset($this->disks[$_GET['id']])){
177         $s_entry= $_GET['id'];
178         $s_action= "edit";
179       }
180     }
182     /* New Listhandling */
183     foreach($_POST as $name => $value){
184       if(preg_match("/^edit_[^_]*_[xy]$/",$name)){
185         $s_entry = preg_replace("/^edit_/","",$name);
186         $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
187         $s_action = "edit"; 
188         break;
189       }
190       if (!preg_match("/freeze/i", $this->FAIstate) && preg_match("/^delete_[^_]*_/",$name)){
191         $s_entry = preg_replace("/^delete_/","",$name);
192         $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
193         $s_action = "remove";
194         break;
195       }
196     }
199     /* Disk remove was requested. 
200      * Now check if the disk is still in use, in this case 
201      *  display a warning message and abort the removal. 
202      * If the disk is not used anymore, then remove it. 
203      */
204     if($s_action == "remove"){
205       if(isset($this->disks[$s_entry])){
207         /* Create a list of all partitions that are used in 
208          *  lvm or raid compilations. 
209          */
210         $list = array();
211         foreach($this->disks as $dname => $disk){
212           if($disk['FAIdiskType'] != "disk" && $dname != $s_entry){
213             if($disk['FAIdiskType'] == "lvm"){
214               foreach($disk['FAIlvmDevice'] as $partname){
215                 $list[preg_replace("/:.*$/","",$partname)][] = $disk;
216               }
217             }
218             foreach($disk['partitions'] as $partkey => $part){
219               if($disk['FAIdiskType'] == "raid"){
220                 foreach(explode(",",$part['FAIpartitionSize']) as $partname){
221                   $list[preg_replace("/:.*$/","",$partname)][] = $disk;
222                 }
223               }
224             }  
225           }
226         }
228         /* Now that we've a list of all partition references, lets check if
229          *  one of the partitions we are going to remove is still in use.
230          */
231         $used = array();
232         foreach($this->disks[$s_entry]['partitions'] as $part){
233           if(isset($list[$part['cn']])){    
234             foreach($list[$part['cn']] as $disk){
235               $used[$disk['cn']] = $disk['cn'];
236             }
237           }
238         }
240         /* Skip removal while disk is in use. 
241          */
242         if(count($used)){
243           $used = implode(",",$used);
244           msg_dialog::display(_("Error"), 
245               sprintf(_("The disk cannot be deleted while it is used in the '%s' disk definition!"), 
246                 $used), ERROR_DIALOG);
247         }else{
249           /* Everything is ok, we can remove the disk now.
250            */
251           unset($this->disks[$s_entry]);
252         } 
253       } 
254     }
256     
258     if($s_action == "edit"){
260       /* Set object info string, which will be displayed in plugin info line */ 
261       if(isset($this->disks[$s_entry]['dn'])){
262         set_object_info($this->disks[$s_entry]['dn']);
263         $dn = $this->disks[$s_entry]['dn'];
264       }else{
265         set_object_info("");
266         $dn = "new";
267       }
269       $type ="old"; 
270       if(isset($this->disks[$s_entry]['FAIdiskType'])){
271         $type = $this->disks[$s_entry]['FAIdiskType'];
272       }
273       if(in_array_strict($type,array('raid','lvm','disk'))){
274         $this->dialog = new faiDiskEntry(
275             $this->config,$this->dn,$this,$this->disks[$s_entry], 
276             $this->disks[$s_entry]['FAIdiskType']); 
278       }else{
279         $this->dialog = new faiPartitionTableEntry(
280             $this->config,$this->dn,$this,$this->disks[$s_entry]); 
281       }
282       if($this->dialog){
283         $this->dialog->set_acl_base($this->acl_base_for_current_object($dn));
284         $this->dialog->set_acl_category("fai");
285         $this->dialog->FAIstate = $this->FAIstate;
286         $this->is_dialog = true;
287       }
288     }
290     /* Edit aborted, close dialog, without saving anything
291      */
292     if(isset($_POST['CancelDisk'])){
293       unset($this->dialog);
294       $this->dialog = FALSE;
295       $this->is_dialog=false;
296     }
298     /* Dialog saved
299      * Save given data from Dialog, if no error is occurred
300      */
301     if(isset($_POST['SaveDisk'])){
303       if (!preg_match("/freeze/i", $this->FAIstate)){
304         $this->dialog->save_object();
305         if(count($this->dialog->check())){
306           foreach($this->dialog->check() as $msg){
307             msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
308           }
309         }else{
310           $disk = $this->dialog->save();
311           if(isset($disk['rename'])){
312             unset($this->disks[$disk['rename']['from']]);
313             $disk['cn']= $disk['rename']['to'];
314           }
316           $this->disks[$disk['cn']]=$disk; 
317           unset($this->dialog);
318           $this->dialog = FALSE;
319           $this->is_dialog=false;
320           ksort($this->disks);
321         }
322       }else{
323         $this->dialog = FALSE;
324         $this->is_dialog=false;
325       }
326     }
328     /* Display dialog if one is defined
329      */
330     if(is_object($this->dialog)){
331       $this->dialog->save_object();
332       return($this->dialog->execute());
333     }
335     /* Assign all attributes to smarty engine
336      */
337     foreach($this->attributes as $attrs){
338       $smarty->assign($attrs,$this->$attrs);
339       if($this->$attrs){
340         $smarty->assign($attrs."CHK"," ");
341       }else{
342         $smarty->assign($attrs."CHK"," disabled ");
343       }
344     }
345    
346     $dn = $this->acl_base_for_current_object($this->dn);
347     $smarty->assign("sub_object_is_addable",
348         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
349         !preg_match("/freeze/",$this->FAIstate));
350     $smarty->assign("sub_object_is_removeable",
351         preg_match("/d/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
352         !preg_match("/freeze/",$this->FAIstate));
354     $tmp = $this->plInfo();
355     foreach($tmp['plProvidedAcls'] as $name => $translated){
356       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/i",$this->FAIstate)));
357     }
359     /* Assign mode */
360     if ($this->FAIpartitionMethod == ""){
361       $smarty->assign("mode", "");
362       $smarty->assign("storage_mode", "disabled");
363     } else {
364       $smarty->assign("mode", "checked");
365       $smarty->assign("storage_mode", "");
366     }
367     if (!count($this->disks)) {
368       $smarty->assign("lockmode", "");
369     } else {
370       $smarty->assign("lockmode", "disabled");
371     }
372     if (isset($this->disks['raid'])){
373       $smarty->assign("addraid", "disabled");
374     } else {
375       $smarty->assign("addraid", "");
376     }
378     /* Divlist containing disks */
379     $divlist = new divSelectBox("FAItemplates");
380     $divlist->setHeight(400);
381     foreach($this->disks as $key => $disk){
382       $act = "";
384       $dn = "new";
385       if(isset($obj['dn'])){
386         $dn = $obj['dn'];
387       }
388       $dn = $this->acl_base_for_current_object($dn);
389       $acl = $this->ui->get_permissions($dn,"fai/faiPartitionTableEntry");
390       if(preg_match("/(r|w)/",$acl)) {
392         $act .= "<input type='image' src='images/lists/edit.png'   name='edit_%s'    title='"._("edit")."' alt='"._("edit")."'>";
393         if(preg_match("/d/",$acl) && !preg_match("/freeze/", $this->FAIstate)){
394           $act .="<input type='image' src='images/lists/trash.png' name='delete_%s'  title='"._("delete")."' alt='"._("delete")."'>";
395         }
397         $cnt= count($disk['partitions']);
399         $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".$key."'>".$key."</a>";
400         $types= array("old" => "plugins/fai/images/fai_partitionTable.png", "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         $divlist->AddEntry(array( 
404               array("string"=> "<img border='0' src='".$type."'>", "attach"=>"style='width:16px'"),
405               array("string"=> $edit_link, "attach"=>"style='width:100px'"),
406               array("string"=> $disk['description']),
407               array("string"=> $cnt,  "attach"=>"style='width:16px'"),
408               array("string"=>str_replace("%s",base64_encode($key),$act),
409                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
410       }
411     }
412     $smarty->assign("Entry_divlist",$divlist->DrawList());
414     $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
415     return($display);
416   }
419   function getUsedDiskNames()
420   {
421     $ret = array();
422     foreach($this->disks as $disk){
423       $ret[] = $disk['cn'];
424     }
425     return($ret);  
426   }
429   /* Delete me, and all my subtrees
430    */
431   function remove_from_parent()
432   {
433     $ldap = $this->config->get_ldap_link();
434     $ldap->cd ($this->dn);
436     $release = $this->parent->parent->fai_release;
437     $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
439     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
440     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);   
441     foreach($this->initial_disks as $disk){
442       $disk_dn = "cn=".$disk['cn'].",".$this->dn;
443       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i",$release, $disk_dn);
444       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
445       foreach($disk['partitions'] as $key => $partition){    
446         $partition_dn= "FAIpartitionNr=".$partition['FAIpartitionNr'].",".$disk_dn;      
447         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $partition_dn);
448         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
449       }
450     }
451   }
454   /* Save data to object 
455    */
456   function save_object()
457   {
458     if(isset($_POST['FAIpartitionTablePosted'])){
459       if (preg_match("/freeze/", $this->FAIstate)) return;
460       plugin::save_object();
461       foreach($this->attributes as $attrs){
462         if(isset($_POST[$attrs])){
463           $this->$attrs = $_POST[$attrs];
464         }
465       }
466       if(!count($this->disks)){
467         if(isset($_POST['mode'])){
468           $this->FAIpartitionMethod = "setup-storage";
469         }else{
470           $this->FAIpartitionMethod = "";
471         }
472       }
473     }
474   }
477   /* Check supplied data */
478   function check()
479   {
480     /* Call common method to give check the hook */
481     $message= plugin::check();
483     /* Ensure that we do not overwrite an allready existing entry 
484      */
485     if($this->is_new){
486       $release = $this->parent->parent->fai_release;
487       $new_dn= 'cn='.$this->cn.",".get_ou('faiPartitionRDN').get_ou('faiBaseRDN').$release;
488       $res = faiManagement::check_class_name("FAIpartitionTable",$this->cn,$new_dn);
489       if(isset($res[$this->cn])){
490         $message[] = msgPool::duplicated(_("Name"));
491       }
492     }
493     return ($message);
494   }
497   /* Save to LDAP */
498   function save()
499   {
500     plugin::save();
502     /* Save current settings.
503      * 1 : We must save the partition table, with its description and cn 
504      * 2 : Append Disk with cn and  description.
505      * 3 : Save partitions for each disk
506      */  
508     $ldap = $this->config->get_ldap_link();
510     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
512     if($this->initially_was_account){
513       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
514       @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$this->dn , "Saving disk: ");
515     }else{
516       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
517       @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$this->dn , "Adding disk: ");
518     }
519  
520     // Prepare disks to be saved - The 'status' attribute is added here.
521     $this->prepareDiskToBeSave(); 
522  
523     /* Sort entries, because we must delete entries with status="delete" first */
524     $order = array();
525     foreach($this->disks as $key => $disk){
526       if($disk['status'] == "delete"){
527         $order[$key] = $disk;
528       }
529     }
530     foreach($this->disks as $key => $disk){
531       if($disk['status'] != "delete"){
532         $order[$key] = $disk;
533       }
534     }
537     /* Append all disks to ldap */
538     foreach($order as $cn=>$disk){
540       $disk_dn                    = "cn=".$disk['cn'].",".$this->dn;
541       $short_dn                   = "cn=".$disk['cn'].",...";
542       $disk_attrs['cn']           =  $disk['cn'];
543       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
544   
546       if($disk['status']=="new"){
547         $ldap->cat($disk_dn,array("objectClass"));
548         if($ldap->count()){
549           $disk['status']="edited";
550         }
551       }
553       foreach(array("description","FAIdiskType","FAIdiskOption","FAIlvmDevice") as $attr){
554         if($disk['status'] == "new"){
555           if(isset($disk_attrs[$attr])) unset($disk_attrs[$attr]);
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           }
563         }else{
564           if(isset($disk[$attr]) && !empty($disk[$attr])){
565             if(is_array($disk[$attr])){
566               $disk_attrs[$attr] = array_values($disk[$attr]);
567             }else{
568               $disk_attrs[$attr] = $disk[$attr];
569             }
570           }else{
571             $disk_attrs[$attr] = array();
572           }
573         }
574       }
577       /* Tag object */
578       $this->tag_attrs($disk_attrs, $disk_dn, $this->gosaUnitTag);
580       if($disk['status'] == "delete"){
581         @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Removing disk: ");
582         FAI::prepare_to_save_FAI_object($disk_dn,array(),true);
583         $this->handle_post_events("remove");
584         unset($this->disks[$cn]);
585       }elseif($disk['status'] == "edited"){
586         @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Updating disk: ");
587         FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
588         $this->handle_post_events("modify");
589       }elseif($disk['status']=="new"){
590         @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Adding disk: ");
591         FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
592         $this->handle_post_events("add");
593       }
595       if($disk['status']!="delete")
597       /* Add all partitions */
598       foreach($disk['partitions'] as $pkey => $partition){
599         $partition_attrs = array();
601         foreach($partition as $key => $value){
602           if(!empty($value)){
603             $partition_attrs[$key]=$value;        
604           }else{
605             unset($partition_attrs[$key]);        
606           }
607         }
609         $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;     
610         $short_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",...";
611      
612         $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
613         
614         unset($partition_attrs['status']);
615         unset($partition_attrs['old_cn']);
617         if($partition['status']=="new"){
618           $ldap->cat($partition_dn,array("objectClass"));
619           if($ldap->count()){
620             $partition['status']="edited";
621           }
622         }
624         if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
625           $partition_attrs['FAImountPoint']="swap";
626         }
628         /* Tag object */
629         $this->tag_attrs($partition_attrs, $partition_dn, $this->gosaUnitTag);
631         
632         if($partition['status'] == "delete"){
633           @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Removing partition: ");
634           FAI::prepare_to_save_FAI_object($partition_dn,array(),true);
635           $this->handle_post_events("remove");
636           unset($this->disks[$cn]['partitions'][$pkey]);
637         }elseif($partition['status'] == "edited"){
638           @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Updating partition: ");
639           FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
640           $this->handle_post_events("modify");
641         }elseif($partition['status']=="new"){
642           @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Adding partition: ");
643           FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
644           $this->handle_post_events("add");
645         }
646         
647         // We do not need the status flag any longer 
648         if(isset($this->disks[$cn]['partitions'][$pkey]['status'])){
649           unset($this->disks[$cn]['partitions'][$pkey]['status']);
650         }
651       }
652       // We do not need the status flag any longer 
653       if(isset($this->disks[$cn]['status'])){
654         unset($this->disks[$cn]['status']);
655       }
656     }
657     $this->initial_disks = $this->disks;
658     $this->handle_post_events("add");
659   }
662   function prepareDiskToBeSave()
663   {
664     foreach($this->disks as $id => $disk){
665       
666       /* Correct FAIpartitionNr.
667        * If we've only primary partition then set the partition numbers from  
668        *  1 to 4, else set the primary from 1 to 3 and logical >= 5 
669        * 
670        */
671       if(!isset($disk['partitions'])){
672         $disk['partitions'] = array();
673       }
674       $newSetup = array();
676       if($disk['FAIdiskType'] == "disk"){
677         $primary = $logical = array();
678         foreach($disk['partitions'] as $partid => $part){
679           if($part['FAIpartitionType'] == "primary"){
680             $primary[$partid] = $part;
681           }elseif($part['FAIpartitionType'] == "logical"){
682             $logical[$partid] = $part;
683           }else{
684             trigger_error("Fatal: unknown disk type? ".$part['FAIpartitionType']); 
685           }
686         }
687         $cnt = 1;
688         foreach($primary as $part){
689           $part['FAIpartitionNr'] = $cnt;
690           $part['cn'] = $disk['cn'].$cnt;
691           $newSetup[$cnt] = $part;
692           $cnt ++;
693         } 
694         $cnt = 5;
695         foreach($logical as $part){
696           $part['FAIpartitionNr'] = $cnt;
697           $part['cn'] = $disk['cn'].$cnt;
698           $newSetup[$cnt] = $part;
699           $cnt ++;
700         }
701         $this->disks[$disk['cn']]['partitions'] = $newSetup; 
702       }
703     }
705  
706     # FAIpartitionNr have to be used as index for this->disks
707     #  else the next operation will fail. 
709  
710     /* Check if there are disks to be removed, edited or added.
711      * We compare the initial disk setup with the current setup and
712      *  and add a status flag, which will then be used to perform the 
713      *  correct action - add, edited, remove.
714      */
715     foreach($this->disks as $key => $disk){
717       // - A complete NEW disk
718       if(!isset($this->initial_disks[$disk['cn']])){
719         $this->disks[$key]['status'] = "new";
720         foreach($disk['partitions'] as $pkey => $part){
721           $this->disks[$disk['cn']]['partitions'][$pkey]['status'] = "new";
722         }
723       }else{
724     
725         // - Disk was "EDITED" 
726         $this->disks[$key]['status'] = "edited";
727         foreach($disk['partitions'] as $pkey => $part){
729           // - Check whether partition is "NEW" or "EDITED" 
730           if(!isset($this->initial_disks[$key]['partitions'][$pkey])){
731             $this->disks[$key]['partitions'][$pkey]['status'] = "new";
732           }else{
733             $this->disks[$key]['partitions'][$pkey]['status'] = "edited";
734           }
735         }
736       }
737     }
738      
739     /* Check which partitions havbe to be removed. 
740      * (They intially existed, but are now gone.)
741      */ 
742     foreach($this->initial_disks as $ikey => $idisk){
743       
744       // - Complete disk was REMOVED.
745       if(!isset($this->disks[$idisk['cn']])){
746         $this->disks[$idisk['cn']] = $idisk;
747         $this->disks[$idisk['cn']]['status'] = "delete";
748         foreach($idisk['partitions'] as $pkey=>$part){
749           $this->disks[$idisk['cn']]['partitions'][$pkey] = $part;
750           $this->disks[$idisk['cn']]['partitions'][$pkey]["status"] = "delete";
751         }
752       }else{
753         foreach($idisk['partitions'] as $pkey=>$part){
754           if(!isset($this->disks[$idisk['cn']]['partitions'][$pkey])){
755             $this->disks[$idisk['cn']]['partitions'][$pkey] = $part;
756             $this->disks[$idisk['cn']]['partitions'][$pkey]["status"] = "delete";
757           }
758         }
759       }
760     }
761   }
762   
764   function PrepareForCopyPaste($source)
765   {
766     plugin::PrepareForCopyPaste($source);
768     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
769      */
771     /* To be sure to copy disks and partitions correctly, just create a 
772      *  new PartitionTable object and use the 'disks' attribute 
773      *  from this it. This is much easier and less code.
774      */
775     $obj = new faiPartitionTable($this->config, $source['dn']);
776     $this->disks = $obj->disks;
777   }
780   /* Return plugin informations for acl handling */ 
781   static function plInfo()
782   {
783     return (array( 
784           "plShortName" => _("Partition table"),
785           "plDescription" => _("FAI partition table"),
786           "plSelfModify"  => FALSE,
787           "plDepends"     => array(),
788           "plPriority"    => 26,
789           "plSection"     => array("administration"),
790           "plCategory"    => array("fai"),
791           "plProvidedAcls" => array(
792             "cn"                => _("Name")."&nbsp;("._("Read only").")",
793             "description"       => _("Description"))
794           ));
795   }
798   /*! \brief  Used for copy & paste.
799     Returns a HTML input mask, which allows to change the cn of this entry.
800     @param  Array   Array containing current status && a HTML template.
801    */
802   function getCopyDialog()
803   {
804     $vars = array("cn");
805     $smarty = get_smarty();
806     $smarty->assign("cn", htmlentities($this->cn));
807     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
808     $ret = array();
809     $ret['string'] = $str;
810     $ret['status'] = "";
811     return($ret);
812   }
815   /*! \brief  Used for copy & paste.
816     Some entries must be renamed to avaoid duplicate entries.
817    */
818   function saveCopyDialog()
819   {
820     if(isset($_POST['cn'])){
821       $this->cn = get_post('cn');
822     }
823   }
825   /* Reload some attributes */
826   function get_object_attributes($object,$attributes)
827   {
828     $ldap = $this->config->get_ldap_link();
829     $ldap->cd($this->config->current['BASE']);
830     $ldap->cat($object['dn'],$attributes);
831     $tmp  = $ldap->fetch();
833     foreach($attributes as $attrs){
834       if(isset($tmp[$attrs][0])){
835         $var = $tmp[$attrs][0];
837         /* Check if we must decode some attributes */
838         if(in_array_ics($attrs,$this->sub64coded)){
839           $var = base64_decode($var);
840         }
842         /*  check if this is a binary entry */
843         if(in_array_ics($attrs,$this->subBinary)){
844           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
845         }
847         /* Fix slashes */
848         $var = addslashes($var);
849         $object[$attrs] = $var;
850       }
851     }
852     return($object);
853   }
857 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
858 ?>