Code

Added more loggin to fai remove. Just for testing
[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     = "Software deployment";
26   var $plDescription    = "Manage software packages and deployment reciepes";
28         /* Headpage attributes */
29   var $lock_type    = "";    // should be branch/freeze
30   var $lock_name    = "";
31   var $lock_dn      = "";  
33         /* attribute list for save action */
34         var $attributes         = array("lock_type","lock_name","lock_dn");     //      Attributes Managed by this plugin 
35         var $objectclasses= array();    //      ObjectClasses which the attributes are related to
36         var $dialog                             = array();      //      This object contains every dialog we have currently opened
38         var $objects                    = array();      //      This array contains all available objects shown in divlist
39         var $is_dialog          = false;
41   var $dispNewBranch= false;
42   var $dispNewFreeze= false;
44   var $DivListFai;
45   var $start_pasting_copied_objects = FALSE;
46   var $CopyPasteHandler = FALSE;
48   /* Allow inserting of new elements if freezed releases 
49   */
50   var $allow_freeze_object_attach = FALSE;
52   var $no_save;
53   var $fai_base     ="";
54   var $fai_release  ="";
56         /* construction/reconstruction 
57          */
58         function faiManagement (&$config, $ui)
59         {
60                 /* Set defaults */
61                 $this->dn                       = "";
62                 $this->config   = $config;
63                 $this->ui                       = $ui;  
64     
65     /* Creat dialog object */
66     $this->DivListFai = new divListFai($this->config,$this);
68     /* Copy & Paste handler */
69     if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
70       $this->CopyPasteHandler= new CopyPasteHandler($this->config);
71     }
73     /* Set default release */
74     $this->fai_base = get_ou("faiou").$this->config->current['BASE'];
75     if(!session::is_set("fai_filter")){
76       session::set("fai_filter",array("fai_release" => $this->fai_base));
77     }
79     $fai_filter = session::get("fai_filter");
80     $this->fai_release = $fai_filter['fai_release'];
81         }
83         function execute()
84         {
85     /* Call parent execute */
86     plugin::execute();
88     /* Initialise vars and smarty */
89                 $smarty         = get_smarty();
90                 $smarty->assign("BranchName","");
91     
92                 $display        = "";
93     $s_action   = "";
94                 $s_entry        = "";
95     $no_save = FALSE;   // hide Apply / Save buttons
96     
97     /* If an entry was locked, these vars will be stored in a session to allow direct edit */
98     session::set('LOCK_VARS_TO_USE',array("/^edit_freeze_entry$/","/^edit_entry$/","/^id$/","/^entry_edit_/","/^entry_delete_/","/^item_selected/","/^remove_multiple_fai_objects/","/^menu_action/"));
101     /****************
102       Handle posts 
103      ****************/
105                 /* Check ImageButton posts
106                  * Create new tab ich new_xx is posted
107                  */
108     $posts = array( "/remove_branch/"=>"remove_branch",    "/branch_branch/"=>"branch_branch",
109                     "/freeze_branch/"=>"freeze_branch",    "/create_partition/i"=>"new_partition",
110                     "/create_script/i"=>"new_script",      "/create_hook/i"=>"new_hook",
111                     "/create_variable/i"=>"new_variable",  "/create_template/i"=>"new_template",
112                     "/create_package/i"=>"new_package",    "/create_profile/i"=>"new_profile",
113                     "/edit_continue/"=>"select_class_name_finished",
114                     "/^multiple_copy_fai/" => "copy_multiple", 
115                     "/^multiple_cut_fai/" => "cut_multiple", 
116                     "/^copy/" => "copy",
117                     "/^remove_multiple_fai_objects/" => "del_multiple");
119                 foreach($_POST as $name => $value){
120       foreach($posts as $reg => $act ){
121         if(preg_match($reg,$name)){
122           $s_action = $act;
123           $s_entry = ltrim(preg_replace($reg,"",$name),"_");
124           $s_entry = preg_replace("/_.*$/","",$s_entry);
125         }
126       }
127                         if(preg_match("/^entry_edit_.*/",$name)){
128                                 $s_entry = preg_replace("/^entry_edit_/","",$name);
129                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
130                                 $s_action = "edit";
131                         }elseif(preg_match("/^entry_freeze_edit_.*/",$name)){
132                                 $s_entry = preg_replace("/^entry_freeze_edit_/","",$name);
133                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
134                                 $s_action = "edit";
135         $no_save = TRUE;
136                         }elseif(preg_match("/^entry_delete_.*/",$name)){
137                                 $s_entry = preg_replace("/^entry_delete_/","",$name);
138                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
139         $s_action = "delete";
140       }
141     }
143                 if(isset($_GET['edit_entry'])){
144                         $s_entry = $_GET['edit_entry'];
145                         $s_action = "edit";
146                 }
148                 if(isset($_GET['edit_freeze_entry'])){
149                         $s_entry = $_GET['edit_freeze_entry'];
150                         $s_action = "edit";
151       $no_save = TRUE;
152                 }
154     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch"){
155       $s_action = "freeze_branch";
156     }
157     if(isset($_GET['act']) && $_GET['act'] == "branch_branch"){
158       $s_action = "branch_branch";
159     }
160     if(isset($_GET['act']) && $_GET['act'] == "remove_branch"){
161       $s_action = "remove_branch";
162     }
163     
164     if((isset($_POST['CancelBranchName'])) || (isset($_POST['CloseIFrame']))){
165       $this->dispNewBranch = false;
166       $this->dispNewFreeze = false;
167     }
170     $type_acl_mapping = array(
171         "FAIpartitionTable"  => "faiPartitionTable", 
172         "FAIpackageList"     => "faiPackage",
173         "FAIscript"          => "faiScript",
174         "FAIvariable"        => "faiVariable",
175         "FAIhook"            => "faiHook",
176         "FAIprofile"         => "faiProfile",
177         "FAItemplate"        => "faiTemplate");
180     /* handle C&P from layers menu */
181     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
182       $s_action = "copy_multiple";
183     }
184     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
185       $s_action = "cut_multiple";
186     }
187     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
188       $s_action = "editPaste";
189     }
191     /* Create options */
192     if(isset($_POST['menu_action']) && preg_match("/^Create_/",$_POST['menu_action'])){
193       $s_action = "new_".preg_replace("/^Create_/","",$_POST['menu_action']);;
194       $s_entry  = preg_replace("/^Create_/","",$_POST['menu_action']);
195     }
197     /* handle remove from layers menu */
198     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
199       $s_action = "del_multiple";
200     }
203     if(!empty($s_action)){
204       $this->no_save = $no_save;
205     }
207     /********************
208       Copy & Paste
209      ********************/
211     /* Display the copy & paste dialog, if it is currently open */
212     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
213     if($ret){
214       return($ret);
215     }
218     /****************
219       Delete confirme dialog 
220      ****************/
222                 if ($s_action=="delete"){
224       /* Get 'dn' from posted termlinst */
225       $this->dn= $this->objects[$s_entry]['dn'];
227                         /* Load permissions for selected 'dn' and check if
228                            we're allowed to remove this 'dn' */
229       $acl  = $this->ui->get_permissions($this->dn,"fai/".$type_acl_mapping[$this->objects[$s_entry]['type']]);
230       if(preg_match("/d/",$acl)){
232                                 /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
233                                 if (($user= get_lock($this->dn)) != ""){
234                                         return(gen_locked_message ($user, $this->dn));
235                                 }
237                                 /* Lock the current entry, so nobody will edit it during deletion */
238                                 add_lock ($this->dn, $this->ui->dn);
239                                 $smarty->assign("warning",msgPool::deleteInfo(@LDAP::fix($this->dn),_("FAI object")));
240         $smarty->assign("multiple", false);
241                                 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
242                         } else {
244                                 /* Obviously the user isn't allowed to delete. Show message and clean session. */
245         msg_dialog::display(_("Permission error"), msgPool::permDelete(),ERROR_DIALOG);
246                         }
247                 }
250     /********************
251       Delete MULTIPLE entries requested, display confirm dialog
252      ********************/
254     if ($s_action=="del_multiple"){
255       $this->dns = array();
256       $ids = $this->list_get_selected_items();
258       if(count($ids)){
260         $errors = "";
261         foreach($ids as $id){
262           $dn = $this->objects[$id]['dn'];
263           $cn = $this->objects[$id]['cn'];
264           if(!preg_match('/^freeze/', $this->objects[$id]['FAIstate'])){
265             $this->dns[$id] = $dn;
266           }else{
267             $errors .= $cn.", ";
268           }
269         }
270         if ($user= get_multiple_locks($this->dns)){
271           return(gen_locked_message($user,$this->dns));
272         }
274         if($errors != ""){
275           msg_dialog::display(_("Branch locked"),sprintf(_("The following entries are locked, you can't remove them %s."),
276             "<br><br>".trim($errors,", ")),INFO_DIALOG);
277         }
279         if(count($this->dns)){
281           $dns_names = array();
282           foreach($this->dns as $dn){
283             add_lock ($dn, $this->ui->dn);
284             $dns_names[] = @LDAP::fix($dn);
285           }
287           /* Lock the current entry, so nobody will edit it during deletion */
288                                   $smarty->assign("warning",msgPool::deleteInfo($dns_names,_("FAI object")));
289           $smarty->assign("multiple", true);
290           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
291         }
292       }
293     }
296     /********************
297       Delete MULTIPLE entries confirmed
298      ********************/
300     /* Confirmation for deletion has been passed. Users should be deleted. */
301     if (isset($_POST['delete_multiple_fai_object_confirm'])){
303       /* Find out more about the object type */
304       $ldap   = $this->config->get_ldap_link();
306       /* Remove user by user and check acls before removeing them */
307       foreach($this->dns as $key => $dn){
309         $ldap->cat($dn, array('objectClass'));
310         $attrs  = $ldap->fetch();
311         $type   = $this->get_type($attrs);
313         $acl  = $this->ui->get_permissions($dn,"fai/".$type[1]);
314         if(preg_match("/d/",$acl)){
316           $this->dialog = new $type[0]($this->config, $this->config->data['TABS'][$type[2]], $dn,"fai");
317           $this->dialog->parent = &$this;
318           $this->dialog->set_acl_base($dn);
319           $this->dialog->by_object[$type[1]]->remove_from_parent ();
320           unset ($this->dialog);
321           $this->dialog= FALSE;
322           $to_del = FAI::clean_up_releases($dn);
323           FAI::save_release_changes_now();
325           foreach($to_del as $dn){
326             $ldap->rmdir_recursive($dn);
327           }
329         } else {
331           /* Normally this shouldn't be reached, send some extra
332              logs to notify the administrator */
333           msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
334           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
335         }
337         /* Remove lock file after successfull deletion */
338         del_lock ($dn);
339         unset($this->dns[$key]);
340       }
341     }
344     /********************
345       Delete MULTIPLE entries Canceled
346      ********************/
348     /* Remove lock */
349     if(isset($_POST['delete_multiple_fai_object_cancel'])){
350       foreach($this->dns as $key => $dn){
351         del_lock ($dn);
352         unset($this->dns[$key]);
353       }
354     }
357     /****************
358       Delete aborted  
359      ****************/
361                 /* Delete canceled? */
362                 if (isset($_POST['delete_cancel'])){
363                         del_lock ($this->dn);
364                 }
367     /****************
368       Delete confirmed 
369      ****************/
371                 /* Deltetion was confirmed, so delete this entry
372      */
373     if (isset($_POST['delete_terminal_confirm'])){
375       /* Some nice guy may send this as POST, so we've to check
376          for the permissions again. */
378       /* Find out more about the object type */
379       $ldap       = $this->config->get_ldap_link();
380       $ldap->cat($this->dn, array('objectClass'));
381       if($ldap->count()){
382         $attrs  = $ldap->fetch();
383         $type     = $this->get_type($attrs);                    
385         $acl  = $this->ui->get_permissions($this->dn,"fai/".$type[1]);
386         if(preg_match("/d/",$acl)){
388           $this->dialog = new $type[0]($this->config,   $this->config->data['TABS'][$type[2]], $this->dn,"fai");
389           $this->dialog->set_acl_base($this->dn);
390           $this->dialog->parent = &$this;
391           $this->dialog->by_object[$type[1]]->remove_from_parent ();
392           unset ($this->dialog);
393           $this->dialog= FALSE;
394           $to_del = FAI::clean_up_releases($this->dn);
395           FAI::save_release_changes_now();
397           foreach($to_del as $dn){
398             $ldap->rmdir_recursive($dn);
399           }
401         } else {
403           /* Normally this shouldn't be reached, send some extra
404              logs to notify the administrator */
405           msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
406           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
407         }
409       }else{
410         msg_dialog::display(_("Error"), sprintf(_("Cannot delete '%s': object does not exist!"), $this->dn) , ERROR_DIALOG);
411       }
412       /* Remove lock file after successfull deletion */
413       del_lock ($this->dn);
414     }
417     /****************
418       Edit entry 
419      ****************/
421                 if(($s_action == "edit") && (!isset($this->dialog->config))){
422                         $entry    = $this->objects[$s_entry];
423                         $a_setup  = $this->get_type($entry);
424                         $this->dn = $entry['dn'];
426                         /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
427                         if (($user= get_lock($this->dn)) != ""){
428                                 return(gen_locked_message ($user, $this->dn));
429                         }
430                         add_lock ($this->dn, $this->ui->dn);
432                         $this->dialog     = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
433       $this->dialog->parent = &$this;
434       $this->dialog->set_acl_base($this->dn);
435                         $this->is_dialog  = true;
437       if(preg_match("/^freeze/", $entry['FAIstate']) || $this->no_save){
438 #        $this->dialog->set_acl_base("freezed")  ;    
439       }
440                         session::set('objectinfo',$this->dn);
441                 }
444     /*  Branch handling 
445         09.01.2006
446     */
448     /****************
449       Remove branch
450      ****************/
452     /* Remove branch 
453      */
454     if($s_action == "remove_branch"){
455       $base= $this->fai_release;
457       /* Check if we have a post remove method configured
458        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
459        */
460       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
461         /* Load permissions for selected 'dn' and check if
462            we're allowed to remove this 'dn' */
463         if($this->acl_is_removeable()){
465           $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->fai_release),_("FAI branch/freeze")));
466           return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
467         } else {
468           msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
469         }
470       }
471     }
473     
474     /****************
475       Remove branch confirmed
476      ****************/
478     if(isset($_POST['delete_branch_confirm'])){
480       /* Check if we have a post remove method configured
481        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
482        */
483       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
485         $bb =  $this->fai_release;
486         if(!isset($ldap)){
487           $ldap = $this->config->get_ldap_link();
488         }
490         $br = $this->getBranches();
492         if(isset($br[$bb]) && $this->acl_is_removeable()){
493           $name = $br[$bb];
494           $ldap->cd($bb);
495           $ldap->recursive_remove();
496           $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('applicationou'), $bb));
497           $ldap->recursive_remove();
498           $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('mimetypeou'), $bb));
499           $ldap->recursive_remove();
500           $this->fai_release = $this->fai_base;
502           /* Post remove */
503           $this->lock_name   = $name;
504           $this->lock_dn     = $bb;
505           $this->postremove();
507           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
508           trigger_error("Should be removed");
509         }else{
510           if(!isset($br[$bb])){
511             trigger_error("Invalid release ".$bb);
512           }
513           if(!$this->acl_is_removeable()){
514             trigger_error("Invalid permission");
515           }
516         }
517       }else{
518         trigger_error("No remove Hook");
519       }
520     }
523     /****************
524       Create a new branch "insert Name"
525      ****************/
527     if((isset($_POST['UseBranchName']))&&(($this->dispNewBranch)||($this->dispNewFreeze))){
528       session::set('LASTPOST',$_POST);
530       if($this->dispNewBranch){
531         $type = "branch";
532       }else{
533         $type = "freeze";
534       }
536       /* Check branch name */
537       $name = $_POST['BranchName'];
538       $is_ok = true;
539       $smarty->assign("BranchName",$name);
540       $base= $this->fai_base;
542       /* Check used characters */
543       if(!preg_match("/^[0-9a-z \ö\ä\ü\.\-_:,]*$/i",$name)){
544         msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z \ö\ä\ü\.\-_:,]/i"), ERROR_DIALOG);
545         $is_ok = false;
546       }
548       /* Check if this name is already in use */
549       if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
550         msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
551         $is_ok = false;
552       }
554       if($is_ok){
556         if(session::is_set('LASTPOST')){
557           $LASTPOST = session::get('LASTPOST');
558         }else{
559           $LASTPOST = array();
560         }
561         $LASTPOST['base'] = $base;
562         $LASTPOST['type'] = $type;
563         session::set('LASTPOST',$LASTPOST);
564         $smarty->assign("iframe", true);
565         $smarty->assign("plugID", $_GET['plug']);
566         $display        = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
567         return($display);
568       }
569     }
572     /****************
573       Create a new branch 
574      ****************/
576     if(isset($_GET['PerformBranch'])){
577     
578       if(!$this->acl_is_createable()){
579         msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
580       }else{
582         /* Create it know */
583         $this->dispNewBranch = false;
584         $this->dispNewFreeze = false;
586         $LASTPOST = session::get('LASTPOST');
587         $base = $LASTPOST['base'];
588         $_POST  = session::get('LASTPOST');      
589         $name = $_POST['BranchName'];
591         $type = $LASTPOST['type'];
592         $ldap = $this->config->get_ldap_link();
594         $baseToUse = $base;
595         if($this->fai_release !=  $this->fai_base){
596           $baseToUse = $this->fai_release;
597         }
599         /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
601         $CurrentReleases  = $this->getBranches();
602         $NewReleaseName   = $name;
603         if(isset($CurrentReleases[$this->fai_release])) {
604           if($this->fai_release != $this->fai_base){
605             $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
606             $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName); 
607           }else{
608             $NewReleaseName   = $name;
609           }
610         }
612         $appsrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),$baseToUse); 
613         $appdst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),"ou=".$name.",".$baseToUse) ; 
615         $mimesrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),$baseToUse); 
616         $mimedst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),"ou=".$name.",".$baseToUse) ; 
618         /* Check if source depeartments exist */
619         foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
620           $ldap->cd($this->config->current['BASE']);
621           $ldap->cat($dep);
622           if(!$ldap->count()){
623             $ldap->create_missing_trees($dep);
624           }
625         }
627         /* Print header to have styles included */
628         echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
629           <html>
630           <head>
631           <title></title>
632           <style type="text/css">@import url("themes/default/style.css");</style>
633           <script language="javascript" src="include/focus.js" type="text/javascript"></script>
634           </head>
635           <body style="background: none;margin:3px;color:black">
636           ';
638         new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
640         /* Duplicate group application releases 
641          */
642         FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
644         /* Duplicate applications 
645          */
646         $ldap->cat($appsrc,array("dn")) ;
647         if($ldap->count()){
648           $ldap->cd ($appdst);
649           $ldap->recursive_remove();
650           FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
651         }
653         /* Duplicate mime types 
654          */
655         $ldap->cat($mimesrc,array("dn")) ;
656         if($ldap->count()){
657           $ldap->cd ($mimedst);
658           $ldap->recursive_remove();
659           FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
660         }
662         $attr = array();
663         $attr['objectClass'] = array("organizationalUnit","FAIbranch");
664         $attr['ou']       = $name;
665         $attr['FAIstate'] = $type;
666         $ldap->cd($this->config->current['BASE']);
667         $ldap->cd("ou=".$name.",".$baseToUse);
668         $ldap->cat("ou=".$name.",".$baseToUse);
669         if($ldap->count()){
670           $ldap->modify($attr);
671         }else{
672           $ldap->add($attr);
673         }
675         /* Duplicate fai objects 
676          */
677         //      $ldap->cd ("ou=".$name.",".$baseToUse);
678         //      $ldap->recursive_remove();
679         //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
681         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
682           <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
683           </form></div>";
685         echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
687         /* Print footer to have valid html */
688         echo "</body></html>";
690         $this->dispNewFreeze = false; 
692         /* Postcreate */ 
694         /* Assign possible attributes */
695         $this->lock_type  = $type; 
696         $this->lock_name  = $name; 
697         $this->lock_dn    = $baseToUse;
698         $this->postcreate();
699         exit();
700       }
701     }
703     /****************
704       Display dialog to enter new Branch name
705      ****************/
707     /* Check if we have a post create method configured
708      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
709      */
710     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
711       if(($s_action == "branch_branch")||($this->dispNewBranch)){
712         if(!$this->acl_is_createable()){
713         msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
714         }else{
715           $this->dispNewBranch=true;
716           $smarty->assign("iframe",false);
717           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
718           return($display);
719         }
720       } 
721     }
723  
724     /****************
725       Display dialog to enter new Freeze name
726      ****************/
728     /* Check if we have a post create method configured
729      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
730      */
731     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
732       if(($s_action == "freeze_branch")||($this->dispNewFreeze)){
733         if(!$this->acl_is_createable()){
734           msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
735         }else{
736           $this->dispNewFreeze = true;
737           $smarty->assign("iframe",false);
738           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
739           return($display);
740         }
741       }
742     }
745     /****************
746       Create a new object 
747      ****************/
749     $types = array( "new_partition"     =>  "FAIpartitionTable",
750                     "new_script"        =>  "FAIscript",
751                     "new_hook"          =>  "FAIhook",
752                     "new_variable"      =>  "FAIvariable",
753                     "new_template"      =>  "FAItemplate",
754                     "new_package"       =>  "FAIpackageList");
755     $types_i18n = array( "new_partition"     =>  _("partition table"),
756                     "new_script"        =>  _("script"),
757                     "new_hook"          =>  _("hook"),
758                     "new_variable"      =>  _("variable"),
759                     "new_template"      =>  _("template"),
760                     "new_package"       =>  _("package list"));
762     if(isset($types[$s_action])){
763       $acl = $this->ui->get_permissions($this->fai_base,"fai/".$type_acl_mapping[$types[$s_action]]);
764       if(preg_match("/c/",$acl)){
765         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,$types[$s_action]);
766         $this->dialog->parent = &$this;
767       }else{
768         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), $types_i18n[$s_action]), ERROR_DIALOG);
769       }
770     }
772     /* New Profile */
773     if($s_action == "new_profile"){
774       $this->dn = "new" ;
776       $acl = $this->ui->get_permissions($this->fai_base,"fai/faiProfile");
777       if(preg_match("/c/",$acl)){
778         $a_setup= $this->get_type(array("objectClass"=>array("FAIprofile")));
779         $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
780         $this->dialog->set_acl_base($this->base);
781         $this->dialog->parent = &$this;
783         $this->is_dialog = false;
784       }else{
785         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
786       }
787     }
790     /****************
791       Get from ask class name dialog 
792      ****************/
794     if($s_action == "select_class_name_finished"){
795       $this->dialog->save_object();
796       if(count($this->dialog->check())!=0){
797         foreach($this->dialog->check() as $msg){
798           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
799         }               
800       }elseif(isset($this->dialog->objectClass)){
801         $this->dn = "new" ;
802         $a_setup= $this->get_type(array("objectClass"=>array($this->dialog->objectClass)));
803         $name = $this->dialog->save();
805         if(class_exists($a_setup[0])){
806           $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
807           $this->dialog->set_acl_base($this->base);
808           $this->dialog->by_object[$a_setup[1]]->cn = $name;
809           $this->dialog->parent = &$this;
810           $this->is_dialog = true;
811         }
812       }         
813     }   
816     /****************
817      Cancel dialogs 
818      ****************/
820                 if(isset($_POST['edit_cancel'])){
821                         unset($this->dialog);
822                         $this->dialog=FALSE;
823                         $this->is_dialog = false;
824                         session::un_set('objectinfo');
825                         del_lock ($this->dn);
826                 }
829     /****************
830       Save sub dialogs 
831      ****************/
833                 /* This check if the given tab could be saved 
834                  * If it was possible to save it, remove dialog object. 
835                  * If it wasn't possible, show errors and keep dialog.
836                  */
837                 if((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->dialog->config))){
838                         $this->dialog->save_object();
839       $msgs= $this->dialog->check();
840                         if(count($msgs)!=0){
841                                 foreach($msgs as $msg){
842           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
843                                 }
844                         }else{
845                                 $this->dialog->save();
846         FAI::save_release_changes_now();
847         if (!isset($_POST['edit_apply'])){
848           del_lock ($this->dn);
849           unset($this->dialog);
850           $this->dialog=FALSE;
851           $this->is_dialog=false;
852           session::un_set('objectinfo');
853         }
854                         }
855                 }
858     /****************
859       Display currently open dialog 
860      ****************/
862                 /* If dialog is set, but $this->is_dialog==false, then 
863                  *  only the "abort" button is shown, this are dialogs that must not be saved.  
864                  * If is_dialog == true, we are currently editing tab objects.
865                  *  Here we need both, save and cancel
866                  */ 
868                 if(is_object($this->dialog)){
869                         $display .= $this->dialog->execute();
870                         /* Don't show buttons if tab dialog requests this */
872       if(isset($this->dialog->current)){
874         $obj = $this->dialog->by_object[$this->dialog->current];
876         if((isset($obj->is_dialog) && (!$obj->is_dialog)) || (isset($obj->dialog) && (!$obj->dialog))){
878           $display.= "<p style=\"text-align:right\">\n";
879           if(!$this->no_save){
880             $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
881             $display.= "&nbsp;\n";
882             if ($this->dn != "new"){
883               $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
884               $display.= "&nbsp;\n";
885             }
886           }
887           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
888           $display.= "</p>";
889         }elseif(!isset($this->dialog->current)){
890           $display.= "<p style=\"text-align:right\">\n";
891           $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
892           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
893           $display.= "</p>";
894         }
895       }else{
896         $display.= "<p style=\"text-align:right\">\n";
897         $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
898         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
899         $display.= "</p>";
901       }
902       return($display);
903                 }
904                 
906     /****************
907       Dialog display
908      ****************/
910     /* Check if there is a snapshot dialog open */
911     $base = $this->fai_base;
912     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
913       return($str);
914     }
916     /* Display dialog with system list */
917     $this->reload();
918     $this->DivListFai->parent = &$this;
919     $this->DivListFai->execute();
920     $this->DivListFai->setEntries($this->objects);
921     return($this->DivListFai->Draw());
922         }
925   /* Return departments, that will be included within snapshot detection */
926   function get_used_snapshot_bases()
927   {
928     $tmp = array();
929     $types = array("faipartitionou","faiscriptou","faitemplateou","faihookou","faiprofileou","faivariableou","faipackageou");
930     foreach($types as $type){
931       $tmp[] = get_ou($type).$this->fai_release;
932     }
933     return($tmp);
934   }
937   /* Get available branches for current base */
938   function getBranches($base = false,$prefix = "")
939   {
940     $ret = array("/"=>$this->fai_base);
941     $ldap = $this->config->get_ldap_link();
942     if(!$base){
943       $base = $this->fai_base;
944     }
945     $tmp = FAI::get_all_releases_from_base($base,true);
946     foreach($tmp as $dn => $name){
947       $ret[$name]=$dn;
948     }
949     ksort($ret);
950     $ret = array_flip($ret);
952     /* Ensure that we have a valid release selected */
953     if(!isset($ret[$this->fai_release])){
954       $this->fai_release = $this->fai_base;
955     }
957     return ($ret);
958   }
959   
961   function list_get_selected_items()
962   {
963     $ids = array();
964     foreach($_POST as $name => $value){
965       if(preg_match("/^item_selected_[0-9]*$/",$name)){
966         $id   = preg_replace("/^item_selected_/","",$name);
967         $ids[$id] = $id;
968       }
969     }
970     return($ids);
971   }
974   /* reload list of objects */
975   function reload()
976   {
977     /* Variable initialisation */
978     $str            = "";
979     $Regex          = $this->DivListFai->Regex;
980     $this->objects  = array();
982     /* Get base */
983     $base = $this->fai_base;
984     if($this->fai_release != $this->fai_base){
985       $br = $this->getBranches();
986       if(isset($br[$this->fai_release])){
987         $base = $this->fai_release;
988       }else{
989         $base = $this->fai_base;
990       }
991     }
992     $this->base = $base;
993     $this->set_acl_base($this->base);
995     $this->lock_type = FAI::get_release_tag(FAI::get_release_dn($base));
997     /* Create a new list of FAI object 
998      * Generate List of Partitions,Hooks,Scripts,Templates,Profiles ... 
999      */
1000     $ObjectTypes = array(
1001         "FAIpartitionTable"  => array("OU"=> get_ou('faipartitionou') , "CHKBOX"=>"ShowPartitions"  ,"ACL" => "faiPartitionTable"),
1002         "FAIpackageList"     => array("OU"=> get_ou('faipackageou')   , "CHKBOX"=>"ShowPackages"    ,"ACL" => "faiPackage"),
1003         "FAIscript"          => array("OU"=> get_ou('faiscriptou')    , "CHKBOX"=>"ShowScripts"     ,"ACL" => "faiScript"),
1004         "FAIvariable"        => array("OU"=> get_ou('faivariableou')  , "CHKBOX"=>"ShowVariables"   ,"ACL" => "faiVariable"),
1005         "FAIhook"            => array("OU"=> get_ou('faihookou')      , "CHKBOX"=>"ShowHooks"       ,"ACL" => "faiHook"),
1006         "FAIprofile"         => array("OU"=> get_ou('faiprofileou')   , "CHKBOX"=>"ShowProfiles"    ,"ACL" => "faiProfile"),
1007         "FAItemplate"        => array("OU"=> get_ou('faitemplateou')  , "CHKBOX"=>"ShowTemplates"   ,"ACL" => "faiTemplate"));
1009     $filter = "";
1010     foreach($ObjectTypes as $key => $data){
1011       if($this->DivListFai->$data['CHKBOX']){
1012         $filter.= "(objectClass=".$key.")";
1013       }
1014     }
1015     $filter = "(&(|".$filter.")(cn=$Regex))";
1016     
1017     /* Get resolved release dependencies */
1018     $tmp = FAI::get_all_objects_for_given_base($base,$filter);
1020     /* Ge listed ldap objects */
1021     $ldap = $this->config->get_ldap_link();
1022     $ldap->cd($this->config->current['BASE']);
1024     /* Get release tag 
1025        If current release is freezed, all objects are freezed to.
1026      */
1027     $ldap->cat($base);
1028     $release_attrs = $ldap->fetch();
1029     $force_freezed = FALSE;
1030     if(isset($release_attrs['FAIstate'][0]) && preg_match("/freeze/i",$release_attrs['FAIstate'][0])){
1031       $force_freezed = TRUE;
1032     }
1034     foreach($tmp as $entry){
1036       /* Get some more informations about the object */ 
1037       $ldap->cat($entry['dn'], array("cn","description","objectClass","FAIclass","FAIstate","objectClass"));
1038       $object  = $ldap->fetch();
1040       /* Walk through possible types */
1041       foreach($ObjectTypes as $type => $rest){  
1043         $acl = $this->ui->get_permissions($object['dn'],"fai/".$rest ['ACL']);
1045         if(in_array($type,$object['objectClass']) && preg_match("/r/",$acl)){
1047           /* Prepare object */
1048           unset($object['objectClass']['count']);
1049           if(!isset($object['description'][0])){
1050             $object['description'][0]="";
1051           }
1053           /* Clean up object informations */
1054           $obj                  = array();
1055           $obj['cn']                          = $object['cn'][0];
1056           $obj['dn']                          = $object['dn'];
1057           $obj['acl']                       = $acl;
1058           $obj['description']   = $object['description'][0];
1059           $obj['objectClass']   = $object['objectClass'];
1061           /* Append type to this string, to be able to check if the selected 
1062            * entry is of type 'freeze' or 'branch'
1063            */
1064           if(!isset($object['FAIstate'])){
1065             $obj['FAIstate'] = $this->lock_type;
1066           }else{
1067             $obj['FAIstate'] = $object['FAIstate'][0]; 
1068           }
1070           if($force_freezed){
1071             $obj['FAIstate'] = "freeze";
1072           }
1074           $this->objects[strtolower($obj['cn']).$obj['cn'].$type] = $obj;
1075           $this->objects[strtolower($obj['cn']).$obj['cn'].$type]['type']=$type;
1076         }
1077                         }
1078                 }
1080                 ksort($this->objects);
1081                 reset ($this->objects);
1082         
1083                 /* use numeric index, thats a bit more secure */        
1084                 $tmp0 = array();
1085                 foreach($this->objects as $obj){
1086                         $tmp0[]= $obj;
1087                 }
1088                 $this->objects = array();
1089                 $this->objects = $tmp0;
1090         }
1092         function remove_lock()
1093         {
1094                 if (isset($this->dn)){
1095                         del_lock ($this->dn);
1096                 }
1097         }
1099         function get_type($array){
1100                 if(in_array("FAIpartitionTable",$array['objectClass'])){
1101                         return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
1102                 }
1103                 if(in_array("FAIscript",$array['objectClass'])){
1104                         return(array("tabsScript","faiScript","FAISCRIPTTABS"));
1105                 }
1106                 if(in_array("FAItemplate",$array['objectClass'])){
1107                         return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
1108                 }
1109                 if(in_array("FAIhook",$array['objectClass'])){
1110                         return(array("tabsHook","faiHook","FAIHOOKTABS"));
1111                 }
1112                 if(in_array("FAIvariable",$array['objectClass'])){
1113                         return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
1114                 }
1115                 if(in_array("FAIprofile",$array['objectClass'])){
1116                         return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
1117                 }
1118                 
1119                 if(in_array("FAIpackageList",$array['objectClass'])){
1120                         return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
1121                 }
1122         }
1124   function CheckNewBranchName($name,$base)
1125   {
1126     $f = $this->fai_release;
1127     if($name == ""){
1128       return(false);
1129     }elseif(in_array($name,$this->getBranches($f))) {
1130       return(false);
1131     }elseif(tests::is_department_name_reserved($name,$base)){
1132       return(false);
1133     }
1134     return(true);
1135   }
1137   function save_object()
1138   {
1139     $this->DivListFai->save_object();
1141     /* Get posted release */
1142     $r_releases = array_flip($this->getBranches());
1143     if(isset($_POST['fai_release']) && isset($r_releases[get_post('fai_release')])){
1144       $this->fai_release = $r_releases[get_post('fai_release')];
1146       $fai_filter = session::get("fai_filter");
1147       $fai_filter['fai_release'] = $this->fai_release;
1148       session::set("fai_filter",$fai_filter);
1149     }
1151     if(is_object($this->CopyPasteHandler)){
1152       $this->CopyPasteHandler->save_object();
1153     }
1154   }
1157   function copyPasteHandling_from_queue($s_action,$s_entry)
1158   {
1159     /* Check if Copy & Paste is disabled */
1160     if(!is_object($this->CopyPasteHandler)){
1161       return("");
1162     }
1165     /* Add a single entry to queue */
1166     if($s_action == "copy"){
1168       /* Cleanup object queue */
1169       $this->CopyPasteHandler->cleanup_queue();
1170       $entry    = $this->objects[$s_entry];
1171       $a_setup  = $this->get_type($entry);
1172       $dn = $entry['dn'];
1173       $this->CopyPasteHandler->add_to_queue($dn,$s_action,$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1174     }
1176     /* Add entries to queue */
1177     if($s_action == "copy_multiple"){
1179       /* Cleanup object queue */
1180       $this->CopyPasteHandler->cleanup_queue();
1182       /* Add new entries to CP queue */
1183       foreach($this->list_get_selected_items() as $id){
1185         /* Cleanup object queue */
1186         $entry    = $this->objects[$id];
1187         $a_setup  = $this->get_type($entry);
1188         $dn = $entry['dn'];
1191         if($s_action == "copy_multiple"){
1192           $this->CopyPasteHandler->add_to_queue($dn,"copy",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1193         }
1194       }
1195     }
1197     /* Start pasting entries */
1198     if($s_action == "editPaste" && !($this->lock_type == "freeze" && !$this->allow_freeze_object_attach)){
1199       $this->start_pasting_copied_objects = TRUE;
1200     }
1202     /* Return C&P dialog */
1203     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1205       /* Get dialog */
1206       $this->CopyPasteHandler->SetVar("parent",$this);
1207       $data = $this->CopyPasteHandler->execute();
1209       FAI::save_release_changes_now();
1211       /* Return dialog data */
1212       if(!empty($data)){
1213         return($data);
1214       }
1215     }
1217     /* Automatically disable status for pasting */
1218     if(!$this->CopyPasteHandler->entries_queued()){
1219       $this->start_pasting_copied_objects = FALSE;
1220     }
1221     return("");
1222   }
1225   /* Check if the given FAI class is used in this release 
1226    */
1227   static function check_class_name($oc,$name,$dn)
1228   {
1229     $base = FAI::get_release_dn($dn);
1230     $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
1231     $delete = array();
1232     $used   = array();
1233     foreach($res as $object){
1234       $used[$object['cn'][0]]= $object['cn'][0];
1235     }
1236     return($used);
1237   }
1240   /* Return plugin informations for acl handling */ 
1241   static function plInfo()
1242   {
1243     return (array( 
1244           "plShortName"   => _("FAI releases"),
1245           "plDescription" => _("FAI release management"),
1246           "plSelfModify"  => FALSE,
1247           "plDepends"     => array(),
1248           "plPriority"    => 0,
1249           "plSection"     => array("administration"),           
1250           "plCategory"    => array("fai"=> array("description" => _("FAI"),
1251                                                  "objectClass" => "FAIclass")),
1252           "plProvidedAcls"=> array()));
1253   }
1255 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1256 ?>