Code

Updated FAI management
[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_roleManagement.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 reciepes";
27   var $plIcon  = "plugins/fai/images/plugin.png";
29   // Tab definition 
30   protected $tabClass = "roletabs";
31   protected $tabType = "ROLETABS";
32   protected $aclCategory = "roles";
33   protected $aclPlugin   = "role";
34   protected $objectName   = "role";
36   //  Attributes Managed by this plugin can be used in post events
37   protected $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     $this->fai_release = $this->fai_base;
65     $this->acl_base = $this->config->current['BASE'];
66     $this->ui = $ui;
67     $this->storagePoints = array(
68         get_ou('faiPartitionRDN'),
69         get_ou('faiPackageRDN'),
70         get_ou('faiScriptRDN'),
71         get_ou('faiVariableRDN'),
72         get_ou('faiHookRDN'),
73         get_ou('faiProfileRDN'),
74         get_ou('faiTemplateRDN'));
76     // Build filter
77     if (session::global_is_set(get_class($this)."_filter")){
78       $filter= session::global_get(get_class($this)."_filter");
79     } else {
80       $filter = new filter(get_template_path("fai-filter.xml", true));
81       $filter->setObjectStorage($this->storagePoints);
82     }
83     $this->setFilter($filter);
85     // Build headpage
86     $headpage = new listing(get_template_path("fai-list.xml", true));
87     $headpage->setFilter($filter);
88     $headpage->setBase($this->fai_release);
89     $headpage->registerElementFilter("filterProperties", "faiManagement::filterProperties");
91     // Add copy&paste and snapshot handler.
92     if ($this->config->boolValueIsTrue("main", "copyPaste")){
93       $this->cpHandler = new CopyPasteHandler($this->config);
94     }
96     $this->registerAction("remove_multiple","removeEntryRequested");
97     $this->registerAction("new_profile","newEntry");
98     $this->registerAction("new_template","newEntry");
99     $this->registerAction("new_script","newEntry");
100     $this->registerAction("new_hook","newEntry");
101     $this->registerAction("new_variable","newEntry");
102     $this->registerAction("new_package","newEntry");
103     $this->registerAction("new_partition","newEntry");
104     $this->registerAction("newClassNameSelected","newClassNameSelected");
105     $this->registerAction("saveOpsiProperties","saveOpsiProperties");
106     $this->registerAction("editByGroup","editByGroup");
107     $this->registerAction("createBranch","createBranch");
108     $this->registerAction("createFreeze","createFreeze");
109     $this->registerAction("removeBranch","removeBranch");
110     $this->registerAction("removeBranchConfirmed","removeBranchConfirmed");
111     $this->registerAction("saveBranch","saveBranch");
112     $this->registerAction("PerformBranch","PerformBranch");
114     parent::__construct($config, $ui, "roles", $headpage);
115   }
118   /*! \brief  Act on copy & paste actions here.
119    */
120   function copyPasteHandler($action="",$target=array(),$all=array(),$altTabClass ="",$altTabType="",$altAclCategory="",$altAclPlugin="")
121   {
122     // Collect real dns, the listed objects are grouped by their cn
123     $headpage = $this->getHeadpage();
124     if($action == "copy"){
125       foreach($target as $t){
126         $entry = $headpage->getEntry($t);
128         // Check for valid FAI objects
129         if(in_array('FAKE_OC_FAI', $entry['objectClass'])){
130           foreach($entry['GROUPS'] as $g){
131             $type = $this->get_type($g);
132             $this->cpHandler->add_to_queue($g['dn'],"copy",$type[0],$type[2],'fai',$this);
133             @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
134           }
135         }
136       }
137     }
139     // Initiate pasting
140     if($action == "paste"){
141       $this->cpPastingStarted = TRUE;
142     }
144     // Display any c&p dialogs, eg. object modifications required before pasting.
145     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
146       $this->cpHandler->SetVar("base",$headpage->getBase());
147       $data = $this->cpHandler->execute();
148       FAI::save_release_changes_now();
149       if(!empty($data)){
150         return($data);
151       }
152     }
154     // Automatically disable pasting process since there is no entry left to paste.
155     if(!$this->cpHandler->entries_queued()){
156       $this->cpPastingStarted = FALSE;
157     }
158     return("");
159   }
162   /*! \brief  A new FAI object was requested, let the user specify a name theis object now. 
163    */
164   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
165   {
166     $types = array( 
167         "new_partition"     =>  "FAIpartitionTable",
168         "new_script"        =>  "FAIscript",
169         "new_hook"          =>  "FAIhook",
170         "new_variable"      =>  "FAIvariable",
171         "new_template"      =>  "FAItemplate",
172         "new_package"       =>  "FAIpackageList");
173     $types_i18n = array( 
174         "new_partition"     =>  _("partition table"),
175         "new_script"        =>  _("script"),
176         "new_hook"          =>  _("hook"),
177         "new_variable"      =>  _("variable"),
178         "new_template"      =>  _("template"),
179         "new_package"       =>  _("package list"));
181     if(isset($types[$action])){
182       $type_acl_mapping = array(
183           "FAIpartitionTable"  => "faiPartitionTable",
184           "FAIpackageList"     => "faiPackage",
185           "FAIscript"          => "faiScript",
186           "FAIvariable"        => "faiVariable",
187           "FAIhook"            => "faiHook",
188           "FAIprofile"         => "faiProfile",
189           "FAItemplate"        => "faiTemplate");
191       $acl = $this->ui->get_permissions($this->acl_base,"fai/".$type_acl_mapping[$types[$action]]);
192       if(preg_match("/c/",$acl)){
193         $this->dialogObject = new askClassName($this->config,$this->dn,$this->ui,$types[$action]);
194         $this->dialogObject->parent = &$this;
195       }else{
196         msg_dialog::display(_("Permission error"), 
197             sprintf(_("You have no permission to create a new %s!"), $types_i18n[$action]),      ERROR_DIALOG);
198       }
199     }
200     if($action == "new_profile"){
201       $this->dn = "new" ;
203       $acl = $this->ui->get_permissions($this->acl_base,"fai/faiProfile");
204       if(preg_match("/c/",$acl)){
205         $type= $this->get_type(array("objectClass"=>array("FAIprofile")));
206         $str= management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
207         if($str) return($str);
208         $this->tabObject->set_acl_base($this->acl_base);
209         $this->tabObject->by_object[$type[1]]->cn = $name;
210       }else{
211         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
212       }
213     }
214   }
217   /*! \brief   A new FAI class was requested and the user had a specify a name for it.
218    *           Here we check if this name is useable and then open the edit dialogs.
219    */
220   function newClassNameSelected()
221   {
222     $this->dialogObject->save_object();
223     if(count($this->dialogObject->check())!=0){
224       foreach($this->dialogObject->check() as $msg){
225         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
226       }
227     }elseif(isset($this->dialogObject->objectClass)){
228       $this->dn = "new" ;
229       $type= $this->get_type(array("objectClass"=>array($this->dialogObject->objectClass)));
230       $name = $this->dialogObject->save();
232       if(class_exists($type[0])){
233         $this->closeDialogs();
234         $str = management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
235         if($str) return($str);
236         $this->tabObject->set_acl_base($this->acl_base);
237         $this->tabObject->by_object[$type[1]]->cn = $name;
238       }
239     }
240   }
243   /*! \brief   Edit the selected entry.
244    *           If there was a FAI group clicked, display a dialog with all members of the group.
245    */
246   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
247   {
248     $headpage = $this->getHeadpage();
249     if(count($target) == 1){
250       $entry = $headpage->getEntry($target[0]);
251       if($entry){
253         // Edit Opsi objects here 
254         if(in_array("opsi_local", $entry['TYPES']) || in_array("opsi_netboot", $entry['TYPES'])){ 
255           $name = $entry['cn'];
256           $cfg = $this->opsi->get_product_properties($name);
258           $str = management::editEntry('editEntry',array($name),array(),'tabs_opsiProdConfig','OPSIPRODCONFIG','opsi');
259           if($str) return($str);
260           if(isset($this->tabObject->by_object['opsiProperties'])){
261             $this->tabObject->by_object['opsiProperties']->set_cfg($cfg);
262             $this->tabObject->by_object['opsiProperties']->set_product($name);
263             $this->skipFooter = TRUE;
264           }else{
265             trigger_error("Unknown tab, please check config.");
266           }
268         }else{
270           // Edit FAI objects here 
271           if(count($entry['GROUPS']) == 1){
272             $data = array_pop($entry['GROUPS']);
273             $type = $this->get_type($data);
274             $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
275             if($str) return($str);
276             $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
277             $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
279           }else{
280             $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"edit");
281           }
282         }
283       }
284     }
285   }
288   /*! \brief   Save changes made in opsi dialogs.
289    */
290   function saveOpsiProperties()
291   {
292     if($this->tabObject instanceof tabs_opsiProdConfig && isset($_POST['save_properties'])){
293       $this->tabObject->save_object();
294       $op    = $this->tabObject->by_object['opsiProperties'];
295       $name  = $op->get_product();
296       $cfg   = $op->get_cfg();
297       $this->opsi->set_product_properties($name,$cfg);
298       if($this->opsi->is_error()){
299         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
300       }else{
301         $this->remove_lock();
302         $this->closeDialogs();
303       }
304     }
305   }
308   /*! \brief   Someone wants to remove some object(s)
309    *            ask for a confirmation now.
310    */
311   function removeEntryRequested($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
312   {
313     $this->closeDialogs();
314     if($action == "remove_multiple"){
316       // Collect objects to delete
317       $headpage = $this->getHeadpage();
318       $to_delete = array();
319       foreach($target as $id){
320         $object = $headpage->getEntry($id);
321         if(in_array("FAKE_OC_FAI", $object['objectClass'])){
322           foreach($object['GROUPS']  as $entry){
323             array_push($to_delete, $entry);
324           }
325         }
326       }
327       return($this->removeFAIObjects($to_delete));
328     }else{
330       // Try to remove a single object, only FAI objects can be removed!
331       $headpage = $this->getHeadpage();
332       if(count($target) == 1){
333         $entry = $headpage->getEntry($target[0]);
334         if($entry && in_array("FAKE_OC_FAI",$entry['objectClass'])){
335           $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"remove");
336         }
337       }
338     }
339   }
342   /*! \brief   Someone wants to remove some object(s)
343    *            ask for a confirmation now.
344    */
345   function removeFAIObjects($to_delete)
346   {
347     // Do not allow to remove objects with state freeezed
348     $errors=array();
349     foreach($to_delete as $obj){
350       if(isset($obj['FAIstate']) && preg_match('/^freeze/', $obj['FAIstate'])){
351         $errors[] = $obj['dn'];
352       }else{
353         $this->dns[] = $obj['dn'];
354       }
355     }
356     if(count($errors)){
357       msg_dialog::display(_("Branch locked"),
358           sprintf(_("The following entries are locked, you can't remove them %s."),msgPool::buildList($errors)),INFO_DIALOG);
359     }
361     // Check entry locking
362     if(count($this->dns)){
363       if ($user= get_multiple_locks($this->dns)){
364         return(gen_locked_message($user,$this->dns));
365       }
366       if(count($this->dns)){
367         $smarty = get_smarty();
368         $dns_names = array();
369         foreach($this->dns as $dn){
370           add_lock ($dn, $this->ui->dn);
371           $dns_names[] = LDAP::fix($dn);
372         }
373         $smarty->assign("info",msgPool::deleteInfo($dns_names,_("FAI object")));
374         $smarty->assign("multiple", true);
375         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
376       }
377     }
378   }
381   /*! \brief   Entry removal is confirmed, now remove objects
382    */
383   function removeEntryConfirmed($action="",$target=array(),$all=array(),
384       $altTabClass="",$altTabType="",$altAclCategory="")
385   {
386     $ldap = $this->config->get_ldap_link();
387     $ldap->cd($this->config->current['BASE']); 
389     foreach($this->dns as $key => $dn){
390       $ldap->cat($dn);
391       if($ldap->count()){
392         $attrs = $ldap->fetch();
393         $type= $this->get_type($attrs);
394         $str = management::removeEntryConfirmed($action,array($dn),$all,$type[0],$type[2],$type[1]);
395         if($str) return($str);
397         // Now save changes
398         FAI::save_release_changes_now();
399         $to_del = FAI::clean_up_releases($dn);
400         foreach($to_del as $dn){
401           $ldap->rmdir_recursive($dn);
402         }
403       }
404     }
405   }
408   /*! \brief   Someone clicked on edit/remove for a grouped FAI object.
409    *           We are now going to display a dialog to let the user select the entry 
410    *            he wants to perform the action on.
411    */
412   function editByGroup()
413   {
414     if($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "edit"){
415       $this->dialogObject->save_object();
416       $entry = $this->dialogObject->get_selected();
417       $this->closeDialogs();
418       $data = array_pop($entry);
419       $type = $this->get_type($data);
420       $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
421       if($str) return($str);
422       $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
423       $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
424     }elseif($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "remove"){
425       $this->dialogObject->save_object();
426       $to_delete = $entry = $this->dialogObject->get_selected();
427       return($this->removeFAIObjects($to_delete));
428     }
429   }
432   /*! \brief   Save dialog/object modifications
433    */
434   protected function saveChanges()
435   {
436     $str = management::saveChanges();
437     if($str) return($str);
439     // Now save changes
440     FAI::save_release_changes_now();
441     $to_del = FAI::clean_up_releases($this->last_dn);
442     foreach($to_del as $dn){
443       $ldap->rmdir_recursive($dn);
444     }
445   }
448   /*! \brief   Save dialog/object modifications but keep the dialogs opened
449    */
450   protected function applyChanges()
451   {
452     $str = management::applyChanges();
453     if($str) return($str);
455     // Now save changes
456     FAI::save_release_changes_now();
457     $to_del = FAI::clean_up_releases($this->last_dn);
458     foreach($to_del as $dn){
459       $ldap->rmdir_recursive($dn);
460     }
461   }
464   /*! \brief   Initiates release removal
465    */
466   function removeBranch()
467   {
468     /* Check if we have a post remove method configured
469      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
470      */
471     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
472       /* Load permissions for selected 'dn' and check if
473          we're allowed to remove this 'dn' */
474       if(preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
475         $smarty=get_smarty();
476         $smarty->assign("release_hidden",base64_encode($this->fai_release));
477         $smarty->assign("info", msgPool::deleteInfo(LDAP::fix($this->fai_release),_("FAI branch/freeze")));
478         return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
479       } else {
480         msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
481       }
482     }
483   }
486   /*! \brief   Remove a release after removal was confirmed
487    */
488   function removeBranchConfirmed()
489   {
490     /* Check if we have a post remove method configured
491      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
492      */
493     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
495       if(!isset($_POST['release_hidden']) || base64_decode($_POST['release_hidden']) != $this->fai_release){
496         msg_dialog::display(_("Warning"),_("Release remove aborted because the release name check failed!"));
497       }else{
499         $bb =  $this->fai_release;
500         $ldap = $this->config->get_ldap_link();
502         $br = $this->getBranches();
504         if(isset($br[$bb]) && preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
505           $name = $br[$bb];
507           $ldap->cd($bb);
508           $ldap->recursive_remove();
509           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('applicationRDN'), $bb));
510           $ldap->recursive_remove();
511           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('mimetypeRDN'), $bb));
512           $ldap->recursive_remove();
514           /* Search for all groups with configured application menus.
515              - First search all groups, to ensure that we only remove entries form whithin groups.
516              - The search für menu configuration for the specified release and collect all those dns.
517              - Remove entries
518            */
519           $release_ou = preg_replace("/".preg_quote(get_ou("faiBaseRDN"), '/').".*$/i","",$bb);
520           $ldap->cd($this->config->current['BASE']);
521           $ldap->search("(objectClass=posixGroup)",array("dn"));
523           /* Collect all group dns
524            */
525           $groups = array();
526           while($attrs = $ldap->fetch()){
527             $groups[] = $attrs['dn'];
528           }
531           /* Collect all group menu release dns that match the release we have removed
532            */
533           $dns = array();
534           foreach($groups as $dn){
535             $ldap->cd($dn);
536             $ldap->search("(objectClass=FAIbranch)",array("dn"));
537             while($attrs = $ldap->fetch()){
538               if(preg_match("/^".preg_quote($release_ou, '/')."/",$attrs['dn'])){
539                 $dns[] = $attrs['dn'];
540               }
541             }
542           }
544           /* Finally remove collected release dns
545            */
546           foreach($dns as $dn){
547             $ldap->cd($dn);
548             $ldap->recursive_remove();
549           }
551           /* Post remove */
552           $this->fai_release = $this->fai_base;
553           $this->lock_name   = $name;
554           $this->lock_dn     = $bb;
555           $this->postremove();
557           $fai_filter = session::get("fai_filter");
558           $fai_filter['fai_release'] = $this->fai_release;
559           session::set("fai_filter",$fai_filter);
561           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
562         }
563       }
564     }
565   }
568   /*! \brief   Initiates release creation 
569    */
570   function createBranch()
571   { 
572     $smarty = get_smarty();
573     $this->dispNewBranch=true;
574     $this->dispNewFreeze=false;
575     $smarty->assign("iframe",false);
576     if(isset($_POST['BranchName'])){
577       $smarty->assign("BranchName", $_POST['BranchName']);
578     }else{
579       $smarty->assign("BranchName","");
580     }
581     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
582   }
585   /*! \brief   Initiates release creation 
586    */
587   function createFreeze()
588   { 
589     $smarty = get_smarty();
590     $this->dispNewFreeze=true;
591     $this->dispNewBranch=false;
592     $smarty->assign("iframe",false);
593     if(isset($_POST['BranchName'])){
594       $smarty->assign("BranchName", $_POST['BranchName']);
595     }else{
596       $smarty->assign("BranchName","");
597     }
598     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
599   }
602   /*! \brief   Creates a new branch
603    */
604   function PerformBranch()
605   {
606     if(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
607       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
608     }else{
610       /* In order to see error messages we have to reset the error handler.
611          Due to the exit();
612        */
613       restore_error_handler();
615       $this->dispNewBranch = false;
616       $this->dispNewFreeze = false;
618       $LASTPOST = session::get('LASTPOST');
619       $base = $LASTPOST['base'];
620       $_POST  = session::get('LASTPOST');
621       $name = $_POST['BranchName'];
623       $type = $LASTPOST['type'];
624       $ldap = $this->config->get_ldap_link();
626       $baseToUse = $base;
627       if($this->fai_release !=  $this->fai_base){
628         $baseToUse = $this->fai_release;
629       }
631       /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
633       $CurrentReleases  = $this->getBranches();
634       $NewReleaseName   = $name;
635       if(isset($CurrentReleases[$this->fai_release])) {
636         if($this->fai_release != $this->fai_base){
637           $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
638           $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName);
639         }else{
640           $NewReleaseName   = $name;
641         }
642       }
643       $appsrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),$baseToUse);
644       $appdst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),"ou=".$name.",".$baseToUse) ;
646       $mimesrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),$baseToUse);
647       $mimedst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),"ou=".$name.",".$baseToUse) ;
649       /* Check if source depeartments exist */
650       foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
651         $ldap->cd($this->config->current['BASE']);
652         $ldap->cat($dep);
653         if(!$ldap->count()){
654           $ldap->create_missing_trees($dep);
655         }
656       }
658       /* Print header to have styles included */
659       echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
660         <html>
661         <head>
662         <title></title>
663         <style type="text/css">@import url("themes/default/style.css");</style>
664         <script language="javascript" src="include/focus.js" type="text/javascript"></script>
665         </head>
666         <body style="background: none;margin:3px;color:black">
667         ';
669       new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
671       /* Duplicate group application releases
672        */
673       FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
675       /* Duplicate applications
676        */
677       $ldap->cat($appsrc,array("dn")) ;
678       if($ldap->count()){
679         $ldap->cd ($appdst);
680         $ldap->recursive_remove();
681         FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
682       }
684       /* Duplicate mime types
685        */
686       $ldap->cat($mimesrc,array("dn")) ;
687       if($ldap->count()){
688         $ldap->cd ($mimedst);
689         $ldap->recursive_remove();
690         FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
691       }
693       $attr = array();
694       $attr['objectClass'] = array("organizationalUnit","FAIbranch");
695       $attr['ou']       = $name;
696       $attr['FAIstate'] = $type;
697       $ldap->cd($this->config->current['BASE']);
698       $ldap->cd("ou=".$name.",".$baseToUse);
699       $ldap->cat("ou=".$name.",".$baseToUse);
700       if($ldap->count()){
701         $ldap->modify($attr);
702       }else{
703         $ldap->add($attr);
704       }
706       /* Duplicate fai objects
707        */
708       //      $ldap->cd ("ou=".$name.",".$baseToUse);
709       //      $ldap->recursive_remove();
710       //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
712       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
713         <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
714         <input type='hidden' name='php_c_check' value='1'>
715         </form></div>";
717       echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
719       /* Print footer to have valid html */
720       echo "</body></html>";
722       $this->dispNewFreeze = false;
724       /* Postcreate */
726       /* Assign possible attributes */
727       $this->lock_type  = $type;
728       $this->lock_name  = $name;
729       $this->lock_dn    = $baseToUse;
730       $this->postcreate();
732       /* Send daemon event to reload the fai release database
733        */
734       if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
735         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
736         if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'])){
737           $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'];
738           $tmp = new $evt['CLASS_NAME']($this->config);
739           $tmp->set_type(TRIGGERED_EVENT);
740           $tmp->add_targets(array("GOSA"));
741           $o_queue = new gosaSupportDaemon();
742           if(!$o_queue->append($tmp)){
743             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
744           }
745         }
746       }else{
747         trigger_error("Unknown class DaemonEvent / gosaSupportDaemon");
748         msg_dialog::display(_("Fatal error"),
749             "Daemon events called but classes where not accessible, DaemonEvent gosaSupportDaemon",
750             FATAL_ERROR_DIALOG);
751       }
752       exit();
753     }
754   }
757   /*! \brief   Creates a new branch, after a useable name was specified.
758    */
759   function saveBranch()
760   {
761     if($this->dispNewBranch){
762       $type = "branch";
763     }else{
764       $type = "freeze";
765     }
767     /* Check branch name */
768     $name = $_POST['BranchName'];
769     $is_ok = true;
770     $smarty = get_smarty();
771     $smarty->assign("BranchName",$name);
772     $base= $this->fai_base;
774     /* Check used characters */
775     if(!preg_match("/^[0-9a-z\.]*$/",$name)){
776       msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z\.]/"), ERROR_DIALOG);
777       $is_ok = false;
778     }
780     // Check if this name is already in use 
781     if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
782       msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
783       $is_ok = false;
784     }
786     // Handle errors
787     if(!$is_ok && $this->dispNewFreeze){
788       return($this->createFreeze());
789     }elseif(!$is_ok && $this->dispNewBranch){
790       return($this->createBranch());
791     }
793     // Now create new release
795     if(session::is_set('LASTPOST')){
796       $LASTPOST = session::get('LASTPOST');
797     }else{
798       $LASTPOST = array();
799     }
800     $LASTPOST['base'] = $base;
801     $LASTPOST['type'] = $type;
802     $LASTPOST['BranchName'] = $name;
803     session::set('LASTPOST',$LASTPOST);
804     $smarty->assign("iframe", true);
805     $smarty->assign("plugID", $_GET['plug']);
806     $display  = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
807     return($display);
809   }
813   /*! \brief   Returns a list of all releases for useable for drop down boxes.
814    *                      ou=fai... /
815    *              ou=siga,ou=fai... &nbsp; siga
816    *         ou=1,ou=siga,ou=fai... &nbsp;&nbsp; 1
817    */
818   function getReleaseList($base = "", $prefix ="")
819   {
820     $list = array();
821     if(empty($base)){
822       $base = $this->fai_base;
823       $list[$base] = "/";
824     }
826     $ldap = $this->config->get_ldap_link();
827     $ldap->ls("(objectClass=FAIbranch)",$base,array("ou","FAIstate"));
828     $cfg_rel = $this->config->search("faiManagement","DEFAULTFAIRELEASE",array("menu"));
830     while($release = $ldap->fetch()){
831       $list[$release['dn']] = $prefix.$release['ou'][0];
833       // Preset to prefered releaes if necessary  
834       if(empty($this->fai_release) && $cfg_rel == $release['dn']){
835         $this->fai_release = $release['dn'];
836       }
838       $list = array_merge($list,$this->getReleaseList($release['dn'],$prefix."&nbsp; "));
839     }
840     return($list);
841   }
844   /*! \brief   Returns a list of all releases with full release names
845    *                      ou=fai... /
846    *              ou=siga,ou=fai... siga
847    *         ou=1,ou=siga,ou=fai... siga/1
848    */
849   function getBranches($base = false,$prefix = "")
850   {
851     $ret = array("/"=>$this->fai_base);
852     $ldap = $this->config->get_ldap_link();
853     if(!$base){
854       $base = $this->fai_base;
855     }
856     $tmp = FAI::get_all_releases_from_base($base,true);
857     foreach($tmp as $dn => $name){
858       $ret[$name]=$dn;
859     }
860     ksort($ret);
861     $ret = array_flip($ret);
863     return ($ret);
864   }
867   /*! \brief   Detects object info like corresponding  tab,class,acl 
868    *    e.g.    [0]   =  tabsPartition
869    *            [1]   =  faiPartitionTable
870    *            [2]   =  FAIPARTITIONTABS
871    */
872   function get_type($array)
873   {
874     if(!isset($array['objectClass'])) return(array());
875     if(in_array("FAIpartitionTable",$array['objectClass'])){
876       return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
877     }
878     if(in_array("FAIscript",$array['objectClass'])){
879       return(array("tabsScript","faiScript","FAISCRIPTTABS"));
880     }
881     if(in_array("FAItemplate",$array['objectClass'])){
882       return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
883     }
884     if(in_array("FAIhook",$array['objectClass'])){
885       return(array("tabsHook","faiHook","FAIHOOKTABS"));
886     }
887     if(in_array("FAIvariable",$array['objectClass'])){
888       return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
889     }
890     if(in_array("FAIprofile",$array['objectClass'])){
891       return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
892     }
893     if(in_array("FAIpackageList",$array['objectClass'])){
894       return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
895     }
896     return(array());
897   }
900   /*! \brief   Checks if the given string can be used as class name
901    */
902   static function check_class_name($oc,$name,$dn)
903   {
904     $base = FAI::get_release_dn($dn);
906     if($oc == "FAIprofile"){
907       $f = "";
908       $ocs = array("FAIprofile","FAItemplate","FAIhook","FAIpartitionTable","FAIpackageList","FAIscript","FAIvariable");
909       foreach($ocs as $oc){
910         $f .= "(objectClass=".$oc.")";
911       }
912       $res  = FAI::get_all_objects_for_given_base($base,"(|".$f.")",TRUE);
913     }else{
914       $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
915     }
916     $delete = array();
917     $used   = array();
918     foreach($res as $object){
919       $used[$object['cn'][0]]= $object['cn'][0];
920     }
921     return($used);
922   }
925   /*! \brief   Checks if the given string can be used for a new release
926    */
927   function CheckNewBranchName($name,$base)
928   {
929     $f = $this->fai_release;
930     if($name == ""){
931       return(false);
932     }elseif(in_array($name,$this->getBranches($f))) {
933       return(false);
934     }elseif(tests::is_department_name_reserved($name,$base)){
935       return(false);
936     }
937     return(true);
938   }
941   /*! \brief   This filter is used to display small icons for each listed object 
942    *            instead of their typ names
943    */
944   static function filterProperties($row, $classes)
945   {
946     $objects = array(
947         "FAIpartitionTable"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png",
948           "NAME"=>_("Partition table"),"KZL"=> "PT", "VAR"=>"ShowPartitions"),
949         "FAIpackageList"     => array("IMG"=> "plugins/fai/images/fai_packages.png",
950           "NAME"=>_("Package list") ,  "KZL"=> "PL", "VAR"=>"ShowPackages"),
951         "FAIscript"          => array("IMG"=> "plugins/fai/images/fai_script.png",
952           "NAME"=>_("Scripts") ,       "KZL"=> "S",  "VAR"=>"ShowScripts"),
953         "FAIvariable"        => array("IMG"=> "plugins/fai/images/fai_variable.png",
954           "NAME"=>_("Variables") ,     "KZL"=> "V",  "VAR"=>"ShowVariables"),
955         "FAIhook"            => array("IMG"=> "plugins/fai/images/fai_hook.png",
956           "NAME"=>_("Hooks"),          "KZL"=> "H",  "VAR"=>"ShowHooks"),
957         "FAIprofile"         => array("IMG"=> "plugins/fai/images/fai_profile.png",
958           "NAME"=>_("Profile") ,       "KZL"=> "P",  "VAR"=>"ShowProfiles"),
959         "FAItemplate"        => array("IMG"=> "plugins/fai/images/fai_template.png",
960           "NAME"=>_("Templates") ,     "KZL"=> "T",  "VAR"=>"ShowTemplates"),
961         "opsi_netboot"       => array("IMG"=> "plugins/opsi/images/netboot_package.png",
962           "NAME"=>_("OPSI netboot product") ,     "KZL"=> "ON",  "VAR"=>"ShowOpsiNetboot"),
963         "opsi_local"         => array("IMG"=> "plugins/opsi/images/local_package.png",
964           "NAME"=>_("OPSI localboot product")   ,     "KZL"=> "OL",  "VAR"=>"ShowOpsiLocal"));
966     $icon_list = "";
967     foreach($objects as $type => $type_data){
968       if(in_array($type, $classes)){
969         $icon_list .= "<input type='image' src='".$type_data['IMG']."' title='".$type_data['NAME']."'
970           alt='".$type_data['KZL']."' class='center' name='edit_".$row."_".$type."'>\n";
971       }else{
972         $icon_list .= "<img src='images/empty.png' alt=' ' class='center'>\n";
973       }
974     }
976     return $icon_list;
977   }
980   /*! \brief   Overridden render method of class mangement. 
981    *            this allows us to add a release selection box.
982    */
983   function renderList()
984   {
985     $filter = $this->getFilter();
986     $headpage = $this->getHeadpage();
987     $filter->setComboBoxOptions("RELEASE",$this->getReleaseList());
989     if(isset($_POST['RELEASE'])){
990       $this->fai_release = get_post('RELEASE');
991     }
992     $headpage->setBase($this->fai_release);
993     $headpage->update();
994     $smarty = get_smarty();
995     $smarty->assign("fai_release", $this->fai_release);
996     $smarty->assign("opsi_available", is_object($this->opsi));
997     $smarty->assign("fai_base", $this->fai_base);
998     $r = $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'));
999     $c = $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'));
1000     $smarty->assign("allow_create", $c);
1001     $smarty->assign("allow_remove", $r);
1002     $display = $headpage->render();
1003     return($this->getHeader().$display);
1004   }
1007   /*! \brief   Convert POST and GET variables into actions.
1008    */
1009   function detectPostActions()
1010   {
1011     $action = management::detectPostActions();
1012     if(isset($_POST['remove_multiple'])) $action['action'] = "remove";
1013     if(isset($_POST['new_profile'])) $action['action'] = "new_profile";
1014     if(isset($_POST['new_template'])) $action['action'] = "new_template";
1015     if(isset($_POST['new_script'])) $action['action'] = "new_script";
1016     if(isset($_POST['new_hook'])) $action['action'] = "new_hook";
1017     if(isset($_POST['new_variable'])) $action['action'] = "new_variable";
1018     if(isset($_POST['new_package'])) $action['action'] = "new_package";
1019     if(isset($_POST['new_partition'])) $action['action'] = "new_partition";
1021     if(isset($_POST['save_properties'])) $action['action'] = "saveOpsiProperties";
1022     if(isset($_POST['cancel_properties'])) $action['action'] = "cancel";
1024     if(isset($_POST['edit_continue'])) $action['action'] = "newClassNameSelected";
1025     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
1027     if(isset($_POST['faiGroupHandle_cancel'])) $action['action'] = "cancel";
1028     if(isset($_POST['CancelBranchName'])) $action['action'] = "cancel";
1029     if(isset($_POST['delete_branch_confirm'])) $action['action'] = "removeBranchConfirmed";
1030     if(isset($_GET['PerformBranch'])) $action['action'] = "PerformBranch";
1031     if(isset($_POST['UseBranchName'])) $action['action'] = "saveBranch";
1032     if(isset($_POST['faiGroupHandle_apply']))  $action['action'] = "editByGroup";
1033     if(isset($_GET['act']) && $_GET['act'] == "branch_branch")  $action['action'] = "createBranch";
1034     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch")  $action['action'] = "createFreeze";
1035     if(isset($_GET['act']) && $_GET['act'] == "remove_branch")  $action['action'] = "removeBranch";
1037     foreach($_POST as $name => $value){
1038       if(preg_match("/^edit_([0-9]*)_([a-z_]*)_(x|y)/i", $name)){
1039         $id = preg_replace("/^edit_([0-9]*)_([a-z_]*)_(x|y)/i","\\1", $name);
1040         $tab = preg_replace("/^edit_([0-9]*)_([a-z_]*)_(x|y)/i","\\2", $name);
1042         $headpage = $this->getHeadpage();
1043         $entry = $headpage->entries[$id];
1045         if(in_array('FAKE_OC_FAI', $entry['objectClass'])){
1046           if(isset($headpage->entries[$id]['GROUPS'][$tab])){
1047             $data =$headpage->entries[$id]['GROUPS'][$tab];
1048             $type = $this->get_type($data);
1049             $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]); 
1050             if($str) return($str);
1051           } 
1052         }else{
1053           $str = $this->editEntry('editEntry',array($entry['dn'])); 
1054           if($str) return($str);
1055         }
1056         break;
1057       }
1058     }
1059     return($action);
1060   }
1063   static function plInfo()
1064   {
1065     return (array(
1066           "plShortName"   => _("FAI releases"),
1067           "plDescription" => _("FAI release management"),
1068           "plSelfModify"  => FALSE,
1069           "plDepends"     => array(),
1070           "plPriority"    => 0,
1071           "plSection"     => array("administration"),
1072           "plCategory"    => array("fai"=> array("description" => _("FAI"),
1073               "objectClass" => "FAIclass")),
1074           "plProvidedAcls"=> array()));
1075   }
1076
1077 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1078 ?>