Code

a33359b6b78e3778177389c4cac0ed03b74f3b88
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiManagement.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id: class_roleManagement.inc 14742 2009-11-04 13:18:33Z hickert $$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class faiManagement extends management
24 {
25   var $plHeadline     = "Software deployment";
26   var $plDescription  = "Manage software packages and deployment reciepes";
27   var $plIcon  = "plugins/fai/images/plugin.png";
29   // Tab definition 
30   protected $tabClass = "roletabs";
31   protected $tabType = "ROLETABS";
32   protected $aclCategory = "roles";
33   protected $aclPlugin   = "role";
34   protected $objectName   = "role";
36   //  Attributes Managed by this plugin can be used in post events
37   protected $attributes   = array("lock_type","lock_name","lock_dn");  
39   var $dispNewBranch=false;
40   var $dispNewFreeze=false;
42   var $fai_release = ""; // The currently selected release while in release management mode!
43   var $fai_base = ""; 
44   var $acl_base = ""; 
46   var $lock_type = "";
47   var $lock_dn = "";
48   var $lock_name = "";
50   var $opsi = NULL;
53   function __construct($config,$ui)
54   {
55     $this->config = $config;
57     /* Check if the opsi plugin is installed.
58      */
59     if(class_available("opsi")){
60       $this->opsi = new opsi($this->config);;
61     }
63     $this->fai_base = get_ou("faiBaseRDN").$this->config->current['BASE'];
64     $this->fai_release = $this->fai_base;
65     $this->acl_base = $this->config->current['BASE'];
66     $this->ui = $ui;
67     $this->storagePoints = array(
68         get_ou('faiPartitionRDN'),
69         get_ou('faiPackageRDN'),
70         get_ou('faiScriptRDN'),
71         get_ou('faiVariableRDN'),
72         get_ou('faiHookRDN'),
73         get_ou('faiProfileRDN'),get_ou('faiTemplateRDN'));
75 #    // Build filter
76 #    if (session::global_is_set(get_class($this)."_filter")){
77 #      $filter= session::global_get(get_class($this)."_filter");
78 #    } else {
79       $filter = new filter(get_template_path("fai-filter.xml", true));
80       $filter->setObjectStorage($this->storagePoints);
81 #    }
82     $this->setFilter($filter);
84     // Build headpage
85     $headpage = new listing(get_template_path("fai-list.xml", true));
86     $headpage->setFilter($filter);
87     $headpage->setBase($this->fai_release);
88     $headpage->registerElementFilter("filterProperties", "faiManagement::filterProperties");
90     // Add copy&paste and snapshot handler.
91     if ($this->config->boolValueIsTrue("main", "copyPaste")){
92       $this->cpHandler = new CopyPasteHandler($this->config);
93     }
94     if($this->config->get_cfg_value("enableSnapshots") == "true"){
95       $this->snapHandler = new SnapshotHandler($this->config);
96     }
98     $this->registerAction("new_profile","newEntry");
99     $this->registerAction("new_template","newEntry");
100     $this->registerAction("new_script","newEntry");
101     $this->registerAction("new_hook","newEntry");
102     $this->registerAction("new_variable","newEntry");
103     $this->registerAction("new_package","newEntry");
104     $this->registerAction("new_partition","newEntry");
106     $this->registerAction("newClassNameSelected","newClassNameSelected");
108     $this->registerAction("saveOpsiProperties","saveOpsiProperties");
110     $this->registerAction("editByGroup","editByGroup");
111     $this->registerAction("createBranch","createBranch");
112     $this->registerAction("createFreeze","createFreeze");
113     $this->registerAction("removeBranch","removeBranch");
114     $this->registerAction("removeBranchConfirmed","removeBranchConfirmed");
115     $this->registerAction("saveBranch","saveBranch");
116     $this->registerAction("PerformBranch","PerformBranch");
117     parent::__construct($config, $ui, "roles", $headpage);
118   }
121   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
122   {
123     /****************
124       Create a new object
125      ****************/
127     $types = array( 
128         "new_partition"     =>  "FAIpartitionTable",
129         "new_script"        =>  "FAIscript",
130         "new_hook"          =>  "FAIhook",
131         "new_variable"      =>  "FAIvariable",
132         "new_template"      =>  "FAItemplate",
133         "new_package"       =>  "FAIpackageList");
134     $types_i18n = array( 
135         "new_partition"     =>  _("partition table"),
136         "new_script"        =>  _("script"),
137         "new_hook"          =>  _("hook"),
138         "new_variable"      =>  _("variable"),
139         "new_template"      =>  _("template"),
140         "new_package"       =>  _("package list"));
142     if(isset($types[$action])){
143       $type_acl_mapping = array(
144           "FAIpartitionTable"  => "faiPartitionTable",
145           "FAIpackageList"     => "faiPackage",
146           "FAIscript"          => "faiScript",
147           "FAIvariable"        => "faiVariable",
148           "FAIhook"            => "faiHook",
149           "FAIprofile"         => "faiProfile",
150           "FAItemplate"        => "faiTemplate");
152       $acl = $this->ui->get_permissions($this->acl_base,"fai/".$type_acl_mapping[$types[$action]]);
153       if(preg_match("/c/",$acl)){
154         $this->dialogObject = new askClassName($this->config,$this->dn,$this->ui,$types[$action]);
155         $this->dialogObject->parent = &$this;
156       }else{
157         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), $types_i18n[$action]),      ERROR_DIALOG);
158       }
159     }
160     if($action == "new_profile"){
161       $this->dn = "new" ;
163       $acl = $this->ui->get_permissions($this->acl_base,"fai/faiProfile");
164       if(preg_match("/c/",$acl)){
165         $type= $this->get_type(array("objectClass"=>array("FAIprofile")));
166         $str= management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
167         if($str) return($str);
168         $this->tabObject->set_acl_base($this->acl_base);
169         $this->tabObject->by_object[$type[1]]->cn = $name;
170       }else{
171         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
172       }
173     }
174   }
177   function newClassNameSelected()
178   {
179     $this->dialogObject->save_object();
180     if(count($this->dialogObject->check())!=0){
181       foreach($this->dialogObject->check() as $msg){
182         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
183       }
184     }elseif(isset($this->dialogObject->objectClass)){
185       $this->dn = "new" ;
186       $type= $this->get_type(array("objectClass"=>array($this->dialogObject->objectClass)));
187       $name = $this->dialogObject->save();
189       if(class_exists($type[0])){
190         $this->closeDialogs();
191         $str = management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
192         if($str) return($str);
193         $this->tabObject->set_acl_base($this->acl_base);
194         $this->tabObject->by_object[$type[1]]->cn = $name;
195       }
196     }
197   }
201   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
202   {
203     $headpage = $this->getHeadpage();
204     if(count($target) == 1){
205       $entry = $headpage->getEntry($target[0]);
206       if($entry){
208         if(in_array("opsi_local", $entry['TYPES']) || in_array("opsi_netboot", $entry['TYPES'])){ 
209           $name = $entry['cn'];
210           $cfg = $this->opsi->get_product_properties($name);
212           $str = management::editEntry('editEntry',array($name),array(),'tabs_opsiProdConfig','OPSIPRODCONFIG','opsi');
213           if($str) return($str);
214           if(isset($this->tabObject->by_object['opsiProperties'])){
215             $this->tabObject->by_object['opsiProperties']->set_cfg($cfg);
216             $this->tabObject->by_object['opsiProperties']->set_product($name);
217             $this->skipFooter = TRUE;
218           }else{
219             trigger_error("Unknown tab, please check config.");
220           }
222         }else{
223           if(count($entry['GROUPS']) == 1){
224             $data = array_pop($entry['GROUPS']);
225             $type = $this->get_type($data);
226             $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
227             if($str) return($str);
228             $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
229             $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
231           }else{
232             $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"edit");
233           }
234         }
235       }
236     }
237   }
240   function saveOpsiProperties()
241   {
242     if($this->tabObject instanceof tabs_opsiProdConfig && isset($_POST['save_properties'])){
243       $this->tabObject->save_object();
244       $op    = $this->tabObject->by_object['opsiProperties'];
245       $name  = $op->get_product();
246       $cfg   = $op->get_cfg();
247       $this->opsi->set_product_properties($name,$cfg);
248       if($this->opsi->is_error()){
249         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
250       }else{
251         $this->remove_lock();
252         $this->closeDialogs();
253       }
254     }
255   }
258   function removeEntryRequested($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
259   {
260     $headpage = $this->getHeadpage();
261     if(count($target) == 1){
262       $entry = $headpage->getEntry($target[0]);
263       if($entry){
264         $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"remove");
265       }
266     }
267   }
269   /*! \brief  Object removal was confirmed, now remove the requested entries.
270    *
271    *  @param  String  'action'  The name of the action which was the used as trigger.
272    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
273    *  @param  Array   'all'     A combination of both 'action' and 'target'.
274    */
275   function removeEntryConfirmed($action="",$target=array(),$all=array(),
276       $altTabClass="",$altTabType="",$altAclCategory="")
277   {
278     $ldap = $this->config->get_ldap_link();
279     $ldap->cd($this->config->current['BASE']);  
280     foreach($this->dns as $key => $dn){
281       $ldap->cat($dn);
282       if($ldap->count()){
283         $attrs = $ldap->fetch();
284         $type= $this->get_type($attrs);
285         $str = management::removeEntryConfirmed($action,array($dn),$all,$type[0],$type[2],$type[1]);
286         if($str) return($str);
288         // Now save changes
289         FAI::save_release_changes_now();
290         $to_del = FAI::clean_up_releases($dn);
291         foreach($to_del as $dn){
292           $ldap->rmdir_recursive($dn);
293         }
294       }
295     }
296   }
299   function editByGroup()
300   {
301     if($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "edit"){
302       $this->dialogObject->save_object();
303       $entry = $this->dialogObject->get_selected();
304       $this->closeDialogs();
305       $data = array_pop($entry);
306       $type = $this->get_type($data);
307       $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
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     }elseif($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "remove"){
313       $this->dialogObject->save_object();
314       $to_delete = $entry = $this->dialogObject->get_selected();
315       $dns = array();
318       // Check FAIstate to ensure that we do not remove frozen objects
319       //  additionally check ACLs
320       $locked = array();
321       $disallowed = array();
322       foreach($to_delete as $obj){
323         if(isset($obj['FAIstate']) && preg_match('/^freeze/', $obj['FAIstate'])){
324           $locked[] = $obj['dn'];
325         }else{
326           $type = $this->get_type($obj);
327           $acl = $this->ui->get_permissions($obj['dn'], 'acl/'.$type[1]);
328           if(!preg_match("/d/",$acl)){
329             $disallowed[] = $obj['dn']; 
330           }else{
331             $dns[] = $obj['dn'];
332           }
333         }
334       }
336       // Display info dialog about locked and not removeable entries
337       if(count($locked)){
338         msg_dialog::display(_("Branch locked"),sprintf(_("The following entries are locked, you can't remove them %s."),
339             msgPool::buildList($locked)));
340       }
342       // Display info dialog about not removeable entries due to permissions
343       if(count($disallowed)){
344         msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
345       }
347       // There are entries left to be removed
348       if(count($dns)){  
349         $this->closeDialogs();
350         $this->dns = $dns;
352         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
354         // check locks
355         if ($user= get_multiple_locks($this->dns)){
356           return(gen_locked_message($user,$this->dns));
357         }
359         // Add locks
360         $dns_names = array();
361         foreach($this->dns as $dn){
362           $dns_names[] =LDAP::fix($dn);
363         }
364         add_lock ($this->dns, $this->ui->dn);
366         // Display confirmation dialog.
367         $smarty = get_smarty();
368         $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
369         $smarty->assign("multiple", true);
370         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
371       }else{
372         $this->closeDialogs();
373       }
374     }
375   }
377   
378   protected function saveChanges()
379   {
380     $str = management::saveChanges();
381     if($str) return($str);
383     // Now save changes
384     FAI::save_release_changes_now();
385     $to_del = FAI::clean_up_releases($this->last_dn);
386     foreach($to_del as $dn){
387       $ldap->rmdir_recursive($dn);
388     }
389   }
391   protected function applyChanges()
392   {
393     $str = management::applyChanges();
394     if($str) return($str);
396     // Now save changes
397     FAI::save_release_changes_now();
398     $to_del = FAI::clean_up_releases($this->last_dn);
399     foreach($to_del as $dn){
400       $ldap->rmdir_recursive($dn);
401     }
402   }
404   function detectPostActions()
405   {
406     $action = management::detectPostActions();
407     if(isset($_POST['new_profile'])) $action['action'] = "new_profile";
408     if(isset($_POST['new_template'])) $action['action'] = "new_template";
409     if(isset($_POST['new_script'])) $action['action'] = "new_script";
410     if(isset($_POST['new_hook'])) $action['action'] = "new_hook";
411     if(isset($_POST['new_variable'])) $action['action'] = "new_variable";
412     if(isset($_POST['new_package'])) $action['action'] = "new_package";
413     if(isset($_POST['new_partition'])) $action['action'] = "new_partition";
416     if(isset($_POST['save_properties'])) $action['action'] = "saveOpsiProperties";
417     if(isset($_POST['cancel_properties'])) $action['action'] = "cancel";
419     if(isset($_POST['edit_continue'])) $action['action'] = "newClassNameSelected";
420     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
422     if(isset($_POST['faiGroupHandle_cancel'])) $action['action'] = "cancel";
423     if(isset($_POST['CancelBranchName'])) $action['action'] = "cancel";
424     if(isset($_POST['delete_branch_confirm'])) $action['action'] = "removeBranchConfirmed";
425     if(isset($_GET['PerformBranch'])) $action['action'] = "PerformBranch";
426     if(isset($_POST['UseBranchName'])) $action['action'] = "saveBranch";
427     if(isset($_POST['faiGroupHandle_apply']))  $action['action'] = "editByGroup";
428     if(isset($_GET['act']) && $_GET['act'] == "branch_branch")  $action['action'] = "createBranch";
429     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch")  $action['action'] = "createFreeze";
430     if(isset($_GET['act']) && $_GET['act'] == "remove_branch")  $action['action'] = "removeBranch";
432     foreach($_POST as $name => $value){
433       if(preg_match("/^edit_([0-9]*)_([a-z]*)_(x|y)/i", $name)){
434         $id = preg_replace("/^edit_([0-9]*)_([a-z]*)_(x|y)/i","\\1", $name);
435         $tab = preg_replace("/^edit_([0-9]*)_([a-z]*)_(x|y)/i","\\2", $name);
436         $headpage = $this->getHeadpage();
437         if(isset($headpage->entries[$id]['GROUPS'][$tab])){
438           $data =$headpage->entries[$id]['GROUPS'][$tab];
439           $type = $this->get_type($data);
440           $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]); 
441           if($str) return($str);
442         } 
443         break;
444       }
445     }
446     return($action);
447   }
450   function renderList()
451   {
452     $filter = $this->getFilter();
453     $headpage = $this->getHeadpage();
454     $filter->setComboBoxOptions("RELEASE",$this->getReleaseList());
456     if(isset($_POST['RELEASE'])){
457       $this->fai_release = get_post('RELEASE');
458     }
459     $headpage->setBase($this->fai_release);
460     $headpage->update();
461     $smarty = get_smarty();
462     $smarty->assign("fai_release", $this->fai_release);
463     $smarty->assign("fai_base", $this->fai_base);
464     $r = $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'));
465     $c = $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'));
466     $smarty->assign("allow_create", $c);
467     $smarty->assign("allow_remove", $r);
468     $display = $headpage->render();
469     return($this->getHeader().$display);
470   }
472   function getReleaseList($base = "", $prefix ="")
473   {
474     $list = array();
475     if(empty($base)){
476       $base = $this->fai_base;
477       $list[$base] = "/";
478     }
480     $ldap = $this->config->get_ldap_link();
481     $ldap->ls("(objectClass=FAIbranch)",$base,array("ou","FAIstate"));
482     $cfg_rel = $this->config->search("faiManagement","DEFAULTFAIRELEASE",array("menu"));
484     while($release = $ldap->fetch()){
485       $list[$release['dn']] = $prefix.$release['ou'][0];
487       // Preset to prefered releaes if necessary  
488       if(empty($this->fai_release) && $cfg_rel == $release['dn']){
489         $this->fai_release = $release['dn'];
490       }
492       $list = array_merge($list,$this->getReleaseList($release['dn'],$prefix."&nbsp; "));
493     }
494     return($list);
495   }
498   static function filterProperties($row, $classes)
499   {
500     $objects = array(
501         "FAIpartitionTable"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png",
502           "NAME"=>_("Partition table"),"KZL"=> "PT", "VAR"=>"ShowPartitions"),
503         "FAIpackageList"     => array("IMG"=> "plugins/fai/images/fai_packages.png",
504           "NAME"=>_("Package list") ,  "KZL"=> "PL", "VAR"=>"ShowPackages"),
505         "FAIscript"          => array("IMG"=> "plugins/fai/images/fai_script.png",
506           "NAME"=>_("Scripts") ,       "KZL"=> "S",  "VAR"=>"ShowScripts"),
507         "FAIvariable"        => array("IMG"=> "plugins/fai/images/fai_variable.png",
508           "NAME"=>_("Variables") ,     "KZL"=> "V",  "VAR"=>"ShowVariables"),
509         "FAIhook"            => array("IMG"=> "plugins/fai/images/fai_hook.png",
510           "NAME"=>_("Hooks"),          "KZL"=> "H",  "VAR"=>"ShowHooks"),
511         "FAIprofile"         => array("IMG"=> "plugins/fai/images/fai_profile.png",
512           "NAME"=>_("Profile") ,       "KZL"=> "P",  "VAR"=>"ShowProfiles"),
513         "FAItemplate"        => array("IMG"=> "plugins/fai/images/fai_template.png",
514           "NAME"=>_("Templates") ,     "KZL"=> "T",  "VAR"=>"ShowTemplates"),
515         "opsi_netboot"       => array("IMG"=> "plugins/opsi/images/netboot_package.png",
516           "NAME"=>_("OPSI netboot product") ,     "KZL"=> "ON",  "VAR"=>"ShowOpsiNetboot"),
517         "opsi_local"         => array("IMG"=> "plugins/opsi/images/local_package.png",
518           "NAME"=>_("OPSI localboot product")   ,     "KZL"=> "OL",  "VAR"=>"ShowOpsiLocal"));
520     $icon_list = "";
521     foreach($objects as $type => $type_data){
522       if(in_array($type, $classes)){
523         $icon_list .= "<input type='image' src='".$type_data['IMG']."' title='".$type_data['NAME']."'
524           alt='".$type_data['KZL']."' class='center' name='edit_".$row."_".$type."'>\n";
525       }else{
526         $icon_list .= "<img src='images/empty.png' alt=' ' class='center'>\n";
527       }
528     }
530     return $icon_list;
531   }
536   function removeBranch()
537   {
538     /* Check if we have a post remove method configured
539      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
540      */
541     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
542       /* Load permissions for selected 'dn' and check if
543          we're allowed to remove this 'dn' */
544       if(preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
545         $smarty=get_smarty();
546         $smarty->assign("release_hidden",base64_encode($this->fai_release));
547         $smarty->assign("info", msgPool::deleteInfo(LDAP::fix($this->fai_release),_("FAI branch/freeze")));
548         return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
549       } else {
550         msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
551       }
552     }
553   }
556   function removeBranchConfirmed()
557   {
558     /* Check if we have a post remove method configured
559      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
560      */
561     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
563       if(!isset($_POST['release_hidden']) || base64_decode($_POST['release_hidden']) != $this->fai_release){
564         msg_dialog::display(_("Warning"),_("Release remove aborted because the release name check failed!"));
565       }else{
567         $bb =  $this->fai_release;
568         $ldap = $this->config->get_ldap_link();
570         $br = $this->getBranches();
572         if(isset($br[$bb]) && preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
573           $name = $br[$bb];
575           $ldap->cd($bb);
576           $ldap->recursive_remove();
577           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('applicationRDN'), $bb));
578           $ldap->recursive_remove();
579           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('mimetypeRDN'), $bb));
580           $ldap->recursive_remove();
582           /* Search for all groups with configured application menus.
583              - First search all groups, to ensure that we only remove entries form whithin groups.
584              - The search für menu configuration for the specified release and collect all those dns.
585              - Remove entries
586            */
587           $release_ou = preg_replace("/".preg_quote(get_ou("faiBaseRDN"), '/').".*$/i","",$bb);
588           $ldap->cd($this->config->current['BASE']);
589           $ldap->search("(objectClass=posixGroup)",array("dn"));
591           /* Collect all group dns
592            */
593           $groups = array();
594           while($attrs = $ldap->fetch()){
595             $groups[] = $attrs['dn'];
596           }
599           /* Collect all group menu release dns that match the release we have removed
600            */
601           $dns = array();
602           foreach($groups as $dn){
603             $ldap->cd($dn);
604             $ldap->search("(objectClass=FAIbranch)",array("dn"));
605             while($attrs = $ldap->fetch()){
606               if(preg_match("/^".preg_quote($release_ou, '/')."/",$attrs['dn'])){
607                 $dns[] = $attrs['dn'];
608               }
609             }
610           }
612           /* Finally remove collected release dns
613            */
614           foreach($dns as $dn){
615             $ldap->cd($dn);
616             $ldap->recursive_remove();
617           }
619           /* Post remove */
620           $this->fai_release = $this->fai_base;
621           $this->lock_name   = $name;
622           $this->lock_dn     = $bb;
623           $this->postremove();
625           $fai_filter = session::get("fai_filter");
626           $fai_filter['fai_release'] = $this->fai_release;
627           session::set("fai_filter",$fai_filter);
629           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
630         }
631       }
632     }
633   }
636   function createBranch()
637   { 
638     $smarty = get_smarty();
639     $this->dispNewBranch=true;
640     $this->dispNewFreeze=false;
641     $smarty->assign("iframe",false);
642     if(isset($_POST['BranchName'])){
643       $smarty->assign("BranchName", $_POST['BranchName']);
644     }else{
645       $smarty->assign("BranchName","");
646     }
647     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
648   }
650   function createFreeze()
651   { 
652     $smarty = get_smarty();
653     $this->dispNewFreeze=true;
654     $this->dispNewBranch=false;
655     $smarty->assign("iframe",false);
656     if(isset($_POST['BranchName'])){
657       $smarty->assign("BranchName", $_POST['BranchName']);
658     }else{
659       $smarty->assign("BranchName","");
660     }
661     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
662   }
666   function PerformBranch()
667   {
668     if(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
669       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
670     }else{
672       /* In order to see error messages we have to reset the error handler.
673          Due to the exit();
674        */
675       restore_error_handler();
677       $this->dispNewBranch = false;
678       $this->dispNewFreeze = false;
680       $LASTPOST = session::get('LASTPOST');
681       $base = $LASTPOST['base'];
682       $_POST  = session::get('LASTPOST');
683       $name = $_POST['BranchName'];
685       $type = $LASTPOST['type'];
686       $ldap = $this->config->get_ldap_link();
688       $baseToUse = $base;
689       if($this->fai_release !=  $this->fai_base){
690         $baseToUse = $this->fai_release;
691       }
693       /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
695       $CurrentReleases  = $this->getBranches();
696       $NewReleaseName   = $name;
697       if(isset($CurrentReleases[$this->fai_release])) {
698         if($this->fai_release != $this->fai_base){
699           $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
700           $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName);
701         }else{
702           $NewReleaseName   = $name;
703         }
704       }
705       $appsrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),$baseToUse);
706       $appdst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),"ou=".$name.",".$baseToUse) ;
708       $mimesrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),$baseToUse);
709       $mimedst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),"ou=".$name.",".$baseToUse) ;
711       /* Check if source depeartments exist */
712       foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
713         $ldap->cd($this->config->current['BASE']);
714         $ldap->cat($dep);
715         if(!$ldap->count()){
716           $ldap->create_missing_trees($dep);
717         }
718       }
720       /* Print header to have styles included */
721       echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
722         <html>
723         <head>
724         <title></title>
725         <style type="text/css">@import url("themes/default/style.css");</style>
726         <script language="javascript" src="include/focus.js" type="text/javascript"></script>
727         </head>
728         <body style="background: none;margin:3px;color:black">
729         ';
731       new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
733       /* Duplicate group application releases
734        */
735       FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
737       /* Duplicate applications
738        */
739       $ldap->cat($appsrc,array("dn")) ;
740       if($ldap->count()){
741         $ldap->cd ($appdst);
742         $ldap->recursive_remove();
743         FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
744       }
746       /* Duplicate mime types
747        */
748       $ldap->cat($mimesrc,array("dn")) ;
749       if($ldap->count()){
750         $ldap->cd ($mimedst);
751         $ldap->recursive_remove();
752         FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
753       }
755       $attr = array();
756       $attr['objectClass'] = array("organizationalUnit","FAIbranch");
757       $attr['ou']       = $name;
758       $attr['FAIstate'] = $type;
759       $ldap->cd($this->config->current['BASE']);
760       $ldap->cd("ou=".$name.",".$baseToUse);
761       $ldap->cat("ou=".$name.",".$baseToUse);
762       if($ldap->count()){
763         $ldap->modify($attr);
764       }else{
765         $ldap->add($attr);
766       }
768       /* Duplicate fai objects
769        */
770       //      $ldap->cd ("ou=".$name.",".$baseToUse);
771       //      $ldap->recursive_remove();
772       //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
774       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
775         <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
776         <input type='hidden' name='php_c_check' value='1'>
777         </form></div>";
779       echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
781       /* Print footer to have valid html */
782       echo "</body></html>";
784       $this->dispNewFreeze = false;
786       /* Postcreate */
788       /* Assign possible attributes */
789       $this->lock_type  = $type;
790       $this->lock_name  = $name;
791       $this->lock_dn    = $baseToUse;
792       $this->postcreate();
794       /* Send daemon event to reload the fai release database
795        */
796       if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
797         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
798         if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'])){
799           $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'];
800           $tmp = new $evt['CLASS_NAME']($this->config);
801           $tmp->set_type(TRIGGERED_EVENT);
802           $tmp->add_targets(array("GOSA"));
803           $o_queue = new gosaSupportDaemon();
804           if(!$o_queue->append($tmp)){
805             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
806           }
807         }
808       }else{
809         trigger_error("Unknown class DaemonEvent / gosaSupportDaemon");
810         msg_dialog::display(_("Fatal error"),
811             "Daemon events called but classes where not accessible, DaemonEvent gosaSupportDaemon",
812             FATAL_ERROR_DIALOG);
813       }
814       exit();
815     }
816   }
821   function saveBranch()
822   {
823     if($this->dispNewBranch){
824       $type = "branch";
825     }else{
826       $type = "freeze";
827     }
829     /* Check branch name */
830     $name = $_POST['BranchName'];
831     $is_ok = true;
832     $smarty = get_smarty();
833     $smarty->assign("BranchName",$name);
834     $base= $this->fai_base;
836     /* Check used characters */
837     if(!preg_match("/^[0-9a-z\.]*$/",$name)){
838       msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z\.]/"), ERROR_DIALOG);
839       $is_ok = false;
840     }
842     // Check if this name is already in use 
843     if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
844       msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
845       $is_ok = false;
846     }
848     // Handle errors
849     if(!$is_ok && $this->dispNewFreeze){
850       return($this->createFreeze());
851     }elseif(!$is_ok && $this->dispNewBranch){
852       return($this->createBranch());
853     }
855     // Now create new release
857     if(session::is_set('LASTPOST')){
858       $LASTPOST = session::get('LASTPOST');
859     }else{
860       $LASTPOST = array();
861     }
862     $LASTPOST['base'] = $base;
863     $LASTPOST['type'] = $type;
864     $LASTPOST['BranchName'] = $name;
865     session::set('LASTPOST',$LASTPOST);
866     $smarty->assign("iframe", true);
867     $smarty->assign("plugID", $_GET['plug']);
868     $display  = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
869     return($display);
870  
871   }
874   function CheckNewBranchName($name,$base)
875   {
876     $f = $this->fai_release;
877     if($name == ""){
878       return(false);
879     }elseif(in_array($name,$this->getBranches($f))) {
880       return(false);
881     }elseif(tests::is_department_name_reserved($name,$base)){
882       return(false);
883     }
884     return(true);
885   }
888   /* Get available branches for current base */
889   function getBranches($base = false,$prefix = "")
890   {
891     $ret = array("/"=>$this->fai_base);
892     $ldap = $this->config->get_ldap_link();
893     if(!$base){
894       $base = $this->fai_base;
895     }
896     $tmp = FAI::get_all_releases_from_base($base,true);
897     foreach($tmp as $dn => $name){
898       $ret[$name]=$dn;
899     }
900     ksort($ret);
901     $ret = array_flip($ret);
903     return ($ret);
904   }
907   function get_type($array)
908   {
909     if(!isset($array['objectClass'])) return(array());
910     if(in_array("FAIpartitionTable",$array['objectClass'])){
911       return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
912     }
913     if(in_array("FAIscript",$array['objectClass'])){
914       return(array("tabsScript","faiScript","FAISCRIPTTABS"));
915     }
916     if(in_array("FAItemplate",$array['objectClass'])){
917       return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
918     }
919     if(in_array("FAIhook",$array['objectClass'])){
920       return(array("tabsHook","faiHook","FAIHOOKTABS"));
921     }
922     if(in_array("FAIvariable",$array['objectClass'])){
923       return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
924     }
925     if(in_array("FAIprofile",$array['objectClass'])){
926       return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
927     }
928     if(in_array("FAIpackageList",$array['objectClass'])){
929       return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
930     }
931     return(array());
932   }
935   /* Check if the given FAI class is used in this release
936    */
937   static function check_class_name($oc,$name,$dn)
938   {
939     $base = FAI::get_release_dn($dn);
941     if($oc == "FAIprofile"){
942       $f = "";
943       $ocs = array("FAIprofile","FAItemplate","FAIhook","FAIpartitionTable","FAIpackageList","FAIscript","FAIvariable");
944       foreach($ocs as $oc){
945         $f .= "(objectClass=".$oc.")";
946       }
947       $res  = FAI::get_all_objects_for_given_base($base,"(|".$f.")",TRUE);
948     }else{
949       $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
950     }
951     $delete = array();
952     $used   = array();
953     foreach($res as $object){
954       $used[$object['cn'][0]]= $object['cn'][0];
955     }
956     return($used);
957   }
960
961 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
962 ?>