Code

Updated FAI release creation check, valid characters for a release are now "a-z0-9."
[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 = TRUE;
52   var $no_save;
53   var $fai_base     ="";
54   var $fai_release  ="";
56   var $acl_module = array("fai");
58         /* construction/reconstruction 
59          */
60         function faiManagement (&$config, $ui)
61         {
62                 /* Set defaults */
63                 $this->dn                       = "";
64                 $this->config   = $config;
65                 $this->ui                       = $ui;  
66     
67     /* Creat dialog object */
68     $this->DivListFai = new divListFai($this->config,$this);
70     /* Copy & Paste handler */
71     if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
72       $this->CopyPasteHandler= new CopyPasteHandler($this->config);
73     }
75     /* Set default release */
76     $this->fai_base = get_ou("faiou").$this->config->current['BASE'];
77     if(!session::is_set("fai_filter")){
78       session::set("fai_filter",array("fai_release" => $this->fai_base));
79     }
81     $fai_filter = session::get("fai_filter");
82     $this->fai_release = $fai_filter['fai_release'];
83         }
85         function execute()
86         {
87     /* Call parent execute */
88     plugin::execute();
90     /* Initialise vars and smarty */
91                 $smarty         = get_smarty();
92                 $smarty->assign("BranchName","");
93     
94                 $display        = "";
95     $s_action   = "";
96                 $s_entry        = "";
97     $no_save = FALSE;   // hide Apply / Save buttons
98     
99     /* If an entry was locked, these vars will be stored in a session to allow direct edit */
100     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/"));
103     /****************
104       Handle posts 
105      ****************/
107                 /* Check ImageButton posts
108                  * Create new tab ich new_xx is posted
109                  */
110     $posts = array( "/remove_branch/"=>"remove_branch",    "/branch_branch/"=>"branch_branch",
111                     "/freeze_branch/"=>"freeze_branch",    "/create_partition/i"=>"new_partition",
112                     "/create_script/i"=>"new_script",      "/create_hook/i"=>"new_hook",
113                     "/create_variable/i"=>"new_variable",  "/create_template/i"=>"new_template",
114                     "/create_package/i"=>"new_package",    "/create_profile/i"=>"new_profile",
115                     "/edit_continue/"=>"select_class_name_finished",
116                     "/^multiple_copy_fai/" => "copy_multiple", 
117                     "/^multiple_cut_fai/" => "cut_multiple", 
118                     "/^copy/" => "copy",
119                     "/^remove_multiple_fai_objects/" => "del_multiple");
121                 foreach($_POST as $name => $value){
122       foreach($posts as $reg => $act ){
123         if(preg_match($reg,$name)){
124           $s_action = $act;
125           $s_entry = ltrim(preg_replace($reg,"",$name),"_");
126           $s_entry = preg_replace("/_.*$/","",$s_entry);
127         }
128       }
129                         if(preg_match("/^entry_edit_.*/",$name)){
130                                 $s_entry = preg_replace("/^entry_edit_/","",$name);
131                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
132                                 $s_action = "edit";
133                         }elseif(preg_match("/^entry_freeze_edit_.*/",$name)){
134                                 $s_entry = preg_replace("/^entry_freeze_edit_/","",$name);
135                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
136                                 $s_action = "edit";
137         $no_save = TRUE;
138                         }elseif(preg_match("/^entry_delete_.*/",$name)){
139                                 $s_entry = preg_replace("/^entry_delete_/","",$name);
140                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
141         $s_action = "delete";
142       }
143     }
145                 if(isset($_GET['edit_entry'])){
146                         $s_entry = $_GET['edit_entry'];
147                         $s_action = "edit";
148                 }
150                 if(isset($_GET['edit_freeze_entry'])){
151                         $s_entry = $_GET['edit_freeze_entry'];
152                         $s_action = "edit";
153       $no_save = TRUE;
154                 }
156     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch"){
157       $s_action = "freeze_branch";
158     }
159     if(isset($_GET['act']) && $_GET['act'] == "branch_branch"){
160       $s_action = "branch_branch";
161     }
162     if(isset($_GET['act']) && $_GET['act'] == "remove_branch"){
163       $s_action = "remove_branch";
164     }
165     
166     if((isset($_POST['CancelBranchName'])) || (isset($_POST['CloseIFrame']))){
167       $this->dispNewBranch = false;
168       $this->dispNewFreeze = false;
169     }
172     $type_acl_mapping = array(
173         "FAIpartitionTable"  => "faiPartitionTable", 
174         "FAIpackageList"     => "faiPackage",
175         "FAIscript"          => "faiScript",
176         "FAIvariable"        => "faiVariable",
177         "FAIhook"            => "faiHook",
178         "FAIprofile"         => "faiProfile",
179         "FAItemplate"        => "faiTemplate");
182     /* handle C&P from layers menu */
183     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
184       $s_action = "copy_multiple";
185     }
186     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
187       $s_action = "cut_multiple";
188     }
189     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
190       $s_action = "editPaste";
191     }
193     /* Create options */
194     if(isset($_POST['menu_action']) && preg_match("/^Create_/",$_POST['menu_action'])){
195       $s_action = "new_".preg_replace("/^Create_/","",$_POST['menu_action']);;
196       $s_entry  = preg_replace("/^Create_/","",$_POST['menu_action']);
197     }
199     /* handle remove from layers menu */
200     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
201       $s_action = "del_multiple";
202     }
205     if(!empty($s_action)){
206       $this->no_save = $no_save;
207     }
209     /********************
210       Copy & Paste
211      ********************/
213     /* Display the copy & paste dialog, if it is currently open */
214     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
215     if($ret){
216       return($ret);
217     }
220     /****************
221       Delete confirme dialog 
222      ****************/
224                 if ($s_action=="delete"){
226       /* Get 'dn' from posted termlinst */
227       $this->dn= $this->objects[$s_entry]['dn'];
229                         /* Load permissions for selected 'dn' and check if
230                            we're allowed to remove this 'dn' */
231       $acl  = $this->ui->get_permissions($this->dn,"fai/".$type_acl_mapping[$this->objects[$s_entry]['type']]);
232       if(preg_match("/d/",$acl)){
234                                 /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
235                                 if (($user= get_lock($this->dn)) != ""){
236                                         return(gen_locked_message ($user, $this->dn));
237                                 }
239                                 /* Lock the current entry, so nobody will edit it during deletion */
240                                 add_lock ($this->dn, $this->ui->dn);
241                                 $smarty->assign("warning",msgPool::deleteInfo(@LDAP::fix($this->dn),_("FAI object")));
242         $smarty->assign("multiple", false);
243                                 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
244                         } else {
246                                 /* Obviously the user isn't allowed to delete. Show message and clean session. */
247         msg_dialog::display(_("Permission error"), msgPool::permDelete(),ERROR_DIALOG);
248                         }
249                 }
252     /********************
253       Delete MULTIPLE entries requested, display confirm dialog
254      ********************/
256     if ($s_action=="del_multiple"){
257       $this->dns = array();
258       $ids = $this->list_get_selected_items();
260       if(count($ids)){
262         $errors = "";
263         foreach($ids as $id){
264           $dn = $this->objects[$id]['dn'];
265           $cn = $this->objects[$id]['cn'];
266           if(!preg_match('/^freeze/', $this->objects[$id]['FAIstate'])){
267             $this->dns[$id] = $dn;
268           }else{
269             $errors .= $cn.", ";
270           }
271         }
272         if ($user= get_multiple_locks($this->dns)){
273           return(gen_locked_message($user,$this->dns));
274         }
276         if($errors != ""){
277           msg_dialog::display(_("Branch locked"),sprintf(_("The following entries are locked, you can't remove them %s."),
278             "<br><br>".trim($errors,", ")),INFO_DIALOG);
279         }
281         if(count($this->dns)){
283           $dns_names = array();
284           foreach($this->dns as $dn){
285             add_lock ($dn, $this->ui->dn);
286             $dns_names[] = @LDAP::fix($dn);
287           }
289           /* Lock the current entry, so nobody will edit it during deletion */
290                                   $smarty->assign("warning",msgPool::deleteInfo($dns_names,_("FAI object")));
291           $smarty->assign("multiple", true);
292           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
293         }
294       }
295     }
298     /********************
299       Delete MULTIPLE entries confirmed
300      ********************/
302     /* Confirmation for deletion has been passed. Users should be deleted. */
303     if (isset($_POST['delete_multiple_fai_object_confirm'])){
305       /* Find out more about the object type */
306       $ldap   = $this->config->get_ldap_link();
308       /* Remove user by user and check acls before removeing them */
309       foreach($this->dns as $key => $dn){
311         $ldap->cat($dn, array('objectClass'));
312         $attrs  = $ldap->fetch();
313         $type   = $this->get_type($attrs);
315         $acl  = $this->ui->get_permissions($dn,"fai/".$type[1]);
316         if(preg_match("/d/",$acl)){
318           $this->dialog = new $type[0]($this->config, $this->config->data['TABS'][$type[2]], $dn,"fai");
319           $this->dialog->parent = &$this;
320           $this->dialog->set_acl_base($dn);
321           $this->dialog->by_object[$type[1]]->remove_from_parent ();
322           unset ($this->dialog);
323           $this->dialog= FALSE;
324           $to_del = FAI::clean_up_releases($dn);
325           FAI::save_release_changes_now();
327           foreach($to_del as $dn){
328             $ldap->rmdir_recursive($dn);
329           }
331         } else {
333           /* Normally this shouldn't be reached, send some extra
334              logs to notify the administrator */
335           msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
336           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
337         }
338       }
340       /* Remove lock file after successfull deletion */
341       $this->remove_lock();
342       $this->dns = array();
343     }
346     /********************
347       Delete MULTIPLE entries Canceled
348      ********************/
350     /* Remove lock */
351     if(isset($_POST['delete_multiple_fai_object_cancel'])){
352       $this->dns = array();
353       $this->remove_lock();
354     }
357     /****************
358       Delete aborted  
359      ****************/
361                 /* Delete canceled? */
362                 if (isset($_POST['delete_cancel'])){
363       $this->remove_lock();
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       }
413       /* Remove lock file after successfull deletion */
414       $this->remove_lock();
415     }
418     /****************
419       Edit entry 
420      ****************/
422                 if(($s_action == "edit") && (!isset($this->dialog->config))){
423                         $entry    = $this->objects[$s_entry];
424                         $a_setup  = $this->get_type($entry);
425                         $this->dn = $entry['dn'];
427                         /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
428                         if (($user= get_lock($this->dn)) != ""){
429                                 return(gen_locked_message ($user, $this->dn));
430                         }
431                         add_lock ($this->dn, $this->ui->dn);
433                         $this->dialog     = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
434       $this->dialog->parent = &$this;
435       $this->dialog->set_acl_base($this->dn);
436                         $this->is_dialog  = true;
438       if(preg_match("/^freeze/", $entry['FAIstate']) || $this->no_save){
439 #        $this->dialog->set_acl_base("freezed")  ;    
440       }
441                         session::set('objectinfo',$this->dn);
442                 }
445     /*  Branch handling 
446         09.01.2006
447     */
449     /****************
450       Remove branch
451      ****************/
453     /* Remove branch 
454      */
455     if($s_action == "remove_branch"){
456       $base= $this->fai_release;
458       /* Check if we have a post remove method configured
459        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
460        */
461       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
462         /* Load permissions for selected 'dn' and check if
463            we're allowed to remove this 'dn' */
464         if($this->acl_is_removeable()){
465           $smarty->assign("release_hidden",base64_encode($this->fai_release));
466           $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->fai_release),_("FAI branch/freeze")));
467           return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
468         } else {
469           msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
470         }
471       }
472     }
474     
475     /****************
476       Remove branch confirmed
477      ****************/
479     if(isset($_POST['delete_branch_confirm'])){
481       /* Check if we have a post remove method configured
482        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
483        */
484       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
486         if(!isset($_POST['release_hidden']) || base64_decode($_POST['release_hidden']) != $this->fai_release){
487           msg_dialog::display(_("Warning"),_("Release remove aborted because the release name check failed!"));
488         }else{
490           $bb =  $this->fai_release;
491           if(!isset($ldap)){
492             $ldap = $this->config->get_ldap_link();
493           }
495           $br = $this->getBranches();
497           if(isset($br[$bb]) && $this->acl_is_removeable()){
498             $name = $br[$bb];
500             $ldap->cd($bb);
501             $ldap->recursive_remove();
502             $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('applicationou'), $bb));
503             $ldap->recursive_remove();
504             $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('mimetypeou'), $bb));
505             $ldap->recursive_remove();
507             /* Search for all groups with configured application menus.
508               - First search all groups, to ensure that we only remove entries form whithin groups. 
509               - The search für menu configuration for the specified release and collect all those dns.
510               - Remove entries
511              */
512             $release_ou = preg_replace("/".normalizePreg(get_ou("faiou")).".*$/","",$bb);
513             $ldap->cd($this->config->current['BASE']);
514             $ldap->search("(objectClass=posixGroup)",array("dn"));
515           
516             /* Collect all group dns 
517              */
518             $groups = array();
519             while($attrs = $ldap->fetch()){
520               $groups[] = $attrs['dn'];
521             }
523             /* Collect all group menu release dns that match the release we have removed 
524              */
525             $dns = array();
526             foreach($groups as $dn){
527               $ldap->cd($dn);
528               $ldap->search("(objectClass=FAIbranch)",array("dn"));
529               while($attrs = $ldap->fetch()){
530                 if(preg_match("/^".normalizePreg($release_ou)."/",$attrs['dn'])){
531                   $dns[] = $attrs['dn'];
532                 }
533               }
534             }
535             
536             /* Finally remove collected release dns 
537              */
538             foreach($dns as $dn){
539               $ldap->cd($dn);
540               $ldap->recursive_remove();
541             }
543             /* Post remove */
544             $this->fai_release = $this->fai_base;
545             $this->lock_name   = $name;
546             $this->lock_dn     = $bb;
547             $this->postremove();
549             $fai_filter = session::get("fai_filter");
550             $fai_filter['fai_release'] = $this->fai_release;
551             session::set("fai_filter",$fai_filter);
553             new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
554           }
555         }
556       }
557     }
560     /****************
561       Create a new branch "insert Name"
562      ****************/
564     if((isset($_POST['UseBranchName']))&&(($this->dispNewBranch)||($this->dispNewFreeze))){
565       session::set('LASTPOST',$_POST);
567       if($this->dispNewBranch){
568         $type = "branch";
569       }else{
570         $type = "freeze";
571       }
573       /* Check branch name */
574       $name = $_POST['BranchName'];
575       $is_ok = true;
576       $smarty->assign("BranchName",$name);
577       $base= $this->fai_base;
579       /* Check used characters */
580       if(!preg_match("/^[0-9a-z\.]*$/",$name)){
581         msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z\.]/"), ERROR_DIALOG);
582         $is_ok = false;
583       }
585       /* Check if this name is already in use */
586       if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
587         msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
588         $is_ok = false;
589       }
591       if($is_ok){
593         if(session::is_set('LASTPOST')){
594           $LASTPOST = session::get('LASTPOST');
595         }else{
596           $LASTPOST = array();
597         }
598         $LASTPOST['base'] = $base;
599         $LASTPOST['type'] = $type;
600         session::set('LASTPOST',$LASTPOST);
601         $smarty->assign("iframe", true);
602         $smarty->assign("plugID", $_GET['plug']);
603         $display        = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
604         return($display);
605       }
606     }
609     /****************
610       Create a new branch 
611      ****************/
613     if(isset($_GET['PerformBranch'])){
614     
615       if(!$this->acl_is_createable()){
616         msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
617       }else{
619         /* Create it know */
620         $this->dispNewBranch = false;
621         $this->dispNewFreeze = false;
623         $LASTPOST = session::get('LASTPOST');
624         $base = $LASTPOST['base'];
625         $_POST  = session::get('LASTPOST');      
626         $name = $_POST['BranchName'];
628         $type = $LASTPOST['type'];
629         $ldap = $this->config->get_ldap_link();
631         $baseToUse = $base;
632         if($this->fai_release !=  $this->fai_base){
633           $baseToUse = $this->fai_release;
634         }
636         /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
638         $CurrentReleases  = $this->getBranches();
639         $NewReleaseName   = $name;
640         if(isset($CurrentReleases[$this->fai_release])) {
641           if($this->fai_release != $this->fai_base){
642             $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
643             $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName); 
644           }else{
645             $NewReleaseName   = $name;
646           }
647         }
649         $appsrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),$baseToUse); 
650         $appdst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),"ou=".$name.",".$baseToUse) ; 
652         $mimesrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),$baseToUse); 
653         $mimedst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),"ou=".$name.",".$baseToUse) ; 
655         /* Check if source depeartments exist */
656         foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
657           $ldap->cd($this->config->current['BASE']);
658           $ldap->cat($dep);
659           if(!$ldap->count()){
660             $ldap->create_missing_trees($dep);
661           }
662         }
664         /* Print header to have styles included */
665         echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
666           <html>
667           <head>
668           <title></title>
669           <style type="text/css">@import url("themes/default/style.css");</style>
670           <script language="javascript" src="include/focus.js" type="text/javascript"></script>
671           </head>
672           <body style="background: none;margin:3px;color:black">
673           ';
675         new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
677         /* Duplicate group application releases 
678          */
679         FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
681         /* Duplicate applications 
682          */
683         $ldap->cat($appsrc,array("dn")) ;
684         if($ldap->count()){
685           $ldap->cd ($appdst);
686           $ldap->recursive_remove();
687           FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
688         }
690         /* Duplicate mime types 
691          */
692         $ldap->cat($mimesrc,array("dn")) ;
693         if($ldap->count()){
694           $ldap->cd ($mimedst);
695           $ldap->recursive_remove();
696           FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
697         }
699         $attr = array();
700         $attr['objectClass'] = array("organizationalUnit","FAIbranch");
701         $attr['ou']       = $name;
702         $attr['FAIstate'] = $type;
703         $ldap->cd($this->config->current['BASE']);
704         $ldap->cd("ou=".$name.",".$baseToUse);
705         $ldap->cat("ou=".$name.",".$baseToUse);
706         if($ldap->count()){
707           $ldap->modify($attr);
708         }else{
709           $ldap->add($attr);
710         }
712         /* Duplicate fai objects 
713          */
714         //      $ldap->cd ("ou=".$name.",".$baseToUse);
715         //      $ldap->recursive_remove();
716         //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
718         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
719           <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
720           </form></div>";
722         echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
724         /* Print footer to have valid html */
725         echo "</body></html>";
727         $this->dispNewFreeze = false; 
729         /* Postcreate */ 
731         /* Assign possible attributes */
732         $this->lock_type  = $type; 
733         $this->lock_name  = $name; 
734         $this->lock_dn    = $baseToUse;
735         $this->postcreate();
736         exit();
737       }
738     }
740     /****************
741       Display dialog to enter new Branch name
742      ****************/
744     /* Check if we have a post create method configured
745      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
746      */
747     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
748       if(($s_action == "branch_branch")||($this->dispNewBranch)){
749         if(!$this->acl_is_createable()){
750         msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
751         }else{
752           $this->dispNewBranch=true;
753           $smarty->assign("iframe",false);
754           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
755           return($display);
756         }
757       } 
758     }
760  
761     /****************
762       Display dialog to enter new Freeze name
763      ****************/
765     /* Check if we have a post create method configured
766      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
767      */
768     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
769       if(($s_action == "freeze_branch")||($this->dispNewFreeze)){
770         if(!$this->acl_is_createable()){
771           msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
772         }else{
773           $this->dispNewFreeze = true;
774           $smarty->assign("iframe",false);
775           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
776           return($display);
777         }
778       }
779     }
782     /****************
783       Create a new object 
784      ****************/
786     $types = array( "new_partition"     =>  "FAIpartitionTable",
787                     "new_script"        =>  "FAIscript",
788                     "new_hook"          =>  "FAIhook",
789                     "new_variable"      =>  "FAIvariable",
790                     "new_template"      =>  "FAItemplate",
791                     "new_package"       =>  "FAIpackageList");
792     $types_i18n = array( "new_partition"     =>  _("partition table"),
793                     "new_script"        =>  _("script"),
794                     "new_hook"          =>  _("hook"),
795                     "new_variable"      =>  _("variable"),
796                     "new_template"      =>  _("template"),
797                     "new_package"       =>  _("package list"));
799     if(isset($types[$s_action])){
800       $acl = $this->ui->get_permissions($this->fai_base,"fai/".$type_acl_mapping[$types[$s_action]]);
801       if(preg_match("/c/",$acl)){
802         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,$types[$s_action]);
803         $this->dialog->parent = &$this;
804       }else{
805         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), $types_i18n[$s_action]), ERROR_DIALOG);
806       }
807     }
809     /* New Profile */
810     if($s_action == "new_profile"){
811       $this->dn = "new" ;
813       $acl = $this->ui->get_permissions($this->fai_base,"fai/faiProfile");
814       if(preg_match("/c/",$acl)){
815         $a_setup= $this->get_type(array("objectClass"=>array("FAIprofile")));
816         $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
817         $this->dialog->set_acl_base($this->base);
818         $this->dialog->parent = &$this;
820         $this->is_dialog = false;
821       }else{
822         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
823       }
824     }
827     /****************
828       Get from ask class name dialog 
829      ****************/
831     if($s_action == "select_class_name_finished"){
832       $this->dialog->save_object();
833       if(count($this->dialog->check())!=0){
834         foreach($this->dialog->check() as $msg){
835           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
836         }               
837       }elseif(isset($this->dialog->objectClass)){
838         $this->dn = "new" ;
839         $a_setup= $this->get_type(array("objectClass"=>array($this->dialog->objectClass)));
840         $name = $this->dialog->save();
842         if(class_exists($a_setup[0])){
843           $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
844           $this->dialog->set_acl_base($this->base);
845           $this->dialog->by_object[$a_setup[1]]->cn = $name;
846           $this->dialog->parent = &$this;
847           $this->is_dialog = true;
848         }
849       }         
850     }   
853     /****************
854      Cancel dialogs 
855      ****************/
857                 if(isset($_POST['edit_cancel'])){
858                         $this->dialog=FALSE;
859                         $this->is_dialog = false;
860                         session::un_set('objectinfo');
861       $this->remove_lock();
862                 }
865     /****************
866       Save sub dialogs 
867      ****************/
869                 /* This check if the given tab could be saved 
870                  * If it was possible to save it, remove dialog object. 
871                  * If it wasn't possible, show errors and keep dialog.
872                  */
873                 if((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->dialog->config))){
874                         $this->dialog->save_object();
875       $msgs= $this->dialog->check();
876                         if(count($msgs)!=0){
877                                 foreach($msgs as $msg){
878           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
879                                 }
880                         }else{
881                                 $this->dialog->save();
882         FAI::save_release_changes_now();
883         if (!isset($_POST['edit_apply'])){
884           $this->remove_lock();
885           $this->dialog=FALSE;
886           $this->is_dialog=false;
887           session::un_set('objectinfo');
888         }else{
890           /* Reinitialize tab */
891           if($this->dialog instanceof tabs){
892             $this->dialog->re_init();
893           }
894         }
895                         }
896                 }
899     /****************
900       Display currently open dialog 
901      ****************/
903                 /* If dialog is set, but $this->is_dialog==false, then 
904                  *  only the "abort" button is shown, this are dialogs that must not be saved.  
905                  * If is_dialog == true, we are currently editing tab objects.
906                  *  Here we need both, save and cancel
907                  */ 
909                 if(is_object($this->dialog)){
910                         $display .= $this->dialog->execute();
911                         /* Don't show buttons if tab dialog requests this */
913       if(isset($this->dialog->current)){
915         $obj = $this->dialog->by_object[$this->dialog->current];
917         if((isset($obj->is_dialog) && (!$obj->is_dialog)) || (isset($obj->dialog) && (!$obj->dialog))){
919           $display.= "<p style=\"text-align:right\">\n";
920           if(!$this->no_save){
921             $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
922             $display.= "&nbsp;\n";
923             if ($this->dn != "new"){
924               $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
925               $display.= "&nbsp;\n";
926             }
927           }
928           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
929           $display.= "</p>";
930         }elseif(!isset($this->dialog->current)){
931           $display.= "<p style=\"text-align:right\">\n";
932           $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
933           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
934           $display.= "</p>";
935         }
936       }else{
937         $display.= "<p style=\"text-align:right\">\n";
938         $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
939         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
940         $display.= "</p>";
942       }
943       return($display);
944                 }
945                 
947     /****************
948       Dialog display
949      ****************/
951     /* Check if there is a snapshot dialog open */
952     $base = $this->fai_base;
953     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
954       return($str);
955     }
957     /* Display dialog with system list */
958     $this->reload();
959     $this->DivListFai->parent = &$this;
960     $this->DivListFai->execute();
961     $this->DivListFai->setEntries($this->objects);
962     return($this->DivListFai->Draw());
963         }
966   /* Return departments, that will be included within snapshot detection */
967   function get_used_snapshot_bases()
968   {
969     $tmp = array();
970     $types = array("faipartitionou","faiscriptou","faitemplateou","faihookou","faiprofileou","faivariableou","faipackageou");
971     foreach($types as $type){
972       $tmp[] = get_ou($type).$this->fai_release;
973     }
974     return($tmp);
975   }
978   /* Get available branches for current base */
979   function getBranches($base = false,$prefix = "")
980   {
981     $ret = array("/"=>$this->fai_base);
982     $ldap = $this->config->get_ldap_link();
983     if(!$base){
984       $base = $this->fai_base;
985     }
986     $tmp = FAI::get_all_releases_from_base($base,true);
987     foreach($tmp as $dn => $name){
988       $ret[$name]=$dn;
989     }
990     ksort($ret);
991     $ret = array_flip($ret);
993     return ($ret);
994   }
995   
997   function list_get_selected_items()
998   {
999     $ids = array();
1000     foreach($_POST as $name => $value){
1001       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1002         $id   = preg_replace("/^item_selected_/","",$name);
1003         $ids[$id] = $id;
1004       }
1005     }
1006     return($ids);
1007   }
1010   /* reload list of objects */
1011   function reload()
1012   {
1013     /* Variable initialisation */
1014     $str            = "";
1015     $Regex          = $this->DivListFai->Regex;
1016     $this->objects  = array();
1018     /* Get base */
1019     $base = $this->fai_base;
1020     if($this->fai_release != $this->fai_base){
1021       $br = $this->getBranches();
1022       if(isset($br[$this->fai_release])){
1023         $base = $this->fai_release;
1024       }else{
1025         $base = $this->fai_base;
1026       }
1027     }
1028     $this->base = $base;
1029     $this->set_acl_base($this->base);
1031     $this->lock_type = FAI::get_release_tag(FAI::get_release_dn($base));
1033     /* Create a new list of FAI object 
1034      * Generate List of Partitions,Hooks,Scripts,Templates,Profiles ... 
1035      */
1036     $ObjectTypes = array(
1037         "FAIpartitionTable"  => array("OU"=> get_ou('faipartitionou') , "CHKBOX"=>"ShowPartitions"  ,"ACL" => "faiPartitionTable"),
1038         "FAIpackageList"     => array("OU"=> get_ou('faipackageou')   , "CHKBOX"=>"ShowPackages"    ,"ACL" => "faiPackage"),
1039         "FAIscript"          => array("OU"=> get_ou('faiscriptou')    , "CHKBOX"=>"ShowScripts"     ,"ACL" => "faiScript"),
1040         "FAIvariable"        => array("OU"=> get_ou('faivariableou')  , "CHKBOX"=>"ShowVariables"   ,"ACL" => "faiVariable"),
1041         "FAIhook"            => array("OU"=> get_ou('faihookou')      , "CHKBOX"=>"ShowHooks"       ,"ACL" => "faiHook"),
1042         "FAIprofile"         => array("OU"=> get_ou('faiprofileou')   , "CHKBOX"=>"ShowProfiles"    ,"ACL" => "faiProfile"),
1043         "FAItemplate"        => array("OU"=> get_ou('faitemplateou')  , "CHKBOX"=>"ShowTemplates"   ,"ACL" => "faiTemplate"));
1045     $filter = "";
1046     foreach($ObjectTypes as $key => $data){
1047       if($this->DivListFai->$data['CHKBOX']){
1048         $filter.= "(objectClass=".$key.")";
1049       }
1050     }
1051     $filter = "(&(|".$filter.")(cn=$Regex))";
1052     
1053     /* Get resolved release dependencies */
1054     $tmp = FAI::get_all_objects_for_given_base($base,$filter);
1056     /* Ge listed ldap objects */
1057     $ldap = $this->config->get_ldap_link();
1058     $ldap->cd($this->config->current['BASE']);
1060     /* Get release tag 
1061        If current release is freezed, all objects are freezed to.
1062      */
1063     $ldap->cat($base);
1064     $release_attrs = $ldap->fetch();
1065     $force_freezed = FALSE;
1066     if(isset($release_attrs['FAIstate'][0]) && preg_match("/freeze/i",$release_attrs['FAIstate'][0])){
1067       $force_freezed = TRUE;
1068     }
1070     foreach($tmp as $entry){
1072       /* Get some more informations about the object */ 
1073       $ldap->cat($entry['dn'], array("cn","description","objectClass","FAIclass","FAIstate","objectClass"));
1074       $object  = $ldap->fetch();
1076       /* Walk through possible types */
1077       foreach($ObjectTypes as $type => $rest){  
1079         $acl = $this->ui->get_permissions($object['dn'],"fai/".$rest ['ACL']);
1081         if(in_array($type,$object['objectClass']) && preg_match("/r/",$acl)){
1083           /* Prepare object */
1084           unset($object['objectClass']['count']);
1085           if(!isset($object['description'][0])){
1086             $object['description'][0]="";
1087           }
1089           /* Clean up object informations */
1090           $obj                  = array();
1091           $obj['cn']                          = $object['cn'][0];
1092           $obj['dn']                          = $object['dn'];
1093           $obj['acl']                       = $acl;
1094           $obj['class']                           = $rest ['ACL'];
1095           $obj['description']   = $object['description'][0];
1096           $obj['objectClass']   = $object['objectClass'];
1098           /* Append type to this string, to be able to check if the selected 
1099            * entry is of type 'freeze' or 'branch'
1100            */
1101           if(!isset($object['FAIstate'])){
1102             
1103             /* Is this entry inherited from an earlier release? 
1104                In this case inherit the FAIstate from the release container.
1105                If this is a newly created entry (allow_freeze_object_attach == TRUE) 
1106                 then allow to edit this entry by setting its FAIstate manually to branch. 
1107              */
1108             if($this->allow_freeze_object_attach && preg_match("/".normalizePreg($this->fai_release)."$/i",$object['dn'])){
1109               $obj['FAIstate'] = "branch|overridden_by_management_reload";
1110             }else{
1111               $obj['FAIstate'] = $this->lock_type;
1112             }
1113   
1114           }else{
1115             $obj['FAIstate'] = $object['FAIstate'][0]; 
1116           }
1118           if($force_freezed && !$this->allow_freeze_object_attach){
1119             $obj['FAIstate'] = "freeze";
1120           }
1122           $this->objects[strtolower($obj['cn']).$obj['cn'].$type] = $obj;
1123           $this->objects[strtolower($obj['cn']).$obj['cn'].$type]['type']=$type;
1124         }
1125                         }
1126                 }
1128                 ksort($this->objects);
1129                 reset ($this->objects);
1130         
1131                 /* use numeric index, thats a bit more secure */        
1132                 $tmp0 = array();
1133                 foreach($this->objects as $obj){
1134                         $tmp0[]= $obj;
1135                 }
1136                 $this->objects = array();
1137                 $this->objects = $tmp0;
1138         }
1140         function remove_lock()
1141         {
1142                 if (isset($this->dn)){
1143                         del_lock ($this->dn);
1144                 }
1145     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
1146       del_lock ($this->dns);
1147     }
1148         }
1150         function get_type($array){
1151                 if(in_array("FAIpartitionTable",$array['objectClass'])){
1152                         return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
1153                 }
1154                 if(in_array("FAIscript",$array['objectClass'])){
1155                         return(array("tabsScript","faiScript","FAISCRIPTTABS"));
1156                 }
1157                 if(in_array("FAItemplate",$array['objectClass'])){
1158                         return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
1159                 }
1160                 if(in_array("FAIhook",$array['objectClass'])){
1161                         return(array("tabsHook","faiHook","FAIHOOKTABS"));
1162                 }
1163                 if(in_array("FAIvariable",$array['objectClass'])){
1164                         return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
1165                 }
1166                 if(in_array("FAIprofile",$array['objectClass'])){
1167                         return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
1168                 }
1169                 
1170                 if(in_array("FAIpackageList",$array['objectClass'])){
1171                         return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
1172                 }
1173         }
1175   function CheckNewBranchName($name,$base)
1176   {
1177     $f = $this->fai_release;
1178     if($name == ""){
1179       return(false);
1180     }elseif(in_array($name,$this->getBranches($f))) {
1181       return(false);
1182     }elseif(tests::is_department_name_reserved($name,$base)){
1183       return(false);
1184     }
1185     return(true);
1186   }
1188   function save_object()
1189   {
1190     $this->DivListFai->save_object();
1192     /* Get posted release */
1193     $r_releases = array_flip($this->getBranches());
1194     if(isset($_POST['fai_release']) && isset($r_releases[get_post('fai_release')])){
1196       /* Ensure that we have a valid release selected */
1197       if(!isset($r_releases[get_post('fai_release')])){
1198         msg_dialog::display(_("Warning"),_("The selected release is not available anymore. All triggered actions are skipped."));
1199         $_POST = array();
1200         $plug =$_GET['plug'];
1201         $_GET  = array("plug" => $plug);
1202         $this->fai_release = $this->fai_base;
1203       }else{
1204         $this->fai_release = $r_releases[get_post('fai_release')];
1205       }
1207       $fai_filter = session::get("fai_filter");
1208       $fai_filter['fai_release'] = $this->fai_release;
1209       session::set("fai_filter",$fai_filter);
1210     }
1212     if(is_object($this->CopyPasteHandler)){
1213       $this->CopyPasteHandler->save_object();
1214     }
1215   }
1218   function copyPasteHandling_from_queue($s_action,$s_entry)
1219   {
1220     /* Check if Copy & Paste is disabled */
1221     if(!is_object($this->CopyPasteHandler)){
1222       return("");
1223     }
1225     $ui = get_userinfo();
1227     /* Add a single entry to queue */
1228     if($s_action == "copy"){
1230       /* Cleanup object queue */
1231       $this->CopyPasteHandler->cleanup_queue();
1232       $entry    = $this->objects[$s_entry];
1233       $a_setup  = $this->get_type($entry);
1234       $dn = $entry['dn'];
1236       if($ui->is_copyable($dn,"fai",$a_setup[1])){
1237         $this->CopyPasteHandler->add_to_queue($dn,$s_action,$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1238       }
1239     }
1241     /* Add entries to queue */
1242     if($s_action == "copy_multiple"){
1244       /* Cleanup object queue */
1245       $this->CopyPasteHandler->cleanup_queue();
1247       /* Add new entries to CP queue */
1248       foreach($this->list_get_selected_items() as $id){
1250         /* Cleanup object queue */
1251         $entry    = $this->objects[$id];
1252         $a_setup  = $this->get_type($entry);
1253         $dn = $entry['dn'];
1255         if($s_action == "copy_multiple" && $ui->is_copyable($dn,"fai",$a_setup[1])){
1256           $this->CopyPasteHandler->add_to_queue($dn,"copy",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1257         }
1258       }
1259     }
1261     /* Start pasting entries */
1262     if($s_action == "editPaste" && !($this->lock_type == "freeze" && !$this->allow_freeze_object_attach)){
1263       $this->start_pasting_copied_objects = TRUE;
1264     }
1266     /* Return C&P dialog */
1267     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1269       /* Get dialog */
1270       $this->CopyPasteHandler->SetVar("parent",$this);
1271       $data = $this->CopyPasteHandler->execute();
1273       FAI::save_release_changes_now();
1275       /* Return dialog data */
1276       if(!empty($data)){
1277         return($data);
1278       }
1279     }
1281     /* Automatically disable status for pasting */
1282     if(!$this->CopyPasteHandler->entries_queued()){
1283       $this->start_pasting_copied_objects = FALSE;
1284     }
1285     return("");
1286   }
1289   /* Check if the given FAI class is used in this release 
1290    */
1291   static function check_class_name($oc,$name,$dn)
1292   {
1293     $base = FAI::get_release_dn($dn);
1294     $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
1295     $delete = array();
1296     $used   = array();
1297     foreach($res as $object){
1298       $used[$object['cn'][0]]= $object['cn'][0];
1299     }
1300     return($used);
1301   }
1304   /* Return plugin informations for acl handling */ 
1305   static function plInfo()
1306   {
1307     return (array( 
1308           "plShortName"   => _("FAI releases"),
1309           "plDescription" => _("FAI release management"),
1310           "plSelfModify"  => FALSE,
1311           "plDepends"     => array(),
1312           "plPriority"    => 0,
1313           "plSection"     => array("administration"),           
1314           "plCategory"    => array("fai"=> array("description" => _("FAI"),
1315                                                  "objectClass" => "FAIclass")),
1316           "plProvidedAcls"=> array()));
1317   }
1319 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1320 ?>