Code

Fixed faiManagement, cleaning up the FAI-releases was using a wrong entry dn, when...
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiManagement.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id: class_faiManagement.inc 14742 2009-11-04 13:18:33Z hickert $$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class faiManagement extends management
24 {
25   var $plHeadline     = "Software deployment";
26   var $plDescription  = "Manage software packages and deployment recipes";
27   var $plIcon  = "plugins/fai/images/plugin.png";
29   // Tab definition 
30   protected $tabClass = "";
31   protected $tabType = "";
32   protected $aclCategory = "";
33   protected $aclPlugin   = "";
34   protected $objectName   = "FAI object";
36   //  Attributes Managed by this plugin can be used in post events
37   public $attributes   = array("lock_type","lock_name","lock_dn");  
39   var $dispNewBranch=false;
40   var $dispNewFreeze=false;
42   var $fai_release = ""; // The currently selected release while in release management mode!
43   var $fai_base = ""; 
44   var $acl_base = ""; 
46   var $lock_type = "";
47   var $lock_dn = "";
48   var $lock_name = "";
50   var $opsi = NULL;
53   function __construct($config,$ui)
54   {
55     $this->config = $config;
57     /* Check if the opsi plugin is installed.
58      */
59     if(class_available("opsi")){
60       $this->opsi = new opsi($this->config);;
61     }
63     $this->fai_base = get_ou("faiBaseRDN").$this->config->current['BASE'];
64     $cfg_rel = $this->config->search("faiManagement","DEFAULTFAIRELEASE",array("menu"));
65     if(!empty($cfg_rel)){
66       $this->fai_release = $cfg_rel;
67     }else{
68       $this->fai_release = $this->fai_base;
69     }
71     $releases = $this->getReleaseList();
72     if(!isset($releases[$this->fai_release])){
73       $this->fai_release = $this->fai_base;
74     }
76     $this->acl_base = $this->config->current['BASE'];
77     $this->ui = $ui;
78     $this->storagePoints = array(
79         get_ou('faiPartitionRDN'),
80         get_ou('faiPackageRDN'),
81         get_ou('faiScriptRDN'),
82         get_ou('faiVariableRDN'),
83         get_ou('faiHookRDN'),
84         get_ou('faiProfileRDN'),
85         get_ou('faiTemplateRDN'));
87     // Build filter
88     if (session::global_is_set(get_class($this)."_filter")){
89       $filter= session::global_get(get_class($this)."_filter");
90     } else {
91       $filter = new filter(get_template_path("fai-filter.xml", true));
92       $filter->setObjectStorage($this->storagePoints);
93     }
94     $filter->elementValues['RELEASE'] = $this->fai_release;
95     $this->setFilter($filter);
97     // Build headpage
98     $headpage = new listing(get_template_path("fai-list.xml", true));
99     $headpage->setFilter($filter);
100     $headpage->setBase($this->fai_release);
101     $headpage->registerElementFilter("filterProperties", "faiManagement::filterProperties");
103     // Add copy&paste and snapshot handler.
104     if ($this->config->boolValueIsTrue("main", "copyPaste")){
105       $this->cpHandler = new CopyPasteHandler($this->config);
106     }
108     $this->registerAction("remove_multiple","removeEntryRequested");
109     $this->registerAction("new_profile","newEntry");
110     $this->registerAction("new_template","newEntry");
111     $this->registerAction("new_script","newEntry");
112     $this->registerAction("new_hook","newEntry");
113     $this->registerAction("new_variable","newEntry");
114     $this->registerAction("new_package","newEntry");
115     $this->registerAction("new_partition","newEntry");
116     $this->registerAction("newClassNameSelected","newClassNameSelected");
117     $this->registerAction("saveOpsiProperties","saveOpsiProperties");
118     $this->registerAction("editByGroup","editByGroup");
119     $this->registerAction("createBranch","createBranch");
120     $this->registerAction("createFreeze","createFreeze");
121     $this->registerAction("removeBranch","removeBranch");
122     $this->registerAction("removeBranchConfirmed","removeBranchConfirmed");
123     $this->registerAction("saveBranch","saveBranch");
124     $this->registerAction("PerformBranch","PerformBranch");
126     parent::__construct($config, $ui, "FAI object", $headpage);
127   }
130   /*! \brief  Act on copy & paste actions here.
131    */
132   function copyPasteHandler($action="",$target=array(),$all=array(),$altTabClass ="",$altTabType="",$altAclCategory="",$altAclPlugin="")
133   {
134     // Collect real dns, the listed objects are grouped by their cn
135     $headpage = $this->getHeadpage();
136     if($action == "copy"){
138       if(count($target) == 1){
140         // We just want to copy a single FAI object, let the user choose entries from the FAI-Group
141         $entry = $headpage->getEntry($target[0]);
142         if(count($entry['GROUPS']) == 1){
143           $data = array_pop($entry['GROUPS']);
144           $type = $this->get_type($data);
145           $this->cpHandler->add_to_queue($data['dn'],"copy",$type[0],$type[2],'fai',$this);
146           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$data['dn'],"Entry copied!");
147         }else{
148           $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"copy");
149         }
150         
151       }else{
153         // More than one group was selected, expect that the user wants to copy the complete groups.
154         foreach($target as $t){
155           $entry = $headpage->getEntry($t);
157           // Check for valid FAI objects
158           if(in_array_strict('FAKE_OC_FAI', $entry['objectClass'])){
159             foreach($entry['GROUPS'] as $g){
160               $type = $this->get_type($g);
161               $this->cpHandler->add_to_queue($g['dn'],"copy",$type[0],$type[2],'fai',$this);
162               @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$g['dn'],"Entry copied!");
163             }
164           }
165         }
166       }
167     }
169     // Initiate pasting
170     if($action == "paste"){
171       $this->cpPastingStarted = TRUE;
172     }
174     // Display any c&p dialogs, eg. object modifications required before pasting.
175     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
176       $this->cpHandler->SetVar("base",$headpage->getBase());
177       $data = $this->cpHandler->execute();
178       FAI::save_release_changes_now();
179       if(!empty($data)){
180         return($data);
181       }
182     }
184     // Automatically disable pasting process since there is no entry left to paste.
185     if(isset($this->cpHandler) && !$this->cpHandler->entries_queued()){
186       $this->cpPastingStarted = FALSE;
187     }
188     return("");
189   }
192   /*! \brief  A new FAI object was requested, let the user specify a name theis object now. 
193    */
194   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
195   {
196     $types = array( 
197         "new_partition"     =>  "FAIpartitionTable",
198         "new_script"        =>  "FAIscript",
199         "new_hook"          =>  "FAIhook",
200         "new_variable"      =>  "FAIvariable",
201         "new_template"      =>  "FAItemplate",
202         "new_package"       =>  "FAIpackageList");
203     $types_i18n = array( 
204         "new_partition"     =>  _("partition table"),
205         "new_script"        =>  _("script"),
206         "new_hook"          =>  _("hook"),
207         "new_variable"      =>  _("variable"),
208         "new_template"      =>  _("template"),
209         "new_package"       =>  _("package list"));
211     if(isset($types[$action])){
212       $type_acl_mapping = array(
213           "FAIpartitionTable"  => "faiPartitionTable",
214           "FAIpackageList"     => "faiPackage",
215           "FAIscript"          => "faiScript",
216           "FAIvariable"        => "faiVariable",
217           "FAIhook"            => "faiHook",
218           "FAIprofile"         => "faiProfile",
219           "FAItemplate"        => "faiTemplate");
221       $acl = $this->ui->get_permissions($this->acl_base,"fai/".$type_acl_mapping[$types[$action]]);
222       if(preg_match("/c/",$acl)){
223         $this->dialogObject = new askClassName($this->config,$this->dn,$this->ui,$types[$action]);
224         $this->dialogObject->parent = &$this;
225       }else{
226         msg_dialog::display(_("Permission error"), 
227             sprintf(_("You have no permission to create a new %s!"), $types_i18n[$action]),      ERROR_DIALOG);
228       }
229     }
230     if($action == "new_profile"){
231       $this->dn = "new" ;
233       $acl = $this->ui->get_permissions($this->acl_base,"fai/faiProfile");
234       if(preg_match("/c/",$acl)){
235         $type= $this->get_type(array("objectClass"=>array("FAIprofile")));
236         $str= management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
237         if($str) return($str);
238         $this->tabObject->set_acl_base($this->acl_base);
239         $this->tabObject->by_object[$type[1]]->cn = $name;
240       }else{
241         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
242       }
243     }
244   }
247   /*! \brief   A new FAI class was requested and the user had a specify a name for it.
248    *           Here we check if this name is useable and then open the edit dialogs.
249    */
250   function newClassNameSelected()
251   {
252     $this->dialogObject->save_object();
253     if(count($this->dialogObject->check())!=0){
254       foreach($this->dialogObject->check() as $msg){
255         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
256       }
257     }elseif(isset($this->dialogObject->objectClass)){
258       $this->dn = "new" ;
259       $type= $this->get_type(array("objectClass"=>array($this->dialogObject->objectClass)));
260       $name = $this->dialogObject->save();
262       if(class_exists($type[0])){
263         $this->closeDialogs();
264         $str = management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
265         if($str) return($str);
266         $this->tabObject->set_acl_base($this->acl_base);
267         $this->tabObject->by_object[$type[1]]->cn = $name;
268       }
269     }
270   }
273   /*! \brief   Edit the selected entry.
274    *           If there was a FAI group clicked, display a dialog with all members of the group.
275    */
276   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
277   {
278     $headpage = $this->getHeadpage();
279     if(count($target) == 1){
280       $entry = $headpage->getEntry($target[0]);
281       if($entry){
283         // Edit Opsi objects here 
284         if(in_array_strict("opsi_local", $entry['TYPES']) || in_array_strict("opsi_netboot", $entry['TYPES'])){ 
285           $name = $entry['cn'];
286           $cfg = $this->opsi->get_product_properties($name);
288           $str = management::editEntry('editEntry',array($name),array(),'tabs_opsiProdConfig','OPSIPRODCONFIG','opsi');
289           if($str) return($str);
290           if(isset($this->tabObject->by_object['opsiProperties'])){
291             $this->tabObject->by_object['opsiProperties']->set_cfg($cfg);
292             $this->tabObject->by_object['opsiProperties']->set_product($name);
293             $this->skipFooter = TRUE;
294           }else{
295             trigger_error("Unknown tab, please check config.");
296           }
298         }else{
300           // Edit FAI objects here 
301           if(count($entry['GROUPS']) == 1){
302             $data = array_pop($entry['GROUPS']);
303             $type = $this->get_type($data);
304             $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
305             if($str) return($str);
306             $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
307             $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
309           }else{
310             $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"edit");
311           }
312         }
313       }
314     }
315   }
318   /*! \brief   Save changes made in opsi dialogs.
319    */
320   function saveOpsiProperties()
321   {
322     if($this->tabObject instanceof tabs_opsiProdConfig && isset($_POST['save_properties'])){
323       $this->tabObject->save_object();
324       $op    = $this->tabObject->by_object['opsiProperties'];
325       $name  = $op->get_product();
326       $cfg   = $op->get_cfg();
327       $this->opsi->set_product_properties($name,$cfg);
328       if($this->opsi->is_error()){
329         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
330       }else{
331         $this->remove_lock();
332         $this->closeDialogs();
333       }
334     }
335   }
338   /*! \brief   Someone wants to remove some object(s)
339    *            ask for a confirmation now.
340    */
341   function removeEntryRequested($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
342   {
343     $this->closeDialogs();
344     if($action == "remove_multiple"){
346       // Collect objects to delete
347       $headpage = $this->getHeadpage();
348       $to_delete = array();
349       foreach($target as $id){
350         $object = $headpage->getEntry($id);
351         if(in_array_strict("FAKE_OC_FAI", $object['objectClass'])){
352           foreach($object['GROUPS']  as $entry){
353             array_push($to_delete, $entry);
354           }
355         }
356       }
357       return($this->removeFAIObjects($to_delete));
358     }else{
360       // Try to remove a single object, only FAI objects can be removed!
361       $headpage = $this->getHeadpage();
362       if(count($target) == 1){
363         $entry = $headpage->getEntry($target[0]);
364         if($entry && in_array_strict("FAKE_OC_FAI",$entry['objectClass'])){
365           $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"remove");
366         }
367       }
368     }
369   }
372   /*! \brief   Someone wants to remove some object(s)
373    *            ask for a confirmation now.
374    */
375   function removeFAIObjects($to_delete)
376   {
377     // Do not allow to remove objects with state freeezed
378     $errors = $disallowed = array();
379   
380     foreach($to_delete as $obj){
381       $type = $this->get_type($obj);
382       $acl  = $this->ui->get_permissions($obj['dn'],"fai/".$type[1]);
383       if(!preg_match("/d/",$acl)){
384         $disallowed[] = $obj['dn'];
385       }elseif(isset($obj['FAIstate']) && preg_match('/^freeze/', $obj['FAIstate'])){
386         $errors[] = $obj['dn'];
387       }else{
388         $this->dns[] = $obj['dn'];
389       }
390     }
391     if(count($errors)){
392       msg_dialog::display(_("Branch locked"),
393           sprintf(_("The following entries are locked, you can't remove them %s."),msgPool::buildList($errors)),INFO_DIALOG);
394     }
395     if(count($disallowed)){ 
396       msg_dialog::display(_("Permission error"), msgPool::permDelete($disallowed), ERROR_DIALOG);
397     }
399     // Check entry locking
400     if(count($this->dns)){
401       if ($user= get_multiple_locks($this->dns)){
402         return(gen_locked_message($user,$this->dns));
403       }
404       if(count($this->dns)){
405         $smarty = get_smarty();
406         $dns_names = array();
407         foreach($this->dns as $dn){
408           add_lock ($dn, $this->ui->dn);
409           $dns_names[] = LDAP::fix($dn);
410         }
411         $smarty->assign("info",msgPool::deleteInfo($dns_names,_("FAI object")));
412         $smarty->assign("multiple", true);
413         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
414       }
415     }
416   }
419   /*! \brief   Entry removal is confirmed, now remove objects
420    */
421   function removeEntryConfirmed($action="",$target=array(),$all=array(),
422       $altTabClass="",$altTabType="",$altAclCategory="")
423   {
424     $ldap = $this->config->get_ldap_link();
425     $ldap->cd($this->config->current['BASE']); 
427     $disallowed = array();
428     foreach($this->dns as $key => $dn){
429       $ldap->cat($dn);
430       if($ldap->count()){
431         $attrs = $ldap->fetch();
432         $type= $this->get_type($attrs);
434         $acl  = $this->ui->get_permissions($dn,"fai/".$type[1]);
435         if(preg_match("/d/",$acl)){
437           // Now save changes
438           $str = management::removeEntryConfirmed($action,array($dn),$all,$type[0],$type[2],$type[1]);
439           if(!empty($str)) return($str);
440           FAI::save_release_changes_now();
441           $to_del = FAI::clean_up_releases($dn);
442           foreach($to_del as $dn){
443             $ldap->rmdir_recursive($dn);
444           }
446         } else {
447           $disallowed[] = $dn;
448           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
449         }
450       }
451     }
453     /* Normally this shouldn't be reached, send some extra
454        logs to notify the administrator */
455     if(count($disallowed)){ 
456       msg_dialog::display(_("Permission error"), msgPool::permDelete($disallowed), ERROR_DIALOG);
457     }
458   }
461   /*! \brief   Someone clicked on edit/remove for a grouped FAI object.
462    *           We are now going to display a dialog to let the user select the entry 
463    *            he wants to perform the action on.
464    */
465   function editByGroup()
466   {
467     if($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "edit"){
468       $this->dialogObject->save_object();
469       $entry = $this->dialogObject->get_selected();
470       $this->closeDialogs();
471       $data = array_pop($entry);
472       $type = $this->get_type($data);
473       $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
474       if($str) return($str);
475       $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
476       $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
477     }elseif($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "remove"){
478       $this->dialogObject->save_object();
479       $to_delete = $entry = $this->dialogObject->get_selected();
480       if(count($to_delete)) $this->closeDialogs();
481       return($this->removeFAIObjects($to_delete));
482     }elseif($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "copy"){
483       $this->dialogObject->save_object();
484       $entries = $entry = $this->dialogObject->get_selected();
485       if(count($entries)){
486         foreach($entries as $entry){
487           $type = $this->get_type($entry);
488           $this->cpHandler->add_to_queue($entry['dn'],"copy",$type[0],$type[2],'fai',$this);
489           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry['dn'],"Entry copied!");
490         }
491         $this->closeDialogs();
492       }
493     }
494   }
497   /*! \brief   Save dialog/object modifications
498    */
499   protected function saveChanges()
500   {
501     $str = management::saveChanges();
502     if($str) return($str);
504     // Now save changes
505     FAI::save_release_changes_now();
506     $to_del = FAI::clean_up_releases($this->last_dn);
507     $ldap= $this->config->get_ldap_link();
508     foreach($to_del as $dn){
509       $ldap->rmdir_recursive($dn);
510     }
511   }
514   /*! \brief   Save dialog/object modifications but keep the dialogs opened
515    */
516   protected function applyChanges()
517   {
518     $str = management::applyChanges();
519     if($str) return($str);
521     // Now save changes
522     FAI::save_release_changes_now();
523     $to_del = FAI::clean_up_releases($this->dn);
524     foreach($to_del as $dn){
525       $ldap->rmdir_recursive($dn);
526     }
527   }
530   /*! \brief   Initiates release removal
531    */
532   function removeBranch()
533   {
534     /* Check if we have a post remove method configured
535      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
536      */
537     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
538       /* Load permissions for selected 'dn' and check if
539          we're allowed to remove this 'dn' */
540       if(preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
541         $smarty=get_smarty();
542         $smarty->assign("release_hidden",base64_encode($this->fai_release));
543         $smarty->assign("info", msgPool::deleteInfo(LDAP::fix($this->fai_release),_("FAI branch/freeze")));
544         return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
545       } else {
546         msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
547       }
548     }
549   }
552   /*! \brief   Remove a release after removal was confirmed
553    */
554   function removeBranchConfirmed()
555   {
556     /* Check if we have a post remove method configured
557      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
558      */
559     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
561       if(!isset($_POST['release_hidden']) || base64_decode($_POST['release_hidden']) != $this->fai_release){
562         msg_dialog::display(_("Warning"),_("Release remove aborted because the release name check failed!"));
563       }else{
565         $bb =  $this->fai_release;
566         $ldap = $this->config->get_ldap_link();
568         $br = $this->getBranches();
570         if(isset($br[$bb]) && preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
571           $name = $br[$bb];
573           $ldap->cd($bb);
574           $ldap->recursive_remove();
575           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('applicationRDN'), $bb));
576           $ldap->recursive_remove();
577           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('mimetypeRDN'), $bb));
578           $ldap->recursive_remove();
580           /* Search for all groups with configured application menus.
581              - First search all groups, to ensure that we only remove entries form whithin groups.
582              - The search für menu configuration for the specified release and collect all those dns.
583              - Remove entries
584            */
585           $release_ou = preg_replace("/".preg_quote(get_ou("faiBaseRDN"), '/').".*$/i","",$bb);
586           $ldap->cd($this->config->current['BASE']);
587           $ldap->search("(objectClass=posixGroup)",array("dn"));
589           /* Collect all group dns
590            */
591           $groups = array();
592           while($attrs = $ldap->fetch()){
593             $groups[] = $attrs['dn'];
594           }
597           /* Collect all group menu release dns that match the release we have removed
598            */
599           $dns = array();
600           foreach($groups as $dn){
601             $ldap->cd($dn);
602             $ldap->search("(objectClass=FAIbranch)",array("dn"));
603             while($attrs = $ldap->fetch()){
604               if(preg_match("/^".preg_quote($release_ou, '/')."/",$attrs['dn'])){
605                 $dns[] = $attrs['dn'];
606               }
607             }
608           }
610           /* Finally remove collected release dns
611            */
612           foreach($dns as $dn){
613             $ldap->cd($dn);
614             $ldap->recursive_remove();
615           }
617           /* Post remove */
618           $this->fai_release = $this->fai_base;
619           $this->lock_name   = $name;
620           $this->lock_dn     = $bb;
621           $this->handle_post_events("remove");
623           $fai_filter = session::get("fai_filter");
624           $fai_filter['fai_release'] = $this->fai_release;
625           session::set("fai_filter",$fai_filter);
627           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
628         }
629       }
630     }
631   }
634   /*! \brief   Initiates release creation 
635    */
636   function createBranch()
637   { 
638     if($this->config->search("faiManagement", "POSTCREATE",array('menu','tabs')) == ""){ 
639       msg_dialog::display(_("Configuration"), msgPool::cmdnotfound("POSTCREATE", get_class()), ERROR_DIALOG);
640     }elseif(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
641       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
642     }else{
643       $smarty = get_smarty();
644       $this->dispNewBranch=true;
645       $this->dispNewFreeze=false;
646       $smarty->assign("iframe",false);
647       if(isset($_POST['BranchName'])){
648         $smarty->assign("BranchName", $_POST['BranchName']);
649       }else{
650         $smarty->assign("BranchName","");
651       }
652       return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
653     }
654   }
657   /*! \brief   Initiates release creation 
658    */
659   function createFreeze()
660   {
661     if($this->config->search("faiManagement", "POSTCREATE",array('menu','tabs')) == ""){ 
662       msg_dialog::display(_("Configuration"), msgPool::cmdnotfound("POSTCREATE", get_class()), ERROR_DIALOG);
663     }elseif(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
664       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
665     }else{
666       $smarty = get_smarty();
667       $this->dispNewFreeze=true;
668       $this->dispNewBranch=false;
669       $smarty->assign("iframe",false);
670       if(isset($_POST['BranchName'])){
671         $smarty->assign("BranchName", $_POST['BranchName']);
672       }else{
673         $smarty->assign("BranchName","");
674       }
675       return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
676     }
677   }
680   /*! \brief   Creates a new branch
681    */
682   function PerformBranch()
683   {
684     if(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
685       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
686     }else{
688       /* In order to see error messages we have to reset the error handler.
689          Due to the exit();
690        */
691       restore_error_handler();
693       $this->dispNewBranch = false;
694       $this->dispNewFreeze = false;
696       $LASTPOST = session::get('LASTPOST');
697       $base = $LASTPOST['base'];
698       $_POST  = session::get('LASTPOST');
699       $name = $_POST['BranchName'];
701       $type = $LASTPOST['type'];
702       $ldap = $this->config->get_ldap_link();
704       $baseToUse = $base;
705       if($this->fai_release !=  $this->fai_base){
706         $baseToUse = $this->fai_release;
707       }
709       /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
711       $CurrentReleases  = $this->getBranches();
712       $NewReleaseName   = $name;
713       if(isset($CurrentReleases[$this->fai_release])) {
714         if($this->fai_release != $this->fai_base){
715           $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
716           $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName);
717         }else{
718           $NewReleaseName   = $name;
719         }
720       }
721       $appsrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),$baseToUse);
722       $appdst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),"ou=".$name.",".$baseToUse) ;
724       $mimesrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),$baseToUse);
725       $mimedst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),"ou=".$name.",".$baseToUse) ;
727       /* Check if source depeartments exist */
728       foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
729         $ldap->cd($this->config->current['BASE']);
730         $ldap->cat($dep);
731         if(!$ldap->count()){
732           $ldap->create_missing_trees($dep);
733         }
734       }
736       /* Print header to have styles included */
737       echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
738         <html>
739         <head>
740         <title></title>
741         <style type="text/css">@import url("themes/default/style.css");</style>
742         <script language="javascript" src="include/focus.js" type="text/javascript"></script>
743         </head>
744         <body style="background: none;margin:3px;color:black">
745         ';
747       new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
749       /* Duplicate group application releases
750        */
751       FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
753       /* Duplicate applications
754        */
755       $ldap->cat($appsrc,array("dn")) ;
756       if($ldap->count()){
757         $ldap->cd ($appdst);
758         $ldap->recursive_remove();
759         FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
760       }
762       /* Duplicate mime types
763        */
764       $ldap->cat($mimesrc,array("dn")) ;
765       if($ldap->count()){
766         $ldap->cd ($mimedst);
767         $ldap->recursive_remove();
768         FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
769       }
771       $attr = array();
772       $attr['objectClass'] = array("organizationalUnit","FAIbranch");
773       $attr['ou']       = $name;
774       $attr['FAIstate'] = $type;
775       $ldap->cd($this->config->current['BASE']);
776       $ldap->cd("ou=".$name.",".$baseToUse);
777       $ldap->cat("ou=".$name.",".$baseToUse);
778       if($ldap->count()){
779         $ldap->modify($attr);
780       }else{
781         $ldap->add($attr);
782       }
784       /* Duplicate fai objects
785        */
786       //      $ldap->cd ("ou=".$name.",".$baseToUse);
787       //      $ldap->recursive_remove();
788       //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
790       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
791         <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
792         <input type='hidden' name='php_c_check' value='1'>
793         </form></div>";
795       echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
797       /* Print footer to have valid html */
798       echo "</body></html>";
800       $this->dispNewFreeze = false;
802       /* Postcreate */
804       /* Assign possible attributes */
805       $this->lock_type  = $type;
806       $this->lock_name  = $name;
807       $this->lock_dn    = $baseToUse;
808       $this->handle_post_events("add");
810       /* Send daemon event to reload the fai release database
811        */
812       if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
813         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
814         if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'])){
815           $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'];
816           $tmp = new $evt['CLASS_NAME']($this->config);
817           $tmp->set_type(TRIGGERED_EVENT);
818           $tmp->add_targets(array("GOSA"));
819           $o_queue = new gosaSupportDaemon();
820           if(!$o_queue->append($tmp)){
821             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
822           }
823         }
824       }else{
825         trigger_error("Unknown class DaemonEvent / gosaSupportDaemon");
826         msg_dialog::display(_("Fatal error"),
827             "Daemon events called but classes where not accessible, DaemonEvent gosaSupportDaemon",
828             FATAL_ERROR_DIALOG);
829       }
830       exit();
831     }
832   }
835   /*! \brief   Creates a new branch, after a useable name was specified.
836    */
837   function saveBranch()
838   {
839     if($this->dispNewBranch){
840       $type = "branch";
841     }else{
842       $type = "freeze";
843     }
845     /* Check branch name */
846     $name = $_POST['BranchName'];
847     $is_ok = true;
848     $smarty = get_smarty();
849     $smarty->assign("BranchName",$name);
850     $base= $this->fai_base;
852     /* Check used characters */
853     if(!preg_match("/^[0-9a-z\.]*$/",$name)){
854       msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z\.]/"), ERROR_DIALOG);
855       $is_ok = false;
856     }
858     // Check if this name is already in use 
859     if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
860       msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
861       $is_ok = false;
862     }
864     // Handle errors
865     if(!$is_ok && $this->dispNewFreeze){
866       return($this->createFreeze());
867     }elseif(!$is_ok && $this->dispNewBranch){
868       return($this->createBranch());
869     }
871     // Now create new release
873     if(session::is_set('LASTPOST')){
874       $LASTPOST = session::get('LASTPOST');
875     }else{
876       $LASTPOST = array();
877     }
878     $LASTPOST['base'] = $base;
879     $LASTPOST['type'] = $type;
880     $LASTPOST['BranchName'] = $name;
881     session::set('LASTPOST',$LASTPOST);
882     $smarty->assign("iframe", true);
883     $smarty->assign("plugID", $_GET['plug']);
884     $display  = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
885     return($display);
887   }
891   /*! \brief   Returns a list of all releases for useable for drop down boxes.
892    *                      ou=fai... /
893    *              ou=siga,ou=fai... &nbsp; siga
894    *         ou=1,ou=siga,ou=fai... &nbsp;&nbsp; 1
895    */
896   function getReleaseList($base = "", $prefix ="")
897   {
898     $list = array();
899     if(empty($base)){
900       $base = $this->fai_base;
901       $list[$base] = "/";
902     }
904     $ldap = $this->config->get_ldap_link();
905     $ldap->ls("(objectClass=FAIbranch)",$base,array("ou","FAIstate"));
907     while($release = $ldap->fetch()){
908       $list[$release['dn']] = $prefix.$release['ou'][0];
909       $list = array_merge($list,$this->getReleaseList($release['dn'],$prefix."&nbsp; "));
910     }
911     return($list);
912   }
915   /*! \brief   Returns a list of all releases with full release names
916    *                      ou=fai... /
917    *              ou=siga,ou=fai... siga
918    *         ou=1,ou=siga,ou=fai... siga/1
919    */
920   function getBranches($base = false,$prefix = "")
921   {
922     $ret = array("/"=>$this->fai_base);
923     $ldap = $this->config->get_ldap_link();
924     if(!$base){
925       $base = $this->fai_base;
926     }
927     $tmp = FAI::get_all_releases_from_base($base,true);
928     foreach($tmp as $dn => $name){
929       $ret[$name]=$dn;
930     }
931     ksort($ret);
932     $ret = array_flip($ret);
934     return ($ret);
935   }
938   /*! \brief   Detects object info like corresponding  tab,class,acl 
939    *    e.g.    [0]   =  tabsPartition
940    *            [1]   =  faiPartitionTable
941    *            [2]   =  FAIPARTITIONTABS
942    */
943   function get_type($array)
944   {
945     if(!isset($array['objectClass'])) return(array());
946     if(in_array_strict("FAIpartitionTable",$array['objectClass'])){
947       return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
948     }
949     if(in_array_strict("FAIscript",$array['objectClass'])){
950       return(array("tabsScript","faiScript","FAISCRIPTTABS"));
951     }
952     if(in_array_strict("FAItemplate",$array['objectClass'])){
953       return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
954     }
955     if(in_array_strict("FAIhook",$array['objectClass'])){
956       return(array("tabsHook","faiHook","FAIHOOKTABS"));
957     }
958     if(in_array_strict("FAIvariable",$array['objectClass'])){
959       return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
960     }
961     if(in_array_strict("FAIprofile",$array['objectClass'])){
962       return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
963     }
964     if(in_array_strict("FAIpackageList",$array['objectClass'])){
965       return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
966     }
967     return(array());
968   }
971   /*! \brief   Checks if the given string can be used as class name
972    */
973   static function check_class_name($oc,$name,$dn)
974   {
975     $base = FAI::get_release_dn($dn);
977     if($oc == "FAIprofile"){
978       $f = "";
979       $ocs = array("FAIprofile","FAItemplate","FAIhook","FAIpartitionTable","FAIpackageList","FAIscript","FAIvariable");
980       foreach($ocs as $oc){
981         $f .= "(objectClass=".$oc.")";
982       }
983       $res  = FAI::get_all_objects_for_given_base($base,"(|".$f.")",TRUE);
984     }else{
985       $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
986     }
987     $delete = array();
988     $used   = array();
989     foreach($res as $object){
990       $used[$object['cn'][0]]= $object['cn'][0];
991     }
992     return($used);
993   }
996   /*! \brief   Checks if the given string can be used for a new release
997    */
998   function CheckNewBranchName($name,$base)
999   {
1000     $f = $this->fai_release;
1001     if($name == ""){
1002       return(false);
1003     }elseif(in_array_strict($name,$this->getBranches($f))) {
1004       return(false);
1005     }elseif(tests::is_department_name_reserved($name,$base)){
1006       return(false);
1007     }
1008     return(true);
1009   }
1012   /*! \brief   This filter is used to display small icons for each listed object 
1013    *            instead of their typ names
1014    */
1015   static function filterProperties($row, $classes)
1016   {
1017     $objects = array(
1018         "FAIpartitionTable"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png",
1019           "NAME"=>_("Partition table"),"KZL"=> "PT", "VAR"=>"ShowPartitions"),
1020         "FAIpackageList"     => array("IMG"=> "plugins/fai/images/fai_packages.png",
1021           "NAME"=>_("Package list") ,  "KZL"=> "PL", "VAR"=>"ShowPackages"),
1022         "FAIscript"          => array("IMG"=> "plugins/fai/images/fai_script.png",
1023           "NAME"=>_("Scripts") ,       "KZL"=> "S",  "VAR"=>"ShowScripts"),
1024         "FAIvariable"        => array("IMG"=> "plugins/fai/images/fai_variable.png",
1025           "NAME"=>_("Variables") ,     "KZL"=> "V",  "VAR"=>"ShowVariables"),
1026         "FAIhook"            => array("IMG"=> "plugins/fai/images/fai_hook.png",
1027           "NAME"=>_("Hooks"),          "KZL"=> "H",  "VAR"=>"ShowHooks"),
1028         "FAIprofile"         => array("IMG"=> "plugins/fai/images/fai_profile.png",
1029           "NAME"=>_("Profile") ,       "KZL"=> "P",  "VAR"=>"ShowProfiles"),
1030         "FAItemplate"        => array("IMG"=> "plugins/fai/images/fai_template.png",
1031           "NAME"=>_("Templates") ,     "KZL"=> "T",  "VAR"=>"ShowTemplates"),
1032         "opsi_netboot"       => array("IMG"=> "plugins/opsi/images/netboot_package.png",
1033           "NAME"=>_("OPSI netboot product") ,     "KZL"=> "ON",  "VAR"=>"ShowOpsiNetboot"),
1034         "opsi_local"         => array("IMG"=> "plugins/opsi/images/local_package.png",
1035           "NAME"=>_("OPSI localboot product")   ,     "KZL"=> "OL",  "VAR"=>"ShowOpsiLocal"));
1037     $icon_list = "";
1038     foreach($objects as $type => $type_data){
1039       if(in_array_strict($type, $classes)){
1040         $icon_list .= "<input type='image' src='".$type_data['IMG']."' title='".$type_data['NAME']."'
1041           alt='".$type_data['KZL']."' class='center' name='edit_".$row."_".$type."'>\n";
1042       }else{
1043         $icon_list .= "<img src='images/empty.png' alt=' ' class='center'>\n";
1044       }
1045     }
1047     return $icon_list;
1048   }
1051   /*! \brief   Overridden render method of class mangement. 
1052    *            this allows us to add a release selection box.
1053    */
1054   function renderList()
1055   {
1056     $filter = $this->getFilter();
1057     $headpage = $this->getHeadpage();
1058     $filter->setComboBoxOptions("RELEASE",$this->getReleaseList());
1060     if(isset($_POST['RELEASE'])){
1061       $this->fai_release = get_post('RELEASE');
1062     }
1063     $headpage->setBase($this->fai_release);
1064     $headpage->update();
1065     $smarty = get_smarty();
1066     $smarty->assign("fai_release", $this->fai_release);
1067     $smarty->assign("opsi_available", (is_object($this->opsi) && $this->opsi->enabled()));
1068     $smarty->assign("fai_base", $this->fai_base);
1069     $r = $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'));
1070     $c = $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'));
1071     $smarty->assign("allow_create", $c);
1072     $smarty->assign("allow_remove", $r);
1073     $display = $headpage->render();
1074     return($this->getHeader().$display);
1075   }
1078   /*! \brief   Convert POST and GET variables into actions.
1079    */
1080   function detectPostActions()
1081   {
1082     $action = management::detectPostActions();
1083     if(isset($_POST['remove_multiple'])) $action['action'] = "remove";
1084     if(isset($_POST['new_profile'])) $action['action'] = "new_profile";
1085     if(isset($_POST['new_template'])) $action['action'] = "new_template";
1086     if(isset($_POST['new_script'])) $action['action'] = "new_script";
1087     if(isset($_POST['new_hook'])) $action['action'] = "new_hook";
1088     if(isset($_POST['new_variable'])) $action['action'] = "new_variable";
1089     if(isset($_POST['new_package'])) $action['action'] = "new_package";
1090     if(isset($_POST['new_partition'])) $action['action'] = "new_partition";
1092     if(isset($_POST['save_properties'])) $action['action'] = "saveOpsiProperties";
1093     if(isset($_POST['cancel_properties'])) $action['action'] = "cancel";
1095     if(isset($_POST['edit_continue'])) $action['action'] = "newClassNameSelected";
1096     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
1098     if(isset($_POST['faiGroupHandle_cancel'])) $action['action'] = "cancel";
1099     if(isset($_POST['CancelBranchName'])) $action['action'] = "cancel";
1100     if(isset($_POST['delete_branch_confirm'])) $action['action'] = "removeBranchConfirmed";
1101     if(isset($_GET['PerformBranch'])) $action['action'] = "PerformBranch";
1102     if(isset($_POST['UseBranchName'])) $action['action'] = "saveBranch";
1103     if(isset($_POST['faiGroupHandle_apply']))  $action['action'] = "editByGroup";
1104     if(isset($_GET['act']) && $_GET['act'] == "branch_branch")  $action['action'] = "createBranch";
1105     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch")  $action['action'] = "createFreeze";
1106     if(isset($_GET['act']) && $_GET['act'] == "remove_branch")  $action['action'] = "removeBranch";
1108     foreach($_POST as $name => $value){
1109       if(preg_match("/^edit_([0-9]*)_([a-z_]*)_(x|y)/i", $name)){
1110         $id = preg_replace("/^edit_([0-9]*)_([a-z_]*)_(x|y)/i","\\1", $name);
1111         $tab = preg_replace("/^edit_([0-9]*)_([a-z_]*)_(x|y)/i","\\2", $name);
1113         $headpage = $this->getHeadpage();
1114         $entry = $headpage->entries[$id];
1116         if(in_array_strict('FAKE_OC_FAI', $entry['objectClass'])){
1117           if(isset($headpage->entries[$id]['GROUPS'][$tab])){
1118             $data =$headpage->entries[$id]['GROUPS'][$tab];
1119             $type = $this->get_type($data);
1120             $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]); 
1121             if($str) return($str);
1122           } 
1123         }else{
1124           $str = $this->editEntry('editEntry',array($entry['dn'])); 
1125           if($str) return($str);
1126         }
1127         break;
1128       }
1129     }
1130     return($action);
1131   }
1134   static function plInfo()
1135   {
1136     return (array(
1137           "plShortName"   => _("FAI releases"),
1138           "plDescription" => _("FAI release management"),
1139           "plSelfModify"  => FALSE,
1140           "plDepends"     => array(),
1141           "plPriority"    => 0,
1142           "plSection"     => array("administration"),
1143           "plCategory"    => array("fai"=> array("description" => _("FAI"),
1144               "objectClass" => "FAIclass")),
1145           "plProvidedAcls"=> array()));
1146   }
1147
1148 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1149 ?>