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