Code

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