1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003 Cajus Pollmeier
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 class applicationManagement extends plugin
22 {
23 /* Definitions */
24 var $plHeadline = "Applications";
25 var $plDescription = "This does something";
27 /* Dialog attributes */
28 var $apptabs = NULL;
29 var $ui = NULL;
30 var $CopyPasteHandler = NULL;
31 var $DivListApplication = NULL;
32 var $applications = array();
33 var $enableReleaseManagement = false;
34 var $start_pasting_copied_objects = FALSE;
36 var $app_base ="";
37 var $app_release ="";
38 var $acl_module = array("application");
40 var $dns = array();
42 function IsReleaseManagementActivated()
43 {
44 /* Check if we should enable the release selection */
45 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
46 if(!empty($tmp)){
47 return(true);
48 }
49 return(false);
50 }
53 function applicationManagement (&$config, &$ui)
54 {
55 /* Save configuration for internal use */
56 $this->config = &$config;
57 $this->ui = &$ui;
59 /* Check if copy & paste is activated */
60 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
61 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
62 }
64 /* Creat dialog object */
65 $this->DivListApplication = new divListApplication($this->config,$this);
67 if($this->IsReleaseManagementActivated()){
68 /* Check if we should enable the release selection */
69 $this->enableReleaseManagement = true;
71 /* Hide SubSearch checkbox */
72 $this->DivListApplication->DisableCheckBox("SubSearch");
73 }
75 /* Set default release */
76 if(!$this->IsReleaseManagementActivated()){
77 $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
78 if(!session::is_set("app_filter")){
79 session::set("app_filter",array("app_base" => $this->app_base));
80 }
81 $app_filter = session::get("app_filter");
82 $this->app_base = $app_filter['app_base'];
83 }else{
84 $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
85 if(!session::is_set("app_filter")){
86 session::set("app_filter",array("app_base" => $this->app_base,"app_release" => $this->app_base));
87 }
88 $app_filter = session::get("app_filter");
89 $this->app_base = $app_filter['app_base'];
90 $this->app_release = $app_filter['app_release'];
91 }
92 }
95 function getReleases()
96 {
97 $ldap = $this->config->get_ldap_link();
98 $ret = array();
99 $base = $this->app_base;
101 $ret[$this->app_base] = "/";
102 $ldap->cd($base);
103 $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou"));
104 while($attrs = $ldap->fetch()){
105 $str = str_replace($base,"",$attrs['dn']);
106 $tmp = array_reverse( split("ou=",$str));
107 $str = "";
108 foreach($tmp as $val){
109 $val = trim(preg_replace("/,/","",$val));
110 if(empty($val)) break;
111 $str .= "/".$val;
112 }
113 if(!empty($str)){
114 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
115 }
116 }
117 asort($ret);
118 return($ret);
119 }
121 function execute()
122 {
123 /* Call parent execute */
124 plugin::execute();
127 /****************
128 Variable init
129 ****************/
131 /* These vars will be stored if you try to open a locked app,
132 to be able to perform your last requests after showing a warning message */
133 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/","/^menu_action/"));
135 $smarty = get_smarty(); // Smarty instance
136 $s_action = ""; // Contains the action to proceed
137 $s_entry = ""; // The value for s_action
138 $base_back = ""; // The Link for Backbutton
140 /* Test Posts */
141 foreach($_POST as $key => $val){
142 // Post for delete
143 if(preg_match("/appl_del.*/",$key)){
144 $s_action = "del";
145 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
146 // Post for edit
147 }elseif(preg_match("/appl_edit_.*/",$key)){
148 $s_action="edit";
149 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
150 // Post for new
151 }elseif(preg_match("/^copy_.*/",$key)){
152 $s_action="copy";
153 $s_entry = preg_replace("/^copy_/i","",$key);
154 }elseif(preg_match("/^cut_.*/",$key)){
155 $s_action="cut";
156 $s_entry = preg_replace("/^cut_/i","",$key);
157 // Post for new
158 }elseif(preg_match("/^appl_new.*/",$key)){
159 $s_action="new";
160 }elseif(preg_match("/^remove_multiple_applications/",$key)){
161 $s_action="del_multiple";
162 }elseif(preg_match("/^editPaste.*/i",$key)){
163 $s_action="editPaste";
164 }elseif(preg_match("/^multiple_copy_groups/",$key)){
165 $s_action = "copy_multiple";
166 }elseif(preg_match("/^multiple_cut_groups/",$key)){
167 $s_action = "cut_multiple";
168 }
169 }
171 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
172 $s_action ="edit";
173 $s_entry = $_GET['id'];
174 }
176 $s_entry = preg_replace("/_.$/","",$s_entry);
179 /* handle C&P from layers menu */
180 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
181 $s_action = "copy_multiple";
182 }
183 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
184 $s_action = "cut_multiple";
185 }
186 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
187 $s_action = "editPaste";
188 }
190 /* Create options */
191 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "appl_new"){
192 $s_action = "new";
193 }
195 /* handle remove from layers menu */
196 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
197 $s_action = "del_multiple";
198 }
200 /****************
201 Copy & Paste handling
202 ****************/
204 /* Display the copy & paste dialog, if it is currently open */
205 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
206 if($ret){
207 return($ret);
208 }
210 /****************
211 Create a new app
212 ****************/
214 /* New application? */
215 if ($s_action=="new"){
217 /* By default we set 'dn' to 'new', all relevant plugins will
218 react on this. */
219 $this->dn= "new";
221 /* Create new usertab object */
222 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
223 $this->apptabs->parent = &$this;
224 $this->apptabs->set_acl_base($this->app_base);
225 }
228 /****************
229 Edit entry canceled
230 ****************/
232 /* Cancel dialogs */
233 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
234 $this->remove_lock();
235 $this->apptabs= NULL;
236 session::un_set('objectinfo');
237 }
240 /****************
241 Edit entry finished
242 ****************/
244 /* Finish apps edit is triggered by the tabulator dialog, so
245 the user wants to save edited data. Check and save at this
246 point. */
247 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
249 /* Check tabs, will feed message array */
250 $this->apptabs->last= $this->apptabs->current;
251 $this->apptabs->save_object();
252 $message= $this->apptabs->check();
254 /* Save, or display error message? */
255 if (count($message) == 0){
257 /* Save data data to ldap */
258 $this->apptabs->save();
260 if (!isset($_POST['edit_apply'])){
261 /* Application has been saved successfully, remove lock from
262 LDAP. */
263 if ($this->dn != "new"){
264 $this->remove_lock();
265 }
266 $this->apptabs= NULL;
267 session::un_set('objectinfo');
268 }else{
270 /* Reinitialize tab */
271 if($this->apptabs instanceof tabs){
272 $this->apptabs->re_init();
273 }
274 }
275 } else {
276 /* Ok. There seem to be errors regarding to the tab data,
277 show message and continue as usual. */
278 msg_dialog::displayChecks($message);
279 }
280 }
283 /****************
284 Edit entry
285 ****************/
287 /* User wants to edit data? */
288 if (($s_action=="edit") && (!isset($this->apptabs->config))){
290 /* Get 'dn' from posted 'applist', must be unique */
291 $this->dn= $this->applications[$s_entry]['dn'];
293 /* Check locking, save current plugin in 'back_plugin', so
294 the dialog knows where to return. */
295 if (($user= get_lock($this->dn)) != ""){
296 return(gen_locked_message ($user, $this->dn));
297 }
299 /* Lock the current entry, so everyone will get the
300 above dialog */
301 add_lock ($this->dn, $this->ui->dn);
303 /* Register apptabs to trigger edit dialog */
304 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
305 if($this->IsReleaseManagementActivated()){
306 $this->apptabs->set_FAIstate($this->applications[$s_entry]['FAIstate'][0]);
307 }
308 $this->apptabs->parent = &$this;
309 $this->apptabs->set_acl_base($this->dn);
310 session::set('objectinfo',$this->dn);
311 }
315 /********************
316 Delete MULTIPLE entries requested, display confirm dialog
317 ********************/
319 if ($s_action=="del_multiple"){
320 $ids = $this->list_get_selected_items();
322 if(count($ids)){
323 $this->dns = array();
325 $disallowed = array();
326 foreach($ids as $id){
327 $dn = $this->applications[$id]['dn'];
328 $acl = $this->ui->get_permissions($dn, "application/application");
329 if(preg_match("/d/",$acl)){
330 $this->dns[$id] = $dn;
331 }else{
332 $disallowed[] = $dn;
333 }
334 }
336 if(count($disallowed)){
337 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
338 }
340 if(count($this->dns)){
342 if ($user= get_multiple_locks($this->dns)){
343 return(gen_locked_message($user,$this->dns));
344 }
346 $dns_names = array();
347 foreach($this->dns as $dn){
348 add_lock ($dn, $this->ui->dn);
349 $dns_names[] =@LDAP::fix($dn);
350 }
352 /* Lock the current entry, so nobody will edit it during deletion */
353 $smarty->assign("intro", msgPool::deleteInfo($dns_names,_("application")));
354 $smarty->assign("multiple", true);
355 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
356 }
357 }
358 }
361 /********************
362 Delete MULTIPLE entries confirmed
363 ********************/
365 /* Confirmation for deletion has been passed. Users should be deleted. */
366 if (isset($_POST['delete_multiple_application_confirm'])){
368 /* Remove user by user and check acls before removeing them */
369 foreach($this->dns as $key => $dn){
371 $ui = get_userinfo();
372 $acl = $ui->get_permissions($dn ,"application/application");
373 if (preg_match('/d/', $acl)){
375 /* Delete request is permitted, perform LDAP action */
376 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
377 $this->apptabs->parent = &$this;
378 $this->apptabs->set_acl_base($dn);
379 $this->apptabs->delete ();
380 unset ($this->apptabs);
381 $this->apptabs= NULL;
383 } else {
384 /* Normally this shouldn't be reached, send some extra
385 logs to notify the administrator */
386 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
387 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
388 }
389 }
391 /* Remove lock file after successfull deletion */
392 $this->remove_lock();
393 $this->dns = array();
394 }
397 /********************
398 Delete MULTIPLE entries Canceled
399 ********************/
401 /* Remove lock */
402 if(isset($_POST['delete_multiple_application_cancel'])){
404 /* Remove lock file after successfull deletion */
405 $this->remove_lock();
406 $this->dns = array();
407 }
409 /****************
410 Delete app
411 ****************/
413 /* Remove user was requested */
414 if ($s_action == "del"){
416 /* Get 'dn' from posted 'uid' */
417 $this->dn= $this->applications[$s_entry]['dn'];
419 /* Load permissions for selected 'dn' and check if
420 we're allowed to remove this 'dn' */
421 $ui = get_userinfo();
422 $acl = $ui->get_permissions($this->dn ,"application/application");
424 if(preg_match("/d/",$acl)){
425 /* Check locking, save current plugin in 'back_plugin', so
426 the dialog knows where to return. */
427 if (($user= get_lock($this->dn)) != ""){
428 return (gen_locked_message ($user, $this->dn));
429 }
431 /* Lock the current entry, so nobody will edit it during deletion */
432 add_lock ($this->dn, $this->ui->dn);
433 $smarty= get_smarty();
434 $smarty->assign("intro",msgPool::deleteInfo(@LDAP::fix($this->dn),_("application")));
435 $smarty->assign("multiple", false);
436 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
437 } else {
439 /* Obviously the user isn't allowed to delete. Show message and
440 clean session. */
441 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
442 }
443 }
446 /****************
447 Delete app confirmed
448 ****************/
450 /* Confirmation for deletion has been passed. Group should be deleted. */
451 if (isset($_POST['delete_app_confirm'])){
453 /* Some nice guy may send this as POST, so we've to check
454 for the permissions again. */
455 $ui = get_userinfo();
456 $acl = $ui->get_permissions($this->dn ,"application/application");
458 if(preg_match("/d/",$acl)){
460 /* Delete request is permitted, perform LDAP action */
461 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
462 $this->apptabs->parent = &$this;
463 $this->apptabs->set_acl_base($this->dn);
464 $this->apptabs->delete ();
465 unset ($this->apptabs);
466 $this->apptabs= NULL;
468 } else {
470 /* Normally this shouldn't be reached, send some extra
471 logs to notify the administrator */
472 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
473 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
474 }
476 /* Remove lock file after successfull deletion */
477 $this->remove_lock();
478 }
481 /****************
482 Delete app canceled
483 ****************/
485 /* Delete application canceled? */
486 if (isset($_POST['delete_cancel'])){
487 $this->remove_lock();
488 session::un_set('objectinfo');
489 }
491 /* Show tab dialog if object is present */
492 if (($this->apptabs) && (isset($this->apptabs->config))){
493 $display= $this->apptabs->execute();
495 /* Don't show buttons if tab dialog requests this */
496 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
497 $display.= "<p style=\"text-align:right\">\n";
499 if(isset($this->apptabs->FAIstate) && !preg_match("/freeze/i",$this->apptabs->FAIstate)){
500 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
501 $display.= " \n";
502 if ($this->dn != "new"){
503 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
504 $display.= " \n";
505 }
506 }
507 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
508 $display.= "</p>";
509 }
510 return ($display);
511 }
514 /****************
515 Dialog display
516 ****************/
518 /* Check if there is a snapshot dialog open */
519 if($this->IsReleaseManagementActivated()){
520 $base = $this->app_release;
521 }else{
522 $base = $this->app_base;
523 }
524 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
525 return($str);
526 }
528 /* Display dialog with system list */
529 $this->DivListApplication->parent = $this;
530 $this->DivListApplication->execute();
531 if(!$this->IsReleaseManagementActivated()){
532 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
533 }
534 $this->reload();
535 $this->DivListApplication->setEntries($this->applications);
536 return($this->DivListApplication->Draw());
537 }
540 /* Return departments, that will be included within snapshot detection */
541 function get_used_snapshot_bases()
542 {
543 if($this->IsReleaseManagementActivated()){
544 return(array($this->app_release));
545 }else{
546 return(array($this->app_base));
547 }
548 }
551 function reload()
552 {
553 $this->applications= array();
555 /* Set base for all searches */
556 $Regex = $this->DivListApplication->Regex;
557 $SubSearch = $this->DivListApplication->SubSearch;
558 $Flags = GL_NONE | GL_SIZELIMIT;
559 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
560 $tmp = array();
562 if(!$this->IsReleaseManagementActivated()){
563 $use_base = $this->app_base;
564 if($SubSearch){
565 $use_base = preg_replace("/^".normalizePreg(get_ou("applicationou"))."/","",$use_base);
566 }
567 }else{
568 $use_base = $this->app_release;
569 $SubSearch= FALSE;
570 }
572 /* Add FAIstate to the search attributes */
573 $search_attrs = array("cn","description","dn","objectClass");
574 if($this->IsReleaseManagementActivated()) {
575 $search_attrs[] = "FAIstate";
576 }
578 if($SubSearch){
579 $res= get_sub_list($Filter, "application",get_ou("applicationou"), $use_base, $search_attrs, $Flags);
580 }else{
581 $res= get_list($Filter, "application",$use_base, $search_attrs, $Flags);
582 }
583 $tmp2 = array();
584 foreach ($res as $val){
585 if(!isset($val['FAIstate'])){
586 $val['FAIstate'][0] = "";
587 }
588 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
589 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
590 }
592 natcasesort($tmp2);
593 $this->applications=array();
594 foreach($tmp2 as $val){
595 $this->applications[]=$tmp[$val];
596 }
597 reset ($this->applications);
598 }
600 function remove_from_parent()
601 {
602 /* Optionally execute a command after we're done */
603 $this->postremove();
604 }
607 function copyPasteHandling_from_queue($s_action,$s_entry)
608 {
609 /* Check if Copy & Paste is disabled */
610 if(!is_object($this->CopyPasteHandler)){
611 return("");
612 }
614 $ui = get_userinfo();
616 /* Add a single entry to queue */
617 if($s_action == "cut" || $s_action == "copy"){
619 /* Cleanup object queue */
620 $this->CopyPasteHandler->cleanup_queue();
621 $dn = $this->applications[$s_entry]['dn'];
622 if($s_action == "copy" && $ui->is_copyable($dn,"application","application")){
623 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
624 }
625 if($s_action == "cut" && $ui->is_cutable($dn,"application","application")){
626 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
627 }
628 }
631 /* Add entries to queue */
632 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
634 /* Cleanup object queue */
635 $this->CopyPasteHandler->cleanup_queue();
637 /* Add new entries to CP queue */
638 foreach($this->list_get_selected_items() as $id){
639 $dn = $this->applications[$id]['dn'];
641 if($s_action == "copy_multiple" && $ui->is_copyable($dn,"application","application")){
642 $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
643 }
644 if($s_action == "cut_multiple" && $ui->is_cutable($dn,"application","application")){
645 $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
646 }
647 }
648 }
650 /* Start pasting entries */
651 if($s_action == "editPaste"){
652 $this->start_pasting_copied_objects = TRUE;
653 }
656 /* Return C&P dialog */
657 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
660 /* Get dialog */
661 $this->CopyPasteHandler->SetVar("base",$this->app_base);
662 $this->CopyPasteHandler->SetVar("parent",$this);
663 $data = $this->CopyPasteHandler->execute();
665 /* Return dialog data */
666 if(!empty($data)){
667 return($data);
668 }
669 }
671 /* Automatically disable status for pasting */
672 if(!$this->CopyPasteHandler->entries_queued()){
673 $this->start_pasting_copied_objects = FALSE;
674 }
675 return("");
676 }
679 function list_get_selected_items()
680 {
681 $ids = array();
682 foreach($_POST as $name => $value){
683 if(preg_match("/^item_selected_[0-9]*$/",$name)){
684 $id = preg_replace("/^item_selected_/","",$name);
685 $ids[$id] = $id;
686 }
687 }
688 return($ids);
689 }
692 /* Save to LDAP */
693 function save()
694 {
695 /* Optionally execute a command after we're done */
696 $this->postcreate();
697 }
700 function remove_lock()
701 {
702 if (isset($this->apptabs->dn)){
703 del_lock ($this->apptabs->dn);
704 }
705 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
706 del_lock($this->dn);
707 }
708 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
709 del_lock($this->dns);
710 }
711 }
714 function save_object()
715 {
716 $this->DivListApplication->save_object();
717 if(is_object($this->CopyPasteHandler)){
718 $this->CopyPasteHandler->save_object();
719 }
721 if($this->IsReleaseManagementActivated() && isset($_POST['app_release'])){
722 $sel_rel = get_post('app_release');
723 $releases = array_flip($this->getReleases());
724 if(isset($releases[$sel_rel])){
725 $this->app_release = $releases[$sel_rel];
726 }
727 $app_filter = session::get("app_filter");
728 $app_filter['app_release'] = $this->app_release;
729 session::set("app_filter",$app_filter);
730 }elseif(!$this->IsReleaseManagementActivated()){
731 $this->app_base = get_ou("applicationou").$this->DivListApplication->selectedBase;
732 $app_filter = session::get("app_filter");
733 $app_filter['app_base'] = $this->app_base;
734 session::set("app_filter",$app_filter);
735 }
736 }
738 function check() {}
739 function adapt_from_template($dn, $skip= array()) {}
740 function password_change_needed() {}
741 }
742 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
743 ?>