Code

Moved fai
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
22 class faiManagement extends plugin
23 {
24         /* Definitions */
25         var $plHeadline                 = "FAI";
26         var $plDescription              = "Fully Automatic Installation - management";
28         /* CLI vars */
29         var $cli_summary                  = "Handling of FAI entries";
30         var $cli_description    = "This plugin represents a management tool\n
31                 which allows us to manage all needed attributes for fully automatic installations (FAI)";
32         var $cli_parameters             = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
34         /* Headpage attributes */
35   var $lock_type    = "";    // should be branch/freeze
36   var $lock_name    = "";
37   var $lock_dn      = "";  
39         /* attribute list for save action */
40         var $attributes         = array("lock_type","lock_name","lock_dn");     //      Attributes Managed by this plugin 
41         var $objectclasses= array();    //      ObjectClasses which the attributes are related to
42         var $dialog                             = array();      //      This object contains every dialog we have currently opened
44         var $objects                    = array();      //      This array contains all available objects shown in divlist
45         var $is_dialog          = false;
47   var $dispNewBranch= false;
48   var $dispNewFreeze= false;
50   var $DivListFai;
51   var $start_pasting_copied_objects = FALSE;
52   var $CopyPasteHandler = FALSE;
53   var $base = "";
55         /* construction/reconstruction 
56          */
57         function faiManagement (&$config, $ui)
58         {
59                 /* Set defaults */
60                 $this->dn                       = "";
61                 $this->config   = $config;
62                 $this->ui                       = $ui;  
63     
64     /* Creat dialog object */
65     $this->DivListFai = new divListFai($this->config,$this);
67     /* Copy & Paste handler */
68     if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
69       $this->CopyPasteHandler= new CopyPasteHandler($this->config);
70     }
71         }
73         function execute()
74         {
75     /* Call parent execute */
76     plugin::execute();
78     /* Initialise vars and smarty */
79                 $smarty         = get_smarty();
80                 $smarty->assign("BranchName","");
81     
82                 $display        = "";
83     $s_action   = "";
84                 $s_entry        = "";
85     
86     /* If an entry was locked, these vars will be stored in a session to allow direct edit */
87     session::set('LOCK_VARS_TO_USE',array("/^edit_entry$/","/^id$/","/^entry_edit_/","/^entry_delete_/","/^item_selected/","/^remove_multiple_fai_objects/"));
90     /****************
91       Handle posts 
92      ****************/
94                 /* Check ImageButton posts
95                  * Create new tab ich new_xx is posted
96                  */
97     $posts = array( "/remove_branch/"=>"remove_branch",    "/branch_branch/"=>"branch_branch",
98                     "/freeze_branch/"=>"freeze_branch",    "/create_partition/i"=>"new_partition",
99                     "/create_script/i"=>"new_script",      "/create_hook/i"=>"new_hook",
100                     "/create_variable/i"=>"new_variable",  "/create_template/i"=>"new_template",
101                     "/create_package/i"=>"new_package",    "/create_profile/i"=>"new_profile",
102                     "/edit_continue/"=>"select_class_name_finished",
103                     "/^multiple_copy_fai/" => "copy_multiple", 
104                     "/^multiple_cut_fai/" => "cut_multiple", 
105                     "/^copy/" => "copy",
106                     "/^cut/" => "cut",
107                     "/^remove_multiple_fai_objects/" => "del_multiple");
109                 foreach($_POST as $name => $value){
110       foreach($posts as $reg => $act ){
111         if(preg_match($reg,$name)){
112           $s_action = $act;
113           $s_entry = ltrim(preg_replace($reg,"",$name),"_");
114           $s_entry = preg_replace("/_.*$/","",$s_entry);
115         }
116       }
117                         if(preg_match("/^entry_edit_.*/",$name)){
118                                 $s_entry = preg_replace("/^entry_edit_/","",$name);
119                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
120                                 $s_action = "edit";
121                         }elseif(preg_match("/^entry_delete_.*/",$name)){
122                                 $s_entry = preg_replace("/^entry_delete_/","",$name);
123                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
124         $s_action = "delete";
125       }
126     }
128                 if(isset($_GET['edit_entry'])){
129                         $s_entry = $_GET['edit_entry'];
130                         $s_action = "edit";
131                 }
133     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch"){
134       $s_action = "freeze_branch";
135     }
136     if(isset($_GET['act']) && $_GET['act'] == "branch_branch"){
137       $s_action = "branch_branch";
138     }
139     if(isset($_GET['act']) && $_GET['act'] == "remove_branch"){
140       $s_action = "remove_branch";
141     }
142     
143     if((isset($_POST['CancelBranchName'])) || (isset($_POST['CloseIFrame']))){
144       $this->dispNewBranch = false;
145       $this->dispNewFreeze = false;
146     }
149     $type_acl_mapping = array(
150         "FAIpartitionTable"  => "faiPartitionTable", 
151         "FAIpackageList"     => "faiPackage",
152         "FAIscript"          => "faiScript",
153         "FAIvariable"        => "faiVariable",
154         "FAIhook"            => "faiHook",
155         "FAIprofile"         => "faiProfile",
156         "FAItemplate"        => "faiTemplate");
159     /* handle C&P from layers menu */
160     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
161       $s_action = "copy_multiple";
162     }
163     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
164       $s_action = "cut_multiple";
165     }
166     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
167       $s_action = "editPaste";
168     }
170     /* Create options */
171     if(isset($_POST['menu_action']) && preg_match("/^Create_/",$_POST['menu_action'])){
172       $s_action = "new_".preg_replace("/^Create_/","",$_POST['menu_action']);;
173       $s_entry  = preg_replace("/^Create_/","",$_POST['menu_action']);
174     }
176     /* handle remove from layers menu */
177     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
178       $s_action = "del_multiple";
179     }
181     /********************
182       Copy & Paste
183      ********************/
185     /* Display the copy & paste dialog, if it is currently open */
186     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
187     if($ret){
188       return($ret);
189     }
192     /****************
193       Delete confirme dialog 
194      ****************/
196                 if ($s_action=="delete"){
198       /* Get 'dn' from posted termlinst */
199       $this->dn= $this->objects[$s_entry]['dn'];
201                         /* Load permissions for selected 'dn' and check if
202                            we're allowed to remove this 'dn' */
203       $acl  = $this->ui->get_permissions($this->dn,"fai/".$type_acl_mapping[$this->objects[$s_entry]['type']]);
204       if(preg_match("/d/",$acl)){
206                                 /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
207                                 if (($user= get_lock($this->dn)) != ""){
208                                         return(gen_locked_message ($user, $this->dn));
209                                 }
211                                 /* Lock the current entry, so nobody will edit it during deletion */
212                                 add_lock ($this->dn, $this->ui->dn);
213                                 $smarty->assign("warning", sprintf(_("You're about to delete all information about the FAI class at '%s'."), @LDAP::fix($this->dn)));
214         $smarty->assign("multiple", false);
215                                 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
216                         } else {
218                                 /* Obviously the user isn't allowed to delete. Show message and
219                                    clean session. */
220                                 print_red (_("You are not allowed to delete this component!"));
221                         }
222                 }
225     /********************
226       Delete MULTIPLE entries requested, display confirm dialog
227      ********************/
229     if ($s_action=="del_multiple"){
230       $this->dns = array();
231       $ids = $this->list_get_selected_items();
233       if(count($ids)){
235         foreach($ids as $id){
236           $dn = $this->objects[$id]['dn'];
237           if (($user= get_lock($dn)) != ""){
238             return(gen_locked_message ($user, $dn));
239           }
240           $this->dns[$id] = $dn;
241         }
243         $dns_names = "<br><pre>";
244         foreach($this->dns as $dn){
245           add_lock ($dn, $this->ui->dn);
246           $dns_names .= $dn."\n";
247         }
248         $dns_names .="</pre>";
250         /* Lock the current entry, so nobody will edit it during deletion */
251         if (count($this->dns) == 1){
252           $smarty->assign("warning",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
253         } else {
254           $smarty->assign("warning",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
255         }
256         $smarty->assign("multiple", true);
257         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
258       }
259     }
262     /********************
263       Delete MULTIPLE entries confirmed
264      ********************/
266     /* Confirmation for deletion has been passed. Users should be deleted. */
267     if (isset($_POST['delete_multiple_fai_object_confirm'])){
269       /* Find out more about the object type */
270       $ldap   = $this->config->get_ldap_link();
272       /* Remove user by user and check acls before removeing them */
273       foreach($this->dns as $key => $dn){
275         $ldap->cat($dn, array('objectClass'));
276         $attrs  = $ldap->fetch();
277         $type   = $this->get_type($attrs);
279         $acl  = $this->ui->get_permissions($dn,"fai/".$type[1]);
280         if(preg_match("/d/",$acl)){
282           $this->dialog = new $type[0]($this->config, $this->config->data['TABS'][$type[2]], $dn,"fai");
283           $this->dialog->set_acl_base($dn);
284           $this->dialog->by_object[$type[1]]->remove_from_parent ();
285           unset ($this->dialog);
286           $this->dialog= FALSE;
287           $to_del = FAI::clean_up_releases($dn);
288           FAI::save_release_changes_now();
290           foreach($to_del as $dn){
291             $ldap->rmdir_recursive($dn);
292           }
294         } else {
296           /* Normally this shouldn't be reached, send some extra
297              logs to notify the administrator */
298           print_red (_("You are not allowed to delete this component!"));
299           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
300         }
302         /* Remove lock file after successfull deletion */
303         del_lock ($dn);
304         unset($this->dns[$key]);
305       }
306     }
309     /********************
310       Delete MULTIPLE entries Canceled
311      ********************/
313     /* Remove lock */
314     if(isset($_POST['delete_multiple_fai_object_cancel'])){
315       foreach($this->dns as $key => $dn){
316         del_lock ($dn);
317         unset($this->dns[$key]);
318       }
319     }
322     /****************
323       Delete aborted  
324      ****************/
326                 /* Delete canceled? */
327                 if (isset($_POST['delete_cancel'])){
328                         del_lock ($this->dn);
329                 }
332     /****************
333       Delete confirmed 
334      ****************/
336                 /* Deltetion was confirmed, so delete this entry
337      */
338     if (isset($_POST['delete_terminal_confirm'])){
340       /* Some nice guy may send this as POST, so we've to check
341          for the permissions again. */
343       /* Find out more about the object type */
344       $ldap       = $this->config->get_ldap_link();
345       $ldap->cat($this->dn, array('objectClass'));
346       if($ldap->count()){
347         $attrs  = $ldap->fetch();
348         $type     = $this->get_type($attrs);                    
350         $acl  = $this->ui->get_permissions($this->dn,"fai/".$type[1]);
351         if(preg_match("/d/",$acl)){
353           $this->dialog = new $type[0]($this->config,   $this->config->data['TABS'][$type[2]], $this->dn,"fai");
354           $this->dialog->set_acl_base($this->dn);
355           $this->dialog->by_object[$type[1]]->remove_from_parent ();
356           unset ($this->dialog);
357           $this->dialog= FALSE;
358           $to_del = FAI::clean_up_releases($this->dn);
359           FAI::save_release_changes_now();
361           foreach($to_del as $dn){
362             $ldap->rmdir_recursive($dn);
363           }
365         } else {
367           /* Normally this shouldn't be reached, send some extra
368              logs to notify the administrator */
369           print_red (_("You are not allowed to delete this component!"));
370           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
371         }
373       }else{
374         print_red(sprintf(_("Object could not be deleted '%s', object does not exist."),$this->dn));
375       }
376       /* Remove lock file after successfull deletion */
377       del_lock ($this->dn);
378     }
381     /****************
382       Edit entry 
383      ****************/
385                 if(($s_action == "edit") && (!isset($this->dialog->config))){
386                         $entry    = $this->objects[$s_entry];
387                         $a_setup  = $this->get_type($entry);
388                         $this->dn = $entry['dn'];
390                         /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
391                         if (($user= get_lock($this->dn)) != ""){
392                                 return(gen_locked_message ($user, $this->dn));
393                         }
394                         add_lock ($this->dn, $this->ui->dn);
396                         $this->dialog     = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
397       $this->dialog->set_acl_base($this->dn);
398                         $this->is_dialog  = true;
400       if($entry['FAIstate'] == "freeze"){
401         #$this->dialog->set_acl(array("*none*"))  ;    
402       }
403                         session::set('objectinfo',$this->dn);
404                 }
407     /*  Branch handling 
408         09.01.2006
409     */
411     /****************
412       Remove branch
413      ****************/
415     /* Remove branch 
416      */
417     if($s_action == "remove_branch"){
418       $base= $this->DivListFai->selectedBranch;
420       /* Check if we have a post remove method configured
421        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
422        */
423       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
424         /* Load permissions for selected 'dn' and check if
425            we're allowed to remove this 'dn' */
426         if($this->acl_is_removeable()){
428           $smarty->assign("info", sprintf(_("You're about to delete a fai branch / freeze  '%s'."), $this->DivListFai->selectedBranch));
429           return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
430         } else {
431           print_red (_("You are not allowed to delete this release!"));
432         }
433       }
434     }
436     
437     /****************
438       Remove branch confirmed
439      ****************/
441     if(isset($_POST['delete_branch_confirm'])){
443       /* Check if we have a post remove method configured
444        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
445        */
446       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
448         $bb =  $this->DivListFai->selectedBranch;
449         if(!isset($ldap)){
450           $ldap = $this->config->get_ldap_link();
451         }
453         $br = $this->getBranches();
455         if(isset($br[$bb]) && $this->acl_is_removeable()){
456           $name = $br[$bb];
457           $ldap->cd($bb);
458           $ldap->recursive_remove();
459           $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('applicationou'), $bb));
460           $ldap->recursive_remove();
461           $this->DivListFai->selectedBranch = "main";
463           /* Post remove */
464           $this->lock_name   = $name;
465           $this->lock_dn     = $bb;
466           $this->postremove();
468           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
469         }
470       }
471     }
474     /****************
475       Create a new branch "insert Name"
476      ****************/
478     if((isset($_POST['UseBranchName']))&&(($this->dispNewBranch)||($this->dispNewFreeze))){
479       session::set('LASTPOST',$_POST);
481       if($this->dispNewBranch){
482         $type = "branch";
483       }else{
484         $type = "freeze";
485       }
487       /* Check branch name */
488       $name = $_POST['BranchName'];
489       $is_ok = true;
490       $smarty->assign("BranchName",$name);
491       $base= get_ou('faiou').$this->DivListFai->selectedBase;
493       /* Check used characters */
494       if(preg_match("/[^0-9a-z \ö\ä\ü\.-_:,]/i",$name)){
495         if($type == "branch"){
496           print_red(_("Specified branch name is invalid."));
497         }else{
498           print_red(_("Specified freeze name is invalid."));
499         }
500         $is_ok = false;
501       }
503       /* Check if this name is already in use */
504       if(!$this->CheckNewBranchName($_POST['BranchName'],$this->DivListFai->selectedBranch)){
505         print_red(_("This name is already in use."));
506         $is_ok = false;
507       }
509       if($is_ok){
511         if(session::is_set('LASTPOST')){
512           $LASTPOST = session::get('LASTPOST');
513         }else{
514           $LASTPOST = array();
515         }
516         $LASTPOST['base'] = $base;
517         $LASTPOST['type'] = $type;
518         session::set('LASTPOST',$LASTPOST);
519         $smarty->assign("iframe", true);
520         $smarty->assign("plugID", $_GET['plug']);
521         $display        = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
522         return($display);
523       }
524     }
527     /****************
528       Create a new branch 
529      ****************/
531     if(isset($_GET['PerformBranch'])){
532     
533       if(!$this->acl_is_createable()){
534         print_red(_("You are not allowed to create a new branch."));
535       }else{
537         /* Create it know */
538         $this->dispNewBranch = false;
539         $this->dispNewFreeze = false;
541         $LASTPOST = session::get('LASTPOST');
542         $base = $LASTPOST['base'];
543         $_POST  = session::get('LASTPOST');      
544         $name = $_POST['BranchName'];
546         $type = $LASTPOST['type'];
547         $ldap = $this->config->get_ldap_link();
549         $baseToUse = $base;
550         if($this->DivListFai->selectedBranch != "main" ){
551           $baseToUse = $this->DivListFai->selectedBranch;
552         }
554         /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
556         $CurrentReleases  = $this->getBranches();
557         $NewReleaseName   = $name;
558         if(isset($CurrentReleases[$this->DivListFai->selectedBranch])) {
559           if($this->DivListFai->selectedBranch != "main"){
560             $NewReleaseName = $CurrentReleases[$this->DivListFai->selectedBranch]."/".$name;
561             $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName); 
562           }else{
563             $NewReleaseName   = $name;
564           }
565         }
567         $appsrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),$baseToUse); 
568         $appdst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),"ou=".$name.",".$baseToUse) ; 
570         $mimesrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),$baseToUse); 
571         $mimedst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),"ou=".$name.",".$baseToUse) ; 
573         /* Check if source depeartments exist */
574         foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
575           $ldap->cd($this->config->current['BASE']);
576           $ldap->cat($dep);
577           if(!$ldap->count()){
578             $ldap->create_missing_trees($dep);
579           }
580         }
582         /* Print header to have styles included */
583         echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
584           <html>
585           <head>
586           <title></title>
587           <style type="text/css">@import url("themes/default/style.css");</style>
588           <script language="javascript" src="include/focus.js" type="text/javascript"></script>
589           </head>
590           <body style="background: none;margin:3px;color:black">
591           ';
593         new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
595         /* Duplicate applications 
596          */
597         $ldap->cat($appsrc,array("dn")) ;
598         if($ldap->count()){
599           $ldap->cd ($appdst);
600           $ldap->recursive_remove();
601           FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
602         }
604         /* Duplicate mime types 
605          */
606         $ldap->cat($mimesrc,array("dn")) ;
607         if($ldap->count()){
608           $ldap->cd ($mimedst);
609           $ldap->recursive_remove();
610           FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
611         }
613         $attr = array();
614         $attr['objectClass'] = array("organizationalUnit","FAIbranch");
615         $attr['ou']       = $name;
616         $attr['FAIstate'] = $type;
617         $ldap->cd($this->config->current['BASE']);
618         $ldap->cd("ou=".$name.",".$baseToUse);
619         $ldap->cat("ou=".$name.",".$baseToUse);
620         if($ldap->count()){
621           $ldap->modify($attr);
622         }else{
623           $ldap->add($attr);
624         }
626         /* Duplicate fai objects 
627          */
628         //      $ldap->cd ("ou=".$name.",".$baseToUse);
629         //      $ldap->recursive_remove();
630         //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
632         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
633           <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
634           </form></div>";
636         echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
638         /* Print footer to have valid html */
639         echo "</body></html>";
641         $this->dispNewFreeze = false; 
643         /* Postcreate */ 
645         /* Assign possible attributes */
646         $this->lock_type  = $type; 
647         $this->lock_name  = $name; 
648         $this->lock_dn    = $baseToUse;
649         $this->postcreate();
650         exit();
651       }
652     }
654     /****************
655       Display dialog to enter new Branch name
656      ****************/
658     /* Check if we have a post create method configured
659      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
660      */
661     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
662       if(($s_action == "branch_branch")||($this->dispNewBranch)){
663         if(!$this->acl_is_createable()){
664           print_red(_("You are not allowed to create a new branch."));
665         }else{
666           $this->dispNewBranch=true;
667           $smarty->assign("iframe",false);
668           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
669           return($display);
670         }
671       } 
672     }
674  
675     /****************
676       Display dialog to enter new Freeze name
677      ****************/
679     /* Check if we have a post create method configured
680      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
681      */
682     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
683       if(($s_action == "freeze_branch")||($this->dispNewFreeze)){
684         if(!$this->acl_is_createable()){
685           print_red(_("You are not allowed to create a new branch."));
686         }else{
687           $this->dispNewFreeze = true;
688           $smarty->assign("iframe",false);
689           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
690           return($display);
691         }
692       }
693     }
696     /****************
697       Create a new object 
698      ****************/
700     $types = array( "new_partition"     =>  "FAIpartitionTable",
701                     "new_script"        =>  "FAIscript",
702                     "new_hook"          =>  "FAIhook",
703                     "new_variable"      =>  "FAIvariable",
704                     "new_template"      =>  "FAItemplate",
705                     "new_package"       =>  "FAIpackageList");
707     if(isset($types[$s_action])){
708       $acl = $this->ui->get_permissions($this->DivListFai->selectedBase,"fai/".$type_acl_mapping[$types[$s_action]]);
709       if(preg_match("/c/",$acl)){
710         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,$types[$s_action]);
711       }else{
712         print_red(sprintf(_("You are not allowed to create a new '%s' object."),$types[$s_action]));
713       }
714     }
716     /* New Profile */
717     if($s_action == "new_profile"){
718       $this->dn = "new" ;
720       $acl = $this->ui->get_permissions($this->DivListFai->selectedBase,"fai/faiProfile");
721       if(preg_match("/c/",$acl)){
722         $a_setup= $this->get_type(array("objectClass"=>array("FAIprofile")));
723         $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
724         $this->dialog->set_acl_base($this->base);
726         $this->is_dialog = false;
727       }else{
728         print_red(sprintf(_("You are not allowed to create a new '%s' object."),"FAIprofile"));
729       }
730     }
733     /****************
734       Get from ask class name dialog 
735      ****************/
737     if($s_action == "select_class_name_finished"){
738       $this->dialog->save_object();
739       if(count($this->dialog->check())!=0){
740         foreach($this->dialog->check() as $msg){
741           print_red($msg);
742         }               
743       }elseif(isset($this->dialog->objectClass)){
744         $this->dn = "new" ;
745         $a_setup= $this->get_type(array("objectClass"=>array($this->dialog->objectClass)));
746         $name = $this->dialog->save();
748         if(class_exists($a_setup[0])){
749           $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
750           $this->dialog->set_acl_base($this->base);
751           $this->dialog->by_object[$a_setup[1]]->cn = $name;
752           $this->is_dialog = true;
753         }
754       }         
755     }   
758     /****************
759      Cancel dialogs 
760      ****************/
762                 if(isset($_POST['edit_cancel'])){
763                         unset($this->dialog);
764                         $this->dialog=FALSE;
765                         $this->is_dialog = false;
766                         session::un_set('objectinfo');
767                         del_lock ($this->dn);
768                 }
771     /****************
772       Save sub dialogs 
773      ****************/
775                 /* This check if the given tab could be saved 
776                  * If it was possible to save it, remove dialog object. 
777                  * If it wasn't possible, show errors and keep dialog.
778                  */
779                 if((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->dialog->config))){
780                         $this->dialog->save_object();
781       $msgs= $this->dialog->check();
782                         if(count($msgs)!=0){
783                                 foreach($msgs as $msg){
784                                         print_red($msg);
785                                 }
786                         }else{
787                                 $this->dialog->save();
788         FAI::save_release_changes_now();
789         if (!isset($_POST['edit_apply'])){
790           del_lock ($this->dn);
791           unset($this->dialog);
792           $this->dialog=FALSE;
793           $this->is_dialog=false;
794           session::un_set('objectinfo');
795         }
796                         }
797                 }
800     /****************
801       Display currently open dialog 
802      ****************/
804                 /* If dialog is set, but $this->is_dialog==false, then 
805                  *  only the "abort" button is shown, this are dialogs that must not be saved.  
806                  * If is_dialog == true, we are currently editing tab objects.
807                  *  Here we need both, save and cancel
808                  */ 
810                 if(is_object($this->dialog)){
811                         $display .= $this->dialog->execute();
812                         /* Don't show buttons if tab dialog requests this */
814       if(isset($this->dialog->current)){
816         $obj = $this->dialog->by_object[$this->dialog->current];
818         if((isset($obj->is_dialog) && (!$obj->is_dialog)) || (isset($obj->dialog) && (!$obj->dialog))){
820           $display.= "<p style=\"text-align:right\">\n";
821           $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
822           $display.= "&nbsp;\n";
823           if ($this->dn != "new"){
824             $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
825             $display.= "&nbsp;\n";
826           }
827           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
828           $display.= "</p>";
829         }elseif(!isset($this->dialog->current)){
830           $display.= "<p style=\"text-align:right\">\n";
831           $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
832           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
833           $display.= "</p>";
834         }
835       }else{
836         $display.= "<p style=\"text-align:right\">\n";
837         $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
838         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
839         $display.= "</p>";
841       }
842       return($display);
843                 }
844                 
846     /****************
847       Dialog display
848      ****************/
850     /* Check if there is a snapshot dialog open */
851     $base = $this->DivListFai->selectedBase;
852     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
853       return($str);
854     }
856     /* Display dialog with system list */
857     $this->DivListFai->parent = $this;
858     $this->DivListFai->execute();
859     $this->DivListFai->AddDepartments($this->DivListFai->selectedBase,4,1);
860     $this->reload();
861     $this->DivListFai->setEntries($this->objects);
862     return($this->DivListFai->Draw());
863         }
866   /* Return departments, that will be included within snapshot detection */
867   function get_used_snapshot_bases()
868   {
869     $tmp = array();
870     $types = array("faipartitionou","faiscriptou","faitemplateou","faihookou","faiprofileou","faivariableou","faipackageou");
871     foreach($types as $type){
872       if($this->DivListFai->selectedBranch == "main"){
873         $tmp[] = get_ou($type).get_ou('faiou').$this->DivListFai->selectedBase;
874       }else{
875         $tmp[] = get_ou($type).$this->DivListFai->selectedBranch;
876       }
877     }
878     return($tmp);
879   }
882   /* Get available branches for current base */
883   function getBranches($base = false,$prefix = "")
884   {
885     $ret = array("/"=>"main");
886     $ldap = $this->config->get_ldap_link();
887     if(!$base){
888       $base = session::get('CurrentMainBase');
889     }
890     $tmp = FAI::get_all_releases_from_base($base,true);
891     foreach($tmp as $dn => $name){
892       $ret[$name]=$dn;
893     }
894     ksort($ret);
895     $ret = array_flip($ret);
897     return ($ret);
898   }
899   
901   function list_get_selected_items()
902   {
903     $ids = array();
904     foreach($_POST as $name => $value){
905       if(preg_match("/^item_selected_[0-9]*$/",$name)){
906         $id   = preg_replace("/^item_selected_/","",$name);
907         $ids[$id] = $id;
908       }
909     }
910     return($ids);
911   }
914   /* reload list of objects */
915   function reload()
916   {
917     /* Variable initialisation */
918     $str            = "";
919     $Regex          = $this->DivListFai->Regex;
920     $this->objects  = array();
922     /* Get base */
923     $base = get_ou('faiou').$this->DivListFai->selectedBase;
924     if($this->DivListFai->selectedBranch != "main"){
925       $br = $this->getBranches();
926       if(isset($br[$this->DivListFai->selectedBranch])){
927         $base = $this->DivListFai->selectedBranch;
928       }else{
929         $base = get_ou('faiou').$this->DivListFai->selectedBase;
930       }
931     }
932     $this->base = $base;
933     $this->set_acl_base($this->base);
935     $this->lock_type = FAI::get_release_tag(FAI::get_release_dn($base));
937     /* Create a new list of FAI object 
938      * Generate List of Partitions,Hooks,Scripts,Templates,Profiles ... 
939      */
940     $ObjectTypes = array(
941         "FAIpartitionTable"  => array("OU"=> get_ou('faipartitionou') , "CHKBOX"=>"ShowPartitions"  ,"ACL" => "faiPartitionTable"),
942         "FAIpackageList"     => array("OU"=> get_ou('faipackageou')   , "CHKBOX"=>"ShowPackages"    ,"ACL" => "faiPackage"),
943         "FAIscript"          => array("OU"=> get_ou('faiscriptou')    , "CHKBOX"=>"ShowScripts"     ,"ACL" => "faiScript"),
944         "FAIvariable"        => array("OU"=> get_ou('faivariableou')  , "CHKBOX"=>"ShowVariables"   ,"ACL" => "faiVariable"),
945         "FAIhook"            => array("OU"=> get_ou('faihookou')      , "CHKBOX"=>"ShowHooks"       ,"ACL" => "faiHook"),
946         "FAIprofile"         => array("OU"=> get_ou('faiprofileou')   , "CHKBOX"=>"ShowProfiles"    ,"ACL" => "faiProfile"),
947         "FAItemplate"        => array("OU"=> get_ou('faitemplateou')  , "CHKBOX"=>"ShowTemplates"   ,"ACL" => "faiTemplate"));
949     $filter = "";
950     foreach($ObjectTypes as $key => $data){
951       if($this->DivListFai->$data['CHKBOX']){
952         $filter.= "(objectClass=".$key.")";
953       }
954     }
955     $filter = "(&(|".$filter.")(cn=$Regex))";
956     
957     /* Get resolved release dependencies */
958     $tmp = FAI::get_all_objects_for_given_base($base,$filter);
960     /* Ge listed ldap objects */
961     $ldap = $this->config->get_ldap_link();
962     $ldap->cd($this->config->current['BASE']);
963     foreach($tmp as $entry){
965       /* Get some more informations about the object */ 
966       $ldap->cat($entry['dn'], array("cn","description","objectClass","FAIclass","FAIstate","objectClass"));
967       $object  = $ldap->fetch();
969       /* Walk through possible types */
970       foreach($ObjectTypes as $type => $rest){  
972         $acl = $this->ui->get_permissions($object['dn'],"fai/".$rest ['ACL']);
974         if(in_array($type,$object['objectClass']) && preg_match("/r/",$acl)){
976           /* Prepare object */
977           unset($object['objectClass']['count']);
978           if(!isset($object['description'][0])){
979             $object['description'][0]="";
980           }
982           /* Clean up object informations */
983           $obj                  = array();
984           $obj['cn']                          = $object['cn'][0];
985           $obj['dn']                          = $object['dn'];
986           $obj['acl']                       = $acl;
987           $obj['description']   = $object['description'][0];
988           $obj['objectClass']   = $object['objectClass'];
990           /* Append type to this string, to be able to check if the selected 
991            * entry is of type 'freeze' or 'branch'
992            */
993           if(!isset($object['FAIstate'])){
994             $obj['FAIstate'] = $this->lock_type;
995           }else{
996             $obj['FAIstate'] = $object['FAIstate'][0]; 
997           }
999           $this->objects[strtolower($obj['cn']).$obj['cn'].$type] = $obj;
1000           $this->objects[strtolower($obj['cn']).$obj['cn'].$type]['type']=$type;
1001         }
1002                         }
1003                 }
1005                 ksort($this->objects);
1006                 reset ($this->objects);
1007         
1008                 /* use numeric index, thats a bit more secure */        
1009                 $tmp0 = array();
1010                 foreach($this->objects as $obj){
1011                         $tmp0[]= $obj;
1012                 }
1013                 $this->objects = array();
1014                 $this->objects = $tmp0;
1015         }
1017         function remove_lock()
1018         {
1019                 if (isset($this->dn)){
1020                         del_lock ($this->dn);
1021                 }
1022         }
1024         function get_type($array){
1025                 if(in_array("FAIpartitionTable",$array['objectClass'])){
1026                         return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
1027                 }
1028                 if(in_array("FAIscript",$array['objectClass'])){
1029                         return(array("tabsScript","faiScript","FAISCRIPTTABS"));
1030                 }
1031                 if(in_array("FAItemplate",$array['objectClass'])){
1032                         return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
1033                 }
1034                 if(in_array("FAIhook",$array['objectClass'])){
1035                         return(array("tabsHook","faiHook","FAIHOOKTABS"));
1036                 }
1037                 if(in_array("FAIvariable",$array['objectClass'])){
1038                         return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
1039                 }
1040                 if(in_array("FAIprofile",$array['objectClass'])){
1041                         return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
1042                 }
1043                 
1044                 if(in_array("FAIpackageList",$array['objectClass'])){
1045                         return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
1046                 }
1047         }
1049   function CheckNewBranchName($name,$base)
1050   {
1051     $f = $this->DivListFai->selectedBranch;
1052     if($name == ""){
1053       return(false);
1054     }elseif(in_array($name,$this->getBranches($f))) {
1055       return(false);
1056     }elseif(tests::is_department_name_reserved($name,$base)){
1057       return(false);
1058     }
1059     return(true);
1060   }
1062   function save_object()
1063   {
1064     $this->DivListFai->save_object();
1065   }
1068   function copyPasteHandling_from_queue($s_action,$s_entry)
1069   {
1070     /* Check if Copy & Paste is disabled */
1071     if(!is_object($this->CopyPasteHandler)){
1072       return("");
1073     }
1075     $base = $this->DivListFai->selectedBranch;
1076     if($base == "main"){
1077       $base = $this->DivListFai->selectedBase;
1078     }
1080     /* Add a single entry to queue */
1081     if($s_action == "cut" || $s_action == "copy"){
1083       /* Cleanup object queue */
1084       $this->CopyPasteHandler->cleanup_queue();
1085       $entry    = $this->objects[$s_entry];
1086       $a_setup  = $this->get_type($entry);
1087       $dn = $entry['dn'];
1088       $this->CopyPasteHandler->add_to_queue($dn,$s_action,$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1089     }
1091     /* Add entries to queue */
1092     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1094       /* Cleanup object queue */
1095       $this->CopyPasteHandler->cleanup_queue();
1097       /* Add new entries to CP queue */
1098       foreach($this->list_get_selected_items() as $id){
1100         /* Cleanup object queue */
1101         $entry    = $this->objects[$id];
1102         $a_setup  = $this->get_type($entry);
1103         $dn = $entry['dn'];
1106         if($s_action == "copy_multiple"){
1107           $this->CopyPasteHandler->add_to_queue($dn,"copy",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1108         }
1109         if($s_action == "cut_multiple"){
1110           $this->CopyPasteHandler->add_to_queue($dn,"cut",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1111         }
1112       }
1113     }
1115     /* Start pasting entries */
1116     if($s_action == "editPaste"){
1117       $this->start_pasting_copied_objects = TRUE;
1118     }
1120     /* Return C&P dialog */
1121     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1123       /* Load entry from queue and set base */
1124       $this->CopyPasteHandler->load_entry_from_queue();
1125       $this->CopyPasteHandler->SetVar("base",$base);
1127       /* Get dialog */
1128       $data = $this->CopyPasteHandler->execute();
1130       FAI::save_release_changes_now();
1132       /* Return dialog data */
1133       if(!empty($data)){
1134         return($data);
1135       }
1136     }
1138     /* Automatically disable status for pasting */
1139     if(!$this->CopyPasteHandler->entries_queued()){
1140       $this->start_pasting_copied_objects = FALSE;
1141     }
1142     return("");
1143   }
1146   /* Return plugin informations for acl handling */ 
1147   static function plInfo()
1148   {
1149     return (array( 
1150           "plShortName"   => _("FAI releases"),
1151           "plDescription" => _("FAI release management"),
1152           "plSelfModify"  => FALSE,
1153           "plDepends"     => array(),
1154           "plPriority"    => 0,
1155           "plSection"     => array("administration"),           
1156           "plCategory"    => array("fai"=> array("description" => _("FAI"),
1157                                                  "objectClass" => "FAIclass")),
1158           "plProvidedAcls"=> array()));
1159   }
1161 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1162 ?>