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 ="";
39 var $dns = array();
41 function IsReleaseManagementActivated()
42 {
43 /* Check if we should enable the release selection */
44 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
45 if(!empty($tmp)){
46 return(true);
47 }
48 return(false);
49 }
52 function applicationManagement (&$config, &$ui)
53 {
54 /* Save configuration for internal use */
55 $this->config = &$config;
56 $this->ui = &$ui;
58 /* Check if copy & paste is activated */
59 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
60 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
61 }
63 /* Creat dialog object */
64 $this->DivListApplication = new divListApplication($this->config,$this);
66 if($this->IsReleaseManagementActivated()){
67 /* Check if we should enable the release selection */
68 $this->enableReleaseManagement = true;
70 /* Hide SubSearch checkbox */
71 $this->DivListApplication->DisableCheckBox("SubSearch");
72 }
74 /* Set default release */
75 if(!$this->IsReleaseManagementActivated()){
76 $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
77 if(!session::is_set("app_filter")){
78 session::set("app_filter",array("app_base" => $this->app_base));
79 }
80 $app_filter = session::get("app_filter");
81 $this->app_base = $app_filter['app_base'];
82 }else{
83 $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
84 if(!session::is_set("app_filter")){
85 session::set("app_filter",array("app_base" => $this->app_base,"app_release" => $this->app_base));
86 }
87 $app_filter = session::get("app_filter");
88 $this->app_base = $app_filter['app_base'];
89 $this->app_release = $app_filter['app_release'];
90 }
91 }
94 function getReleases()
95 {
96 $ldap = $this->config->get_ldap_link();
97 $ret = array();
98 $base = $this->app_base;
100 $ret[$this->app_base] = "/";
101 $ldap->cd($base);
102 $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou"));
103 while($attrs = $ldap->fetch()){
104 $str = str_replace($base,"",$attrs['dn']);
105 $tmp = array_reverse( split("ou=",$str));
106 $str = "";
107 foreach($tmp as $val){
108 $val = trim(preg_replace("/,/","",$val));
109 if(empty($val)) break;
110 $str .= "/".$val;
111 }
112 if(!empty($str)){
113 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
114 }
115 }
116 asort($ret);
117 return($ret);
118 }
120 function execute()
121 {
122 /* Call parent execute */
123 plugin::execute();
126 /****************
127 Variable init
128 ****************/
130 /* These vars will be stored if you try to open a locked app,
131 to be able to perform your last requests after showing a warning message */
132 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/","/^menu_action/"));
134 $smarty = get_smarty(); // Smarty instance
135 $s_action = ""; // Contains the action to proceed
136 $s_entry = ""; // The value for s_action
137 $base_back = ""; // The Link for Backbutton
139 /* Test Posts */
140 foreach($_POST as $key => $val){
141 // Post for delete
142 if(preg_match("/appl_del.*/",$key)){
143 $s_action = "del";
144 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
145 // Post for edit
146 }elseif(preg_match("/appl_edit_.*/",$key)){
147 $s_action="edit";
148 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
149 // Post for new
150 }elseif(preg_match("/^copy_.*/",$key)){
151 $s_action="copy";
152 $s_entry = preg_replace("/^copy_/i","",$key);
153 }elseif(preg_match("/^cut_.*/",$key)){
154 $s_action="cut";
155 $s_entry = preg_replace("/^cut_/i","",$key);
156 // Post for new
157 }elseif(preg_match("/^appl_new.*/",$key)){
158 $s_action="new";
159 }elseif(preg_match("/^remove_multiple_applications/",$key)){
160 $s_action="del_multiple";
161 }elseif(preg_match("/^editPaste.*/i",$key)){
162 $s_action="editPaste";
163 }elseif(preg_match("/^multiple_copy_groups/",$key)){
164 $s_action = "copy_multiple";
165 }elseif(preg_match("/^multiple_cut_groups/",$key)){
166 $s_action = "cut_multiple";
167 }
168 }
170 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
171 $s_action ="edit";
172 $s_entry = $_GET['id'];
173 }
175 $s_entry = preg_replace("/_.$/","",$s_entry);
178 /* handle C&P from layers menu */
179 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
180 $s_action = "copy_multiple";
181 }
182 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
183 $s_action = "cut_multiple";
184 }
185 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
186 $s_action = "editPaste";
187 }
189 /* Create options */
190 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "appl_new"){
191 $s_action = "new";
192 }
194 /* handle remove from layers menu */
195 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
196 $s_action = "del_multiple";
197 }
199 /****************
200 Copy & Paste handling
201 ****************/
203 /* Display the copy & paste dialog, if it is currently open */
204 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
205 if($ret){
206 return($ret);
207 }
209 /****************
210 Create a new app
211 ****************/
213 /* New application? */
214 if ($s_action=="new"){
216 /* By default we set 'dn' to 'new', all relevant plugins will
217 react on this. */
218 $this->dn= "new";
220 /* Create new usertab object */
221 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
222 $this->apptabs->parent = &$this;
223 $this->apptabs->set_acl_base($this->app_base);
224 }
227 /****************
228 Edit entry canceled
229 ****************/
231 /* Cancel dialogs */
232 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
233 $this->remove_lock();
234 $this->apptabs= NULL;
235 session::un_set('objectinfo');
236 }
239 /****************
240 Edit entry finished
241 ****************/
243 /* Finish apps edit is triggered by the tabulator dialog, so
244 the user wants to save edited data. Check and save at this
245 point. */
246 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
248 /* Check tabs, will feed message array */
249 $this->apptabs->last= $this->apptabs->current;
250 $this->apptabs->save_object();
251 $message= $this->apptabs->check();
253 /* Save, or display error message? */
254 if (count($message) == 0){
256 /* Save data data to ldap */
257 $this->apptabs->save();
259 if (!isset($_POST['edit_apply'])){
260 /* Application has been saved successfully, remove lock from
261 LDAP. */
262 if ($this->dn != "new"){
263 $this->remove_lock();
264 }
265 $this->apptabs= NULL;
266 session::un_set('objectinfo');
267 }else{
269 /* Reinitialize tab */
270 if($this->apptabs instanceof tabs){
271 $this->apptabs->re_init();
272 }
273 }
274 } else {
275 /* Ok. There seem to be errors regarding to the tab data,
276 show message and continue as usual. */
277 msg_dialog::displayChecks($message);
278 }
279 }
282 /****************
283 Edit entry
284 ****************/
286 /* User wants to edit data? */
287 if (($s_action=="edit") && (!isset($this->apptabs->config))){
289 /* Get 'dn' from posted 'applist', must be unique */
290 $this->dn= $this->applications[$s_entry]['dn'];
292 /* Check locking, save current plugin in 'back_plugin', so
293 the dialog knows where to return. */
294 if (($user= get_lock($this->dn)) != ""){
295 return(gen_locked_message ($user, $this->dn));
296 }
298 /* Lock the current entry, so everyone will get the
299 above dialog */
300 add_lock ($this->dn, $this->ui->dn);
302 /* Register apptabs to trigger edit dialog */
303 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
304 $this->apptabs->parent = &$this;
305 $this->apptabs->set_acl_base($this->dn);
306 session::set('objectinfo',$this->dn);
307 }
311 /********************
312 Delete MULTIPLE entries requested, display confirm dialog
313 ********************/
315 if ($s_action=="del_multiple"){
316 $ids = $this->list_get_selected_items();
318 if(count($ids)){
319 $this->dns = array();
321 $disallowed = array();
322 foreach($ids as $id){
323 $dn = $this->applications[$id]['dn'];
324 $acl = $this->ui->get_permissions($dn, "application/application");
325 if(preg_match("/d/",$acl)){
326 $this->dns[$id] = $dn;
327 }else{
328 $disallowed[] = $dn;
329 }
330 }
332 if(count($disallowed)){
333 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
334 }
336 if(count($this->dns)){
338 if ($user= get_multiple_locks($this->dns)){
339 return(gen_locked_message($user,$this->dns));
340 }
342 $dns_names = array();
343 foreach($this->dns as $dn){
344 add_lock ($dn, $this->ui->dn);
345 $dns_names[] =@LDAP::fix($dn);
346 }
348 /* Lock the current entry, so nobody will edit it during deletion */
349 $smarty->assign("intro", msgPool::deleteInfo($dns_names,_("application")));
350 $smarty->assign("multiple", true);
351 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
352 }
353 }
354 }
357 /********************
358 Delete MULTIPLE entries confirmed
359 ********************/
361 /* Confirmation for deletion has been passed. Users should be deleted. */
362 if (isset($_POST['delete_multiple_application_confirm'])){
364 /* Remove user by user and check acls before removeing them */
365 foreach($this->dns as $key => $dn){
367 $ui = get_userinfo();
368 $acl = $ui->get_permissions($dn ,"application/application");
369 if (preg_match('/d/', $acl)){
371 /* Delete request is permitted, perform LDAP action */
372 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
373 $this->apptabs->parent = &$this;
374 $this->apptabs->set_acl_base($dn);
375 $this->apptabs->delete ();
376 unset ($this->apptabs);
377 $this->apptabs= NULL;
379 } else {
380 /* Normally this shouldn't be reached, send some extra
381 logs to notify the administrator */
382 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
383 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
384 }
385 }
387 /* Remove lock file after successfull deletion */
388 $this->remove_lock();
389 $this->dns = array();
390 }
393 /********************
394 Delete MULTIPLE entries Canceled
395 ********************/
397 /* Remove lock */
398 if(isset($_POST['delete_multiple_application_cancel'])){
400 /* Remove lock file after successfull deletion */
401 $this->remove_lock();
402 $this->dns = array();
403 }
405 /****************
406 Delete app
407 ****************/
409 /* Remove user was requested */
410 if ($s_action == "del"){
412 /* Get 'dn' from posted 'uid' */
413 $this->dn= $this->applications[$s_entry]['dn'];
415 /* Load permissions for selected 'dn' and check if
416 we're allowed to remove this 'dn' */
417 $ui = get_userinfo();
418 $acl = $ui->get_permissions($this->dn ,"application/application");
420 if(preg_match("/d/",$acl)){
421 /* Check locking, save current plugin in 'back_plugin', so
422 the dialog knows where to return. */
423 if (($user= get_lock($this->dn)) != ""){
424 return (gen_locked_message ($user, $this->dn));
425 }
427 /* Lock the current entry, so nobody will edit it during deletion */
428 add_lock ($this->dn, $this->ui->dn);
429 $smarty= get_smarty();
430 $smarty->assign("intro",msgPool::deleteInfo(@LDAP::fix($this->dn),_("application")));
431 $smarty->assign("multiple", false);
432 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
433 } else {
435 /* Obviously the user isn't allowed to delete. Show message and
436 clean session. */
437 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
438 }
439 }
442 /****************
443 Delete app confirmed
444 ****************/
446 /* Confirmation for deletion has been passed. Group should be deleted. */
447 if (isset($_POST['delete_app_confirm'])){
449 /* Some nice guy may send this as POST, so we've to check
450 for the permissions again. */
451 $ui = get_userinfo();
452 $acl = $ui->get_permissions($this->dn ,"application/application");
454 if(preg_match("/d/",$acl)){
456 /* Delete request is permitted, perform LDAP action */
457 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
458 $this->apptabs->parent = &$this;
459 $this->apptabs->set_acl_base($this->dn);
460 $this->apptabs->delete ();
461 unset ($this->apptabs);
462 $this->apptabs= NULL;
464 } else {
466 /* Normally this shouldn't be reached, send some extra
467 logs to notify the administrator */
468 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
469 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
470 }
472 /* Remove lock file after successfull deletion */
473 $this->remove_lock();
474 }
477 /****************
478 Delete app canceled
479 ****************/
481 /* Delete application canceled? */
482 if (isset($_POST['delete_cancel'])){
483 $this->remove_lock();
484 session::un_set('objectinfo');
485 }
487 /* Show tab dialog if object is present */
488 if (($this->apptabs) && (isset($this->apptabs->config))){
489 $display= $this->apptabs->execute();
491 /* Don't show buttons if tab dialog requests this */
492 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
493 $display.= "<p style=\"text-align:right\">\n";
494 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
495 $display.= " \n";
496 if ($this->dn != "new"){
497 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
498 $display.= " \n";
499 }
500 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
501 $display.= "</p>";
502 }
503 return ($display);
504 }
507 /****************
508 Dialog display
509 ****************/
511 /* Check if there is a snapshot dialog open */
512 if($this->IsReleaseManagementActivated()){
513 $base = $this->app_release;
514 }else{
515 $base = $this->app_base;
516 }
517 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
518 return($str);
519 }
521 /* Display dialog with system list */
522 $this->DivListApplication->parent = $this;
523 $this->DivListApplication->execute();
524 if(!$this->IsReleaseManagementActivated()){
525 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
526 }
527 $this->reload();
528 $this->DivListApplication->setEntries($this->applications);
529 return($this->DivListApplication->Draw());
530 }
533 /* Return departments, that will be included within snapshot detection */
534 function get_used_snapshot_bases()
535 {
536 if($this->IsReleaseManagementActivated()){
537 return(array($this->app_release));
538 }else{
539 return(array($this->app_base));
540 }
541 }
544 function reload()
545 {
546 $this->applications= array();
548 /* Set base for all searches */
549 $Regex = $this->DivListApplication->Regex;
550 $SubSearch = $this->DivListApplication->SubSearch;
551 $Flags = GL_NONE | GL_SIZELIMIT;
552 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
553 $tmp = array();
555 if(!$this->IsReleaseManagementActivated()){
556 $use_base = $this->app_base;
557 if($SubSearch){
558 $use_base = preg_replace("/^".normalizePreg(get_ou("applicationou"))."/","",$use_base);
559 }
560 }else{
561 $use_base = $this->app_release;
562 $SubSearch= FALSE;
563 }
565 if($SubSearch){
566 $res= get_sub_list($Filter, "application",get_ou("applicationou"), $use_base, array("cn","description","dn","objectClass"), $Flags);
567 }else{
568 $res= get_list($Filter, "application",$use_base, array("cn","description","dn","objectClass"), $Flags);
569 }
570 $tmp2 = array();
571 foreach ($res as $val){
572 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
573 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
574 }
576 natcasesort($tmp2);
577 $this->applications=array();
578 foreach($tmp2 as $val){
579 $this->applications[]=$tmp[$val];
580 }
581 reset ($this->applications);
582 }
584 function remove_from_parent()
585 {
586 /* Optionally execute a command after we're done */
587 $this->postremove();
588 }
591 function copyPasteHandling_from_queue($s_action,$s_entry)
592 {
593 /* Check if Copy & Paste is disabled */
594 if(!is_object($this->CopyPasteHandler)){
595 return("");
596 }
598 $ui = get_userinfo();
600 /* Add a single entry to queue */
601 if($s_action == "cut" || $s_action == "copy"){
603 /* Cleanup object queue */
604 $this->CopyPasteHandler->cleanup_queue();
605 $dn = $this->applications[$s_entry]['dn'];
606 if($s_action == "copy" && preg_match("/r/",$ui->has_complete_category_acls($dn,"application"))){
607 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
608 }
609 if($s_action == "cut" && preg_match("/(r.*d|d.*r)/",$ui->has_complete_category_acls($dn,"application"))){
610 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
611 }
612 }
615 /* Add entries to queue */
616 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
618 /* Cleanup object queue */
619 $this->CopyPasteHandler->cleanup_queue();
621 /* Add new entries to CP queue */
622 foreach($this->list_get_selected_items() as $id){
623 $dn = $this->applications[$id]['dn'];
625 if($s_action == "copy_multiple" && preg_match("/r/",$ui->has_complete_category_acls($dn,"application"))){
626 $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
627 }
628 if($s_action == "cut_multiple" && preg_match("/(r.*d|d.*r)/",$ui->has_complete_category_acls($dn,"application"))){
629 $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
630 }
631 }
632 }
634 /* Start pasting entries */
635 if($s_action == "editPaste"){
636 $this->start_pasting_copied_objects = TRUE;
637 }
640 /* Return C&P dialog */
641 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
644 /* Get dialog */
645 $this->CopyPasteHandler->SetVar("base",$this->app_base);
646 $this->CopyPasteHandler->SetVar("parent",$this);
647 $data = $this->CopyPasteHandler->execute();
649 /* Return dialog data */
650 if(!empty($data)){
651 return($data);
652 }
653 }
655 /* Automatically disable status for pasting */
656 if(!$this->CopyPasteHandler->entries_queued()){
657 $this->start_pasting_copied_objects = FALSE;
658 }
659 return("");
660 }
663 function list_get_selected_items()
664 {
665 $ids = array();
666 foreach($_POST as $name => $value){
667 if(preg_match("/^item_selected_[0-9]*$/",$name)){
668 $id = preg_replace("/^item_selected_/","",$name);
669 $ids[$id] = $id;
670 }
671 }
672 return($ids);
673 }
676 /* Save to LDAP */
677 function save()
678 {
679 /* Optionally execute a command after we're done */
680 $this->postcreate();
681 }
684 function remove_lock()
685 {
686 if (isset($this->apptabs->dn)){
687 del_lock ($this->apptabs->dn);
688 }
689 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
690 del_lock($this->dn);
691 }
692 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
693 del_lock($this->dns);
694 }
695 }
698 function save_object()
699 {
700 $this->DivListApplication->save_object();
701 if(is_object($this->CopyPasteHandler)){
702 $this->CopyPasteHandler->save_object();
703 }
705 if($this->IsReleaseManagementActivated() && isset($_POST['app_release'])){
706 $sel_rel = get_post('app_release');
707 $releases = array_flip($this->getReleases());
708 if(isset($releases[$sel_rel])){
709 $this->app_release = $releases[$sel_rel];
710 }
711 $app_filter = session::get("app_filter");
712 $app_filter['app_release'] = $this->app_release;
713 session::set("app_filter",$app_filter);
714 }elseif(!$this->IsReleaseManagementActivated()){
715 $this->app_base = get_ou("applicationou").$this->DivListApplication->selectedBase;
716 $app_filter = session::get("app_filter");
717 $app_filter['app_base'] = $this->app_base;
718 session::set("app_filter",$app_filter);
719 }
720 }
722 function check() {}
723 function adapt_from_template($dn, $skip= array()) {}
724 function password_change_needed() {}
725 }
726 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
727 ?>