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","ENABLECOPYPASTE")){
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("applicationou").$this->config->current['BASE'];
79 if(!session::is_set("app_filter")){
81 /* Set intial release */
82 $rel = $config->search("faiManagement","DEFAULT_RELEASE",array("menu"));
83 if(empty($rel)){
84 $rel = $this->app_base;
85 }
86 session::set("app_filter",array("app_base" => $rel));
87 }
88 $app_filter = session::get("app_filter");
89 $this->app_base = $app_filter['app_base'];
90 }else{
91 $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
92 if(!session::is_set("app_filter")){
93 session::set("app_filter",array("app_base" => $this->app_base,"app_release" => $this->app_base));
94 }
95 $app_filter = session::get("app_filter");
96 $this->app_base = $app_filter['app_base'];
97 $this->app_release = $app_filter['app_release'];
98 }
99 }
102 function getReleases()
103 {
104 $ldap = $this->config->get_ldap_link();
105 $ret = array();
106 $base = $this->app_base;
108 $ret[$this->app_base] = "/";
109 $ldap->cd($base);
110 $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou"));
111 while($attrs = $ldap->fetch()){
112 $str = str_replace($base,"",$attrs['dn']);
113 $tmp = array_reverse( split("ou=",$str));
114 $str = "";
115 foreach($tmp as $val){
116 $val = trim(preg_replace("/,/","",$val));
117 if(empty($val)) break;
118 $str .= "/".$val;
119 }
120 if(!empty($str)){
121 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
122 }
123 }
124 asort($ret);
125 return($ret);
126 }
128 function execute()
129 {
130 /* Call parent execute */
131 plugin::execute();
134 /****************
135 Variable init
136 ****************/
138 /* These vars will be stored if you try to open a locked app,
139 to be able to perform your last requests after showing a warning message */
140 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/","/^menu_action/"));
142 $smarty = get_smarty(); // Smarty instance
143 $s_action = ""; // Contains the action to proceed
144 $s_entry = ""; // The value for s_action
145 $base_back = ""; // The Link for Backbutton
147 /* Test Posts */
148 foreach($_POST as $key => $val){
149 // Post for delete
150 if(preg_match("/appl_del.*/",$key)){
151 $s_action = "del";
152 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
153 // Post for edit
154 }elseif(preg_match("/appl_edit_.*/",$key)){
155 $s_action="edit";
156 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
157 // Post for new
158 }elseif(preg_match("/^copy_.*/",$key)){
159 $s_action="copy";
160 $s_entry = preg_replace("/^copy_/i","",$key);
161 }elseif(preg_match("/^cut_.*/",$key)){
162 $s_action="cut";
163 $s_entry = preg_replace("/^cut_/i","",$key);
164 // Post for new
165 }elseif(preg_match("/^appl_new.*/",$key)){
166 $s_action="new";
167 }elseif(preg_match("/^remove_multiple_applications/",$key)){
168 $s_action="del_multiple";
169 }elseif(preg_match("/^editPaste.*/i",$key)){
170 $s_action="editPaste";
171 }elseif(preg_match("/^multiple_copy_groups/",$key)){
172 $s_action = "copy_multiple";
173 }elseif(preg_match("/^multiple_cut_groups/",$key)){
174 $s_action = "cut_multiple";
175 }
176 }
178 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
179 $s_action ="edit";
180 $s_entry = $_GET['id'];
181 }
183 $s_entry = preg_replace("/_.$/","",$s_entry);
186 /* handle C&P from layers menu */
187 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
188 $s_action = "copy_multiple";
189 }
190 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
191 $s_action = "cut_multiple";
192 }
193 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
194 $s_action = "editPaste";
195 }
197 /* Create options */
198 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "appl_new"){
199 $s_action = "new";
200 }
202 /* handle remove from layers menu */
203 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
204 $s_action = "del_multiple";
205 }
207 /****************
208 Copy & Paste handling
209 ****************/
211 /* Display the copy & paste dialog, if it is currently open */
212 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
213 if($ret){
214 return($ret);
215 }
217 /****************
218 Create a new app
219 ****************/
221 /* New application? */
222 if ($s_action=="new"){
224 /* By default we set 'dn' to 'new', all relevant plugins will
225 react on this. */
226 $this->dn= "new";
228 /* Create new usertab object */
229 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
230 $this->apptabs->parent = &$this;
231 $this->apptabs->set_acl_base($this->app_base);
232 }
235 /****************
236 Edit entry canceled
237 ****************/
239 /* Cancel dialogs */
240 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
241 $this->remove_lock();
242 $this->apptabs= NULL;
243 session::un_set('objectinfo');
244 }
247 /****************
248 Edit entry finished
249 ****************/
251 /* Finish apps edit is triggered by the tabulator dialog, so
252 the user wants to save edited data. Check and save at this
253 point. */
254 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
256 /* Check tabs, will feed message array */
257 $this->apptabs->last= $this->apptabs->current;
258 $this->apptabs->save_object();
259 $message= $this->apptabs->check();
261 /* Save, or display error message? */
262 if (count($message) == 0){
264 /* Save data data to ldap */
265 $this->apptabs->save();
267 if (!isset($_POST['edit_apply'])){
268 /* Application has been saved successfully, remove lock from
269 LDAP. */
270 if ($this->dn != "new"){
271 $this->remove_lock();
272 }
273 $this->apptabs= NULL;
274 session::un_set('objectinfo');
275 }else{
277 /* Reinitialize tab */
278 if($this->apptabs instanceof tabs){
279 $this->apptabs->re_init();
280 }
281 }
282 } else {
283 /* Ok. There seem to be errors regarding to the tab data,
284 show message and continue as usual. */
285 msg_dialog::displayChecks($message);
286 }
287 }
290 /****************
291 Edit entry
292 ****************/
294 /* User wants to edit data? */
295 if (($s_action=="edit") && (!isset($this->apptabs->config))){
297 /* Get 'dn' from posted 'applist', must be unique */
298 $this->dn= $this->applications[$s_entry]['dn'];
300 /* Check locking, save current plugin in 'back_plugin', so
301 the dialog knows where to return. */
302 if (($user= get_lock($this->dn)) != ""){
303 return(gen_locked_message ($user, $this->dn));
304 }
306 /* Lock the current entry, so everyone will get the
307 above dialog */
308 add_lock ($this->dn, $this->ui->dn);
310 /* Register apptabs to trigger edit dialog */
311 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
312 if($this->IsReleaseManagementActivated()){
313 $this->apptabs->set_FAIstate($this->applications[$s_entry]['FAIstate'][0]);
314 }
315 $this->apptabs->parent = &$this;
316 $this->apptabs->set_acl_base($this->dn);
317 session::set('objectinfo',$this->dn);
318 }
322 /********************
323 Delete MULTIPLE entries requested, display confirm dialog
324 ********************/
326 if ($s_action=="del_multiple"){
327 $ids = $this->list_get_selected_items();
329 if(count($ids)){
330 $this->dns = array();
332 $disallowed = array();
333 foreach($ids as $id){
334 $dn = $this->applications[$id]['dn'];
335 $acl = $this->ui->get_permissions($dn, "application/application");
336 if(preg_match("/d/",$acl)){
337 $this->dns[$id] = $dn;
338 }else{
339 $disallowed[] = $dn;
340 }
341 }
343 if(count($disallowed)){
344 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
345 }
347 if(count($this->dns)){
349 if ($user= get_multiple_locks($this->dns)){
350 return(gen_locked_message($user,$this->dns));
351 }
353 $dns_names = array();
354 foreach($this->dns as $dn){
355 add_lock ($dn, $this->ui->dn);
356 $dns_names[] =@LDAP::fix($dn);
357 }
359 /* Lock the current entry, so nobody will edit it during deletion */
360 $smarty->assign("intro", msgPool::deleteInfo($dns_names,_("application")));
361 $smarty->assign("multiple", true);
362 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
363 }
364 }
365 }
368 /********************
369 Delete MULTIPLE entries confirmed
370 ********************/
372 /* Confirmation for deletion has been passed. Users should be deleted. */
373 if (isset($_POST['delete_multiple_application_confirm'])){
375 /* Remove user by user and check acls before removeing them */
376 foreach($this->dns as $key => $dn){
378 $ui = get_userinfo();
379 $acl = $ui->get_permissions($dn ,"application/application");
380 if (preg_match('/d/', $acl)){
382 /* Delete request is permitted, perform LDAP action */
383 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
384 $this->apptabs->parent = &$this;
385 $this->apptabs->set_acl_base($dn);
386 $this->apptabs->delete ();
387 unset ($this->apptabs);
388 $this->apptabs= NULL;
390 } else {
391 /* Normally this shouldn't be reached, send some extra
392 logs to notify the administrator */
393 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
394 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
395 }
396 }
398 /* Remove lock file after successfull deletion */
399 $this->remove_lock();
400 $this->dns = array();
401 }
404 /********************
405 Delete MULTIPLE entries Canceled
406 ********************/
408 /* Remove lock */
409 if(isset($_POST['delete_multiple_application_cancel'])){
411 /* Remove lock file after successfull deletion */
412 $this->remove_lock();
413 $this->dns = array();
414 }
416 /****************
417 Delete app
418 ****************/
420 /* Remove user was requested */
421 if ($s_action == "del"){
423 /* Get 'dn' from posted 'uid' */
424 $this->dn= $this->applications[$s_entry]['dn'];
426 /* Load permissions for selected 'dn' and check if
427 we're allowed to remove this 'dn' */
428 $ui = get_userinfo();
429 $acl = $ui->get_permissions($this->dn ,"application/application");
431 if(preg_match("/d/",$acl)){
432 /* Check locking, save current plugin in 'back_plugin', so
433 the dialog knows where to return. */
434 if (($user= get_lock($this->dn)) != ""){
435 return (gen_locked_message ($user, $this->dn));
436 }
438 /* Lock the current entry, so nobody will edit it during deletion */
439 add_lock ($this->dn, $this->ui->dn);
440 $smarty= get_smarty();
441 $smarty->assign("intro",msgPool::deleteInfo(@LDAP::fix($this->dn),_("application")));
442 $smarty->assign("multiple", false);
443 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
444 } else {
446 /* Obviously the user isn't allowed to delete. Show message and
447 clean session. */
448 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
449 }
450 }
453 /****************
454 Delete app confirmed
455 ****************/
457 /* Confirmation for deletion has been passed. Group should be deleted. */
458 if (isset($_POST['delete_app_confirm'])){
460 /* Some nice guy may send this as POST, so we've to check
461 for the permissions again. */
462 $ui = get_userinfo();
463 $acl = $ui->get_permissions($this->dn ,"application/application");
465 if(preg_match("/d/",$acl)){
467 /* Delete request is permitted, perform LDAP action */
468 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
469 $this->apptabs->parent = &$this;
470 $this->apptabs->set_acl_base($this->dn);
471 $this->apptabs->delete ();
472 unset ($this->apptabs);
473 $this->apptabs= NULL;
475 } else {
477 /* Normally this shouldn't be reached, send some extra
478 logs to notify the administrator */
479 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
480 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
481 }
483 /* Remove lock file after successfull deletion */
484 $this->remove_lock();
485 }
488 /****************
489 Delete app canceled
490 ****************/
492 /* Delete application canceled? */
493 if (isset($_POST['delete_cancel'])){
494 $this->remove_lock();
495 session::un_set('objectinfo');
496 }
498 /* Show tab dialog if object is present */
499 if (($this->apptabs) && (isset($this->apptabs->config))){
500 $display= $this->apptabs->execute();
502 /* Don't show buttons if tab dialog requests this */
503 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
504 $display.= "<p style=\"text-align:right\">\n";
506 if(isset($this->apptabs->FAIstate) && !preg_match("/freeze/i",$this->apptabs->FAIstate)){
507 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
508 $display.= " \n";
509 if ($this->dn != "new"){
510 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
511 $display.= " \n";
512 }
513 }
514 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
515 $display.= "</p>";
516 }
517 return ($display);
518 }
521 /****************
522 Dialog display
523 ****************/
525 /* Check if there is a snapshot dialog open */
526 if($this->IsReleaseManagementActivated()){
527 $base = $this->app_release;
528 }else{
529 $base = $this->app_base;
530 }
531 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
532 return($str);
533 }
535 /* Display dialog with system list */
536 $this->DivListApplication->parent = $this;
537 $this->DivListApplication->execute();
538 if(!$this->IsReleaseManagementActivated()){
539 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
540 }
541 $this->reload();
542 $this->DivListApplication->setEntries($this->applications);
543 return($this->DivListApplication->Draw());
544 }
547 /* Return departments, that will be included within snapshot detection */
548 function get_used_snapshot_bases()
549 {
550 if($this->IsReleaseManagementActivated()){
551 return(array($this->app_release));
552 }else{
553 return(array($this->app_base));
554 }
555 }
558 function reload()
559 {
560 $this->applications= array();
562 /* Set base for all searches */
563 $Regex = $this->DivListApplication->Regex;
564 $SubSearch = $this->DivListApplication->SubSearch;
565 $Flags = GL_NONE | GL_SIZELIMIT;
566 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
567 $tmp = array();
569 if(!$this->IsReleaseManagementActivated()){
570 $use_base = $this->app_base;
571 if($SubSearch){
572 $use_base = preg_replace("/^".normalizePreg(get_ou("applicationou"))."/","",$use_base);
573 }
574 }else{
575 $use_base = $this->app_release;
576 $SubSearch= FALSE;
577 }
579 /* Add FAIstate to the search attributes */
580 $search_attrs = array("cn","description","dn","objectClass");
581 if($this->IsReleaseManagementActivated()) {
582 $search_attrs[] = "FAIstate";
583 }
585 if($SubSearch){
586 $res= get_sub_list($Filter, "application",get_ou("applicationou"), $use_base, $search_attrs, $Flags);
587 }else{
588 $res= get_list($Filter, "application",$use_base, $search_attrs, $Flags);
589 }
590 $tmp2 = array();
591 foreach ($res as $val){
592 if(!isset($val['FAIstate'])){
593 $val['FAIstate'][0] = "";
594 }
595 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
596 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
597 }
599 natcasesort($tmp2);
600 $this->applications=array();
601 foreach($tmp2 as $val){
602 $this->applications[]=$tmp[$val];
603 }
604 reset ($this->applications);
605 }
607 function remove_from_parent()
608 {
609 /* Optionally execute a command after we're done */
610 $this->postremove();
611 }
614 function copyPasteHandling_from_queue($s_action,$s_entry)
615 {
616 /* Check if Copy & Paste is disabled */
617 if(!is_object($this->CopyPasteHandler)){
618 return("");
619 }
621 $ui = get_userinfo();
623 /* Add a single entry to queue */
624 if($s_action == "cut" || $s_action == "copy"){
626 /* Cleanup object queue */
627 $this->CopyPasteHandler->cleanup_queue();
628 $dn = $this->applications[$s_entry]['dn'];
629 if($s_action == "copy" && $ui->is_copyable($dn,"application","application")){
630 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
631 }
632 if($s_action == "cut" && $ui->is_cutable($dn,"application","application")){
633 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
634 }
635 }
638 /* Add entries to queue */
639 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
641 /* Cleanup object queue */
642 $this->CopyPasteHandler->cleanup_queue();
644 /* Add new entries to CP queue */
645 foreach($this->list_get_selected_items() as $id){
646 $dn = $this->applications[$id]['dn'];
648 if($s_action == "copy_multiple" && $ui->is_copyable($dn,"application","application")){
649 $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
650 }
651 if($s_action == "cut_multiple" && $ui->is_cutable($dn,"application","application")){
652 $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
653 }
654 }
655 }
657 /* Start pasting entries */
658 if($s_action == "editPaste"){
659 $this->start_pasting_copied_objects = TRUE;
660 }
663 /* Return C&P dialog */
664 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
667 /* Get dialog */
668 $this->CopyPasteHandler->SetVar("base",$this->app_base);
669 $this->CopyPasteHandler->SetVar("parent",$this);
670 $data = $this->CopyPasteHandler->execute();
672 /* Return dialog data */
673 if(!empty($data)){
674 return($data);
675 }
676 }
678 /* Automatically disable status for pasting */
679 if(!$this->CopyPasteHandler->entries_queued()){
680 $this->start_pasting_copied_objects = FALSE;
681 }
682 return("");
683 }
686 function list_get_selected_items()
687 {
688 $ids = array();
689 foreach($_POST as $name => $value){
690 if(preg_match("/^item_selected_[0-9]*$/",$name)){
691 $id = preg_replace("/^item_selected_/","",$name);
692 $ids[$id] = $id;
693 }
694 }
695 return($ids);
696 }
699 /* Save to LDAP */
700 function save()
701 {
702 /* Optionally execute a command after we're done */
703 $this->postcreate();
704 }
707 function remove_lock()
708 {
709 if (isset($this->apptabs->dn)){
710 del_lock ($this->apptabs->dn);
711 }
712 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
713 del_lock($this->dn);
714 }
715 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
716 del_lock($this->dns);
717 }
718 }
721 function save_object()
722 {
723 $this->DivListApplication->save_object();
724 if(is_object($this->CopyPasteHandler)){
725 $this->CopyPasteHandler->save_object();
726 }
728 if($this->IsReleaseManagementActivated() && isset($_POST['app_release'])){
729 $sel_rel = get_post('app_release');
730 $releases = array_flip($this->getReleases());
731 if(isset($releases[$sel_rel])){
732 $this->app_release = $releases[$sel_rel];
733 }
734 $app_filter = session::get("app_filter");
735 $app_filter['app_release'] = $this->app_release;
736 session::set("app_filter",$app_filter);
737 }elseif(!$this->IsReleaseManagementActivated()){
738 $this->app_base = get_ou("applicationou").$this->DivListApplication->selectedBase;
739 $app_filter = session::get("app_filter");
740 $app_filter['app_base'] = $this->app_base;
741 session::set("app_filter",$app_filter);
742 }
743 }
745 function check() {}
746 function adapt_from_template($dn, $skip= array()) {}
747 function password_change_needed() {}
748 }
749 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
750 ?>