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";
26 var $plIcon = "plugins/goto/images/application.png";
28 /* Dialog attributes */
29 var $apptabs = NULL;
30 var $ui = NULL;
31 var $CopyPasteHandler = NULL;
32 var $DivListApplication = NULL;
33 var $applications = array();
34 var $enableReleaseManagement = false;
35 var $start_pasting_copied_objects = FALSE;
37 var $app_base ="";
38 var $app_release ="";
39 var $acl_module = array("application");
41 var $dns = array();
43 function IsReleaseManagementActivated()
44 {
45 /* Check if we should enable the release selection */
46 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
47 if(!empty($tmp)){
48 return(true);
49 }
50 return(false);
51 }
54 function applicationManagement (&$config, &$ui)
55 {
56 /* Save configuration for internal use */
57 $this->config = &$config;
58 $this->ui = &$ui;
60 /* Check if copy & paste is activated */
61 if($this->config->boolValueIsTrue("MAIN","COPYPASTE")){
62 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
63 }
65 /* Creat dialog object */
66 $this->DivListApplication = new divListApplication($this->config,$this);
68 if($this->IsReleaseManagementActivated()){
69 /* Check if we should enable the release selection */
70 $this->enableReleaseManagement = true;
72 /* Hide SubSearch checkbox */
73 $this->DivListApplication->DisableCheckBox("SubSearch");
74 }
76 /* Set default release */
77 if(!$this->IsReleaseManagementActivated()){
78 $this->app_base = get_ou("applicationRDN").$this->config->current['BASE'];
79 if(!session::is_set("app_filter")){
80 session::set("app_filter",array("app_base" => $this->app_base));
81 }
82 $app_filter = session::get("app_filter");
83 }else{
85 /* Set intial release */
86 $this->app_base = get_ou("applicationRDN").$this->config->current['BASE'];
87 $rel = $config->search("faiManagement","DEFAULTFAIRELEASE",array("menu"));
88 $rels = array_flip($this->getReleases());
89 if(isset($rels[$rel])){
90 $rel = $rels[$rel];
91 }else{
92 $rel = $this->app_base;
93 }
95 if(!session::is_set("app_filter")){
96 session::set("app_filter",array("app_base" => $this->app_base,"app_release" => $rel));
97 }
98 $app_filter = session::get("app_filter");
99 $this->app_base = $app_filter['app_base'];
100 $this->app_release = $app_filter['app_release'];
101 }
102 }
105 function getReleases()
106 {
107 $ldap = $this->config->get_ldap_link();
108 $ret = array();
109 $base = $this->app_base;
111 $ret[$this->app_base] = "/";
112 $ldap->cd($base);
113 $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou"));
114 while($attrs = $ldap->fetch()){
115 $str = str_replace($base,"",$attrs['dn']);
116 $tmp = array_reverse( split("ou=",$str));
117 $str = "";
118 foreach($tmp as $val){
119 $val = trim(preg_replace("/,/","",$val));
120 if(empty($val)) break;
121 $str .= "/".$val;
122 }
123 if(!empty($str)){
124 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
125 }
126 }
127 asort($ret);
128 return($ret);
129 }
131 function execute()
132 {
133 /* Call parent execute */
134 plugin::execute();
137 /****************
138 Variable init
139 ****************/
141 /* These vars will be stored if you try to open a locked app,
142 to be able to perform your last requests after showing a warning message */
143 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/","/^menu_action/"));
145 $smarty = get_smarty(); // Smarty instance
146 $s_action = ""; // Contains the action to proceed
147 $s_entry = ""; // The value for s_action
148 $base_back = ""; // The Link for Backbutton
150 /* Test Posts */
151 foreach($_POST as $key => $val){
152 // Post for delete
153 if(preg_match("/appl_del.*/",$key)){
154 $s_action = "del";
155 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
156 // Post for edit
157 }elseif(preg_match("/appl_edit_.*/",$key)){
158 $s_action="edit";
159 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
160 // Post for new
161 }elseif(preg_match("/^copy_.*/",$key)){
162 $s_action="copy";
163 $s_entry = preg_replace("/^copy_/i","",$key);
164 }elseif(preg_match("/^cut_.*/",$key)){
165 $s_action="cut";
166 $s_entry = preg_replace("/^cut_/i","",$key);
167 // Post for new
168 }elseif(preg_match("/^appl_new.*/",$key)){
169 $s_action="new";
170 }elseif(preg_match("/^remove_multiple_applications/",$key)){
171 $s_action="del_multiple";
172 }elseif(preg_match("/^editPaste.*/i",$key)){
173 $s_action="editPaste";
174 }elseif(preg_match("/^multiple_copy_groups/",$key)){
175 $s_action = "copy_multiple";
176 }elseif(preg_match("/^multiple_cut_groups/",$key)){
177 $s_action = "cut_multiple";
178 }
179 }
181 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
182 $s_action ="edit";
183 $s_entry = $_GET['id'];
184 }
186 $s_entry = preg_replace("/_.$/","",$s_entry);
189 /* handle C&P from layers menu */
190 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
191 $s_action = "copy_multiple";
192 }
193 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
194 $s_action = "cut_multiple";
195 }
196 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
197 $s_action = "editPaste";
198 }
200 /* Create options */
201 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "appl_new"){
202 $s_action = "new";
203 }
205 /* handle remove from layers menu */
206 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
207 $s_action = "del_multiple";
208 }
210 /****************
211 Copy & Paste handling
212 ****************/
214 /* Display the copy & paste dialog, if it is currently open */
215 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
216 if($ret){
217 return($ret);
218 }
220 /****************
221 Create a new app
222 ****************/
224 /* New application? */
225 if ($s_action=="new"){
227 /* By default we set 'dn' to 'new', all relevant plugins will
228 react on this. */
229 $this->dn= "new";
231 /* Create new usertab object */
232 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
233 $this->apptabs->parent = &$this;
234 $this->apptabs->set_acl_base($this->app_base);
235 }
238 /****************
239 Edit entry canceled
240 ****************/
242 /* Cancel dialogs */
243 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
244 $this->remove_lock();
245 $this->apptabs= NULL;
246 session::un_set('objectinfo');
247 }
250 /****************
251 Edit entry finished
252 ****************/
254 /* Finish apps edit is triggered by the tabulator dialog, so
255 the user wants to save edited data. Check and save at this
256 point. */
257 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
259 /* Check tabs, will feed message array */
260 $this->apptabs->last= $this->apptabs->current;
261 $this->apptabs->save_object();
262 $message= $this->apptabs->check();
264 /* Save, or display error message? */
265 if (count($message) == 0){
267 /* Save data data to ldap */
268 $this->apptabs->save();
270 if (!isset($_POST['edit_apply'])){
271 /* Application has been saved successfully, remove lock from
272 LDAP. */
273 if ($this->dn != "new"){
274 $this->remove_lock();
275 }
276 $this->apptabs= NULL;
277 session::un_set('objectinfo');
278 }else{
280 /* Reinitialize tab */
281 if($this->apptabs instanceof tabs){
282 $this->apptabs->re_init();
283 }
284 }
285 } else {
286 /* Ok. There seem to be errors regarding to the tab data,
287 show message and continue as usual. */
288 msg_dialog::displayChecks($message);
289 }
290 }
293 /****************
294 Edit entry
295 ****************/
297 /* User wants to edit data? */
298 if (($s_action=="edit") && (!isset($this->apptabs->config))){
300 /* Get 'dn' from posted 'applist', must be unique */
301 $this->dn= $this->applications[$s_entry]['dn'];
303 /* Check locking, save current plugin in 'back_plugin', so
304 the dialog knows where to return. */
305 if (($user= get_lock($this->dn)) != ""){
306 return(gen_locked_message ($user, $this->dn));
307 }
309 /* Lock the current entry, so everyone will get the
310 above dialog */
311 add_lock ($this->dn, $this->ui->dn);
313 /* Register apptabs to trigger edit dialog */
314 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
315 if($this->IsReleaseManagementActivated()){
316 $this->apptabs->set_FAIstate($this->applications[$s_entry]['FAIstate'][0]);
317 }
318 $this->apptabs->parent = &$this;
319 $this->apptabs->set_acl_base($this->dn);
320 session::set('objectinfo',$this->dn);
321 }
325 /********************
326 Delete MULTIPLE entries requested, display confirm dialog
327 ********************/
329 if ($s_action=="del_multiple"){
330 $ids = $this->list_get_selected_items();
332 if(count($ids)){
333 $this->dns = array();
335 $disallowed = array();
336 foreach($ids as $id){
337 $dn = $this->applications[$id]['dn'];
338 $acl = $this->ui->get_permissions($dn, "application/application");
339 if(preg_match("/d/",$acl)){
340 $this->dns[$id] = $dn;
341 }else{
342 $disallowed[] = $dn;
343 }
344 }
346 if(count($disallowed)){
347 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
348 }
350 if(count($this->dns)){
352 if ($user= get_multiple_locks($this->dns)){
353 return(gen_locked_message($user,$this->dns));
354 }
356 $dns_names = array();
357 foreach($this->dns as $dn){
358 add_lock ($dn, $this->ui->dn);
359 $dns_names[] =@LDAP::fix($dn);
360 }
362 /* Lock the current entry, so nobody will edit it during deletion */
363 $smarty->assign("intro", msgPool::deleteInfo($dns_names,_("application")));
364 $smarty->assign("multiple", true);
365 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
366 }
367 }
368 }
371 /********************
372 Delete MULTIPLE entries confirmed
373 ********************/
375 /* Confirmation for deletion has been passed. Users should be deleted. */
376 if (isset($_POST['delete_multiple_application_confirm'])){
378 /* Remove user by user and check acls before removeing them */
379 foreach($this->dns as $key => $dn){
381 $ui = get_userinfo();
382 $acl = $ui->get_permissions($dn ,"application/application");
383 if (preg_match('/d/', $acl)){
385 /* Delete request is permitted, perform LDAP action */
386 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
387 $this->apptabs->parent = &$this;
388 $this->apptabs->set_acl_base($dn);
389 $this->apptabs->delete ();
390 unset ($this->apptabs);
391 $this->apptabs= NULL;
393 } else {
394 /* Normally this shouldn't be reached, send some extra
395 logs to notify the administrator */
396 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
397 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
398 }
399 }
401 /* Remove lock file after successfull deletion */
402 $this->remove_lock();
403 $this->dns = array();
404 }
407 /********************
408 Delete MULTIPLE entries Canceled
409 ********************/
411 /* Remove lock */
412 if(isset($_POST['delete_multiple_application_cancel'])){
414 /* Remove lock file after successfull deletion */
415 $this->remove_lock();
416 $this->dns = array();
417 }
419 /****************
420 Delete app
421 ****************/
423 /* Remove user was requested */
424 if ($s_action == "del"){
426 /* Get 'dn' from posted 'uid' */
427 $this->dn= $this->applications[$s_entry]['dn'];
429 /* Load permissions for selected 'dn' and check if
430 we're allowed to remove this 'dn' */
431 $ui = get_userinfo();
432 $acl = $ui->get_permissions($this->dn ,"application/application");
434 if(preg_match("/d/",$acl)){
435 /* Check locking, save current plugin in 'back_plugin', so
436 the dialog knows where to return. */
437 if (($user= get_lock($this->dn)) != ""){
438 return (gen_locked_message ($user, $this->dn));
439 }
441 /* Lock the current entry, so nobody will edit it during deletion */
442 add_lock ($this->dn, $this->ui->dn);
443 $smarty= get_smarty();
444 $smarty->assign("intro",msgPool::deleteInfo(@LDAP::fix($this->dn),_("application")));
445 $smarty->assign("multiple", false);
446 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
447 } else {
449 /* Obviously the user isn't allowed to delete. Show message and
450 clean session. */
451 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
452 }
453 }
456 /****************
457 Delete app confirmed
458 ****************/
460 /* Confirmation for deletion has been passed. Group should be deleted. */
461 if (isset($_POST['delete_app_confirm'])){
463 /* Some nice guy may send this as POST, so we've to check
464 for the permissions again. */
465 $ui = get_userinfo();
466 $acl = $ui->get_permissions($this->dn ,"application/application");
468 if(preg_match("/d/",$acl)){
470 /* Delete request is permitted, perform LDAP action */
471 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
472 $this->apptabs->parent = &$this;
473 $this->apptabs->set_acl_base($this->dn);
474 $this->apptabs->delete ();
475 unset ($this->apptabs);
476 $this->apptabs= NULL;
478 } else {
480 /* Normally this shouldn't be reached, send some extra
481 logs to notify the administrator */
482 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
483 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
484 }
486 /* Remove lock file after successfull deletion */
487 $this->remove_lock();
488 }
491 /****************
492 Delete app canceled
493 ****************/
495 /* Delete application canceled? */
496 if (isset($_POST['delete_cancel'])){
497 $this->remove_lock();
498 session::un_set('objectinfo');
499 }
501 /* Show tab dialog if object is present */
502 if (($this->apptabs) && (isset($this->apptabs->config))){
503 $display= $this->apptabs->execute();
505 /* Don't show buttons if tab dialog requests this */
506 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
507 $display.= "<p style=\"text-align:right\">\n";
509 if(isset($this->apptabs->FAIstate) && !preg_match("/freeze/i",$this->apptabs->FAIstate)){
510 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
511 $display.= " \n";
512 if ($this->dn != "new"){
513 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
514 $display.= " \n";
515 }
516 }
517 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
518 $display.= "</p>";
519 }
520 return ($display);
521 }
524 /****************
525 Dialog display
526 ****************/
528 /* Check if there is a snapshot dialog open */
529 if($this->IsReleaseManagementActivated()){
530 $base = $this->app_release;
531 }else{
532 $base = $this->app_base;
533 }
534 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
535 return($str);
536 }
538 /* Display dialog with system list */
539 $this->DivListApplication->parent = $this;
540 $this->DivListApplication->execute();
541 if(!$this->IsReleaseManagementActivated()){
542 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
543 }
544 $this->reload();
545 $this->DivListApplication->setEntries($this->applications);
546 return($this->DivListApplication->Draw());
547 }
550 /* Return departments, that will be included within snapshot detection */
551 function get_used_snapshot_bases()
552 {
553 if($this->IsReleaseManagementActivated()){
554 return(array($this->app_release));
555 }else{
556 return(array($this->app_base));
557 }
558 }
561 function reload()
562 {
563 $this->applications= array();
565 /* Set base for all searches */
566 $Regex = $this->DivListApplication->Regex;
567 $SubSearch = $this->DivListApplication->SubSearch;
568 $Flags = GL_NONE | GL_SIZELIMIT;
569 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
570 $tmp = array();
572 if(!$this->IsReleaseManagementActivated()){
573 $use_base = $this->app_base;
574 if($SubSearch){
575 $use_base = preg_replace("/^".normalizePreg(get_ou("applicationRDN"))."/","",$use_base);
576 }
577 }else{
578 $use_base = $this->app_release;
579 $SubSearch= FALSE;
580 }
582 /* Add FAIstate to the search attributes */
583 $search_attrs = array("cn","description","dn","objectClass");
584 if($this->IsReleaseManagementActivated()) {
585 $search_attrs[] = "FAIstate";
586 }
588 if($SubSearch){
589 $res= get_sub_list($Filter, "application",get_ou("applicationRDN"), $use_base, $search_attrs, $Flags);
590 }else{
591 $res= get_list($Filter, "application",$use_base, $search_attrs, $Flags);
592 }
593 $tmp2 = array();
594 foreach ($res as $val){
595 if(!isset($val['FAIstate'])){
596 $val['FAIstate'][0] = "";
597 }
598 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
599 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
600 }
602 natcasesort($tmp2);
603 $this->applications=array();
604 foreach($tmp2 as $val){
605 $this->applications[]=$tmp[$val];
606 }
607 reset ($this->applications);
608 }
610 function remove_from_parent()
611 {
612 /* Optionally execute a command after we're done */
613 $this->postremove();
614 }
617 function copyPasteHandling_from_queue($s_action,$s_entry)
618 {
619 /* Check if Copy & Paste is disabled */
620 if(!is_object($this->CopyPasteHandler)){
621 return("");
622 }
624 $ui = get_userinfo();
626 /* Add a single entry to queue */
627 if($s_action == "cut" || $s_action == "copy"){
629 /* Cleanup object queue */
630 $this->CopyPasteHandler->cleanup_queue();
631 $dn = $this->applications[$s_entry]['dn'];
632 if($s_action == "copy" && $ui->is_copyable($dn,"application","application")){
633 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
634 }
635 if($s_action == "cut" && $ui->is_cutable($dn,"application","application")){
636 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
637 }
638 }
641 /* Add entries to queue */
642 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
644 /* Cleanup object queue */
645 $this->CopyPasteHandler->cleanup_queue();
647 /* Add new entries to CP queue */
648 foreach($this->list_get_selected_items() as $id){
649 $dn = $this->applications[$id]['dn'];
651 if($s_action == "copy_multiple" && $ui->is_copyable($dn,"application","application")){
652 $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
653 }
654 if($s_action == "cut_multiple" && $ui->is_cutable($dn,"application","application")){
655 $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
656 }
657 }
658 }
660 /* Start pasting entries */
661 if($s_action == "editPaste"){
662 $this->start_pasting_copied_objects = TRUE;
663 }
666 /* Return C&P dialog */
667 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
670 /* Get dialog */
671 $this->CopyPasteHandler->SetVar("base",$this->app_base);
672 $this->CopyPasteHandler->SetVar("parent",$this);
673 $data = $this->CopyPasteHandler->execute();
675 /* Return dialog data */
676 if(!empty($data)){
677 return($data);
678 }
679 }
681 /* Automatically disable status for pasting */
682 if(!$this->CopyPasteHandler->entries_queued()){
683 $this->start_pasting_copied_objects = FALSE;
684 }
685 return("");
686 }
689 function list_get_selected_items()
690 {
691 $ids = array();
692 foreach($_POST as $name => $value){
693 if(preg_match("/^item_selected_[0-9]*$/",$name)){
694 $id = preg_replace("/^item_selected_/","",$name);
695 $ids[$id] = $id;
696 }
697 }
698 return($ids);
699 }
702 /* Save to LDAP */
703 function save()
704 {
705 /* Optionally execute a command after we're done */
706 $this->postcreate();
707 }
710 function remove_lock()
711 {
712 if (isset($this->apptabs->dn)){
713 del_lock ($this->apptabs->dn);
714 }
715 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
716 del_lock($this->dn);
717 }
718 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
719 del_lock($this->dns);
720 }
721 }
724 function save_object()
725 {
726 $this->DivListApplication->save_object();
727 if(is_object($this->CopyPasteHandler)){
728 $this->CopyPasteHandler->save_object();
729 }
731 if($this->IsReleaseManagementActivated() && isset($_POST['app_release'])){
732 $sel_rel = get_post('app_release');
733 $releases = array_flip($this->getReleases());
734 if(isset($releases[$sel_rel])){
735 $this->app_release = $releases[$sel_rel];
736 }
737 $app_filter = session::get("app_filter");
738 $app_filter['app_release'] = $this->app_release;
739 session::set("app_filter",$app_filter);
740 }elseif(!$this->IsReleaseManagementActivated()){
741 $this->app_base = get_ou("applicationRDN").$this->DivListApplication->selectedBase;
742 $app_filter = session::get("app_filter");
743 $app_filter['app_base'] = $this->app_base;
744 session::set("app_filter",$app_filter);
745 }
746 }
748 function check() {}
749 function adapt_from_template($dn, $skip= array()) {}
750 function password_change_needed() {}
751 }
752 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
753 ?>