Code

get rid of the div stuff
[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 reciepes for Windows and Linux systems";
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";
35   protected $releaseSelector = NULL;
37   //  Attributes Managed by this plugin can be used in post events
38   public $attributes   = array("lock_type","lock_name","lock_dn");  
40   var $dispNewBranch=false;
41   var $dispNewFreeze=false;
43   var $fai_release = ""; // The currently selected release while in release management mode!
44   var $fai_base = ""; 
45   var $acl_base = ""; 
47   var $lock_type = "";
48   var $lock_dn = "";
49   var $lock_name = "";
51   var $opsi = NULL;
54   function __construct($config,$ui)
55   {
56     $this->config = $config;
58     /* Check if the opsi plugin is installed.
59      */
60     if(class_available("opsi")){
61       $this->opsi = new opsi($this->config);;
62     }
64     $this->fai_base = get_ou("faiBaseRDN").$this->config->current['BASE'];
65     $cfg_rel = $this->config->search("faiManagement","DEFAULTFAIRELEASE",array("menu"));
66     if(!empty($cfg_rel)){
67       $this->fai_release = $cfg_rel;
68     }else{
69       $this->fai_release = $this->fai_base;
70     }
72     $releases = $this->getReleaseList();
73     if(!isset($releases[$this->fai_release])){
74       $this->fai_release = $this->fai_base;
75     }
77     $this->acl_base = $this->config->current['BASE'];
78     $this->ui = $ui;
79     $this->storagePoints = array(
80         get_ou('faiPartitionRDN'),
81         get_ou('faiPackageRDN'),
82         get_ou('faiScriptRDN'),
83         get_ou('faiVariableRDN'),
84         get_ou('faiHookRDN'),
85         get_ou('faiProfileRDN'),
86         get_ou('faiTemplateRDN'));
88     // Build filter
89     if (session::global_is_set(get_class($this)."_filter")){
90       $filter= session::global_get(get_class($this)."_filter");
91     } else {
92       $filter = new filter(get_template_path("fai-filter.xml", true));
93       $filter->setObjectStorage($this->storagePoints);
94     }
95     $filter->elementValues['RELEASE'] = $this->fai_release;
96     $this->setFilter($filter);
98     // Build headpage
99     $headpage = new listing(get_template_path("fai-list.xml", true));
100     $headpage->setFilter($filter);
101     $headpage->setBase($this->fai_release);
102     $headpage->registerElementFilter("filterProperties", "faiManagement::filterProperties");
104     // Add copy&paste and snapshot handler.
105     if ($this->config->boolValueIsTrue("main", "copyPaste")){
106       $this->cpHandler = new CopyPasteHandler($this->config);
107     }
109     $this->registerAction("remove_multiple","removeEntryRequested");
110     $this->registerAction("new_profile","newEntry");
111     $this->registerAction("new_template","newEntry");
112     $this->registerAction("new_script","newEntry");
113     $this->registerAction("new_hook","newEntry");
114     $this->registerAction("new_variable","newEntry");
115     $this->registerAction("new_package","newEntry");
116     $this->registerAction("new_partition","newEntry");
117     $this->registerAction("newClassNameSelected","newClassNameSelected");
118     $this->registerAction("saveOpsiProperties","saveOpsiProperties");
119     $this->registerAction("editByGroup","editByGroup");
120     $this->registerAction("createBranch","createBranch");
121     $this->registerAction("createFreeze","createFreeze");
122     $this->registerAction("removeBranch","removeBranch");
123     $this->registerAction("removeBranchConfirmed","removeBranchConfirmed");
124     $this->registerAction("saveBranch","saveBranch");
125     $this->registerAction("PerformBranch","PerformBranch");
127     $releases = $this->getReleaseStates($this->config,$this->fai_base);
128     $this->releaseSelector = new releaseSelector($releases, $this->fai_release, $this->fai_release);
129     parent::__construct($config, $ui, "FAI object", $headpage);
130   }
133   /*! \brief  Act on copy & paste actions here.
134    */
135   function copyPasteHandler($action="",$target=array(),$all=array(),$altTabClass ="",$altTabType="",$altAclCategory="",$altAclPlugin="")
136   {
137     // Collect real dns, the listed objects are grouped by their cn
138     $headpage = $this->getHeadpage();
139     if($action == "copy"){
141       if(count($target) == 1){
143         // We just want to copy a single FAI object, let the user choose entries from the FAI-Group
144         $entry = $headpage->getEntry($target[0]);
145         if(count($entry['GROUPS']) == 1){
146           $data = array_pop($entry['GROUPS']);
147           $type = $this->get_type($data);
148           $this->cpHandler->add_to_queue($data['dn'],"copy",$type[0],$type[2],'fai',$this);
149           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$g['dn'],"Entry copied!");
150         }else{
151           $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"copy");
152         }
153         
154       }else{
156         // More than one group was selected, expect that the user wants to copy the complete groups.
157         foreach($target as $t){
158           $entry = $headpage->getEntry($t);
160           // Check for valid FAI objects
161           if(in_array('FAKE_OC_FAI', $entry['objectClass'])){
162             foreach($entry['GROUPS'] as $g){
163               $type = $this->get_type($g);
164               $this->cpHandler->add_to_queue($g['dn'],"copy",$type[0],$type[2],'fai',$this);
165               @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$g['dn'],"Entry copied!");
166             }
167           }
168         }
169       }
170     }
172     // Initiate pasting
173     if($action == "paste"){
174       $this->cpPastingStarted = TRUE;
175     }
177     // Display any c&p dialogs, eg. object modifications required before pasting.
178     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
179       $this->cpHandler->SetVar("base",$headpage->getBase());
180       $data = $this->cpHandler->execute();
181       FAI::save_release_changes_now();
182       if(!empty($data)){
183         return($data);
184       }
185     }
187     // Automatically disable pasting process since there is no entry left to paste.
188     if(isset($this->cpHandler) && !$this->cpHandler->entries_queued()){
189       $this->cpPastingStarted = FALSE;
190     }
191     return("");
192   }
195   /*! \brief  A new FAI object was requested, let the user specify a name theis object now. 
196    */
197   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
198   {
199     $types = array( 
200         "new_partition"     =>  "FAIpartitionTable",
201         "new_script"        =>  "FAIscript",
202         "new_hook"          =>  "FAIhook",
203         "new_variable"      =>  "FAIvariable",
204         "new_template"      =>  "FAItemplate",
205         "new_package"       =>  "FAIpackageList");
206     $types_i18n = array( 
207         "new_partition"     =>  _("partition table"),
208         "new_script"        =>  _("script"),
209         "new_hook"          =>  _("hook"),
210         "new_variable"      =>  _("variable"),
211         "new_template"      =>  _("template"),
212         "new_package"       =>  _("package list"));
214     if(isset($types[$action])){
215       $type_acl_mapping = array(
216           "FAIpartitionTable"  => "faiPartitionTable",
217           "FAIpackageList"     => "faiPackage",
218           "FAIscript"          => "faiScript",
219           "FAIvariable"        => "faiVariable",
220           "FAIhook"            => "faiHook",
221           "FAIprofile"         => "faiProfile",
222           "FAItemplate"        => "faiTemplate");
224       $acl = $this->ui->get_permissions($this->acl_base,"fai/".$type_acl_mapping[$types[$action]]);
225       if(preg_match("/c/",$acl)){
226         $this->dialogObject = new askClassName($this->config,$this->dn,$this->ui,$types[$action]);
227         $this->dialogObject->parent = &$this;
228       }else{
229         msg_dialog::display(_("Permission error"), 
230             sprintf(_("You have no permission to create a new %s!"), $types_i18n[$action]),      ERROR_DIALOG);
231       }
232     }
233     if($action == "new_profile"){
234       $this->dn = "new" ;
236       $acl = $this->ui->get_permissions($this->acl_base,"fai/faiProfile");
237       if(preg_match("/c/",$acl)){
238         $type= $this->get_type(array("objectClass"=>array("FAIprofile")));
239         $str= management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
240         if($str) return($str);
241         $this->tabObject->set_acl_base($this->acl_base);
242         $this->tabObject->by_object[$type[1]]->cn = $name;
243       }else{
244         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
245       }
246     }
247   }
250   /*! \brief   A new FAI class was requested and the user had a specify a name for it.
251    *           Here we check if this name is useable and then open the edit dialogs.
252    */
253   function newClassNameSelected()
254   {
255     $this->dialogObject->save_object();
256     if(count($this->dialogObject->check())!=0){
257       foreach($this->dialogObject->check() as $msg){
258         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
259       }
260     }elseif(isset($this->dialogObject->objectClass)){
261       $this->dn = "new" ;
262       $type= $this->get_type(array("objectClass"=>array($this->dialogObject->objectClass)));
263       $name = $this->dialogObject->save();
265       if(class_exists($type[0])){
266         $this->closeDialogs();
267         $str = management::newEntry('newEntry',array(),array(),$type[0],$type[2],"fai");
268         if($str) return($str);
269         $this->tabObject->set_acl_base($this->acl_base);
270         $this->tabObject->by_object[$type[1]]->cn = $name;
271       }
272     }
273   }
276   /*! \brief   Edit the selected entry.
277    *           If there was a FAI group clicked, display a dialog with all members of the group.
278    */
279   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
280   {
281     $headpage = $this->getHeadpage();
282     if(count($target) == 1){
283       $entry = $headpage->getEntry($target[0]);
284       if($entry){
286         // Edit Opsi objects here 
287         if(in_array("opsi_local", $entry['TYPES']) || in_array("opsi_netboot", $entry['TYPES'])){ 
288           $name = $entry['cn'];
289           $cfg = $this->opsi->get_product_properties($name);
291           $str = management::editEntry('editEntry',array($name),array(),'tabs_opsiProdConfig','OPSIPRODCONFIG','opsi');
292           if($str) return($str);
293           if(isset($this->tabObject->by_object['opsiProperties'])){
294             $this->tabObject->by_object['opsiProperties']->set_cfg($cfg);
295             $this->tabObject->by_object['opsiProperties']->set_product($name);
296             $this->skipFooter = TRUE;
297           }else{
298             trigger_error("Unknown tab, please check config.");
299           }
301         }else{
303           // Edit FAI objects here 
304           if(count($entry['GROUPS']) == 1){
305             $data = array_pop($entry['GROUPS']);
306             $type = $this->get_type($data);
307             $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],"fai");
308             if($str) return($str);
309             $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
310             $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
312           }else{
313             $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"edit");
314           }
315         }
316       }
317     }
318   }
321   /*! \brief   Save changes made in opsi dialogs.
322    */
323   function saveOpsiProperties()
324   {
325     if($this->tabObject instanceof tabs_opsiProdConfig && isset($_POST['save_properties'])){
326       $this->tabObject->save_object();
327       $op    = $this->tabObject->by_object['opsiProperties'];
328       $name  = $op->get_product();
329       $cfg   = $op->get_cfg();
330       $this->opsi->set_product_properties($name,$cfg);
331       if($this->opsi->is_error()){
332         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
333       }else{
334         $this->remove_lock();
335         $this->closeDialogs();
336       }
337     }
338   }
341   /*! \brief   Someone wants to remove some object(s)
342    *            ask for a confirmation now.
343    */
344   function removeEntryRequested($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
345   {
346     $this->closeDialogs();
347     if($action == "remove_multiple"){
349       // Collect objects to delete
350       $headpage = $this->getHeadpage();
351       $to_delete = array();
352       foreach($target as $id){
353         $object = $headpage->getEntry($id);
354         if(in_array("FAKE_OC_FAI", $object['objectClass'])){
355           foreach($object['GROUPS']  as $entry){
356             array_push($to_delete, $entry);
357           }
358         }
359       }
360       return($this->removeFAIObjects($to_delete));
361     }else{
363       // Try to remove a single object, only FAI objects can be removed!
364       $headpage = $this->getHeadpage();
365       if(count($target) == 1){
366         $entry = $headpage->getEntry($target[0]);
367         if($entry && in_array("FAKE_OC_FAI",$entry['objectClass'])){
368           $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"remove");
369         }
370       }
371     }
372   }
375   /*! \brief   Someone wants to remove some object(s)
376    *            ask for a confirmation now.
377    */
378   function removeFAIObjects($to_delete)
379   {
380     // Close dialogs and remove locks for currently handled dns
381     $this->cancelEdit();
383     // Do not allow to remove objects with state freeezed
384     $errors = $disallowed = array();
385   
386     foreach($to_delete as $obj){
387       $type = $this->get_type($obj);
388       $acl  = $this->ui->get_permissions($obj['dn'],"fai/".$type[1]);
389       if(!preg_match("/d/",$acl)){
390         $disallowed[] = $obj['dn'];
391       }elseif(isset($obj['FAIstate']) && preg_match('/^freeze/', $obj['FAIstate'])){
392         $errors[] = $obj['dn'];
393       }else{
394         $this->dns[] = $obj['dn'];
395       }
396     }
397     if(count($errors)){
398       msg_dialog::display(_("Branch locked"),
399           sprintf(_("The following entries are locked, you can't remove them %s."),msgPool::buildList($errors)),INFO_DIALOG);
400     }
401     if(count($disallowed)){ 
402       msg_dialog::display(_("Permission error"), msgPool::permDelete($disallowed), ERROR_DIALOG);
403     }
405     // Check entry locking
406     $smarty = get_smarty();
407     if(count($this->dns)){
408       if ($user= get_multiple_locks($this->dns)){
409         return(gen_locked_message($user,$this->dns));
410       }
411       if(count($this->dns)){
413         // Add locks
414         $dns_names = array();
415         $types = array();
416         $h = $this->getHeadpage();
418         // Build list of object -labels
419         foreach($h->objectTypes as $type){
420           $map[$type['objectClass']]= $type['label'];
421         }
423         foreach($this->dns as $dn){
424           $tmp = $h->getType($dn);
425           if(isset($map[$tmp])){
426             $dns_names[] = '('._($map[$tmp]).')&nbsp;-&nbsp;'.LDAP::fix($dn);
427           }else{
428             $dns_names[] =LDAP::fix($dn);
429           }
430         }
431         add_lock ($this->dns, $this->ui->dn);
433         $smarty->assign("info",msgPool::deleteInfo($dns_names));
434         return($smarty->fetch(get_template_path('removeEntries.tpl')));
435       }
436     }
437   }
440   /*! \brief   Entry removal is confirmed, now remove objects
441    */
442   function removeEntryConfirmed($action="",$target=array(),$all=array(),
443       $altTabClass="",$altTabType="",$altAclCategory="", $altAclPlugin="")
444   {
445     $ldap = $this->config->get_ldap_link();
446     $ldap->cd($this->config->current['BASE']); 
448     $disallowed = array();
449     foreach($this->dns as $key => $dn){
450       $ldap->cat($dn);
451       if($ldap->count()){
452         $attrs = $ldap->fetch();
453         $type= $this->get_type($attrs);
454         $acl  = $this->ui->get_permissions($dn,"fai/".$type[1]);
455         if(preg_match("/d/",$acl)){
457           // Delete the object
458           $this->dn = $dn;
459           $tabClass = $type[0];
460           $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$type[2]], 
461               $this->dn, "fai", true, true);
462           $this->tabObject->set_acl_base($this->dn);
463           $this->tabObject->parent = &$this;
464           $this->tabObject->by_object[$type[1]]->remove_from_parent ();
466           FAI::save_release_changes_now();
467           $to_del = FAI::clean_up_releases($dn);
468           foreach($to_del as $dn){
469             $ldap->rmdir_recursive($dn);
470           }
472         } else {
473           $disallowed[] = $dn;
474           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
475         }
476       }
477     }
478    
479     $this->remove_lock();
480     $this->closeDialogs();
482     /* Normally this shouldn't be reached, send some extra
483        logs to notify the administrator */
484     if(count($disallowed)){ 
485       msg_dialog::display(_("Permission error"), msgPool::permDelete($disallowed), ERROR_DIALOG);
486     }
487   }
490   /*! \brief   Someone clicked on edit/remove for a grouped FAI object.
491    *           We are now going to display a dialog to let the user select the entry 
492    *            he wants to perform the action on.
493    */
494   function editByGroup()
495   {
496     if($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "edit"){
497       $this->dialogObject->save_object();
498       $entry = $this->dialogObject->get_selected();
499       $this->closeDialogs();
500       $data = array_pop($entry);
501       $type = $this->get_type($data);
502       $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
503       if($str) return($str);
504       $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
505       $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
506     }elseif($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "remove"){
507       $this->dialogObject->save_object();
508       $to_delete = $entry = $this->dialogObject->get_selected();
509       if(count($to_delete)) $this->closeDialogs();
510       return($this->removeFAIObjects($to_delete));
511     }elseif($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "copy"){
512       $this->dialogObject->save_object();
513       $entries = $entry = $this->dialogObject->get_selected();
514       if(count($entries)){
515         foreach($entries as $entry){
516           $type = $this->get_type($entry);
517           $this->cpHandler->add_to_queue($entry['dn'],"copy",$type[0],$type[2],'fai',$this);
518           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry['dn'],"Entry copied!");
519         }
520         $this->closeDialogs();
521       }
522     }
523   }
526   /*! \brief   Save dialog/object modifications
527    */
528   protected function saveChanges()
529   {
530     $str = management::saveChanges();
531     if($str) return($str);
533     // Now save changes
534     FAI::save_release_changes_now();
535     $to_del = FAI::clean_up_releases($this->last_dn);
536     $ldap= $this->config->get_ldap_link();
537     foreach($to_del as $dn){
538       $ldap->rmdir_recursive($dn);
539     }
540   }
543   /*! \brief   Save dialog/object modifications but keep the dialogs opened
544    */
545   protected function applyChanges()
546   {
547     $str = management::applyChanges();
548     if($str) return($str);
550     // Now save changes
551     FAI::save_release_changes_now();
552     $to_del = FAI::clean_up_releases($this->last_dn);
553     foreach($to_del as $dn){
554       $ldap->rmdir_recursive($dn);
555     }
556   }
559   /*! \brief   Initiates release removal
560    */
561   function removeBranch()
562   {
563     /* Check if we have a post remove method configured
564      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
565      */
566     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
567       /* Load permissions for selected 'dn' and check if
568          we're allowed to remove this 'dn' */
569       if(preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
570         $smarty=get_smarty();
571         $smarty->assign("release_hidden",base64_encode($this->fai_release));
572         $smarty->assign("info", msgPool::deleteInfo(LDAP::fix($this->fai_release),_("FAI branch/freeze")));
573         return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
574       } else {
575         msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
576       }
577     }
578   }
581   /*! \brief   Remove a release after removal was confirmed
582    */
583   function removeBranchConfirmed()
584   {
585     /* Check if we have a post remove method configured
586      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
587      */
588     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
590       if(!isset($_POST['release_hidden']) || base64_decode($_POST['release_hidden']) != $this->fai_release){
591         msg_dialog::display(_("Warning"),_("Release remove aborted because the release name check failed!"));
592       }else{
594         $bb =  $this->fai_release;
595         $ldap = $this->config->get_ldap_link();
597         $br = $this->getBranches();
599         if(isset($br[$bb]) && preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
600           $name = $br[$bb];
602           $ldap->cd($bb);
603           $ldap->recursive_remove();
604           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('applicationRDN'), $bb));
605           $ldap->recursive_remove();
606           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('mimetypeRDN'), $bb));
607           $ldap->recursive_remove();
609           /* Search for all groups with configured application menus.
610              - First search all groups, to ensure that we only remove entries form whithin groups.
611              - The search für menu configuration for the specified release and collect all those dns.
612              - Remove entries
613            */
614           $release_ou = preg_replace("/".preg_quote(get_ou("faiBaseRDN"), '/').".*$/i","",$bb);
615           $ldap->cd($this->config->current['BASE']);
616           $ldap->search("(objectClass=posixGroup)",array("dn"));
618           /* Collect all group dns
619            */
620           $groups = array();
621           while($attrs = $ldap->fetch()){
622             $groups[] = $attrs['dn'];
623           }
626           /* Collect all group menu release dns that match the release we have removed
627            */
628           $dns = array();
629           foreach($groups as $dn){
630             $ldap->cd($dn);
631             $ldap->search("(objectClass=FAIbranch)",array("dn"));
632             while($attrs = $ldap->fetch()){
633               if(preg_match("/^".preg_quote($release_ou, '/')."/",$attrs['dn'])){
634                 $dns[] = $attrs['dn'];
635               }
636             }
637           }
639           /* Finally remove collected release dns
640            */
641           foreach($dns as $dn){
642             $ldap->cd($dn);
643             $ldap->recursive_remove();
644           }
646           /* Post remove */
647           $this->fai_release = preg_replace("/^[^,]*+,/","", $this->fai_release);
649           $this->lock_name   = $name;
650           $this->lock_dn     = $bb;
651           $this->handle_post_events('remove');
653           // Update list and release selector to point to a valid release again.
654           $releases = $this->getReleaseStates($this->config,$this->fai_base);
655           if(!isset($releases[$this->fai_release])){
656             $this->fai_release = $this->fai_base;
657           }
659           $headpage = $this->getHeadpage();
660           $headpage->setBase($this->fai_release);
661           $headpage->update();
663           $this->releaseSelector->setBases($releases);
664           $this->releaseSelector->setBase($this->fai_release);
666           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
667         }
668       }
669     }
670   }
673   /*! \brief   Initiates release creation 
674    */
675   function createBranch()
676   { 
677     if($this->config->search("faiManagement", "POSTCREATE",array('menu','tabs')) == ""){ 
678       msg_dialog::display(_("Configuration"), msgPool::cmdnotfound("POSTCREATE", get_class()), ERROR_DIALOG);
679     }elseif(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
680       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
681     }else{
682       $smarty = get_smarty();
683       $this->dispNewBranch=true;
684       $this->dispNewFreeze=false;
685       $smarty->assign("iframe",false);
686       if(isset($_POST['BranchName'])){
687         $smarty->assign("BranchName", $_POST['BranchName']);
688       }else{
689         $smarty->assign("BranchName","");
690       }
691       return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
692     }
693   }
696   /*! \brief   Initiates release creation 
697    */
698   function createFreeze()
699   {
700     if($this->config->search("faiManagement", "POSTCREATE",array('menu','tabs')) == ""){ 
701       msg_dialog::display(_("Configuration"), msgPool::cmdnotfound("POSTCREATE", get_class()), ERROR_DIALOG);
702     }elseif(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
703       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
704     }else{
705       $smarty = get_smarty();
706       $this->dispNewFreeze=true;
707       $this->dispNewBranch=false;
708       $smarty->assign("iframe",false);
709       if(isset($_POST['BranchName'])){
710         $smarty->assign("BranchName", $_POST['BranchName']);
711       }else{
712         $smarty->assign("BranchName","");
713       }
714       return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
715     }
716   }
719   /*! \brief   Creates a new branch
720    */
721   function PerformBranch()
722   {
723     if(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
724       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
725     }else{
727       /* In order to see error messages we have to reset the error handler.
728          Due to the exit();
729        */
730       restore_error_handler();
732       $this->dispNewBranch = false;
733       $this->dispNewFreeze = false;
735       $LASTPOST = session::get('LASTPOST');
736       $base = $LASTPOST['base'];
737       $_POST  = session::get('LASTPOST');
738       $name = $_POST['BranchName'];
740       $type = $LASTPOST['type'];
741       $ldap = $this->config->get_ldap_link();
743       $baseToUse = $base;
744       if($this->fai_release !=  $this->fai_base){
745         $baseToUse = $this->fai_release;
746       }
748       /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
750       $CurrentReleases  = $this->getBranches();
751       $NewReleaseName   = $name;
752       if(isset($CurrentReleases[$this->fai_release])) {
753         if($this->fai_release != $this->fai_base){
754           $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
755           $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName);
756         }else{
757           $NewReleaseName   = $name;
758         }
759       }
761       $targetReleaseDN = "ou=".$name.",".$baseToUse;
763       $appsrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),$baseToUse);
764       $appdst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),"ou=".$name.",".$baseToUse) ;
766       $mimesrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),$baseToUse);
767       $mimedst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),"ou=".$name.",".$baseToUse) ;
769       /* Check if source depeartments exist */
770       foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
771         $ldap->cd($this->config->current['BASE']);
772         $ldap->cat($dep);
773         if(!$ldap->count()){
774           $ldap->create_missing_trees($dep);
775         }
776       }
778       /* Print header to have styles included */
779       echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
780         <html>
781         <head>
782         <title></title>
783         <style type="text/css">@import url("themes/default/style.css");</style>
784         <script language="javascript" src="include/gosa.js" type="text/javascript"></script>
785         </head>
786         <body style="background: none;margin:3px;color:black">
787         ';
789       new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
791       /* Duplicate group application releases
792        */
793       FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
795       /* Duplicate applications
796        */
797       $ldap->cat($appsrc,array("dn")) ;
798       if($ldap->count()){
799         $ldap->cd ($appdst);
800         $ldap->recursive_remove();
801         FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
802       }
804       /* Duplicate mime types
805        */
806       $ldap->cat($mimesrc,array("dn")) ;
807       if($ldap->count()){
808         $ldap->cd ($mimedst);
809         $ldap->recursive_remove();
810         FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
811       }
813       $attr = array();
814       $attr['objectClass'] = array("organizationalUnit","FAIbranch");
815       $attr['ou']       = $name;
816       $attr['FAIstate'] = $type;
817       $ldap->cd($this->config->current['BASE']);
818       $ldap->cd("ou=".$name.",".$baseToUse);
819       $ldap->cat("ou=".$name.",".$baseToUse);
820       if($ldap->count()){
821         $ldap->modify($attr);
822       }else{
823         $ldap->add($attr);
824       }
826       /* Duplicate fai objects
827        */
828       //      $ldap->cd ("ou=".$name.",".$baseToUse);
829       //      $ldap->recursive_remove();
830       //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
832       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
833         <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
834         <input type='hidden' name='php_c_check' value='1'>
835         </form></div>";
837       echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
839       /* Print footer to have valid html */
840       echo "</body></html>";
842       $this->dispNewFreeze = false;
844       /* Assign possible attributes */
845       $this->lock_type  = $type;
846       $this->lock_name  = $name;
847       $this->lock_dn    = $baseToUse;
848       $this->handle_post_events('add');
850       /* Send daemon event to reload the fai release database
851        */
852       if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
853         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
854         if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'])){
855           $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'];
856           $tmp = new $evt['CLASS_NAME']($this->config);
857           $tmp->set_type(TRIGGERED_EVENT);
858           $tmp->add_targets(array("GOSA"));
859           $o_queue = new gosaSupportDaemon();
860           if(!$o_queue->append($tmp)){
861             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
862           }
863         }
864       }else{
865         trigger_error("Unknown class DaemonEvent / gosaSupportDaemon");
866         msg_dialog::display(_("Fatal error"),
867             "Daemon events called but classes where not accessible, DaemonEvent gosaSupportDaemon",
868             FATAL_ERROR_DIALOG);
869       }
870     
871       // Update list and release selector to point to the newly created release 
872       $headpage = $this->getHeadpage();
873       $this->fai_release = $targetReleaseDN;
874       $headpage->setBase($this->fai_release);
875       $headpage->update();
876  
877       $releases = $this->getReleaseStates($this->config,$this->fai_base);
878       $this->releaseSelector->setBases($releases);
879       $this->releaseSelector->setBase($this->fai_release);
880       exit();
881     }
882   }
885   /*! \brief   Creates a new branch, after a useable name was specified.
886    */
887   function saveBranch()
888   {
889     if($this->dispNewBranch){
890       $type = "branch";
891     }else{
892       $type = "freeze";
893     }
895     /* Check branch name */
896     $name = $_POST['BranchName'];
897     $is_ok = true;
898     $smarty = get_smarty();
899     $smarty->assign("BranchName",$name);
900     $base= $this->fai_base;
902     /* Check used characters */
903     if(!preg_match("/^[0-9a-z\.]*$/",$name)){
904       msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z\.]/"), ERROR_DIALOG);
905       $is_ok = false;
906     }
908     // Check if this name is already in use 
909     if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
910       msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
911       $is_ok = false;
912     }
914     // Handle errors
915     if(!$is_ok && $this->dispNewFreeze){
916       return($this->createFreeze());
917     }elseif(!$is_ok && $this->dispNewBranch){
918       return($this->createBranch());
919     }
921     // Now create new release
923     if(session::is_set('LASTPOST')){
924       $LASTPOST = session::get('LASTPOST');
925     }else{
926       $LASTPOST = array();
927     }
928     $LASTPOST['base'] = $base;
929     $LASTPOST['type'] = $type;
930     $LASTPOST['BranchName'] = $name;
931     session::set('LASTPOST',$LASTPOST);
932     $smarty->assign("iframe", true);
933     $smarty->assign("plugID", $_GET['plug']);
934     $display  = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
935     return($display);
937   }
941   /*! \brief   Returns a list of all releases for useable for drop down boxes.
942    *                      ou=fai... /
943    *              ou=siga,ou=fai... &nbsp; siga
944    *         ou=1,ou=siga,ou=fai... &nbsp;&nbsp; 1
945    */
946   function getReleaseList($base = "", $prefix ="")
947   {
948     $list = array();
949     if(empty($base)){
950       $base = $this->fai_base;
951       $list[$base] = "/";
952     }
954     $ldap = $this->config->get_ldap_link();
955     $ldap->ls("(objectClass=FAIbranch)",$base,array("ou","FAIstate"));
956     while($release = $ldap->fetch()){
957       $list[$release['dn']] = $prefix.$release['ou'][0];
958       $list = array_merge($list,$this->getReleaseList($release['dn'],$prefix."&nbsp; "));
959     }
960     return($list);
961   }
964   /*! \brief   Returns a list of all releases for the releaseSelector
965    *                      ou=fai... 
966    *              ou=siga,ou=fai... "branch"
967    *         ou=1,ou=siga,ou=fai... "freeze"
968    */
969   static function getReleaseStates($config,$base = "", $fai_base="")
970   {
971     $list = array();
972     if(empty($fai_base)){
973       $fai_base = $base;
974       $list[$base] = "";
975     }
977     $ldap = $config->get_ldap_link();
978     $ldap->ls("(objectClass=FAIbranch)",$base,array("ou","FAIstate"));
980     while($release = $ldap->fetch()){
981       $list[$release['dn']] = _("Release");
982       if(isset($release['FAIstate'][0]) && preg_match("/freeze/i",$release['FAIstate'][0])){
983         $list[$release['dn']] = _("Freeze");
984       }
985       $list = array_merge($list,faiManagement::getReleaseStates($config,$release['dn'], $fai_base));
986     }
987     return($list);
988   }
991   /*! \brief   Returns a list of all releases with full release names
992    *                      ou=fai... /
993    *              ou=siga,ou=fai... siga
994    *         ou=1,ou=siga,ou=fai... siga/1
995    */
996   function getBranches($base = false,$prefix = "")
997   {
998     $ret = array("/"=>$this->fai_base);
999     $ldap = $this->config->get_ldap_link();
1000     if(!$base){
1001       $base = $this->fai_base;
1002     }
1003     $tmp = FAI::get_all_releases_from_base($base,true);
1004     foreach($tmp as $dn => $name){
1005       $ret[$name]=$dn;
1006     }
1007     ksort($ret);
1008     $ret = array_flip($ret);
1010     return ($ret);
1011   }
1014   /*! \brief   Detects object info like corresponding  tab,class,acl 
1015    *    e.g.    [0]   =  tabsPartition
1016    *            [1]   =  faiPartitionTable
1017    *            [2]   =  FAIPARTITIONTABS
1018    */
1019   function get_type($array)
1020   {
1021     if(!isset($array['objectClass'])) return(array());
1022     if(in_array("FAIpartitionTable",$array['objectClass'])){
1023       return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
1024     }
1025     if(in_array("FAIscript",$array['objectClass'])){
1026       return(array("tabsScript","faiScript","FAISCRIPTTABS"));
1027     }
1028     if(in_array("FAItemplate",$array['objectClass'])){
1029       return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
1030     }
1031     if(in_array("FAIhook",$array['objectClass'])){
1032       return(array("tabsHook","faiHook","FAIHOOKTABS"));
1033     }
1034     if(in_array("FAIvariable",$array['objectClass'])){
1035       return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
1036     }
1037     if(in_array("FAIprofile",$array['objectClass'])){
1038       return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
1039     }
1040     if(in_array("FAIpackageList",$array['objectClass'])){
1041       return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
1042     }
1043     return(array());
1044   }
1047   /*! \brief   Checks if the given string can be used as class name
1048    */
1049   static function check_class_name($oc,$name,$dn)
1050   {
1051     $base = FAI::get_release_dn($dn);
1053     if($oc == "FAIprofile"){
1054       $f = "";
1055       $ocs = array("FAIprofile","FAItemplate","FAIhook","FAIpartitionTable","FAIpackageList","FAIscript","FAIvariable");
1056       foreach($ocs as $oc){
1057         $f .= "(objectClass=".$oc.")";
1058       }
1059       $res  = FAI::get_all_objects_for_given_base($base,"(|".$f.")",TRUE);
1060     }else{
1061       $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
1062     }
1063     $delete = array();
1064     $used   = array();
1065     foreach($res as $object){
1066       $used[$object['cn'][0]]= $object['cn'][0];
1067     }
1068     return($used);
1069   }
1072   /*! \brief   Checks if the given string can be used for a new release
1073    */
1074   function CheckNewBranchName($name,$base)
1075   {
1076     $f = $this->fai_release;
1077     if($name == ""){
1078       return(false);
1079     }elseif(in_array($name,$this->getBranches($f))) {
1080       return(false);
1081     }elseif(tests::is_department_name_reserved($name,$base)){
1082       return(false);
1083     }
1084     return(true);
1085   }
1088   /*! \brief   This filter is used to display small icons for each listed object 
1089    *            instead of their typ names
1090    */
1091   static function filterProperties($row, $classes)
1092   {
1093     $objects = array(
1094         "FAIpartitionTable"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png",
1095           "NAME"=>_("Partition table"),"KZL"=> "PT", "VAR"=>"ShowPartitions"),
1096         "FAIpackageList"     => array("IMG"=> "plugins/fai/images/fai_packages.png",
1097           "NAME"=>_("Package list") ,  "KZL"=> "PL", "VAR"=>"ShowPackages"),
1098         "FAIscript"          => array("IMG"=> "plugins/fai/images/fai_script.png",
1099           "NAME"=>_("Scripts") ,       "KZL"=> "S",  "VAR"=>"ShowScripts"),
1100         "FAIvariable"        => array("IMG"=> "plugins/fai/images/fai_variable.png",
1101           "NAME"=>_("Variables") ,     "KZL"=> "V",  "VAR"=>"ShowVariables"),
1102         "FAIhook"            => array("IMG"=> "plugins/fai/images/fai_hook.png",
1103           "NAME"=>_("Hooks"),          "KZL"=> "H",  "VAR"=>"ShowHooks"),
1104         "FAIprofile"         => array("IMG"=> "plugins/fai/images/fai_profile.png",
1105           "NAME"=>_("Profile") ,       "KZL"=> "P",  "VAR"=>"ShowProfiles"),
1106         "FAItemplate"        => array("IMG"=> "plugins/fai/images/fai_template.png",
1107           "NAME"=>_("Templates") ,     "KZL"=> "T",  "VAR"=>"ShowTemplates"),
1108         "opsi_netboot"       => array("IMG"=> "plugins/opsi/images/netboot_package.png",
1109           "NAME"=>_("OPSI netboot product") ,     "KZL"=> "ON",  "VAR"=>"ShowOpsiNetboot"),
1110         "opsi_local"         => array("IMG"=> "plugins/opsi/images/local_package.png",
1111           "NAME"=>_("OPSI localboot product")   ,     "KZL"=> "OL",  "VAR"=>"ShowOpsiLocal"));
1113     $icon_list = "";
1114     foreach($objects as $type => $type_data){
1115       if(in_array($type, $classes)){
1116         $icon_list .= image($type_data['IMG'],'edit_'.$row.'_'.$type,$type_data['NAME']);
1117       }else{
1118         $icon_list .= image('images/empty.png');
1119       }
1120     }
1122     return $icon_list;
1123   }
1126   /*! \brief   Overridden render method of class mangement. 
1127    *            this allows us to add a release selection box.
1128    */
1129   function renderList()
1130   {
1131     $filter = $this->getFilter();
1132     $headpage = $this->getHeadpage();
1133     $this->releaseSelector->update();
1134     $this->fai_release = $this->releaseSelector->getBase();
1135     $headpage->setBase($this->fai_release);
1136     $headpage->update();
1137     $smarty = get_smarty();
1138     $r = $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'));
1139     $c = $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'));
1140     $smarty->assign("allow_create", $c);
1141     $smarty->assign("allow_remove", $r);
1142     $smarty->assign("RELEASE", $this->releaseSelector->render());
1143     $display = $headpage->render();
1144     return($this->getHeader().$display);
1145   }
1148   /*! \brief   Convert POST and GET variables into actions.
1149    */
1150   function detectPostActions()
1151   {
1152     $action = management::detectPostActions();
1153     if(isset($_POST['remove_multiple'])) $action['action'] = "remove";
1154     if(isset($_POST['new_profile'])) $action['action'] = "new_profile";
1155     if(isset($_POST['new_template'])) $action['action'] = "new_template";
1156     if(isset($_POST['new_script'])) $action['action'] = "new_script";
1157     if(isset($_POST['new_hook'])) $action['action'] = "new_hook";
1158     if(isset($_POST['new_variable'])) $action['action'] = "new_variable";
1159     if(isset($_POST['new_package'])) $action['action'] = "new_package";
1160     if(isset($_POST['new_partition'])) $action['action'] = "new_partition";
1162     if(isset($_POST['save_properties'])) $action['action'] = "saveOpsiProperties";
1163     if(isset($_POST['cancel_properties'])) $action['action'] = "cancel";
1165     if(isset($_POST['edit_continue'])) $action['action'] = "newClassNameSelected";
1166     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
1168     if(isset($_POST['faiGroupHandle_cancel'])) $action['action'] = "cancel";
1169     if(isset($_POST['CancelBranchName'])) $action['action'] = "cancel";
1170     if(isset($_POST['delete_branch_confirm'])) $action['action'] = "removeBranchConfirmed";
1171     if(isset($_GET['PerformBranch'])) $action['action'] = "PerformBranch";
1172     if(isset($_POST['UseBranchName'])) $action['action'] = "saveBranch";
1173     if(isset($_POST['faiGroupHandle_apply']))  $action['action'] = "editByGroup";
1174     if(isset($_GET['act']) && $_GET['act'] == "branch_branch")  $action['action'] = "createBranch";
1175     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch")  $action['action'] = "createFreeze";
1176     if(isset($_GET['act']) && $_GET['act'] == "remove_branch")  $action['action'] = "removeBranch";
1178     foreach($_POST as $name => $value){
1179       if(preg_match("/^edit_([0-9]*)_([a-z_]*)/i", $name)){
1180         $id = preg_replace("/^edit_([0-9]*)_([a-z_]*)/i","\\1", $name);
1181         $tab = preg_replace("/^edit_([0-9]*)_([a-z_]*)/i","\\2", $name);
1183         $headpage = $this->getHeadpage();
1184         $entry = $headpage->entries[$id];
1186         if(in_array('FAKE_OC_FAI', $entry['objectClass'])){
1187           unset($_POST[$name]);
1188           if(isset($headpage->entries[$id]['GROUPS'][$tab])){
1189             $data =$headpage->entries[$id]['GROUPS'][$tab];
1190             $type = $this->get_type($data);
1191             $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],"fai"); 
1192             if($str) return($str);
1193           } 
1194         }else{
1195           $str = $this->editEntry('editEntry',array($entry['dn'])); 
1196           if($str) return($str);
1197         }
1198         break;
1199       }
1200     }
1201     return($action);
1202   }
1205   static function plInfo()
1206   {
1207     return (array(
1208           "plShortName"   => _("FAI releases"),
1209           "plDescription" => _("FAI release management"),
1210           "plSelfModify"  => FALSE,
1211           "plDepends"     => array(),
1212           "plPriority"    => 0,
1213           "plSection"     => array("administration"),
1214           "plCategory"    => array("fai"=> array("description" => _("FAI"),
1215               "objectClass" => "FAIclass")),
1216           "plProvidedAcls"=> array()));
1217   }
1218
1219 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1220 ?>